VCLCommon.cpp 94 KB

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