VCLCommon.cpp 68 KB

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