VCLCommon.cpp 42 KB

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