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