WinInterface.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <shlwapi.h>
  5. #include <Common.h>
  6. #include <Exceptions.h>
  7. #include <CoreMain.h>
  8. #include <TextsCore.h>
  9. #include <TextsWin.h>
  10. #include <HelpWin.h>
  11. #include <HelpCore.h>
  12. #include <Interface.h>
  13. #include <VCLCommon.h>
  14. #include <Glyphs.h>
  15. #include <PasTools.hpp>
  16. #include "WinInterface.h"
  17. #include "CustomWinConfiguration.h"
  18. #include "GUITools.h"
  19. //---------------------------------------------------------------------------
  20. #pragma package(smart_init)
  21. //---------------------------------------------------------------------------
  22. #define WM_TRAY_ICON (WM_WINSCP_USER + 5)
  23. //---------------------------------------------------------------------
  24. TNotifyEvent GlobalOnMinimize = NULL;
  25. //---------------------------------------------------------------------
  26. void __fastcall FormHelp(TForm * Form)
  27. {
  28. InvokeHelp(Form->ActiveControl != NULL ? Form->ActiveControl : Form);
  29. }
  30. //---------------------------------------------------------------------------
  31. TMessageParams::TMessageParams(unsigned int AParams)
  32. {
  33. Reset();
  34. Params = AParams;
  35. }
  36. //---------------------------------------------------------------------------
  37. TMessageParams::TMessageParams(const TQueryParams * AParams)
  38. {
  39. Reset();
  40. if (AParams != NULL)
  41. {
  42. Aliases = AParams->Aliases;
  43. AliasesCount = AParams->AliasesCount;
  44. Timer = AParams->Timer;
  45. TimerEvent = AParams->TimerEvent;
  46. TimerMessage = AParams->TimerMessage;
  47. TimerAnswers = AParams->TimerAnswers;
  48. TimerQueryType = AParams->TimerQueryType;
  49. Timeout = AParams->Timeout;
  50. TimeoutAnswer = AParams->TimeoutAnswer;
  51. if (FLAGSET(AParams->Params, qpNeverAskAgainCheck))
  52. {
  53. Params |= mpNeverAskAgainCheck;
  54. }
  55. if (FLAGSET(AParams->Params, qpAllowContinueOnError))
  56. {
  57. Params |= mpAllowContinueOnError;
  58. }
  59. }
  60. }
  61. //---------------------------------------------------------------------------
  62. inline void TMessageParams::Reset()
  63. {
  64. Params = 0;
  65. Aliases = NULL;
  66. AliasesCount = 0;
  67. Timer = 0;
  68. TimerEvent = NULL;
  69. TimerMessage = L"";
  70. TimerAnswers = 0;
  71. TimerQueryType = static_cast<TQueryType>(-1);
  72. Timeout = 0;
  73. TimeoutAnswer = 0;
  74. NeverAskAgainTitle = L"";
  75. NeverAskAgainAnswer = 0;
  76. NeverAskAgainCheckedInitially = false;
  77. AllowHelp = true;
  78. ImageName = L"";
  79. }
  80. //---------------------------------------------------------------------------
  81. static bool __fastcall IsPositiveAnswer(unsigned int Answer)
  82. {
  83. return (Answer == qaYes) || (Answer == qaOK) || (Answer == qaYesToAll);
  84. }
  85. //---------------------------------------------------------------------------
  86. static void __fastcall NeverAskAgainCheckClick(void * /*Data*/, TObject * Sender)
  87. {
  88. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(Sender);
  89. assert(CheckBox != NULL);
  90. TForm * Dialog = dynamic_cast<TForm *>(CheckBox->Owner);
  91. assert(Dialog != NULL);
  92. unsigned int PositiveAnswer = 0;
  93. if (CheckBox->Checked)
  94. {
  95. if (CheckBox->Tag > 0)
  96. {
  97. PositiveAnswer = CheckBox->Tag;
  98. }
  99. else
  100. {
  101. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  102. {
  103. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  104. if (Button != NULL)
  105. {
  106. if (IsPositiveAnswer(Button->ModalResult))
  107. {
  108. PositiveAnswer = Button->ModalResult;
  109. break;
  110. }
  111. }
  112. }
  113. }
  114. assert(PositiveAnswer != 0);
  115. }
  116. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  117. {
  118. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  119. if (Button != NULL)
  120. {
  121. if ((Button->ModalResult != 0) && (Button->ModalResult != static_cast<int>(qaCancel)))
  122. {
  123. Button->Enabled = !CheckBox->Checked || (Button->ModalResult == static_cast<int>(PositiveAnswer));
  124. }
  125. if (Button->DropDownMenu != NULL)
  126. {
  127. for (int iii = 0; iii < Button->DropDownMenu->Items->Count; iii++)
  128. {
  129. TMenuItem * Item = Button->DropDownMenu->Items->Items[iii];
  130. Item->Enabled = Item->Default || !CheckBox->Checked;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. //---------------------------------------------------------------------------
  137. static TCheckBox * __fastcall FindNeverAskAgainCheck(TForm * Dialog)
  138. {
  139. return NOT_NULL(dynamic_cast<TCheckBox *>(Dialog->FindComponent(L"NeverAskAgainCheck")));
  140. }
  141. //---------------------------------------------------------------------------
  142. TForm * __fastcall CreateMessageDialogEx(const UnicodeString Msg,
  143. TStrings * MoreMessages, TQueryType Type, unsigned int Answers, UnicodeString HelpKeyword,
  144. const TMessageParams * Params, TButton *& TimeoutButton)
  145. {
  146. TMsgDlgType DlgType;
  147. switch (Type) {
  148. case qtConfirmation: DlgType = mtConfirmation; break;
  149. case qtInformation: DlgType = mtInformation; break;
  150. case qtError: DlgType = mtError; break;
  151. case qtWarning: DlgType = mtWarning; break;
  152. default: assert(false);
  153. }
  154. unsigned int TimeoutAnswer = (Params != NULL) ? Params->TimeoutAnswer : 0;
  155. unsigned int ActualAnswers = Answers;
  156. if ((Params == NULL) || Params->AllowHelp)
  157. {
  158. Answers = Answers | qaHelp;
  159. }
  160. if (HelpKeyword == HELP_INTERNAL_ERROR)
  161. {
  162. Answers = Answers | qaReport;
  163. }
  164. if ((MoreMessages != NULL) && (MoreMessages->Count == 0))
  165. {
  166. MoreMessages = NULL;
  167. }
  168. UnicodeString ImageName;
  169. if (Params != NULL)
  170. {
  171. ImageName = Params->ImageName;
  172. }
  173. const TQueryButtonAlias * Aliases = (Params != NULL) ? Params->Aliases : NULL;
  174. unsigned int AliasesCount = (Params != NULL) ? Params->AliasesCount : 0;
  175. UnicodeString NeverAskAgainCaption;
  176. bool HasNeverAskAgain = (Params != NULL) && FLAGSET(Params->Params, mpNeverAskAgainCheck);
  177. if (HasNeverAskAgain)
  178. {
  179. NeverAskAgainCaption =
  180. !Params->NeverAskAgainTitle.IsEmpty() ?
  181. (UnicodeString)Params->NeverAskAgainTitle :
  182. // qaOK | qaIgnore is used, when custom "non-answer" button is required
  183. LoadStr(((ActualAnswers == qaOK) || (ActualAnswers == (qaOK | qaIgnore))) ?
  184. NEVER_SHOW_AGAIN : NEVER_ASK_AGAIN);
  185. }
  186. TForm * Dialog = CreateMoreMessageDialog(Msg, MoreMessages, DlgType, Answers,
  187. Aliases, AliasesCount, TimeoutAnswer, &TimeoutButton, ImageName, NeverAskAgainCaption);
  188. try
  189. {
  190. if (HasNeverAskAgain && ALWAYS_TRUE(Params != NULL))
  191. {
  192. TCheckBox * NeverAskAgainCheck = FindNeverAskAgainCheck(Dialog);
  193. NeverAskAgainCheck->Checked = Params->NeverAskAgainCheckedInitially;
  194. if (Params->NeverAskAgainAnswer > 0)
  195. {
  196. NeverAskAgainCheck->Tag = Params->NeverAskAgainAnswer;
  197. }
  198. TNotifyEvent OnClick;
  199. ((TMethod*)&OnClick)->Code = NeverAskAgainCheckClick;
  200. NeverAskAgainCheck->OnClick = OnClick;
  201. }
  202. Dialog->HelpKeyword = HelpKeyword;
  203. if (FLAGSET(Answers, qaHelp))
  204. {
  205. Dialog->BorderIcons = Dialog->BorderIcons << biHelp;
  206. }
  207. ResetSystemSettings(Dialog);
  208. }
  209. catch(...)
  210. {
  211. delete Dialog;
  212. throw;
  213. }
  214. return Dialog;
  215. }
  216. //---------------------------------------------------------------------------
  217. unsigned int __fastcall ExecuteMessageDialog(TForm * Dialog, unsigned int Answers, const TMessageParams * Params)
  218. {
  219. FlashOnBackground();
  220. unsigned int Answer = Dialog->ShowModal();
  221. // mrCancel is returned always when X button is pressed, despite
  222. // no Cancel button was on the dialog. Find valid "cancel" answer.
  223. // mrNone is retuned when Windows session is closing (log off)
  224. if ((Answer == mrCancel) || (Answer == mrNone))
  225. {
  226. Answer = CancelAnswer(Answers);
  227. }
  228. if ((Params != NULL) && (Params->Params & mpNeverAskAgainCheck))
  229. {
  230. TCheckBox * NeverAskAgainCheck = FindNeverAskAgainCheck(Dialog);
  231. if (NeverAskAgainCheck->Checked)
  232. {
  233. bool PositiveAnswer =
  234. (Params->NeverAskAgainAnswer > 0) ?
  235. (Answer == Params->NeverAskAgainAnswer) :
  236. IsPositiveAnswer(Answer);
  237. if (PositiveAnswer)
  238. {
  239. Answer = qaNeverAskAgain;
  240. }
  241. }
  242. }
  243. return Answer;
  244. }
  245. //---------------------------------------------------------------------------
  246. class TMessageTimer : public TTimer
  247. {
  248. public:
  249. TQueryParamsTimerEvent Event;
  250. TForm * Dialog;
  251. __fastcall TMessageTimer(TComponent * AOwner);
  252. protected:
  253. void __fastcall DoTimer(TObject * Sender);
  254. };
  255. //---------------------------------------------------------------------------
  256. __fastcall TMessageTimer::TMessageTimer(TComponent * AOwner) : TTimer(AOwner)
  257. {
  258. Event = NULL;
  259. OnTimer = DoTimer;
  260. Dialog = NULL;
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TMessageTimer::DoTimer(TObject * /*Sender*/)
  264. {
  265. if (Event != NULL)
  266. {
  267. unsigned int Result = 0;
  268. Event(Result);
  269. if (Result != 0)
  270. {
  271. Dialog->ModalResult = Result;
  272. }
  273. }
  274. }
  275. //---------------------------------------------------------------------------
  276. class TMessageTimeout : public TTimer
  277. {
  278. public:
  279. __fastcall TMessageTimeout(TComponent * AOwner, unsigned int Timeout,
  280. TButton * Button);
  281. void __fastcall Suspend();
  282. void __fastcall Cancel();
  283. protected:
  284. unsigned int FOrigTimeout;
  285. unsigned int FTimeout;
  286. unsigned int FSuspended;
  287. TButton * FButton;
  288. UnicodeString FOrigCaption;
  289. void __fastcall DoTimer(TObject * Sender);
  290. void __fastcall UpdateButton();
  291. };
  292. //---------------------------------------------------------------------------
  293. __fastcall TMessageTimeout::TMessageTimeout(TComponent * AOwner,
  294. unsigned int Timeout, TButton * Button) :
  295. TTimer(AOwner), FOrigTimeout(Timeout), FTimeout(Timeout), FButton(Button)
  296. {
  297. OnTimer = DoTimer;
  298. Interval = MSecsPerSec;
  299. FOrigCaption = FButton->Caption;
  300. FSuspended = 0;
  301. UpdateButton();
  302. }
  303. //---------------------------------------------------------------------------
  304. void __fastcall TMessageTimeout::Suspend()
  305. {
  306. FSuspended = 30 * MSecsPerSec;
  307. FTimeout = FOrigTimeout;
  308. UpdateButton();
  309. }
  310. //---------------------------------------------------------------------------
  311. void __fastcall TMessageTimeout::Cancel()
  312. {
  313. Enabled = false;
  314. UpdateButton();
  315. }
  316. //---------------------------------------------------------------------------
  317. void __fastcall TMessageTimeout::UpdateButton()
  318. {
  319. assert(FButton != NULL);
  320. FButton->Caption =
  321. !Enabled ? FOrigCaption : FMTLOAD(TIMEOUT_BUTTON, (FOrigCaption, int(FTimeout / MSecsPerSec)));
  322. }
  323. //---------------------------------------------------------------------------
  324. void __fastcall TMessageTimeout::DoTimer(TObject * /*Sender*/)
  325. {
  326. if (FTimeout <= MSecsPerSec)
  327. {
  328. assert(FButton != NULL);
  329. TForm * Dialog = dynamic_cast<TForm *>(FButton->Parent);
  330. assert(Dialog != NULL);
  331. Dialog->ModalResult = FButton->ModalResult;
  332. }
  333. else
  334. {
  335. unsigned int & Timeout = (FSuspended > 0) ? FSuspended : FTimeout;
  336. if (Timeout > Interval)
  337. {
  338. Timeout -= Interval;
  339. }
  340. else
  341. {
  342. Timeout = 0;
  343. }
  344. UpdateButton();
  345. }
  346. }
  347. //---------------------------------------------------------------------
  348. class TPublicControl : public TControl
  349. {
  350. friend void __fastcall MenuPopup(TObject * Sender, const TPoint & MousePos, bool & Handled);
  351. friend void __fastcall SetTimeoutEvents(TControl * Control, TMessageTimeout * Timeout);
  352. };
  353. //---------------------------------------------------------------------
  354. class TPublicWinControl : public TWinControl
  355. {
  356. friend void __fastcall SetTimeoutEvents(TControl * Control, TMessageTimeout * Timeout);
  357. };
  358. //---------------------------------------------------------------------------
  359. static void __fastcall MessageDialogMouseMove(void * Data, TObject * /*Sender*/,
  360. TShiftState /*Shift*/, int /*X*/, int /*Y*/)
  361. {
  362. assert(Data != NULL);
  363. TMessageTimeout * Timeout = static_cast<TMessageTimeout *>(Data);
  364. Timeout->Suspend();
  365. }
  366. //---------------------------------------------------------------------------
  367. static void __fastcall MessageDialogMouseDown(void * Data, TObject * /*Sender*/,
  368. TMouseButton /*Button*/, TShiftState /*Shift*/, int /*X*/, int /*Y*/)
  369. {
  370. assert(Data != NULL);
  371. TMessageTimeout * Timeout = static_cast<TMessageTimeout *>(Data);
  372. Timeout->Cancel();
  373. }
  374. //---------------------------------------------------------------------------
  375. static void __fastcall MessageDialogKeyDownUp(void * Data, TObject * /*Sender*/,
  376. Word & /*Key*/, TShiftState /*Shift*/)
  377. {
  378. assert(Data != NULL);
  379. TMessageTimeout * Timeout = static_cast<TMessageTimeout *>(Data);
  380. Timeout->Cancel();
  381. }
  382. //---------------------------------------------------------------------------
  383. void __fastcall SetTimeoutEvents(TControl * Control, TMessageTimeout * Timeout)
  384. {
  385. TPublicControl * PublicControl = reinterpret_cast<TPublicControl *>(Control);
  386. assert(PublicControl->OnMouseMove == NULL);
  387. PublicControl->OnMouseMove = MakeMethod<TMouseMoveEvent>(Timeout, MessageDialogMouseMove);
  388. assert(PublicControl->OnMouseDown == NULL);
  389. PublicControl->OnMouseDown = MakeMethod<TMouseEvent>(Timeout, MessageDialogMouseDown);
  390. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  391. if (WinControl != NULL)
  392. {
  393. TPublicWinControl * PublicWinControl = reinterpret_cast<TPublicWinControl *>(Control);
  394. assert(PublicWinControl->OnKeyDown == NULL);
  395. PublicWinControl->OnKeyDown = MakeMethod<TKeyEvent>(Timeout, MessageDialogKeyDownUp);
  396. assert(PublicWinControl->OnKeyUp == NULL);
  397. PublicWinControl->OnKeyUp = MakeMethod<TKeyEvent>(Timeout, MessageDialogKeyDownUp);
  398. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  399. {
  400. SetTimeoutEvents(WinControl->Controls[Index], Timeout);
  401. }
  402. }
  403. }
  404. //---------------------------------------------------------------------------
  405. unsigned int __fastcall MoreMessageDialog(const UnicodeString Message, TStrings * MoreMessages,
  406. TQueryType Type, unsigned int Answers, UnicodeString HelpKeyword, const TMessageParams * Params)
  407. {
  408. unsigned int Result;
  409. TForm * Dialog = NULL;
  410. TMessageTimer * Timer = NULL;
  411. TMessageTimeout * Timeout = NULL;
  412. try
  413. {
  414. UnicodeString AMessage = Message;
  415. if ((Params != NULL) && (Params->Timer > 0))
  416. {
  417. Timer = new TMessageTimer(Application);
  418. Timer->Interval = Params->Timer;
  419. Timer->Event = Params->TimerEvent;
  420. if (Params->TimerAnswers > 0)
  421. {
  422. Answers = Params->TimerAnswers;
  423. }
  424. if (Params->TimerQueryType >= 0)
  425. {
  426. Type = Params->TimerQueryType;
  427. }
  428. if (!Params->TimerMessage.IsEmpty())
  429. {
  430. AMessage = Params->TimerMessage;
  431. }
  432. }
  433. TButton * TimeoutButton = NULL;
  434. Dialog = CreateMessageDialogEx(AMessage, MoreMessages, Type, Answers,
  435. HelpKeyword, Params, TimeoutButton);
  436. if (Timer != NULL)
  437. {
  438. Timer->Dialog = Dialog;
  439. }
  440. if (Params != NULL)
  441. {
  442. if (Params->Timeout > 0)
  443. {
  444. Timeout = new TMessageTimeout(Application, Params->Timeout, TimeoutButton);
  445. SetTimeoutEvents(Dialog, Timeout);
  446. }
  447. }
  448. Result = ExecuteMessageDialog(Dialog, Answers, Params);
  449. }
  450. __finally
  451. {
  452. delete Dialog;
  453. delete Timer;
  454. delete Timeout;
  455. }
  456. return Result;
  457. }
  458. //---------------------------------------------------------------------------
  459. unsigned int __fastcall MessageDialog(const UnicodeString Msg, TQueryType Type,
  460. unsigned int Answers, UnicodeString HelpKeyword, const TMessageParams * Params)
  461. {
  462. return MoreMessageDialog(Msg, NULL, Type, Answers, HelpKeyword, Params);
  463. }
  464. //---------------------------------------------------------------------------
  465. unsigned int __fastcall SimpleErrorDialog(const UnicodeString Msg, const UnicodeString MoreMessages)
  466. {
  467. unsigned int Result;
  468. TStrings * More = NULL;
  469. try
  470. {
  471. if (!MoreMessages.IsEmpty())
  472. {
  473. More = new TStringList();
  474. More->Text = MoreMessages;
  475. }
  476. Result = MoreMessageDialog(Msg, More, qtError, qaOK, HELP_NONE);
  477. }
  478. __finally
  479. {
  480. delete More;
  481. }
  482. return Result;
  483. }
  484. //---------------------------------------------------------------------------
  485. unsigned int __fastcall ExceptionMessageDialog(Exception * E, TQueryType Type,
  486. const UnicodeString MessageFormat, unsigned int Answers, UnicodeString HelpKeyword,
  487. const TMessageParams * Params)
  488. {
  489. TStrings * MoreMessages = NULL;
  490. ExtException * EE = dynamic_cast<ExtException *>(E);
  491. if (EE != NULL)
  492. {
  493. MoreMessages = EE->MoreMessages;
  494. }
  495. UnicodeString Message;
  496. // this is always called from within ExceptionMessage check,
  497. // so it should never fail here
  498. CHECK(ExceptionMessageFormatted(E, Message));
  499. HelpKeyword = MergeHelpKeyword(HelpKeyword, GetExceptionHelpKeyword(E));
  500. return MoreMessageDialog(
  501. FORMAT(MessageFormat.IsEmpty() ? UnicodeString(L"%s") : MessageFormat, (Message)),
  502. MoreMessages, Type, Answers, HelpKeyword, Params);
  503. }
  504. //---------------------------------------------------------------------------
  505. unsigned int __fastcall FatalExceptionMessageDialog(Exception * E, TQueryType Type,
  506. int SessionReopenTimeout, const UnicodeString MessageFormat, unsigned int Answers,
  507. UnicodeString HelpKeyword, const TMessageParams * Params)
  508. {
  509. assert(FLAGCLEAR(Answers, qaRetry));
  510. Answers |= qaRetry;
  511. TQueryButtonAlias Aliases[1];
  512. Aliases[0].Button = qaRetry;
  513. Aliases[0].Alias = LoadStr(RECONNECT_BUTTON);
  514. TMessageParams AParams;
  515. if (Params != NULL)
  516. {
  517. AParams = *Params;
  518. }
  519. assert(AParams.Timeout == 0);
  520. // the condition is de facto excess
  521. if (SessionReopenTimeout > 0)
  522. {
  523. AParams.Timeout = SessionReopenTimeout;
  524. AParams.TimeoutAnswer = qaRetry;
  525. }
  526. assert(AParams.Aliases == NULL);
  527. AParams.Aliases = Aliases;
  528. AParams.AliasesCount = LENOF(Aliases);
  529. return ExceptionMessageDialog(E, Type, MessageFormat, Answers, HelpKeyword, &AParams);
  530. }
  531. //---------------------------------------------------------------------------
  532. void * __fastcall BusyStart()
  533. {
  534. void * Token = reinterpret_cast<void *>(Screen->Cursor);
  535. Screen->Cursor = crHourGlass;
  536. return Token;
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall BusyEnd(void * Token)
  540. {
  541. Screen->Cursor = reinterpret_cast<TCursor>(Token);
  542. }
  543. //---------------------------------------------------------------------------
  544. void __fastcall CopyParamListButton(TButton * Button)
  545. {
  546. if (!SupportsSplitButton())
  547. {
  548. MenuButton(Button);
  549. }
  550. }
  551. //---------------------------------------------------------------------------
  552. const int cpiDefault = -1;
  553. const int cpiConfigure = -2;
  554. const int cpiCustom = -3;
  555. const int cpiSaveSettings = -4;
  556. //---------------------------------------------------------------------------
  557. void __fastcall CopyParamListPopup(TRect Rect, TPopupMenu * Menu,
  558. const TCopyParamType & Param, UnicodeString Preset, TNotifyEvent OnClick,
  559. int Options, int CopyParamAttrs, bool SaveSettings)
  560. {
  561. Menu->Items->Clear();
  562. TMenuItem * CustomizeItem = NULL;
  563. TMenuItem * Item;
  564. if (FLAGSET(Options, cplCustomize))
  565. {
  566. Item = new TMenuItem(Menu);
  567. Item->Caption = LoadStr(COPY_PARAM_CUSTOM);
  568. Item->Tag = cpiCustom;
  569. Item->Default = FLAGSET(Options, cplCustomizeDefault);
  570. Item->OnClick = OnClick;
  571. Menu->Items->Add(Item);
  572. CustomizeItem = Item;
  573. }
  574. if (FLAGSET(Options, cplSaveSettings))
  575. {
  576. Item = new TMenuItem(Menu);
  577. Item->Caption = LoadStr(COPY_PARAM_SAVE_SETTINGS);
  578. Item->Tag = cpiSaveSettings;
  579. Item->Checked = SaveSettings;
  580. Item->OnClick = OnClick;
  581. Menu->Items->Add(Item);
  582. }
  583. Item = new TMenuItem(Menu);
  584. Item->Caption = LoadStr(COPY_PARAM_PRESET_HEADER);
  585. Item->Visible = false;
  586. Item->Enabled = false;
  587. Menu->Items->Add(Item);
  588. bool AnyChecked = false;
  589. Item = new TMenuItem(Menu);
  590. Item->Caption = LoadStr(COPY_PARAM_DEFAULT);
  591. Item->Tag = cpiDefault;
  592. Item->Checked =
  593. Preset.IsEmpty() && (GUIConfiguration->CopyParamPreset[L""] == Param);
  594. AnyChecked = AnyChecked || Item->Checked;
  595. Item->OnClick = OnClick;
  596. Menu->Items->Add(Item);
  597. TCopyParamType DefaultParam;
  598. const TCopyParamList * CopyParamList = GUIConfiguration->CopyParamList;
  599. for (int i = 0; i < CopyParamList->Count; i++)
  600. {
  601. UnicodeString Name = CopyParamList->Names[i];
  602. TCopyParamType AParam = GUIConfiguration->CopyParamPreset[Name];
  603. if (AParam.AnyUsableCopyParam(CopyParamAttrs) ||
  604. // This makes "Binary" preset visible,
  605. // as long as we care about transfer mode
  606. ((AParam == DefaultParam) &&
  607. FLAGCLEAR(CopyParamAttrs, cpaIncludeMaskOnly) &&
  608. FLAGCLEAR(CopyParamAttrs, cpaNoTransferMode)))
  609. {
  610. Item = new TMenuItem(Menu);
  611. Item->Caption = Name;
  612. Item->Tag = i;
  613. Item->Checked =
  614. (Preset == Name) && (AParam == Param);
  615. AnyChecked = AnyChecked || Item->Checked;
  616. Item->OnClick = OnClick;
  617. Menu->Items->Add(Item);
  618. }
  619. }
  620. if (CustomizeItem != NULL)
  621. {
  622. CustomizeItem->Checked = !AnyChecked;
  623. }
  624. Item = new TMenuItem(Menu);
  625. Item->Caption = L"-";
  626. Menu->Items->Add(Item);
  627. Item = new TMenuItem(Menu);
  628. Item->Caption = LoadStr(COPY_PARAM_CONFIGURE);
  629. Item->Tag = cpiConfigure;
  630. Item->OnClick = OnClick;
  631. Menu->Items->Add(Item);
  632. MenuPopup(Menu, Rect, NULL);
  633. }
  634. //---------------------------------------------------------------------------
  635. bool __fastcall CopyParamListPopupClick(TObject * Sender,
  636. TCopyParamType & Param, UnicodeString & Preset, int CopyParamAttrs,
  637. bool * SaveSettings)
  638. {
  639. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  640. assert(Item != NULL);
  641. assert((Item->Tag >= cpiSaveSettings) && (Item->Tag < GUIConfiguration->CopyParamList->Count));
  642. bool Result;
  643. if (Item->Tag == cpiConfigure)
  644. {
  645. bool MatchedPreset = (GUIConfiguration->CopyParamPreset[Preset] == Param);
  646. DoPreferencesDialog(pmPresets);
  647. Result = (MatchedPreset && GUIConfiguration->HasCopyParamPreset[Preset]);
  648. if (Result)
  649. {
  650. Param = GUIConfiguration->CopyParamPreset[Preset];
  651. }
  652. }
  653. else if (Item->Tag == cpiCustom)
  654. {
  655. Result = DoCopyParamCustomDialog(Param, CopyParamAttrs);
  656. }
  657. else if (Item->Tag == cpiSaveSettings)
  658. {
  659. if (ALWAYS_TRUE(SaveSettings != NULL))
  660. {
  661. *SaveSettings = !*SaveSettings;
  662. }
  663. Result = false;
  664. }
  665. else
  666. {
  667. Preset = (Item->Tag >= 0) ?
  668. GUIConfiguration->CopyParamList->Names[Item->Tag] : UnicodeString();
  669. Param = GUIConfiguration->CopyParamPreset[Preset];
  670. Result = true;
  671. }
  672. return Result;
  673. }
  674. //---------------------------------------------------------------------------
  675. TWinInteractiveCustomCommand::TWinInteractiveCustomCommand(
  676. TCustomCommand * ChildCustomCommand, const UnicodeString CustomCommandName) :
  677. TInteractiveCustomCommand(ChildCustomCommand)
  678. {
  679. FCustomCommandName = StripHotkey(CustomCommandName);
  680. }
  681. //---------------------------------------------------------------------------
  682. void __fastcall TWinInteractiveCustomCommand::Prompt(
  683. const UnicodeString & Prompt, UnicodeString & Value)
  684. {
  685. UnicodeString APrompt = Prompt;
  686. if (APrompt.IsEmpty())
  687. {
  688. APrompt = FMTLOAD(CUSTOM_COMMANDS_PARAM_PROMPT, (FCustomCommandName));
  689. }
  690. TStrings * History = CustomWinConfiguration->History[L"CustomCommandParam"];
  691. if (InputDialog(FMTLOAD(CUSTOM_COMMANDS_PARAM_TITLE, (FCustomCommandName)),
  692. APrompt, Value, HELP_CUSTOM_COMMAND_PARAM, History))
  693. {
  694. CustomWinConfiguration->History[L"CustomCommandParam"] = History;
  695. }
  696. else
  697. {
  698. Abort();
  699. }
  700. }
  701. //---------------------------------------------------------------------------
  702. void __fastcall TWinInteractiveCustomCommand::Execute(
  703. const UnicodeString & Command, UnicodeString & Value)
  704. {
  705. // inspired by
  706. // http://forum.codecall.net/topic/72472-execute-a-console-program-and-capture-its-output/
  707. HANDLE StdOutOutput;
  708. HANDLE StdOutInput;
  709. HANDLE StdInOutput;
  710. HANDLE StdInInput;
  711. SECURITY_ATTRIBUTES SecurityAttributes;
  712. SecurityAttributes.nLength = sizeof(SecurityAttributes);
  713. SecurityAttributes.lpSecurityDescriptor = NULL;
  714. SecurityAttributes.bInheritHandle = TRUE;
  715. try
  716. {
  717. if (!CreatePipe(&StdOutOutput, &StdOutInput, &SecurityAttributes, 0))
  718. {
  719. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"out")));
  720. }
  721. else if (!CreatePipe(&StdInOutput, &StdInInput, &SecurityAttributes, 0))
  722. {
  723. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"in")));
  724. }
  725. else
  726. {
  727. STARTUPINFO StartupInfo;
  728. PROCESS_INFORMATION ProcessInformation;
  729. FillMemory(&StartupInfo, sizeof(StartupInfo), 0);
  730. StartupInfo.cb = sizeof(StartupInfo);
  731. StartupInfo.wShowWindow = SW_HIDE;
  732. StartupInfo.hStdInput = StdInOutput;
  733. StartupInfo.hStdOutput = StdOutInput;
  734. StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  735. if (!CreateProcess(NULL, Command.c_str(), &SecurityAttributes, &SecurityAttributes,
  736. TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInformation))
  737. {
  738. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"process")));
  739. }
  740. else
  741. {
  742. try
  743. {
  744. // wait until the console program terminated
  745. bool Running = true;
  746. while (Running)
  747. {
  748. switch (WaitForSingleObject(ProcessInformation.hProcess, 200))
  749. {
  750. case WAIT_TIMEOUT:
  751. Application->ProcessMessages();
  752. break;
  753. case WAIT_OBJECT_0:
  754. Running = false;
  755. break;
  756. default:
  757. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"wait")));
  758. }
  759. }
  760. char Buffer[1024];
  761. unsigned long Read;
  762. while (PeekNamedPipe(StdOutOutput, NULL, 0, NULL, &Read, NULL) &&
  763. (Read > 0))
  764. {
  765. if (!ReadFile(StdOutOutput, &Buffer, Read, &Read, NULL))
  766. {
  767. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"read")));
  768. }
  769. else if (Read > 0)
  770. {
  771. Value += UnicodeString(AnsiString(Buffer, Read));
  772. }
  773. }
  774. // trim trailing cr/lf
  775. Value = TrimRight(Value);
  776. }
  777. __finally
  778. {
  779. CloseHandle(ProcessInformation.hProcess);
  780. CloseHandle(ProcessInformation.hThread);
  781. }
  782. }
  783. }
  784. }
  785. __finally
  786. {
  787. if (StdOutOutput != INVALID_HANDLE_VALUE)
  788. {
  789. CloseHandle(StdOutOutput);
  790. }
  791. if (StdOutInput != INVALID_HANDLE_VALUE)
  792. {
  793. CloseHandle(StdOutInput);
  794. }
  795. if (StdInOutput != INVALID_HANDLE_VALUE)
  796. {
  797. CloseHandle(StdInOutput);
  798. }
  799. if (StdInInput != INVALID_HANDLE_VALUE)
  800. {
  801. CloseHandle(StdInInput);
  802. }
  803. }
  804. }
  805. //---------------------------------------------------------------------------
  806. void __fastcall MenuPopup(TPopupMenu * Menu, TButton * Button)
  807. {
  808. MenuPopup(Menu, CalculatePopupRect(Button), Button);
  809. }
  810. //---------------------------------------------------------------------------
  811. void __fastcall MenuPopup(TObject * Sender, const TPoint & MousePos, bool & Handled)
  812. {
  813. TControl * Control = dynamic_cast<TControl *>(Sender);
  814. assert(Control != NULL);
  815. TPoint Point;
  816. if ((MousePos.x == -1) && (MousePos.y == -1))
  817. {
  818. Point = Control->ClientToScreen(TPoint(0, 0));
  819. }
  820. else
  821. {
  822. Point = Control->ClientToScreen(MousePos);
  823. }
  824. TPopupMenu * PopupMenu = (reinterpret_cast<TPublicControl *>(Control))->PopupMenu;
  825. assert(PopupMenu != NULL);
  826. TRect Rect(Point, Point);
  827. MenuPopup(PopupMenu, Rect, Control);
  828. Handled = true;
  829. }
  830. //---------------------------------------------------------------------------
  831. void __fastcall MenuButton(TButton * Button)
  832. {
  833. Button->Images = GlyphsModule->ButtonImages;
  834. Button->ImageIndex = 0;
  835. Button->DisabledImageIndex = 1;
  836. Button->ImageAlignment = iaRight;
  837. }
  838. //---------------------------------------------------------------------------
  839. TRect __fastcall CalculatePopupRect(TButton * Button)
  840. {
  841. TPoint UpPoint = Button->ClientToScreen(TPoint(0, 0));
  842. TPoint DownPoint = Button->ClientToScreen(TPoint(Button->Width, Button->Height));
  843. TRect Rect(UpPoint, DownPoint);
  844. // With themes enabled, button are rendered 1 pixel smaller than their actual size
  845. int Offset = UseThemes() ? -1 : 0;
  846. Rect.Inflate(Offset, Offset);
  847. return Rect;
  848. }
  849. //---------------------------------------------------------------------------
  850. TRect __fastcall CalculatePopupRect(TControl * Control, TPoint MousePos)
  851. {
  852. MousePos = Control->ClientToScreen(MousePos);
  853. TRect Rect(MousePos, MousePos);
  854. return Rect;
  855. }
  856. //---------------------------------------------------------------------------
  857. void __fastcall FixButtonImage(TButton * Button)
  858. {
  859. // this themes enabled, button image is by default drawn too high
  860. if (UseThemes())
  861. {
  862. Button->ImageMargins->Top = 1;
  863. }
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall UncenterButtonImage(TButton * Button)
  867. {
  868. Button->ImageMargins->Left = 0;
  869. if (UseThemes())
  870. {
  871. Button->Caption = TrimLeft(Button->Caption);
  872. }
  873. }
  874. //---------------------------------------------------------------------------
  875. void __fastcall CenterButtonImage(TButton * Button)
  876. {
  877. UncenterButtonImage(Button);
  878. // with themes disabled, the text seems to be drawn over the icon,
  879. // so that the padding spaces hide away most of the icon
  880. if (UseThemes())
  881. {
  882. Button->ImageAlignment = iaCenter;
  883. int ImageWidth = Button->Images->Width;
  884. std::unique_ptr<TControlCanvas> Canvas(new TControlCanvas());
  885. Canvas->Control = Button;
  886. UnicodeString Caption = Button->Caption;
  887. UnicodeString Padding;
  888. while (Canvas->TextWidth(Padding) < ImageWidth)
  889. {
  890. Padding += L" ";
  891. }
  892. Caption = Padding + Caption;
  893. Button->Caption = Caption;
  894. int CaptionWidth = Canvas->TextWidth(Caption);
  895. // The margins seem to extend the area over which the image is centered,
  896. // so we have to set it to a double of desired padding.
  897. // Note that (CaptionWidth / 2) - (ImageWidth / 2)
  898. // is approximatelly same as half of caption width before padding.
  899. Button->ImageMargins->Left = - 2 * ((CaptionWidth / 2) - (ImageWidth / 2) + ScaleByTextHeight(Button, 2));
  900. }
  901. else
  902. {
  903. // at least do not draw it so near to the edge
  904. Button->ImageMargins->Left = 1;
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall SetGlobalMinimizeHandler(TCustomForm * /*Form*/, TNotifyEvent OnMinimize)
  909. {
  910. if (GlobalOnMinimize == NULL)
  911. {
  912. GlobalOnMinimize = OnMinimize;
  913. }
  914. }
  915. //---------------------------------------------------------------------------
  916. void __fastcall ClearGlobalMinimizeHandler(TNotifyEvent OnMinimize)
  917. {
  918. if (GlobalOnMinimize == OnMinimize)
  919. {
  920. GlobalOnMinimize = NULL;
  921. }
  922. }
  923. //---------------------------------------------------------------------------
  924. void __fastcall CallGlobalMinimizeHandler(TObject * Sender)
  925. {
  926. Configuration->Usage->Inc(L"OperationMinimizations");
  927. if (ALWAYS_TRUE(GlobalOnMinimize != NULL))
  928. {
  929. GlobalOnMinimize(Sender);
  930. }
  931. }
  932. //---------------------------------------------------------------------------
  933. bool __fastcall IsApplicationMinimized()
  934. {
  935. // VCL help recommends handling Application->OnMinimize/OnRestore
  936. // for tracking state, but OnRestore is actually not called
  937. // (OnMinimize is), when app is minimized from e.g. Progress window
  938. bool AppMinimized = IsIconic(Application->Handle);
  939. bool MainFormMinimized = IsIconic(Application->MainFormHandle);
  940. return AppMinimized || MainFormMinimized;
  941. }
  942. //---------------------------------------------------------------------------
  943. UnicodeString __fastcall TranslateDateFormat(UnicodeString FormatStr)
  944. {
  945. UnicodeString Result;
  946. CALTYPE CalendarType = StrToIntDef(GetLocaleStr(GetDefaultLCID(), LOCALE_ICALENDARTYPE, L"1"), 1);
  947. if ((CalendarType != CAL_JAPAN) && (CalendarType != CAL_TAIWAN) && (CalendarType != CAL_KOREA))
  948. {
  949. bool RemoveEra =
  950. (SysLocale.PriLangID == LANG_JAPANESE) ||
  951. (SysLocale.PriLangID == LANG_CHINESE) ||
  952. (SysLocale.PriLangID == LANG_KOREAN);
  953. if (RemoveEra)
  954. {
  955. int I = 1;
  956. while (I <= FormatStr.Length())
  957. {
  958. if ((FormatStr[I] != L'g') && (FormatStr[I] != L'G'))
  959. {
  960. Result += FormatStr[I];
  961. }
  962. I++;
  963. }
  964. }
  965. else
  966. {
  967. Result = FormatStr;
  968. }
  969. }
  970. else
  971. {
  972. int I = 1;
  973. while (I <= FormatStr.Length())
  974. {
  975. if (FormatStr.IsLeadSurrogate(I))
  976. {
  977. int L = CharLength(FormatStr, I);
  978. Result += FormatStr.SubString(I, L);
  979. I += L;
  980. }
  981. else
  982. {
  983. if (StrLIComp(FormatStr.c_str() + I - 1, L"gg", 2) == 0)
  984. {
  985. Result += L"ggg";
  986. I++;
  987. }
  988. else if (StrLIComp(FormatStr.c_str() + I - 1, L"yyyy", 4) == 0)
  989. {
  990. Result += L"eeee";
  991. I += 4 - 1;
  992. }
  993. else if (StrLIComp(FormatStr.c_str() + I - 1, L"yy", 2) == 0)
  994. {
  995. Result += L"ee";
  996. I += 2 - 1;
  997. }
  998. else if ((FormatStr[I] == L'y') || (FormatStr[I] == L'Y'))
  999. {
  1000. Result += L"e";
  1001. }
  1002. else
  1003. {
  1004. Result += FormatStr[I];
  1005. }
  1006. I++;
  1007. }
  1008. }
  1009. }
  1010. return Result;
  1011. }
  1012. //---------------------------------------------------------------------------
  1013. void __fastcall GetFormatSettingsFix()
  1014. {
  1015. // todo InitSysLocale
  1016. // todo GetMonthDayNames
  1017. // todo GetEraNamesAndYearOffsets
  1018. LCID DefaultLCID = GetDefaultLCID();
  1019. FormatSettings = TFormatSettings::Create(DefaultLCID);
  1020. #pragma warn -8111
  1021. CurrencyString = GetLocaleStr(DefaultLCID, LOCALE_SCURRENCY, L"");
  1022. CurrencyFormat = (Byte) StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ICURRENCY, L"0"), 0);
  1023. NegCurrFormat = (Byte) StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_INEGCURR, L"0"), 0);
  1024. ThousandSeparator = GetLocaleChar(DefaultLCID, LOCALE_STHOUSAND, L',');
  1025. DecimalSeparator = GetLocaleChar(DefaultLCID, LOCALE_SDECIMAL, L'.');
  1026. CurrencyDecimals = (Byte) StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ICURRDIGITS, L"0"), 0);
  1027. DateSeparator = GetLocaleChar(DefaultLCID, LOCALE_SDATE, L'/');
  1028. ShortDateFormat = TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SSHORTDATE, L"m/d/yy"));
  1029. LongDateFormat = TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SLONGDATE, L"mmmm d, yyyy"));
  1030. TimeSeparator = GetLocaleChar(DefaultLCID, LOCALE_STIME, L':');
  1031. TimeAMString = GetLocaleStr(DefaultLCID, LOCALE_S1159, L"am");
  1032. TimePMString = GetLocaleStr(DefaultLCID, LOCALE_S2359, L"pm");
  1033. UnicodeString HourFormat;
  1034. if (StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ITLZERO, L"0"), 0) == 0)
  1035. {
  1036. HourFormat = L"h";
  1037. }
  1038. else
  1039. {
  1040. HourFormat = L"hh";
  1041. }
  1042. UnicodeString TimePrefix, TimePostfix;
  1043. if (StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ITIME, L"0"), 0) == 0)
  1044. {
  1045. if (StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ITIMEMARKPOSN, L"0"), 0) == 0)
  1046. {
  1047. TimePostfix = L" AMPM";
  1048. }
  1049. else
  1050. {
  1051. TimePrefix = L"AMPM ";
  1052. }
  1053. }
  1054. ShortTimeFormat = TimePrefix + HourFormat + L":mm" + TimePostfix;
  1055. LongTimeFormat = TimePrefix + HourFormat + L":mm:ss" + TimePostfix;
  1056. ListSeparator = GetLocaleChar(DefaultLCID, LOCALE_SLIST, L',');
  1057. #pragma warn .8111
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. void __fastcall WinInitialize()
  1061. {
  1062. SetErrorMode(SEM_FAILCRITICALERRORS);
  1063. // HACK
  1064. if (::IsWin7())
  1065. {
  1066. GetFormatSettingsFix();
  1067. }
  1068. #pragma warn -8111
  1069. #pragma warn .8111
  1070. }
  1071. //---------------------------------------------------------------------------
  1072. struct TNotifyIconData5
  1073. {
  1074. DWORD cbSize;
  1075. HWND hWnd;
  1076. UINT uID;
  1077. UINT uFlags;
  1078. UINT uCallbackMessage;
  1079. HICON hIcon;
  1080. TCHAR szTip[128];
  1081. DWORD dwState;
  1082. DWORD dwStateMask;
  1083. TCHAR szInfo[256];
  1084. union {
  1085. UINT uTimeout;
  1086. UINT uVersion;
  1087. } DUMMYUNIONNAME;
  1088. TCHAR szInfoTitle[64];
  1089. DWORD dwInfoFlags;
  1090. };
  1091. //---------------------------------------------------------------------------
  1092. __fastcall ::TTrayIcon::TTrayIcon(unsigned int Id)
  1093. {
  1094. FVisible = false;
  1095. FOnClick = NULL;
  1096. FOnBalloonClick = NULL;
  1097. FBalloonUserData = NULL;
  1098. FTrayIcon = new TNotifyIconData5;
  1099. memset(FTrayIcon, 0, sizeof(*FTrayIcon));
  1100. FTrayIcon->cbSize = sizeof(*FTrayIcon);
  1101. FTrayIcon->uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  1102. // LoadIconMetric is available from Windows Vista only
  1103. HMODULE ComCtl32Dll = GetModuleHandle(comctl32);
  1104. if (ALWAYS_TRUE(ComCtl32Dll))
  1105. {
  1106. typedef HRESULT WINAPI (* TLoadIconMetric)(HINSTANCE hinst, PCWSTR pszName, int lims, __out HICON *phico);
  1107. TLoadIconMetric LoadIconMetric = (TLoadIconMetric)GetProcAddress(ComCtl32Dll, "LoadIconMetric");
  1108. if (LoadIconMetric != NULL)
  1109. {
  1110. // Prefer not to use Application->Icon->Handle as that shows 32x32 scaled down to 16x16 for some reason
  1111. LoadIconMetric(MainInstance, L"MAINICON", LIM_SMALL, &FTrayIcon->hIcon);
  1112. }
  1113. }
  1114. if (FTrayIcon->hIcon == 0)
  1115. {
  1116. FTrayIcon->hIcon = Application->Icon->Handle;
  1117. }
  1118. FTrayIcon->uID = Id;
  1119. FTrayIcon->hWnd = AllocateHWnd(WndProc);
  1120. FTrayIcon->uCallbackMessage = WM_TRAY_ICON;
  1121. FTaskbarCreatedMsg = RegisterWindowMessage(L"TaskbarCreated");
  1122. }
  1123. //---------------------------------------------------------------------------
  1124. __fastcall ::TTrayIcon::~TTrayIcon()
  1125. {
  1126. // make sure we hide icon even in case it was shown just to pop up the balloon
  1127. // (in which case Visible == false)
  1128. CancelBalloon();
  1129. Visible = false;
  1130. DeallocateHWnd(FTrayIcon->hWnd);
  1131. delete FTrayIcon;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void __fastcall ::TTrayIcon::PopupBalloon(UnicodeString Title,
  1135. const UnicodeString & Str, TQueryType QueryType, unsigned int Timeout,
  1136. TNotifyEvent OnBalloonClick, TObject * BalloonUserData)
  1137. {
  1138. if (Timeout > 30000)
  1139. {
  1140. // this is probably system limit, do not try more, especially for
  1141. // the timeout-driven hiding of the tray icon (for Win2k)
  1142. Timeout = 30000;
  1143. }
  1144. FTrayIcon->uFlags |= NIF_INFO;
  1145. Title = FORMAT(L"%s - %s", (Title, AppNameString()));
  1146. StrPLCopy(FTrayIcon->szInfoTitle, Title, LENOF(FTrayIcon->szInfoTitle) - 1);
  1147. StrPLCopy(FTrayIcon->szInfo, Str, LENOF(FTrayIcon->szInfo) - 1);
  1148. FTrayIcon->uTimeout = Timeout;
  1149. switch (QueryType)
  1150. {
  1151. case qtError:
  1152. FTrayIcon->dwInfoFlags = NIIF_ERROR;
  1153. break;
  1154. case qtInformation:
  1155. case qtConfirmation:
  1156. FTrayIcon->dwInfoFlags = NIIF_INFO;
  1157. break;
  1158. case qtWarning:
  1159. default:
  1160. FTrayIcon->dwInfoFlags = NIIF_WARNING;
  1161. break;
  1162. }
  1163. KillTimer(FTrayIcon->hWnd, 1);
  1164. if (Visible)
  1165. {
  1166. Update();
  1167. }
  1168. else
  1169. {
  1170. Notify(NIM_ADD);
  1171. }
  1172. FOnBalloonClick = OnBalloonClick;
  1173. delete FBalloonUserData;
  1174. FBalloonUserData = BalloonUserData;
  1175. // Clearing the flag ensures that subsequent updates does not hide the baloon
  1176. // unless CancelBalloon is called explicitly
  1177. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  1178. }
  1179. //---------------------------------------------------------------------------
  1180. void __fastcall ::TTrayIcon::BalloonCancelled()
  1181. {
  1182. FOnBalloonClick = NULL;
  1183. delete FBalloonUserData;
  1184. FBalloonUserData = NULL;
  1185. }
  1186. //---------------------------------------------------------------------------
  1187. void __fastcall ::TTrayIcon::CancelBalloon()
  1188. {
  1189. KillTimer(FTrayIcon->hWnd, 1);
  1190. if (Visible)
  1191. {
  1192. FTrayIcon->uFlags |= NIF_INFO;
  1193. FTrayIcon->szInfo[0] = L'\0';
  1194. Update();
  1195. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  1196. }
  1197. else
  1198. {
  1199. Notify(NIM_DELETE);
  1200. }
  1201. BalloonCancelled();
  1202. }
  1203. //---------------------------------------------------------------------------
  1204. bool __fastcall ::TTrayIcon::Notify(unsigned int Message)
  1205. {
  1206. bool Result = SUCCEEDED(Shell_NotifyIcon(Message, (NOTIFYICONDATA*)FTrayIcon));
  1207. if (Result && (Message == NIM_ADD))
  1208. {
  1209. UINT Timeout = FTrayIcon->uTimeout;
  1210. try
  1211. {
  1212. FTrayIcon->uVersion = NOTIFYICON_VERSION;
  1213. Result = SUCCEEDED(Shell_NotifyIcon(NIM_SETVERSION, (NOTIFYICONDATA*)FTrayIcon));
  1214. }
  1215. __finally
  1216. {
  1217. FTrayIcon->uTimeout = Timeout;
  1218. }
  1219. }
  1220. return Result;
  1221. }
  1222. //---------------------------------------------------------------------------
  1223. void __fastcall ::TTrayIcon::Update()
  1224. {
  1225. if (Visible)
  1226. {
  1227. Notify(NIM_MODIFY);
  1228. }
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. void __fastcall ::TTrayIcon::SetVisible(bool value)
  1232. {
  1233. if (Visible != value)
  1234. {
  1235. if (value)
  1236. {
  1237. FVisible = Notify(NIM_ADD);
  1238. }
  1239. else
  1240. {
  1241. FVisible = false;
  1242. KillTimer(FTrayIcon->hWnd, 1);
  1243. Notify(NIM_DELETE);
  1244. BalloonCancelled();
  1245. }
  1246. }
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall ::TTrayIcon::WndProc(TMessage & Message)
  1250. {
  1251. try
  1252. {
  1253. if (Message.Msg == WM_TRAY_ICON)
  1254. {
  1255. assert(Message.WParam == 0);
  1256. switch (Message.LParam)
  1257. {
  1258. // old shell32
  1259. case WM_LBUTTONUP:
  1260. case WM_RBUTTONUP:
  1261. // new shell32:
  1262. case WM_CONTEXTMENU:
  1263. if (OnClick != NULL)
  1264. {
  1265. OnClick(NULL);
  1266. }
  1267. Message.Result = true;
  1268. break;
  1269. }
  1270. if (Message.LParam == NIN_BALLOONUSERCLICK)
  1271. {
  1272. if (FOnBalloonClick != NULL)
  1273. {
  1274. // prevent the user data from being freed by possible call
  1275. // to CancelBalloon or PopupBalloon during call to OnBalloonClick
  1276. std::unique_ptr<TObject> UserData(FBalloonUserData);
  1277. FBalloonUserData = NULL;
  1278. FOnBalloonClick(UserData.get());
  1279. }
  1280. else if (OnClick != NULL)
  1281. {
  1282. OnClick(NULL);
  1283. }
  1284. }
  1285. switch (Message.LParam)
  1286. {
  1287. case NIN_BALLOONHIDE:
  1288. case NIN_BALLOONTIMEOUT:
  1289. case NIN_BALLOONUSERCLICK:
  1290. KillTimer(FTrayIcon->hWnd, 1);
  1291. // if icon was shown just to display balloon, hide it with the balloon
  1292. if (!Visible)
  1293. {
  1294. Notify(NIM_DELETE);
  1295. }
  1296. BalloonCancelled();
  1297. break;
  1298. }
  1299. }
  1300. else if (Message.Msg == WM_TIMER)
  1301. {
  1302. // sanity check
  1303. Notify(NIM_DELETE);
  1304. BalloonCancelled();
  1305. }
  1306. else if (Message.Msg == FTaskbarCreatedMsg)
  1307. {
  1308. if (Visible)
  1309. {
  1310. // force recreation
  1311. Visible = false;
  1312. Visible = true;
  1313. }
  1314. }
  1315. else
  1316. {
  1317. Message.Result = DefWindowProc(FTrayIcon->hWnd, Message.Msg, Message.WParam, Message.LParam);
  1318. }
  1319. }
  1320. catch(Exception & E)
  1321. {
  1322. Application->HandleException(&E);
  1323. }
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. UnicodeString __fastcall ::TTrayIcon::GetHint()
  1327. {
  1328. return FTrayIcon->szTip;
  1329. }
  1330. //---------------------------------------------------------------------------
  1331. void __fastcall ::TTrayIcon::SetHint(UnicodeString value)
  1332. {
  1333. if (Hint != value)
  1334. {
  1335. unsigned int Max = LENOF(FTrayIcon->szTip);
  1336. StrPLCopy(FTrayIcon->szTip, value, Max - 1);
  1337. Update();
  1338. }
  1339. }
  1340. //---------------------------------------------------------------------------