VCLCommon.cpp 95 KB

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