VCLCommon.cpp 93 KB

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