VCLCommon.cpp 94 KB

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