VCLCommon.cpp 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  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. // When showing an early error message
  724. if (WinConfiguration != NULL)
  725. {
  726. UnicodeString FlipStr = LoadStr(FLIP_CHILDREN);
  727. int FlipChildrenFlag =
  728. AdjustLocaleFlag(FlipStr, WinConfiguration->FlipChildrenOverride, false, true, false);
  729. if (static_cast<bool>(FlipChildrenFlag))
  730. {
  731. Control->FlipChildren(true);
  732. FlipAnchors(Control);
  733. }
  734. }
  735. ResetSystemSettings(Control);
  736. };
  737. //---------------------------------------------------------------------------
  738. void __fastcall UseSystemSettings(TCustomForm * Control)
  739. {
  740. UseSystemSettingsPre(Control);
  741. UseSystemSettingsPost(Control);
  742. };
  743. //---------------------------------------------------------------------------
  744. void __fastcall ResetSystemSettings(TCustomForm * /*Control*/)
  745. {
  746. // noop
  747. }
  748. //---------------------------------------------------------------------------
  749. class TPublicForm : public TForm
  750. {
  751. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage, bool BringToFront);
  752. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  753. friend void __fastcall ShowFormNoActivate(TForm * Form);
  754. };
  755. //---------------------------------------------------------------------------
  756. struct TShowAsModalStorage
  757. {
  758. void * FocusWindowList;
  759. HWND FocusActiveWindow;
  760. TFocusState FocusState;
  761. TCursor SaveCursor;
  762. int SaveCount;
  763. };
  764. //---------------------------------------------------------------------------
  765. void __fastcall ShowAsModal(TForm * Form, void *& Storage, bool BringToFront)
  766. {
  767. SetCorrectFormParent(Form);
  768. CancelDrag();
  769. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  770. ReleaseCapture();
  771. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  772. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  773. AStorage->FocusActiveWindow = GetActiveWindow();
  774. AStorage->FocusState = SaveFocusState();
  775. Screen->SaveFocusedList->Insert(0, Screen->FocusedForm);
  776. Screen->FocusedForm = Form;
  777. AStorage->SaveCursor = Screen->Cursor;
  778. // This is particularly used when displaing progress window
  779. // while downloading via temporary folder
  780. // (the mouse cursor is yet hidden at this point).
  781. // While the code was added to workaround the above problem,
  782. // it is taken from TCustomForm.ShowModal and
  783. // should have been here ever since.
  784. Screen->Cursor = crDefault;
  785. AStorage->SaveCount = Screen->CursorCount;
  786. AStorage->FocusWindowList = DisableTaskWindows(0);
  787. // VCLCOPY (TCustomForm::Show)
  788. Form->Visible = true;
  789. if (BringToFront)
  790. {
  791. Form->BringToFront();
  792. }
  793. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  794. Storage = AStorage;
  795. }
  796. //---------------------------------------------------------------------------
  797. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  798. {
  799. DebugAssert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  800. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  801. Storage = NULL;
  802. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  803. if (GetActiveWindow() != Form->Handle)
  804. {
  805. AStorage->FocusActiveWindow = 0;
  806. }
  807. Form->Hide();
  808. if (Screen->CursorCount == AStorage->SaveCount)
  809. {
  810. Screen->Cursor = AStorage->SaveCursor;
  811. }
  812. else
  813. {
  814. Screen->Cursor = crDefault;
  815. }
  816. EnableTaskWindows(AStorage->FocusWindowList);
  817. if (Screen->SaveFocusedList->Count > 0)
  818. {
  819. Screen->FocusedForm = static_cast<TCustomForm *>(Screen->SaveFocusedList->First());
  820. Screen->SaveFocusedList->Remove(Screen->FocusedForm);
  821. }
  822. else
  823. {
  824. Screen->FocusedForm = NULL;
  825. }
  826. if (AStorage->FocusActiveWindow != 0)
  827. {
  828. SetActiveWindow(AStorage->FocusActiveWindow);
  829. }
  830. RestoreFocusState(AStorage->FocusState);
  831. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  832. delete AStorage;
  833. }
  834. //---------------------------------------------------------------------------
  835. bool __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  836. {
  837. bool Result = (Storage != NULL);
  838. if (Result)
  839. {
  840. HideAsModal(Form, Storage);
  841. }
  842. return Result;
  843. }
  844. //---------------------------------------------------------------------------
  845. bool __fastcall SelectDirectory(UnicodeString & Path, const UnicodeString Prompt,
  846. bool PreserveFileName)
  847. {
  848. bool Result;
  849. unsigned int ErrorMode;
  850. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  851. try
  852. {
  853. UnicodeString Directory;
  854. UnicodeString FileName;
  855. if (!PreserveFileName || DirectoryExists(ApiPath(Path)))
  856. {
  857. Directory = Path;
  858. }
  859. else
  860. {
  861. Directory = ExtractFilePath(Path);
  862. FileName = ExtractFileName(Path);
  863. }
  864. Result = SelectDirectory(Prompt, L"", Directory);
  865. if (Result)
  866. {
  867. Path = Directory;
  868. if (!FileName.IsEmpty())
  869. {
  870. Path = IncludeTrailingBackslash(Path) + FileName;
  871. }
  872. }
  873. }
  874. __finally
  875. {
  876. SetErrorMode(ErrorMode);
  877. }
  878. return Result;
  879. }
  880. //---------------------------------------------------------------------------
  881. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  882. {
  883. bool AnyChecked = false;
  884. for (int Index = 0; Index < ListView->Items->Count; Index++)
  885. {
  886. if (ListView->Items->Item[Index]->Checked == Checked)
  887. {
  888. AnyChecked = true;
  889. break;
  890. }
  891. }
  892. return AnyChecked;
  893. }
  894. //---------------------------------------------------------------------------
  895. void __fastcall ListViewCheckAll(TListView * ListView,
  896. TListViewCheckAll CheckAll)
  897. {
  898. bool Check;
  899. if (CheckAll == caToggle)
  900. {
  901. Check = ListViewAnyChecked(ListView, false);
  902. }
  903. else
  904. {
  905. Check = (CheckAll == caCheck);
  906. }
  907. for (int Index = 0; Index < ListView->Items->Count; Index++)
  908. {
  909. ListView->Items->Item[Index]->Checked = Check;
  910. }
  911. }
  912. //---------------------------------------------------------------------------
  913. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  914. {
  915. int PrevIndex = ComboBox->ItemIndex;
  916. ComboBox->Items->BeginUpdate();
  917. try
  918. {
  919. ComboBox->Clear();
  920. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  921. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  922. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  923. }
  924. __finally
  925. {
  926. ComboBox->Items->EndUpdate();
  927. }
  928. DebugAssert(PrevIndex < ComboBox->Items->Count);
  929. ComboBox->ItemIndex = PrevIndex;
  930. }
  931. //---------------------------------------------------------------------------
  932. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  933. {
  934. ComboBox->ItemIndex = 2 - Value;
  935. if (ComboBox->ItemIndex < 0)
  936. {
  937. ComboBox->ItemIndex = 0;
  938. }
  939. }
  940. //---------------------------------------------------------------------------
  941. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  942. {
  943. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  944. }
  945. //---------------------------------------------------------------------------
  946. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  947. {
  948. switch (Value)
  949. {
  950. case asOn:
  951. CheckBox->State = cbChecked;
  952. break;
  953. case asOff:
  954. CheckBox->State = cbUnchecked;
  955. break;
  956. default:
  957. CheckBox->State = cbGrayed;
  958. break;
  959. }
  960. }
  961. //---------------------------------------------------------------------------
  962. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  963. {
  964. switch (CheckBox->State)
  965. {
  966. case cbChecked:
  967. return asOn;
  968. case cbUnchecked:
  969. return asOff;
  970. default:
  971. return asAuto;
  972. }
  973. }
  974. //---------------------------------------------------------------------------
  975. static const wchar_t PathWordDelimiters[] = L"\\/ ;,.";
  976. //---------------------------------------------------------------------------
  977. static bool IsPathWordDelimiter(wchar_t Ch)
  978. {
  979. return (wcschr(PathWordDelimiters, Ch) != NULL);
  980. }
  981. //---------------------------------------------------------------------------
  982. // Windows algorithm is as follows (tested on W2k):
  983. // right:
  984. // is_delimiter(current)
  985. // false:
  986. // right(left(current) + 1)
  987. // true:
  988. // right(right(current) + 1)
  989. // left:
  990. // right(left(current) + 1)
  991. int CALLBACK PathWordBreakProc(wchar_t * Ch, int Current, int Len, int Code)
  992. {
  993. int Result;
  994. UnicodeString ACh(Ch, Len);
  995. if (Code == WB_ISDELIMITER)
  996. {
  997. // we return negacy of what WinAPI docs says
  998. Result = !IsPathWordDelimiter(ACh[Current + 1]);
  999. }
  1000. else if (Code == WB_LEFT)
  1001. {
  1002. // skip consecutive delimiters
  1003. while ((Current > 0) &&
  1004. IsPathWordDelimiter(ACh[Current]))
  1005. {
  1006. Current--;
  1007. }
  1008. Result = ACh.SubString(1, Current - 1).LastDelimiter(PathWordDelimiters);
  1009. }
  1010. else if (Code == WB_RIGHT)
  1011. {
  1012. if (Current == 0)
  1013. {
  1014. // will be called again with Current == 1
  1015. Result = 0;
  1016. }
  1017. else
  1018. {
  1019. const wchar_t * P = wcspbrk(ACh.c_str() + Current - 1, PathWordDelimiters);
  1020. if (P == NULL)
  1021. {
  1022. Result = Len;
  1023. }
  1024. else
  1025. {
  1026. Result = P - ACh.c_str() + 1;
  1027. // skip consecutive delimiters
  1028. while ((Result < Len) &&
  1029. IsPathWordDelimiter(ACh[Result + 1]))
  1030. {
  1031. Result++;
  1032. }
  1033. }
  1034. }
  1035. }
  1036. else
  1037. {
  1038. DebugFail();
  1039. Result = 0;
  1040. }
  1041. return Result;
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. static void __fastcall PathEditKeyPress(void * /*Data*/, TObject * Sender, char & Key)
  1045. {
  1046. // Ctrl+Backspace
  1047. // Have to use OnKeyPress as the Ctrl+Backspace is handled in WM_CHAR as any other char,
  1048. // so we have to swallow it here to prevent it getting inserted to the text
  1049. if (Key == '\x7F')
  1050. {
  1051. Key = '\0';
  1052. TCustomEdit * Edit = dynamic_cast<TCustomEdit *>(Sender);
  1053. TCustomComboBox * ComboBox = dynamic_cast<TCustomComboBox *>(Sender);
  1054. TWinControl * WinControl = DebugNotNull(dynamic_cast<TWinControl *>(Sender));
  1055. if (((Edit != NULL) && (Edit->SelLength == 0)) ||
  1056. ((ComboBox != NULL) && (ComboBox->SelLength == 0)))
  1057. {
  1058. // See TCustomMaskEdit.SetCursor
  1059. TKeyboardState KeyState;
  1060. GetKeyboardState(KeyState);
  1061. TKeyboardState NewKeyState;
  1062. memset(NewKeyState, 0, sizeof(NewKeyState));
  1063. NewKeyState[VK_CONTROL] = 0x81;
  1064. NewKeyState[VK_SHIFT] = 0x81;
  1065. SetKeyboardState(NewKeyState);
  1066. SendMessage(WinControl->Handle, WM_KEYDOWN, VK_LEFT, 1);
  1067. NewKeyState[VK_SHIFT] = 0;
  1068. NewKeyState[VK_CONTROL] = 0;
  1069. SetKeyboardState(NewKeyState);
  1070. SendMessage(WinControl->Handle, WM_KEYDOWN, VK_DELETE, 1);
  1071. SetKeyboardState(KeyState);
  1072. }
  1073. }
  1074. }
  1075. //---------------------------------------------------------------------------
  1076. class TPublicCustomCombo : public TCustomCombo
  1077. {
  1078. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  1079. };
  1080. //---------------------------------------------------------------------------
  1081. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  1082. {
  1083. // Since we are setting Application->ModalPopupMode = pmAuto,
  1084. // this has to be called from OnShow, not from constructor anymore,
  1085. // to have any effect
  1086. HWND Wnd;
  1087. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  1088. {
  1089. TPublicCustomCombo * Combo =
  1090. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  1091. Combo->HandleNeeded();
  1092. Wnd = Combo->EditHandle;
  1093. }
  1094. else
  1095. {
  1096. Wnd = Control->Handle;
  1097. }
  1098. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  1099. TPublicWinControl * PublicWinControl = static_cast<TPublicWinControl *>(Control);
  1100. if (DebugAlwaysTrue(PublicWinControl->OnKeyDown == NULL))
  1101. {
  1102. PublicWinControl->OnKeyPress = MakeMethod<TKeyPressEvent>(NULL, PathEditKeyPress);
  1103. }
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  1107. {
  1108. int Shift = 0;
  1109. for (int Index = 0; Index < Count; Index++)
  1110. {
  1111. if (ControlsOrder[Index]->Visible)
  1112. {
  1113. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  1114. }
  1115. else
  1116. {
  1117. Shift++;
  1118. }
  1119. }
  1120. Count -= Shift;
  1121. }
  1122. //---------------------------------------------------------------------------
  1123. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  1124. {
  1125. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1126. if (Count > 0)
  1127. {
  1128. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1129. CommonParent->DisableAlign();
  1130. try
  1131. {
  1132. int Top = 0;
  1133. for (int Index = 0; Index < Count; Index++)
  1134. {
  1135. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1136. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  1137. {
  1138. Top = ControlsOrder[Index]->Top;
  1139. }
  1140. }
  1141. for (int Index = 0; Index < Count; Index++)
  1142. {
  1143. TControl * Control = ControlsOrder[Index];
  1144. Control->Top = Top;
  1145. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  1146. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  1147. {
  1148. Top += Control->Height;
  1149. }
  1150. }
  1151. }
  1152. __finally
  1153. {
  1154. CommonParent->EnableAlign();
  1155. }
  1156. }
  1157. }
  1158. //---------------------------------------------------------------------------
  1159. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  1160. {
  1161. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  1162. if (Count > 0)
  1163. {
  1164. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  1165. CommonParent->DisableAlign();
  1166. try
  1167. {
  1168. int Left = 0;
  1169. for (int Index = 0; Index < Count; Index++)
  1170. {
  1171. DebugAssert(ControlsOrder[Index]->Parent == CommonParent);
  1172. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  1173. {
  1174. Left = ControlsOrder[Index]->Left;
  1175. }
  1176. }
  1177. for (int Index = 0; Index < Count; Index++)
  1178. {
  1179. TControl * Control = ControlsOrder[Index];
  1180. Control->Left = Left;
  1181. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  1182. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  1183. {
  1184. Left += Control->Width;
  1185. }
  1186. // vertical alignment has priority, so alBottom-aligned controls start
  1187. // at the very left, even if there are any alLeft/alRight controls.
  1188. // for the reason this code is not necessary in SetVerticalControlsOrder.
  1189. // we could exit the loop as well here.
  1190. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  1191. {
  1192. Left = 0;
  1193. }
  1194. }
  1195. }
  1196. __finally
  1197. {
  1198. CommonParent->EnableAlign();
  1199. }
  1200. }
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  1204. {
  1205. if (After->TabOrder > Control->TabOrder)
  1206. {
  1207. After->TabOrder = Control->TabOrder;
  1208. }
  1209. else if (After->TabOrder < Control->TabOrder - 1)
  1210. {
  1211. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  1212. }
  1213. }
  1214. //---------------------------------------------------------------------------
  1215. void __fastcall CutFormToDesktop(TForm * Form)
  1216. {
  1217. DebugAssert(Form->Monitor != NULL);
  1218. TRect Workarea = Form->Monitor->WorkareaRect;
  1219. if (Form->Top + Form->Height > Workarea.Bottom)
  1220. {
  1221. Form->Height = Workarea.Bottom - Form->Top;
  1222. }
  1223. if (Form->Left + Form->Width >= Workarea.Right)
  1224. {
  1225. Form->Width = Workarea.Right - Form->Left;
  1226. }
  1227. }
  1228. //---------------------------------------------------------------------------
  1229. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  1230. {
  1231. if ((Position == poScreenCenter) ||
  1232. (Position == poOwnerFormCenter) ||
  1233. (Position == poMainFormCenter))
  1234. {
  1235. TCustomForm * CenterForm = NULL;
  1236. if ((Position == poOwnerFormCenter) ||
  1237. (Position == poMainFormCenter))
  1238. {
  1239. CenterForm = Application->MainForm;
  1240. if ((Position == poOwnerFormCenter) &&
  1241. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  1242. {
  1243. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  1244. }
  1245. }
  1246. TRect Bounds = Form->BoundsRect;
  1247. int X, Y;
  1248. if (CenterForm != NULL)
  1249. {
  1250. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  1251. ((TForm *)CenterForm)->Left;
  1252. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  1253. ((TForm *)CenterForm)->Top;
  1254. }
  1255. else
  1256. {
  1257. X = (Form->Monitor->Width - Bounds.Width()) / 2;
  1258. Y = (Form->Monitor->Height - Bounds.Height()) / 2;
  1259. }
  1260. if (X < 0)
  1261. {
  1262. X = 0;
  1263. }
  1264. if (Y < 0)
  1265. {
  1266. Y = 0;
  1267. }
  1268. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  1269. }
  1270. }
  1271. //---------------------------------------------------------------------------
  1272. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  1273. {
  1274. // This has to be called only after DoFormWindowProc(CM_SHOWINGCHANGED),
  1275. // so that a correct monitor is considered.
  1276. // Note that we cannot use LastMonitor(), as ResizeForm is also called from
  1277. // TConsoleDialog::DoAdjustWindow, where we need to use the actual monitor
  1278. // (in case user moves the console window to a different monitor,
  1279. // than where a main window is [no matter how unlikely that is])
  1280. TRect WorkareaRect = Form->Monitor->WorkareaRect;
  1281. if (Height > WorkareaRect.Height())
  1282. {
  1283. Height = WorkareaRect.Height();
  1284. }
  1285. if (Width > WorkareaRect.Width())
  1286. {
  1287. Width = WorkareaRect.Width();
  1288. }
  1289. if (Height < Form->Constraints->MinHeight)
  1290. {
  1291. Height = Form->Constraints->MinHeight;
  1292. }
  1293. if (Width < Form->Constraints->MinWidth)
  1294. {
  1295. Width = Form->Constraints->MinWidth;
  1296. }
  1297. TRect Bounds = Form->BoundsRect;
  1298. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  1299. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  1300. if (Top + Height > WorkareaRect.Bottom)
  1301. {
  1302. Top = WorkareaRect.Bottom - Height;
  1303. }
  1304. if (Left + Width >= WorkareaRect.Right)
  1305. {
  1306. Left = WorkareaRect.Right - Width;
  1307. }
  1308. // WorkareaRect.Left is not 0, when secondary monitor is placed left of primary one.
  1309. // Similarly for WorkareaRect.Top.
  1310. if (Top < WorkareaRect.Top)
  1311. {
  1312. Top = WorkareaRect.Top;
  1313. }
  1314. if (Left < WorkareaRect.Left)
  1315. {
  1316. Left = WorkareaRect.Left;
  1317. }
  1318. Form->SetBounds(Left, Top, Width, Height);
  1319. Bounds = Form->BoundsRect;
  1320. // due to constraints, form can remain larger, make sure it is centered although
  1321. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  1322. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  1323. Form->SetBounds(Left, Top, Width, Height);
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. TComponent * __fastcall GetFormOwner()
  1327. {
  1328. if (Screen->ActiveForm != NULL)
  1329. {
  1330. return Screen->ActiveForm;
  1331. }
  1332. else
  1333. {
  1334. return Application;
  1335. }
  1336. }
  1337. //---------------------------------------------------------------------------
  1338. void __fastcall SetCorrectFormParent(TForm * /*Form*/)
  1339. {
  1340. // noop
  1341. // remove
  1342. }
  1343. //---------------------------------------------------------------------------
  1344. void __fastcall InvokeHelp(TWinControl * Control)
  1345. {
  1346. DebugAssert(Control != NULL);
  1347. HELPINFO HelpInfo;
  1348. HelpInfo.cbSize = sizeof(HelpInfo);
  1349. HelpInfo.iContextType = HELPINFO_WINDOW;
  1350. HelpInfo.iCtrlId = 0;
  1351. HelpInfo.hItemHandle = Control->Handle;
  1352. HelpInfo.dwContextId = 0;
  1353. HelpInfo.MousePos.x = 0;
  1354. HelpInfo.MousePos.y = 0;
  1355. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  1356. }
  1357. //---------------------------------------------------------------------------
  1358. //---------------------------------------------------------------------------
  1359. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  1360. TControlCanvas ** ACanvas, TRect & R)
  1361. {
  1362. TControlCanvas * Canvas = new TControlCanvas();
  1363. try
  1364. {
  1365. Canvas->Control = StaticText;
  1366. R = StaticText->ClientRect;
  1367. TSize TextSize;
  1368. if (StaticText->AutoSize)
  1369. {
  1370. // We possibly could use the same code as in !AutoSize branch,
  1371. // keeping this to avoid problems in existing code
  1372. UnicodeString Caption = StaticText->Caption;
  1373. bool AccelChar = false;
  1374. if (StaticText->ShowAccelChar)
  1375. {
  1376. Caption = StripHotkey(Caption);
  1377. AccelChar = (Caption != StaticText->Caption);
  1378. }
  1379. TextSize = Canvas->TextExtent(Caption);
  1380. DebugAssert(StaticText->BorderStyle == sbsNone); // not taken into account
  1381. if (AccelChar)
  1382. {
  1383. TextSize.cy += 2;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. TRect TextRect;
  1389. SetRect(&TextRect, 0, 0, StaticText->Width, 0);
  1390. DrawText(Canvas->Handle, StaticText->Caption.c_str(), -1, &TextRect,
  1391. DT_CALCRECT | DT_WORDBREAK |
  1392. StaticText->DrawTextBiDiModeFlagsReadingOnly());
  1393. TextSize = TextRect.GetSize();
  1394. }
  1395. R.Bottom = R.Top + TextSize.cy;
  1396. // Should call ChangeBiDiModeAlignment when UseRightToLeftAlignment(),
  1397. // but the label seems to draw the text wrongly aligned, even though
  1398. // the alignment is correctly flipped in TCustomStaticText.CreateParams
  1399. switch (StaticText->Alignment)
  1400. {
  1401. case taLeftJustify:
  1402. R.Right = R.Left + TextSize.cx;
  1403. break;
  1404. case taRightJustify:
  1405. R.Left = Max(0, R.Right - TextSize.cx);
  1406. break;
  1407. case taCenter:
  1408. {
  1409. DebugFail(); // not used branch, possibly untested
  1410. int Diff = R.Width() - TextSize.cx;
  1411. R.Left += Diff / 2;
  1412. R.Right -= Diff - (Diff / 2);
  1413. }
  1414. break;
  1415. }
  1416. }
  1417. __finally
  1418. {
  1419. if (ACanvas == NULL)
  1420. {
  1421. delete Canvas;
  1422. }
  1423. }
  1424. if (ACanvas != NULL)
  1425. {
  1426. *ACanvas = Canvas;
  1427. }
  1428. }
  1429. //---------------------------------------------------------------------------
  1430. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1431. bool & Clicked)
  1432. {
  1433. Clicked = false;
  1434. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1435. if (Message.Msg == WM_LBUTTONDOWN)
  1436. {
  1437. StaticText->SetFocus();
  1438. // in case the action takes long, make sure focus is shown immediately
  1439. UpdateWindow(StaticText->Handle);
  1440. Clicked = true;
  1441. Message.Result = 1;
  1442. }
  1443. else if (Message.Msg == WM_RBUTTONDOWN)
  1444. {
  1445. StaticText->SetFocus();
  1446. Message.Result = 1;
  1447. }
  1448. else if (Message.Msg == WM_CHAR)
  1449. {
  1450. if (reinterpret_cast<TWMChar &>(Message).CharCode == L' ')
  1451. {
  1452. Clicked = true;
  1453. Message.Result = 1;
  1454. }
  1455. else
  1456. {
  1457. ControlWndProc(StaticText)(Message);
  1458. }
  1459. }
  1460. else if (Message.Msg == CM_DIALOGCHAR)
  1461. {
  1462. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1463. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1464. {
  1465. StaticText->SetFocus();
  1466. // in case the action takes long, make sure focus is shown immediately
  1467. UpdateWindow(StaticText->Handle);
  1468. Clicked = true;
  1469. Message.Result = 1;
  1470. }
  1471. else
  1472. {
  1473. ControlWndProc(StaticText)(Message);
  1474. }
  1475. }
  1476. else
  1477. {
  1478. ControlWndProc(StaticText)(Message);
  1479. }
  1480. if (Message.Msg == WM_PAINT)
  1481. {
  1482. TRect R;
  1483. TControlCanvas * Canvas;
  1484. FocusableLabelCanvas(StaticText, &Canvas, R);
  1485. try
  1486. {
  1487. if (StaticText->Focused())
  1488. {
  1489. Canvas->DrawFocusRect(R);
  1490. }
  1491. else if (!StaticText->Font->Style.Contains(fsUnderline))
  1492. {
  1493. Canvas->Pen->Style = psDot;
  1494. Canvas->Brush->Style = bsClear;
  1495. if (!StaticText->Enabled)
  1496. {
  1497. Canvas->Pen->Color = clBtnHighlight;
  1498. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1499. Canvas->LineTo(R.Right + 1, R.Bottom);
  1500. Canvas->Pen->Color = clGrayText;
  1501. }
  1502. Canvas->MoveTo(R.Left, R.Bottom - 1);
  1503. Canvas->LineTo(R.Right, R.Bottom - 1);
  1504. }
  1505. }
  1506. __finally
  1507. {
  1508. delete Canvas;
  1509. }
  1510. }
  1511. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1512. (Message.Msg == CM_ENABLEDCHANGED))
  1513. {
  1514. StaticText->Invalidate();
  1515. }
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. static THintWindow * PersistentHintWindow = NULL;
  1519. static TControl * PersistentHintControl = NULL;
  1520. //---------------------------------------------------------------------------
  1521. void __fastcall CancelPersistentHint()
  1522. {
  1523. if (PersistentHintWindow != NULL)
  1524. {
  1525. PersistentHintControl = NULL;
  1526. SAFE_DESTROY(PersistentHintWindow);
  1527. }
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1531. {
  1532. CancelPersistentHint();
  1533. THintInfo HintInfo;
  1534. HintInfo.HintControl = Control;
  1535. HintInfo.HintPos = HintPos;
  1536. HintInfo.HintMaxWidth = GetParentForm(Control)->Monitor->Width;
  1537. HintInfo.HintColor = Application->HintColor;
  1538. HintInfo.HintStr = GetShortHint(Control->Hint);
  1539. HintInfo.HintData = NULL;
  1540. bool CanShow = true;
  1541. if (Application->OnShowHint != NULL)
  1542. {
  1543. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1544. }
  1545. if (CanShow)
  1546. {
  1547. PersistentHintControl = Control;
  1548. PersistentHintWindow = new TScreenTipHintWindow(Application);
  1549. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1550. PersistentHintWindow->Color = HintInfo.HintColor;
  1551. TRect HintWinRect;
  1552. if (HintInfo.HintMaxWidth < Control->Width)
  1553. {
  1554. HintInfo.HintMaxWidth = Control->Width;
  1555. }
  1556. HintWinRect = PersistentHintWindow->CalcHintRect(
  1557. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1558. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1559. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1560. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1561. }
  1562. }
  1563. //---------------------------------------------------------------------------
  1564. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1565. {
  1566. bool Clicked = false;
  1567. bool Cancel = false;
  1568. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1569. if (Message.Msg == CM_HINTSHOW)
  1570. {
  1571. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1572. if (PersistentHintControl == StaticText)
  1573. {
  1574. // do not allow standard hint when persistent is already shown
  1575. HintShow.Result = 1;
  1576. }
  1577. else
  1578. {
  1579. HintShow.HintInfo->HideTimeout = 100000; // never
  1580. }
  1581. }
  1582. else if (Message.Msg == CN_KEYDOWN)
  1583. {
  1584. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1585. (PersistentHintControl == StaticText))
  1586. {
  1587. CancelPersistentHint();
  1588. StaticText->Invalidate();
  1589. Message.Result = 1;
  1590. }
  1591. else
  1592. {
  1593. FocusableLabelWindowProc(Data, Message, Clicked);
  1594. }
  1595. }
  1596. else
  1597. {
  1598. FocusableLabelWindowProc(Data, Message, Clicked);
  1599. }
  1600. if (Message.Msg == CM_CANCELMODE)
  1601. {
  1602. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  1603. if ((CancelMessage.Sender != StaticText) &&
  1604. (CancelMessage.Sender != PersistentHintWindow))
  1605. {
  1606. Cancel = true;
  1607. }
  1608. }
  1609. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  1610. {
  1611. Cancel = true;
  1612. }
  1613. if (Cancel && (PersistentHintControl == StaticText))
  1614. {
  1615. CancelPersistentHint();
  1616. }
  1617. if (Clicked && (PersistentHintControl != StaticText))
  1618. {
  1619. TRect R;
  1620. TPoint HintPos;
  1621. FocusableLabelCanvas(StaticText, NULL, R);
  1622. HintPos.y = R.Bottom - R.Top;
  1623. HintPos.x = R.Left;
  1624. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  1625. }
  1626. }
  1627. //---------------------------------------------------------------------------
  1628. void __fastcall HintLabel(TStaticText * StaticText, UnicodeString Hint)
  1629. {
  1630. // Currently all are right-justified, when other alignment is used,
  1631. // test respective branches in FocusableLabelCanvas.
  1632. DebugAssert(StaticText->Alignment == taRightJustify);
  1633. // With right-justify, it has to be off. We may not notice on original
  1634. // English version, results will differ with translations only
  1635. DebugAssert(!StaticText->AutoSize);
  1636. StaticText->ParentFont = true;
  1637. if (!Hint.IsEmpty())
  1638. {
  1639. StaticText->Hint = Hint;
  1640. }
  1641. StaticText->ShowHint = true;
  1642. StaticText->Cursor = crHandPoint;
  1643. TWndMethod WindowProc;
  1644. ((TMethod*)&WindowProc)->Data = StaticText;
  1645. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  1646. StaticText->WindowProc = WindowProc;
  1647. }
  1648. //---------------------------------------------------------------------------
  1649. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  1650. {
  1651. // it is TCustomComboxBox, but the properties are published only by TComboBox
  1652. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  1653. if (Message.Msg == WM_SIZE)
  1654. {
  1655. UnicodeString Text = ComboBox->Text;
  1656. try
  1657. {
  1658. ControlWndProc(ComboBox)(Message);
  1659. }
  1660. __finally
  1661. {
  1662. // workaround for bug in combo box, that causes it to change text to any
  1663. // item from drop down list which starts with current text,
  1664. // after control is resized (unless the text is in drop down list as well)
  1665. ComboBox->Text = Text;
  1666. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  1667. if (!ComboBox->Focused())
  1668. {
  1669. ComboBox->SelLength = 0;
  1670. }
  1671. }
  1672. }
  1673. else
  1674. {
  1675. ControlWndProc(ComboBox)(Message);
  1676. }
  1677. }
  1678. //---------------------------------------------------------------------------
  1679. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  1680. {
  1681. TWndMethod WindowProc;
  1682. ((TMethod*)&WindowProc)->Data = ComboBox;
  1683. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  1684. ComboBox->WindowProc = WindowProc;
  1685. }
  1686. //---------------------------------------------------------------------------
  1687. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  1688. {
  1689. if (StaticText->OnClick != NULL)
  1690. {
  1691. StaticText->OnClick(StaticText);
  1692. }
  1693. else
  1694. {
  1695. OpenBrowser(StaticText->Caption);
  1696. }
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  1700. {
  1701. bool Clicked = false;
  1702. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1703. if (Message.Msg == WM_CONTEXTMENU)
  1704. {
  1705. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  1706. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  1707. {
  1708. TRect R;
  1709. FocusableLabelCanvas(StaticText, NULL, R);
  1710. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  1711. ContextMenu.Pos.x = static_cast<short>(P.x);
  1712. ContextMenu.Pos.y = static_cast<short>(P.y);
  1713. }
  1714. }
  1715. else if (Message.Msg == WM_KEYDOWN)
  1716. {
  1717. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  1718. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == L'C'))
  1719. {
  1720. TInstantOperationVisualizer Visualizer;
  1721. CopyToClipboard(StaticText->Caption);
  1722. Message.Result = 1;
  1723. }
  1724. else
  1725. {
  1726. FocusableLabelWindowProc(Data, Message, Clicked);
  1727. }
  1728. }
  1729. FocusableLabelWindowProc(Data, Message, Clicked);
  1730. if (Message.Msg == WM_DESTROY)
  1731. {
  1732. delete StaticText->PopupMenu;
  1733. DebugAssert(StaticText->PopupMenu == NULL);
  1734. }
  1735. if (Clicked)
  1736. {
  1737. LinkLabelClick(StaticText);
  1738. }
  1739. }
  1740. //---------------------------------------------------------------------------
  1741. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  1742. {
  1743. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1744. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  1745. DebugAssert(MenuItem != NULL);
  1746. if (MenuItem->Tag == 0)
  1747. {
  1748. LinkLabelClick(StaticText);
  1749. }
  1750. else
  1751. {
  1752. TInstantOperationVisualizer Visualizer;
  1753. CopyToClipboard(StaticText->Caption);
  1754. }
  1755. }
  1756. //---------------------------------------------------------------------------
  1757. static void __fastcall DoLinkLabel(TStaticText * StaticText)
  1758. {
  1759. StaticText->Transparent = false;
  1760. StaticText->ParentFont = true;
  1761. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  1762. StaticText->Cursor = crHandPoint;
  1763. TWndMethod WindowProc;
  1764. ((TMethod*)&WindowProc)->Data = StaticText;
  1765. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  1766. StaticText->WindowProc = WindowProc;
  1767. }
  1768. //---------------------------------------------------------------------------
  1769. void __fastcall LinkLabel(TStaticText * StaticText, UnicodeString Url,
  1770. TNotifyEvent OnEnter)
  1771. {
  1772. DoLinkLabel(StaticText);
  1773. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  1774. if (!Url.IsEmpty())
  1775. {
  1776. StaticText->Caption = Url;
  1777. }
  1778. bool IsUrl = IsHttpOrHttpsUrl(StaticText->Caption);
  1779. if (IsUrl)
  1780. {
  1781. DebugAssert(StaticText->PopupMenu == NULL);
  1782. StaticText->PopupMenu = new TPopupMenu(StaticText);
  1783. try
  1784. {
  1785. TNotifyEvent ContextMenuOnClick;
  1786. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  1787. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  1788. TMenuItem * Item;
  1789. Item = new TMenuItem(StaticText->PopupMenu);
  1790. Item->Caption = LoadStr(URL_LINK_OPEN);
  1791. Item->Tag = 0;
  1792. Item->ShortCut = ShortCut(L' ', TShiftState());
  1793. Item->OnClick = ContextMenuOnClick;
  1794. StaticText->PopupMenu->Items->Add(Item);
  1795. Item = new TMenuItem(StaticText->PopupMenu);
  1796. Item->Caption = LoadStr(URL_LINK_COPY);
  1797. Item->Tag = 1;
  1798. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  1799. Item->OnClick = ContextMenuOnClick;
  1800. StaticText->PopupMenu->Items->Add(Item);
  1801. }
  1802. catch(...)
  1803. {
  1804. delete StaticText->PopupMenu;
  1805. DebugAssert(StaticText->PopupMenu == NULL);
  1806. throw;
  1807. }
  1808. }
  1809. StaticText->Font->Color = LinkColor;
  1810. }
  1811. //---------------------------------------------------------------------------
  1812. void __fastcall LinkAppLabel(TStaticText * StaticText)
  1813. {
  1814. DoLinkLabel(StaticText);
  1815. }
  1816. //---------------------------------------------------------------------------
  1817. static void __fastcall HotTrackLabelMouseEnter(void * /*Data*/, TObject * Sender)
  1818. {
  1819. reinterpret_cast<TLabel *>(Sender)->Font->Color = clBlue;
  1820. }
  1821. //---------------------------------------------------------------------------
  1822. static void __fastcall HotTrackLabelMouseLeave(void * /*Data*/, TObject * Sender)
  1823. {
  1824. reinterpret_cast<TLabel *>(Sender)->ParentFont = true;
  1825. }
  1826. //---------------------------------------------------------------------------
  1827. void __fastcall HotTrackLabel(TLabel * Label)
  1828. {
  1829. DebugAssert(Label->OnMouseEnter == NULL);
  1830. DebugAssert(Label->OnMouseLeave == NULL);
  1831. Label->OnMouseEnter = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseEnter);
  1832. Label->OnMouseLeave = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseLeave);
  1833. }
  1834. //---------------------------------------------------------------------------
  1835. void __fastcall SetLabelHintPopup(TLabel * Label, const UnicodeString & Hint)
  1836. {
  1837. Label->Caption = Hint;
  1838. Label->Hint = Hint;
  1839. TRect Rect(0, 0, Label->Width, 0);
  1840. TScreenTipHintWindow::CalcHintTextRect(Label, Label->Canvas, Rect, Label->Caption);
  1841. Label->ShowHint = (Rect.Bottom > Label->Height);
  1842. }
  1843. //---------------------------------------------------------------------------
  1844. bool __fastcall HasLabelHintPopup(TLabel * Label, const UnicodeString & HintStr)
  1845. {
  1846. return (Label->Caption == HintStr);
  1847. }
  1848. //---------------------------------------------------------------------------
  1849. Forms::TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  1850. {
  1851. Forms::TMonitor * Result;
  1852. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  1853. {
  1854. Result = Application->MainForm->Monitor;
  1855. }
  1856. else if (LastMonitor != NULL)
  1857. {
  1858. Result = LastMonitor;
  1859. }
  1860. else
  1861. {
  1862. int i = 0;
  1863. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  1864. {
  1865. i++;
  1866. }
  1867. DebugAssert(Screen->Monitors[i]->Primary);
  1868. Result = Screen->Monitors[i];
  1869. }
  1870. return Result;
  1871. }
  1872. //---------------------------------------------------------------------------
  1873. int __fastcall GetLastMonitor()
  1874. {
  1875. if (LastMonitor != NULL)
  1876. {
  1877. return LastMonitor->MonitorNum;
  1878. }
  1879. else
  1880. {
  1881. return -1;
  1882. }
  1883. }
  1884. //---------------------------------------------------------------------------
  1885. void __fastcall SetLastMonitor(int MonitorNum)
  1886. {
  1887. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  1888. {
  1889. LastMonitor = Screen->Monitors[MonitorNum];
  1890. }
  1891. else
  1892. {
  1893. LastMonitor = NULL;
  1894. }
  1895. }
  1896. //---------------------------------------------------------------------------
  1897. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  1898. {
  1899. TForm * Form;
  1900. if (Owner == NULL)
  1901. {
  1902. Owner = GetFormOwner();
  1903. }
  1904. // If there is no main form yet, make this one main.
  1905. // This:
  1906. // - Makes other forms (dialogs invoked from this one),
  1907. // be placed on the same monitor (otherwise all new forms get placed
  1908. // on primary monitor)
  1909. // - Triggers MainForm-specific code in DoFormWindowProc.
  1910. // - Shows button on taskbar
  1911. if (Application->MainForm == NULL)
  1912. {
  1913. Application->CreateForm(FormClass, &Form);
  1914. DebugAssert(Application->MainForm == Form);
  1915. }
  1916. else
  1917. {
  1918. Form = dynamic_cast<TForm *>(Construct(FormClass, Owner));
  1919. DebugAssert(Form != NULL);
  1920. }
  1921. return Form;
  1922. }
  1923. //---------------------------------------------------------------------------
  1924. TImageList * __fastcall SharedSystemImageList(bool Large)
  1925. {
  1926. TSHFileInfo FileInfo;
  1927. TImageList * Result = new TImageList(Application);
  1928. int ImageListHandle = SHGetFileInfo(L"", 0, &FileInfo, sizeof(FileInfo),
  1929. SHGFI_SYSICONINDEX | (Large ? SHGFI_LARGEICON : SHGFI_SMALLICON));
  1930. if (ImageListHandle != 0)
  1931. {
  1932. Result->ShareImages = true;
  1933. Result->Handle = ImageListHandle;
  1934. }
  1935. return Result;
  1936. }
  1937. //---------------------------------------------------------------------------
  1938. bool __fastcall SupportsSplitButton()
  1939. {
  1940. return (Win32MajorVersion >= 6);
  1941. }
  1942. //---------------------------------------------------------------------------
  1943. static TButton * __fastcall FindDefaultButton(TWinControl * Control)
  1944. {
  1945. TButton * Result = NULL;
  1946. int Index = 0;
  1947. while ((Result == NULL) && (Index < Control->ControlCount))
  1948. {
  1949. TControl * ChildControl = Control->Controls[Index];
  1950. TButton * Button = dynamic_cast<TButton *>(ChildControl);
  1951. if ((Button != NULL) && Button->Default)
  1952. {
  1953. Result = Button;
  1954. }
  1955. else
  1956. {
  1957. TWinControl * WinControl = dynamic_cast<TWinControl *>(ChildControl);
  1958. if (WinControl != NULL)
  1959. {
  1960. Result = FindDefaultButton(WinControl);
  1961. }
  1962. }
  1963. Index++;
  1964. }
  1965. return Result;
  1966. }
  1967. //---------------------------------------------------------------------------
  1968. TModalResult __fastcall DefaultResult(TCustomForm * Form, TButton * DefaultButton)
  1969. {
  1970. // The point of this is to avoid hardcoding mrOk when checking dialog results.
  1971. // Previously we used != mrCancel instead, as mrCancel is more reliable,
  1972. // being automatically used for Esc/X buttons (and hence kind of forced to be used
  1973. // for Cancel buttons). But that failed to be reliable in the end, for
  1974. // ModalResult being mrNone, when Windows session is being logged off.
  1975. // We interpreted mrNone as OK, causing lots of troubles.
  1976. TModalResult Result = mrNone;
  1977. TButton * Button = FindDefaultButton(Form);
  1978. if (DebugAlwaysTrue(Button != NULL))
  1979. {
  1980. Result = Button->ModalResult;
  1981. }
  1982. if (Result == mrNone)
  1983. {
  1984. DebugAssert((DefaultButton != NULL) && (DefaultButton->ModalResult != mrNone));
  1985. Result = DefaultButton->ModalResult;
  1986. }
  1987. else
  1988. {
  1989. // If default button fallback was provided,
  1990. // make sure it is the default button we actually detected
  1991. DebugAssert((DefaultButton == NULL) || (Button == DefaultButton));
  1992. }
  1993. return Result;
  1994. }
  1995. //---------------------------------------------------------------------------
  1996. void __fastcall DefaultButton(TButton * Button, bool Default)
  1997. {
  1998. // default property setter does not have guard for "the same value"
  1999. if (Button->Default != Default)
  2000. {
  2001. Button->Default = Default;
  2002. }
  2003. }
  2004. //---------------------------------------------------------------------------
  2005. void __fastcall MemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  2006. {
  2007. // Sender can be Form or Memo itself
  2008. TControl * Control = dynamic_cast<TControl *>(Sender);
  2009. if (DebugAlwaysTrue(Control != NULL))
  2010. {
  2011. TCustomForm * Form = GetParentForm(Control);
  2012. // Particularly when WantReturns is true,
  2013. // memo swallows also Esc, so we have to handle it ourselves.
  2014. // See also ReadOnlyControl.
  2015. if ((Key == VK_ESCAPE) && Shift.Empty())
  2016. {
  2017. Form->ModalResult = mrCancel;
  2018. Key = 0;
  2019. }
  2020. else if ((Key == VK_RETURN) && Shift.Contains(ssCtrl))
  2021. {
  2022. Form->ModalResult = DefaultResult(Form);
  2023. Key = 0;
  2024. }
  2025. }
  2026. }
  2027. //---------------------------------------------------------------------------
  2028. class TIconOwnerComponent : public TComponent
  2029. {
  2030. public:
  2031. __fastcall TIconOwnerComponent(TIcon * Icon) :
  2032. TComponent(NULL),
  2033. FIcon(Icon)
  2034. {
  2035. }
  2036. private:
  2037. std::unique_ptr<TIcon> FIcon;
  2038. };
  2039. //---------------------------------------------------------------------------
  2040. static void __fastcall FixFormIcon(TForm * Form, int Size, int WidthMetric, int HeightMetric)
  2041. {
  2042. // Whole this code is to call ReadIcon from Vcl.Graphics.pas with correct size
  2043. // Clone the icon data (whole .ico file content, that is originally loaded from .dfm)
  2044. // to a new TIcon that does not have a size fixed yet (size cannot be changed after handle is allocated)
  2045. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  2046. Form->Icon->SaveToStream(Stream.get());
  2047. std::unique_ptr<TIcon> Icon(new TIcon());
  2048. Stream->Position = 0;
  2049. Icon->LoadFromStream(Stream.get());
  2050. // Set desired size
  2051. Icon->SetSize(GetSystemMetrics(WidthMetric), GetSystemMetrics(HeightMetric));
  2052. // This calls TIcon::RequireHandle that retrieves the best icon for given size
  2053. LPARAM LParam = reinterpret_cast<LPARAM>(Icon->Handle);
  2054. SendMessage(Form->Handle, WM_SETICON, Size, LParam);
  2055. // Make sure the icon is released
  2056. TIconOwnerComponent * IconOwnerComponent = new TIconOwnerComponent(Icon.release());
  2057. IconOwnerComponent->Name = TIconOwnerComponent::QualifiedClassName() + IntToStr(Size);
  2058. Form->InsertComponent(IconOwnerComponent);
  2059. }
  2060. //---------------------------------------------------------------------------
  2061. void __fastcall FixFormIcons(TForm * Form)
  2062. {
  2063. // VCL sets only ICON_BIG (so small icon is scaled down by OS from big icon),
  2064. // and it uses a random (first?) size from the resource,
  2065. // not the best size.
  2066. FixFormIcon(Form, ICON_SMALL, SM_CXSMICON, SM_CYSMICON);
  2067. FixFormIcon(Form, ICON_BIG, SM_CXICON, SM_CYICON);
  2068. // We rely on VCL not calling WM_SETICON ever after
  2069. // (what it would do, if CreateWnd is called again).
  2070. // That would overwrite the ICON_BIG.
  2071. // We might be able to make sure it uses a correct size by calling
  2072. // TIcon.ReleaseHandle and setting a correct size.
  2073. }
  2074. //---------------------------------------------------------------------------
  2075. void __fastcall UseDesktopFont(TControl * Control)
  2076. {
  2077. TCustomStatusBar * StatusBar = dynamic_cast<TCustomStatusBar *>(Control);
  2078. if (StatusBar != NULL)
  2079. {
  2080. // otherwise setting DesktopFont below has no effect
  2081. StatusBar->UseSystemFont = false;
  2082. }
  2083. class TPublicControl : public TControl
  2084. {
  2085. public:
  2086. __property DesktopFont;
  2087. };
  2088. reinterpret_cast<TPublicControl *>(Control)->DesktopFont = true;
  2089. }
  2090. //---------------------------------------------------------------------------
  2091. TShiftState __fastcall AllKeyShiftStates()
  2092. {
  2093. return TShiftState() << ssShift << ssAlt << ssCtrl;
  2094. }
  2095. //---------------------------------------------------------------------------
  2096. static bool __fastcall FormActivationHook(void * Data, TMessage & Message)
  2097. {
  2098. bool Result = false;
  2099. // Some dialogs, when application is restored from minimization,
  2100. // do not get activated. So we do it explicitly here.
  2101. // We cannot do this from TApplication::OnActivate because
  2102. // TApplication.WndProc resets focus to the last active window afterwards.
  2103. // So we override CM_ACTIVATE implementation here completelly.
  2104. if (Message.Msg == CM_ACTIVATE)
  2105. {
  2106. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  2107. if (Screen->FocusedForm == Form)
  2108. {
  2109. ::SetFocus(Form->Handle);
  2110. // VCLCOPY
  2111. if (Application->OnActivate != NULL)
  2112. {
  2113. Application->OnActivate(Application);
  2114. }
  2115. Result = true;
  2116. }
  2117. }
  2118. return Result;
  2119. }
  2120. //---------------------------------------------------------------------------
  2121. void __fastcall HookFormActivation(TCustomForm * Form)
  2122. {
  2123. Application->HookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2124. }
  2125. //---------------------------------------------------------------------------
  2126. void __fastcall UnhookFormActivation(TCustomForm * Form)
  2127. {
  2128. Application->UnhookMainWindow(MakeMethod<TWindowHook>(Form, FormActivationHook));
  2129. }
  2130. //---------------------------------------------------------------------------
  2131. void __fastcall ShowFormNoActivate(TForm * Form)
  2132. {
  2133. TPublicForm * PublicForm = static_cast<TPublicForm *>(Form);
  2134. // This is same as SendToBack, except for added SWP_NOACTIVATE (VCLCOPY)
  2135. SetWindowPos(PublicForm->WindowHandle, HWND_BOTTOM, 0, 0, 0, 0,
  2136. SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  2137. // This replaces TCustomForm::CMShowingChanged()
  2138. // which calls ShowWindow(Handle, SW_SHOWNORMAL).
  2139. ShowWindow(Form->Handle, SW_SHOWNOACTIVATE);
  2140. // - so we have to call DoShow explicitly.
  2141. PublicForm->DoShow();
  2142. // - also we skip applying TForm::Position (VCLCOPY)
  2143. if (DebugAlwaysTrue(Form->Position == poOwnerFormCenter))
  2144. {
  2145. TCustomForm * CenterForm = Application->MainForm;
  2146. TCustomForm * OwnerForm = dynamic_cast<TCustomForm *>(Form->Owner);
  2147. if (OwnerForm != NULL)
  2148. {
  2149. CenterForm = OwnerForm;
  2150. }
  2151. int X, Y;
  2152. if ((CenterForm != NULL) && (CenterForm != Form))
  2153. {
  2154. TRect Bounds = CenterForm->BoundsRect;
  2155. X = ((Bounds.Width() - Form->Width) / 2) + CenterForm->Left;
  2156. Y = ((Bounds.Height() - Form->Height) / 2) + CenterForm->Top;
  2157. }
  2158. else
  2159. {
  2160. X = (Screen->Width - Form->Width) / 2;
  2161. Y = (Screen->Height - Form->Height) / 2;
  2162. }
  2163. if (X < Screen->DesktopLeft)
  2164. {
  2165. X = Screen->DesktopLeft;
  2166. }
  2167. if (Y < Screen->DesktopTop)
  2168. {
  2169. Y = Screen->DesktopTop;
  2170. }
  2171. Form->SetBounds(X, Y, Form->Width, Form->Height);
  2172. // We cannot call SetWindowToMonitor().
  2173. // We cannot set FPosition = poDesigned, so workarea-checking code
  2174. // in DoFormWindowProc is not triggered
  2175. // If application is restored, dialog is not activated, do it manually.
  2176. // Wait for application to be activated to activate ourself.
  2177. HookFormActivation(Form);
  2178. }
  2179. }
  2180. //---------------------------------------------------------------------------
  2181. TPanel * __fastcall CreateBlankPanel(TComponent * Owner)
  2182. {
  2183. TPanel * Panel = new TPanel(Owner);
  2184. Panel->BevelOuter = bvNone;
  2185. Panel->BevelInner = bvNone; // default
  2186. Panel->BevelKind = bkNone;
  2187. return Panel;
  2188. }