VCLCommon.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "WinInterface.h"
  5. #include "VCLCommon.h"
  6. #include <Common.h>
  7. #include <TextsWin.h>
  8. #include <RemoteFiles.h>
  9. #include <GUITools.h>
  10. #include <Tools.h>
  11. #include <CustomWinConfiguration.h>
  12. #include <CoreMain.h>
  13. #include <Vcl.StdActns.hpp>
  14. #include <PasswordEdit.hpp>
  15. #include <FileCtrl.hpp>
  16. #include <PathLabel.hpp>
  17. #include <PasTools.hpp>
  18. #include <StrUtils.hpp>
  19. #include <Vcl.Imaging.pngimage.hpp>
  20. #include <Math.hpp>
  21. #include <TB2ExtItems.hpp>
  22. #include <TBXExtItems.hpp>
  23. #include <IEListView.hpp>
  24. #include <WinApi.h>
  25. #include <vssym32.h>
  26. //---------------------------------------------------------------------------
  27. const UnicodeString ContextSeparator(TraceInitStr(L"\x00BB"));
  28. const UnicodeString LinkAppLabelMark(TraceInitStr(UnicodeString(L" ") + ContextSeparator));
  29. //---------------------------------------------------------------------------
  30. #pragma package(smart_init)
  31. //---------------------------------------------------------------------------
  32. void __fastcall FixListColumnWidth(TListView * TListView, int Index)
  33. {
  34. if (Index < 0)
  35. {
  36. Index = TListView->Columns->Count + Index;
  37. }
  38. TListView->Column[Index]->Tag = 1;
  39. }
  40. //---------------------------------------------------------------------------
  41. static int __fastcall GetColumnTextWidth(TListView * ListView, int ColumnPadding, const UnicodeString & Text)
  42. {
  43. return
  44. ColumnPadding +
  45. ListView->Canvas->TextExtent(Text).Width;
  46. }
  47. //---------------------------------------------------------------------------
  48. void DisableRedraw(TWinControl * Control)
  49. {
  50. SendMessage(Control->Handle, WM_SETREDRAW, false, 0);
  51. }
  52. //---------------------------------------------------------------------------
  53. void EnableRedraw(TWinControl * Control)
  54. {
  55. SendMessage(Control->Handle, WM_SETREDRAW, true, 0);
  56. // As recommended by documentation for WM_SETREDRAW.
  57. // Without this, session list on Import dialog stops working after the list is reloaded while visible
  58. // (i.e. when chaing import source)
  59. RedrawWindow(Control->Handle, NULL, NULL, RDW_ERASE | RDW_FRAME | RDW_INVALIDATE | RDW_ALLCHILDREN);
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall AutoSizeListColumnsWidth(TListView * ListView, int ColumnToShrinkIndex)
  63. {
  64. // Preallocate handle to precreate columns, otherwise our changes may get
  65. // overwritten once the handle is created.
  66. // This should actually get called only once the handle are allocated.
  67. // Otherwise we end up recreating the handles,
  68. // what may cause a flicker of the currently focused window title.
  69. ListView->HandleNeeded();
  70. DisableRedraw(ListView);
  71. try
  72. {
  73. int ColumnPadding = 2 * ScaleByTextHeightRunTime(ListView, 6);
  74. int ColumnShrinkMinWidth = ScaleByTextHeightRunTime(ListView, 60);
  75. int ResizableWidth = 0;
  76. int NonResizableWidth = 0;
  77. int LastResizable = -1;
  78. for (int Index = 0; Index < ListView->Columns->Count; Index++)
  79. {
  80. TListColumn * Column = ListView->Columns->Items[Index];
  81. int CaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, Column->Caption);
  82. int OrigWidth = -1;
  83. bool NonResizable = (Column->Tag != 0);
  84. if (NonResizable) // optimization
  85. {
  86. OrigWidth = ListView_GetColumnWidth(ListView->Handle, Index);
  87. }
  88. int Width;
  89. // LVSCW_AUTOSIZE does not work for OwnerData list views
  90. if (!ListView->OwnerData)
  91. {
  92. ListView_SetColumnWidth(ListView->Handle, Index, LVSCW_AUTOSIZE);
  93. Width = ListView_GetColumnWidth(ListView->Handle, Index);
  94. }
  95. else
  96. {
  97. Width = 0;
  98. for (int ItemIndex = 0; ItemIndex < ListView->Items->Count; ItemIndex++)
  99. {
  100. TListItem * Item = ListView->Items->Item[ItemIndex];
  101. UnicodeString Text;
  102. if (Index == 0)
  103. {
  104. Text = Item->Caption;
  105. }
  106. // Particularly EditorListView3 on Preferences dialog does not have all subitems filled for internal editor
  107. else if (Index <= Item->SubItems->Count)
  108. {
  109. Text = Item->SubItems->Strings[Index - 1];
  110. }
  111. int TextWidth = GetColumnTextWidth(ListView, ColumnPadding, Text);
  112. if (TextWidth > Width)
  113. {
  114. Width = TextWidth;
  115. }
  116. }
  117. }
  118. Width = Max(Width, CaptionWidth);
  119. // Never shrink the non-resizable columns
  120. if (NonResizable && (Width < OrigWidth))
  121. {
  122. Width = OrigWidth;
  123. }
  124. Column->Width = Width;
  125. if (NonResizable)
  126. {
  127. NonResizableWidth += Width;
  128. }
  129. else
  130. {
  131. LastResizable = Index;
  132. ResizableWidth += Width;
  133. }
  134. }
  135. DebugAssert(LastResizable >= 0);
  136. int ClientWidth = ListView->ClientWidth;
  137. int RowCount = ListView->Items->Count;
  138. if ((ListView->VisibleRowCount < RowCount) &&
  139. (ListView->Width - ListView->ClientWidth < GetSystemMetricsForControl(ListView, SM_CXVSCROLL)))
  140. {
  141. ClientWidth -= GetSystemMetricsForControl(ListView, SM_CXVSCROLL);
  142. }
  143. if (DebugAlwaysTrue(NonResizableWidth < ClientWidth))
  144. {
  145. int Remaining = ClientWidth - NonResizableWidth;
  146. bool ProportionalResize = true;
  147. bool Shrinking = (Remaining < ResizableWidth);
  148. // If columns are too wide to fit and we have dedicated shrink column, shrink it
  149. if (Shrinking &&
  150. (ColumnToShrinkIndex >= 0))
  151. {
  152. TListColumn * ColumnToShrink = ListView->Columns->Items[ColumnToShrinkIndex];
  153. int ColumnToShrinkCaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, ColumnToShrink->Caption);
  154. int ColumnToShrinkMinWidth = Max(ColumnShrinkMinWidth, ColumnToShrinkCaptionWidth);
  155. // This falls back to proprotional shrinking when the shrink column would fall below min width.
  156. // Question is whether we should not shrink to min width instead.
  157. if ((ResizableWidth - ColumnToShrink->Width) < (Remaining - ColumnToShrinkMinWidth))
  158. {
  159. int ColumnToShrinkWidth = Remaining - (ResizableWidth - ColumnToShrink->Width);
  160. ColumnToShrink->Width = ColumnToShrinkWidth;
  161. ProportionalResize = false;
  162. }
  163. }
  164. if (ProportionalResize)
  165. {
  166. for (int Index = 0; Index <= LastResizable; Index++)
  167. {
  168. TListColumn * Column = ListView->Columns->Items[Index];
  169. if (Column->Tag == 0)
  170. {
  171. int Width = ListView_GetColumnWidth(ListView->Handle, Index);
  172. int AutoWidth = Width;
  173. if (Index < LastResizable)
  174. {
  175. Width = (Remaining * Width) / ResizableWidth;
  176. }
  177. else
  178. {
  179. Width = Remaining;
  180. }
  181. int CaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, Column->Caption);
  182. Width = Max(Width, CaptionWidth);
  183. Width = Max(Width, Min(ColumnShrinkMinWidth, AutoWidth));
  184. Column->Width = Width;
  185. Remaining -= Min(Width, Remaining);
  186. }
  187. }
  188. DebugAssert(Remaining == 0);
  189. }
  190. }
  191. }
  192. __finally
  193. {
  194. EnableRedraw(ListView);
  195. }
  196. }
  197. //---------------------------------------------------------------------------
  198. static void __fastcall SetParentColor(TControl * Control)
  199. {
  200. TColor Color = clBtnFace;
  201. if (UseThemes)
  202. {
  203. bool OnTabSheet = false;
  204. TWinControl * Parent = Control->Parent;
  205. while ((Parent != NULL) && !OnTabSheet)
  206. {
  207. TTabSheet * TabSheet = dynamic_cast<TTabSheet *>(Parent);
  208. OnTabSheet = (TabSheet != NULL) && TabSheet->TabVisible;
  209. Parent = Parent->Parent;
  210. }
  211. if (OnTabSheet)
  212. {
  213. HTHEME Theme = OpenThemeData(NULL, L"tab");
  214. if (Theme != NULL)
  215. {
  216. COLORREF RGB;
  217. // XP with classic theme: Does not get past OpenThemeData, clBtnFace is exact color
  218. // XP with XP theme: not the exact color (probably same as clBtnFace), but close
  219. // Vista - ok
  220. // 2016 without desktop - ok
  221. // 7 with classic and high contrast themes: Do not get past OpenThemeData, clBtnFace is exact color
  222. // 7 with 7 and basic themes - ok
  223. // 10 with high contrast themes - ok (note the difference to 7 with high contract themes)
  224. // 10 - ok
  225. if (GetThemeColor(Theme, TABP_AEROWIZARDBODY, TIS_NORMAL, TMT_FILLCOLOR, &RGB) == S_OK)
  226. {
  227. Color = static_cast<TColor>(RGB);
  228. }
  229. CloseThemeData(Theme);
  230. }
  231. }
  232. }
  233. ((TEdit*)Control)->Color = Color;
  234. }
  235. //---------------------------------------------------------------------------
  236. void __fastcall EnableControl(TControl * Control, bool Enable)
  237. {
  238. if (Control->Enabled != Enable)
  239. {
  240. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  241. if ((WinControl != NULL) &&
  242. (WinControl->ControlCount > 0))
  243. {
  244. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  245. {
  246. EnableControl(WinControl->Controls[Index], Enable);
  247. }
  248. }
  249. Control->Enabled = Enable;
  250. }
  251. if ((dynamic_cast<TCustomEdit *>(Control) != NULL) ||
  252. (dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  253. (dynamic_cast<TCustomListView *>(Control) != NULL) ||
  254. (dynamic_cast<TCustomTreeView *>(Control) != NULL))
  255. {
  256. if (Enable)
  257. {
  258. ((TEdit*)Control)->Color = clWindow;
  259. }
  260. else
  261. {
  262. // This does not work for list view with
  263. // LVS_EX_DOUBLEBUFFER (TCustomDirView).
  264. // It automatically gets gray background.
  265. // Though on Windows 7, the control has to be disabled
  266. // only after it is showing already (see TCustomScpExplorerForm::UpdateControls())
  267. ((TEdit*)Control)->Color = clBtnFace;
  268. }
  269. }
  270. };
  271. //---------------------------------------------------------------------------
  272. void __fastcall DoReadOnlyControl(TControl * Control, bool ReadOnly, bool Color);
  273. //---------------------------------------------------------------------------
  274. void __fastcall ReadOnlyAndEnabledControl(TControl * Control, bool ReadOnly, bool Enabled)
  275. {
  276. // Change color only in only one of EnableControl and DoReadOnlyControl to prevent flicker
  277. if (ReadOnly)
  278. {
  279. DebugAssert(dynamic_cast<TWinControl *>(Control)->ControlCount == 0);
  280. // As EnableControl, but with no color change
  281. Control->Enabled = Enabled;
  282. DoReadOnlyControl(Control, ReadOnly, true);
  283. }
  284. else
  285. {
  286. DoReadOnlyControl(Control, ReadOnly, false);
  287. EnableControl(Control, Enabled);
  288. }
  289. }
  290. //---------------------------------------------------------------------------
  291. static void __fastcall ReadOnlyEditContextPopup(void * /*Data*/, TObject * Sender, const TPoint & MousePos, bool & Handled)
  292. {
  293. TEdit * Edit = static_cast<TEdit *>(Sender);
  294. if (Edit->ReadOnly)
  295. {
  296. MenuPopup(Sender, MousePos, Handled);
  297. }
  298. }
  299. //---------------------------------------------------------------------------
  300. void __fastcall DoReadOnlyControl(TControl * Control, bool ReadOnly, bool Color)
  301. {
  302. if (dynamic_cast<TCustomEdit *>(Control) != NULL)
  303. {
  304. TEdit * Edit = static_cast<TEdit *>(Control);
  305. Edit->ReadOnly = ReadOnly;
  306. TMemo * Memo = dynamic_cast<TMemo *>(Control);
  307. if (ReadOnly)
  308. {
  309. if (Color)
  310. {
  311. SetParentColor(Control);
  312. }
  313. if (Memo != NULL)
  314. {
  315. // Is true by default and makes the control swallow not only
  316. // returns but also escapes.
  317. // See also MemoKeyDown
  318. Memo->WantReturns = false;
  319. }
  320. if ((Edit->PopupMenu == NULL) && (dynamic_cast<TPasswordEdit *>(Control) == NULL))
  321. {
  322. std::unique_ptr<TPopupMenu> PopupMenu(new TPopupMenu(Edit));
  323. TMenuItem * Item;
  324. Item = new TMenuItem(PopupMenu.get());
  325. PopupMenu->Items->Add(Item);
  326. Item->Action = new TEditCopy(Item);
  327. Item->Caption = LoadStr(EDIT_COPY);
  328. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  329. Item = new TMenuItem(PopupMenu.get());
  330. PopupMenu->Items->Add(Item);
  331. Item->Action = new TEditSelectAll(Item);
  332. Item->Caption = LoadStr(EDIT_SELECT_ALL);
  333. Item->ShortCut = ShortCut(L'A', TShiftState() << ssCtrl);
  334. Edit->PopupMenu = PopupMenu.release();
  335. Edit->OnContextPopup = MakeMethod<TContextPopupEvent>(NULL, ReadOnlyEditContextPopup);
  336. }
  337. }
  338. else
  339. {
  340. if (Color)
  341. {
  342. Edit->Color = clWindow;
  343. }
  344. // not supported atm, we need to persist previous value of WantReturns
  345. DebugAssert(Memo == NULL);
  346. if ((Edit->PopupMenu != NULL) && (Edit->PopupMenu->Owner == Edit))
  347. {
  348. delete Edit->PopupMenu;
  349. }
  350. }
  351. }
  352. else if ((dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  353. (dynamic_cast<TCustomTreeView *>(Control) != NULL))
  354. {
  355. EnableControl(Control, !ReadOnly);
  356. }
  357. else
  358. {
  359. DebugFail();
  360. }
  361. }
  362. //---------------------------------------------------------------------------
  363. void __fastcall ReadOnlyControl(TControl * Control, bool ReadOnly)
  364. {
  365. DoReadOnlyControl(Control, ReadOnly, true);
  366. }
  367. //---------------------------------------------------------------------------
  368. int CalculateCheckBoxWidth(TControl * Control, const UnicodeString & Caption)
  369. {
  370. return
  371. ScaleByTextHeight(Control, 13 + 3 + 8) + // checkbox, padding and buffer
  372. GetParentForm(Control)->Canvas->TextWidth(StripHotkey(Caption));
  373. }
  374. //---------------------------------------------------------------------------
  375. void AutoSizeCheckBox(TCheckBox * CheckBox)
  376. {
  377. CheckBox->Width = CalculateCheckBoxWidth(CheckBox, CheckBox->Caption);
  378. }
  379. //---------------------------------------------------------------------------
  380. // Some of MainFormLike code can now be obsolete, thanks to Application->OnGetMainFormHandle.
  381. static TForm * MainLikeForm = NULL;
  382. //---------------------------------------------------------------------------
  383. TForm * __fastcall GetMainForm()
  384. {
  385. TForm * Result;
  386. if (MainLikeForm != NULL)
  387. {
  388. Result = MainLikeForm;
  389. }
  390. else
  391. {
  392. Result = Application->MainForm;
  393. }
  394. return Result;
  395. }
  396. //---------------------------------------------------------------------------
  397. bool __fastcall IsMainFormHidden()
  398. {
  399. bool Result = false;
  400. TForm * MainForm = GetMainForm();
  401. if (MainForm != NULL)
  402. {
  403. Result =
  404. !MainForm->Visible ||
  405. (MainForm->Perform(WM_IS_HIDDEN, 0, 0) == 1);
  406. }
  407. // we do not expect this to return true when MainLikeForm is set
  408. DebugAssert(!Result || (MainLikeForm == NULL));
  409. return Result;
  410. }
  411. //---------------------------------------------------------------------------
  412. bool __fastcall IsMainFormLike(TCustomForm * Form)
  413. {
  414. return
  415. (GetMainForm() == Form) ||
  416. // this particularly happens if error occurs while main
  417. // window is being shown (e.g. non existent local directory when opening
  418. // explorer)
  419. IsMainFormHidden();
  420. }
  421. //---------------------------------------------------------------------------
  422. UnicodeString __fastcall FormatMainFormCaption(const UnicodeString & Caption, const UnicodeString & SessionName)
  423. {
  424. UnicodeString Suffix = AppName;
  425. if (!SessionName.IsEmpty())
  426. {
  427. Suffix = SessionName + TitleSeparator + Suffix;
  428. }
  429. UnicodeString Result = Caption;
  430. if (Result.IsEmpty())
  431. {
  432. Result = Suffix;
  433. }
  434. else
  435. {
  436. Suffix = TitleSeparator + Suffix;
  437. if (!EndsStr(Suffix, Result))
  438. {
  439. Result += Suffix;
  440. }
  441. }
  442. return Result;
  443. }
  444. //---------------------------------------------------------------------------
  445. UnicodeString __fastcall FormatFormCaption(
  446. TCustomForm * Form, const UnicodeString & Caption, const UnicodeString & SessionName)
  447. {
  448. UnicodeString Result = Caption;
  449. if (IsMainFormLike(Form))
  450. {
  451. Result = FormatMainFormCaption(Result, SessionName);
  452. }
  453. return Result;
  454. }
  455. //---------------------------------------------------------------------------
  456. class TPublicWinControl : public TWinControl
  457. {
  458. friend TWndMethod __fastcall ControlWndProc(TWinControl * Control);
  459. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  460. };
  461. //---------------------------------------------------------------------------
  462. TWndMethod __fastcall ControlWndProc(TWinControl * Control)
  463. {
  464. TPublicWinControl * PublicWinControl = static_cast<TPublicWinControl *>(Control);
  465. return &PublicWinControl->WndProc;
  466. }
  467. //---------------------------------------------------------------------
  468. class TPublicControl : public TControl
  469. {
  470. friend void __fastcall RealignControl(TControl * Control);
  471. friend void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc, TMessage & Message);
  472. friend TCanvas * CreateControlCanvas(TControl * Control);
  473. };
  474. //---------------------------------------------------------------------
  475. class TPublicForm : public TForm
  476. {
  477. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage, bool BringToFront, bool TriggerModalStarted);
  478. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  479. friend void __fastcall ShowFormNoActivate(TForm * Form);
  480. };
  481. //---------------------------------------------------------------------------
  482. void __fastcall RealignControl(TControl * Control)
  483. {
  484. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  485. PublicControl->RequestAlign();
  486. }
  487. //---------------------------------------------------------------------------
  488. static Forms::TMonitor * LastMonitor = NULL;
  489. //---------------------------------------------------------------------------
  490. class TRescaleComponent : public TComponent
  491. {
  492. public:
  493. __fastcall TRescaleComponent(TRescaleEvent AOnRescale, TObject * AToken, bool AOwnsToken) :
  494. TComponent(NULL)
  495. {
  496. OnRescale = AOnRescale;
  497. Token = AToken;
  498. OwnsToken = AOwnsToken;
  499. }
  500. virtual __fastcall ~TRescaleComponent()
  501. {
  502. if (OwnsToken)
  503. {
  504. delete Token;
  505. }
  506. }
  507. TRescaleEvent OnRescale;
  508. TObject * Token;
  509. bool OwnsToken;
  510. };
  511. //---------------------------------------------------------------------------
  512. void __fastcall SetRescaleFunction(
  513. TComponent * Component, TRescaleEvent OnRescale, TObject * Token, bool OwnsToken)
  514. {
  515. TRescaleComponent * RescaleComponent = new TRescaleComponent(OnRescale, Token, OwnsToken);
  516. RescaleComponent->Name = TRescaleComponent::QualifiedClassName();
  517. Component->InsertComponent(RescaleComponent);
  518. }
  519. //---------------------------------------------------------------------------
  520. static void __fastcall ChangeControlScale(TControl * Control)
  521. {
  522. TCustomCombo * CustomCombo = dynamic_cast<TCustomCombo *>(Control);
  523. if (CustomCombo != NULL)
  524. {
  525. // WORKAROUND
  526. // Some combo boxes (e.g. path box on Copy dialog) visualize selection after rescaling, even when not focused
  527. if (!CustomCombo->Focused())
  528. {
  529. CustomCombo->SelLength = 0;
  530. }
  531. }
  532. for (int Index = 0; Index < Control->ComponentCount; Index++)
  533. {
  534. TComponent * Component = Control->Components[Index];
  535. TControl * ChildControl = dynamic_cast<TControl *>(Component);
  536. if (ChildControl != NULL)
  537. {
  538. ChangeControlScale(ChildControl);
  539. }
  540. TRescaleComponent * RescaleComponent =
  541. dynamic_cast<TRescaleComponent *>(Component->FindComponent(TRescaleComponent::QualifiedClassName()));
  542. if (RescaleComponent != NULL)
  543. {
  544. RescaleComponent->OnRescale(Component, RescaleComponent->Token);
  545. }
  546. }
  547. Control->Perform(CM_DPICHANGED, 0, 0);
  548. }
  549. //---------------------------------------------------------------------------
  550. typedef std::pair<int, int> TRatio;
  551. typedef std::map<TRatio, TRatio > TRatioMap;
  552. //---------------------------------------------------------------------------
  553. class TFormCustomizationComponent : public TComponent
  554. {
  555. public:
  556. __fastcall TFormCustomizationComponent() :
  557. TComponent(NULL)
  558. {
  559. WindowStateBeforeMimimize = wsNormal;
  560. }
  561. TWindowState WindowStateBeforeMimimize;
  562. };
  563. //---------------------------------------------------------------------------
  564. static TFormCustomizationComponent * GetFormCustomizationComponent(TForm * Form)
  565. {
  566. TFormCustomizationComponent * FormCustomizationComponent =
  567. dynamic_cast<TFormCustomizationComponent *>(Form->FindComponent(TFormCustomizationComponent::QualifiedClassName()));
  568. if (FormCustomizationComponent == NULL)
  569. {
  570. FormCustomizationComponent = new TFormCustomizationComponent();
  571. FormCustomizationComponent->Name = TFormCustomizationComponent::QualifiedClassName();
  572. Form->InsertComponent(FormCustomizationComponent);
  573. }
  574. return FormCustomizationComponent;
  575. }
  576. //---------------------------------------------------------------------------
  577. static void __fastcall ChangeFormPixelsPerInch(TForm * Form)
  578. {
  579. AppLogFmt(L"Scaling window %s", (Form->Caption));
  580. ChangeControlScale(Form);
  581. }
  582. //---------------------------------------------------------------------------
  583. static void __fastcall FormShowingChanged(TForm * Form, TWndMethod WndProc, TMessage & Message)
  584. {
  585. if (IsMainFormLike(Form))
  586. {
  587. if (Form->Showing)
  588. {
  589. if (Application->MainForm != Form)
  590. {
  591. MainLikeForm = Form;
  592. // When main form is hidden, no taskbar button for it is shown and
  593. // this window does not become main window, so there won't be no taskbar
  594. // icon created automatically (by VCL). So we force it manually here.
  595. // This particularly happen for all windows/dialogs of command-line
  596. // operations (e.g. /synchronize) after CreateScpExplorer() happens.
  597. // Also CM_SHOWINGCHANGED happens twice, and the WS_EX_APPWINDOW flag
  598. // from the first call is not preserved, re-applying on the second call too.
  599. // TODO: What about minimize to tray?
  600. int Style = GetWindowLong(Form->Handle, GWL_EXSTYLE);
  601. if (FLAGCLEAR(Style, WS_EX_APPWINDOW))
  602. {
  603. Style |= WS_EX_APPWINDOW;
  604. SetWindowLong(Form->Handle, GWL_EXSTYLE, Style);
  605. }
  606. }
  607. if (Form->Perform(WM_MANAGES_CAPTION, 0, 0) == 0)
  608. {
  609. Form->Caption = FormatFormCaption(Form, Form->Caption);
  610. }
  611. SendMessage(Form->Handle, WM_SETICON, ICON_BIG, reinterpret_cast<long>(Application->Icon->Handle));
  612. }
  613. else
  614. {
  615. if (MainLikeForm == Form)
  616. {
  617. // Do not bother with hiding the WS_EX_APPWINDOW flag
  618. // as the window is closing anyway.
  619. MainLikeForm = NULL;
  620. }
  621. }
  622. }
  623. // Part of following code (but actually not all, TODO), has to happen
  624. // for all windows when VCL main window is hidden (particularly the last branch).
  625. // This is different from above brach, that should happen only for top-level visible window.
  626. if ((Application->MainForm == Form) ||
  627. // this particularly happens if error occurs while main
  628. // window is being shown (e.g. non existent local directory when opening
  629. // explorer)
  630. ((Application->MainForm != NULL) && !Application->MainForm->Visible))
  631. {
  632. if (!Form->Showing)
  633. {
  634. // when closing main form, remember its monitor,
  635. // so that the next form is shown on the same one
  636. LastMonitor = Form->Monitor;
  637. }
  638. else if ((LastMonitor != NULL) && (LastMonitor != Form->Monitor) &&
  639. Form->Showing)
  640. {
  641. // would actually always be poScreenCenter, see _SafeFormCreate
  642. if ((Form->Position == poMainFormCenter) ||
  643. (Form->Position == poOwnerFormCenter) ||
  644. (Form->Position == poScreenCenter))
  645. {
  646. // this would typically be a standalone message box (e.g. /UninstallCleanup or /Update)
  647. // If DPI changes (as the form moves to a monitor with a non-system DPI),
  648. // we have to re-center as the form size changed too.
  649. int PixelsPerInch;
  650. do
  651. {
  652. PixelsPerInch = Form->PixelsPerInch;
  653. // taken from TCustomForm::SetWindowToMonitor
  654. Form->SetBounds(LastMonitor->Left + ((LastMonitor->Width - Form->Width) / 2),
  655. LastMonitor->Top + ((LastMonitor->Height - Form->Height) / 2),
  656. Form->Width, Form->Height);
  657. Form->Position = poDesigned;
  658. }
  659. while (PixelsPerInch != Form->PixelsPerInch);
  660. }
  661. else if ((Form->Position != poDesigned) &&
  662. (Form->Position != poDefaultPosOnly))
  663. {
  664. // we do not expect any other positioning
  665. DebugFail();
  666. }
  667. }
  668. // otherwise it would not get centered
  669. else if ((Form->Position == poMainFormCenter) ||
  670. (Form->Position == poOwnerFormCenter))
  671. {
  672. Form->Position = poScreenCenter;
  673. }
  674. }
  675. bool WasFormCenter =
  676. (Form->Position == poMainFormCenter) ||
  677. (Form->Position == poOwnerFormCenter);
  678. WndProc(Message);
  679. // Make sure dialogs are shown on-screen even if center of the main window
  680. // is off-screen. Occurs e.g. if you move the main window so that
  681. // only window title is visible above taksbar.
  682. if (Form->Showing && WasFormCenter && (Form->Position == poDesigned))
  683. {
  684. TRect Rect;
  685. // Reading Form.Left/Form.Top instead here does not work, likely due to some
  686. // bug, when querying TProgressForm opened from TEditorForm (reloading remote file)
  687. GetWindowRect(Form->Handle, &Rect);
  688. int Left = Rect.Left;
  689. int Top = Rect.Top;
  690. TRect WorkArea = Form->Monitor->WorkareaRect;
  691. if (Left + Rect.Width() > WorkArea.Right)
  692. {
  693. Left = WorkArea.Right - Rect.Width();
  694. }
  695. if (Left < WorkArea.Left)
  696. {
  697. Left = WorkArea.Left;
  698. }
  699. if (Top + Rect.Height() > WorkArea.Bottom)
  700. {
  701. Top = WorkArea.Bottom - Rect.Height();
  702. }
  703. if (Top < WorkArea.Top)
  704. {
  705. Top = WorkArea.Top;
  706. }
  707. if ((Left != Rect.Left) ||
  708. (Top != Rect.Top))
  709. {
  710. SetWindowPos(Form->Handle, 0, Left, Top, Rect.Width(), Rect.Height(),
  711. SWP_NOZORDER + SWP_NOACTIVATE);
  712. }
  713. }
  714. }
  715. //---------------------------------------------------------------------------
  716. TWindowState GetWindowStateBeforeMimimize(TForm * Form)
  717. {
  718. return GetFormCustomizationComponent(Form)->WindowStateBeforeMimimize;
  719. }
  720. //---------------------------------------------------------------------------
  721. static TCustomForm * WindowPrintForm = NULL;
  722. static DWORD WindowPrintPrevClick = 0;
  723. static unsigned int WindowPrintClickCount = 0;
  724. //---------------------------------------------------------------------------
  725. void __fastcall CountClicksForWindowPrint(TForm * Form)
  726. {
  727. if ((WinConfiguration != NULL) && WinConfiguration->AllowWindowPrint)
  728. {
  729. DWORD Tick = GetTickCount();
  730. if (WindowPrintForm != Form)
  731. {
  732. WindowPrintForm = Form;
  733. WindowPrintClickCount = 0;
  734. }
  735. if (WindowPrintPrevClick < Tick - 500)
  736. {
  737. WindowPrintClickCount = 0;
  738. }
  739. WindowPrintClickCount++;
  740. WindowPrintPrevClick = Tick;
  741. if (WindowPrintClickCount == 3)
  742. {
  743. WindowPrintClickCount = 0;
  744. TInstantOperationVisualizer Visualizer;
  745. // get the device context of the screen
  746. HDC ScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);
  747. // and a device context to put it in
  748. HDC MemoryDC = CreateCompatibleDC(ScreenDC);
  749. try
  750. {
  751. bool Sizable = (Form->BorderStyle == bsSizeable);
  752. int Frame = GetSystemMetrics(Sizable ? SM_CXSIZEFRAME : SM_CXFIXEDFRAME) - 1;
  753. int Width = Form->Width - 2*Frame;
  754. int Height = Form->Height - Frame;
  755. // maybe worth checking these are positive values
  756. HBITMAP Bitmap = CreateCompatibleBitmap(ScreenDC, Width, Height);
  757. try
  758. {
  759. // get a new bitmap
  760. HBITMAP OldBitmap = static_cast<HBITMAP>(SelectObject(MemoryDC, Bitmap));
  761. BitBlt(MemoryDC, 0, 0, Width, Height, ScreenDC, Form->Left + Frame, Form->Top, SRCCOPY);
  762. Bitmap = static_cast<HBITMAP>(SelectObject(MemoryDC, OldBitmap));
  763. OpenClipboard(NULL);
  764. try
  765. {
  766. EmptyClipboard();
  767. SetClipboardData(CF_BITMAP, Bitmap);
  768. }
  769. __finally
  770. {
  771. CloseClipboard();
  772. }
  773. }
  774. __finally
  775. {
  776. DeleteObject(Bitmap);
  777. }
  778. }
  779. __finally
  780. {
  781. DeleteDC(MemoryDC);
  782. DeleteDC(ScreenDC);
  783. }
  784. }
  785. }
  786. }
  787. //---------------------------------------------------------------------------
  788. struct TWMDpiChangedData
  789. {
  790. TMonitorDpiChangedEvent OnAfterMonitorDpiChanged;
  791. };
  792. //---------------------------------------------------------------------------
  793. static void __fastcall AfterMonitorDpiChanged(void * AData, TObject * Sender, int OldDPI, int NewDPI)
  794. {
  795. TWMDpiChangedData * Data = static_cast<TWMDpiChangedData *>(AData);
  796. ChangeFormPixelsPerInch(DebugNotNull(dynamic_cast<TForm *>(Sender)));
  797. if (Data->OnAfterMonitorDpiChanged != NULL)
  798. {
  799. Data->OnAfterMonitorDpiChanged(Sender, OldDPI, NewDPI);
  800. }
  801. }
  802. //---------------------------------------------------------------------------
  803. inline void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc,
  804. TMessage & Message)
  805. {
  806. TForm * AForm = dynamic_cast<TForm *>(Form);
  807. DebugAssert(AForm != NULL);
  808. if (Message.Msg == WM_SYSCOMMAND)
  809. {
  810. if (Message.WParam == SC_CONTEXTHELP)
  811. {
  812. FormHelp(Form);
  813. Message.Result = 1;
  814. }
  815. else if (Message.WParam == SC_MINIMIZE)
  816. {
  817. GetFormCustomizationComponent(AForm)->WindowStateBeforeMimimize = Form->WindowState;
  818. WndProc(Message);
  819. }
  820. else
  821. {
  822. WndProc(Message);
  823. }
  824. }
  825. else if (Message.Msg == CM_SHOWINGCHANGED)
  826. {
  827. FormShowingChanged(AForm, WndProc, Message);
  828. }
  829. else if (Message.Msg == WM_SETICON)
  830. {
  831. // WORKAROUND: Swallow VCL attempt to clear the icon from TCustomForm.WMDestroy.
  832. // The clearing still happens to be visualised before the form is hidden.
  833. // On resizable forms, the icon gets replaced by the default application icon,
  834. // on the other forms, the icon disappears and window caption is shifted left.
  835. if (Message.LParam != 0)
  836. {
  837. WndProc(Message);
  838. }
  839. }
  840. else if (Message.Msg == WM_DPICHANGED)
  841. {
  842. TWMDpiChangedData WMDpiChangedData;
  843. WMDpiChangedData.OnAfterMonitorDpiChanged = AForm->OnAfterMonitorDpiChanged;
  844. AForm->OnAfterMonitorDpiChanged = MakeMethod<TMonitorDpiChangedEvent>(&WMDpiChangedData, AfterMonitorDpiChanged);
  845. try
  846. {
  847. WndProc(Message);
  848. }
  849. __finally
  850. {
  851. AForm->OnAfterMonitorDpiChanged = WMDpiChangedData.OnAfterMonitorDpiChanged;
  852. }
  853. }
  854. else if ((Message.Msg == WM_LBUTTONDOWN) || (Message.Msg == WM_LBUTTONDBLCLK))
  855. {
  856. CountClicksForWindowPrint(AForm);
  857. WndProc(Message);
  858. }
  859. else
  860. {
  861. WndProc(Message);
  862. }
  863. }
  864. //---------------------------------------------------------------------------
  865. static void __fastcall FormWindowProc(void * Data, TMessage & Message)
  866. {
  867. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  868. DoFormWindowProc(Form, ControlWndProc(Form), Message);
  869. }
  870. //---------------------------------------------------------------------------
  871. void __fastcall InitializeSystemSettings()
  872. {
  873. }
  874. //---------------------------------------------------------------------------
  875. void __fastcall FinalizeSystemSettings()
  876. {
  877. }
  878. //---------------------------------------------------------------------------
  879. #ifdef _DEBUG
  880. void __fastcall VerifyControl(TControl * Control)
  881. {
  882. // If at this time the control has allocated persistence data that are used
  883. // for delayed handle recreation, we are at potential risk, as the future
  884. // de-persistence may overwrite meanwhile changed data.
  885. // For instance it may happen with list view that DPI scaling gets lost.
  886. // This for example happens when the list view has both design time
  887. // ReadOnly = true and some items set. We cannot usually explicitly
  888. // check for the presence of items as while the listview does not have
  889. // a handle allocated, item count querying does not work
  890. // (see also a check below)
  891. if (ControlHasRecreationPersistenceData(Control))
  892. {
  893. // Though if RTL bidi mode is set, the controls are recreated always,
  894. // as we cannot really prevent it. So we force creation here.
  895. DebugAssert(Application->BiDiMode != bdLeftToRight);
  896. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  897. // It must be TWinControl if ControlHasRecreationPersistenceData returned true
  898. if (DebugAlwaysTrue(WinControl != NULL))
  899. {
  900. WinControl->HandleNeeded();
  901. }
  902. }
  903. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  904. if (ListView != NULL)
  905. {
  906. // As of now the HandleAllocated check is pointless as
  907. // ListView->Items->Count returns 0 when the handle is not allocated yet.
  908. // But we want to know if the implementation ever changes to allocate the handle
  909. // on the call. Because we do not want to allocate a handle here as
  910. // that would change the debug mode behavior from release behavior,
  911. // possibly hiding away some problems.
  912. DebugAssert(!ListView->HandleAllocated() || (ListView->Items->Count == 0));
  913. }
  914. }
  915. #endif
  916. //---------------------------------------------------------------------------
  917. void __fastcall ApplySystemSettingsOnControl(TControl * Control)
  918. {
  919. #ifdef _DEBUG
  920. VerifyControl(Control);
  921. #endif
  922. // WORKAROUND
  923. // VCL does not scale status par panels (while for instance it does
  924. // scale list view headers). Remove this if they ever "fix" this.
  925. // Neither they scale the status bar size if UseSystemFont is true.
  926. // they claim it should scale with font size, but it does not,
  927. // probably because they eat WM_SIZE in TCustomStatusBar.WMSize.
  928. // (used to be issue 83599 on Embarcadero QC, when it was still online)
  929. // Check TCustomStatusBar.ChangeScale() for changes.
  930. // For TBX status bars, this is implemented in TTBXCustomStatusBar.ChangeScale
  931. TStatusBar * StatusBar = dynamic_cast<TStatusBar *>(Control);
  932. if (StatusBar != NULL)
  933. {
  934. // We should have UseSystemFont and bottom alignment set for all status bars.
  935. if (DebugAlwaysTrue(StatusBar->UseSystemFont) &&
  936. DebugAlwaysTrue(StatusBar->Align == alBottom))
  937. {
  938. StatusBar->Height = ScaleByTextHeight(StatusBar, StatusBar->Height);
  939. }
  940. for (int Index = 0; Index < StatusBar->Panels->Count; Index++)
  941. {
  942. TStatusPanel * Panel = StatusBar->Panels->Items[Index];
  943. Panel->Width = ScaleByTextHeight(StatusBar, Panel->Width);
  944. }
  945. }
  946. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  947. TCustomIEListView * IEListView = dynamic_cast<TCustomIEListView *>(Control);
  948. // For IEListView, this is (somewhat) handled in the TCustomListViewColProperties
  949. if ((ListView != NULL) && (IEListView == NULL))
  950. {
  951. TListView * PublicListView = reinterpret_cast<TListView *>(ListView);
  952. for (int Index = 0; Index < PublicListView->Columns->Count; Index++)
  953. {
  954. TListColumn * Column = ListView->Column[Index];
  955. Column->MaxWidth = ScaleByTextHeight(ListView, Column->MaxWidth);
  956. Column->MinWidth = ScaleByTextHeight(ListView, Column->MinWidth);
  957. }
  958. }
  959. // WORKAROUND for lack of public API for mimicking Explorer-style mouse selection
  960. // See https://stackoverflow.com/q/15750842/850848
  961. if (IEListView != NULL)
  962. {
  963. // It should not be a problem to call the LVM_QUERYINTERFACE
  964. // on earlier versions of Windows. It should be noop.
  965. if (IsWin7())
  966. {
  967. IListView_Win7 * ListViewIntf = NULL;
  968. SendMessage(IEListView->Handle, LVM_QUERYINTERFACE, reinterpret_cast<WPARAM>(&IID_IListView_Win7), reinterpret_cast<LPARAM>(&ListViewIntf));
  969. if (ListViewIntf != NULL)
  970. {
  971. ListViewIntf->SetSelectionFlags(1, 1);
  972. ListViewIntf->Release();
  973. }
  974. else
  975. {
  976. DebugAssert(IsWine());
  977. }
  978. }
  979. }
  980. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  981. if (WinControl != NULL)
  982. {
  983. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  984. {
  985. ApplySystemSettingsOnControl(WinControl->Controls[Index]);
  986. }
  987. }
  988. }
  989. //---------------------------------------------------------------------------
  990. // Settings that must be set as soon as possible.
  991. void __fastcall UseSystemSettingsPre(TForm * Control)
  992. {
  993. LocalSystemSettings(Control);
  994. TWndMethod WindowProc;
  995. ((TMethod*)&WindowProc)->Data = Control;
  996. ((TMethod*)&WindowProc)->Code = FormWindowProc;
  997. Control->WindowProc = WindowProc;
  998. if (Control->HelpKeyword.IsEmpty())
  999. {
  1000. // temporary help keyword to enable F1 key in all forms
  1001. Control->HelpKeyword = L"start";
  1002. }
  1003. // We have legacy XE6 font (Tahoma) explicitly set in DFMs to match the layout.
  1004. // That unlinks the font fonts from the Application->DefaultFont. We set the DefaultFont to Tahoma in WinSCP.cpp.
  1005. DebugAssert(SameFont(Control->Font, Application->DefaultFont));
  1006. ApplySystemSettingsOnControl(Control);
  1007. };
  1008. //---------------------------------------------------------------------------
  1009. static void FlipAnchors(TControl * Control)
  1010. {
  1011. // WORKAROUND VCL flips the Align, but not the Anchors
  1012. TAnchors Anchors = Control->Anchors;
  1013. if (Anchors.Contains(akLeft) != Anchors.Contains(akRight))
  1014. {
  1015. if (Anchors.Contains(akLeft))
  1016. {
  1017. Anchors << akRight;
  1018. Anchors >> akLeft;
  1019. }
  1020. else
  1021. {
  1022. Anchors << akLeft;
  1023. Anchors >> akRight;
  1024. }
  1025. }
  1026. Control->Anchors = Anchors;
  1027. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  1028. if (WinControl != NULL)
  1029. {
  1030. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  1031. {
  1032. FlipAnchors(WinControl->Controls[Index]);
  1033. }
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. // Settings that must be set only after whole form is constructed
  1038. void __fastcall UseSystemSettingsPost(TForm * Control)
  1039. {
  1040. // When showing an early error message
  1041. if (WinConfiguration != NULL)
  1042. {
  1043. UnicodeString FlipStr = LoadStr(FLIP_CHILDREN);
  1044. int FlipChildrenFlag =
  1045. AdjustLocaleFlag(FlipStr, WinConfiguration->FlipChildrenOverride, false, true, false);
  1046. if (static_cast<bool>(FlipChildrenFlag))
  1047. {
  1048. Control->FlipChildren(true);
  1049. FlipAnchors(Control);
  1050. }
  1051. }
  1052. ResetSystemSettings(Control);
  1053. };
  1054. //---------------------------------------------------------------------------
  1055. void __fastcall UseSystemSettings(TForm * Control)
  1056. {
  1057. UseSystemSettingsPre(Control);
  1058. UseSystemSettingsPost(Control);
  1059. };
  1060. //---------------------------------------------------------------------------
  1061. void __fastcall ResetSystemSettings(TForm * /*Control*/)
  1062. {
  1063. // noop
  1064. }
  1065. //---------------------------------------------------------------------------
  1066. struct TShowAsModalStorage
  1067. {
  1068. bool TriggerModalFinished;
  1069. void * FocusWindowList;
  1070. HWND FocusActiveWindow;
  1071. TFocusState FocusState;
  1072. TCursor SaveCursor;
  1073. int SaveCount;
  1074. };
  1075. //---------------------------------------------------------------------------
  1076. void __fastcall ShowAsModal(TForm * Form, void *& Storage, bool BringToFront, bool TriggerModalStarted)
  1077. {
  1078. SetCorrectFormParent(Form);
  1079. CancelDrag();
  1080. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  1081. ReleaseCapture();
  1082. if (TriggerModalStarted)
  1083. {
  1084. Application->ModalStarted();
  1085. }
  1086. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  1087. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  1088. AStorage->TriggerModalFinished = TriggerModalStarted;
  1089. AStorage->FocusActiveWindow = GetActiveWindow();
  1090. AStorage->FocusState = SaveFocusState();
  1091. Screen->SaveFocusedList->Insert(0, Screen->FocusedForm);
  1092. Screen->FocusedForm = Form;
  1093. AStorage->SaveCursor = Screen->Cursor;
  1094. // This is particularly used when displaing progress window
  1095. // while downloading via temporary folder
  1096. // (the mouse cursor is yet hidden at this point).
  1097. // While the code was added to workaround the above problem,
  1098. // it is taken from TCustomForm.ShowModal and
  1099. // should have been here ever since.
  1100. Screen->Cursor = crDefault;
  1101. AStorage->SaveCount = Screen->CursorCount;
  1102. AStorage->FocusWindowList = DisableTaskWindows(0);
  1103. // VCLCOPY (TCustomForm::Show)
  1104. Form->Visible = true;
  1105. if (BringToFront)
  1106. {
  1107. Form->BringToFront();
  1108. }
  1109. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  1110. Storage = AStorage;
  1111. }
  1112. //---------------------------------------------------------------------------
  1113. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  1114. {
  1115. DebugAssert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  1116. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  1117. Storage = NULL;
  1118. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  1119. if (GetActiveWindow() != Form->Handle)
  1120. {
  1121. AStorage->FocusActiveWindow = 0;
  1122. }
  1123. Form->Hide();
  1124. if (Screen->CursorCount == AStorage->SaveCount)
  1125. {
  1126. Screen->Cursor = AStorage->SaveCursor;
  1127. }
  1128. else
  1129. {
  1130. Screen->Cursor = crDefault;
  1131. }
  1132. EnableTaskWindows(AStorage->FocusWindowList);
  1133. if (Screen->SaveFocusedList->Count > 0)
  1134. {
  1135. Screen->FocusedForm = static_cast<TCustomForm *>(Screen->SaveFocusedList->First());
  1136. Screen->SaveFocusedList->Remove(Screen->FocusedForm);
  1137. }
  1138. else
  1139. {
  1140. Screen->FocusedForm = NULL;
  1141. }
  1142. if (AStorage->FocusActiveWindow != 0)
  1143. {
  1144. SetActiveWindow(AStorage->FocusActiveWindow);
  1145. }
  1146. RestoreFocusState(AStorage->FocusState);
  1147. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  1148. if (AStorage->TriggerModalFinished)
  1149. {
  1150. Application->ModalFinished();
  1151. }
  1152. delete AStorage;
  1153. }
  1154. //---------------------------------------------------------------------------
  1155. bool __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  1156. {
  1157. bool Result = (Storage != NULL);
  1158. if (Result)
  1159. {
  1160. HideAsModal(Form, Storage);
  1161. }
  1162. return Result;
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. bool __fastcall SelectDirectory(UnicodeString & Path, const UnicodeString Prompt,
  1166. bool PreserveFileName)
  1167. {
  1168. bool Result;
  1169. unsigned int ErrorMode;
  1170. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  1171. try
  1172. {
  1173. UnicodeString Directory;
  1174. UnicodeString FileName;
  1175. // We do not have any real use for the PreserveFileName
  1176. if (!PreserveFileName || DirectoryExists(ApiPath(Path)))
  1177. {
  1178. Directory = Path;
  1179. }
  1180. else
  1181. {
  1182. Directory = ExtractFilePath(Path);
  1183. FileName = ExtractFileName(Path);
  1184. }
  1185. TSelectDirExtOpts Opts = TSelectDirExtOpts() << sdNewUI;
  1186. Result = SelectDirectory(Prompt, EmptyStr, Directory, Opts);
  1187. if (Result)
  1188. {
  1189. Path = Directory;
  1190. if (!FileName.IsEmpty())
  1191. {
  1192. Path = IncludeTrailingBackslash(Path) + FileName;
  1193. }
  1194. }
  1195. }
  1196. __finally
  1197. {
  1198. SetErrorMode(ErrorMode);
  1199. }
  1200. return Result;
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. void SelectDirectoryForEdit(THistoryComboBox * Edit)
  1204. {
  1205. UnicodeString OriginalDirectory = ExpandEnvironmentVariables(Edit->Text);
  1206. UnicodeString Directory = OriginalDirectory;
  1207. if (SelectDirectory(Directory, LoadStr(SELECT_LOCAL_DIRECTORY), true) &&
  1208. !SamePaths(OriginalDirectory, Directory))
  1209. {
  1210. Edit->Text = Directory;
  1211. if (Edit->OnChange != NULL)
  1212. {
  1213. Edit->OnChange(Edit);
  1214. }
  1215. }
  1216. }
  1217. //---------------------------------------------------------------------------
  1218. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  1219. {
  1220. bool AnyChecked = false;
  1221. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1222. {
  1223. if (ListView->Items->Item[Index]->Checked == Checked)
  1224. {
  1225. AnyChecked = true;
  1226. break;
  1227. }
  1228. }
  1229. return AnyChecked;
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall ListViewCheckAll(TListView * ListView,
  1233. TListViewCheckAll CheckAll)
  1234. {
  1235. bool Check;
  1236. if (CheckAll == caToggle)
  1237. {
  1238. Check = ListViewAnyChecked(ListView, false);
  1239. }
  1240. else
  1241. {
  1242. Check = (CheckAll == caCheck);
  1243. }
  1244. for (int Index = 0; Index < ListView->Items->Count; Index++)
  1245. {
  1246. ListView->Items->Item[Index]->Checked = Check;
  1247. }
  1248. }
  1249. //---------------------------------------------------------------------------
  1250. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  1251. {
  1252. int PrevIndex = ComboBox->ItemIndex;
  1253. ComboBox->Items->BeginUpdate();
  1254. try
  1255. {
  1256. ComboBox->Clear();
  1257. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  1258. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  1259. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  1260. }
  1261. __finally
  1262. {
  1263. ComboBox->Items->EndUpdate();
  1264. }
  1265. DebugAssert(PrevIndex < ComboBox->Items->Count);
  1266. ComboBox->ItemIndex = PrevIndex;
  1267. }
  1268. //---------------------------------------------------------------------------
  1269. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  1270. {
  1271. ComboBox->ItemIndex = 2 - Value;
  1272. if (ComboBox->ItemIndex < 0)
  1273. {
  1274. ComboBox->ItemIndex = 0;
  1275. }
  1276. }
  1277. //---------------------------------------------------------------------------
  1278. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  1279. {
  1280. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  1281. }
  1282. //---------------------------------------------------------------------------
  1283. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  1284. {
  1285. switch (Value)
  1286. {
  1287. case asOn:
  1288. CheckBox->State = cbChecked;
  1289. break;
  1290. case asOff:
  1291. CheckBox->State = cbUnchecked;
  1292. break;
  1293. default:
  1294. CheckBox->State = cbGrayed;
  1295. break;
  1296. }
  1297. }
  1298. //---------------------------------------------------------------------------
  1299. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  1300. {
  1301. switch (CheckBox->State)
  1302. {
  1303. case cbChecked:
  1304. return asOn;
  1305. case cbUnchecked:
  1306. return asOff;
  1307. default:
  1308. return asAuto;
  1309. }
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. static const wchar_t PathWordDelimiters[] = L"\\/ ;,.\r\n=";
  1313. //---------------------------------------------------------------------------
  1314. static bool IsPathWordDelimiter(wchar_t Ch)
  1315. {
  1316. return (wcschr(PathWordDelimiters, Ch) != NULL);
  1317. }
  1318. //---------------------------------------------------------------------------
  1319. // Windows algorithm is as follows (tested on W2k):
  1320. // right:
  1321. // is_delimiter(current)
  1322. // false:
  1323. // right(left(current) + 1)
  1324. // true:
  1325. // right(right(current) + 1)
  1326. // left:
  1327. // right(left(current) + 1)
  1328. int CALLBACK PathWordBreakProc(wchar_t * Ch, int Current, int Len, int Code)
  1329. {
  1330. int Result;
  1331. UnicodeString ACh(Ch, Len);
  1332. if (Code == WB_ISDELIMITER)
  1333. {
  1334. // we return negacy of what WinAPI docs says
  1335. Result = !IsPathWordDelimiter(ACh[Current + 1]);
  1336. }
  1337. else if (Code == WB_LEFT)
  1338. {
  1339. // skip consecutive delimiters
  1340. while ((Current > 0) &&
  1341. IsPathWordDelimiter(ACh[Current]))
  1342. {
  1343. Current--;
  1344. }
  1345. Result = ACh.SubString(1, Current - 1).LastDelimiter(PathWordDelimiters);
  1346. }
  1347. else if (Code == WB_RIGHT)
  1348. {
  1349. if (Current == 0)
  1350. {
  1351. // will be called again with Current == 1
  1352. Result = 0;
  1353. }
  1354. else
  1355. {
  1356. const wchar_t * P = wcspbrk(ACh.c_str() + Current - 1, PathWordDelimiters);
  1357. if (P == NULL)
  1358. {
  1359. Result = Len;
  1360. }
  1361. else
  1362. {
  1363. Result = P - ACh.c_str() + 1;
  1364. // skip consecutive delimiters
  1365. while ((Result < Len) &&
  1366. IsPathWordDelimiter(ACh[Result + 1]))
  1367. {
  1368. Result++;
  1369. }
  1370. }
  1371. }
  1372. }
  1373. else
  1374. {
  1375. DebugFail();
  1376. Result = 0;
  1377. }
  1378. return Result;
  1379. }
  1380. //---------------------------------------------------------------------------
  1381. class TPathWordBreakProcComponent : public TComponent
  1382. {
  1383. public:
  1384. __fastcall TPathWordBreakProcComponent() :
  1385. TComponent(NULL)
  1386. {
  1387. }
  1388. void __fastcall PathWordBreakEditWindowProc(TMessage & Message);
  1389. TWinControl * WinControl;
  1390. TWndMethod PrevWindowProc;
  1391. };
  1392. //---------------------------------------------------------------------------
  1393. void __fastcall TPathWordBreakProcComponent::PathWordBreakEditWindowProc(TMessage & Message)
  1394. {
  1395. bool Handled = false;
  1396. if (Message.Msg == WM_CHAR)
  1397. {
  1398. // Ctrl+Backspace
  1399. // Ctrl+Backspace is handled in WM_CHAR as any other char,
  1400. // so we have to swallow it here to prevent it getting inserted to the text
  1401. TWMChar & CharMessage = *reinterpret_cast<TWMChar *>(&Message);
  1402. if (CharMessage.CharCode == '\x7F')
  1403. {
  1404. TCustomEdit * Edit = dynamic_cast<TCustomEdit *>(WinControl);
  1405. TCustomComboBox * ComboBox = dynamic_cast<TCustomComboBox *>(WinControl);
  1406. if (((Edit != NULL) && (Edit->SelLength == 0)) ||
  1407. ((ComboBox != NULL) && (ComboBox->SelLength == 0)))
  1408. {
  1409. // See TCustomMaskEdit.SetCursor
  1410. TKeyboardState KeyState;
  1411. GetKeyboardState(KeyState);
  1412. TKeyboardState NewKeyState;
  1413. memset(NewKeyState, 0, sizeof(NewKeyState));
  1414. NewKeyState[VK_CONTROL] = 0x81;
  1415. NewKeyState[VK_SHIFT] = 0x81;
  1416. SetKeyboardState(NewKeyState);
  1417. SendMessage(WinControl->Handle, WM_KEYDOWN, VK_LEFT, 1);
  1418. NewKeyState[VK_SHIFT] = 0;
  1419. NewKeyState[VK_CONTROL] = 0;
  1420. SetKeyboardState(NewKeyState);
  1421. SendMessage(WinControl->Handle, WM_KEYDOWN, VK_DELETE, 1);
  1422. SetKeyboardState(KeyState);
  1423. }
  1424. Message.Result = 1;
  1425. Handled = true;
  1426. }
  1427. }
  1428. if (!Handled)
  1429. {
  1430. if (PrevWindowProc != NULL)
  1431. {
  1432. PrevWindowProc(Message);
  1433. }
  1434. else
  1435. {
  1436. ControlWndProc(WinControl)(Message);
  1437. }
  1438. }
  1439. }
  1440. //---------------------------------------------------------------------------
  1441. class TPublicCustomCombo : public TCustomCombo
  1442. {
  1443. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  1444. };
  1445. //---------------------------------------------------------------------------
  1446. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  1447. {
  1448. // Since we are setting Application->ModalPopupMode = pmAuto,
  1449. // this has to be called from OnShow, not from constructor anymore,
  1450. // to have any effect
  1451. HWND Wnd;
  1452. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  1453. {
  1454. TPublicCustomCombo * Combo =
  1455. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  1456. Combo->HandleNeeded();
  1457. Wnd = Combo->EditHandle;
  1458. }
  1459. else
  1460. {
  1461. Wnd = Control->Handle;
  1462. }
  1463. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  1464. TPathWordBreakProcComponent * PathWordBreakProcComponent = new TPathWordBreakProcComponent();
  1465. PathWordBreakProcComponent->Name = TPathWordBreakProcComponent::QualifiedClassName();
  1466. Control->InsertComponent(PathWordBreakProcComponent);
  1467. PathWordBreakProcComponent->WinControl = Control;
  1468. // Have to remember the proc because of TTBEditItemViewer.EditWndProc
  1469. PathWordBreakProcComponent->PrevWindowProc =
  1470. // Test is probably redundant, it's there to limit impact of the change.
  1471. ((Control->WindowProc != ControlWndProc(Control)) ? Control->WindowProc : TWndMethod());
  1472. Control->WindowProc = PathWordBreakProcComponent->PathWordBreakEditWindowProc;
  1473. }
  1474. //---------------------------------------------------------------------------
  1475. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  1476. {
  1477. int Shift = 0;
  1478. for (int Index = 0; Index < Count; Index++)
  1479. {
  1480. if (ControlsOrder[Index]->Visible)
  1481. {
  1482. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  1483. }
  1484. else
  1485. {
  1486. Shift++;
  1487. }
  1488. }
  1489. Count -= Shift;
  1490. }
  1491. //---------------------------------------------------------------------------
  1492. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  1493. {
  1494. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1495. if (Count > 0)
  1496. {
  1497. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1498. CommonParent->DisableAlign();
  1499. try
  1500. {
  1501. int Top = 0;
  1502. for (int Index = 0; Index < Count; Index++)
  1503. {
  1504. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1505. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  1506. {
  1507. Top = ControlsOrder[Index]->Top;
  1508. }
  1509. }
  1510. for (int Index = 0; Index < Count; Index++)
  1511. {
  1512. TControl * Control = ControlsOrder[Index];
  1513. Control->Top = Top;
  1514. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  1515. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  1516. {
  1517. Top += Control->Height;
  1518. }
  1519. }
  1520. }
  1521. __finally
  1522. {
  1523. CommonParent->EnableAlign();
  1524. }
  1525. }
  1526. }
  1527. //---------------------------------------------------------------------------
  1528. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  1529. {
  1530. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1531. if (Count > 0)
  1532. {
  1533. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1534. CommonParent->DisableAlign();
  1535. try
  1536. {
  1537. int Left = 0;
  1538. for (int Index = 0; Index < Count; Index++)
  1539. {
  1540. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1541. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  1542. {
  1543. Left = ControlsOrder[Index]->Left;
  1544. }
  1545. }
  1546. for (int Index = 0; Index < Count; Index++)
  1547. {
  1548. TControl * Control = ControlsOrder[Index];
  1549. Control->Left = Left;
  1550. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  1551. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  1552. {
  1553. Left += Control->Width;
  1554. }
  1555. // vertical alignment has priority, so alBottom-aligned controls start
  1556. // at the very left, even if there are any alLeft/alRight controls.
  1557. // for the reason this code is not necessary in SetVerticalControlsOrder.
  1558. // we could exit the loop as well here.
  1559. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  1560. {
  1561. Left = 0;
  1562. }
  1563. }
  1564. }
  1565. __finally
  1566. {
  1567. CommonParent->EnableAlign();
  1568. }
  1569. }
  1570. }
  1571. //---------------------------------------------------------------------------
  1572. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  1573. {
  1574. if (After->TabOrder > Control->TabOrder)
  1575. {
  1576. After->TabOrder = Control->TabOrder;
  1577. }
  1578. else if (After->TabOrder < Control->TabOrder - 1)
  1579. {
  1580. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  1581. }
  1582. }
  1583. //---------------------------------------------------------------------------
  1584. void __fastcall CutFormToDesktop(TForm * Form)
  1585. {
  1586. DebugAssert(Form->Monitor != NULL);
  1587. TRect Workarea = Form->Monitor->WorkareaRect;
  1588. if (Form->Top + Form->Height > Workarea.Bottom)
  1589. {
  1590. Form->Height = Workarea.Bottom - Form->Top;
  1591. }
  1592. if (Form->Left + Form->Width >= Workarea.Right)
  1593. {
  1594. Form->Width = Workarea.Right - Form->Left;
  1595. }
  1596. }
  1597. //---------------------------------------------------------------------------
  1598. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  1599. {
  1600. if ((Position == poScreenCenter) ||
  1601. (Position == poOwnerFormCenter) ||
  1602. (Position == poMainFormCenter))
  1603. {
  1604. TCustomForm * CenterForm = NULL;
  1605. if ((Position == poOwnerFormCenter) ||
  1606. (Position == poMainFormCenter))
  1607. {
  1608. CenterForm = Application->MainForm;
  1609. if ((Position == poOwnerFormCenter) &&
  1610. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  1611. {
  1612. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  1613. }
  1614. }
  1615. TRect Bounds = Form->BoundsRect;
  1616. int X, Y;
  1617. if (CenterForm != NULL)
  1618. {
  1619. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  1620. ((TForm *)CenterForm)->Left;
  1621. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  1622. ((TForm *)CenterForm)->Top;
  1623. }
  1624. else
  1625. {
  1626. X = (Form->Monitor->Width - Bounds.Width()) / 2;
  1627. Y = (Form->Monitor->Height - Bounds.Height()) / 2;
  1628. }
  1629. if (X < 0)
  1630. {
  1631. X = 0;
  1632. }
  1633. if (Y < 0)
  1634. {
  1635. Y = 0;
  1636. }
  1637. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  1638. }
  1639. }
  1640. //---------------------------------------------------------------------------
  1641. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  1642. {
  1643. // This has to be called only after DoFormWindowProc(CM_SHOWINGCHANGED),
  1644. // so that a correct monitor is considered.
  1645. // Note that we cannot use LastMonitor(), as ResizeForm is also called from
  1646. // TConsoleDialog::DoAdjustWindow, where we need to use the actual monitor
  1647. // (in case user moves the console window to a different monitor,
  1648. // than where a main window is [no matter how unlikely that is])
  1649. TRect WorkareaRect = Form->Monitor->WorkareaRect;
  1650. if (Height > WorkareaRect.Height())
  1651. {
  1652. Height = WorkareaRect.Height();
  1653. }
  1654. if (Width > WorkareaRect.Width())
  1655. {
  1656. Width = WorkareaRect.Width();
  1657. }
  1658. if (Height < Form->Constraints->MinHeight)
  1659. {
  1660. Height = Form->Constraints->MinHeight;
  1661. }
  1662. if (Width < Form->Constraints->MinWidth)
  1663. {
  1664. Width = Form->Constraints->MinWidth;
  1665. }
  1666. TRect Bounds = Form->BoundsRect;
  1667. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  1668. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  1669. if (Top + Height > WorkareaRect.Bottom)
  1670. {
  1671. Top = WorkareaRect.Bottom - Height;
  1672. }
  1673. if (Left + Width >= WorkareaRect.Right)
  1674. {
  1675. Left = WorkareaRect.Right - Width;
  1676. }
  1677. // WorkareaRect.Left is not 0, when secondary monitor is placed left of primary one.
  1678. // Similarly for WorkareaRect.Top.
  1679. if (Top < WorkareaRect.Top)
  1680. {
  1681. Top = WorkareaRect.Top;
  1682. }
  1683. if (Left < WorkareaRect.Left)
  1684. {
  1685. Left = WorkareaRect.Left;
  1686. }
  1687. Form->SetBounds(Left, Top, Width, Height);
  1688. Bounds = Form->BoundsRect;
  1689. // due to constraints, form can remain larger, make sure it is centered although
  1690. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  1691. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  1692. Form->SetBounds(Left, Top, Width, Height);
  1693. }
  1694. //---------------------------------------------------------------------------
  1695. TComponent * __fastcall GetFormOwner()
  1696. {
  1697. if (Screen->ActiveForm != NULL)
  1698. {
  1699. return Screen->ActiveForm;
  1700. }
  1701. else
  1702. {
  1703. return Application;
  1704. }
  1705. }
  1706. //---------------------------------------------------------------------------
  1707. void __fastcall SetCorrectFormParent(TForm * /*Form*/)
  1708. {
  1709. // noop
  1710. // remove
  1711. }
  1712. //---------------------------------------------------------------------------
  1713. void __fastcall InvokeHelp(TWinControl * Control)
  1714. {
  1715. DebugAssert(Control != NULL);
  1716. HELPINFO HelpInfo;
  1717. HelpInfo.cbSize = sizeof(HelpInfo);
  1718. HelpInfo.iContextType = HELPINFO_WINDOW;
  1719. HelpInfo.iCtrlId = 0;
  1720. HelpInfo.hItemHandle = Control->Handle;
  1721. HelpInfo.dwContextId = 0;
  1722. HelpInfo.MousePos.x = 0;
  1723. HelpInfo.MousePos.y = 0;
  1724. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  1725. }
  1726. //---------------------------------------------------------------------------
  1727. //---------------------------------------------------------------------------
  1728. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  1729. TCanvas ** ACanvas, TRect & R)
  1730. {
  1731. TCanvas * Canvas = CreateControlCanvas(StaticText);
  1732. try
  1733. {
  1734. R = StaticText->ClientRect;
  1735. TSize TextSize;
  1736. if (StaticText->AutoSize)
  1737. {
  1738. // We possibly could use the same code as in !AutoSize branch,
  1739. // keeping this to avoid problems in existing code
  1740. UnicodeString Caption = StaticText->Caption;
  1741. bool AccelChar = false;
  1742. if (StaticText->ShowAccelChar)
  1743. {
  1744. Caption = StripHotkey(Caption);
  1745. AccelChar = (Caption != StaticText->Caption);
  1746. }
  1747. TextSize = Canvas->TextExtent(Caption);
  1748. DebugAssert(StaticText->BorderStyle == sbsNone); // not taken into account
  1749. if (AccelChar)
  1750. {
  1751. TextSize.cy += 2;
  1752. }
  1753. }
  1754. else
  1755. {
  1756. TRect TextRect;
  1757. SetRect(&TextRect, 0, 0, StaticText->Width, 0);
  1758. DrawText(Canvas->Handle, StaticText->Caption.c_str(), -1, &TextRect,
  1759. DT_CALCRECT | DT_WORDBREAK |
  1760. StaticText->DrawTextBiDiModeFlagsReadingOnly());
  1761. TextSize = TextRect.GetSize();
  1762. }
  1763. R.Bottom = R.Top + TextSize.cy;
  1764. // Should call ChangeBiDiModeAlignment when UseRightToLeftAlignment(),
  1765. // but the label seems to draw the text wrongly aligned, even though
  1766. // the alignment is correctly flipped in TCustomStaticText.CreateParams
  1767. switch (StaticText->Alignment)
  1768. {
  1769. case taLeftJustify:
  1770. R.Right = R.Left + TextSize.cx;
  1771. break;
  1772. case taRightJustify:
  1773. R.Left = Max(0, R.Right - TextSize.cx);
  1774. break;
  1775. case taCenter:
  1776. {
  1777. DebugFail(); // not used branch, possibly untested
  1778. int Diff = R.Width() - TextSize.cx;
  1779. R.Left += Diff / 2;
  1780. R.Right -= Diff - (Diff / 2);
  1781. }
  1782. break;
  1783. }
  1784. }
  1785. __finally
  1786. {
  1787. if (ACanvas == NULL)
  1788. {
  1789. delete Canvas;
  1790. }
  1791. }
  1792. if (ACanvas != NULL)
  1793. {
  1794. *ACanvas = Canvas;
  1795. }
  1796. }
  1797. //---------------------------------------------------------------------------
  1798. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1799. bool & Clicked)
  1800. {
  1801. Clicked = false;
  1802. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1803. if (Message.Msg == WM_LBUTTONDOWN)
  1804. {
  1805. StaticText->SetFocus();
  1806. // in case the action takes long, make sure focus is shown immediately
  1807. UpdateWindow(StaticText->Handle);
  1808. Clicked = true;
  1809. Message.Result = 1;
  1810. }
  1811. else if (Message.Msg == WM_RBUTTONDOWN)
  1812. {
  1813. StaticText->SetFocus();
  1814. Message.Result = 1;
  1815. }
  1816. else if (Message.Msg == WM_CHAR)
  1817. {
  1818. if (reinterpret_cast<TWMChar &>(Message).CharCode == L' ')
  1819. {
  1820. Clicked = true;
  1821. Message.Result = 1;
  1822. }
  1823. else
  1824. {
  1825. ControlWndProc(StaticText)(Message);
  1826. }
  1827. }
  1828. else if (Message.Msg == CM_DIALOGCHAR)
  1829. {
  1830. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1831. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1832. {
  1833. StaticText->SetFocus();
  1834. // in case the action takes long, make sure focus is shown immediately
  1835. UpdateWindow(StaticText->Handle);
  1836. Clicked = true;
  1837. Message.Result = 1;
  1838. }
  1839. else
  1840. {
  1841. ControlWndProc(StaticText)(Message);
  1842. }
  1843. }
  1844. else
  1845. {
  1846. ControlWndProc(StaticText)(Message);
  1847. }
  1848. if (Message.Msg == WM_PAINT)
  1849. {
  1850. TRect R;
  1851. TCanvas * Canvas;
  1852. FocusableLabelCanvas(StaticText, &Canvas, R);
  1853. try
  1854. {
  1855. if (StaticText->Focused())
  1856. {
  1857. Canvas->DrawFocusRect(R);
  1858. }
  1859. else if ((StaticText->Font->Color != LinkColor) && // LinkActionLabel and LinkLabel
  1860. !EndsStr(LinkAppLabelMark, StaticText->Caption)) // LinkAppLabel
  1861. {
  1862. Canvas->Pen->Style = psDot;
  1863. Canvas->Brush->Style = bsClear;
  1864. if (!StaticText->Enabled)
  1865. {
  1866. Canvas->Pen->Color = clBtnHighlight;
  1867. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1868. Canvas->LineTo(R.Right + 1, R.Bottom);
  1869. Canvas->Pen->Color = clGrayText;
  1870. }
  1871. Canvas->MoveTo(R.Left, R.Bottom - 1);
  1872. Canvas->LineTo(R.Right, R.Bottom - 1);
  1873. }
  1874. }
  1875. __finally
  1876. {
  1877. delete Canvas;
  1878. }
  1879. }
  1880. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1881. (Message.Msg == CM_ENABLEDCHANGED))
  1882. {
  1883. StaticText->Invalidate();
  1884. }
  1885. }
  1886. //---------------------------------------------------------------------------
  1887. static THintWindow * PersistentHintWindow = NULL;
  1888. static TControl * PersistentHintControl = NULL;
  1889. //---------------------------------------------------------------------------
  1890. void __fastcall CancelPersistentHint()
  1891. {
  1892. if (PersistentHintWindow != NULL)
  1893. {
  1894. PersistentHintControl = NULL;
  1895. SAFE_DESTROY(PersistentHintWindow);
  1896. }
  1897. }
  1898. //---------------------------------------------------------------------------
  1899. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1900. {
  1901. CancelPersistentHint();
  1902. THintInfo HintInfo;
  1903. HintInfo.HintControl = Control;
  1904. HintInfo.HintPos = HintPos;
  1905. HintInfo.HintMaxWidth = GetParentForm(Control)->Monitor->Width;
  1906. HintInfo.HintColor = Application->HintColor;
  1907. HintInfo.HintStr = GetShortHint(Control->Hint);
  1908. HintInfo.HintData = NULL;
  1909. bool CanShow = true;
  1910. if (Application->OnShowHint != NULL)
  1911. {
  1912. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1913. }
  1914. if (CanShow)
  1915. {
  1916. PersistentHintControl = Control;
  1917. PersistentHintWindow = new TScreenTipHintWindow(Application);
  1918. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1919. PersistentHintWindow->Color = HintInfo.HintColor;
  1920. TRect HintWinRect;
  1921. if (HintInfo.HintMaxWidth < Control->Width)
  1922. {
  1923. HintInfo.HintMaxWidth = Control->Width;
  1924. }
  1925. HintWinRect = PersistentHintWindow->CalcHintRect(
  1926. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1927. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1928. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1929. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1930. }
  1931. }
  1932. //---------------------------------------------------------------------------
  1933. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1934. {
  1935. bool Clicked = false;
  1936. bool Cancel = false;
  1937. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1938. if (Message.Msg == CM_HINTSHOW)
  1939. {
  1940. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1941. if (PersistentHintControl == StaticText)
  1942. {
  1943. // do not allow standard hint when persistent is already shown
  1944. HintShow.Result = 1;
  1945. }
  1946. else
  1947. {
  1948. HintShow.HintInfo->HideTimeout = 100000; // never
  1949. }
  1950. }
  1951. else if (Message.Msg == CN_KEYDOWN)
  1952. {
  1953. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1954. (PersistentHintControl == StaticText))
  1955. {
  1956. CancelPersistentHint();
  1957. StaticText->Invalidate();
  1958. Message.Result = 1;
  1959. }
  1960. else
  1961. {
  1962. FocusableLabelWindowProc(Data, Message, Clicked);
  1963. }
  1964. }
  1965. else
  1966. {
  1967. FocusableLabelWindowProc(Data, Message, Clicked);
  1968. }
  1969. if (Message.Msg == CM_CANCELMODE)
  1970. {
  1971. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  1972. if ((CancelMessage.Sender != StaticText) &&
  1973. (CancelMessage.Sender != PersistentHintWindow))
  1974. {
  1975. Cancel = true;
  1976. }
  1977. }
  1978. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  1979. {
  1980. Cancel = true;
  1981. }
  1982. if (Cancel && (PersistentHintControl == StaticText))
  1983. {
  1984. CancelPersistentHint();
  1985. }
  1986. if (Clicked && (PersistentHintControl != StaticText))
  1987. {
  1988. TRect R;
  1989. TPoint HintPos;
  1990. FocusableLabelCanvas(StaticText, NULL, R);
  1991. HintPos.y = R.Bottom - R.Top;
  1992. HintPos.x = R.Left;
  1993. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  1994. }
  1995. }
  1996. //---------------------------------------------------------------------------
  1997. void __fastcall HintLabel(TStaticText * StaticText, UnicodeString Hint)
  1998. {
  1999. // Currently all are right-justified, when other alignment is used,
  2000. // test respective branches in FocusableLabelCanvas.
  2001. DebugAssert(StaticText->Alignment == taRightJustify);
  2002. // With right-justify, it has to be off. We may not notice on original
  2003. // English version, results will differ with translations only
  2004. DebugAssert(!StaticText->AutoSize);
  2005. StaticText->ParentFont = true;
  2006. if (!Hint.IsEmpty())
  2007. {
  2008. StaticText->Hint = Hint;
  2009. }
  2010. StaticText->ShowHint = true;
  2011. StaticText->Cursor = crHandPoint;
  2012. TWndMethod WindowProc;
  2013. ((TMethod*)&WindowProc)->Data = StaticText;
  2014. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  2015. StaticText->WindowProc = WindowProc;
  2016. }
  2017. //---------------------------------------------------------------------------
  2018. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  2019. {
  2020. // it is TCustomComboxBox, but the properties are published only by TComboBox
  2021. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  2022. if (Message.Msg == WM_SIZE)
  2023. {
  2024. UnicodeString Text = ComboBox->Text;
  2025. try
  2026. {
  2027. ControlWndProc(ComboBox)(Message);
  2028. }
  2029. __finally
  2030. {
  2031. // workaround for bug in combo box, that causes it to change text to any
  2032. // item from drop down list which starts with current text,
  2033. // after control is resized (unless the text is in drop down list as well)
  2034. ComboBox->Text = Text;
  2035. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  2036. if (!ComboBox->Focused())
  2037. {
  2038. ComboBox->SelLength = 0;
  2039. }
  2040. }
  2041. }
  2042. else
  2043. {
  2044. ControlWndProc(ComboBox)(Message);
  2045. }
  2046. }
  2047. //---------------------------------------------------------------------------
  2048. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  2049. {
  2050. TWndMethod WindowProc;
  2051. ((TMethod*)&WindowProc)->Data = ComboBox;
  2052. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  2053. ComboBox->WindowProc = WindowProc;
  2054. }
  2055. //---------------------------------------------------------------------------
  2056. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  2057. {
  2058. if (StaticText->OnClick != NULL)
  2059. {
  2060. StaticText->OnClick(StaticText);
  2061. }
  2062. else
  2063. {
  2064. OpenBrowser(StaticText->Caption);
  2065. }
  2066. }
  2067. //---------------------------------------------------------------------------
  2068. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  2069. {
  2070. bool Clicked = false;
  2071. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  2072. if (Message.Msg == WM_CONTEXTMENU)
  2073. {
  2074. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  2075. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  2076. {
  2077. TRect R;
  2078. FocusableLabelCanvas(StaticText, NULL, R);
  2079. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  2080. ContextMenu.Pos.x = static_cast<short>(P.x);
  2081. ContextMenu.Pos.y = static_cast<short>(P.y);
  2082. }
  2083. }
  2084. else if (Message.Msg == WM_KEYDOWN)
  2085. {
  2086. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  2087. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == L'C'))
  2088. {
  2089. TInstantOperationVisualizer Visualizer;
  2090. CopyToClipboard(StaticText->Caption);
  2091. Message.Result = 1;
  2092. }
  2093. else
  2094. {
  2095. FocusableLabelWindowProc(Data, Message, Clicked);
  2096. }
  2097. }
  2098. FocusableLabelWindowProc(Data, Message, Clicked);
  2099. if (Message.Msg == WM_DESTROY)
  2100. {
  2101. delete StaticText->PopupMenu;
  2102. DebugAssert(StaticText->PopupMenu == NULL);
  2103. }
  2104. if (Clicked)
  2105. {
  2106. LinkLabelClick(StaticText);
  2107. }
  2108. }
  2109. //---------------------------------------------------------------------------
  2110. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  2111. {
  2112. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  2113. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  2114. DebugAssert(MenuItem != NULL);
  2115. if (MenuItem->Tag == 0)
  2116. {
  2117. LinkLabelClick(StaticText);
  2118. }
  2119. else
  2120. {
  2121. TInstantOperationVisualizer Visualizer;
  2122. CopyToClipboard(StaticText->Caption);
  2123. }
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. static void __fastcall DoLinkLabel(TStaticText * StaticText)
  2127. {
  2128. StaticText->Transparent = false;
  2129. StaticText->ParentFont = true;
  2130. StaticText->Cursor = crHandPoint;
  2131. TWndMethod WindowProc;
  2132. ((TMethod*)&WindowProc)->Data = StaticText;
  2133. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  2134. StaticText->WindowProc = WindowProc;
  2135. }
  2136. //---------------------------------------------------------------------------
  2137. void __fastcall LinkLabel(TStaticText * StaticText, UnicodeString Url,
  2138. TNotifyEvent OnEnter)
  2139. {
  2140. DoLinkLabel(StaticText);
  2141. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  2142. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  2143. if (!Url.IsEmpty())
  2144. {
  2145. StaticText->Caption = Url;
  2146. }
  2147. bool IsUrl = IsHttpOrHttpsUrl(StaticText->Caption);
  2148. if (IsUrl)
  2149. {
  2150. DebugAssert(StaticText->PopupMenu == NULL);
  2151. StaticText->PopupMenu = new TPopupMenu(StaticText);
  2152. try
  2153. {
  2154. TNotifyEvent ContextMenuOnClick;
  2155. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  2156. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  2157. TMenuItem * Item;
  2158. Item = new TMenuItem(StaticText->PopupMenu);
  2159. Item->Caption = LoadStr(URL_LINK_OPEN);
  2160. Item->Tag = 0;
  2161. Item->ShortCut = ShortCut(L' ', TShiftState());
  2162. Item->OnClick = ContextMenuOnClick;
  2163. StaticText->PopupMenu->Items->Add(Item);
  2164. Item = new TMenuItem(StaticText->PopupMenu);
  2165. Item->Caption = LoadStr(EDIT_COPY);
  2166. Item->Tag = 1;
  2167. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  2168. Item->OnClick = ContextMenuOnClick;
  2169. StaticText->PopupMenu->Items->Add(Item);
  2170. }
  2171. catch(...)
  2172. {
  2173. delete StaticText->PopupMenu;
  2174. DebugAssert(StaticText->PopupMenu == NULL);
  2175. throw;
  2176. }
  2177. }
  2178. StaticText->Font->Color = LinkColor;
  2179. }
  2180. //---------------------------------------------------------------------------
  2181. void __fastcall LinkActionLabel(TStaticText * StaticText)
  2182. {
  2183. DoLinkLabel(StaticText);
  2184. StaticText->Font->Color = LinkColor;
  2185. }
  2186. //---------------------------------------------------------------------------
  2187. void __fastcall LinkAppLabel(TStaticText * StaticText)
  2188. {
  2189. DoLinkLabel(StaticText);
  2190. StaticText->Caption = StaticText->Caption + LinkAppLabelMark;
  2191. }
  2192. //---------------------------------------------------------------------------
  2193. static void __fastcall HotTrackLabelMouseEnter(void * /*Data*/, TObject * Sender)
  2194. {
  2195. reinterpret_cast<TLabel *>(Sender)->Font->Color = clBlue;
  2196. }
  2197. //---------------------------------------------------------------------------
  2198. static void __fastcall HotTrackLabelMouseLeave(void * /*Data*/, TObject * Sender)
  2199. {
  2200. reinterpret_cast<TLabel *>(Sender)->ParentFont = true;
  2201. }
  2202. //---------------------------------------------------------------------------
  2203. void __fastcall HotTrackLabel(TLabel * Label)
  2204. {
  2205. DebugAssert(Label->OnMouseEnter == NULL);
  2206. DebugAssert(Label->OnMouseLeave == NULL);
  2207. Label->OnMouseEnter = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseEnter);
  2208. Label->OnMouseLeave = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseLeave);
  2209. }
  2210. //---------------------------------------------------------------------------
  2211. void __fastcall SetLabelHintPopup(TLabel * Label, const UnicodeString & Hint)
  2212. {
  2213. Label->Caption = Hint;
  2214. Label->Hint = Hint;
  2215. TRect Rect(0, 0, Label->Width, 0);
  2216. TScreenTipHintWindow::CalcHintTextRect(Label, Label->Canvas, Rect, Label->Caption);
  2217. Label->ShowHint = (Rect.Bottom > Label->Height);
  2218. }
  2219. //---------------------------------------------------------------------------
  2220. bool __fastcall HasLabelHintPopup(TControl * Control, const UnicodeString & HintStr)
  2221. {
  2222. TLabel * HintLabel = dynamic_cast<TLabel *>(Control);
  2223. return (HintLabel != NULL) && (GetShortHint(HintLabel->Caption) == HintStr);
  2224. }
  2225. //---------------------------------------------------------------------------
  2226. Forms::TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  2227. {
  2228. Forms::TMonitor * Result;
  2229. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  2230. {
  2231. Result = Application->MainForm->Monitor;
  2232. }
  2233. else if (LastMonitor != NULL)
  2234. {
  2235. Result = LastMonitor;
  2236. }
  2237. else
  2238. {
  2239. int i = 0;
  2240. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  2241. {
  2242. i++;
  2243. }
  2244. DebugAssert(Screen->Monitors[i]->Primary);
  2245. Result = Screen->Monitors[i];
  2246. }
  2247. return Result;
  2248. }
  2249. //---------------------------------------------------------------------------
  2250. int __fastcall GetLastMonitor()
  2251. {
  2252. if (LastMonitor != NULL)
  2253. {
  2254. return LastMonitor->MonitorNum;
  2255. }
  2256. else
  2257. {
  2258. return -1;
  2259. }
  2260. }
  2261. //---------------------------------------------------------------------------
  2262. void __fastcall SetLastMonitor(int MonitorNum)
  2263. {
  2264. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  2265. {
  2266. LastMonitor = Screen->Monitors[MonitorNum];
  2267. }
  2268. else
  2269. {
  2270. LastMonitor = NULL;
  2271. }
  2272. }
  2273. //---------------------------------------------------------------------------
  2274. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  2275. {
  2276. TForm * Form;
  2277. if (Owner == NULL)
  2278. {
  2279. Owner = GetFormOwner();
  2280. }
  2281. // If there is no main form yet, make this one main.
  2282. // This:
  2283. // - Makes other forms (dialogs invoked from this one),
  2284. // be placed on the same monitor (otherwise all new forms get placed
  2285. // on primary monitor)
  2286. // - Triggers MainForm-specific code in DoFormWindowProc.
  2287. // - Shows button on taskbar
  2288. if (Application->MainForm == NULL)
  2289. {
  2290. Application->CreateForm(FormClass, &Form);
  2291. DebugAssert(Application->MainForm == Form);
  2292. }
  2293. else
  2294. {
  2295. Form = dynamic_cast<TForm *>(Construct(FormClass, Owner));
  2296. DebugAssert(Form != NULL);
  2297. }
  2298. return Form;
  2299. }
  2300. //---------------------------------------------------------------------------
  2301. bool __fastcall SupportsSplitButton()
  2302. {
  2303. return (Win32MajorVersion() >= 6);
  2304. }
  2305. //---------------------------------------------------------------------------
  2306. static TButton * __fastcall FindStandardButton(TWinControl * Control, bool Default)
  2307. {
  2308. TButton * Result = NULL;
  2309. int Index = 0;
  2310. while ((Result == NULL) && (Index < Control->ControlCount))
  2311. {
  2312. TControl * ChildControl = Control->Controls[Index];
  2313. TButton * Button = dynamic_cast<TButton *>(ChildControl);
  2314. if ((Button != NULL) && (Default ? Button->Default : Button->Cancel))
  2315. {
  2316. Result = Button;
  2317. }
  2318. else
  2319. {
  2320. TWinControl * WinControl = dynamic_cast<TWinControl *>(ChildControl);
  2321. if (WinControl != NULL)
  2322. {
  2323. Result = FindStandardButton(WinControl, Default);
  2324. }
  2325. }
  2326. Index++;
  2327. }
  2328. return Result;
  2329. }
  2330. //---------------------------------------------------------------------------
  2331. TModalResult __fastcall DefaultResult(TCustomForm * Form, TButton * DefaultButton)
  2332. {
  2333. // The point of this is to avoid hardcoding mrOk when checking dialog results.
  2334. // Previously we used != mrCancel instead, as mrCancel is more reliable,
  2335. // being automatically used for Esc/X buttons (and hence kind of forced to be used
  2336. // for Cancel buttons). But that failed to be reliable in the end, for
  2337. // ModalResult being mrNone, when Windows session is being logged off.
  2338. // We interpreted mrNone as OK, causing lots of troubles.
  2339. TModalResult Result = mrNone;
  2340. TButton * Button = FindStandardButton(Form, true);
  2341. if (DebugAlwaysTrue(Button != NULL))
  2342. {
  2343. Result = Button->ModalResult;
  2344. }
  2345. if (Result == mrNone)
  2346. {
  2347. DebugAssert((DefaultButton != NULL) && (DefaultButton->ModalResult != mrNone));
  2348. Result = DefaultButton->ModalResult;
  2349. }
  2350. else
  2351. {
  2352. // If default button fallback was provided,
  2353. // make sure it is the default button we actually detected
  2354. DebugAssert((DefaultButton == NULL) || (Button == DefaultButton));
  2355. }
  2356. return Result;
  2357. }
  2358. //---------------------------------------------------------------------------
  2359. void __fastcall DefaultButton(TButton * Button, bool Default)
  2360. {
  2361. // default property setter does not have guard for "the same value"
  2362. if (Button->Default != Default)
  2363. {
  2364. Button->Default = Default;
  2365. }
  2366. }
  2367. //---------------------------------------------------------------------------
  2368. void __fastcall MemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  2369. {
  2370. // Sender can be Form or Memo itself
  2371. TControl * Control = dynamic_cast<TControl *>(Sender);
  2372. if (DebugAlwaysTrue(Control != NULL))
  2373. {
  2374. TCustomForm * Form = GetParentForm(Control);
  2375. // Particularly when WantReturns is true,
  2376. // memo swallows also Esc, so we have to handle it ourselves.
  2377. // See also ReadOnlyControl.
  2378. if ((Key == VK_ESCAPE) && Shift.Empty())
  2379. {
  2380. Form->ModalResult = mrCancel;
  2381. Key = 0;
  2382. }
  2383. else if ((Key == VK_RETURN) && Shift.Contains(ssCtrl))
  2384. {
  2385. Form->ModalResult = DefaultResult(Form);
  2386. Key = 0;
  2387. }
  2388. }
  2389. }
  2390. //---------------------------------------------------------------------------
  2391. class TIconOwnerComponent : public TComponent
  2392. {
  2393. public:
  2394. __fastcall TIconOwnerComponent(TIcon * Icon) :
  2395. TComponent(NULL),
  2396. FIcon(Icon)
  2397. {
  2398. }
  2399. private:
  2400. std::unique_ptr<TIcon> FIcon;
  2401. };
  2402. //---------------------------------------------------------------------------
  2403. static void __fastcall FixFormIcon(TForm * Form, int Size, int WidthMetric, int HeightMetric)
  2404. {
  2405. // Whole this code is to call ReadIcon from Vcl.Graphics.pas with correct size
  2406. // Clone the icon data (whole .ico file content, that is originally loaded from .dfm)
  2407. // to a new TIcon that does not have a size fixed yet (size cannot be changed after handle is allocated)
  2408. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  2409. Form->Icon->SaveToStream(Stream.get());
  2410. std::unique_ptr<TIcon> Icon(new TIcon());
  2411. Stream->Position = 0;
  2412. Icon->LoadFromStream(Stream.get());
  2413. // Set desired size
  2414. int Width = GetSystemMetricsForControl(Form, WidthMetric);
  2415. int Height = GetSystemMetricsForControl(Form, HeightMetric);
  2416. Icon->SetSize(Width, Height);
  2417. // This calls TIcon::RequireHandle that retrieves the best icon for given size
  2418. LPARAM LParam = reinterpret_cast<LPARAM>(Icon->Handle);
  2419. SendMessage(Form->Handle, WM_SETICON, Size, LParam);
  2420. // Make sure the icon is released
  2421. TIconOwnerComponent * IconOwnerComponent = new TIconOwnerComponent(Icon.release());
  2422. IconOwnerComponent->Name = TIconOwnerComponent::QualifiedClassName() + IntToStr(Size);
  2423. Form->InsertComponent(IconOwnerComponent);
  2424. }
  2425. //---------------------------------------------------------------------------
  2426. void __fastcall FixFormIcons(TForm * Form)
  2427. {
  2428. // VCL sets only ICON_BIG (so small icon is scaled down by OS from big icon),
  2429. // and it uses a random (first?) size from the resource,
  2430. // not the best size.
  2431. FixFormIcon(Form, ICON_SMALL, SM_CXSMICON, SM_CYSMICON);
  2432. FixFormIcon(Form, ICON_BIG, SM_CXICON, SM_CYICON);
  2433. // We rely on VCL not calling WM_SETICON ever after
  2434. // (what it would do, if CreateWnd is called again).
  2435. // That would overwrite the ICON_BIG.
  2436. // We might be able to make sure it uses a correct size by calling
  2437. // TIcon.ReleaseHandle and setting a correct size.
  2438. }
  2439. //---------------------------------------------------------------------------
  2440. class TDesktopFontManager : public TComponent
  2441. {
  2442. public:
  2443. TDesktopFontManager();
  2444. virtual __fastcall ~TDesktopFontManager();
  2445. void AddControl(TControl * Control);
  2446. void Update();
  2447. protected:
  2448. virtual void __fastcall Notification(TComponent * AComponent, TOperation Operation);
  2449. private:
  2450. HWND FWindowHandle;
  2451. typedef std::set<TControl *> TControlSet;
  2452. TControlSet FControls;
  2453. void __fastcall WndProc(TMessage & Message);
  2454. void UpdateControl(TControl * Control);
  2455. };
  2456. //---------------------------------------------------------------------------
  2457. TDesktopFontManager::TDesktopFontManager() : TComponent(NULL)
  2458. {
  2459. // Alternative is using Application->HookMainWindow
  2460. FWindowHandle = AllocateHWnd(WndProc);
  2461. }
  2462. //---------------------------------------------------------------------------
  2463. __fastcall TDesktopFontManager::~TDesktopFontManager()
  2464. {
  2465. DeallocateHWnd(FWindowHandle);
  2466. }
  2467. //---------------------------------------------------------------------------
  2468. void __fastcall TDesktopFontManager::Notification(TComponent * AComponent, TOperation Operation)
  2469. {
  2470. if (DebugAlwaysTrue(Operation == opRemove))
  2471. {
  2472. TControl * Control = DebugNotNull(dynamic_cast<TControl *>(AComponent));
  2473. FControls.erase(Control);
  2474. }
  2475. TComponent::Notification(AComponent, Operation);
  2476. }
  2477. //---------------------------------------------------------------------------
  2478. void TDesktopFontManager::UpdateControl(TControl * Control)
  2479. {
  2480. class TPublicControl : public TControl
  2481. {
  2482. public:
  2483. __property Font;
  2484. };
  2485. TPublicControl * PublicControl = reinterpret_cast<TPublicControl *>(Control);
  2486. std::unique_ptr<TFont> DesktopFont(new TFont());
  2487. if (WinConfiguration->PanelFont.FontName.IsEmpty())
  2488. {
  2489. int PixelsPerInch = GetControlPixelsPerInch(Control);
  2490. TLogFont LogFont;
  2491. if (DebugAlwaysTrue(SystemParametersInfoForPixelsPerInch(SPI_GETICONTITLELOGFONT, sizeof(LogFont), &LogFont, 0, PixelsPerInch)))
  2492. {
  2493. DesktopFont->Handle = CreateFontIndirect(&LogFont);
  2494. }
  2495. }
  2496. else
  2497. {
  2498. TWinConfiguration::RestoreFont(WinConfiguration->PanelFont, DesktopFont.get());
  2499. DesktopFont->Height = ScaleByPixelsPerInchFromSystem(DesktopFont->Height, Control);
  2500. DesktopFont->PixelsPerInch = PublicControl->Font->PixelsPerInch;
  2501. }
  2502. // Neither CreateFontIndirect nor RestoreFont set color, so we should have the default set by TFont constructor here.
  2503. DebugAssert(DesktopFont->Color == clWindowText);
  2504. if (!SameFont(DesktopFont.get(), PublicControl->Font) ||
  2505. (DesktopFont->PixelsPerInch != PublicControl->Font->PixelsPerInch))
  2506. {
  2507. // Preserve color (particularly white color of file panel font in dark mode)
  2508. DesktopFont->Color = PublicControl->Font->Color;
  2509. PublicControl->Font->Assign(DesktopFont.get());
  2510. }
  2511. }
  2512. //---------------------------------------------------------------------------
  2513. void TDesktopFontManager::AddControl(TControl * Control)
  2514. {
  2515. FControls.insert(Control);
  2516. Control->FreeNotification(this);
  2517. UpdateControl(Control);
  2518. }
  2519. //---------------------------------------------------------------------------
  2520. void TDesktopFontManager::Update()
  2521. {
  2522. TControlSet::iterator I = FControls.begin();
  2523. while (I != FControls.end())
  2524. {
  2525. UpdateControl(*I);
  2526. I++;
  2527. }
  2528. }
  2529. //---------------------------------------------------------------------------
  2530. void __fastcall TDesktopFontManager::WndProc(TMessage & Message)
  2531. {
  2532. if (Message.Msg == WM_WININICHANGE)
  2533. {
  2534. Update();
  2535. }
  2536. Message.Result = DefWindowProc(FWindowHandle, Message.Msg, Message.WParam, Message.LParam);
  2537. }
  2538. //---------------------------------------------------------------------------
  2539. std::unique_ptr<TDesktopFontManager> DesktopFontManager(new TDesktopFontManager());
  2540. //---------------------------------------------------------------------------
  2541. void __fastcall UseDesktopFont(TControl * Control)
  2542. {
  2543. TCustomStatusBar * StatusBar = dynamic_cast<TCustomStatusBar *>(Control);
  2544. if (StatusBar != NULL)
  2545. {
  2546. // prevent syncing to system font
  2547. StatusBar->UseSystemFont = false;
  2548. }
  2549. DesktopFontManager->AddControl(Control);
  2550. }
  2551. //---------------------------------------------------------------------------
  2552. void __fastcall UpdateDesktopFont()
  2553. {
  2554. DesktopFontManager->Update();
  2555. }
  2556. //---------------------------------------------------------------------------
  2557. TShiftState __fastcall AllKeyShiftStates()
  2558. {
  2559. return TShiftState() << ssShift << ssAlt << ssCtrl;
  2560. }
  2561. //---------------------------------------------------------------------------
  2562. static bool __fastcall FormActivationHook(void * Data, TMessage & Message)
  2563. {
  2564. bool Result = false;
  2565. // Some dialogs, when application is restored from minimization,
  2566. // do not get activated. So we do it explicitly here.
  2567. // We cannot do this from TApplication::OnActivate because
  2568. // TApplication.WndProc resets focus to the last active window afterwards.
  2569. // So we override CM_ACTIVATE implementation here completely.
  2570. if (Message.Msg == CM_ACTIVATE)
  2571. {
  2572. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  2573. if (Screen->FocusedForm == Form)
  2574. {
  2575. ::SetFocus(Form->Handle);
  2576. // VCLCOPY
  2577. if (Application->OnActivate != NULL)
  2578. {
  2579. Application->OnActivate(Application);
  2580. }
  2581. Result = true;
  2582. }
  2583. }
  2584. return Result;
  2585. }
  2586. //---------------------------------------------------------------------------
  2587. void __fastcall HookFormActivation(TCustomForm * Form)
  2588. {
  2589. Application->HookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2590. }
  2591. //---------------------------------------------------------------------------
  2592. void __fastcall UnhookFormActivation(TCustomForm * Form)
  2593. {
  2594. Application->UnhookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2595. }
  2596. //---------------------------------------------------------------------------
  2597. void __fastcall ShowFormNoActivate(TForm * Form)
  2598. {
  2599. TPublicForm * PublicForm = static_cast<TPublicForm *>(Form);
  2600. // This is same as SendToBack, except for added SWP_NOACTIVATE (VCLCOPY)
  2601. SetWindowPos(PublicForm->WindowHandle, HWND_BOTTOM, 0, 0, 0, 0,
  2602. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  2603. // This replaces TCustomForm::CMShowingChanged()
  2604. // which calls ShowWindow(Handle, SW_SHOWNORMAL).
  2605. ShowWindow(Form->Handle, SW_SHOWNOACTIVATE);
  2606. // - so we have to call DoShow explicitly.
  2607. PublicForm->DoShow();
  2608. // - also we skip applying TForm::Position (VCLCOPY)
  2609. if (DebugAlwaysTrue(Form->Position == poOwnerFormCenter))
  2610. {
  2611. TCustomForm * CenterForm = Application->MainForm;
  2612. TCustomForm * OwnerForm = dynamic_cast<TCustomForm *>(Form->Owner);
  2613. if (OwnerForm != NULL)
  2614. {
  2615. CenterForm = OwnerForm;
  2616. }
  2617. int X, Y;
  2618. if ((CenterForm != NULL) && (CenterForm != Form))
  2619. {
  2620. TRect Bounds = CenterForm->BoundsRect;
  2621. X = ((Bounds.Width() - Form->Width) / 2) + CenterForm->Left;
  2622. Y = ((Bounds.Height() - Form->Height) / 2) + CenterForm->Top;
  2623. }
  2624. else
  2625. {
  2626. X = (Screen->Width - Form->Width) / 2;
  2627. Y = (Screen->Height - Form->Height) / 2;
  2628. }
  2629. if (X < Screen->DesktopLeft)
  2630. {
  2631. X = Screen->DesktopLeft;
  2632. }
  2633. if (Y < Screen->DesktopTop)
  2634. {
  2635. Y = Screen->DesktopTop;
  2636. }
  2637. Form->SetBounds(X, Y, Form->Width, Form->Height);
  2638. // We cannot call SetWindowToMonitor().
  2639. // We cannot set FPosition = poDesigned, so workarea-checking code
  2640. // in DoFormWindowProc is not triggered
  2641. // If application is restored, dialog is not activated, do it manually.
  2642. // Wait for application to be activated to activate ourself.
  2643. HookFormActivation(Form);
  2644. }
  2645. }
  2646. //---------------------------------------------------------------------------
  2647. TPanel * __fastcall CreateBlankPanel(TComponent * Owner)
  2648. {
  2649. TPanel * Panel = new TPanel(Owner);
  2650. Panel->BevelOuter = bvNone;
  2651. Panel->BevelInner = bvNone; // default
  2652. Panel->BevelKind = bkNone;
  2653. return Panel;
  2654. }
  2655. //---------------------------------------------------------------------------
  2656. bool IsButtonBeingClicked(TButtonControl * Button)
  2657. {
  2658. class TPublicButtonControl : public TButtonControl
  2659. {
  2660. public:
  2661. __property ClicksDisabled;
  2662. };
  2663. TPublicButtonControl * PublicButton = reinterpret_cast<TPublicButtonControl *>(Button);
  2664. // HACK ClicksDisabled is set in TButtonControl.WndProc while changing focus as response to WM_LBUTTONDOWN.
  2665. return PublicButton->ClicksDisabled;
  2666. }
  2667. //---------------------------------------------------------------------------
  2668. // When using this in OnExit handers, it's still possible that the user does not actually click the
  2669. // CancelButton (for example, when the button is released out of the button).
  2670. // Then the validation is bypassed. Consequently, all dialogs that uses this must still
  2671. // gracefully handle submission with non-validated data.
  2672. bool IsCancelButtonBeingClicked(TControl * Control)
  2673. {
  2674. TCustomForm * Form = GetParentForm(Control);
  2675. TButtonControl * CancelButton = FindStandardButton(Form, false);
  2676. // Find dialog has no Cancel button
  2677. return (CancelButton != NULL) && IsButtonBeingClicked(CancelButton);
  2678. }
  2679. //---------------------------------------------------------------------------
  2680. TCanvas * CreateControlCanvas(TControl * Control)
  2681. {
  2682. std::unique_ptr<TControlCanvas> Canvas(new TControlCanvas());
  2683. Canvas->Control = Control;
  2684. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  2685. Canvas->Font = PublicControl->Font;
  2686. return Canvas.release();
  2687. }
  2688. //---------------------------------------------------------------------------
  2689. void AutoSizeButton(TButton * Button)
  2690. {
  2691. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(Button));
  2692. int MinWidth = Canvas->TextWidth(Button->Caption) + ScaleByTextHeight(Button, (2 * 8));
  2693. if (Button->Width < MinWidth)
  2694. {
  2695. if (Button->Anchors.Contains(akRight))
  2696. {
  2697. Button->Left = Button->Left - (MinWidth - Button->Width);
  2698. }
  2699. Button->Width = MinWidth;
  2700. }
  2701. }
  2702. //---------------------------------------------------------------------------
  2703. template<class T>
  2704. bool DoAutoSizeLabel(T * Label, TCanvas * Canvas)
  2705. {
  2706. TRect TextRect;
  2707. SetRect(&TextRect, 0, 0, Label->Width, 0);
  2708. DrawText(Canvas->Handle, Label->Caption.c_str(), Label->Caption.Length() + 1, &TextRect,
  2709. DT_EXPANDTABS | DT_CALCRECT | DT_WORDBREAK | DT_NOPREFIX |
  2710. Label->DrawTextBiDiModeFlagsReadingOnly());
  2711. bool Result = (TextRect.Height() > Label->Height);
  2712. if (Result)
  2713. {
  2714. Label->Height = TextRect.Height();
  2715. Label->AutoSize = false;
  2716. }
  2717. return Result;
  2718. }
  2719. //---------------------------------------------------------------------------
  2720. void AutoSizeLabel(TLabel * Label)
  2721. {
  2722. if (!DoAutoSizeLabel(Label, Label->Canvas))
  2723. {
  2724. Label->WordWrap = false;
  2725. }
  2726. }
  2727. //---------------------------------------------------------------------------
  2728. void AutoSizeLabel(TStaticText * Label)
  2729. {
  2730. std::unique_ptr<TCanvas> Canvas(CreateControlCanvas(Label));
  2731. DoAutoSizeLabel(Label, Canvas.get());
  2732. }//---------------------------------------------------------------------------
  2733. void GiveTBItemPriority(TTBCustomItem * Item)
  2734. {
  2735. DebugAssert(Item->GetTopComponent() != NULL);
  2736. TTBCustomToolbar * ToolbarComponent = dynamic_cast<TTBCustomToolbar *>(Item->GetTopComponent());
  2737. if ((ToolbarComponent != NULL) &&
  2738. // Only for top-level buttons on custom command toolbar, not for submenus of the custom commands menu in the main menu
  2739. (Item->Parent == ToolbarComponent->Items))
  2740. {
  2741. TTBItemViewer * Viewer = ToolbarComponent->View->Find(Item);
  2742. ToolbarComponent->View->GivePriority(Viewer);
  2743. }
  2744. }
  2745. //---------------------------------------------------------------------------
  2746. void DeleteChildren(TWinControl * Control)
  2747. {
  2748. while (Control->ControlCount > 0)
  2749. {
  2750. delete Control->Controls[0];
  2751. }
  2752. }