VCLCommon.cpp 96 KB

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