VCLCommon.cpp 96 KB

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