VCLCommon.cpp 96 KB

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