VCLCommon.cpp 95 KB

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