VCLCommon.cpp 96 KB

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