VCLCommon.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998
  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 <FileCtrl.hpp>
  13. #include <PathLabel.hpp>
  14. #include <PasTools.hpp>
  15. #include <StrUtils.hpp>
  16. #include <Vcl.Imaging.pngimage.hpp>
  17. #include <Math.hpp>
  18. //---------------------------------------------------------------------------
  19. #pragma package(smart_init)
  20. //---------------------------------------------------------------------------
  21. void __fastcall FixListColumnWidth(TListView * TListView, int Index)
  22. {
  23. if (Index < 0)
  24. {
  25. Index = TListView->Columns->Count + Index;
  26. }
  27. TListView->Column[Index]->Tag = 1;
  28. }
  29. //---------------------------------------------------------------------------
  30. static int __fastcall GetColumnTextWidth(TListView * ListView, int ColumnPadding, const UnicodeString & Text)
  31. {
  32. return
  33. ColumnPadding +
  34. ListView->Canvas->TextExtent(Text).Width;
  35. }
  36. //---------------------------------------------------------------------------
  37. void __fastcall AutoSizeListColumnsWidth(TListView * ListView, int ColumnToShrinkIndex)
  38. {
  39. // preallocate handle to precreate columns, otherwise our changes may get
  40. // overwritten once the handle is created
  41. ListView->HandleNeeded();
  42. SendMessage(ListView->Handle, WM_SETREDRAW, false, 0);
  43. try
  44. {
  45. int ColumnPadding = 2 * ScaleByTextHeightRunTime(ListView, 6);
  46. int ColumnShrinkMinWidth = ScaleByTextHeightRunTime(ListView, 60);
  47. int ResizableWidth = 0;
  48. int NonResizableWidth = 0;
  49. int LastResizable = -1;
  50. for (int Index = 0; Index < ListView->Columns->Count; Index++)
  51. {
  52. TListColumn * Column = ListView->Columns->Items[Index];
  53. int CaptionWidth = GetColumnTextWidth(ListView, ColumnPadding, Column->Caption);
  54. int OrigWidth = -1;
  55. bool NonResizable = (Column->Tag != 0);
  56. if (NonResizable) // optimization
  57. {
  58. OrigWidth = ListView_GetColumnWidth(ListView->Handle, Index);
  59. }
  60. int Width;
  61. // LVSCW_AUTOSIZE does not work for OwnerData list views
  62. if (!ListView->OwnerData)
  63. {
  64. ListView_SetColumnWidth(ListView->Handle, Index, LVSCW_AUTOSIZE);
  65. Width = ListView_GetColumnWidth(ListView->Handle, Index);
  66. }
  67. else
  68. {
  69. // For the first column, we can simulate header double-click
  70. if (Index == 0)
  71. {
  72. HWND HeaderHandle = ListView_GetHeader(ListView->Handle);
  73. UINT_PTR From = GetDlgCtrlID(HeaderHandle);
  74. NMHEADER NMHeader;
  75. NMHeader.hdr.idFrom = From;
  76. NMHeader.hdr.hwndFrom = HeaderHandle;
  77. NMHeader.hdr.code = HDN_DIVIDERDBLCLICK;
  78. NMHeader.iItem = Index;
  79. NMHeader.iButton = 0;
  80. NMHeader.pitem = NULL;
  81. SendMessage(ListView->Handle, WM_NOTIFY, From, reinterpret_cast<LPARAM>(&NMHeader));
  82. Width = ListView_GetColumnWidth(ListView->Handle, Index);
  83. }
  84. else
  85. {
  86. // For sub columns, header double click does not work reliably.
  87. // It resizes based on the focused line only and even then not consistently.
  88. Width = 0;
  89. for (int ItemIndex = 0; ItemIndex < ListView->Items->Count; ItemIndex++)
  90. {
  91. TListItem * Item = ListView->Items->Item[ItemIndex];
  92. if (Index < Item->SubItems->Count)
  93. {
  94. UnicodeString Text = Item->SubItems->Strings[Index - 1];
  95. int TextWidth = GetColumnTextWidth(ListView, ColumnPadding, Text);
  96. if (TextWidth > Width)
  97. {
  98. Width = TextWidth;
  99. }
  100. }
  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. assert(LastResizable >= 0);
  122. int ClientWidth = ListView->ClientWidth;
  123. int RowCount = ListView->Items->Count;
  124. if ((ListView->VisibleRowCount < RowCount) &&
  125. (ListView->Width - ListView->ClientWidth < GetSystemMetrics(SM_CXVSCROLL)))
  126. {
  127. ClientWidth -= GetSystemMetrics(SM_CXVSCROLL);
  128. }
  129. if (ALWAYS_TRUE(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 proprotionak 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. ListView->Columns->Items[ColumnToShrinkIndex]->Width =
  146. Remaining - (ResizableWidth - ListView->Columns->Items[ColumnToShrinkIndex]->Width);
  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. assert(Remaining == 0);
  175. }
  176. }
  177. }
  178. __finally
  179. {
  180. SendMessage(ListView->Handle, WM_SETREDRAW, true, 0);
  181. }
  182. }
  183. //---------------------------------------------------------------------------
  184. static void __fastcall SetParentColor(TControl * Control)
  185. {
  186. TColor Color = clBtnFace;
  187. ((TEdit*)Control)->Color = Color;
  188. }
  189. //---------------------------------------------------------------------------
  190. void __fastcall EnableControl(TControl * Control, bool Enable)
  191. {
  192. if (Control->Enabled != Enable)
  193. {
  194. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  195. if ((WinControl != NULL) &&
  196. (WinControl->ControlCount > 0))
  197. {
  198. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  199. {
  200. EnableControl(WinControl->Controls[Index], Enable);
  201. }
  202. }
  203. Control->Enabled = Enable;
  204. }
  205. if ((dynamic_cast<TCustomEdit *>(Control) != NULL) ||
  206. (dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  207. (dynamic_cast<TCustomListView *>(Control) != NULL) ||
  208. (dynamic_cast<TTreeView *>(Control) != NULL))
  209. {
  210. if (Enable)
  211. {
  212. ((TEdit*)Control)->Color = clWindow;
  213. }
  214. else
  215. {
  216. ((TEdit*)Control)->Color = clBtnFace;
  217. }
  218. }
  219. };
  220. //---------------------------------------------------------------------------
  221. void __fastcall ReadOnlyControl(TControl * Control, bool ReadOnly)
  222. {
  223. if (dynamic_cast<TCustomEdit *>(Control) != NULL)
  224. {
  225. ((TEdit*)Control)->ReadOnly = ReadOnly;
  226. TMemo * Memo = dynamic_cast<TMemo *>(Control);
  227. if (ReadOnly)
  228. {
  229. SetParentColor(Control);
  230. if (Memo != NULL)
  231. {
  232. // Is true by default and makes the control swallow not only
  233. // returns but also escapes.
  234. // See also MemoKeyDown
  235. Memo->WantReturns = false;
  236. }
  237. }
  238. else
  239. {
  240. ((TEdit*)Control)->Color = clWindow;
  241. // not supported atm, we need to persist previous value of WantReturns
  242. assert(Memo == NULL);
  243. }
  244. }
  245. else if ((dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  246. (dynamic_cast<TCustomTreeView *>(Control) != NULL))
  247. {
  248. EnableControl(Control, !ReadOnly);
  249. }
  250. else
  251. {
  252. FAIL;
  253. }
  254. }
  255. //---------------------------------------------------------------------------
  256. static TForm * MainLikeForm = NULL;
  257. //---------------------------------------------------------------------------
  258. TForm * __fastcall GetMainForm()
  259. {
  260. TForm * Result;
  261. if (MainLikeForm != NULL)
  262. {
  263. Result = MainLikeForm;
  264. }
  265. else
  266. {
  267. Result = Application->MainForm;
  268. }
  269. return Result;
  270. }
  271. //---------------------------------------------------------------------------
  272. bool __fastcall IsMainFormHidden()
  273. {
  274. bool Result = (GetMainForm() != NULL) && !GetMainForm()->Visible;
  275. // we do not expect this to return true when MainLikeForm is set
  276. assert(!Result || (MainLikeForm == NULL));
  277. return Result;
  278. }
  279. //---------------------------------------------------------------------------
  280. bool __fastcall IsMainFormLike(TCustomForm * Form)
  281. {
  282. return
  283. (GetMainForm() == Form) ||
  284. // this particularly happens if error occurs while main
  285. // window is being shown (e.g. non existent local directory when opening
  286. // explorer)
  287. IsMainFormHidden();
  288. }
  289. //---------------------------------------------------------------------------
  290. UnicodeString __fastcall FormatMainFormCaption(const UnicodeString & Caption)
  291. {
  292. UnicodeString Result = Caption;
  293. if (Result.IsEmpty())
  294. {
  295. Result = AppName;
  296. }
  297. else
  298. {
  299. UnicodeString Suffix = L" - " + AppName;
  300. if (!EndsStr(Suffix, Result))
  301. {
  302. Result += Suffix;
  303. }
  304. }
  305. return Result;
  306. }
  307. //---------------------------------------------------------------------------
  308. UnicodeString __fastcall FormatFormCaption(TCustomForm * Form, const UnicodeString & Caption)
  309. {
  310. UnicodeString Result = Caption;
  311. if (IsMainFormLike(Form))
  312. {
  313. Result = FormatMainFormCaption(Result);
  314. }
  315. return Result;
  316. }
  317. //---------------------------------------------------------------------------
  318. struct TSavedSystemSettings
  319. {
  320. TCustomForm * Form;
  321. UnicodeString FontName;
  322. bool Flipped;
  323. TWndMethod OldWndProc;
  324. };
  325. //---------------------------------------------------------------------------
  326. class TPublicWinControl : public TWinControl
  327. {
  328. friend TWndMethod __fastcall ControlWndProc(TWinControl * Control);
  329. };
  330. //---------------------------------------------------------------------------
  331. TWndMethod __fastcall ControlWndProc(TWinControl * Control)
  332. {
  333. TPublicWinControl * PublicWinControl = static_cast<TPublicWinControl *>(Control);
  334. return &PublicWinControl->WndProc;
  335. }
  336. //---------------------------------------------------------------------
  337. class TPublicControl : public TControl
  338. {
  339. friend void __fastcall RealignControl(TControl * Control);
  340. };
  341. //---------------------------------------------------------------------------
  342. void __fastcall RealignControl(TControl * Control)
  343. {
  344. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  345. PublicControl->RequestAlign();
  346. }
  347. //---------------------------------------------------------------------------
  348. static Forms::TMonitor * LastMonitor = NULL;
  349. //---------------------------------------------------------------------------
  350. inline void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc,
  351. TMessage & Message)
  352. {
  353. if ((Message.Msg == WM_SYSCOMMAND) &&
  354. (Message.WParam == SC_CONTEXTHELP))
  355. {
  356. FormHelp(Form);
  357. Message.Result = 1;
  358. }
  359. else if (Message.Msg == CM_SHOWINGCHANGED)
  360. {
  361. TForm * AForm = dynamic_cast<TForm *>(Form);
  362. assert(AForm != NULL);
  363. if (IsMainFormLike(Form))
  364. {
  365. if (Form->Showing)
  366. {
  367. if (Application->MainForm != Form)
  368. {
  369. MainLikeForm = AForm;
  370. // When main form is hidden, no taskbar button for it is shown and
  371. // this window does not become main window, so there won't be no taskbar
  372. // icon created automatically (by VCL). So we force it manually here.
  373. // This particularly happen for all windows/dialogs of command-line
  374. // operations (e.g. /synchronize) after CreateScpExplorer() happens.
  375. // Also CM_SHOWINGCHANGED happens twice, and the WS_EX_APPWINDOW flag
  376. // from the first call is not preserved, re-applying on the second call too.
  377. // TODO: What about minimize to tray?
  378. int Style = GetWindowLong(Form->Handle, GWL_EXSTYLE);
  379. if (FLAGCLEAR(Style, WS_EX_APPWINDOW))
  380. {
  381. Style |= WS_EX_APPWINDOW;
  382. SetWindowLong(Form->Handle, GWL_EXSTYLE, Style);
  383. }
  384. }
  385. if (Form->Perform(WM_MANAGES_CAPTION, 0, 0) == 0)
  386. {
  387. Form->Caption = FormatFormCaption(Form, Form->Caption);
  388. }
  389. SendMessage(Form->Handle, WM_SETICON, ICON_BIG, reinterpret_cast<long>(Application->Icon->Handle));
  390. }
  391. else
  392. {
  393. if (MainLikeForm == Form)
  394. {
  395. // Do not bother with hiding the WS_EX_APPWINDOW flag
  396. // as the window is closing anyway.
  397. MainLikeForm = NULL;
  398. }
  399. }
  400. }
  401. // Part of following code (but actually not all, TODO), has to happen
  402. // for all windows when VCL main window is hidden (particularly the last branch).
  403. // This is different from above brach, that should happen only for top-level visible window.
  404. if ((Application->MainForm == Form) ||
  405. // this particularly happens if error occurs while main
  406. // window is being shown (e.g. non existent local directory when opening
  407. // explorer)
  408. ((Application->MainForm != NULL) && !Application->MainForm->Visible))
  409. {
  410. if (!Form->Showing)
  411. {
  412. // when closing main form, remember its monitor,
  413. // so that the next form is shown on the same one
  414. LastMonitor = Form->Monitor;
  415. }
  416. else if ((LastMonitor != NULL) && (LastMonitor != Form->Monitor) &&
  417. Form->Showing)
  418. {
  419. // would actually always be poScreenCenter, see _SafeFormCreate
  420. if ((AForm->Position == poMainFormCenter) ||
  421. (AForm->Position == poOwnerFormCenter) ||
  422. (AForm->Position == poScreenCenter))
  423. {
  424. // this would typically be an authentication dialog,
  425. // but it may as well be an message box
  426. // taken from TCustomForm::SetWindowToMonitor
  427. AForm->SetBounds(LastMonitor->Left + ((LastMonitor->Width - AForm->Width) / 2),
  428. LastMonitor->Top + ((LastMonitor->Height - AForm->Height) / 2),
  429. AForm->Width, AForm->Height);
  430. AForm->Position = poDesigned;
  431. }
  432. else if ((AForm->Position != poDesigned) &&
  433. (AForm->Position != poDefaultPosOnly))
  434. {
  435. // we do not expect any other positioning
  436. FAIL;
  437. }
  438. }
  439. else
  440. {
  441. TForm * AForm = dynamic_cast<TForm *>(Form);
  442. assert(AForm != NULL);
  443. // otherwise it would not get centered
  444. if ((AForm->Position == poMainFormCenter) ||
  445. (AForm->Position == poOwnerFormCenter))
  446. {
  447. AForm->Position = poScreenCenter;
  448. }
  449. }
  450. }
  451. bool WasFormCenter =
  452. (AForm->Position == poMainFormCenter) ||
  453. (AForm->Position == poOwnerFormCenter);
  454. WndProc(Message);
  455. // Make sure dialogs are shown on-screen even if center of the main window
  456. // is off-screen. Occurs e.g. if you move the main window so that
  457. // only window title is visible above taksbar.
  458. if (Form->Showing && WasFormCenter && (AForm->Position == poDesigned))
  459. {
  460. TRect Rect;
  461. // Reading Form.Left/Form.Top instead here does not work, likely due to some
  462. // bug, when querying TProgressForm opened from TEditorForm (reloading remote file)
  463. GetWindowRect(Form->Handle, &Rect);
  464. int Left = Rect.Left;
  465. int Top = Rect.Top;
  466. TRect WorkArea = AForm->Monitor->WorkareaRect;
  467. if (Left + Rect.Width() > WorkArea.Right)
  468. {
  469. Left = WorkArea.Right - Rect.Width();
  470. }
  471. if (Left < WorkArea.Left)
  472. {
  473. Left = WorkArea.Left;
  474. }
  475. if (Top + Rect.Height() > WorkArea.Bottom)
  476. {
  477. Top = WorkArea.Bottom - Rect.Height();
  478. }
  479. if (Top < WorkArea.Top)
  480. {
  481. Top = WorkArea.Top;
  482. }
  483. if ((Left != Rect.Left) ||
  484. (Top != Rect.Top))
  485. {
  486. SetWindowPos(Form->Handle, 0, Left, Top, Rect.Width(), Rect.Height(),
  487. SWP_NOZORDER + SWP_NOACTIVATE);
  488. }
  489. }
  490. }
  491. else
  492. {
  493. WndProc(Message);
  494. }
  495. }
  496. //---------------------------------------------------------------------------
  497. static void __fastcall FormWindowProc(void * Data, TMessage & Message)
  498. {
  499. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  500. DoFormWindowProc(Form, ControlWndProc(Form), Message);
  501. }
  502. //---------------------------------------------------------------------------
  503. static void __fastcall FormWindowProcEx(void * Data, TMessage & Message)
  504. {
  505. TSavedSystemSettings * SSettings = static_cast<TSavedSystemSettings *>(Data);
  506. DoFormWindowProc(SSettings->Form, SSettings->OldWndProc, Message);
  507. }
  508. //---------------------------------------------------------------------------
  509. void __fastcall InitializeSystemSettings()
  510. {
  511. }
  512. //---------------------------------------------------------------------------
  513. void __fastcall FinalizeSystemSettings()
  514. {
  515. }
  516. //---------------------------------------------------------------------------
  517. #ifdef _DEBUG
  518. void __fastcall VerifyControl(TControl * Control)
  519. {
  520. // If at this time the control has allocated persistence data that are used
  521. // for delayed handle recreation, we are at potential risk, as the future
  522. // de-persistence may overwrite meanwhile changed data.
  523. // For instance it may happen with list view that DPI scaling gets lost.
  524. // This for example happens when the list view has both design time
  525. // ReadOnly = true and some items set. We cannot usually explicitly
  526. // check for the presence of items as while the listview does not have
  527. // a handle allocated, item count querying does not work
  528. // (see also a check below)
  529. assert(!ControlHasRecreationPersistenceData(Control));
  530. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  531. if (ListView != NULL)
  532. {
  533. // As of now the HandleAllocated check is pointless as
  534. // ListView->Items->Count returns 0 when the handle is not allocated yet.
  535. // But we want to know if the implementation ever changes to allocate the handle
  536. // on the call. Because we do not want to allocate a handle here as
  537. // that would change the debug mode behavior from release behavior,
  538. // possibly hiding away some problems.
  539. assert(!ListView->HandleAllocated() || (ListView->Items->Count == 0));
  540. }
  541. }
  542. #endif
  543. //---------------------------------------------------------------------------
  544. void __fastcall ApplySystemSettingsOnControl(TControl * Control)
  545. {
  546. #ifdef _DEBUG
  547. VerifyControl(Control);
  548. #endif
  549. // WORKAROUND
  550. // VCL does not scale status par panels (while for instance it does
  551. // scale list view headers). Remove this if they ever "fix" this.
  552. // Check TCustomStatusBar.ChangeScale() for changes.
  553. // For TBX status bars, this is implemented in TTBXCustomStatusBar.ChangeScale
  554. TStatusBar * StatusBar = dynamic_cast<TStatusBar *>(Control);
  555. if (StatusBar != NULL)
  556. {
  557. for (int Index = 0; Index < StatusBar->Panels->Count; Index++)
  558. {
  559. TStatusPanel * Panel = StatusBar->Panels->Items[Index];
  560. Panel->Width = ScaleByTextHeight(StatusBar, Panel->Width);
  561. }
  562. }
  563. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  564. if (WinControl != NULL)
  565. {
  566. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  567. {
  568. ApplySystemSettingsOnControl(WinControl->Controls[Index]);
  569. }
  570. }
  571. }
  572. //---------------------------------------------------------------------------
  573. // Settings that must be set as soon as possible.
  574. void __fastcall UseSystemSettingsPre(TCustomForm * Control, void ** Settings)
  575. {
  576. LocalSystemSettings(Control);
  577. TWndMethod WindowProc;
  578. if (Settings)
  579. {
  580. TSavedSystemSettings * SSettings;
  581. SSettings = new TSavedSystemSettings();
  582. *Settings = static_cast<void*>(SSettings);
  583. SSettings->Form = Control;
  584. SSettings->FontName = Control->Font->Name;
  585. SSettings->OldWndProc = Control->WindowProc;
  586. ((TMethod*)&WindowProc)->Data = SSettings;
  587. ((TMethod*)&WindowProc)->Code = FormWindowProcEx;
  588. }
  589. else
  590. {
  591. ((TMethod*)&WindowProc)->Data = Control;
  592. ((TMethod*)&WindowProc)->Code = FormWindowProc;
  593. }
  594. Control->WindowProc = WindowProc;
  595. if (Control->HelpKeyword.IsEmpty())
  596. {
  597. // temporary help keyword to enable F1 key in all forms
  598. Control->HelpKeyword = L"start";
  599. }
  600. ApplySystemSettingsOnControl(Control);
  601. };
  602. //---------------------------------------------------------------------------
  603. // Settings that must be set only after whole form is constructed
  604. void __fastcall UseSystemSettingsPost(TCustomForm * Control, void * Settings)
  605. {
  606. bool Flip;
  607. UnicodeString FlipStr = LoadStr(FLIP_CHILDREN);
  608. Flip = !FlipStr.IsEmpty() && static_cast<bool>(StrToInt(FlipStr));
  609. if (Settings != NULL)
  610. {
  611. static_cast<TSavedSystemSettings*>(Settings)->Flipped = Flip;
  612. }
  613. if (Flip)
  614. {
  615. Control->FlipChildren(true);
  616. }
  617. ResetSystemSettings(Control);
  618. };
  619. //---------------------------------------------------------------------------
  620. void __fastcall UseSystemSettings(TCustomForm * Control, void ** Settings)
  621. {
  622. UseSystemSettingsPre(Control, Settings);
  623. UseSystemSettingsPost(Control, (Settings != NULL) ? *Settings : NULL);
  624. };
  625. //---------------------------------------------------------------------------
  626. void __fastcall ResetSystemSettings(TCustomForm * /*Control*/)
  627. {
  628. // noop
  629. }
  630. //---------------------------------------------------------------------------
  631. void __fastcall DeleteSystemSettings(TCustomForm * Control, void * Settings)
  632. {
  633. assert(Settings);
  634. TSavedSystemSettings * SSettings = static_cast<TSavedSystemSettings *>(Settings);
  635. Control->WindowProc = SSettings->OldWndProc;
  636. delete SSettings;
  637. }
  638. //---------------------------------------------------------------------------
  639. void __fastcall RevokeSystemSettings(TCustomForm * Control, void * Settings)
  640. {
  641. assert(Settings);
  642. TSavedSystemSettings* SSettings = static_cast<TSavedSystemSettings*>(Settings);
  643. if (SSettings->Flipped)
  644. {
  645. Control->FlipChildren(true);
  646. }
  647. DeleteSystemSettings(Control, Settings);
  648. };
  649. //---------------------------------------------------------------------------
  650. class TPublicForm : public TForm
  651. {
  652. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage);
  653. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  654. };
  655. //---------------------------------------------------------------------------
  656. struct TShowAsModalStorage
  657. {
  658. void * FocusWindowList;
  659. HWND FocusActiveWindow;
  660. TFocusState FocusState;
  661. TCursor SaveCursor;
  662. int SaveCount;
  663. };
  664. //---------------------------------------------------------------------------
  665. void __fastcall ShowAsModal(TForm * Form, void *& Storage)
  666. {
  667. SetCorrectFormParent(Form);
  668. CancelDrag();
  669. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  670. ReleaseCapture();
  671. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  672. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  673. AStorage->FocusActiveWindow = GetActiveWindow();
  674. AStorage->FocusState = SaveFocusState();
  675. Screen->SaveFocusedList->Insert(0, Screen->FocusedForm);
  676. Screen->FocusedForm = Form;
  677. AStorage->SaveCursor = Screen->Cursor;
  678. // This is particularly used when displaing progress window
  679. // while downloading via temporary folder
  680. // (the mouse cursor is yet hidden at this point).
  681. // While the code was added to workaround the above problem,
  682. // it is taken from TCustomForm.ShowModal and
  683. // should have been here ever since.
  684. Screen->Cursor = crDefault;
  685. AStorage->SaveCount = Screen->CursorCount;
  686. AStorage->FocusWindowList = DisableTaskWindows(0);
  687. Form->Show();
  688. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  689. Storage = AStorage;
  690. }
  691. //---------------------------------------------------------------------------
  692. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  693. {
  694. assert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  695. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  696. Storage = NULL;
  697. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  698. if (GetActiveWindow() != Form->Handle)
  699. {
  700. AStorage->FocusActiveWindow = 0;
  701. }
  702. Form->Hide();
  703. if (Screen->CursorCount == AStorage->SaveCount)
  704. {
  705. Screen->Cursor = AStorage->SaveCursor;
  706. }
  707. else
  708. {
  709. Screen->Cursor = crDefault;
  710. }
  711. EnableTaskWindows(AStorage->FocusWindowList);
  712. if (Screen->SaveFocusedList->Count > 0)
  713. {
  714. Screen->FocusedForm = static_cast<TCustomForm *>(Screen->SaveFocusedList->First());
  715. Screen->SaveFocusedList->Remove(Screen->FocusedForm);
  716. }
  717. else
  718. {
  719. Screen->FocusedForm = NULL;
  720. }
  721. if (AStorage->FocusActiveWindow != 0)
  722. {
  723. SetActiveWindow(AStorage->FocusActiveWindow);
  724. }
  725. RestoreFocusState(AStorage->FocusState);
  726. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  727. delete AStorage;
  728. }
  729. //---------------------------------------------------------------------------
  730. void __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  731. {
  732. if (Storage != NULL)
  733. {
  734. HideAsModal(Form, Storage);
  735. }
  736. }
  737. //---------------------------------------------------------------------------
  738. bool __fastcall SelectDirectory(UnicodeString & Path, const UnicodeString Prompt,
  739. bool PreserveFileName)
  740. {
  741. bool Result;
  742. unsigned int ErrorMode;
  743. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  744. try
  745. {
  746. UnicodeString Directory;
  747. UnicodeString FileName;
  748. if (!PreserveFileName || DirectoryExists(::ApiPath(Path)))
  749. {
  750. Directory = Path;
  751. }
  752. else
  753. {
  754. Directory = ExtractFilePath(Path);
  755. FileName = ExtractFileName(Path);
  756. }
  757. Result = SelectDirectory(Prompt, L"", Directory);
  758. if (Result)
  759. {
  760. Path = Directory;
  761. if (!FileName.IsEmpty())
  762. {
  763. Path = IncludeTrailingBackslash(Path) + FileName;
  764. }
  765. }
  766. }
  767. __finally
  768. {
  769. SetErrorMode(ErrorMode);
  770. }
  771. return Result;
  772. }
  773. //---------------------------------------------------------------------------
  774. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  775. {
  776. bool AnyChecked = false;
  777. for (int Index = 0; Index < ListView->Items->Count; Index++)
  778. {
  779. if (ListView->Items->Item[Index]->Checked == Checked)
  780. {
  781. AnyChecked = true;
  782. break;
  783. }
  784. }
  785. return AnyChecked;
  786. }
  787. //---------------------------------------------------------------------------
  788. void __fastcall ListViewCheckAll(TListView * ListView,
  789. TListViewCheckAll CheckAll)
  790. {
  791. bool Check;
  792. if (CheckAll == caToggle)
  793. {
  794. Check = ListViewAnyChecked(ListView, false);
  795. }
  796. else
  797. {
  798. Check = (CheckAll == caCheck);
  799. }
  800. for (int Index = 0; Index < ListView->Items->Count; Index++)
  801. {
  802. ListView->Items->Item[Index]->Checked = Check;
  803. }
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  807. {
  808. int PrevIndex = ComboBox->ItemIndex;
  809. ComboBox->Items->BeginUpdate();
  810. try
  811. {
  812. ComboBox->Clear();
  813. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  814. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  815. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  816. }
  817. __finally
  818. {
  819. ComboBox->Items->EndUpdate();
  820. }
  821. assert(PrevIndex < ComboBox->Items->Count);
  822. ComboBox->ItemIndex = PrevIndex;
  823. }
  824. //---------------------------------------------------------------------------
  825. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  826. {
  827. ComboBox->ItemIndex = 2 - Value;
  828. if (ComboBox->ItemIndex < 0)
  829. {
  830. ComboBox->ItemIndex = 0;
  831. }
  832. }
  833. //---------------------------------------------------------------------------
  834. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  835. {
  836. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  837. }
  838. //---------------------------------------------------------------------------
  839. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  840. {
  841. switch (Value)
  842. {
  843. case asOn:
  844. CheckBox->State = cbChecked;
  845. break;
  846. case asOff:
  847. CheckBox->State = cbUnchecked;
  848. break;
  849. default:
  850. CheckBox->State = cbGrayed;
  851. break;
  852. }
  853. }
  854. //---------------------------------------------------------------------------
  855. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  856. {
  857. switch (CheckBox->State)
  858. {
  859. case cbChecked:
  860. return asOn;
  861. case cbUnchecked:
  862. return asOff;
  863. default:
  864. return asAuto;
  865. }
  866. }
  867. //---------------------------------------------------------------------------
  868. static const wchar_t PathWordDelimiters[] = L"\\/ ;,.";
  869. //---------------------------------------------------------------------------
  870. static bool IsPathWordDelimiter(wchar_t Ch)
  871. {
  872. return (wcschr(PathWordDelimiters, Ch) != NULL);
  873. }
  874. //---------------------------------------------------------------------------
  875. // Windows algorithm is as follows (tested on W2k):
  876. // right:
  877. // is_delimiter(current)
  878. // false:
  879. // right(left(current) + 1)
  880. // true:
  881. // right(right(current) + 1)
  882. // left:
  883. // right(left(current) + 1)
  884. int CALLBACK PathWordBreakProc(wchar_t * Ch, int Current, int Len, int Code)
  885. {
  886. int Result;
  887. UnicodeString ACh(Ch, Len);
  888. if (Code == WB_ISDELIMITER)
  889. {
  890. // we return negacy of what WinAPI docs says
  891. Result = !IsPathWordDelimiter(ACh[Current + 1]);
  892. }
  893. else if (Code == WB_LEFT)
  894. {
  895. // skip consecutive delimiters
  896. while ((Current > 0) &&
  897. IsPathWordDelimiter(ACh[Current]))
  898. {
  899. Current--;
  900. }
  901. Result = ACh.SubString(1, Current - 1).LastDelimiter(PathWordDelimiters);
  902. }
  903. else if (Code == WB_RIGHT)
  904. {
  905. if (Current == 0)
  906. {
  907. // will be called again with Current == 1
  908. Result = 0;
  909. }
  910. else
  911. {
  912. const wchar_t * P = wcspbrk(ACh.c_str() + Current - 1, PathWordDelimiters);
  913. if (P == NULL)
  914. {
  915. Result = Len;
  916. }
  917. else
  918. {
  919. Result = P - ACh.c_str() + 1;
  920. // skip consecutive delimiters
  921. while ((Result < Len) &&
  922. IsPathWordDelimiter(ACh[Result + 1]))
  923. {
  924. Result++;
  925. }
  926. }
  927. }
  928. }
  929. else
  930. {
  931. FAIL;
  932. Result = 0;
  933. }
  934. return Result;
  935. }
  936. //---------------------------------------------------------------------------
  937. class TPublicCustomCombo : public TCustomCombo
  938. {
  939. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  940. };
  941. //---------------------------------------------------------------------------
  942. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  943. {
  944. // Since we are setting Application->ModalPopupMode = pmAuto,
  945. // this has to be called from OnShow, not from constructor anymore,
  946. // to have any effect
  947. HWND Wnd;
  948. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  949. {
  950. TPublicCustomCombo * Combo =
  951. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  952. Combo->HandleNeeded();
  953. Wnd = Combo->EditHandle;
  954. }
  955. else
  956. {
  957. Wnd = Control->Handle;
  958. }
  959. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  960. }
  961. //---------------------------------------------------------------------------
  962. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  963. {
  964. int Shift = 0;
  965. for (int Index = 0; Index < Count; Index++)
  966. {
  967. if (ControlsOrder[Index]->Visible)
  968. {
  969. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  970. }
  971. else
  972. {
  973. Shift++;
  974. }
  975. }
  976. Count -= Shift;
  977. }
  978. //---------------------------------------------------------------------------
  979. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  980. {
  981. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  982. if (Count > 0)
  983. {
  984. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  985. CommonParent->DisableAlign();
  986. try
  987. {
  988. int Top = 0;
  989. for (int Index = 0; Index < Count; Index++)
  990. {
  991. assert(ControlsOrder[Index]->Parent == CommonParent);
  992. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  993. {
  994. Top = ControlsOrder[Index]->Top;
  995. }
  996. }
  997. for (int Index = 0; Index < Count; Index++)
  998. {
  999. TControl * Control = ControlsOrder[Index];
  1000. Control->Top = Top;
  1001. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  1002. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  1003. {
  1004. Top += Control->Height;
  1005. }
  1006. }
  1007. }
  1008. __finally
  1009. {
  1010. CommonParent->EnableAlign();
  1011. }
  1012. }
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  1016. {
  1017. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1018. if (Count > 0)
  1019. {
  1020. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1021. CommonParent->DisableAlign();
  1022. try
  1023. {
  1024. int Left = 0;
  1025. for (int Index = 0; Index < Count; Index++)
  1026. {
  1027. assert(ControlsOrder[Index]->Parent == CommonParent);
  1028. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  1029. {
  1030. Left = ControlsOrder[Index]->Left;
  1031. }
  1032. }
  1033. for (int Index = 0; Index < Count; Index++)
  1034. {
  1035. TControl * Control = ControlsOrder[Index];
  1036. Control->Left = Left;
  1037. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  1038. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  1039. {
  1040. Left += Control->Width;
  1041. }
  1042. // vertical alignment has priority, so alBottom-aligned controls start
  1043. // at the very left, even if there are any alLeft/alRight controls.
  1044. // for the reason this code is not necessary in SetVerticalControlsOrder.
  1045. // we could exit the loop as well here.
  1046. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  1047. {
  1048. Left = 0;
  1049. }
  1050. }
  1051. }
  1052. __finally
  1053. {
  1054. CommonParent->EnableAlign();
  1055. }
  1056. }
  1057. }
  1058. //---------------------------------------------------------------------------
  1059. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  1060. {
  1061. if (After->TabOrder > Control->TabOrder)
  1062. {
  1063. After->TabOrder = Control->TabOrder;
  1064. }
  1065. else if (After->TabOrder < Control->TabOrder - 1)
  1066. {
  1067. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  1068. }
  1069. }
  1070. //---------------------------------------------------------------------------
  1071. void __fastcall CutFormToDesktop(TForm * Form)
  1072. {
  1073. assert(Form->Monitor != NULL);
  1074. TRect Workarea = Form->Monitor->WorkareaRect;
  1075. if (Form->Top + Form->Height > Workarea.Bottom)
  1076. {
  1077. Form->Height = Workarea.Bottom - Form->Top;
  1078. }
  1079. if (Form->Left + Form->Width >= Workarea.Right)
  1080. {
  1081. Form->Width = Workarea.Right - Form->Left;
  1082. }
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  1086. {
  1087. if ((Position == poScreenCenter) ||
  1088. (Position == poOwnerFormCenter) ||
  1089. (Position == poMainFormCenter))
  1090. {
  1091. TCustomForm * CenterForm = NULL;
  1092. if ((Position == poOwnerFormCenter) ||
  1093. (Position == poMainFormCenter))
  1094. {
  1095. CenterForm = Application->MainForm;
  1096. if ((Position == poOwnerFormCenter) &&
  1097. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  1098. {
  1099. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  1100. }
  1101. }
  1102. TRect Bounds = Form->BoundsRect;
  1103. int X, Y;
  1104. if (CenterForm != NULL)
  1105. {
  1106. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  1107. ((TForm *)CenterForm)->Left;
  1108. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  1109. ((TForm *)CenterForm)->Top;
  1110. }
  1111. else
  1112. {
  1113. X = (Form->Monitor->Width - Bounds.Width()) / 2;
  1114. Y = (Form->Monitor->Height - Bounds.Height()) / 2;
  1115. }
  1116. if (X < 0)
  1117. {
  1118. X = 0;
  1119. }
  1120. if (Y < 0)
  1121. {
  1122. Y = 0;
  1123. }
  1124. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  1125. }
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  1129. {
  1130. // This has to be called only after DoFormWindowProc(CM_SHOWINGCHANGED),
  1131. // so that a correct monitor is considered.
  1132. // Note that we cannot use LastMonitor(), as ResizeForm is also called from
  1133. // TConsoleDialog::DoAdjustWindow, where we need to use the actual monitor
  1134. // (in case user moves the console window to a different monitor,
  1135. // than where a main window is [no matter how unlikely that is])
  1136. TRect WorkareaRect = Form->Monitor->WorkareaRect;
  1137. if (Height > WorkareaRect.Height())
  1138. {
  1139. Height = WorkareaRect.Height();
  1140. }
  1141. if (Width > WorkareaRect.Width())
  1142. {
  1143. Width = WorkareaRect.Width();
  1144. }
  1145. if (Height < Form->Constraints->MinHeight)
  1146. {
  1147. Height = Form->Constraints->MinHeight;
  1148. }
  1149. if (Width < Form->Constraints->MinWidth)
  1150. {
  1151. Width = Form->Constraints->MinWidth;
  1152. }
  1153. TRect Bounds = Form->BoundsRect;
  1154. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  1155. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  1156. if (Top + Height > WorkareaRect.Bottom)
  1157. {
  1158. Top = WorkareaRect.Bottom - Height;
  1159. }
  1160. if (Left + Width >= WorkareaRect.Right)
  1161. {
  1162. Left = WorkareaRect.Right - Width;
  1163. }
  1164. // WorkareaRect.Left is not 0, when secondary monitor is placed left of primary one.
  1165. // Similarly for WorkareaRect.Top.
  1166. if (Top < WorkareaRect.Top)
  1167. {
  1168. Top = WorkareaRect.Top;
  1169. }
  1170. if (Left < WorkareaRect.Left)
  1171. {
  1172. Left = WorkareaRect.Left;
  1173. }
  1174. Form->SetBounds(Left, Top, Width, Height);
  1175. Bounds = Form->BoundsRect;
  1176. // due to constraints, form can remain larger, make sure it is centered although
  1177. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  1178. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  1179. Form->SetBounds(Left, Top, Width, Height);
  1180. }
  1181. //---------------------------------------------------------------------------
  1182. TComponent * __fastcall GetFormOwner()
  1183. {
  1184. if (Screen->ActiveForm != NULL)
  1185. {
  1186. return Screen->ActiveForm;
  1187. }
  1188. else
  1189. {
  1190. return Application;
  1191. }
  1192. }
  1193. //---------------------------------------------------------------------------
  1194. void __fastcall SetCorrectFormParent(TForm * /*Form*/)
  1195. {
  1196. // noop
  1197. // remove
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. void __fastcall InvokeHelp(TWinControl * Control)
  1201. {
  1202. assert(Control != NULL);
  1203. HELPINFO HelpInfo;
  1204. HelpInfo.cbSize = sizeof(HelpInfo);
  1205. HelpInfo.iContextType = HELPINFO_WINDOW;
  1206. HelpInfo.iCtrlId = 0;
  1207. HelpInfo.hItemHandle = Control->Handle;
  1208. HelpInfo.dwContextId = 0;
  1209. HelpInfo.MousePos.x = 0;
  1210. HelpInfo.MousePos.y = 0;
  1211. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  1212. }
  1213. //---------------------------------------------------------------------------
  1214. //---------------------------------------------------------------------------
  1215. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  1216. TControlCanvas ** ACanvas, TRect & R)
  1217. {
  1218. TControlCanvas * Canvas = new TControlCanvas();
  1219. try
  1220. {
  1221. Canvas->Control = StaticText;
  1222. R = StaticText->ClientRect;
  1223. UnicodeString Caption = StaticText->Caption;
  1224. bool AccelChar = false;
  1225. if (StaticText->ShowAccelChar)
  1226. {
  1227. Caption = StripHotkey(Caption);
  1228. AccelChar = (Caption != StaticText->Caption);
  1229. }
  1230. TSize TextSize = Canvas->TextExtent(Caption);
  1231. assert(StaticText->BorderStyle == sbsNone); // not taken into account
  1232. if (AccelChar)
  1233. {
  1234. TextSize.cy += 2;
  1235. }
  1236. R.Bottom = R.Top + TextSize.cy;
  1237. switch (StaticText->Alignment)
  1238. {
  1239. case taLeftJustify:
  1240. R.Right = R.Left + TextSize.cx;
  1241. break;
  1242. case taRightJustify:
  1243. R.Left = Max(0, R.Right - TextSize.cx);
  1244. break;
  1245. case taCenter:
  1246. {
  1247. FAIL; // not used branch, possibly untested
  1248. int Diff = R.Width() - TextSize.cx;
  1249. R.Left += Diff / 2;
  1250. R.Right -= Diff - (Diff / 2);
  1251. }
  1252. break;
  1253. }
  1254. }
  1255. __finally
  1256. {
  1257. if (ACanvas == NULL)
  1258. {
  1259. delete Canvas;
  1260. }
  1261. }
  1262. if (ACanvas != NULL)
  1263. {
  1264. *ACanvas = Canvas;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1269. bool & Clicked)
  1270. {
  1271. Clicked = false;
  1272. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1273. if (Message.Msg == WM_LBUTTONDOWN)
  1274. {
  1275. StaticText->SetFocus();
  1276. // in case the action takes long, make sure focus is shown immediately
  1277. UpdateWindow(StaticText->Handle);
  1278. Clicked = true;
  1279. Message.Result = 1;
  1280. }
  1281. else if (Message.Msg == WM_RBUTTONDOWN)
  1282. {
  1283. StaticText->SetFocus();
  1284. Message.Result = 1;
  1285. }
  1286. else if (Message.Msg == WM_CHAR)
  1287. {
  1288. if (reinterpret_cast<TWMChar &>(Message).CharCode == L' ')
  1289. {
  1290. Clicked = true;
  1291. Message.Result = 1;
  1292. }
  1293. else
  1294. {
  1295. ControlWndProc(StaticText)(Message);
  1296. }
  1297. }
  1298. else if (Message.Msg == CM_DIALOGCHAR)
  1299. {
  1300. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1301. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1302. {
  1303. StaticText->SetFocus();
  1304. // in case the action takes long, make sure focus is shown immediately
  1305. UpdateWindow(StaticText->Handle);
  1306. Clicked = true;
  1307. Message.Result = 1;
  1308. }
  1309. else
  1310. {
  1311. ControlWndProc(StaticText)(Message);
  1312. }
  1313. }
  1314. else
  1315. {
  1316. ControlWndProc(StaticText)(Message);
  1317. }
  1318. if (Message.Msg == WM_PAINT)
  1319. {
  1320. TRect R;
  1321. TControlCanvas * Canvas;
  1322. FocusableLabelCanvas(StaticText, &Canvas, R);
  1323. try
  1324. {
  1325. if (StaticText->Focused())
  1326. {
  1327. Canvas->DrawFocusRect(R);
  1328. }
  1329. else if (!StaticText->Font->Style.Contains(fsUnderline))
  1330. {
  1331. Canvas->Pen->Style = psDot;
  1332. Canvas->Brush->Style = bsClear;
  1333. if (!StaticText->Enabled)
  1334. {
  1335. Canvas->Pen->Color = clBtnHighlight;
  1336. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1337. Canvas->LineTo(R.Right + 1, R.Bottom);
  1338. Canvas->Pen->Color = clGrayText;
  1339. }
  1340. Canvas->MoveTo(R.Left, R.Bottom - 1);
  1341. Canvas->LineTo(R.Right, R.Bottom - 1);
  1342. }
  1343. }
  1344. __finally
  1345. {
  1346. delete Canvas;
  1347. }
  1348. }
  1349. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1350. (Message.Msg == CM_ENABLEDCHANGED))
  1351. {
  1352. StaticText->Invalidate();
  1353. }
  1354. }
  1355. //---------------------------------------------------------------------------
  1356. static THintWindow * PersistentHintWindow = NULL;
  1357. static TControl * PersistentHintControl = NULL;
  1358. //---------------------------------------------------------------------------
  1359. void __fastcall CancelPersistentHint()
  1360. {
  1361. if (PersistentHintWindow != NULL)
  1362. {
  1363. PersistentHintControl = NULL;
  1364. SAFE_DESTROY(PersistentHintWindow);
  1365. }
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1369. {
  1370. CancelPersistentHint();
  1371. THintInfo HintInfo;
  1372. HintInfo.HintControl = Control;
  1373. HintInfo.HintPos = HintPos;
  1374. HintInfo.HintMaxWidth = GetParentForm(Control)->Monitor->Width;
  1375. HintInfo.HintColor = Application->HintColor;
  1376. HintInfo.HintStr = GetShortHint(Control->Hint);
  1377. HintInfo.HintData = NULL;
  1378. bool CanShow = true;
  1379. if (Application->OnShowHint != NULL)
  1380. {
  1381. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1382. }
  1383. if (CanShow)
  1384. {
  1385. PersistentHintControl = Control;
  1386. PersistentHintWindow = new THintWindow(Application);
  1387. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1388. PersistentHintWindow->Color = HintInfo.HintColor;
  1389. TRect HintWinRect;
  1390. if (HintInfo.HintMaxWidth < Control->Width)
  1391. {
  1392. HintInfo.HintMaxWidth = Control->Width;
  1393. }
  1394. HintWinRect = PersistentHintWindow->CalcHintRect(
  1395. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1396. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1397. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1398. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1399. }
  1400. }
  1401. //---------------------------------------------------------------------------
  1402. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1403. {
  1404. bool Clicked = false;
  1405. bool Cancel = false;
  1406. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1407. if (Message.Msg == CM_HINTSHOW)
  1408. {
  1409. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1410. if (PersistentHintControl == StaticText)
  1411. {
  1412. // do not allow standard hint when persistent is already shown
  1413. HintShow.Result = 1;
  1414. }
  1415. else
  1416. {
  1417. HintShow.HintInfo->HideTimeout = 100000; // never
  1418. }
  1419. }
  1420. else if (Message.Msg == CN_KEYDOWN)
  1421. {
  1422. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1423. (PersistentHintControl == StaticText))
  1424. {
  1425. CancelPersistentHint();
  1426. StaticText->Invalidate();
  1427. Message.Result = 1;
  1428. }
  1429. else
  1430. {
  1431. FocusableLabelWindowProc(Data, Message, Clicked);
  1432. }
  1433. }
  1434. else
  1435. {
  1436. FocusableLabelWindowProc(Data, Message, Clicked);
  1437. }
  1438. if (Message.Msg == CM_CANCELMODE)
  1439. {
  1440. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  1441. if ((CancelMessage.Sender != StaticText) &&
  1442. (CancelMessage.Sender != PersistentHintWindow))
  1443. {
  1444. Cancel = true;
  1445. }
  1446. }
  1447. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  1448. {
  1449. Cancel = true;
  1450. }
  1451. if (Cancel && (PersistentHintControl == StaticText))
  1452. {
  1453. CancelPersistentHint();
  1454. }
  1455. if (Clicked && (PersistentHintControl != StaticText))
  1456. {
  1457. TRect R;
  1458. TPoint HintPos;
  1459. FocusableLabelCanvas(StaticText, NULL, R);
  1460. HintPos.y = R.Bottom - R.Top;
  1461. HintPos.x = R.Left;
  1462. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  1463. }
  1464. }
  1465. //---------------------------------------------------------------------------
  1466. void __fastcall HintLabel(TStaticText * StaticText, UnicodeString Hint)
  1467. {
  1468. // Currently all are right-justified, when other alignemtn is used,
  1469. // test respective branches in FocusableLabelCanvas.
  1470. assert(StaticText->Alignment == taRightJustify);
  1471. // With right-justify, it has to be off. We may not notice on riginal
  1472. // English version, results will differ with translations only
  1473. assert(!StaticText->AutoSize);
  1474. StaticText->ParentFont = true;
  1475. if (!Hint.IsEmpty())
  1476. {
  1477. StaticText->Hint = Hint;
  1478. }
  1479. StaticText->ShowHint = true;
  1480. StaticText->Cursor = crHandPoint;
  1481. TWndMethod WindowProc;
  1482. ((TMethod*)&WindowProc)->Data = StaticText;
  1483. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  1484. StaticText->WindowProc = WindowProc;
  1485. }
  1486. //---------------------------------------------------------------------------
  1487. void __fastcall HintLabelRestore(TStaticText * StaticText)
  1488. {
  1489. StaticText->WindowProc = ControlWndProc(StaticText);
  1490. StaticText->ShowHint = false;
  1491. StaticText->Cursor = crDefault;
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  1495. {
  1496. // it is TCustomComboxBox, but the properties are published only by TComboBox
  1497. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  1498. if (Message.Msg == WM_SIZE)
  1499. {
  1500. UnicodeString Text = ComboBox->Text;
  1501. try
  1502. {
  1503. ControlWndProc(ComboBox)(Message);
  1504. }
  1505. __finally
  1506. {
  1507. // workaround for bug in combo box, that causes it to change text to any
  1508. // item from drop down list which starts with current text,
  1509. // after control is resized (unless the text is in drop down list as well)
  1510. ComboBox->Text = Text;
  1511. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  1512. if (!ComboBox->Focused())
  1513. {
  1514. ComboBox->SelLength = 0;
  1515. }
  1516. }
  1517. }
  1518. else
  1519. {
  1520. ControlWndProc(ComboBox)(Message);
  1521. }
  1522. }
  1523. //---------------------------------------------------------------------------
  1524. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  1525. {
  1526. TWndMethod WindowProc;
  1527. ((TMethod*)&WindowProc)->Data = ComboBox;
  1528. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  1529. ComboBox->WindowProc = WindowProc;
  1530. }
  1531. //---------------------------------------------------------------------------
  1532. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  1533. {
  1534. if (StaticText->OnClick != NULL)
  1535. {
  1536. StaticText->OnClick(StaticText);
  1537. }
  1538. else
  1539. {
  1540. UnicodeString Url = StaticText->Caption;
  1541. if (!SameText(Url.SubString(1, 4), L"http") && (Url.Pos(L"@") > 0))
  1542. {
  1543. Url = L"mailto:" + Url;
  1544. }
  1545. OpenBrowser(Url);
  1546. }
  1547. }
  1548. //---------------------------------------------------------------------------
  1549. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  1550. {
  1551. bool Clicked = false;
  1552. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1553. if (Message.Msg == WM_CONTEXTMENU)
  1554. {
  1555. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  1556. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  1557. {
  1558. TRect R;
  1559. FocusableLabelCanvas(StaticText, NULL, R);
  1560. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  1561. ContextMenu.Pos.x = static_cast<short>(P.x);
  1562. ContextMenu.Pos.y = static_cast<short>(P.y);
  1563. }
  1564. }
  1565. else if (Message.Msg == WM_KEYDOWN)
  1566. {
  1567. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  1568. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == L'C'))
  1569. {
  1570. TInstantOperationVisualizer Visualizer;
  1571. CopyToClipboard(StaticText->Caption);
  1572. Message.Result = 1;
  1573. }
  1574. else
  1575. {
  1576. FocusableLabelWindowProc(Data, Message, Clicked);
  1577. }
  1578. }
  1579. FocusableLabelWindowProc(Data, Message, Clicked);
  1580. if (Message.Msg == WM_DESTROY)
  1581. {
  1582. delete StaticText->PopupMenu;
  1583. assert(StaticText->PopupMenu == NULL);
  1584. }
  1585. if (Clicked)
  1586. {
  1587. LinkLabelClick(StaticText);
  1588. }
  1589. }
  1590. //---------------------------------------------------------------------------
  1591. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  1592. {
  1593. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1594. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  1595. assert(MenuItem != NULL);
  1596. if (MenuItem->Tag == 0)
  1597. {
  1598. LinkLabelClick(StaticText);
  1599. }
  1600. else
  1601. {
  1602. TInstantOperationVisualizer Visualizer;
  1603. CopyToClipboard(StaticText->Caption);
  1604. }
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. void __fastcall LinkLabel(TStaticText * StaticText, UnicodeString Url,
  1608. TNotifyEvent OnEnter)
  1609. {
  1610. StaticText->Transparent = false;
  1611. StaticText->ParentFont = true;
  1612. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  1613. StaticText->Font->Color = LinkColor;
  1614. StaticText->Cursor = crHandPoint;
  1615. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  1616. if (!Url.IsEmpty())
  1617. {
  1618. StaticText->Caption = Url;
  1619. }
  1620. if (StaticText->OnClick == NULL)
  1621. {
  1622. assert(StaticText->PopupMenu == NULL);
  1623. StaticText->PopupMenu = new TPopupMenu(StaticText);
  1624. try
  1625. {
  1626. TNotifyEvent ContextMenuOnClick;
  1627. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  1628. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  1629. TMenuItem * Item;
  1630. Item = new TMenuItem(StaticText->PopupMenu);
  1631. Item->Caption = LoadStr(URL_LINK_OPEN);
  1632. Item->Tag = 0;
  1633. Item->ShortCut = ShortCut(L' ', TShiftState());
  1634. Item->OnClick = ContextMenuOnClick;
  1635. StaticText->PopupMenu->Items->Add(Item);
  1636. Item = new TMenuItem(StaticText->PopupMenu);
  1637. Item->Caption = LoadStr(URL_LINK_COPY);
  1638. Item->Tag = 1;
  1639. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  1640. Item->OnClick = ContextMenuOnClick;
  1641. StaticText->PopupMenu->Items->Add(Item);
  1642. }
  1643. catch(...)
  1644. {
  1645. delete StaticText->PopupMenu;
  1646. assert(StaticText->PopupMenu == NULL);
  1647. throw;
  1648. }
  1649. }
  1650. TWndMethod WindowProc;
  1651. ((TMethod*)&WindowProc)->Data = StaticText;
  1652. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  1653. StaticText->WindowProc = WindowProc;
  1654. }
  1655. //---------------------------------------------------------------------------
  1656. static void __fastcall HotTrackLabelMouseEnter(void * /*Data*/, TObject * Sender)
  1657. {
  1658. reinterpret_cast<TLabel *>(Sender)->Font->Color = clBlue;
  1659. }
  1660. //---------------------------------------------------------------------------
  1661. static void __fastcall HotTrackLabelMouseLeave(void * /*Data*/, TObject * Sender)
  1662. {
  1663. reinterpret_cast<TLabel *>(Sender)->ParentFont = true;
  1664. }
  1665. //---------------------------------------------------------------------------
  1666. void __fastcall HotTrackLabel(TLabel * Label)
  1667. {
  1668. assert(Label->OnMouseEnter == NULL);
  1669. assert(Label->OnMouseLeave == NULL);
  1670. Label->OnMouseEnter = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseEnter);
  1671. Label->OnMouseLeave = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseLeave);
  1672. }
  1673. //---------------------------------------------------------------------------
  1674. Forms::TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  1675. {
  1676. Forms::TMonitor * Result;
  1677. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  1678. {
  1679. Result = Application->MainForm->Monitor;
  1680. }
  1681. else if (LastMonitor != NULL)
  1682. {
  1683. Result = LastMonitor;
  1684. }
  1685. else
  1686. {
  1687. int i = 0;
  1688. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  1689. {
  1690. i++;
  1691. }
  1692. assert(Screen->Monitors[i]->Primary);
  1693. Result = Screen->Monitors[i];
  1694. }
  1695. return Result;
  1696. }
  1697. //---------------------------------------------------------------------------
  1698. int __fastcall GetLastMonitor()
  1699. {
  1700. if (LastMonitor != NULL)
  1701. {
  1702. return LastMonitor->MonitorNum;
  1703. }
  1704. else
  1705. {
  1706. return -1;
  1707. }
  1708. }
  1709. //---------------------------------------------------------------------------
  1710. void __fastcall SetLastMonitor(int MonitorNum)
  1711. {
  1712. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  1713. {
  1714. LastMonitor = Screen->Monitors[MonitorNum];
  1715. }
  1716. else
  1717. {
  1718. LastMonitor = NULL;
  1719. }
  1720. }
  1721. //---------------------------------------------------------------------------
  1722. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  1723. {
  1724. TForm * Form;
  1725. if (Owner == NULL)
  1726. {
  1727. Owner = GetFormOwner();
  1728. }
  1729. // If there is no main form yet, make this one main.
  1730. // This:
  1731. // - Makes other forms (dialogs invoked from this one),
  1732. // be placed on the same monitor (otherwise all new forms get placed
  1733. // on primary monitor)
  1734. // - Triggers MainForm-specific code in DoFormWindowProc.
  1735. // - Shows button on taskbar
  1736. if (Application->MainForm == NULL)
  1737. {
  1738. Application->CreateForm(FormClass, &Form);
  1739. assert(Application->MainForm == Form);
  1740. }
  1741. else
  1742. {
  1743. Form = dynamic_cast<TForm *>(Construct(FormClass, Owner));
  1744. assert(Form != NULL);
  1745. }
  1746. return Form;
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. TImageList * __fastcall SharedSystemImageList(bool Large)
  1750. {
  1751. TSHFileInfo FileInfo;
  1752. TImageList * Result = new TImageList(Application);
  1753. int ImageListHandle = SHGetFileInfo(L"", 0, &FileInfo, sizeof(FileInfo),
  1754. SHGFI_SYSICONINDEX | (Large ? SHGFI_LARGEICON : SHGFI_SMALLICON));
  1755. if (ImageListHandle != 0)
  1756. {
  1757. Result->ShareImages = true;
  1758. Result->Handle = ImageListHandle;
  1759. }
  1760. return Result;
  1761. }
  1762. //---------------------------------------------------------------------------
  1763. bool __fastcall SupportsSplitButton()
  1764. {
  1765. return (Win32MajorVersion >= 6);
  1766. }
  1767. //---------------------------------------------------------------------------
  1768. static TButton * __fastcall FindDefaultButton(TWinControl * Control)
  1769. {
  1770. TButton * Result = NULL;
  1771. int Index = 0;
  1772. while ((Result == NULL) && (Index < Control->ControlCount))
  1773. {
  1774. TControl * ChildControl = Control->Controls[Index];
  1775. TButton * Button = dynamic_cast<TButton *>(ChildControl);
  1776. if ((Button != NULL) && Button->Default)
  1777. {
  1778. Result = Button;
  1779. }
  1780. else
  1781. {
  1782. TWinControl * WinControl = dynamic_cast<TWinControl *>(ChildControl);
  1783. if (WinControl != NULL)
  1784. {
  1785. Result = FindDefaultButton(WinControl);
  1786. }
  1787. }
  1788. Index++;
  1789. }
  1790. return Result;
  1791. }
  1792. //---------------------------------------------------------------------------
  1793. TModalResult __fastcall DefaultResult(TCustomForm * Form, TButton * DefaultButton)
  1794. {
  1795. // The point of this is to avoid hardcoding mrOk when checking dialog results.
  1796. // Previously we used != mrCancel instead, as mrCancel is more reliable,
  1797. // being automatically used for Esc/X buttons (and hence kind of forced to be used
  1798. // for Cancel buttons). But that failed to be reliable in the end, for
  1799. // ModalResult being mrNone, when Windows session is being logged off.
  1800. // We interpreted mrNone as OK, causing lots of troubles.
  1801. TModalResult Result = mrNone;
  1802. TButton * Button = FindDefaultButton(Form);
  1803. if (ALWAYS_TRUE(Button != NULL))
  1804. {
  1805. Result = Button->ModalResult;
  1806. }
  1807. if (Result == mrNone)
  1808. {
  1809. assert((DefaultButton != NULL) && (DefaultButton->ModalResult != mrNone));
  1810. Result = DefaultButton->ModalResult;
  1811. }
  1812. else
  1813. {
  1814. // If default button fallback was provided,
  1815. // make sure it is the default button be actually detected
  1816. assert((DefaultButton == NULL) || (Button == DefaultButton));
  1817. }
  1818. return Result;
  1819. }
  1820. //---------------------------------------------------------------------------
  1821. void __fastcall DefaultButton(TButton * Button, bool Default)
  1822. {
  1823. // default property setter does not have guard for "the same value"
  1824. if (Button->Default != Default)
  1825. {
  1826. Button->Default = Default;
  1827. }
  1828. }
  1829. //---------------------------------------------------------------------------
  1830. void __fastcall MemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  1831. {
  1832. // Sender can be Form or Memo itself
  1833. TControl * Control = dynamic_cast<TControl *>(Sender);
  1834. if (ALWAYS_TRUE(Control != NULL))
  1835. {
  1836. TCustomForm * Form = GetParentForm(Control);
  1837. // Particularly when WantReturns is true,
  1838. // memo swallows also Esc, so we have to handle it ourselves.
  1839. // See also ReadOnlyControl.
  1840. if ((Key == VK_ESCAPE) && Shift.Empty())
  1841. {
  1842. Form->ModalResult = mrCancel;
  1843. Key = 0;
  1844. }
  1845. else if ((Key == VK_RETURN) && Shift.Contains(ssCtrl))
  1846. {
  1847. Form->ModalResult = DefaultResult(Form);
  1848. Key = 0;
  1849. }
  1850. }
  1851. }
  1852. //---------------------------------------------------------------------------
  1853. void __fastcall SetFormIcon(TForm * Form, int Size, const UnicodeString & IconName)
  1854. {
  1855. HICON Icon = LoadIcon(HInstance, IconName.c_str());
  1856. if (ALWAYS_TRUE(Icon != NULL))
  1857. {
  1858. LPARAM LParam = reinterpret_cast<LPARAM>(Icon);
  1859. SendMessage(Form->Handle, WM_SETICON, Size, LParam);
  1860. DestroyIcon(Icon);
  1861. }
  1862. }
  1863. //---------------------------------------------------------------------------
  1864. void __fastcall SetFormIcons(TForm * Form, const UnicodeString & BigIconName,
  1865. const UnicodeString & SmallIconName)
  1866. {
  1867. SetFormIcon(Form, ICON_SMALL, SmallIconName);
  1868. SetFormIcon(Form, ICON_BIG, BigIconName);
  1869. }
  1870. //---------------------------------------------------------------------------
  1871. void __fastcall UseDesktopFont(TControl * Control)
  1872. {
  1873. TCustomStatusBar * StatusBar = dynamic_cast<TCustomStatusBar *>(Control);
  1874. if (StatusBar != NULL)
  1875. {
  1876. // otherwise setting DesktopFont below has no effect
  1877. StatusBar->UseSystemFont = false;
  1878. }
  1879. class TPublicControl : public TControl
  1880. {
  1881. public:
  1882. __property DesktopFont;
  1883. };
  1884. reinterpret_cast<TPublicControl *>(Control)->DesktopFont = true;
  1885. }
  1886. //---------------------------------------------------------------------------
  1887. void __fastcall LoadResourceImage(TImage * Image, const UnicodeString & ImageName)
  1888. {
  1889. std::unique_ptr<TPngImage> Png(new TPngImage());
  1890. Png->LoadFromResourceName(0, ImageName);
  1891. Image->Picture->Assign(Png.get());
  1892. }