VCLCommon.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164
  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 (ALWAYS_TRUE(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. FAIL;
  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. FAIL;
  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 (ALWAYS_TRUE(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. void __fastcall ApplySystemSettingsOnControl(TControl * Control)
  565. {
  566. #ifdef _DEBUG
  567. VerifyControl(Control);
  568. #endif
  569. // WORKAROUND
  570. // VCL does not scale status par panels (while for instance it does
  571. // scale list view headers). Remove this if they ever "fix" this.
  572. // Neither they scale the status bar size if UseSystemFont is true.
  573. // they claim it should scale with font size, but it does not,
  574. // probably because they eat WM_SIZE in TCustomStatusBar.WMSize.
  575. // See http://qc.embarcadero.com/wc/qcmain.aspx?d=83599
  576. // Check TCustomStatusBar.ChangeScale() for changes.
  577. // For TBX status bars, this is implemented in TTBXCustomStatusBar.ChangeScale
  578. TStatusBar * StatusBar = dynamic_cast<TStatusBar *>(Control);
  579. if (StatusBar != NULL)
  580. {
  581. // We should have UseSystemFont and bottom alignment set for all status bars.
  582. if (ALWAYS_TRUE(StatusBar->UseSystemFont) &&
  583. ALWAYS_TRUE(StatusBar->Align == alBottom))
  584. {
  585. StatusBar->Height = ScaleByTextHeight(StatusBar, StatusBar->Height);
  586. }
  587. for (int Index = 0; Index < StatusBar->Panels->Count; Index++)
  588. {
  589. TStatusPanel * Panel = StatusBar->Panels->Items[Index];
  590. Panel->Width = ScaleByTextHeight(StatusBar, Panel->Width);
  591. }
  592. }
  593. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(Control);
  594. if (Toolbar != NULL)
  595. {
  596. TTBCustomItem * Items = Toolbar->Items;
  597. for (int ItemIndex = 0; ItemIndex < Items->Count; ItemIndex++)
  598. {
  599. // We should recurse
  600. TTBCustomItem * Item = Items->Items[ItemIndex];
  601. TTBEditItem * EditItem = dynamic_cast<TTBEditItem *>(Item);
  602. if (EditItem != NULL)
  603. {
  604. EditItem->EditWidth = ScaleByTextHeight(Toolbar, EditItem->EditWidth);
  605. TTBXComboBoxItem * ComboBoxItem = dynamic_cast<TTBXComboBoxItem *>(EditItem);
  606. if (ComboBoxItem != NULL)
  607. {
  608. ComboBoxItem->MinListWidth =
  609. ScaleByTextHeight(Toolbar, ComboBoxItem->MinListWidth);
  610. ComboBoxItem->MaxListWidth =
  611. ScaleByTextHeight(Toolbar, ComboBoxItem->MaxListWidth);
  612. }
  613. }
  614. }
  615. }
  616. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  617. if (ListView != NULL)
  618. {
  619. TListView * PublicListView = reinterpret_cast<TListView *>(ListView);
  620. for (int Index = 0; Index < PublicListView->Columns->Count; Index++)
  621. {
  622. TListColumn * Column = ListView->Column[Index];
  623. Column->MaxWidth = ScaleByTextHeight(ListView, Column->MaxWidth);
  624. Column->MinWidth = ScaleByTextHeight(ListView, Column->MinWidth);
  625. }
  626. }
  627. // WORKAROUND for lask of public API so mimicking Explorer-style mouse selection
  628. // See http://stackoverflow.com/q/15750842/850848
  629. TCustomIEListView * IEListView = dynamic_cast<TCustomIEListView *>(Control);
  630. if (IEListView != NULL)
  631. {
  632. // It should not be a problem to call the LVM_QUERYINTERFACE
  633. // on earlier versions of Windows. It should be noop.
  634. if (IsWin7())
  635. {
  636. IListView_Win7 * ListViewIntf = NULL;
  637. SendMessage(IEListView->Handle, LVM_QUERYINTERFACE, reinterpret_cast<WPARAM>(&IID_IListView_Win7), reinterpret_cast<LPARAM>(&ListViewIntf));
  638. if (ALWAYS_TRUE(ListViewIntf != NULL))
  639. {
  640. ListViewIntf->SetSelectionFlags(1, 1);
  641. ListViewIntf->Release();
  642. }
  643. }
  644. }
  645. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  646. if (WinControl != NULL)
  647. {
  648. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  649. {
  650. ApplySystemSettingsOnControl(WinControl->Controls[Index]);
  651. }
  652. }
  653. }
  654. //---------------------------------------------------------------------------
  655. // Settings that must be set as soon as possible.
  656. void __fastcall UseSystemSettingsPre(TCustomForm * Control)
  657. {
  658. LocalSystemSettings(Control);
  659. TWndMethod WindowProc;
  660. ((TMethod*)&WindowProc)->Data = Control;
  661. ((TMethod*)&WindowProc)->Code = FormWindowProc;
  662. Control->WindowProc = WindowProc;
  663. if (Control->HelpKeyword.IsEmpty())
  664. {
  665. // temporary help keyword to enable F1 key in all forms
  666. Control->HelpKeyword = L"start";
  667. }
  668. ApplySystemSettingsOnControl(Control);
  669. };
  670. //---------------------------------------------------------------------------
  671. static void FlipAnchors(TControl * Control)
  672. {
  673. // WORKAROUND VCL flips the Align, but not the Anchors
  674. TAnchors Anchors = Control->Anchors;
  675. if (Anchors.Contains(akLeft) != Anchors.Contains(akRight))
  676. {
  677. if (Anchors.Contains(akLeft))
  678. {
  679. Anchors << akRight;
  680. Anchors >> akLeft;
  681. }
  682. else
  683. {
  684. Anchors << akLeft;
  685. Anchors >> akRight;
  686. }
  687. }
  688. Control->Anchors = Anchors;
  689. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  690. if (WinControl != NULL)
  691. {
  692. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  693. {
  694. FlipAnchors(WinControl->Controls[Index]);
  695. }
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. // Settings that must be set only after whole form is constructed
  700. void __fastcall UseSystemSettingsPost(TCustomForm * Control)
  701. {
  702. UnicodeString FlipStr = LoadStr(FLIP_CHILDREN);
  703. int FlipChildrenFlag =
  704. AdjustLocaleFlag(FlipStr, WinConfiguration->FlipChildrenOverride, false, true, false);
  705. if (static_cast<bool>(FlipChildrenFlag))
  706. {
  707. Control->FlipChildren(true);
  708. FlipAnchors(Control);
  709. }
  710. ResetSystemSettings(Control);
  711. };
  712. //---------------------------------------------------------------------------
  713. void __fastcall UseSystemSettings(TCustomForm * Control)
  714. {
  715. UseSystemSettingsPre(Control);
  716. UseSystemSettingsPost(Control);
  717. };
  718. //---------------------------------------------------------------------------
  719. void __fastcall ResetSystemSettings(TCustomForm * /*Control*/)
  720. {
  721. // noop
  722. }
  723. //---------------------------------------------------------------------------
  724. class TPublicForm : public TForm
  725. {
  726. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage);
  727. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  728. };
  729. //---------------------------------------------------------------------------
  730. struct TShowAsModalStorage
  731. {
  732. void * FocusWindowList;
  733. HWND FocusActiveWindow;
  734. TFocusState FocusState;
  735. TCursor SaveCursor;
  736. int SaveCount;
  737. };
  738. //---------------------------------------------------------------------------
  739. void __fastcall ShowAsModal(TForm * Form, void *& Storage)
  740. {
  741. SetCorrectFormParent(Form);
  742. CancelDrag();
  743. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  744. ReleaseCapture();
  745. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  746. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  747. AStorage->FocusActiveWindow = GetActiveWindow();
  748. AStorage->FocusState = SaveFocusState();
  749. Screen->SaveFocusedList->Insert(0, Screen->FocusedForm);
  750. Screen->FocusedForm = Form;
  751. AStorage->SaveCursor = Screen->Cursor;
  752. // This is particularly used when displaing progress window
  753. // while downloading via temporary folder
  754. // (the mouse cursor is yet hidden at this point).
  755. // While the code was added to workaround the above problem,
  756. // it is taken from TCustomForm.ShowModal and
  757. // should have been here ever since.
  758. Screen->Cursor = crDefault;
  759. AStorage->SaveCount = Screen->CursorCount;
  760. AStorage->FocusWindowList = DisableTaskWindows(0);
  761. Form->Show();
  762. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  763. Storage = AStorage;
  764. }
  765. //---------------------------------------------------------------------------
  766. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  767. {
  768. DebugAssert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  769. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  770. Storage = NULL;
  771. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  772. if (GetActiveWindow() != Form->Handle)
  773. {
  774. AStorage->FocusActiveWindow = 0;
  775. }
  776. Form->Hide();
  777. if (Screen->CursorCount == AStorage->SaveCount)
  778. {
  779. Screen->Cursor = AStorage->SaveCursor;
  780. }
  781. else
  782. {
  783. Screen->Cursor = crDefault;
  784. }
  785. EnableTaskWindows(AStorage->FocusWindowList);
  786. if (Screen->SaveFocusedList->Count > 0)
  787. {
  788. Screen->FocusedForm = static_cast<TCustomForm *>(Screen->SaveFocusedList->First());
  789. Screen->SaveFocusedList->Remove(Screen->FocusedForm);
  790. }
  791. else
  792. {
  793. Screen->FocusedForm = NULL;
  794. }
  795. if (AStorage->FocusActiveWindow != 0)
  796. {
  797. SetActiveWindow(AStorage->FocusActiveWindow);
  798. }
  799. RestoreFocusState(AStorage->FocusState);
  800. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  801. delete AStorage;
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  805. {
  806. if (Storage != NULL)
  807. {
  808. HideAsModal(Form, Storage);
  809. }
  810. }
  811. //---------------------------------------------------------------------------
  812. bool __fastcall SelectDirectory(UnicodeString & Path, const UnicodeString Prompt,
  813. bool PreserveFileName)
  814. {
  815. bool Result;
  816. unsigned int ErrorMode;
  817. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  818. try
  819. {
  820. UnicodeString Directory;
  821. UnicodeString FileName;
  822. if (!PreserveFileName || DirectoryExists(ApiPath(Path)))
  823. {
  824. Directory = Path;
  825. }
  826. else
  827. {
  828. Directory = ExtractFilePath(Path);
  829. FileName = ExtractFileName(Path);
  830. }
  831. Result = SelectDirectory(Prompt, L"", Directory);
  832. if (Result)
  833. {
  834. Path = Directory;
  835. if (!FileName.IsEmpty())
  836. {
  837. Path = IncludeTrailingBackslash(Path) + FileName;
  838. }
  839. }
  840. }
  841. __finally
  842. {
  843. SetErrorMode(ErrorMode);
  844. }
  845. return Result;
  846. }
  847. //---------------------------------------------------------------------------
  848. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  849. {
  850. bool AnyChecked = false;
  851. for (int Index = 0; Index < ListView->Items->Count; Index++)
  852. {
  853. if (ListView->Items->Item[Index]->Checked == Checked)
  854. {
  855. AnyChecked = true;
  856. break;
  857. }
  858. }
  859. return AnyChecked;
  860. }
  861. //---------------------------------------------------------------------------
  862. void __fastcall ListViewCheckAll(TListView * ListView,
  863. TListViewCheckAll CheckAll)
  864. {
  865. bool Check;
  866. if (CheckAll == caToggle)
  867. {
  868. Check = ListViewAnyChecked(ListView, false);
  869. }
  870. else
  871. {
  872. Check = (CheckAll == caCheck);
  873. }
  874. for (int Index = 0; Index < ListView->Items->Count; Index++)
  875. {
  876. ListView->Items->Item[Index]->Checked = Check;
  877. }
  878. }
  879. //---------------------------------------------------------------------------
  880. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  881. {
  882. int PrevIndex = ComboBox->ItemIndex;
  883. ComboBox->Items->BeginUpdate();
  884. try
  885. {
  886. ComboBox->Clear();
  887. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  888. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  889. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  890. }
  891. __finally
  892. {
  893. ComboBox->Items->EndUpdate();
  894. }
  895. DebugAssert(PrevIndex < ComboBox->Items->Count);
  896. ComboBox->ItemIndex = PrevIndex;
  897. }
  898. //---------------------------------------------------------------------------
  899. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  900. {
  901. ComboBox->ItemIndex = 2 - Value;
  902. if (ComboBox->ItemIndex < 0)
  903. {
  904. ComboBox->ItemIndex = 0;
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  909. {
  910. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  911. }
  912. //---------------------------------------------------------------------------
  913. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  914. {
  915. switch (Value)
  916. {
  917. case asOn:
  918. CheckBox->State = cbChecked;
  919. break;
  920. case asOff:
  921. CheckBox->State = cbUnchecked;
  922. break;
  923. default:
  924. CheckBox->State = cbGrayed;
  925. break;
  926. }
  927. }
  928. //---------------------------------------------------------------------------
  929. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  930. {
  931. switch (CheckBox->State)
  932. {
  933. case cbChecked:
  934. return asOn;
  935. case cbUnchecked:
  936. return asOff;
  937. default:
  938. return asAuto;
  939. }
  940. }
  941. //---------------------------------------------------------------------------
  942. static const wchar_t PathWordDelimiters[] = L"\\/ ;,.";
  943. //---------------------------------------------------------------------------
  944. static bool IsPathWordDelimiter(wchar_t Ch)
  945. {
  946. return (wcschr(PathWordDelimiters, Ch) != NULL);
  947. }
  948. //---------------------------------------------------------------------------
  949. // Windows algorithm is as follows (tested on W2k):
  950. // right:
  951. // is_delimiter(current)
  952. // false:
  953. // right(left(current) + 1)
  954. // true:
  955. // right(right(current) + 1)
  956. // left:
  957. // right(left(current) + 1)
  958. int CALLBACK PathWordBreakProc(wchar_t * Ch, int Current, int Len, int Code)
  959. {
  960. int Result;
  961. UnicodeString ACh(Ch, Len);
  962. if (Code == WB_ISDELIMITER)
  963. {
  964. // we return negacy of what WinAPI docs says
  965. Result = !IsPathWordDelimiter(ACh[Current + 1]);
  966. }
  967. else if (Code == WB_LEFT)
  968. {
  969. // skip consecutive delimiters
  970. while ((Current > 0) &&
  971. IsPathWordDelimiter(ACh[Current]))
  972. {
  973. Current--;
  974. }
  975. Result = ACh.SubString(1, Current - 1).LastDelimiter(PathWordDelimiters);
  976. }
  977. else if (Code == WB_RIGHT)
  978. {
  979. if (Current == 0)
  980. {
  981. // will be called again with Current == 1
  982. Result = 0;
  983. }
  984. else
  985. {
  986. const wchar_t * P = wcspbrk(ACh.c_str() + Current - 1, PathWordDelimiters);
  987. if (P == NULL)
  988. {
  989. Result = Len;
  990. }
  991. else
  992. {
  993. Result = P - ACh.c_str() + 1;
  994. // skip consecutive delimiters
  995. while ((Result < Len) &&
  996. IsPathWordDelimiter(ACh[Result + 1]))
  997. {
  998. Result++;
  999. }
  1000. }
  1001. }
  1002. }
  1003. else
  1004. {
  1005. FAIL;
  1006. Result = 0;
  1007. }
  1008. return Result;
  1009. }
  1010. //---------------------------------------------------------------------------
  1011. class TPublicCustomCombo : public TCustomCombo
  1012. {
  1013. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  1014. };
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  1017. {
  1018. // Since we are setting Application->ModalPopupMode = pmAuto,
  1019. // this has to be called from OnShow, not from constructor anymore,
  1020. // to have any effect
  1021. HWND Wnd;
  1022. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  1023. {
  1024. TPublicCustomCombo * Combo =
  1025. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  1026. Combo->HandleNeeded();
  1027. Wnd = Combo->EditHandle;
  1028. }
  1029. else
  1030. {
  1031. Wnd = Control->Handle;
  1032. }
  1033. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  1037. {
  1038. int Shift = 0;
  1039. for (int Index = 0; Index < Count; Index++)
  1040. {
  1041. if (ControlsOrder[Index]->Visible)
  1042. {
  1043. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  1044. }
  1045. else
  1046. {
  1047. Shift++;
  1048. }
  1049. }
  1050. Count -= Shift;
  1051. }
  1052. //---------------------------------------------------------------------------
  1053. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  1054. {
  1055. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1056. if (Count > 0)
  1057. {
  1058. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1059. CommonParent->DisableAlign();
  1060. try
  1061. {
  1062. int Top = 0;
  1063. for (int Index = 0; Index < Count; Index++)
  1064. {
  1065. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1066. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  1067. {
  1068. Top = ControlsOrder[Index]->Top;
  1069. }
  1070. }
  1071. for (int Index = 0; Index < Count; Index++)
  1072. {
  1073. TControl * Control = ControlsOrder[Index];
  1074. Control->Top = Top;
  1075. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  1076. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  1077. {
  1078. Top += Control->Height;
  1079. }
  1080. }
  1081. }
  1082. __finally
  1083. {
  1084. CommonParent->EnableAlign();
  1085. }
  1086. }
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  1090. {
  1091. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1092. if (Count > 0)
  1093. {
  1094. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1095. CommonParent->DisableAlign();
  1096. try
  1097. {
  1098. int Left = 0;
  1099. for (int Index = 0; Index < Count; Index++)
  1100. {
  1101. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1102. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  1103. {
  1104. Left = ControlsOrder[Index]->Left;
  1105. }
  1106. }
  1107. for (int Index = 0; Index < Count; Index++)
  1108. {
  1109. TControl * Control = ControlsOrder[Index];
  1110. Control->Left = Left;
  1111. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  1112. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  1113. {
  1114. Left += Control->Width;
  1115. }
  1116. // vertical alignment has priority, so alBottom-aligned controls start
  1117. // at the very left, even if there are any alLeft/alRight controls.
  1118. // for the reason this code is not necessary in SetVerticalControlsOrder.
  1119. // we could exit the loop as well here.
  1120. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  1121. {
  1122. Left = 0;
  1123. }
  1124. }
  1125. }
  1126. __finally
  1127. {
  1128. CommonParent->EnableAlign();
  1129. }
  1130. }
  1131. }
  1132. //---------------------------------------------------------------------------
  1133. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  1134. {
  1135. if (After->TabOrder > Control->TabOrder)
  1136. {
  1137. After->TabOrder = Control->TabOrder;
  1138. }
  1139. else if (After->TabOrder < Control->TabOrder - 1)
  1140. {
  1141. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  1142. }
  1143. }
  1144. //---------------------------------------------------------------------------
  1145. void __fastcall CutFormToDesktop(TForm * Form)
  1146. {
  1147. DebugAssert(Form->Monitor != NULL);
  1148. TRect Workarea = Form->Monitor->WorkareaRect;
  1149. if (Form->Top + Form->Height > Workarea.Bottom)
  1150. {
  1151. Form->Height = Workarea.Bottom - Form->Top;
  1152. }
  1153. if (Form->Left + Form->Width >= Workarea.Right)
  1154. {
  1155. Form->Width = Workarea.Right - Form->Left;
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  1160. {
  1161. if ((Position == poScreenCenter) ||
  1162. (Position == poOwnerFormCenter) ||
  1163. (Position == poMainFormCenter))
  1164. {
  1165. TCustomForm * CenterForm = NULL;
  1166. if ((Position == poOwnerFormCenter) ||
  1167. (Position == poMainFormCenter))
  1168. {
  1169. CenterForm = Application->MainForm;
  1170. if ((Position == poOwnerFormCenter) &&
  1171. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  1172. {
  1173. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  1174. }
  1175. }
  1176. TRect Bounds = Form->BoundsRect;
  1177. int X, Y;
  1178. if (CenterForm != NULL)
  1179. {
  1180. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  1181. ((TForm *)CenterForm)->Left;
  1182. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  1183. ((TForm *)CenterForm)->Top;
  1184. }
  1185. else
  1186. {
  1187. X = (Form->Monitor->Width - Bounds.Width()) / 2;
  1188. Y = (Form->Monitor->Height - Bounds.Height()) / 2;
  1189. }
  1190. if (X < 0)
  1191. {
  1192. X = 0;
  1193. }
  1194. if (Y < 0)
  1195. {
  1196. Y = 0;
  1197. }
  1198. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  1199. }
  1200. }
  1201. //---------------------------------------------------------------------------
  1202. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  1203. {
  1204. // This has to be called only after DoFormWindowProc(CM_SHOWINGCHANGED),
  1205. // so that a correct monitor is considered.
  1206. // Note that we cannot use LastMonitor(), as ResizeForm is also called from
  1207. // TConsoleDialog::DoAdjustWindow, where we need to use the actual monitor
  1208. // (in case user moves the console window to a different monitor,
  1209. // than where a main window is [no matter how unlikely that is])
  1210. TRect WorkareaRect = Form->Monitor->WorkareaRect;
  1211. if (Height > WorkareaRect.Height())
  1212. {
  1213. Height = WorkareaRect.Height();
  1214. }
  1215. if (Width > WorkareaRect.Width())
  1216. {
  1217. Width = WorkareaRect.Width();
  1218. }
  1219. if (Height < Form->Constraints->MinHeight)
  1220. {
  1221. Height = Form->Constraints->MinHeight;
  1222. }
  1223. if (Width < Form->Constraints->MinWidth)
  1224. {
  1225. Width = Form->Constraints->MinWidth;
  1226. }
  1227. TRect Bounds = Form->BoundsRect;
  1228. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  1229. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  1230. if (Top + Height > WorkareaRect.Bottom)
  1231. {
  1232. Top = WorkareaRect.Bottom - Height;
  1233. }
  1234. if (Left + Width >= WorkareaRect.Right)
  1235. {
  1236. Left = WorkareaRect.Right - Width;
  1237. }
  1238. // WorkareaRect.Left is not 0, when secondary monitor is placed left of primary one.
  1239. // Similarly for WorkareaRect.Top.
  1240. if (Top < WorkareaRect.Top)
  1241. {
  1242. Top = WorkareaRect.Top;
  1243. }
  1244. if (Left < WorkareaRect.Left)
  1245. {
  1246. Left = WorkareaRect.Left;
  1247. }
  1248. Form->SetBounds(Left, Top, Width, Height);
  1249. Bounds = Form->BoundsRect;
  1250. // due to constraints, form can remain larger, make sure it is centered although
  1251. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  1252. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  1253. Form->SetBounds(Left, Top, Width, Height);
  1254. }
  1255. //---------------------------------------------------------------------------
  1256. TComponent * __fastcall GetFormOwner()
  1257. {
  1258. if (Screen->ActiveForm != NULL)
  1259. {
  1260. return Screen->ActiveForm;
  1261. }
  1262. else
  1263. {
  1264. return Application;
  1265. }
  1266. }
  1267. //---------------------------------------------------------------------------
  1268. void __fastcall SetCorrectFormParent(TForm * /*Form*/)
  1269. {
  1270. // noop
  1271. // remove
  1272. }
  1273. //---------------------------------------------------------------------------
  1274. void __fastcall InvokeHelp(TWinControl * Control)
  1275. {
  1276. DebugAssert(Control != NULL);
  1277. HELPINFO HelpInfo;
  1278. HelpInfo.cbSize = sizeof(HelpInfo);
  1279. HelpInfo.iContextType = HELPINFO_WINDOW;
  1280. HelpInfo.iCtrlId = 0;
  1281. HelpInfo.hItemHandle = Control->Handle;
  1282. HelpInfo.dwContextId = 0;
  1283. HelpInfo.MousePos.x = 0;
  1284. HelpInfo.MousePos.y = 0;
  1285. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  1286. }
  1287. //---------------------------------------------------------------------------
  1288. //---------------------------------------------------------------------------
  1289. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  1290. TControlCanvas ** ACanvas, TRect & R)
  1291. {
  1292. TControlCanvas * Canvas = new TControlCanvas();
  1293. try
  1294. {
  1295. Canvas->Control = StaticText;
  1296. R = StaticText->ClientRect;
  1297. TSize TextSize;
  1298. if (StaticText->AutoSize)
  1299. {
  1300. // We possibly could use the same code as in !AutoSize branch,
  1301. // keeping this to avoid problems in existing code
  1302. UnicodeString Caption = StaticText->Caption;
  1303. bool AccelChar = false;
  1304. if (StaticText->ShowAccelChar)
  1305. {
  1306. Caption = StripHotkey(Caption);
  1307. AccelChar = (Caption != StaticText->Caption);
  1308. }
  1309. TextSize = Canvas->TextExtent(Caption);
  1310. DebugAssert(StaticText->BorderStyle == sbsNone); // not taken into account
  1311. if (AccelChar)
  1312. {
  1313. TextSize.cy += 2;
  1314. }
  1315. }
  1316. else
  1317. {
  1318. TRect TextRect;
  1319. SetRect(&TextRect, 0, 0, StaticText->Width, 0);
  1320. DrawText(Canvas->Handle, StaticText->Caption.c_str(), -1, &TextRect,
  1321. DT_CALCRECT | DT_WORDBREAK |
  1322. StaticText->DrawTextBiDiModeFlagsReadingOnly());
  1323. TextSize = TextRect.GetSize();
  1324. }
  1325. R.Bottom = R.Top + TextSize.cy;
  1326. // Should call ChangeBiDiModeAlignment when UseRightToLeftAlignment(),
  1327. // but the label seems to draw the text wrongly aligned, even though
  1328. // the alignment is correctly flipped in TCustomStaticText.CreateParams
  1329. switch (StaticText->Alignment)
  1330. {
  1331. case taLeftJustify:
  1332. R.Right = R.Left + TextSize.cx;
  1333. break;
  1334. case taRightJustify:
  1335. R.Left = Max(0, R.Right - TextSize.cx);
  1336. break;
  1337. case taCenter:
  1338. {
  1339. FAIL; // not used branch, possibly untested
  1340. int Diff = R.Width() - TextSize.cx;
  1341. R.Left += Diff / 2;
  1342. R.Right -= Diff - (Diff / 2);
  1343. }
  1344. break;
  1345. }
  1346. }
  1347. __finally
  1348. {
  1349. if (ACanvas == NULL)
  1350. {
  1351. delete Canvas;
  1352. }
  1353. }
  1354. if (ACanvas != NULL)
  1355. {
  1356. *ACanvas = Canvas;
  1357. }
  1358. }
  1359. //---------------------------------------------------------------------------
  1360. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1361. bool & Clicked)
  1362. {
  1363. Clicked = false;
  1364. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1365. if (Message.Msg == WM_LBUTTONDOWN)
  1366. {
  1367. StaticText->SetFocus();
  1368. // in case the action takes long, make sure focus is shown immediately
  1369. UpdateWindow(StaticText->Handle);
  1370. Clicked = true;
  1371. Message.Result = 1;
  1372. }
  1373. else if (Message.Msg == WM_RBUTTONDOWN)
  1374. {
  1375. StaticText->SetFocus();
  1376. Message.Result = 1;
  1377. }
  1378. else if (Message.Msg == WM_CHAR)
  1379. {
  1380. if (reinterpret_cast<TWMChar &>(Message).CharCode == L' ')
  1381. {
  1382. Clicked = true;
  1383. Message.Result = 1;
  1384. }
  1385. else
  1386. {
  1387. ControlWndProc(StaticText)(Message);
  1388. }
  1389. }
  1390. else if (Message.Msg == CM_DIALOGCHAR)
  1391. {
  1392. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1393. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1394. {
  1395. StaticText->SetFocus();
  1396. // in case the action takes long, make sure focus is shown immediately
  1397. UpdateWindow(StaticText->Handle);
  1398. Clicked = true;
  1399. Message.Result = 1;
  1400. }
  1401. else
  1402. {
  1403. ControlWndProc(StaticText)(Message);
  1404. }
  1405. }
  1406. else
  1407. {
  1408. ControlWndProc(StaticText)(Message);
  1409. }
  1410. if (Message.Msg == WM_PAINT)
  1411. {
  1412. TRect R;
  1413. TControlCanvas * Canvas;
  1414. FocusableLabelCanvas(StaticText, &Canvas, R);
  1415. try
  1416. {
  1417. if (StaticText->Focused())
  1418. {
  1419. Canvas->DrawFocusRect(R);
  1420. }
  1421. else if (!StaticText->Font->Style.Contains(fsUnderline))
  1422. {
  1423. Canvas->Pen->Style = psDot;
  1424. Canvas->Brush->Style = bsClear;
  1425. if (!StaticText->Enabled)
  1426. {
  1427. Canvas->Pen->Color = clBtnHighlight;
  1428. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1429. Canvas->LineTo(R.Right + 1, R.Bottom);
  1430. Canvas->Pen->Color = clGrayText;
  1431. }
  1432. Canvas->MoveTo(R.Left, R.Bottom - 1);
  1433. Canvas->LineTo(R.Right, R.Bottom - 1);
  1434. }
  1435. }
  1436. __finally
  1437. {
  1438. delete Canvas;
  1439. }
  1440. }
  1441. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1442. (Message.Msg == CM_ENABLEDCHANGED))
  1443. {
  1444. StaticText->Invalidate();
  1445. }
  1446. }
  1447. //---------------------------------------------------------------------------
  1448. static THintWindow * PersistentHintWindow = NULL;
  1449. static TControl * PersistentHintControl = NULL;
  1450. //---------------------------------------------------------------------------
  1451. void __fastcall CancelPersistentHint()
  1452. {
  1453. if (PersistentHintWindow != NULL)
  1454. {
  1455. PersistentHintControl = NULL;
  1456. SAFE_DESTROY(PersistentHintWindow);
  1457. }
  1458. }
  1459. //---------------------------------------------------------------------------
  1460. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1461. {
  1462. CancelPersistentHint();
  1463. THintInfo HintInfo;
  1464. HintInfo.HintControl = Control;
  1465. HintInfo.HintPos = HintPos;
  1466. HintInfo.HintMaxWidth = GetParentForm(Control)->Monitor->Width;
  1467. HintInfo.HintColor = Application->HintColor;
  1468. HintInfo.HintStr = GetShortHint(Control->Hint);
  1469. HintInfo.HintData = NULL;
  1470. bool CanShow = true;
  1471. if (Application->OnShowHint != NULL)
  1472. {
  1473. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1474. }
  1475. if (CanShow)
  1476. {
  1477. PersistentHintControl = Control;
  1478. PersistentHintWindow = new THintWindow(Application);
  1479. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1480. PersistentHintWindow->Color = HintInfo.HintColor;
  1481. TRect HintWinRect;
  1482. if (HintInfo.HintMaxWidth < Control->Width)
  1483. {
  1484. HintInfo.HintMaxWidth = Control->Width;
  1485. }
  1486. HintWinRect = PersistentHintWindow->CalcHintRect(
  1487. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1488. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1489. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1490. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1491. }
  1492. }
  1493. //---------------------------------------------------------------------------
  1494. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1495. {
  1496. bool Clicked = false;
  1497. bool Cancel = false;
  1498. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1499. if (Message.Msg == CM_HINTSHOW)
  1500. {
  1501. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1502. if (PersistentHintControl == StaticText)
  1503. {
  1504. // do not allow standard hint when persistent is already shown
  1505. HintShow.Result = 1;
  1506. }
  1507. else
  1508. {
  1509. HintShow.HintInfo->HideTimeout = 100000; // never
  1510. }
  1511. }
  1512. else if (Message.Msg == CN_KEYDOWN)
  1513. {
  1514. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1515. (PersistentHintControl == StaticText))
  1516. {
  1517. CancelPersistentHint();
  1518. StaticText->Invalidate();
  1519. Message.Result = 1;
  1520. }
  1521. else
  1522. {
  1523. FocusableLabelWindowProc(Data, Message, Clicked);
  1524. }
  1525. }
  1526. else
  1527. {
  1528. FocusableLabelWindowProc(Data, Message, Clicked);
  1529. }
  1530. if (Message.Msg == CM_CANCELMODE)
  1531. {
  1532. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  1533. if ((CancelMessage.Sender != StaticText) &&
  1534. (CancelMessage.Sender != PersistentHintWindow))
  1535. {
  1536. Cancel = true;
  1537. }
  1538. }
  1539. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  1540. {
  1541. Cancel = true;
  1542. }
  1543. if (Cancel && (PersistentHintControl == StaticText))
  1544. {
  1545. CancelPersistentHint();
  1546. }
  1547. if (Clicked && (PersistentHintControl != StaticText))
  1548. {
  1549. TRect R;
  1550. TPoint HintPos;
  1551. FocusableLabelCanvas(StaticText, NULL, R);
  1552. HintPos.y = R.Bottom - R.Top;
  1553. HintPos.x = R.Left;
  1554. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  1555. }
  1556. }
  1557. //---------------------------------------------------------------------------
  1558. void __fastcall HintLabel(TStaticText * StaticText, UnicodeString Hint)
  1559. {
  1560. // Currently all are right-justified, when other alignment is used,
  1561. // test respective branches in FocusableLabelCanvas.
  1562. DebugAssert(StaticText->Alignment == taRightJustify);
  1563. // With right-justify, it has to be off. We may not notice on original
  1564. // English version, results will differ with translations only
  1565. DebugAssert(!StaticText->AutoSize);
  1566. StaticText->ParentFont = true;
  1567. if (!Hint.IsEmpty())
  1568. {
  1569. StaticText->Hint = Hint;
  1570. }
  1571. StaticText->ShowHint = true;
  1572. StaticText->Cursor = crHandPoint;
  1573. TWndMethod WindowProc;
  1574. ((TMethod*)&WindowProc)->Data = StaticText;
  1575. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  1576. StaticText->WindowProc = WindowProc;
  1577. }
  1578. //---------------------------------------------------------------------------
  1579. void __fastcall HintLabelRestore(TStaticText * StaticText)
  1580. {
  1581. StaticText->WindowProc = ControlWndProc(StaticText);
  1582. StaticText->ShowHint = false;
  1583. StaticText->Cursor = crDefault;
  1584. }
  1585. //---------------------------------------------------------------------------
  1586. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  1587. {
  1588. // it is TCustomComboxBox, but the properties are published only by TComboBox
  1589. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  1590. if (Message.Msg == WM_SIZE)
  1591. {
  1592. UnicodeString Text = ComboBox->Text;
  1593. try
  1594. {
  1595. ControlWndProc(ComboBox)(Message);
  1596. }
  1597. __finally
  1598. {
  1599. // workaround for bug in combo box, that causes it to change text to any
  1600. // item from drop down list which starts with current text,
  1601. // after control is resized (unless the text is in drop down list as well)
  1602. ComboBox->Text = Text;
  1603. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  1604. if (!ComboBox->Focused())
  1605. {
  1606. ComboBox->SelLength = 0;
  1607. }
  1608. }
  1609. }
  1610. else
  1611. {
  1612. ControlWndProc(ComboBox)(Message);
  1613. }
  1614. }
  1615. //---------------------------------------------------------------------------
  1616. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  1617. {
  1618. TWndMethod WindowProc;
  1619. ((TMethod*)&WindowProc)->Data = ComboBox;
  1620. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  1621. ComboBox->WindowProc = WindowProc;
  1622. }
  1623. //---------------------------------------------------------------------------
  1624. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  1625. {
  1626. if (StaticText->OnClick != NULL)
  1627. {
  1628. StaticText->OnClick(StaticText);
  1629. }
  1630. else
  1631. {
  1632. OpenBrowser(StaticText->Caption);
  1633. }
  1634. }
  1635. //---------------------------------------------------------------------------
  1636. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  1637. {
  1638. bool Clicked = false;
  1639. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1640. if (Message.Msg == WM_CONTEXTMENU)
  1641. {
  1642. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  1643. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  1644. {
  1645. TRect R;
  1646. FocusableLabelCanvas(StaticText, NULL, R);
  1647. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  1648. ContextMenu.Pos.x = static_cast<short>(P.x);
  1649. ContextMenu.Pos.y = static_cast<short>(P.y);
  1650. }
  1651. }
  1652. else if (Message.Msg == WM_KEYDOWN)
  1653. {
  1654. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  1655. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == L'C'))
  1656. {
  1657. TInstantOperationVisualizer Visualizer;
  1658. CopyToClipboard(StaticText->Caption);
  1659. Message.Result = 1;
  1660. }
  1661. else
  1662. {
  1663. FocusableLabelWindowProc(Data, Message, Clicked);
  1664. }
  1665. }
  1666. FocusableLabelWindowProc(Data, Message, Clicked);
  1667. if (Message.Msg == WM_DESTROY)
  1668. {
  1669. delete StaticText->PopupMenu;
  1670. DebugAssert(StaticText->PopupMenu == NULL);
  1671. }
  1672. if (Clicked)
  1673. {
  1674. LinkLabelClick(StaticText);
  1675. }
  1676. }
  1677. //---------------------------------------------------------------------------
  1678. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  1679. {
  1680. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1681. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  1682. DebugAssert(MenuItem != NULL);
  1683. if (MenuItem->Tag == 0)
  1684. {
  1685. LinkLabelClick(StaticText);
  1686. }
  1687. else
  1688. {
  1689. TInstantOperationVisualizer Visualizer;
  1690. CopyToClipboard(StaticText->Caption);
  1691. }
  1692. }
  1693. //---------------------------------------------------------------------------
  1694. void __fastcall LinkLabel(TStaticText * StaticText, UnicodeString Url,
  1695. TNotifyEvent OnEnter)
  1696. {
  1697. StaticText->Transparent = false;
  1698. StaticText->ParentFont = true;
  1699. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  1700. StaticText->Font->Color = LinkColor;
  1701. StaticText->Cursor = crHandPoint;
  1702. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  1703. if (!Url.IsEmpty())
  1704. {
  1705. StaticText->Caption = Url;
  1706. }
  1707. bool IsUrl = IsHttpUrl(StaticText->Caption);
  1708. if (IsUrl)
  1709. {
  1710. DebugAssert(StaticText->PopupMenu == NULL);
  1711. StaticText->PopupMenu = new TPopupMenu(StaticText);
  1712. try
  1713. {
  1714. TNotifyEvent ContextMenuOnClick;
  1715. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  1716. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  1717. TMenuItem * Item;
  1718. Item = new TMenuItem(StaticText->PopupMenu);
  1719. Item->Caption = LoadStr(URL_LINK_OPEN);
  1720. Item->Tag = 0;
  1721. Item->ShortCut = ShortCut(L' ', TShiftState());
  1722. Item->OnClick = ContextMenuOnClick;
  1723. StaticText->PopupMenu->Items->Add(Item);
  1724. Item = new TMenuItem(StaticText->PopupMenu);
  1725. Item->Caption = LoadStr(URL_LINK_COPY);
  1726. Item->Tag = 1;
  1727. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  1728. Item->OnClick = ContextMenuOnClick;
  1729. StaticText->PopupMenu->Items->Add(Item);
  1730. }
  1731. catch(...)
  1732. {
  1733. delete StaticText->PopupMenu;
  1734. DebugAssert(StaticText->PopupMenu == NULL);
  1735. throw;
  1736. }
  1737. }
  1738. TWndMethod WindowProc;
  1739. ((TMethod*)&WindowProc)->Data = StaticText;
  1740. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  1741. StaticText->WindowProc = WindowProc;
  1742. }
  1743. //---------------------------------------------------------------------------
  1744. static void __fastcall HotTrackLabelMouseEnter(void * /*Data*/, TObject * Sender)
  1745. {
  1746. reinterpret_cast<TLabel *>(Sender)->Font->Color = clBlue;
  1747. }
  1748. //---------------------------------------------------------------------------
  1749. static void __fastcall HotTrackLabelMouseLeave(void * /*Data*/, TObject * Sender)
  1750. {
  1751. reinterpret_cast<TLabel *>(Sender)->ParentFont = true;
  1752. }
  1753. //---------------------------------------------------------------------------
  1754. void __fastcall HotTrackLabel(TLabel * Label)
  1755. {
  1756. DebugAssert(Label->OnMouseEnter == NULL);
  1757. DebugAssert(Label->OnMouseLeave == NULL);
  1758. Label->OnMouseEnter = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseEnter);
  1759. Label->OnMouseLeave = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseLeave);
  1760. }
  1761. //---------------------------------------------------------------------------
  1762. Forms::TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  1763. {
  1764. Forms::TMonitor * Result;
  1765. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  1766. {
  1767. Result = Application->MainForm->Monitor;
  1768. }
  1769. else if (LastMonitor != NULL)
  1770. {
  1771. Result = LastMonitor;
  1772. }
  1773. else
  1774. {
  1775. int i = 0;
  1776. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  1777. {
  1778. i++;
  1779. }
  1780. DebugAssert(Screen->Monitors[i]->Primary);
  1781. Result = Screen->Monitors[i];
  1782. }
  1783. return Result;
  1784. }
  1785. //---------------------------------------------------------------------------
  1786. int __fastcall GetLastMonitor()
  1787. {
  1788. if (LastMonitor != NULL)
  1789. {
  1790. return LastMonitor->MonitorNum;
  1791. }
  1792. else
  1793. {
  1794. return -1;
  1795. }
  1796. }
  1797. //---------------------------------------------------------------------------
  1798. void __fastcall SetLastMonitor(int MonitorNum)
  1799. {
  1800. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  1801. {
  1802. LastMonitor = Screen->Monitors[MonitorNum];
  1803. }
  1804. else
  1805. {
  1806. LastMonitor = NULL;
  1807. }
  1808. }
  1809. //---------------------------------------------------------------------------
  1810. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  1811. {
  1812. TForm * Form;
  1813. if (Owner == NULL)
  1814. {
  1815. Owner = GetFormOwner();
  1816. }
  1817. // If there is no main form yet, make this one main.
  1818. // This:
  1819. // - Makes other forms (dialogs invoked from this one),
  1820. // be placed on the same monitor (otherwise all new forms get placed
  1821. // on primary monitor)
  1822. // - Triggers MainForm-specific code in DoFormWindowProc.
  1823. // - Shows button on taskbar
  1824. if (Application->MainForm == NULL)
  1825. {
  1826. Application->CreateForm(FormClass, &Form);
  1827. DebugAssert(Application->MainForm == Form);
  1828. }
  1829. else
  1830. {
  1831. Form = dynamic_cast<TForm *>(Construct(FormClass, Owner));
  1832. DebugAssert(Form != NULL);
  1833. }
  1834. return Form;
  1835. }
  1836. //---------------------------------------------------------------------------
  1837. TImageList * __fastcall SharedSystemImageList(bool Large)
  1838. {
  1839. TSHFileInfo FileInfo;
  1840. TImageList * Result = new TImageList(Application);
  1841. int ImageListHandle = SHGetFileInfo(L"", 0, &FileInfo, sizeof(FileInfo),
  1842. SHGFI_SYSICONINDEX | (Large ? SHGFI_LARGEICON : SHGFI_SMALLICON));
  1843. if (ImageListHandle != 0)
  1844. {
  1845. Result->ShareImages = true;
  1846. Result->Handle = ImageListHandle;
  1847. }
  1848. return Result;
  1849. }
  1850. //---------------------------------------------------------------------------
  1851. bool __fastcall SupportsSplitButton()
  1852. {
  1853. return (Win32MajorVersion >= 6);
  1854. }
  1855. //---------------------------------------------------------------------------
  1856. static TButton * __fastcall FindDefaultButton(TWinControl * Control)
  1857. {
  1858. TButton * Result = NULL;
  1859. int Index = 0;
  1860. while ((Result == NULL) && (Index < Control->ControlCount))
  1861. {
  1862. TControl * ChildControl = Control->Controls[Index];
  1863. TButton * Button = dynamic_cast<TButton *>(ChildControl);
  1864. if ((Button != NULL) && Button->Default)
  1865. {
  1866. Result = Button;
  1867. }
  1868. else
  1869. {
  1870. TWinControl * WinControl = dynamic_cast<TWinControl *>(ChildControl);
  1871. if (WinControl != NULL)
  1872. {
  1873. Result = FindDefaultButton(WinControl);
  1874. }
  1875. }
  1876. Index++;
  1877. }
  1878. return Result;
  1879. }
  1880. //---------------------------------------------------------------------------
  1881. TModalResult __fastcall DefaultResult(TCustomForm * Form, TButton * DefaultButton)
  1882. {
  1883. // The point of this is to avoid hardcoding mrOk when checking dialog results.
  1884. // Previously we used != mrCancel instead, as mrCancel is more reliable,
  1885. // being automatically used for Esc/X buttons (and hence kind of forced to be used
  1886. // for Cancel buttons). But that failed to be reliable in the end, for
  1887. // ModalResult being mrNone, when Windows session is being logged off.
  1888. // We interpreted mrNone as OK, causing lots of troubles.
  1889. TModalResult Result = mrNone;
  1890. TButton * Button = FindDefaultButton(Form);
  1891. if (ALWAYS_TRUE(Button != NULL))
  1892. {
  1893. Result = Button->ModalResult;
  1894. }
  1895. if (Result == mrNone)
  1896. {
  1897. DebugAssert((DefaultButton != NULL) && (DefaultButton->ModalResult != mrNone));
  1898. Result = DefaultButton->ModalResult;
  1899. }
  1900. else
  1901. {
  1902. // If default button fallback was provided,
  1903. // make sure it is the default button we actually detected
  1904. DebugAssert((DefaultButton == NULL) || (Button == DefaultButton));
  1905. }
  1906. return Result;
  1907. }
  1908. //---------------------------------------------------------------------------
  1909. void __fastcall DefaultButton(TButton * Button, bool Default)
  1910. {
  1911. // default property setter does not have guard for "the same value"
  1912. if (Button->Default != Default)
  1913. {
  1914. Button->Default = Default;
  1915. }
  1916. }
  1917. //---------------------------------------------------------------------------
  1918. void __fastcall MemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  1919. {
  1920. // Sender can be Form or Memo itself
  1921. TControl * Control = dynamic_cast<TControl *>(Sender);
  1922. if (ALWAYS_TRUE(Control != NULL))
  1923. {
  1924. TCustomForm * Form = GetParentForm(Control);
  1925. // Particularly when WantReturns is true,
  1926. // memo swallows also Esc, so we have to handle it ourselves.
  1927. // See also ReadOnlyControl.
  1928. if ((Key == VK_ESCAPE) && Shift.Empty())
  1929. {
  1930. Form->ModalResult = mrCancel;
  1931. Key = 0;
  1932. }
  1933. else if ((Key == VK_RETURN) && Shift.Contains(ssCtrl))
  1934. {
  1935. Form->ModalResult = DefaultResult(Form);
  1936. Key = 0;
  1937. }
  1938. }
  1939. }
  1940. //---------------------------------------------------------------------------
  1941. class TIconOwnerComponent : public TComponent
  1942. {
  1943. public:
  1944. __fastcall TIconOwnerComponent(TIcon * Icon) :
  1945. TComponent(NULL),
  1946. FIcon(Icon)
  1947. {
  1948. }
  1949. private:
  1950. std::unique_ptr<TIcon> FIcon;
  1951. };
  1952. //---------------------------------------------------------------------------
  1953. static void __fastcall FixFormIcon(TForm * Form, int Size, int WidthMetric, int HeightMetric)
  1954. {
  1955. // Whole this code is to call ReadIcon from Vcl.Graphics.pas with correct size
  1956. // Clone the icon data (whole .ico file content, that is originally loaded from .dfm)
  1957. // to a new TIcon that does not have a size fixed yet (size cannot be changed after handle is allocated)
  1958. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  1959. Form->Icon->SaveToStream(Stream.get());
  1960. std::unique_ptr<TIcon> Icon(new TIcon());
  1961. Stream->Position = 0;
  1962. Icon->LoadFromStream(Stream.get());
  1963. // Set desired size
  1964. Icon->SetSize(GetSystemMetrics(WidthMetric), GetSystemMetrics(HeightMetric));
  1965. // This calls TIcon::RequireHandle that retrieves the best icon for given size
  1966. LPARAM LParam = reinterpret_cast<LPARAM>(Icon->Handle);
  1967. SendMessage(Form->Handle, WM_SETICON, Size, LParam);
  1968. // Make sure the icon is released
  1969. TIconOwnerComponent * IconOwnerComponent = new TIconOwnerComponent(Icon.release());
  1970. IconOwnerComponent->Name = TIconOwnerComponent::QualifiedClassName() + IntToStr(Size);
  1971. Form->InsertComponent(IconOwnerComponent);
  1972. }
  1973. //---------------------------------------------------------------------------
  1974. void __fastcall FixFormIcons(TForm * Form)
  1975. {
  1976. // VCL sets only ICON_BIG (so small icon is scaled down by OS from big icon),
  1977. // and it uses a random (first?) size from the resource,
  1978. // not the best size.
  1979. FixFormIcon(Form, ICON_SMALL, SM_CXSMICON, SM_CYSMICON);
  1980. FixFormIcon(Form, ICON_BIG, SM_CXICON, SM_CYICON);
  1981. // We rely on VCL not calling WM_SETICON ever after
  1982. // (what it would do, if CreateWnd is called again).
  1983. // That would overwrite the ICON_BIG.
  1984. // We might be able to make sure it uses a correct size by calling
  1985. // TIcon.ReleaseHandle and setting a correct size.
  1986. }
  1987. //---------------------------------------------------------------------------
  1988. void __fastcall UseDesktopFont(TControl * Control)
  1989. {
  1990. TCustomStatusBar * StatusBar = dynamic_cast<TCustomStatusBar *>(Control);
  1991. if (StatusBar != NULL)
  1992. {
  1993. // otherwise setting DesktopFont below has no effect
  1994. StatusBar->UseSystemFont = false;
  1995. }
  1996. class TPublicControl : public TControl
  1997. {
  1998. public:
  1999. __property DesktopFont;
  2000. };
  2001. reinterpret_cast<TPublicControl *>(Control)->DesktopFont = true;
  2002. }
  2003. //---------------------------------------------------------------------------
  2004. TShiftState __fastcall AllKeyShiftStates()
  2005. {
  2006. return TShiftState() << ssShift << ssAlt << ssCtrl;
  2007. }
  2008. //---------------------------------------------------------------------------
  2009. static bool __fastcall FormActivationHook(void * Data, TMessage & Message)
  2010. {
  2011. bool Result = false;
  2012. // Some dialogs, when application is restord from minimization,
  2013. // do not get activated. So we do it explicitly here.
  2014. // We cannot do this from TApplication::OnActivate because
  2015. // TApplication.WndProc resets focus to the last active window afterwards.
  2016. // So we override CM_ACTIVATE implementation here completelly.
  2017. if (Message.Msg == CM_ACTIVATE)
  2018. {
  2019. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  2020. if (Screen->FocusedForm == Form)
  2021. {
  2022. ::SetFocus(Form->Handle);
  2023. // VCLCOPY
  2024. if (Application->OnActivate != NULL)
  2025. {
  2026. Application->OnActivate(Application);
  2027. }
  2028. Result = true;
  2029. }
  2030. }
  2031. return Result;
  2032. }
  2033. //---------------------------------------------------------------------------
  2034. void __fastcall HookFormActivation(TCustomForm * Form)
  2035. {
  2036. Application->HookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2037. }
  2038. //---------------------------------------------------------------------------
  2039. void __fastcall UnhookFormActivation(TCustomForm * Form)
  2040. {
  2041. Application->UnhookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2042. }
  2043. //---------------------------------------------------------------------------
  2044. TPanel * __fastcall CreateBlankPanel(TComponent * Owner)
  2045. {
  2046. TPanel * Panel = new TPanel(Owner);
  2047. Panel->BevelOuter = bvNone;
  2048. Panel->BevelInner = bvNone; // default
  2049. Panel->BevelKind = bkNone;
  2050. return Panel;
  2051. }