VCLCommon.cpp 73 KB

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