VCLCommon.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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. //---------------------------------------------------------------------------
  19. #pragma package(smart_init)
  20. //---------------------------------------------------------------------------
  21. void __fastcall AdjustListColumnsWidth(TListView * ListView)
  22. {
  23. int OriginalWidth, NewWidth, i, CWidth, LastResizible;
  24. OriginalWidth = 0;
  25. LastResizible = -1;
  26. for (i = 0; i < ListView->Columns->Count; i++)
  27. {
  28. OriginalWidth += ListView->Columns->Items[i]->Width;
  29. if (ListView->Columns->Items[i]->Tag == 0)
  30. {
  31. LastResizible = i;
  32. }
  33. }
  34. assert(LastResizible >= 0);
  35. int RowCount = ListView->Items->Count;
  36. NewWidth = 0;
  37. CWidth = ListView->ClientWidth;
  38. if ((ListView->VisibleRowCount < RowCount) &&
  39. (ListView->Width - ListView->ClientWidth < GetSystemMetrics(SM_CXVSCROLL)))
  40. {
  41. CWidth -= GetSystemMetrics(SM_CXVSCROLL);
  42. }
  43. for (i = 0; i < ListView->Columns->Count; i++)
  44. {
  45. if (i != LastResizible)
  46. {
  47. if (ListView->Columns->Items[i]->Tag == 0)
  48. {
  49. ListView->Columns->Items[i]->Width =
  50. (CWidth * ListView->Columns->Items[i]->Width) / OriginalWidth;
  51. }
  52. NewWidth += ListView->Columns->Items[i]->Width;
  53. }
  54. }
  55. ListView->Columns->Items[LastResizible]->Width = CWidth-NewWidth;
  56. }
  57. //---------------------------------------------------------------------------
  58. static void __fastcall SetParentColor(TControl * Control)
  59. {
  60. TColor Color = clBtnFace;
  61. ((TEdit*)Control)->Color = Color;
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall EnableControl(TControl * Control, bool Enable)
  65. {
  66. if (Control->Enabled != Enable)
  67. {
  68. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  69. if ((WinControl != NULL) &&
  70. (WinControl->ControlCount > 0))
  71. {
  72. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  73. {
  74. EnableControl(WinControl->Controls[Index], Enable);
  75. }
  76. }
  77. Control->Enabled = Enable;
  78. }
  79. if ((dynamic_cast<TCustomEdit *>(Control) != NULL) ||
  80. (dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  81. (dynamic_cast<TCustomListView *>(Control) != NULL) ||
  82. (dynamic_cast<TTreeView *>(Control) != NULL))
  83. {
  84. if (Enable)
  85. {
  86. ((TEdit*)Control)->Color = clWindow;
  87. }
  88. else
  89. {
  90. ((TEdit*)Control)->Color = clBtnFace;
  91. }
  92. }
  93. };
  94. //---------------------------------------------------------------------------
  95. void __fastcall ReadOnlyControl(TControl * Control, bool ReadOnly)
  96. {
  97. if (dynamic_cast<TCustomEdit *>(Control) != NULL)
  98. {
  99. ((TEdit*)Control)->ReadOnly = ReadOnly;
  100. TMemo * Memo = dynamic_cast<TMemo *>(Control);
  101. if (ReadOnly)
  102. {
  103. SetParentColor(Control);
  104. if (Memo != NULL)
  105. {
  106. // Is true by default and makes the control swallow not only
  107. // returns but also escapes.
  108. // See also MemoKeyDown
  109. Memo->WantReturns = false;
  110. }
  111. }
  112. else
  113. {
  114. ((TEdit*)Control)->Color = clWindow;
  115. // not supported atm, we need to persist previous value of WantReturns
  116. assert(Memo == NULL);
  117. }
  118. }
  119. else if ((dynamic_cast<TCustomComboBox *>(Control) != NULL) ||
  120. (dynamic_cast<TCustomTreeView *>(Control) != NULL))
  121. {
  122. EnableControl(Control, !ReadOnly);
  123. }
  124. else
  125. {
  126. FAIL;
  127. }
  128. }
  129. //---------------------------------------------------------------------------
  130. static TForm * MainLikeForm = NULL;
  131. //---------------------------------------------------------------------------
  132. TForm * __fastcall GetMainForm()
  133. {
  134. TForm * Result;
  135. if (MainLikeForm != NULL)
  136. {
  137. Result = MainLikeForm;
  138. }
  139. else
  140. {
  141. Result = Application->MainForm;
  142. }
  143. return Result;
  144. }
  145. //---------------------------------------------------------------------------
  146. bool __fastcall IsMainFormHidden()
  147. {
  148. bool Result = (GetMainForm() != NULL) && !GetMainForm()->Visible;
  149. // we do not expect this to return true when MainLikeForm is set
  150. assert(!Result || (MainLikeForm == NULL));
  151. return Result;
  152. }
  153. //---------------------------------------------------------------------------
  154. bool __fastcall IsMainFormLike(TCustomForm * Form)
  155. {
  156. return
  157. (GetMainForm() == Form) ||
  158. // this particularly happens if error occurs while main
  159. // window is being shown (e.g. non existent local directory when opening
  160. // explorer)
  161. IsMainFormHidden();
  162. }
  163. //---------------------------------------------------------------------------
  164. UnicodeString __fastcall FormatMainFormCaption(const UnicodeString & Caption)
  165. {
  166. UnicodeString Result = Caption;
  167. if (Result.IsEmpty())
  168. {
  169. Result = AppName;
  170. }
  171. else
  172. {
  173. UnicodeString Suffix = L" - " + AppName;
  174. if (!EndsStr(Suffix, Result))
  175. {
  176. Result += Suffix;
  177. }
  178. }
  179. return Result;
  180. }
  181. //---------------------------------------------------------------------------
  182. UnicodeString __fastcall FormatFormCaption(TCustomForm * Form, const UnicodeString & Caption)
  183. {
  184. UnicodeString Result = Caption;
  185. if (IsMainFormLike(Form))
  186. {
  187. Result = FormatMainFormCaption(Result);
  188. }
  189. return Result;
  190. }
  191. //---------------------------------------------------------------------------
  192. struct TSavedSystemSettings
  193. {
  194. TCustomForm * Form;
  195. UnicodeString FontName;
  196. bool Flipped;
  197. TWndMethod OldWndProc;
  198. };
  199. //---------------------------------------------------------------------------
  200. class TPublicControl : public TWinControl
  201. {
  202. friend TWndMethod __fastcall ControlWndProc(TWinControl * Control);
  203. };
  204. //---------------------------------------------------------------------------
  205. TWndMethod __fastcall ControlWndProc(TWinControl * Control)
  206. {
  207. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  208. return &PublicControl->WndProc;
  209. }
  210. //---------------------------------------------------------------------------
  211. static Forms::TMonitor * LastMonitor = NULL;
  212. //---------------------------------------------------------------------------
  213. inline void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc,
  214. TMessage & Message)
  215. {
  216. if ((Message.Msg == WM_SYSCOMMAND) &&
  217. (Message.WParam == SC_CONTEXTHELP))
  218. {
  219. InvokeHelp(Form->ActiveControl);
  220. Message.Result = 1;
  221. }
  222. else if (Message.Msg == CM_SHOWINGCHANGED)
  223. {
  224. TForm * AForm = dynamic_cast<TForm *>(Form);
  225. assert(AForm != NULL);
  226. if (IsMainFormLike(Form))
  227. {
  228. if (Form->Showing)
  229. {
  230. if (Application->MainForm != Form)
  231. {
  232. MainLikeForm = AForm;
  233. // When main form is hidden, no taskbar button for it is shown and
  234. // this window does not become main window, so there won't be no taskbar
  235. // icon created automatically (by VCL). So we force it manually here.
  236. // This particularly happen for all windows/dialogs of command-line
  237. // operations (e.g. /synchronize) after CreateScpExplorer() happens.
  238. // Also CM_SHOWINGCHANGED happens twice, and the WS_EX_APPWINDOW flag
  239. // from the first call is not preserved, re-applying on the second call too.
  240. // TODO: What about minimize to tray?
  241. int Style = GetWindowLong(Form->Handle, GWL_EXSTYLE);
  242. if (FLAGCLEAR(Style, WS_EX_APPWINDOW))
  243. {
  244. Style |= WS_EX_APPWINDOW;
  245. SetWindowLong(Form->Handle, GWL_EXSTYLE, Style);
  246. }
  247. }
  248. if (Form->Perform(WM_MANAGES_CAPTION, 0, 0) == 0)
  249. {
  250. Form->Caption = FormatFormCaption(Form, Form->Caption);
  251. }
  252. SendMessage(Form->Handle, WM_SETICON, ICON_BIG, reinterpret_cast<long>(Application->Icon->Handle));
  253. }
  254. else
  255. {
  256. if (MainLikeForm == Form)
  257. {
  258. // Do not bother with hiding the WS_EX_APPWINDOW flag
  259. // as the window is closing anyway.
  260. MainLikeForm = NULL;
  261. }
  262. }
  263. }
  264. // Part of following code (but actually not all, TODO), has to happen
  265. // for all windows when VCL main window is hidden (particularly the last branch).
  266. // This is different from above brach, that should happen only for top-level visible window.
  267. if ((Application->MainForm == Form) ||
  268. // this particularly happens if error occurs while main
  269. // window is being shown (e.g. non existent local directory when opening
  270. // explorer)
  271. ((Application->MainForm != NULL) && !Application->MainForm->Visible))
  272. {
  273. if (!Form->Showing)
  274. {
  275. // when closing main form, remember its monitor,
  276. // so that the next form is shown on the same one
  277. LastMonitor = Form->Monitor;
  278. }
  279. else if ((LastMonitor != NULL) && (LastMonitor != Form->Monitor) &&
  280. Form->Showing)
  281. {
  282. // would actually always be poScreenCenter, see _SafeFormCreate
  283. if ((AForm->Position == poMainFormCenter) ||
  284. (AForm->Position == poOwnerFormCenter) ||
  285. (AForm->Position == poScreenCenter))
  286. {
  287. // this would typically be an authentication dialog,
  288. // but it may as well be an message box
  289. // taken from TCustomForm::SetWindowToMonitor
  290. AForm->SetBounds(LastMonitor->Left + ((LastMonitor->Width - AForm->Width) / 2),
  291. LastMonitor->Top + ((LastMonitor->Height - AForm->Height) / 2),
  292. AForm->Width, AForm->Height);
  293. AForm->Position = poDesigned;
  294. }
  295. else if ((AForm->Position != poDesigned) &&
  296. (AForm->Position != poDefaultPosOnly))
  297. {
  298. // we do not expect any other positioning
  299. FAIL;
  300. }
  301. }
  302. else
  303. {
  304. TForm * AForm = dynamic_cast<TForm *>(Form);
  305. assert(AForm != NULL);
  306. // otherwise it would not get centered
  307. if ((AForm->Position == poMainFormCenter) ||
  308. (AForm->Position == poOwnerFormCenter))
  309. {
  310. AForm->Position = poScreenCenter;
  311. }
  312. }
  313. }
  314. bool WasFormCenter =
  315. (AForm->Position == poMainFormCenter) ||
  316. (AForm->Position == poOwnerFormCenter);
  317. WndProc(Message);
  318. // Make sure dialogs are shown on-screen even if center of the main window
  319. // is off-screen. Occurs e.g. if you move the main window so that
  320. // only window title is visible above taksbar.
  321. if (Form->Showing && WasFormCenter && (AForm->Position == poDesigned))
  322. {
  323. TRect Rect;
  324. // Reading Form.Left/Form.Top instead here does not work, likely due to some
  325. // bug, when querying TProgressForm opened from TEditorForm (reloading remote file)
  326. GetWindowRect(Form->Handle, &Rect);
  327. int Left = Rect.Left;
  328. int Top = Rect.Top;
  329. TRect WorkArea = AForm->Monitor->WorkareaRect;
  330. if (Left + Rect.Width() > WorkArea.Right)
  331. {
  332. Left = WorkArea.Right - Rect.Width();
  333. }
  334. if (Left < WorkArea.Left)
  335. {
  336. Left = WorkArea.Left;
  337. }
  338. if (Top + Rect.Height() > WorkArea.Bottom)
  339. {
  340. Top = WorkArea.Bottom - Rect.Height();
  341. }
  342. if (Top < WorkArea.Top)
  343. {
  344. Top = WorkArea.Top;
  345. }
  346. if ((Left != Rect.Left) ||
  347. (Top != Rect.Top))
  348. {
  349. SetWindowPos(Form->Handle, 0, Left, Top, Rect.Width(), Rect.Height(),
  350. SWP_NOZORDER + SWP_NOACTIVATE);
  351. }
  352. }
  353. }
  354. else
  355. {
  356. WndProc(Message);
  357. }
  358. }
  359. //---------------------------------------------------------------------------
  360. static void __fastcall FormWindowProc(void * Data, TMessage & Message)
  361. {
  362. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  363. DoFormWindowProc(Form, ControlWndProc(Form), Message);
  364. }
  365. //---------------------------------------------------------------------------
  366. static void __fastcall FormWindowProcEx(void * Data, TMessage & Message)
  367. {
  368. TSavedSystemSettings * SSettings = static_cast<TSavedSystemSettings *>(Data);
  369. DoFormWindowProc(SSettings->Form, SSettings->OldWndProc, Message);
  370. }
  371. //---------------------------------------------------------------------------
  372. void __fastcall InitializeSystemSettings()
  373. {
  374. }
  375. //---------------------------------------------------------------------------
  376. void __fastcall FinalizeSystemSettings()
  377. {
  378. }
  379. //---------------------------------------------------------------------------
  380. #ifdef _DEBUG
  381. void __fastcall VerifyControl(TControl * Control)
  382. {
  383. // If at this time the control has allocated persistence data that are used
  384. // for delayed handle recreation, we are at potential risk, as the future
  385. // de-persistence may overwrite meanwhile changed data.
  386. // For instance it may happen with list view that DPI scaling gets lost.
  387. // This for example happens when the list view has both design time
  388. // ReadOnly = true and some items set. We cannot usually explicitly
  389. // check for the presence of items as while the listview does not have
  390. // a handle allocated, item count querying does not work
  391. // (see also a check below)
  392. assert(!ControlHasRecreationPersistenceData(Control));
  393. TCustomListView * ListView = dynamic_cast<TCustomListView *>(Control);
  394. if (ListView != NULL)
  395. {
  396. // As of now the HandleAllocated check is pointless as
  397. // ListView->Items->Count returns 0 when the handle is not allocated yet.
  398. // But we want to know if the implementation ever changes to allocate the handle
  399. // on the call. Because we do not want to allocate a handle here as
  400. // that would change the debug mode behavior from release behavior,
  401. // possibly hiding away some problems.
  402. assert(!ListView->HandleAllocated() || (ListView->Items->Count == 0));
  403. }
  404. }
  405. #endif
  406. //---------------------------------------------------------------------------
  407. void __fastcall ApplySystemSettingsOnControl(TControl * Control)
  408. {
  409. #ifdef _DEBUG
  410. VerifyControl(Control);
  411. #endif
  412. // WORKAROUND
  413. // VCL does not scale status par panels (while for instance it does
  414. // scale list view headers). Remove this if they ever "fix" this.
  415. // Check TCustomStatusBar.ChangeScale() for changes.
  416. // For TBX status bars, this is implemented in TTBXCustomStatusBar.ChangeScale
  417. TStatusBar * StatusBar = dynamic_cast<TStatusBar *>(Control);
  418. if (StatusBar != NULL)
  419. {
  420. for (int Index = 0; Index < StatusBar->Panels->Count; Index++)
  421. {
  422. TStatusPanel * Panel = StatusBar->Panels->Items[Index];
  423. Panel->Width = ScaleByTextHeight(StatusBar, Panel->Width);
  424. }
  425. }
  426. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  427. if (WinControl != NULL)
  428. {
  429. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  430. {
  431. ApplySystemSettingsOnControl(WinControl->Controls[Index]);
  432. }
  433. }
  434. }
  435. //---------------------------------------------------------------------------
  436. // Settings that must be set as soon as possible.
  437. void __fastcall UseSystemSettingsPre(TCustomForm * Control, void ** Settings)
  438. {
  439. LocalSystemSettings(Control);
  440. TWndMethod WindowProc;
  441. if (Settings)
  442. {
  443. TSavedSystemSettings * SSettings;
  444. SSettings = new TSavedSystemSettings();
  445. *Settings = static_cast<void*>(SSettings);
  446. SSettings->Form = Control;
  447. SSettings->FontName = Control->Font->Name;
  448. SSettings->OldWndProc = Control->WindowProc;
  449. ((TMethod*)&WindowProc)->Data = SSettings;
  450. ((TMethod*)&WindowProc)->Code = FormWindowProcEx;
  451. }
  452. else
  453. {
  454. ((TMethod*)&WindowProc)->Data = Control;
  455. ((TMethod*)&WindowProc)->Code = FormWindowProc;
  456. }
  457. Control->WindowProc = WindowProc;
  458. if (Control->HelpKeyword.IsEmpty())
  459. {
  460. // temporary help keyword to enable F1 key in all forms
  461. Control->HelpKeyword = L"start";
  462. }
  463. ApplySystemSettingsOnControl(Control);
  464. };
  465. //---------------------------------------------------------------------------
  466. // Settings that must be set only after whole form is constructed
  467. void __fastcall UseSystemSettingsPost(TCustomForm * Control, void * Settings)
  468. {
  469. bool Flip;
  470. UnicodeString FlipStr = LoadStr(FLIP_CHILDREN);
  471. Flip = !FlipStr.IsEmpty() && static_cast<bool>(StrToInt(FlipStr));
  472. if (Settings != NULL)
  473. {
  474. static_cast<TSavedSystemSettings*>(Settings)->Flipped = Flip;
  475. }
  476. if (Flip)
  477. {
  478. Control->FlipChildren(true);
  479. }
  480. ResetSystemSettings(Control);
  481. };
  482. //---------------------------------------------------------------------------
  483. void __fastcall UseSystemSettings(TCustomForm * Control, void ** Settings)
  484. {
  485. UseSystemSettingsPre(Control, Settings);
  486. UseSystemSettingsPost(Control, (Settings != NULL) ? *Settings : NULL);
  487. };
  488. //---------------------------------------------------------------------------
  489. void __fastcall ResetSystemSettings(TCustomForm * /*Control*/)
  490. {
  491. // noop
  492. }
  493. //---------------------------------------------------------------------------
  494. void __fastcall DeleteSystemSettings(TCustomForm * Control, void * Settings)
  495. {
  496. assert(Settings);
  497. TSavedSystemSettings * SSettings = static_cast<TSavedSystemSettings *>(Settings);
  498. Control->WindowProc = SSettings->OldWndProc;
  499. delete SSettings;
  500. }
  501. //---------------------------------------------------------------------------
  502. void __fastcall RevokeSystemSettings(TCustomForm * Control, void * Settings)
  503. {
  504. assert(Settings);
  505. TSavedSystemSettings* SSettings = static_cast<TSavedSystemSettings*>(Settings);
  506. if (SSettings->Flipped)
  507. {
  508. Control->FlipChildren(true);
  509. }
  510. DeleteSystemSettings(Control, Settings);
  511. };
  512. //---------------------------------------------------------------------------
  513. class TPublicForm : public TForm
  514. {
  515. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage);
  516. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  517. };
  518. //---------------------------------------------------------------------------
  519. struct TShowAsModalStorage
  520. {
  521. void * FocusWindowList;
  522. HWND FocusActiveWindow;
  523. TFocusState FocusState;
  524. };
  525. //---------------------------------------------------------------------------
  526. void __fastcall ShowAsModal(TForm * Form, void *& Storage)
  527. {
  528. SetCorrectFormParent(Form);
  529. CancelDrag();
  530. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  531. ReleaseCapture();
  532. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  533. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  534. AStorage->FocusActiveWindow = GetActiveWindow();
  535. AStorage->FocusState = SaveFocusState();
  536. Screen->SaveFocusedList->Insert(0, Screen->FocusedForm);
  537. Screen->FocusedForm = Form;
  538. AStorage->FocusWindowList = DisableTaskWindows(0);
  539. Form->Show();
  540. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  541. Storage = AStorage;
  542. }
  543. //---------------------------------------------------------------------------
  544. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  545. {
  546. assert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  547. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  548. Storage = NULL;
  549. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  550. if (GetActiveWindow() != Form->Handle)
  551. {
  552. AStorage->FocusActiveWindow = 0;
  553. }
  554. Form->Hide();
  555. EnableTaskWindows(AStorage->FocusWindowList);
  556. if (Screen->SaveFocusedList->Count > 0)
  557. {
  558. Screen->FocusedForm = static_cast<TCustomForm *>(Screen->SaveFocusedList->First());
  559. Screen->SaveFocusedList->Remove(Screen->FocusedForm);
  560. }
  561. else
  562. {
  563. Screen->FocusedForm = NULL;
  564. }
  565. if (AStorage->FocusActiveWindow != 0)
  566. {
  567. SetActiveWindow(AStorage->FocusActiveWindow);
  568. }
  569. RestoreFocusState(AStorage->FocusState);
  570. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  571. delete AStorage;
  572. }
  573. //---------------------------------------------------------------------------
  574. void __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  575. {
  576. if (Storage != NULL)
  577. {
  578. HideAsModal(Form, Storage);
  579. }
  580. }
  581. //---------------------------------------------------------------------------
  582. bool __fastcall SelectDirectory(UnicodeString & Path, const UnicodeString Prompt,
  583. bool PreserveFileName)
  584. {
  585. bool Result;
  586. unsigned int ErrorMode;
  587. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  588. try
  589. {
  590. UnicodeString Directory;
  591. UnicodeString FileName;
  592. if (!PreserveFileName || DirectoryExists(::ApiPath(Path)))
  593. {
  594. Directory = Path;
  595. }
  596. else
  597. {
  598. Directory = ExtractFilePath(Path);
  599. FileName = ExtractFileName(Path);
  600. }
  601. Result = SelectDirectory(Prompt, L"", Directory);
  602. if (Result)
  603. {
  604. Path = Directory;
  605. if (!FileName.IsEmpty())
  606. {
  607. Path = IncludeTrailingBackslash(Path) + FileName;
  608. }
  609. }
  610. }
  611. __finally
  612. {
  613. SetErrorMode(ErrorMode);
  614. }
  615. return Result;
  616. }
  617. //---------------------------------------------------------------------------
  618. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  619. {
  620. bool AnyChecked = false;
  621. for (int Index = 0; Index < ListView->Items->Count; Index++)
  622. {
  623. if (ListView->Items->Item[Index]->Checked == Checked)
  624. {
  625. AnyChecked = true;
  626. break;
  627. }
  628. }
  629. return AnyChecked;
  630. }
  631. //---------------------------------------------------------------------------
  632. void __fastcall ListViewCheckAll(TListView * ListView,
  633. TListViewCheckAll CheckAll)
  634. {
  635. bool Check;
  636. if (CheckAll == caToggle)
  637. {
  638. Check = ListViewAnyChecked(ListView, false);
  639. }
  640. else
  641. {
  642. Check = (CheckAll == caCheck);
  643. }
  644. for (int Index = 0; Index < ListView->Items->Count; Index++)
  645. {
  646. ListView->Items->Item[Index]->Checked = Check;
  647. }
  648. }
  649. //---------------------------------------------------------------------------
  650. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  651. {
  652. int PrevIndex = ComboBox->ItemIndex;
  653. ComboBox->Items->BeginUpdate();
  654. try
  655. {
  656. ComboBox->Clear();
  657. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  658. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  659. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  660. }
  661. __finally
  662. {
  663. ComboBox->Items->EndUpdate();
  664. }
  665. assert(PrevIndex < ComboBox->Items->Count);
  666. ComboBox->ItemIndex = PrevIndex;
  667. }
  668. //---------------------------------------------------------------------------
  669. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  670. {
  671. ComboBox->ItemIndex = 2 - Value;
  672. if (ComboBox->ItemIndex < 0)
  673. {
  674. ComboBox->ItemIndex = 0;
  675. }
  676. }
  677. //---------------------------------------------------------------------------
  678. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  679. {
  680. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  681. }
  682. //---------------------------------------------------------------------------
  683. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  684. {
  685. switch (Value)
  686. {
  687. case asOn:
  688. CheckBox->State = cbChecked;
  689. break;
  690. case asOff:
  691. CheckBox->State = cbUnchecked;
  692. break;
  693. default:
  694. CheckBox->State = cbGrayed;
  695. break;
  696. }
  697. }
  698. //---------------------------------------------------------------------------
  699. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  700. {
  701. switch (CheckBox->State)
  702. {
  703. case cbChecked:
  704. return asOn;
  705. case cbUnchecked:
  706. return asOff;
  707. default:
  708. return asAuto;
  709. }
  710. }
  711. //---------------------------------------------------------------------------
  712. static const wchar_t PathWordDelimiters[] = L"\\/ ;,.";
  713. //---------------------------------------------------------------------------
  714. static bool IsPathWordDelimiter(wchar_t Ch)
  715. {
  716. return (wcschr(PathWordDelimiters, Ch) != NULL);
  717. }
  718. //---------------------------------------------------------------------------
  719. // Windows algorithm is as follows (tested on W2k):
  720. // right:
  721. // is_delimiter(current)
  722. // false:
  723. // right(left(current) + 1)
  724. // true:
  725. // right(right(current) + 1)
  726. // left:
  727. // right(left(current) + 1)
  728. int CALLBACK PathWordBreakProc(wchar_t * Ch, int Current, int Len, int Code)
  729. {
  730. int Result;
  731. UnicodeString ACh(Ch, Len);
  732. if (Code == WB_ISDELIMITER)
  733. {
  734. // we return negacy of what WinAPI docs says
  735. Result = !IsPathWordDelimiter(ACh[Current + 1]);
  736. }
  737. else if (Code == WB_LEFT)
  738. {
  739. // skip consecutive delimiters
  740. while ((Current > 0) &&
  741. IsPathWordDelimiter(ACh[Current]))
  742. {
  743. Current--;
  744. }
  745. Result = ACh.SubString(1, Current - 1).LastDelimiter(PathWordDelimiters);
  746. }
  747. else if (Code == WB_RIGHT)
  748. {
  749. if (Current == 0)
  750. {
  751. // will be called again with Current == 1
  752. Result = 0;
  753. }
  754. else
  755. {
  756. const wchar_t * P = wcspbrk(ACh.c_str() + Current - 1, PathWordDelimiters);
  757. if (P == NULL)
  758. {
  759. Result = Len;
  760. }
  761. else
  762. {
  763. Result = P - ACh.c_str() + 1;
  764. // skip consecutive delimiters
  765. while ((Result < Len) &&
  766. IsPathWordDelimiter(ACh[Result + 1]))
  767. {
  768. Result++;
  769. }
  770. }
  771. }
  772. }
  773. else
  774. {
  775. FAIL;
  776. Result = 0;
  777. }
  778. return Result;
  779. }
  780. //---------------------------------------------------------------------------
  781. class TPublicCustomCombo : public TCustomCombo
  782. {
  783. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  784. };
  785. //---------------------------------------------------------------------------
  786. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  787. {
  788. // Since we are setting Application->ModalPopupMode = pmAuto,
  789. // this has to be called from OnShow, not from constructor anymore,
  790. // to have any effect
  791. HWND Wnd;
  792. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  793. {
  794. TPublicCustomCombo * Combo =
  795. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  796. Combo->HandleNeeded();
  797. Wnd = Combo->EditHandle;
  798. }
  799. else
  800. {
  801. Wnd = Control->Handle;
  802. }
  803. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  804. }
  805. //---------------------------------------------------------------------------
  806. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  807. {
  808. int Shift = 0;
  809. for (int Index = 0; Index < Count; Index++)
  810. {
  811. if (ControlsOrder[Index]->Visible)
  812. {
  813. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  814. }
  815. else
  816. {
  817. Shift++;
  818. }
  819. }
  820. Count -= Shift;
  821. }
  822. //---------------------------------------------------------------------------
  823. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  824. {
  825. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  826. if (Count > 0)
  827. {
  828. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  829. CommonParent->DisableAlign();
  830. try
  831. {
  832. int Top = 0;
  833. for (int Index = 0; Index < Count; Index++)
  834. {
  835. assert(ControlsOrder[Index]->Parent == CommonParent);
  836. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  837. {
  838. Top = ControlsOrder[Index]->Top;
  839. }
  840. }
  841. for (int Index = 0; Index < Count; Index++)
  842. {
  843. TControl * Control = ControlsOrder[Index];
  844. Control->Top = Top;
  845. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  846. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  847. {
  848. Top += Control->Height;
  849. }
  850. }
  851. }
  852. __finally
  853. {
  854. CommonParent->EnableAlign();
  855. }
  856. }
  857. }
  858. //---------------------------------------------------------------------------
  859. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  860. {
  861. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  862. if (Count > 0)
  863. {
  864. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  865. CommonParent->DisableAlign();
  866. try
  867. {
  868. int Left = 0;
  869. for (int Index = 0; Index < Count; Index++)
  870. {
  871. assert(ControlsOrder[Index]->Parent == CommonParent);
  872. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  873. {
  874. Left = ControlsOrder[Index]->Left;
  875. }
  876. }
  877. for (int Index = 0; Index < Count; Index++)
  878. {
  879. TControl * Control = ControlsOrder[Index];
  880. Control->Left = Left;
  881. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  882. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  883. {
  884. Left += Control->Width;
  885. }
  886. // vertical alignment has priority, so alBottom-aligned controls start
  887. // at the very left, even if there are any alLeft/alRight controls.
  888. // for the reason this code is not necessary in SetVerticalControlsOrder.
  889. // we could exit the loop as well here.
  890. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  891. {
  892. Left = 0;
  893. }
  894. }
  895. }
  896. __finally
  897. {
  898. CommonParent->EnableAlign();
  899. }
  900. }
  901. }
  902. //---------------------------------------------------------------------------
  903. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  904. {
  905. if (After->TabOrder > Control->TabOrder)
  906. {
  907. After->TabOrder = Control->TabOrder;
  908. }
  909. else if (After->TabOrder < Control->TabOrder - 1)
  910. {
  911. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  912. }
  913. }
  914. //---------------------------------------------------------------------------
  915. void __fastcall CutFormToDesktop(TForm * Form)
  916. {
  917. assert(Form->Monitor != NULL);
  918. TRect Workarea = Form->Monitor->WorkareaRect;
  919. if (Form->Top + Form->Height > Workarea.Bottom)
  920. {
  921. Form->Height = Workarea.Bottom - Form->Top;
  922. }
  923. if (Form->Left + Form->Width >= Workarea.Right)
  924. {
  925. Form->Width = Workarea.Right - Form->Left;
  926. }
  927. }
  928. //---------------------------------------------------------------------------
  929. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  930. {
  931. if ((Position == poScreenCenter) ||
  932. (Position == poOwnerFormCenter) ||
  933. (Position == poMainFormCenter))
  934. {
  935. TCustomForm * CenterForm = NULL;
  936. if ((Position == poOwnerFormCenter) ||
  937. (Position == poMainFormCenter))
  938. {
  939. CenterForm = Application->MainForm;
  940. if ((Position == poOwnerFormCenter) &&
  941. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  942. {
  943. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  944. }
  945. }
  946. TRect Bounds = Form->BoundsRect;
  947. int X, Y;
  948. if (CenterForm != NULL)
  949. {
  950. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  951. ((TForm *)CenterForm)->Left;
  952. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  953. ((TForm *)CenterForm)->Top;
  954. }
  955. else
  956. {
  957. X = (Form->Monitor->Width - Bounds.Width()) / 2;
  958. Y = (Form->Monitor->Height - Bounds.Height()) / 2;
  959. }
  960. if (X < 0)
  961. {
  962. X = 0;
  963. }
  964. if (Y < 0)
  965. {
  966. Y = 0;
  967. }
  968. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  969. }
  970. }
  971. //---------------------------------------------------------------------------
  972. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  973. {
  974. // This has to be called only after DoFormWindowProc(CM_SHOWINGCHANGED),
  975. // so that a correct monitor is considered.
  976. // Note that we cannot use LastMonitor(), as ResizeForm is also called from
  977. // TConsoleDialog::DoAdjustWindow, where we need to use the actual monitor
  978. // (in case user moves the console window to a different monitor,
  979. // than where a main window is [no matter how unlikely that is])
  980. TRect WorkareaRect = Form->Monitor->WorkareaRect;
  981. if (Height > WorkareaRect.Height())
  982. {
  983. Height = WorkareaRect.Height();
  984. }
  985. if (Width > WorkareaRect.Width())
  986. {
  987. Width = WorkareaRect.Width();
  988. }
  989. if (Height < Form->Constraints->MinHeight)
  990. {
  991. Height = Form->Constraints->MinHeight;
  992. }
  993. if (Width < Form->Constraints->MinWidth)
  994. {
  995. Width = Form->Constraints->MinWidth;
  996. }
  997. TRect Bounds = Form->BoundsRect;
  998. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  999. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  1000. if (Top + Height > WorkareaRect.Bottom)
  1001. {
  1002. Top = WorkareaRect.Bottom - Height;
  1003. }
  1004. if (Left + Width >= WorkareaRect.Right)
  1005. {
  1006. Left = WorkareaRect.Right - Width;
  1007. }
  1008. // WorkareaRect.Left is not 0, when secondary monitor is placed left of primary one.
  1009. // Similarly for WorkareaRect.Top.
  1010. if (Top < WorkareaRect.Top)
  1011. {
  1012. Top = WorkareaRect.Top;
  1013. }
  1014. if (Left < WorkareaRect.Left)
  1015. {
  1016. Left = WorkareaRect.Left;
  1017. }
  1018. Form->SetBounds(Left, Top, Width, Height);
  1019. Bounds = Form->BoundsRect;
  1020. // due to constraints, form can remain larger, make sure it is centered although
  1021. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  1022. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  1023. Form->SetBounds(Left, Top, Width, Height);
  1024. }
  1025. //---------------------------------------------------------------------------
  1026. TComponent * __fastcall GetFormOwner()
  1027. {
  1028. if (Screen->ActiveForm != NULL)
  1029. {
  1030. return Screen->ActiveForm;
  1031. }
  1032. else
  1033. {
  1034. return Application;
  1035. }
  1036. }
  1037. //---------------------------------------------------------------------------
  1038. void __fastcall SetCorrectFormParent(TForm * /*Form*/)
  1039. {
  1040. // noop
  1041. // remove
  1042. }
  1043. //---------------------------------------------------------------------------
  1044. void __fastcall InvokeHelp(TWinControl * Control)
  1045. {
  1046. assert(Control != NULL);
  1047. HELPINFO HelpInfo;
  1048. HelpInfo.cbSize = sizeof(HelpInfo);
  1049. HelpInfo.iContextType = HELPINFO_WINDOW;
  1050. HelpInfo.iCtrlId = 0;
  1051. HelpInfo.hItemHandle = Control->Handle;
  1052. HelpInfo.dwContextId = 0;
  1053. HelpInfo.MousePos.x = 0;
  1054. HelpInfo.MousePos.y = 0;
  1055. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. //---------------------------------------------------------------------------
  1059. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  1060. TControlCanvas ** ACanvas, TRect & R)
  1061. {
  1062. TControlCanvas * Canvas = new TControlCanvas();
  1063. try
  1064. {
  1065. Canvas->Control = StaticText;
  1066. R = StaticText->ClientRect;
  1067. UnicodeString Caption = StaticText->Caption;
  1068. bool AccelChar = false;
  1069. if (StaticText->ShowAccelChar)
  1070. {
  1071. Caption = StripHotkey(Caption);
  1072. AccelChar = (Caption != StaticText->Caption);
  1073. }
  1074. TSize TextSize = Canvas->TextExtent(Caption);
  1075. assert(StaticText->BorderStyle == sbsNone); // not taken into account
  1076. if (AccelChar)
  1077. {
  1078. TextSize.cy += 2;
  1079. }
  1080. R.Bottom = R.Top + TextSize.cy;
  1081. switch (StaticText->Alignment)
  1082. {
  1083. case taLeftJustify:
  1084. R.Right = R.Left + TextSize.cx;
  1085. break;
  1086. case taRightJustify:
  1087. R.Left = Max(0, R.Right - TextSize.cx);
  1088. break;
  1089. case taCenter:
  1090. {
  1091. FAIL; // not used branch, possibly untested
  1092. int Diff = R.Width() - TextSize.cx;
  1093. R.Left += Diff / 2;
  1094. R.Right -= Diff - (Diff / 2);
  1095. }
  1096. break;
  1097. }
  1098. }
  1099. __finally
  1100. {
  1101. if (ACanvas == NULL)
  1102. {
  1103. delete Canvas;
  1104. }
  1105. }
  1106. if (ACanvas != NULL)
  1107. {
  1108. *ACanvas = Canvas;
  1109. }
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1113. bool & Clicked)
  1114. {
  1115. Clicked = false;
  1116. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1117. if (Message.Msg == WM_LBUTTONDOWN)
  1118. {
  1119. StaticText->SetFocus();
  1120. // in case the action takes long, make sure focus is shown immediately
  1121. UpdateWindow(StaticText->Handle);
  1122. Clicked = true;
  1123. Message.Result = 1;
  1124. }
  1125. else if (Message.Msg == WM_RBUTTONDOWN)
  1126. {
  1127. StaticText->SetFocus();
  1128. Message.Result = 1;
  1129. }
  1130. else if (Message.Msg == WM_CHAR)
  1131. {
  1132. if (reinterpret_cast<TWMChar &>(Message).CharCode == L' ')
  1133. {
  1134. Clicked = true;
  1135. Message.Result = 1;
  1136. }
  1137. else
  1138. {
  1139. ControlWndProc(StaticText)(Message);
  1140. }
  1141. }
  1142. else if (Message.Msg == CM_DIALOGCHAR)
  1143. {
  1144. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1145. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1146. {
  1147. StaticText->SetFocus();
  1148. // in case the action takes long, make sure focus is shown immediately
  1149. UpdateWindow(StaticText->Handle);
  1150. Clicked = true;
  1151. Message.Result = 1;
  1152. }
  1153. else
  1154. {
  1155. ControlWndProc(StaticText)(Message);
  1156. }
  1157. }
  1158. else
  1159. {
  1160. ControlWndProc(StaticText)(Message);
  1161. }
  1162. if (Message.Msg == WM_PAINT)
  1163. {
  1164. TRect R;
  1165. TControlCanvas * Canvas;
  1166. FocusableLabelCanvas(StaticText, &Canvas, R);
  1167. try
  1168. {
  1169. if (StaticText->Focused())
  1170. {
  1171. Canvas->DrawFocusRect(R);
  1172. }
  1173. else if (!StaticText->Font->Style.Contains(fsUnderline))
  1174. {
  1175. Canvas->Pen->Style = psDot;
  1176. Canvas->Brush->Style = bsClear;
  1177. if (!StaticText->Enabled)
  1178. {
  1179. Canvas->Pen->Color = clBtnHighlight;
  1180. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1181. Canvas->LineTo(R.Right + 1, R.Bottom);
  1182. Canvas->Pen->Color = clGrayText;
  1183. }
  1184. Canvas->MoveTo(R.Left, R.Bottom - 1);
  1185. Canvas->LineTo(R.Right, R.Bottom - 1);
  1186. }
  1187. }
  1188. __finally
  1189. {
  1190. delete Canvas;
  1191. }
  1192. }
  1193. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1194. (Message.Msg == CM_ENABLEDCHANGED))
  1195. {
  1196. StaticText->Invalidate();
  1197. }
  1198. }
  1199. //---------------------------------------------------------------------------
  1200. static THintWindow * PersistentHintWindow = NULL;
  1201. static TControl * PersistentHintControl = NULL;
  1202. //---------------------------------------------------------------------------
  1203. void __fastcall CancelPersistentHint()
  1204. {
  1205. if (PersistentHintWindow != NULL)
  1206. {
  1207. PersistentHintControl = NULL;
  1208. SAFE_DESTROY(PersistentHintWindow);
  1209. }
  1210. }
  1211. //---------------------------------------------------------------------------
  1212. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1213. {
  1214. CancelPersistentHint();
  1215. THintInfo HintInfo;
  1216. HintInfo.HintControl = Control;
  1217. HintInfo.HintPos = HintPos;
  1218. HintInfo.HintMaxWidth = GetParentForm(Control)->Monitor->Width;
  1219. HintInfo.HintColor = Application->HintColor;
  1220. HintInfo.HintStr = GetShortHint(Control->Hint);
  1221. HintInfo.HintData = NULL;
  1222. bool CanShow = true;
  1223. if (Application->OnShowHint != NULL)
  1224. {
  1225. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1226. }
  1227. if (CanShow)
  1228. {
  1229. PersistentHintControl = Control;
  1230. PersistentHintWindow = new THintWindow(Application);
  1231. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1232. PersistentHintWindow->Color = HintInfo.HintColor;
  1233. TRect HintWinRect;
  1234. if (HintInfo.HintMaxWidth < Control->Width)
  1235. {
  1236. HintInfo.HintMaxWidth = Control->Width;
  1237. }
  1238. HintWinRect = PersistentHintWindow->CalcHintRect(
  1239. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1240. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1241. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1242. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1243. }
  1244. }
  1245. //---------------------------------------------------------------------------
  1246. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1247. {
  1248. bool Clicked = false;
  1249. bool Cancel = false;
  1250. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1251. if (Message.Msg == CM_HINTSHOW)
  1252. {
  1253. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1254. if (PersistentHintControl == StaticText)
  1255. {
  1256. // do not allow standard hint when persistent is already shown
  1257. HintShow.Result = 1;
  1258. }
  1259. else
  1260. {
  1261. HintShow.HintInfo->HideTimeout = 100000; // never
  1262. }
  1263. }
  1264. else if (Message.Msg == CN_KEYDOWN)
  1265. {
  1266. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1267. (PersistentHintControl == StaticText))
  1268. {
  1269. CancelPersistentHint();
  1270. StaticText->Invalidate();
  1271. Message.Result = 1;
  1272. }
  1273. else
  1274. {
  1275. FocusableLabelWindowProc(Data, Message, Clicked);
  1276. }
  1277. }
  1278. else
  1279. {
  1280. FocusableLabelWindowProc(Data, Message, Clicked);
  1281. }
  1282. if (Message.Msg == CM_CANCELMODE)
  1283. {
  1284. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  1285. if ((CancelMessage.Sender != StaticText) &&
  1286. (CancelMessage.Sender != PersistentHintWindow))
  1287. {
  1288. Cancel = true;
  1289. }
  1290. }
  1291. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  1292. {
  1293. Cancel = true;
  1294. }
  1295. if (Cancel && (PersistentHintControl == StaticText))
  1296. {
  1297. CancelPersistentHint();
  1298. }
  1299. if (Clicked && (PersistentHintControl != StaticText))
  1300. {
  1301. TRect R;
  1302. TPoint HintPos;
  1303. FocusableLabelCanvas(StaticText, NULL, R);
  1304. HintPos.y = R.Bottom - R.Top;
  1305. HintPos.x = R.Left;
  1306. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  1307. }
  1308. }
  1309. //---------------------------------------------------------------------------
  1310. void __fastcall HintLabel(TStaticText * StaticText, UnicodeString Hint)
  1311. {
  1312. // Currently all are right-justified, when other alignemtn is used,
  1313. // test respective branches in FocusableLabelCanvas.
  1314. assert(StaticText->Alignment == taRightJustify);
  1315. // With right-justify, it has to be off. We may not notice on riginal
  1316. // English version, results will differ with translations only
  1317. assert(!StaticText->AutoSize);
  1318. StaticText->ParentFont = true;
  1319. if (!Hint.IsEmpty())
  1320. {
  1321. StaticText->Hint = Hint;
  1322. }
  1323. StaticText->ShowHint = true;
  1324. StaticText->Cursor = crHandPoint;
  1325. TWndMethod WindowProc;
  1326. ((TMethod*)&WindowProc)->Data = StaticText;
  1327. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  1328. StaticText->WindowProc = WindowProc;
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall HintLabelRestore(TStaticText * StaticText)
  1332. {
  1333. StaticText->WindowProc = ControlWndProc(StaticText);
  1334. StaticText->ShowHint = false;
  1335. StaticText->Cursor = crDefault;
  1336. }
  1337. //---------------------------------------------------------------------------
  1338. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  1339. {
  1340. // it is TCustomComboxBox, but the properties are published only by TComboBox
  1341. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  1342. if (Message.Msg == WM_SIZE)
  1343. {
  1344. UnicodeString Text = ComboBox->Text;
  1345. try
  1346. {
  1347. ControlWndProc(ComboBox)(Message);
  1348. }
  1349. __finally
  1350. {
  1351. // workaround for bug in combo box, that causes it to change text to any
  1352. // item from drop down list which starts with current text,
  1353. // after control is resized (unless the text is in drop down list as well)
  1354. ComboBox->Text = Text;
  1355. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  1356. if (!ComboBox->Focused())
  1357. {
  1358. ComboBox->SelLength = 0;
  1359. }
  1360. }
  1361. }
  1362. else
  1363. {
  1364. ControlWndProc(ComboBox)(Message);
  1365. }
  1366. }
  1367. //---------------------------------------------------------------------------
  1368. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  1369. {
  1370. TWndMethod WindowProc;
  1371. ((TMethod*)&WindowProc)->Data = ComboBox;
  1372. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  1373. ComboBox->WindowProc = WindowProc;
  1374. }
  1375. //---------------------------------------------------------------------------
  1376. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  1377. {
  1378. if (StaticText->OnClick != NULL)
  1379. {
  1380. StaticText->OnClick(StaticText);
  1381. }
  1382. else
  1383. {
  1384. UnicodeString Url = StaticText->Caption;
  1385. if (!SameText(Url.SubString(1, 4), L"http") && (Url.Pos(L"@") > 0))
  1386. {
  1387. Url = L"mailto:" + Url;
  1388. }
  1389. OpenBrowser(Url);
  1390. }
  1391. }
  1392. //---------------------------------------------------------------------------
  1393. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  1394. {
  1395. bool Clicked = false;
  1396. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1397. if (Message.Msg == WM_CONTEXTMENU)
  1398. {
  1399. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  1400. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  1401. {
  1402. TRect R;
  1403. FocusableLabelCanvas(StaticText, NULL, R);
  1404. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  1405. ContextMenu.Pos.x = static_cast<short>(P.x);
  1406. ContextMenu.Pos.y = static_cast<short>(P.y);
  1407. }
  1408. }
  1409. else if (Message.Msg == WM_KEYDOWN)
  1410. {
  1411. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  1412. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == L'C'))
  1413. {
  1414. TInstantOperationVisualizer Visualizer;
  1415. CopyToClipboard(StaticText->Caption);
  1416. Message.Result = 1;
  1417. }
  1418. else
  1419. {
  1420. FocusableLabelWindowProc(Data, Message, Clicked);
  1421. }
  1422. }
  1423. FocusableLabelWindowProc(Data, Message, Clicked);
  1424. if (Message.Msg == WM_DESTROY)
  1425. {
  1426. delete StaticText->PopupMenu;
  1427. assert(StaticText->PopupMenu == NULL);
  1428. }
  1429. if (Clicked)
  1430. {
  1431. LinkLabelClick(StaticText);
  1432. }
  1433. }
  1434. //---------------------------------------------------------------------------
  1435. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  1436. {
  1437. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1438. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  1439. assert(MenuItem != NULL);
  1440. if (MenuItem->Tag == 0)
  1441. {
  1442. LinkLabelClick(StaticText);
  1443. }
  1444. else
  1445. {
  1446. TInstantOperationVisualizer Visualizer;
  1447. CopyToClipboard(StaticText->Caption);
  1448. }
  1449. }
  1450. //---------------------------------------------------------------------------
  1451. void __fastcall LinkLabel(TStaticText * StaticText, UnicodeString Url,
  1452. TNotifyEvent OnEnter)
  1453. {
  1454. StaticText->Transparent = false;
  1455. StaticText->ParentFont = true;
  1456. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  1457. StaticText->Font->Color = LinkColor;
  1458. StaticText->Cursor = crHandPoint;
  1459. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  1460. if (!Url.IsEmpty())
  1461. {
  1462. StaticText->Caption = Url;
  1463. }
  1464. if (StaticText->OnClick == NULL)
  1465. {
  1466. assert(StaticText->PopupMenu == NULL);
  1467. StaticText->PopupMenu = new TPopupMenu(StaticText);
  1468. try
  1469. {
  1470. TNotifyEvent ContextMenuOnClick;
  1471. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  1472. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  1473. TMenuItem * Item;
  1474. Item = new TMenuItem(StaticText->PopupMenu);
  1475. Item->Caption = LoadStr(URL_LINK_OPEN);
  1476. Item->Tag = 0;
  1477. Item->ShortCut = ShortCut(L' ', TShiftState());
  1478. Item->OnClick = ContextMenuOnClick;
  1479. StaticText->PopupMenu->Items->Add(Item);
  1480. Item = new TMenuItem(StaticText->PopupMenu);
  1481. Item->Caption = LoadStr(URL_LINK_COPY);
  1482. Item->Tag = 1;
  1483. Item->ShortCut = ShortCut(L'C', TShiftState() << ssCtrl);
  1484. Item->OnClick = ContextMenuOnClick;
  1485. StaticText->PopupMenu->Items->Add(Item);
  1486. }
  1487. catch(...)
  1488. {
  1489. delete StaticText->PopupMenu;
  1490. assert(StaticText->PopupMenu == NULL);
  1491. throw;
  1492. }
  1493. }
  1494. TWndMethod WindowProc;
  1495. ((TMethod*)&WindowProc)->Data = StaticText;
  1496. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  1497. StaticText->WindowProc = WindowProc;
  1498. }
  1499. //---------------------------------------------------------------------------
  1500. static void __fastcall HotTrackLabelMouseEnter(void * /*Data*/, TObject * Sender)
  1501. {
  1502. reinterpret_cast<TLabel *>(Sender)->Font->Color = clBlue;
  1503. }
  1504. //---------------------------------------------------------------------------
  1505. static void __fastcall HotTrackLabelMouseLeave(void * /*Data*/, TObject * Sender)
  1506. {
  1507. reinterpret_cast<TLabel *>(Sender)->ParentFont = true;
  1508. }
  1509. //---------------------------------------------------------------------------
  1510. void __fastcall HotTrackLabel(TLabel * Label)
  1511. {
  1512. assert(Label->OnMouseEnter == NULL);
  1513. assert(Label->OnMouseLeave == NULL);
  1514. Label->OnMouseEnter = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseEnter);
  1515. Label->OnMouseLeave = MakeMethod<TNotifyEvent>(NULL, HotTrackLabelMouseLeave);
  1516. }
  1517. //---------------------------------------------------------------------------
  1518. Forms::TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  1519. {
  1520. Forms::TMonitor * Result;
  1521. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  1522. {
  1523. Result = Application->MainForm->Monitor;
  1524. }
  1525. else if (LastMonitor != NULL)
  1526. {
  1527. Result = LastMonitor;
  1528. }
  1529. else
  1530. {
  1531. int i = 0;
  1532. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  1533. {
  1534. i++;
  1535. }
  1536. assert(Screen->Monitors[i]->Primary);
  1537. Result = Screen->Monitors[i];
  1538. }
  1539. return Result;
  1540. }
  1541. //---------------------------------------------------------------------------
  1542. int __fastcall GetLastMonitor()
  1543. {
  1544. if (LastMonitor != NULL)
  1545. {
  1546. return LastMonitor->MonitorNum;
  1547. }
  1548. else
  1549. {
  1550. return -1;
  1551. }
  1552. }
  1553. //---------------------------------------------------------------------------
  1554. void __fastcall SetLastMonitor(int MonitorNum)
  1555. {
  1556. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  1557. {
  1558. LastMonitor = Screen->Monitors[MonitorNum];
  1559. }
  1560. else
  1561. {
  1562. LastMonitor = NULL;
  1563. }
  1564. }
  1565. //---------------------------------------------------------------------------
  1566. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  1567. {
  1568. TForm * Form;
  1569. if (Owner == NULL)
  1570. {
  1571. Owner = GetFormOwner();
  1572. }
  1573. // If there is no main form yet, make this one main.
  1574. // This:
  1575. // - Makes other forms (dialogs invoked from this one),
  1576. // be placed on the same monitor (otherwise all new forms get placed
  1577. // on primary monitor)
  1578. // - Triggers MainForm-specific code in DoFormWindowProc.
  1579. // - Shows button on taskbar
  1580. if (Application->MainForm == NULL)
  1581. {
  1582. Application->CreateForm(FormClass, &Form);
  1583. assert(Application->MainForm == Form);
  1584. }
  1585. else
  1586. {
  1587. Form = dynamic_cast<TForm *>(Construct(FormClass, Owner));
  1588. assert(Form != NULL);
  1589. }
  1590. return Form;
  1591. }
  1592. //---------------------------------------------------------------------------
  1593. TImageList * __fastcall SharedSystemImageList(bool Large)
  1594. {
  1595. TSHFileInfo FileInfo;
  1596. TImageList * Result = new TImageList(Application);
  1597. int ImageListHandle = SHGetFileInfo(L"", 0, &FileInfo, sizeof(FileInfo),
  1598. SHGFI_SYSICONINDEX | (Large ? SHGFI_LARGEICON : SHGFI_SMALLICON));
  1599. if (ImageListHandle != 0)
  1600. {
  1601. Result->ShareImages = true;
  1602. Result->Handle = ImageListHandle;
  1603. }
  1604. return Result;
  1605. }
  1606. //---------------------------------------------------------------------------
  1607. bool __fastcall SupportsSplitButton()
  1608. {
  1609. return (Win32MajorVersion >= 6);
  1610. }
  1611. //---------------------------------------------------------------------------
  1612. TButton * __fastcall FindDefaultButton(TWinControl * Control)
  1613. {
  1614. TButton * Result = NULL;
  1615. int Index = 0;
  1616. while ((Result == NULL) && (Index < Control->ControlCount))
  1617. {
  1618. TControl * ChildControl = Control->Controls[Index];
  1619. TButton * Button = dynamic_cast<TButton *>(ChildControl);
  1620. if ((Button != NULL) && Button->Default)
  1621. {
  1622. Result = Button;
  1623. }
  1624. else
  1625. {
  1626. TWinControl * WinControl = dynamic_cast<TWinControl *>(ChildControl);
  1627. if (WinControl != NULL)
  1628. {
  1629. Result = FindDefaultButton(WinControl);
  1630. }
  1631. }
  1632. Index++;
  1633. }
  1634. return Result;
  1635. }
  1636. //---------------------------------------------------------------------------
  1637. TModalResult __fastcall DefaultResult(TCustomForm * Form)
  1638. {
  1639. // The point of this is to avoid hardcoding mrOk when checking dialog results.
  1640. // Previously we used != mrCancel instead, as mrCancel is more reliable,
  1641. // being automatically used for Esc/X buttons (and hence kind of forced to be used
  1642. // for Cancel buttons). But that failed to be reliable in the end, for
  1643. // ModalResult being mrNone, when Windows session is being logged off.
  1644. // We interpreted mrNone as OK, causing lots of troubles.
  1645. TModalResult Result = mrNone;
  1646. TButton * Button = FindDefaultButton(Form);
  1647. if (ALWAYS_TRUE(Button != NULL))
  1648. {
  1649. Result = Button->ModalResult;
  1650. }
  1651. if (ALWAYS_FALSE(Result == mrNone))
  1652. {
  1653. Result = mrOk;
  1654. }
  1655. return Result;
  1656. }
  1657. //---------------------------------------------------------------------------
  1658. void __fastcall MemoKeyDown(TObject * Sender, WORD & Key, TShiftState Shift)
  1659. {
  1660. // Sender can be Form or Memo itself
  1661. TControl * Control = dynamic_cast<TControl *>(Sender);
  1662. if (ALWAYS_TRUE(Control != NULL))
  1663. {
  1664. TCustomForm * Form = GetParentForm(Control);
  1665. // Particularly when WantReturns is true,
  1666. // memo swallows also Esc, so we have to handle it ourselves.
  1667. // See also ReadOnlyControl.
  1668. if ((Key == VK_ESCAPE) && Shift.Empty())
  1669. {
  1670. Form->ModalResult = mrCancel;
  1671. Key = 0;
  1672. }
  1673. else if ((Key == VK_RETURN) && Shift.Contains(ssCtrl))
  1674. {
  1675. Form->ModalResult = DefaultResult(Form);
  1676. Key = 0;
  1677. }
  1678. }
  1679. }
  1680. //---------------------------------------------------------------------------
  1681. void __fastcall SetFormIcon(TForm * Form, int Size, const UnicodeString & IconName)
  1682. {
  1683. HICON Icon = LoadIcon(HInstance, IconName.c_str());
  1684. if (ALWAYS_TRUE(Icon != NULL))
  1685. {
  1686. LPARAM LParam = reinterpret_cast<LPARAM>(Icon);
  1687. SendMessage(Form->Handle, WM_SETICON, Size, LParam);
  1688. DestroyIcon(Icon);
  1689. }
  1690. }
  1691. //---------------------------------------------------------------------------
  1692. void __fastcall SetFormIcons(TForm * Form, const UnicodeString & BigIconName,
  1693. const UnicodeString & SmallIconName)
  1694. {
  1695. SetFormIcon(Form, ICON_SMALL, SmallIconName);
  1696. SetFormIcon(Form, ICON_BIG, BigIconName);
  1697. }
  1698. //---------------------------------------------------------------------------
  1699. void __fastcall UseDesktopFont(TControl * Control)
  1700. {
  1701. TCustomStatusBar * StatusBar = dynamic_cast<TCustomStatusBar *>(Control);
  1702. if (StatusBar != NULL)
  1703. {
  1704. // otherwise setting DesktopFont below has no effect
  1705. StatusBar->UseSystemFont = false;
  1706. }
  1707. class TPublicControl : public TControl
  1708. {
  1709. public:
  1710. __property DesktopFont;
  1711. };
  1712. reinterpret_cast<TPublicControl *>(Control)->DesktopFont = true;
  1713. }
  1714. //---------------------------------------------------------------------------
  1715. void __fastcall LoadResourceImage(TImage * Image, const UnicodeString & ImageName)
  1716. {
  1717. std::unique_ptr<TPngImage> Png(new TPngImage());
  1718. Png->LoadFromResourceName(0, ImageName);
  1719. Image->Picture->Assign(Png.get());
  1720. }