VCLCommon.cpp 93 KB

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