VCLCommon.cpp 69 KB

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