VCLCommon.cpp 45 KB

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