VCLCommon.cpp 69 KB

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