VCLCommon.cpp 95 KB

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