VCLCommon.cpp 93 KB

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