VCLCommon.cpp 93 KB

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