VCLCommon.cpp 92 KB

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