VCLCommon.cpp 95 KB

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