VCLCommon.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578
  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 <FileCtrl.hpp>
  12. #include <ThemeMgr.hpp>
  13. #include <PathLabel.hpp>
  14. #include <PasTools.hpp>
  15. //---------------------------------------------------------------------------
  16. #pragma package(smart_init)
  17. //---------------------------------------------------------------------------
  18. static TThemeManager * ThemeManager = NULL;
  19. //---------------------------------------------------------------------------
  20. void __fastcall AdjustListColumnsWidth(TListView* ListView, int RowCount, int RightPad)
  21. {
  22. int OriginalWidth, NewWidth, i, CWidth, LastResizible;
  23. OriginalWidth = 0;
  24. LastResizible = -1;
  25. for (i = 0; i < ListView->Columns->Count; i++)
  26. {
  27. OriginalWidth += ListView->Columns->Items[i]->Width;
  28. if (ListView->Columns->Items[i]->Tag == 0)
  29. {
  30. LastResizible = i;
  31. }
  32. }
  33. assert(LastResizible >= 0);
  34. // when listview is virtual, ListView->Items->Count seems to return invalid
  35. // value, thus provide a method to pass actual count explicitly
  36. if (RowCount < 0)
  37. {
  38. RowCount = ListView->Items->Count;
  39. }
  40. NewWidth = 0;
  41. CWidth = ListView->ClientWidth - RightPad;
  42. if ((ListView->VisibleRowCount < RowCount) &&
  43. (ListView->Width - ListView->ClientWidth < GetSystemMetrics(SM_CXVSCROLL)))
  44. {
  45. CWidth -= GetSystemMetrics(SM_CXVSCROLL);
  46. }
  47. for (i = 0; i < ListView->Columns->Count; i++)
  48. {
  49. if (i != LastResizible)
  50. {
  51. if (ListView->Columns->Items[i]->Tag == 0)
  52. {
  53. ListView->Columns->Items[i]->Width =
  54. (CWidth * ListView->Columns->Items[i]->Width) / OriginalWidth;
  55. }
  56. NewWidth += ListView->Columns->Items[i]->Width;
  57. }
  58. }
  59. ListView->Columns->Items[LastResizible]->Width = CWidth-NewWidth;
  60. }
  61. //---------------------------------------------------------------------------
  62. static void __fastcall SetParentColor(TControl * Control)
  63. {
  64. TColor Color;
  65. assert(ThemeManager != NULL);
  66. if (ThemeManager->ThemesEnabled)
  67. {
  68. bool OnTabSheet = false;
  69. TWinControl * Parent = Control->Parent;
  70. while ((Parent != NULL) && !OnTabSheet)
  71. {
  72. TTabSheet * TabSheet = dynamic_cast<TTabSheet *>(Parent);
  73. OnTabSheet = (TabSheet != NULL) && TabSheet->TabVisible;
  74. Parent = Parent->Parent;
  75. }
  76. if (OnTabSheet)
  77. {
  78. Color = ThemeManager->GetColor(teTab, ::TABP_BODY, 0, ::TMT_FILLCOLORHINT);
  79. }
  80. else
  81. {
  82. Color = ThemeManager->GetColor(teWindow, ::WP_DIALOG, 0, ::TMT_FILLCOLOR);
  83. }
  84. }
  85. else
  86. {
  87. Color = clBtnFace;
  88. }
  89. ((TEdit*)Control)->Color = Color;
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall EnableControl(TControl * Control, bool Enable)
  93. {
  94. if (Control->Enabled != Enable)
  95. {
  96. if (Control->InheritsFrom(__classid(TWinControl)) &&
  97. (((TWinControl*)Control)->ControlCount > 0))
  98. {
  99. for (Integer Index = 0; Index < ((TWinControl*)Control)->ControlCount; Index++)
  100. EnableControl(((TWinControl*)Control)->Controls[Index], Enable);
  101. }
  102. Control->Enabled = Enable;
  103. }
  104. if (Control->InheritsFrom(__classid(TCustomEdit)) ||
  105. Control->InheritsFrom(__classid(TCustomComboBox)) ||
  106. Control->InheritsFrom(__classid(TCustomListView)))
  107. {
  108. if (Enable)
  109. {
  110. ((TEdit*)Control)->Color = clWindow;
  111. }
  112. else
  113. {
  114. ((TEdit*)Control)->Color = clBtnFace;
  115. }
  116. }
  117. };
  118. //---------------------------------------------------------------------------
  119. void __fastcall ReadOnlyControl(TControl * Control, bool ReadOnly)
  120. {
  121. if (Control->InheritsFrom(__classid(TCustomEdit)))
  122. {
  123. ((TEdit*)Control)->ReadOnly = ReadOnly;
  124. if (ReadOnly)
  125. {
  126. SetParentColor(Control);
  127. }
  128. else
  129. {
  130. ((TEdit*)Control)->Color = clWindow;
  131. }
  132. }
  133. else
  134. {
  135. assert(false);
  136. }
  137. }
  138. //---------------------------------------------------------------------------
  139. struct TSavedSystemSettings
  140. {
  141. TCustomForm * Form;
  142. AnsiString FontName;
  143. bool Flipped;
  144. TWndMethod OldWndProc;
  145. };
  146. //---------------------------------------------------------------------------
  147. static void __fastcall ThemeManagerAllowSubclassing(void * /*Data*/,
  148. TThemeManager * /*Sender*/, TControl * Control, bool & Allow)
  149. {
  150. TPathLabel * PathLabel = dynamic_cast<TPathLabel *>(Control);
  151. // intent is to only exclude path labels on the main window
  152. if ((PathLabel != NULL) && (PathLabel->FocusControl != NULL))
  153. {
  154. Allow = false;
  155. }
  156. }
  157. //---------------------------------------------------------------------------
  158. class TPublicControl : public TWinControl
  159. {
  160. friend TWndMethod __fastcall ControlWndProc(TWinControl * Control);
  161. };
  162. //---------------------------------------------------------------------------
  163. TWndMethod __fastcall ControlWndProc(TWinControl * Control)
  164. {
  165. TPublicControl * PublicControl = static_cast<TPublicControl *>(Control);
  166. return &PublicControl->WndProc;
  167. }
  168. //---------------------------------------------------------------------------
  169. static TMonitor * LastMonitor = NULL;
  170. //---------------------------------------------------------------------------
  171. inline void __fastcall DoFormWindowProc(TCustomForm * Form, TWndMethod WndProc,
  172. TMessage & Message)
  173. {
  174. if ((Message.Msg == WM_SYSCOMMAND) &&
  175. (Message.WParam == SC_CONTEXTHELP))
  176. {
  177. InvokeHelp(Form->ActiveControl);
  178. Message.Result = 1;
  179. }
  180. else if (Message.Msg == CM_SHOWINGCHANGED)
  181. {
  182. TForm * AForm = dynamic_cast<TForm *>(Form);
  183. assert(AForm != NULL);
  184. if ((Application->MainForm == Form) ||
  185. // this particularly happens if error occurs while main
  186. // window is being shown (e.g. non existent local directory when opening
  187. // explorer)
  188. ((Application->MainForm != NULL) && !Application->MainForm->Visible))
  189. {
  190. if (!Form->Showing)
  191. {
  192. // when closing main form, remember its monitor,
  193. // so that the next form is shown on the same one
  194. LastMonitor = Form->Monitor;
  195. }
  196. else if ((LastMonitor != NULL) && (LastMonitor != Form->Monitor) &&
  197. Form->Showing)
  198. {
  199. // would actually always be poScreenCenter, see _SafeFormCreate
  200. if ((AForm->Position == poMainFormCenter) ||
  201. (AForm->Position == poScreenCenter))
  202. {
  203. // this would typically be an authentication dialog,
  204. // but it may as well be an message box
  205. // taken from TCustomForm::SetWindowToMonitor
  206. AForm->SetBounds(LastMonitor->Left + ((LastMonitor->Width - AForm->Width) / 2),
  207. LastMonitor->Top + ((LastMonitor->Height - AForm->Height) / 2),
  208. AForm->Width, AForm->Height);
  209. AForm->Position = poDesigned;
  210. }
  211. else if ((AForm->Position != poDesigned) &&
  212. (AForm->Position != poDefaultPosOnly))
  213. {
  214. // we do not expect any other positioning
  215. assert(false);
  216. }
  217. }
  218. else
  219. {
  220. TForm * AForm = dynamic_cast<TForm *>(Form);
  221. assert(AForm != NULL);
  222. // otherwise it would not get centered
  223. if (AForm->Position == poMainFormCenter)
  224. {
  225. AForm->Position = poScreenCenter;
  226. }
  227. }
  228. }
  229. bool WasMainFormCenter = (AForm->Position == poMainFormCenter);
  230. WndProc(Message);
  231. // Make sure dialogs are shown on-screen even if center of the main window
  232. // is off-screen. Occurs e.g. if you move the main window so that
  233. // only window title is visible above taksbar.
  234. if (Form->Showing && WasMainFormCenter && (AForm->Position == poDesigned))
  235. {
  236. TRect Rect;
  237. // Reading Form.Left/Form.Top instead here does not work, likely due to some
  238. // bug, when querying TProgressForm opened from TEditorForm (reloading remote file)
  239. GetWindowRect(Form->Handle, &Rect);
  240. int Left = Rect.Left;
  241. int Top = Rect.Top;
  242. TRect WorkArea = AForm->Monitor->WorkareaRect;
  243. if (Left + Rect.Width() > WorkArea.Right)
  244. {
  245. Left = WorkArea.Right - Rect.Width();
  246. }
  247. if (Left < WorkArea.Left)
  248. {
  249. Left = WorkArea.Left;
  250. }
  251. if (Top + Rect.Height() > WorkArea.Bottom)
  252. {
  253. Top = WorkArea.Bottom - Rect.Height();
  254. }
  255. if (Top < WorkArea.Top)
  256. {
  257. Top = WorkArea.Top;
  258. }
  259. if ((Left != Rect.Left) ||
  260. (Top != Rect.Top))
  261. {
  262. SetWindowPos(Form->Handle, 0, Left, Top, Rect.Width(), Rect.Height(),
  263. SWP_NOZORDER + SWP_NOACTIVATE);
  264. }
  265. }
  266. }
  267. else
  268. {
  269. WndProc(Message);
  270. }
  271. }
  272. //---------------------------------------------------------------------------
  273. static void __fastcall FormWindowProc(void * Data, TMessage & Message)
  274. {
  275. TCustomForm * Form = static_cast<TCustomForm *>(Data);
  276. DoFormWindowProc(Form, ControlWndProc(Form), Message);
  277. }
  278. //---------------------------------------------------------------------------
  279. static void __fastcall FormWindowProcEx(void * Data, TMessage & Message)
  280. {
  281. TSavedSystemSettings * SSettings = static_cast<TSavedSystemSettings *>(Data);
  282. DoFormWindowProc(SSettings->Form, SSettings->OldWndProc, Message);
  283. }
  284. //---------------------------------------------------------------------------
  285. void __fastcall InitializeSystemSettings()
  286. {
  287. if (ThemeManager == NULL)
  288. {
  289. ThemeManager = new TThemeManager(Application);
  290. ThemeManager->Name = "ThemeManager";
  291. // ListView subclassing breaks TDirView
  292. ThemeManager->Options = (ThemeManager->Options >> toSubclassListView);
  293. // Speed Button subclassing on rights frame does not work somehow
  294. // and they are not used elsewhere
  295. ThemeManager->Options = (ThemeManager->Options >> toSubclassSpeedButtons);
  296. TAllowSubclassingEvent OnAllowSubclassing;
  297. ((TMethod*)&OnAllowSubclassing)->Code = ThemeManagerAllowSubclassing;
  298. ThemeManager->OnAllowSubclassing = OnAllowSubclassing;
  299. }
  300. }
  301. //---------------------------------------------------------------------------
  302. void __fastcall FinalizeSystemSettings()
  303. {
  304. if (ThemeManager != NULL)
  305. {
  306. SAFE_DESTROY(ThemeManager);
  307. }
  308. }
  309. //---------------------------------------------------------------------------
  310. // Settings that must be set as soon as possible.
  311. void __fastcall UseSystemSettingsPre(TCustomForm * Control, void ** Settings)
  312. {
  313. LocalSystemSettings(Control);
  314. TWndMethod WindowProc;
  315. if (Settings)
  316. {
  317. TSavedSystemSettings * SSettings;
  318. SSettings = new TSavedSystemSettings();
  319. *Settings = static_cast<void*>(SSettings);
  320. SSettings->Form = Control;
  321. SSettings->FontName = Control->Font->Name;
  322. SSettings->OldWndProc = Control->WindowProc;
  323. ((TMethod*)&WindowProc)->Data = SSettings;
  324. ((TMethod*)&WindowProc)->Code = FormWindowProcEx;
  325. }
  326. else
  327. {
  328. ((TMethod*)&WindowProc)->Data = Control;
  329. ((TMethod*)&WindowProc)->Code = FormWindowProc;
  330. }
  331. Control->WindowProc = WindowProc;
  332. assert(Control && Control->Font);
  333. Control->Font->Name = "MS Shell Dlg";
  334. if (Control->HelpKeyword.IsEmpty())
  335. {
  336. // temporary help keyword to enable F1 key in all forms
  337. Control->HelpKeyword = "start";
  338. }
  339. // especially on login dialog, we need to reapply themes with language change
  340. if ((ThemeManager != NULL) && ThemeManager->ThemesEnabled)
  341. {
  342. ThemeManager->CollectForms(Control);
  343. }
  344. };
  345. //---------------------------------------------------------------------------
  346. // Settings that must be set only after whole form is constructed
  347. void __fastcall UseSystemSettingsPost(TCustomForm * Control, void * Settings)
  348. {
  349. bool Flip;
  350. AnsiString FlipStr = LoadStr(FLIP_CHILDREN);
  351. Flip = !FlipStr.IsEmpty() && static_cast<bool>(StrToInt(FlipStr));
  352. if (Settings != NULL)
  353. {
  354. static_cast<TSavedSystemSettings*>(Settings)->Flipped = Flip;
  355. }
  356. if (Flip)
  357. {
  358. Control->FlipChildren(true);
  359. }
  360. ResetSystemSettings(Control);
  361. };
  362. //---------------------------------------------------------------------------
  363. void __fastcall UseSystemSettings(TCustomForm * Control, void ** Settings)
  364. {
  365. UseSystemSettingsPre(Control, Settings);
  366. UseSystemSettingsPost(Control, (Settings != NULL) ? *Settings : NULL);
  367. };
  368. //---------------------------------------------------------------------------
  369. void __fastcall ResetSystemSettings(TCustomForm * /*Control*/)
  370. {
  371. // noop
  372. }
  373. //---------------------------------------------------------------------------
  374. void __fastcall DeleteSystemSettings(TCustomForm * Control, void * Settings)
  375. {
  376. assert(Settings);
  377. TSavedSystemSettings * SSettings = static_cast<TSavedSystemSettings *>(Settings);
  378. Control->WindowProc = SSettings->OldWndProc;
  379. delete SSettings;
  380. }
  381. //---------------------------------------------------------------------------
  382. void __fastcall RevokeSystemSettings(TCustomForm * Control, void * Settings)
  383. {
  384. assert(Settings);
  385. TSavedSystemSettings* SSettings = static_cast<TSavedSystemSettings*>(Settings);
  386. if (SSettings->Flipped)
  387. {
  388. Control->FlipChildren(true);
  389. }
  390. DeleteSystemSettings(Control, Settings);
  391. };
  392. //---------------------------------------------------------------------------
  393. class TPublicForm : public TForm
  394. {
  395. friend void __fastcall ShowAsModal(TForm * Form, void *& Storage);
  396. friend void __fastcall HideAsModal(TForm * Form, void *& Storage);
  397. };
  398. //---------------------------------------------------------------------------
  399. struct TShowAsModalStorage
  400. {
  401. void * FocusWindowList;
  402. void * FocusActiveWindow;
  403. TFocusState FocusState;
  404. };
  405. //---------------------------------------------------------------------------
  406. static TCustomForm ** __fastcall FocusedForm()
  407. {
  408. return reinterpret_cast<TCustomForm **>(reinterpret_cast<char *>(Screen) + 0x78);
  409. }
  410. //---------------------------------------------------------------------------
  411. static TList * __fastcall SaveFocusedList()
  412. {
  413. return *reinterpret_cast<TList **>(reinterpret_cast<char *>(Screen) + 0x7C);
  414. }
  415. //---------------------------------------------------------------------------
  416. void __fastcall ShowAsModal(TForm * Form, void *& Storage)
  417. {
  418. SetCorrectFormParent(Form);
  419. CancelDrag();
  420. if (GetCapture() != 0) SendMessage(GetCapture(), WM_CANCELMODE, 0, 0);
  421. ReleaseCapture();
  422. (static_cast<TPublicForm*>(Form))->FFormState << fsModal;
  423. TShowAsModalStorage * AStorage = new TShowAsModalStorage;
  424. AStorage->FocusActiveWindow = GetActiveWindow();
  425. AStorage->FocusState = SaveFocusState();
  426. SaveFocusedList()->Insert(0, *FocusedForm());
  427. *FocusedForm() = Form;
  428. AStorage->FocusWindowList = DisableTaskWindows(0);
  429. Form->Show();
  430. SendMessage(Form->Handle, CM_ACTIVATE, 0, 0);
  431. Storage = AStorage;
  432. }
  433. //---------------------------------------------------------------------------
  434. void __fastcall HideAsModal(TForm * Form, void *& Storage)
  435. {
  436. assert((static_cast<TPublicForm*>(Form))->FFormState.Contains(fsModal));
  437. TShowAsModalStorage * AStorage = static_cast<TShowAsModalStorage *>(Storage);
  438. Storage = NULL;
  439. SendMessage(Form->Handle, CM_DEACTIVATE, 0, 0);
  440. if (GetActiveWindow() != Form->Handle)
  441. {
  442. AStorage->FocusActiveWindow = 0;
  443. }
  444. Form->Hide();
  445. EnableTaskWindows(AStorage->FocusWindowList);
  446. TList * ASaveFocusedList = SaveFocusedList();
  447. TCustomForm ** AFocusedForm = FocusedForm();
  448. if (ASaveFocusedList->Count > 0)
  449. {
  450. *AFocusedForm = static_cast<TCustomForm *>(ASaveFocusedList->First());
  451. ASaveFocusedList->Remove(*AFocusedForm);
  452. }
  453. else
  454. {
  455. *AFocusedForm = NULL;
  456. }
  457. if (AStorage->FocusActiveWindow != 0)
  458. {
  459. SetActiveWindow(AStorage->FocusActiveWindow);
  460. }
  461. RestoreFocusState(AStorage->FocusState);
  462. (static_cast<TPublicForm*>(Form))->FFormState >> fsModal;
  463. delete AStorage;
  464. }
  465. //---------------------------------------------------------------------------
  466. void __fastcall ReleaseAsModal(TForm * Form, void *& Storage)
  467. {
  468. if (Storage != NULL)
  469. {
  470. HideAsModal(Form, Storage);
  471. }
  472. }
  473. //---------------------------------------------------------------------------
  474. bool __fastcall SelectDirectory(AnsiString & Path, const AnsiString Prompt,
  475. bool PreserveFileName)
  476. {
  477. bool Result;
  478. unsigned int ErrorMode;
  479. ErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS);
  480. try
  481. {
  482. AnsiString Directory;
  483. AnsiString FileName;
  484. if (!PreserveFileName || DirectoryExists(Path))
  485. {
  486. Directory = Path;
  487. }
  488. else
  489. {
  490. Directory = ExtractFilePath(Path);
  491. FileName = ExtractFileName(Path);
  492. }
  493. Result = SelectDirectory(Prompt, "", Directory);
  494. if (Result)
  495. {
  496. Path = Directory;
  497. if (!FileName.IsEmpty())
  498. {
  499. Path = IncludeTrailingBackslash(Path) + FileName;
  500. }
  501. }
  502. }
  503. __finally
  504. {
  505. SetErrorMode(ErrorMode);
  506. }
  507. return Result;
  508. }
  509. //---------------------------------------------------------------------------
  510. bool __fastcall ListViewAnyChecked(TListView * ListView, bool Checked)
  511. {
  512. bool AnyChecked = false;
  513. for (int Index = 0; Index < ListView->Items->Count; Index++)
  514. {
  515. if (ListView->Items->Item[Index]->Checked == Checked)
  516. {
  517. AnyChecked = true;
  518. break;
  519. }
  520. }
  521. return AnyChecked;
  522. }
  523. //---------------------------------------------------------------------------
  524. void __fastcall ListViewCheckAll(TListView * ListView,
  525. TListViewCheckAll CheckAll)
  526. {
  527. bool Check;
  528. if (CheckAll == caToggle)
  529. {
  530. Check = ListViewAnyChecked(ListView, false);
  531. }
  532. else
  533. {
  534. Check = (CheckAll == caCheck);
  535. }
  536. for (int Index = 0; Index < ListView->Items->Count; Index++)
  537. {
  538. ListView->Items->Item[Index]->Checked = Check;
  539. }
  540. }
  541. //---------------------------------------------------------------------------
  542. void __fastcall ComboAutoSwitchInitialize(TComboBox * ComboBox)
  543. {
  544. int PrevIndex = ComboBox->ItemIndex;
  545. ComboBox->Items->BeginUpdate();
  546. try
  547. {
  548. ComboBox->Clear();
  549. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_AUTO));
  550. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_OFF));
  551. ComboBox->Items->Add(LoadStr(AUTO_SWITCH_ON));
  552. }
  553. __finally
  554. {
  555. ComboBox->Items->EndUpdate();
  556. }
  557. assert(PrevIndex < ComboBox->Items->Count);
  558. ComboBox->ItemIndex = PrevIndex;
  559. }
  560. //---------------------------------------------------------------------------
  561. void __fastcall ComboAutoSwitchLoad(TComboBox * ComboBox, TAutoSwitch Value)
  562. {
  563. ComboBox->ItemIndex = 2 - Value;
  564. if (ComboBox->ItemIndex < 0)
  565. {
  566. ComboBox->ItemIndex = 0;
  567. }
  568. }
  569. //---------------------------------------------------------------------------
  570. TAutoSwitch __fastcall ComboAutoSwitchSave(TComboBox * ComboBox)
  571. {
  572. return (TAutoSwitch)(2 - ComboBox->ItemIndex);
  573. }
  574. //---------------------------------------------------------------------------
  575. void __fastcall CheckBoxAutoSwitchLoad(TCheckBox * CheckBox, TAutoSwitch Value)
  576. {
  577. switch (Value)
  578. {
  579. case asOn:
  580. CheckBox->State = cbChecked;
  581. break;
  582. case asOff:
  583. CheckBox->State = cbUnchecked;
  584. break;
  585. default:
  586. CheckBox->State = cbGrayed;
  587. break;
  588. }
  589. }
  590. //---------------------------------------------------------------------------
  591. TAutoSwitch __fastcall CheckBoxAutoSwitchSave(TCheckBox * CheckBox)
  592. {
  593. switch (CheckBox->State)
  594. {
  595. case cbChecked:
  596. return asOn;
  597. case cbUnchecked:
  598. return asOff;
  599. default:
  600. return asAuto;
  601. }
  602. }
  603. //---------------------------------------------------------------------------
  604. // Windows algorithm is as follows (tested on W2k):
  605. // right:
  606. // is_delimiter(current)
  607. // false:
  608. // right(left(current) + 1)
  609. // true:
  610. // right(right(current) + 1)
  611. // left:
  612. // right(left(current) + 1)
  613. int CALLBACK PathWordBreakProc(char * Ch, int Current, int Len, int Code)
  614. {
  615. char Delimiters[] = "\\/ ;,.";
  616. int Result;
  617. AnsiString ACh;
  618. // stupid unicode autodetection
  619. // (on WinXP (or rather for RichEdit 2.0) we get unicode on input)
  620. if ((Len > 1) && (Ch[1] == '\0'))
  621. {
  622. // this convertes the unicode to ansi
  623. ACh = (wchar_t*)Ch;
  624. }
  625. else
  626. {
  627. ACh = Ch;
  628. }
  629. // it may not be NULL terminated
  630. ACh.SetLength(Len);
  631. if (Code == WB_ISDELIMITER)
  632. {
  633. // we return negacy of what WinAPI docs says
  634. Result = (strchr(Delimiters, ACh[Current + 1]) == NULL);
  635. }
  636. else if (Code == WB_LEFT)
  637. {
  638. Result = ACh.SubString(1, Current - 1).LastDelimiter(Delimiters);
  639. }
  640. else if (Code == WB_RIGHT)
  641. {
  642. if (Current == 0)
  643. {
  644. // will be called again with Current == 1
  645. Result = 0;
  646. }
  647. else
  648. {
  649. const char * P = strpbrk(ACh.c_str() + Current - 1, Delimiters);
  650. if (P == NULL)
  651. {
  652. Result = Len;
  653. }
  654. else
  655. {
  656. Result = P - ACh.c_str() + 1;
  657. }
  658. }
  659. }
  660. else
  661. {
  662. assert(false);
  663. Result = 0;
  664. }
  665. return Result;
  666. }
  667. //---------------------------------------------------------------------------
  668. class TPublicCustomCombo : public TCustomCombo
  669. {
  670. friend void __fastcall InstallPathWordBreakProc(TWinControl * Control);
  671. };
  672. //---------------------------------------------------------------------------
  673. void __fastcall InstallPathWordBreakProc(TWinControl * Control)
  674. {
  675. HWND Wnd;
  676. if (dynamic_cast<TCustomCombo*>(Control) != NULL)
  677. {
  678. TPublicCustomCombo * Combo =
  679. static_cast<TPublicCustomCombo *>(dynamic_cast<TCustomCombo *>(Control));
  680. Combo->HandleNeeded();
  681. Wnd = Combo->EditHandle;
  682. }
  683. else
  684. {
  685. Wnd = Control->Handle;
  686. }
  687. SendMessage(Wnd, EM_SETWORDBREAKPROC, 0, (LPARAM)(EDITWORDBREAKPROC)PathWordBreakProc);
  688. }
  689. //---------------------------------------------------------------------------
  690. static void __fastcall RemoveHiddenControlsFromOrder(TControl ** ControlsOrder, int & Count)
  691. {
  692. int Shift = 0;
  693. for (int Index = 0; Index < Count; Index++)
  694. {
  695. if (ControlsOrder[Index]->Visible)
  696. {
  697. ControlsOrder[Index - Shift] = ControlsOrder[Index];
  698. }
  699. else
  700. {
  701. Shift++;
  702. }
  703. }
  704. Count -= Shift;
  705. }
  706. //---------------------------------------------------------------------------
  707. void __fastcall RepaintStatusBar(TCustomStatusBar * StatusBar)
  708. {
  709. StatusBar->SimplePanel = !StatusBar->SimplePanel;
  710. StatusBar->SimplePanel = !StatusBar->SimplePanel;
  711. }
  712. //---------------------------------------------------------------------------
  713. void __fastcall SetVerticalControlsOrder(TControl ** ControlsOrder, int Count)
  714. {
  715. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  716. if (Count > 0)
  717. {
  718. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  719. CommonParent->DisableAlign();
  720. try
  721. {
  722. int Top = 0;
  723. for (int Index = 0; Index < Count; Index++)
  724. {
  725. assert(ControlsOrder[Index]->Parent == CommonParent);
  726. if ((Index == 0) || (Top > ControlsOrder[Index]->Top))
  727. {
  728. Top = ControlsOrder[Index]->Top;
  729. }
  730. }
  731. for (int Index = 0; Index < Count; Index++)
  732. {
  733. TControl * Control = ControlsOrder[Index];
  734. Control->Top = Top;
  735. if (((Control->Align == alTop) || (Control->Align == alBottom)) ||
  736. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom)))
  737. {
  738. Top += Control->Height;
  739. }
  740. }
  741. }
  742. __finally
  743. {
  744. CommonParent->EnableAlign();
  745. }
  746. }
  747. }
  748. //---------------------------------------------------------------------------
  749. void __fastcall SetHorizontalControlsOrder(TControl ** ControlsOrder, int Count)
  750. {
  751. RemoveHiddenControlsFromOrder(ControlsOrder, Count);
  752. if (Count > 0)
  753. {
  754. TWinControl * CommonParent = ControlsOrder[0]->Parent;
  755. CommonParent->DisableAlign();
  756. try
  757. {
  758. int Left = 0;
  759. for (int Index = 0; Index < Count; Index++)
  760. {
  761. assert(ControlsOrder[Index]->Parent == CommonParent);
  762. if ((Index == 0) || (Left > ControlsOrder[Index]->Left))
  763. {
  764. Left = ControlsOrder[Index]->Left;
  765. }
  766. }
  767. for (int Index = 0; Index < Count; Index++)
  768. {
  769. TControl * Control = ControlsOrder[Index];
  770. Control->Left = Left;
  771. if (((Control->Align == alLeft) || (Control->Align == alRight)) ||
  772. ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alRight)))
  773. {
  774. Left += Control->Width;
  775. }
  776. // vertical alignment has priority, so alBottom-aligned controls start
  777. // at the very left, even if there are any alLeft/alRight controls.
  778. // for the reason this code is not necessary in SetVerticalControlsOrder.
  779. // we could exit the loop as well here.
  780. if ((Index == Count - 1) || (ControlsOrder[Index + 1]->Align == alBottom))
  781. {
  782. Left = 0;
  783. }
  784. }
  785. }
  786. __finally
  787. {
  788. CommonParent->EnableAlign();
  789. }
  790. }
  791. }
  792. //---------------------------------------------------------------------------
  793. TPoint __fastcall GetAveCharSize(TCanvas* Canvas)
  794. {
  795. Integer I;
  796. Char Buffer[52];
  797. TSize Result;
  798. for (I = 0; I <= 25; I++) Buffer[I] = (Char)('A' + I);
  799. for (I = 0; I <= 25; I++) Buffer[I+26] = (Char)('a' + I);
  800. GetTextExtentPoint(Canvas->Handle, Buffer, 52, &Result);
  801. return TPoint(Result.cx / 52, Result.cy);
  802. }
  803. //---------------------------------------------------------------------------
  804. void __fastcall MakeNextInTabOrder(TWinControl * Control, TWinControl * After)
  805. {
  806. if (After->TabOrder > Control->TabOrder)
  807. {
  808. After->TabOrder = Control->TabOrder;
  809. }
  810. else if (After->TabOrder < Control->TabOrder - 1)
  811. {
  812. After->TabOrder = static_cast<TTabOrder>(Control->TabOrder - 1);
  813. }
  814. }
  815. //---------------------------------------------------------------------------
  816. void __fastcall CutFormToDesktop(TForm * Form)
  817. {
  818. assert(Form->Monitor != NULL);
  819. TRect Workarea = Form->Monitor->WorkareaRect;
  820. if (Form->Top + Form->Height > Workarea.Bottom)
  821. {
  822. Form->Height = Workarea.Bottom - Form->Top;
  823. }
  824. if (Form->Left + Form->Width >= Workarea.Right)
  825. {
  826. Form->Width = Workarea.Right - Form->Left;
  827. }
  828. }
  829. //---------------------------------------------------------------------------
  830. void __fastcall UpdateFormPosition(TCustomForm * Form, TPosition Position)
  831. {
  832. if ((Position == poScreenCenter) ||
  833. (Position == poOwnerFormCenter) ||
  834. (Position == poMainFormCenter))
  835. {
  836. TCustomForm * CenterForm = NULL;
  837. if ((Position == poOwnerFormCenter) ||
  838. (Position == poMainFormCenter))
  839. {
  840. CenterForm = Application->MainForm;
  841. if ((Position == poOwnerFormCenter) &&
  842. (dynamic_cast<TCustomForm*>(Form->Owner) != NULL))
  843. {
  844. CenterForm = dynamic_cast<TCustomForm*>(Form->Owner);
  845. }
  846. }
  847. TRect Bounds = Form->BoundsRect;
  848. int X, Y;
  849. if (CenterForm != NULL)
  850. {
  851. X = ((((TForm *)CenterForm)->Width - Bounds.Width()) / 2) +
  852. ((TForm *)CenterForm)->Left;
  853. Y = ((((TForm *)CenterForm)->Height - Bounds.Height()) / 2) +
  854. ((TForm *)CenterForm)->Top;
  855. }
  856. else
  857. {
  858. X = (Screen->Width - Bounds.Width()) / 2;
  859. Y = (Screen->Height - Bounds.Height()) / 2;
  860. }
  861. if (X < 0)
  862. {
  863. X = 0;
  864. }
  865. if (Y < 0)
  866. {
  867. Y = 0;
  868. }
  869. Form->SetBounds(X, Y, Bounds.Width(), Bounds.Height());
  870. }
  871. }
  872. //---------------------------------------------------------------------------
  873. void __fastcall ResizeForm(TCustomForm * Form, int Width, int Height)
  874. {
  875. if (Height > Screen->WorkAreaHeight)
  876. {
  877. Height = Screen->WorkAreaHeight;
  878. }
  879. if (Width > Screen->WorkAreaWidth)
  880. {
  881. Width = Screen->WorkAreaWidth;
  882. }
  883. if (Height < Form->Constraints->MinHeight)
  884. {
  885. Height = Form->Constraints->MinHeight;
  886. }
  887. if (Width < Form->Constraints->MinWidth)
  888. {
  889. Width = Form->Constraints->MinWidth;
  890. }
  891. TRect Bounds = Form->BoundsRect;
  892. int Top = Bounds.Top + ((Bounds.Height() - Height) / 2);
  893. int Left = Bounds.Left + ((Bounds.Width() - Width) / 2);
  894. if (Top + Height > Screen->WorkAreaTop + Screen->WorkAreaHeight)
  895. {
  896. Top = Screen->WorkAreaTop + Screen->WorkAreaHeight - Height;
  897. }
  898. if (Left + Width >= Screen->WorkAreaLeft + Screen->WorkAreaWidth)
  899. {
  900. Left = Screen->WorkAreaLeft + Screen->WorkAreaWidth - Width;
  901. }
  902. if (Top < 0)
  903. {
  904. Top = 0;
  905. }
  906. if (Left < 0)
  907. {
  908. Left = 0;
  909. }
  910. Form->SetBounds(Left, Top, Width, Height);
  911. Bounds = Form->BoundsRect;
  912. // due to constraints, form can remain larger, make sure it is centered although
  913. Left = Bounds.Left + ((Width - Bounds.Width()) / 2);
  914. Top = Bounds.Top + ((Height - Bounds.Height()) / 2);
  915. Form->SetBounds(Left, Top, Width, Height);
  916. }
  917. //---------------------------------------------------------------------------
  918. HWND __fastcall GetCorrectFormParent()
  919. {
  920. HWND Result = NULL;
  921. // Kind of hack (i do not understand this much).
  922. // Rationale: for example when the preferences window is opened from login dialog
  923. // settings Parent to Screen->ActiveForm leads to "cannot focus disabled control",
  924. // so we set Parent only when absolutelly necessary
  925. // (dialog opened from log window or editor)
  926. // TODO: does not work for dialogs opened from preferences dialog
  927. if ((Application->MainForm != NULL) &&
  928. (Application->MainForm != Screen->ActiveForm))
  929. {
  930. if (Screen->ActiveForm != NULL)
  931. {
  932. // the case when we are invoking dialog from (e.g. prefences) dialog
  933. // invokend form non modal window (e.g. editor)
  934. if (Screen->ActiveForm->ParentWindow != NULL)
  935. {
  936. Result = Screen->ActiveForm->Handle;
  937. }
  938. // this should better be check for modal form
  939. else if (Screen->ActiveForm->BorderStyle != bsDialog)
  940. {
  941. Result = Screen->ActiveForm->Handle;
  942. }
  943. }
  944. }
  945. return Result;
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall SetCorrectFormParent(TForm * Form)
  949. {
  950. try
  951. {
  952. HWND Parent = GetCorrectFormParent();
  953. if (Parent != NULL)
  954. {
  955. Form->ParentWindow = Parent;
  956. }
  957. }
  958. catch(...)
  959. {
  960. // avoid any errors, however we want to know about this in debug version.
  961. #ifdef _DEBUG
  962. throw;
  963. #endif
  964. }
  965. }
  966. //---------------------------------------------------------------------------
  967. void __fastcall InvokeHelp(TWinControl * Control)
  968. {
  969. assert(Control != NULL);
  970. HELPINFO HelpInfo;
  971. HelpInfo.cbSize = sizeof(HelpInfo);
  972. HelpInfo.iContextType = HELPINFO_WINDOW;
  973. HelpInfo.iCtrlId = 0;
  974. HelpInfo.hItemHandle = Control->Handle;
  975. HelpInfo.dwContextId = 0;
  976. HelpInfo.MousePos.x = 0;
  977. HelpInfo.MousePos.y = 0;
  978. SendMessage(Control->Handle, WM_HELP, NULL, reinterpret_cast<long>(&HelpInfo));
  979. }
  980. //---------------------------------------------------------------------------
  981. //---------------------------------------------------------------------------
  982. static void __fastcall FocusableLabelCanvas(TStaticText * StaticText,
  983. TControlCanvas ** ACanvas, TRect & R)
  984. {
  985. TControlCanvas * Canvas = new TControlCanvas();
  986. try
  987. {
  988. Canvas->Control = StaticText;
  989. R = StaticText->ClientRect;
  990. AnsiString Caption = StaticText->Caption;
  991. bool AccelChar = false;
  992. if (StaticText->ShowAccelChar)
  993. {
  994. Caption = StripHotkey(Caption);
  995. AccelChar = (Caption != StaticText->Caption);
  996. }
  997. TSize TextSize = Canvas->TextExtent(Caption);
  998. assert(StaticText->BorderStyle == sbsNone); // not taken into account
  999. if (AccelChar)
  1000. {
  1001. TextSize.cy += 2;
  1002. }
  1003. R.Bottom = R.Top + TextSize.cy;
  1004. switch (StaticText->Alignment)
  1005. {
  1006. case taLeftJustify:
  1007. R.Right = R.Left + TextSize.cx;
  1008. break;
  1009. case taRightJustify:
  1010. R.Left = R.Right - TextSize.cx;
  1011. break;
  1012. case taCenter:
  1013. {
  1014. int Diff = R.Width() - TextSize.cx;
  1015. R.Left += Diff / 2;
  1016. R.Right -= Diff - (Diff / 2);
  1017. }
  1018. break;
  1019. }
  1020. }
  1021. __finally
  1022. {
  1023. if (ACanvas == NULL)
  1024. {
  1025. delete Canvas;
  1026. }
  1027. }
  1028. if (ACanvas != NULL)
  1029. {
  1030. *ACanvas = Canvas;
  1031. }
  1032. }
  1033. //---------------------------------------------------------------------------
  1034. static void __fastcall FocusableLabelWindowProc(void * Data, TMessage & Message,
  1035. bool & Clicked)
  1036. {
  1037. Clicked = false;
  1038. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1039. if (Message.Msg == WM_LBUTTONDOWN)
  1040. {
  1041. StaticText->SetFocus();
  1042. // in case the action takes long, make sure focus is shown immediatelly
  1043. UpdateWindow(StaticText->Handle);
  1044. Clicked = true;
  1045. Message.Result = 1;
  1046. }
  1047. else if (Message.Msg == WM_RBUTTONDOWN)
  1048. {
  1049. StaticText->SetFocus();
  1050. Message.Result = 1;
  1051. }
  1052. else if (Message.Msg == WM_CHAR)
  1053. {
  1054. if (reinterpret_cast<TWMChar &>(Message).CharCode == ' ')
  1055. {
  1056. Clicked = true;
  1057. Message.Result = 1;
  1058. }
  1059. else
  1060. {
  1061. ControlWndProc(StaticText)(Message);
  1062. }
  1063. }
  1064. else if (Message.Msg == CM_DIALOGCHAR)
  1065. {
  1066. if (StaticText->CanFocus() && StaticText->ShowAccelChar &&
  1067. IsAccel(reinterpret_cast<TCMDialogChar &>(Message).CharCode, StaticText->Caption))
  1068. {
  1069. StaticText->SetFocus();
  1070. // in case the action takes long, make sure focus is shown immediatelly
  1071. UpdateWindow(StaticText->Handle);
  1072. Clicked = true;
  1073. Message.Result = 1;
  1074. }
  1075. else
  1076. {
  1077. ControlWndProc(StaticText)(Message);
  1078. }
  1079. }
  1080. else
  1081. {
  1082. ControlWndProc(StaticText)(Message);
  1083. }
  1084. if (Message.Msg == WM_PAINT)
  1085. {
  1086. TRect R;
  1087. TControlCanvas * Canvas;
  1088. FocusableLabelCanvas(StaticText, &Canvas, R);
  1089. try
  1090. {
  1091. if (StaticText->Focused())
  1092. {
  1093. Canvas->DrawFocusRect(R);
  1094. }
  1095. else if (!StaticText->Font->Style.Contains(fsUnderline))
  1096. {
  1097. Canvas->Pen->Style = psDot;
  1098. Canvas->Brush->Style = bsClear;
  1099. if (!StaticText->Enabled)
  1100. {
  1101. Canvas->Pen->Color = clBtnHighlight;
  1102. Canvas->MoveTo(R.Left + 1 + 1, R.Bottom);
  1103. Canvas->LineTo(R.Right + 1, R.Bottom);
  1104. Canvas->Pen->Color = clGrayText;
  1105. }
  1106. Canvas->MoveTo(R.Left + 1, R.Bottom - 1);
  1107. Canvas->LineTo(R.Right, R.Bottom - 1);
  1108. }
  1109. }
  1110. __finally
  1111. {
  1112. delete Canvas;
  1113. }
  1114. }
  1115. else if ((Message.Msg == WM_SETFOCUS) || (Message.Msg == WM_KILLFOCUS) ||
  1116. (Message.Msg == CM_ENABLEDCHANGED))
  1117. {
  1118. StaticText->Invalidate();
  1119. }
  1120. }
  1121. //---------------------------------------------------------------------------
  1122. static THintWindow * PersistentHintWindow = NULL;
  1123. static TControl * PersistentHintControl = NULL;
  1124. //---------------------------------------------------------------------------
  1125. void __fastcall CancelPersistentHint()
  1126. {
  1127. if (PersistentHintWindow != NULL)
  1128. {
  1129. PersistentHintControl = NULL;
  1130. SAFE_DESTROY(PersistentHintWindow);
  1131. }
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void __fastcall ShowPersistentHint(TControl * Control, TPoint HintPos)
  1135. {
  1136. CancelPersistentHint();
  1137. THintInfo HintInfo;
  1138. HintInfo.HintControl = Control;
  1139. HintInfo.HintPos = HintPos;
  1140. HintInfo.HintMaxWidth = Screen->Width;
  1141. HintInfo.HintColor = Application->HintColor;
  1142. HintInfo.HintStr = GetShortHint(Control->Hint);
  1143. HintInfo.HintData = NULL;
  1144. bool CanShow = true;
  1145. if (Application->OnShowHint != NULL)
  1146. {
  1147. Application->OnShowHint(HintInfo.HintStr, CanShow, HintInfo);
  1148. }
  1149. if (CanShow)
  1150. {
  1151. PersistentHintControl = Control;
  1152. PersistentHintWindow = new THintWindow(Application);
  1153. PersistentHintWindow->BiDiMode = Control->BiDiMode;
  1154. PersistentHintWindow->Color = HintInfo.HintColor;
  1155. TRect HintWinRect;
  1156. if (HintInfo.HintMaxWidth < Control->Width)
  1157. {
  1158. HintInfo.HintMaxWidth = Control->Width;
  1159. }
  1160. HintWinRect = PersistentHintWindow->CalcHintRect(
  1161. HintInfo.HintMaxWidth, HintInfo.HintStr, HintInfo.HintData);
  1162. OffsetRect(HintWinRect, HintInfo.HintPos.x, HintInfo.HintPos.y);
  1163. // TODO: right align window placement for UseRightToLeftAlignment, see Forms.pas
  1164. PersistentHintWindow->ActivateHintData(HintWinRect, HintInfo.HintStr, HintInfo.HintData);
  1165. }
  1166. }
  1167. //---------------------------------------------------------------------------
  1168. static void __fastcall HintLabelWindowProc(void * Data, TMessage & Message)
  1169. {
  1170. bool Clicked = false;
  1171. bool Cancel = false;
  1172. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1173. if (Message.Msg == CM_HINTSHOW)
  1174. {
  1175. TCMHintShow & HintShow = reinterpret_cast<TCMHintShow &>(Message);
  1176. if (PersistentHintControl == StaticText)
  1177. {
  1178. // do not allow standard hint when persistent is already shown
  1179. HintShow.Result = 1;
  1180. }
  1181. else
  1182. {
  1183. HintShow.HintInfo->HideTimeout = 100000; // never
  1184. }
  1185. }
  1186. else if (Message.Msg == CN_KEYDOWN)
  1187. {
  1188. if ((reinterpret_cast<TWMKey &>(Message).CharCode == VK_ESCAPE) &&
  1189. (PersistentHintControl == StaticText))
  1190. {
  1191. CancelPersistentHint();
  1192. StaticText->Invalidate();
  1193. Message.Result = 1;
  1194. }
  1195. else
  1196. {
  1197. FocusableLabelWindowProc(Data, Message, Clicked);
  1198. }
  1199. }
  1200. else
  1201. {
  1202. FocusableLabelWindowProc(Data, Message, Clicked);
  1203. }
  1204. if (Message.Msg == CM_CANCELMODE)
  1205. {
  1206. TCMCancelMode & CancelMessage = (TCMCancelMode&)Message;
  1207. if ((CancelMessage.Sender != StaticText) &&
  1208. (CancelMessage.Sender != PersistentHintWindow))
  1209. {
  1210. Cancel = true;
  1211. }
  1212. }
  1213. if ((Message.Msg == WM_DESTROY) || (Message.Msg == WM_KILLFOCUS))
  1214. {
  1215. Cancel = true;
  1216. }
  1217. if (Cancel && (PersistentHintControl == StaticText))
  1218. {
  1219. CancelPersistentHint();
  1220. }
  1221. if (Clicked && (PersistentHintControl != StaticText))
  1222. {
  1223. TRect R;
  1224. TPoint HintPos;
  1225. FocusableLabelCanvas(StaticText, NULL, R);
  1226. HintPos.y = R.Bottom - R.Top;
  1227. HintPos.x = R.Left;
  1228. ShowPersistentHint(StaticText, StaticText->ClientToScreen(HintPos));
  1229. }
  1230. }
  1231. //---------------------------------------------------------------------------
  1232. void __fastcall HintLabel(TStaticText * StaticText, AnsiString Hint)
  1233. {
  1234. StaticText->ParentFont = true;
  1235. if (!Hint.IsEmpty())
  1236. {
  1237. StaticText->Hint = Hint;
  1238. }
  1239. StaticText->ShowHint = true;
  1240. StaticText->Cursor = crHandPoint;
  1241. TWndMethod WindowProc;
  1242. ((TMethod*)&WindowProc)->Data = StaticText;
  1243. ((TMethod*)&WindowProc)->Code = HintLabelWindowProc;
  1244. StaticText->WindowProc = WindowProc;
  1245. }
  1246. //---------------------------------------------------------------------------
  1247. void __fastcall HintLabelRestore(TStaticText * StaticText)
  1248. {
  1249. StaticText->WindowProc = ControlWndProc(StaticText);
  1250. StaticText->ShowHint = false;
  1251. StaticText->Cursor = crDefault;
  1252. }
  1253. //---------------------------------------------------------------------------
  1254. static void __fastcall ComboBoxFixWindowProc(void * Data, TMessage & Message)
  1255. {
  1256. // it is TCustomComboxBox, but the properties are published only by TComboBox
  1257. TComboBox * ComboBox = static_cast<TComboBox *>(Data);
  1258. if (Message.Msg == WM_SIZE)
  1259. {
  1260. AnsiString Text = ComboBox->Text;
  1261. try
  1262. {
  1263. ControlWndProc(ComboBox)(Message);
  1264. }
  1265. __finally
  1266. {
  1267. // workaround for bug in combo box, that causes it to change text to any
  1268. // item from drop down list which starts with current text,
  1269. // after control is resized (unless the text is in drop down list as well)
  1270. ComboBox->Text = Text;
  1271. // hide selection, which is wrongly shown when form is resized, even when the box has not focus
  1272. if (!ComboBox->Focused())
  1273. {
  1274. ComboBox->SelLength = 0;
  1275. }
  1276. }
  1277. }
  1278. else
  1279. {
  1280. ControlWndProc(ComboBox)(Message);
  1281. }
  1282. }
  1283. //---------------------------------------------------------------------------
  1284. void __fastcall FixComboBoxResizeBug(TCustomComboBox * ComboBox)
  1285. {
  1286. TWndMethod WindowProc;
  1287. ((TMethod*)&WindowProc)->Data = ComboBox;
  1288. ((TMethod*)&WindowProc)->Code = ComboBoxFixWindowProc;
  1289. ComboBox->WindowProc = WindowProc;
  1290. }
  1291. //---------------------------------------------------------------------------
  1292. static void __fastcall LinkLabelClick(TStaticText * StaticText)
  1293. {
  1294. if (StaticText->OnClick != NULL)
  1295. {
  1296. StaticText->OnClick(StaticText);
  1297. }
  1298. else
  1299. {
  1300. AnsiString Url = StaticText->Caption;
  1301. if (!AnsiSameText(Url.SubString(1, 4), "http") && (Url.Pos("@") > 0))
  1302. {
  1303. Url = "mailto:" + Url;
  1304. }
  1305. OpenBrowser(Url);
  1306. }
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. static void __fastcall LinkLabelWindowProc(void * Data, TMessage & Message)
  1310. {
  1311. bool Clicked = false;
  1312. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1313. if (Message.Msg == WM_CONTEXTMENU)
  1314. {
  1315. TWMContextMenu & ContextMenu = reinterpret_cast<TWMContextMenu &>(Message);
  1316. if ((ContextMenu.Pos.x < 0) && (ContextMenu.Pos.y < 0))
  1317. {
  1318. TRect R;
  1319. FocusableLabelCanvas(StaticText, NULL, R);
  1320. TPoint P = StaticText->ClientToScreen(TPoint(R.Left, R.Bottom));
  1321. ContextMenu.Pos.x = static_cast<short>(P.x);
  1322. ContextMenu.Pos.y = static_cast<short>(P.y);
  1323. }
  1324. }
  1325. else if (Message.Msg == WM_KEYDOWN)
  1326. {
  1327. TWMKey & Key = reinterpret_cast<TWMKey &>(Message);
  1328. if ((GetKeyState(VK_CONTROL) < 0) && (Key.CharCode == 'C'))
  1329. {
  1330. CopyToClipboard(StaticText->Caption);
  1331. Message.Result = 1;
  1332. }
  1333. else
  1334. {
  1335. FocusableLabelWindowProc(Data, Message, Clicked);
  1336. }
  1337. }
  1338. FocusableLabelWindowProc(Data, Message, Clicked);
  1339. if (Message.Msg == WM_DESTROY)
  1340. {
  1341. delete StaticText->PopupMenu;
  1342. assert(StaticText->PopupMenu == NULL);
  1343. }
  1344. if (Clicked)
  1345. {
  1346. LinkLabelClick(StaticText);
  1347. }
  1348. }
  1349. //---------------------------------------------------------------------------
  1350. static void __fastcall LinkLabelContextMenuClick(void * Data, TObject * Sender)
  1351. {
  1352. TStaticText * StaticText = static_cast<TStaticText *>(Data);
  1353. TMenuItem * MenuItem = dynamic_cast<TMenuItem *>(Sender);
  1354. assert(MenuItem != NULL);
  1355. if (MenuItem->Tag == 0)
  1356. {
  1357. LinkLabelClick(StaticText);
  1358. }
  1359. else
  1360. {
  1361. CopyToClipboard(StaticText->Caption);
  1362. }
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. void __fastcall LinkLabel(TStaticText * StaticText, AnsiString Url,
  1366. TNotifyEvent OnEnter)
  1367. {
  1368. StaticText->ParentFont = true;
  1369. StaticText->Font->Style = StaticText->Font->Style << fsUnderline;
  1370. StaticText->Font->Color = clBlue;
  1371. StaticText->Cursor = crHandPoint;
  1372. reinterpret_cast<TButton*>(StaticText)->OnEnter = OnEnter;
  1373. if (!Url.IsEmpty())
  1374. {
  1375. StaticText->Caption = Url;
  1376. }
  1377. if (StaticText->OnClick == NULL)
  1378. {
  1379. assert(StaticText->PopupMenu == NULL);
  1380. StaticText->PopupMenu = new TPopupMenu(StaticText);
  1381. try
  1382. {
  1383. TNotifyEvent ContextMenuOnClick;
  1384. ((TMethod*)&ContextMenuOnClick)->Data = StaticText;
  1385. ((TMethod*)&ContextMenuOnClick)->Code = LinkLabelContextMenuClick;
  1386. TMenuItem * Item;
  1387. Item = new TMenuItem(StaticText->PopupMenu);
  1388. Item->Caption = LoadStr(URL_LINK_OPEN);
  1389. Item->Tag = 0;
  1390. Item->ShortCut = ShortCut(' ', TShiftState());
  1391. Item->OnClick = ContextMenuOnClick;
  1392. StaticText->PopupMenu->Items->Add(Item);
  1393. Item = new TMenuItem(StaticText->PopupMenu);
  1394. Item->Caption = LoadStr(URL_LINK_COPY);
  1395. Item->Tag = 1;
  1396. Item->ShortCut = ShortCut('C', TShiftState() << ssCtrl);
  1397. Item->OnClick = ContextMenuOnClick;
  1398. StaticText->PopupMenu->Items->Add(Item);
  1399. }
  1400. catch(...)
  1401. {
  1402. delete StaticText->PopupMenu;
  1403. assert(StaticText->PopupMenu == NULL);
  1404. throw;
  1405. }
  1406. }
  1407. TWndMethod WindowProc;
  1408. ((TMethod*)&WindowProc)->Data = StaticText;
  1409. ((TMethod*)&WindowProc)->Code = LinkLabelWindowProc;
  1410. StaticText->WindowProc = WindowProc;
  1411. }
  1412. //---------------------------------------------------------------------------
  1413. TMonitor * __fastcall FormMonitor(TCustomForm * Form)
  1414. {
  1415. TMonitor * Result;
  1416. if ((Application->MainForm != NULL) && (Application->MainForm != Form))
  1417. {
  1418. Result = Application->MainForm->Monitor;
  1419. }
  1420. else if (LastMonitor != NULL)
  1421. {
  1422. Result = LastMonitor;
  1423. }
  1424. else
  1425. {
  1426. int i = 0;
  1427. while ((i < Screen->MonitorCount) && !Screen->Monitors[i]->Primary)
  1428. {
  1429. i++;
  1430. }
  1431. assert(Screen->Monitors[i]->Primary);
  1432. Result = Screen->Monitors[i];
  1433. }
  1434. return Result;
  1435. }
  1436. //---------------------------------------------------------------------------
  1437. int __fastcall GetLastMonitor()
  1438. {
  1439. if (LastMonitor != NULL)
  1440. {
  1441. return LastMonitor->MonitorNum;
  1442. }
  1443. else
  1444. {
  1445. return -1;
  1446. }
  1447. }
  1448. //---------------------------------------------------------------------------
  1449. void __fastcall SetLastMonitor(int MonitorNum)
  1450. {
  1451. if ((MonitorNum >= 0) && (MonitorNum < Screen->MonitorCount))
  1452. {
  1453. LastMonitor = Screen->Monitors[MonitorNum];
  1454. }
  1455. else
  1456. {
  1457. LastMonitor = NULL;
  1458. }
  1459. }
  1460. //---------------------------------------------------------------------------
  1461. TForm * __fastcall _SafeFormCreate(TMetaClass * FormClass, TComponent * Owner)
  1462. {
  1463. // we do ignore owner atm, as we do not know how to set it,
  1464. // but it should not cause any problems as we always manage memory
  1465. // destruction ourselves
  1466. assert(Owner == Application);
  1467. USEDPARAM(Owner);
  1468. TForm * Form;
  1469. // if there is no main form yet, make this one main.
  1470. // this, among other, makes other forms (dialogs invoked from this one),
  1471. // be placed on the same monitor (otherwise all new forms get placed
  1472. // on primary monitor)
  1473. if (Application->MainForm == NULL)
  1474. {
  1475. Application->CreateForm(FormClass, &Form);
  1476. assert(Application->MainForm == Form);
  1477. }
  1478. else
  1479. {
  1480. Form = dynamic_cast<TForm *>(Construct(FormClass, Application));
  1481. assert(Form != NULL);
  1482. }
  1483. return Form;
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. TImageList * __fastcall SharedSystemImageList(bool Large)
  1487. {
  1488. TSHFileInfo FileInfo;
  1489. TImageList * Result = new TImageList(Application);
  1490. int ImageListHandle = SHGetFileInfo("", 0, &FileInfo, sizeof(FileInfo),
  1491. SHGFI_SYSICONINDEX | (Large ? SHGFI_LARGEICON : SHGFI_SMALLICON));
  1492. if (ImageListHandle != 0)
  1493. {
  1494. Result->ShareImages = true;
  1495. Result->Handle = ImageListHandle;
  1496. }
  1497. return Result;
  1498. }