VCLCommon.cpp 37 KB

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