VCLCommon.cpp 73 KB

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