VCLCommon.cpp 92 KB

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