VCLCommon.cpp 95 KB

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