VCLCommon.cpp 71 KB

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