VCLCommon.cpp 70 KB

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