VCLCommon.cpp 95 KB

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