VCLCommon.cpp 41 KB

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