WinInterface.cpp 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750
  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 <DateUtils.hpp>
  17. #include <Custom.h>
  18. #include <HistoryComboBox.hpp>
  19. #include "WinInterface.h"
  20. #include "GUITools.h"
  21. #include "JclDebug.hpp"
  22. #include "JclHookExcept.hpp"
  23. #include <StrUtils.hpp>
  24. #include <WinApi.h>
  25. #include "Tools.h"
  26. //---------------------------------------------------------------------------
  27. #pragma package(smart_init)
  28. //---------------------------------------------------------------------------
  29. #define WM_TRAY_ICON (WM_WINSCP_USER + 5)
  30. //---------------------------------------------------------------------
  31. TNotifyEvent GlobalOnMinimize = NULL;
  32. //---------------------------------------------------------------------
  33. const IID IID_IListView_Win7 = {0xE5B16AF2, 0x3990, 0x4681, {0xA6, 0x09, 0x1F, 0x06, 0x0C, 0xD1, 0x42, 0x69}};
  34. //---------------------------------------------------------------------
  35. void __fastcall FormHelp(TCustomForm * Form)
  36. {
  37. InvokeHelp(Form->ActiveControl != NULL ? Form->ActiveControl : Form);
  38. }
  39. //---------------------------------------------------------------------------
  40. TMessageParams::TMessageParams(unsigned int AParams)
  41. {
  42. Reset();
  43. Params = AParams;
  44. }
  45. //---------------------------------------------------------------------------
  46. TMessageParams::TMessageParams(const TQueryParams * AParams)
  47. {
  48. Reset();
  49. if (AParams != NULL)
  50. {
  51. Aliases = AParams->Aliases;
  52. AliasesCount = AParams->AliasesCount;
  53. Timer = AParams->Timer;
  54. TimerEvent = AParams->TimerEvent;
  55. TimerMessage = AParams->TimerMessage;
  56. TimerAnswers = AParams->TimerAnswers;
  57. TimerQueryType = AParams->TimerQueryType;
  58. Timeout = AParams->Timeout;
  59. TimeoutAnswer = AParams->TimeoutAnswer;
  60. if (FLAGSET(AParams->Params, qpNeverAskAgainCheck))
  61. {
  62. Params |= mpNeverAskAgainCheck;
  63. }
  64. if (FLAGSET(AParams->Params, qpAllowContinueOnError))
  65. {
  66. Params |= mpAllowContinueOnError;
  67. }
  68. }
  69. }
  70. //---------------------------------------------------------------------------
  71. inline void TMessageParams::Reset()
  72. {
  73. Params = 0;
  74. Aliases = NULL;
  75. AliasesCount = 0;
  76. Timer = 0;
  77. TimerEvent = NULL;
  78. TimerMessage = L"";
  79. TimerAnswers = 0;
  80. TimerQueryType = static_cast<TQueryType>(-1);
  81. Timeout = 0;
  82. TimeoutAnswer = 0;
  83. NeverAskAgainTitle = L"";
  84. NeverAskAgainAnswer = 0;
  85. NeverAskAgainCheckedInitially = false;
  86. AllowHelp = true;
  87. ImageName = L"";
  88. MoreMessagesUrl = L"";
  89. MoreMessagesSize = TSize();
  90. CustomCaption = L"";
  91. }
  92. //---------------------------------------------------------------------------
  93. static bool __fastcall IsPositiveAnswer(unsigned int Answer)
  94. {
  95. return (Answer == qaYes) || (Answer == qaOK) || (Answer == qaYesToAll);
  96. }
  97. //---------------------------------------------------------------------------
  98. static void __fastcall NeverAskAgainCheckClick(void * /*Data*/, TObject * Sender)
  99. {
  100. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(Sender);
  101. DebugAssert(CheckBox != NULL);
  102. TForm * Dialog = dynamic_cast<TForm *>(CheckBox->Owner);
  103. DebugAssert(Dialog != NULL);
  104. unsigned int PositiveAnswer = 0;
  105. if (CheckBox->Checked)
  106. {
  107. if (CheckBox->Tag > 0)
  108. {
  109. PositiveAnswer = CheckBox->Tag;
  110. }
  111. else
  112. {
  113. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  114. {
  115. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  116. if (Button != NULL)
  117. {
  118. if (IsPositiveAnswer(Button->ModalResult))
  119. {
  120. PositiveAnswer = Button->ModalResult;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. DebugAssert(PositiveAnswer != 0);
  127. }
  128. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  129. {
  130. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  131. if (Button != NULL)
  132. {
  133. if ((Button->ModalResult != 0) && (Button->ModalResult != static_cast<int>(qaCancel)))
  134. {
  135. Button->Enabled = !CheckBox->Checked || (Button->ModalResult == static_cast<int>(PositiveAnswer));
  136. }
  137. if (Button->DropDownMenu != NULL)
  138. {
  139. for (int iii = 0; iii < Button->DropDownMenu->Items->Count; iii++)
  140. {
  141. TMenuItem * Item = Button->DropDownMenu->Items->Items[iii];
  142. Item->Enabled = Item->Default || !CheckBox->Checked;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. //---------------------------------------------------------------------------
  149. static TCheckBox * __fastcall FindNeverAskAgainCheck(TForm * Dialog)
  150. {
  151. return DebugNotNull(dynamic_cast<TCheckBox *>(Dialog->FindComponent(L"NeverAskAgainCheck")));
  152. }
  153. //---------------------------------------------------------------------------
  154. TForm * __fastcall CreateMessageDialogEx(const UnicodeString Msg,
  155. TStrings * MoreMessages, TQueryType Type, unsigned int Answers, UnicodeString HelpKeyword,
  156. const TMessageParams * Params, TButton *& TimeoutButton)
  157. {
  158. TMsgDlgType DlgType;
  159. switch (Type) {
  160. case qtConfirmation: DlgType = mtConfirmation; break;
  161. case qtInformation: DlgType = mtInformation; break;
  162. case qtError: DlgType = mtError; break;
  163. case qtWarning: DlgType = mtWarning; break;
  164. default: DebugFail();
  165. }
  166. unsigned int TimeoutAnswer = (Params != NULL) ? Params->TimeoutAnswer : 0;
  167. unsigned int ActualAnswers = Answers;
  168. if ((Params == NULL) || Params->AllowHelp)
  169. {
  170. Answers = Answers | qaHelp;
  171. }
  172. if (IsInternalErrorHelpKeyword(HelpKeyword))
  173. {
  174. Answers = Answers | qaReport;
  175. }
  176. if ((MoreMessages != NULL) && (MoreMessages->Count == 0))
  177. {
  178. MoreMessages = NULL;
  179. }
  180. UnicodeString ImageName;
  181. UnicodeString MoreMessagesUrl;
  182. TSize MoreMessagesSize;
  183. UnicodeString CustomCaption;
  184. if (Params != NULL)
  185. {
  186. ImageName = Params->ImageName;
  187. MoreMessagesUrl = Params->MoreMessagesUrl;
  188. MoreMessagesSize = Params->MoreMessagesSize;
  189. CustomCaption = Params->CustomCaption;
  190. }
  191. const TQueryButtonAlias * Aliases = (Params != NULL) ? Params->Aliases : NULL;
  192. unsigned int AliasesCount = (Params != NULL) ? Params->AliasesCount : 0;
  193. UnicodeString NeverAskAgainCaption;
  194. bool HasNeverAskAgain = (Params != NULL) && FLAGSET(Params->Params, mpNeverAskAgainCheck);
  195. if (HasNeverAskAgain)
  196. {
  197. NeverAskAgainCaption =
  198. !Params->NeverAskAgainTitle.IsEmpty() ?
  199. (UnicodeString)Params->NeverAskAgainTitle :
  200. // qaOK | qaIgnore is used, when custom "non-answer" button is required
  201. LoadStr(((ActualAnswers == qaOK) || (ActualAnswers == (qaOK | qaIgnore))) ?
  202. NEVER_SHOW_AGAIN : NEVER_ASK_AGAIN);
  203. }
  204. TForm * Dialog = CreateMoreMessageDialog(Msg, MoreMessages, DlgType, Answers,
  205. Aliases, AliasesCount, TimeoutAnswer, &TimeoutButton, ImageName, NeverAskAgainCaption,
  206. MoreMessagesUrl, MoreMessagesSize, CustomCaption);
  207. try
  208. {
  209. if (HasNeverAskAgain && DebugAlwaysTrue(Params != NULL))
  210. {
  211. TCheckBox * NeverAskAgainCheck = FindNeverAskAgainCheck(Dialog);
  212. NeverAskAgainCheck->Checked = Params->NeverAskAgainCheckedInitially;
  213. if (Params->NeverAskAgainAnswer > 0)
  214. {
  215. NeverAskAgainCheck->Tag = Params->NeverAskAgainAnswer;
  216. }
  217. TNotifyEvent OnClick;
  218. ((TMethod*)&OnClick)->Code = NeverAskAgainCheckClick;
  219. NeverAskAgainCheck->OnClick = OnClick;
  220. }
  221. Dialog->HelpKeyword = HelpKeyword;
  222. if (FLAGSET(Answers, qaHelp))
  223. {
  224. Dialog->BorderIcons = Dialog->BorderIcons << biHelp;
  225. }
  226. ResetSystemSettings(Dialog);
  227. }
  228. catch(...)
  229. {
  230. delete Dialog;
  231. throw;
  232. }
  233. return Dialog;
  234. }
  235. //---------------------------------------------------------------------------
  236. unsigned int __fastcall ExecuteMessageDialog(TForm * Dialog, unsigned int Answers, const TMessageParams * Params)
  237. {
  238. FlashOnBackground();
  239. unsigned int Answer = Dialog->ShowModal();
  240. // mrCancel is returned always when X button is pressed, despite
  241. // no Cancel button was on the dialog. Find valid "cancel" answer.
  242. // mrNone is returned when Windows session is closing (log off)
  243. if ((Answer == mrCancel) || (Answer == mrNone))
  244. {
  245. Answer = CancelAnswer(Answers);
  246. }
  247. if ((Params != NULL) && (Params->Params & mpNeverAskAgainCheck))
  248. {
  249. TCheckBox * NeverAskAgainCheck = FindNeverAskAgainCheck(Dialog);
  250. if (NeverAskAgainCheck->Checked)
  251. {
  252. bool PositiveAnswer =
  253. (Params->NeverAskAgainAnswer > 0) ?
  254. (Answer == Params->NeverAskAgainAnswer) :
  255. IsPositiveAnswer(Answer);
  256. if (PositiveAnswer)
  257. {
  258. Answer = qaNeverAskAgain;
  259. }
  260. }
  261. }
  262. return Answer;
  263. }
  264. //---------------------------------------------------------------------------
  265. class TMessageTimer : public TTimer
  266. {
  267. public:
  268. TQueryParamsTimerEvent Event;
  269. TForm * Dialog;
  270. __fastcall TMessageTimer(TComponent * AOwner);
  271. protected:
  272. void __fastcall DoTimer(TObject * Sender);
  273. };
  274. //---------------------------------------------------------------------------
  275. __fastcall TMessageTimer::TMessageTimer(TComponent * AOwner) : TTimer(AOwner)
  276. {
  277. Event = NULL;
  278. OnTimer = DoTimer;
  279. Dialog = NULL;
  280. }
  281. //---------------------------------------------------------------------------
  282. void __fastcall TMessageTimer::DoTimer(TObject * /*Sender*/)
  283. {
  284. if (Event != NULL)
  285. {
  286. unsigned int Result = 0;
  287. Event(Result);
  288. if (Result != 0)
  289. {
  290. Dialog->ModalResult = Result;
  291. }
  292. }
  293. }
  294. //---------------------------------------------------------------------------
  295. class TMessageTimeout : public TTimer
  296. {
  297. public:
  298. __fastcall TMessageTimeout(TComponent * AOwner, unsigned int Timeout,
  299. TButton * Button);
  300. void __fastcall MouseMove();
  301. void __fastcall Cancel();
  302. protected:
  303. unsigned int FOrigTimeout;
  304. unsigned int FTimeout;
  305. TButton * FButton;
  306. UnicodeString FOrigCaption;
  307. TPoint FOrigCursorPos;
  308. void __fastcall DoTimer(TObject * Sender);
  309. void __fastcall UpdateButton();
  310. };
  311. //---------------------------------------------------------------------------
  312. __fastcall TMessageTimeout::TMessageTimeout(TComponent * AOwner,
  313. unsigned int Timeout, TButton * Button) :
  314. TTimer(AOwner), FOrigTimeout(Timeout), FTimeout(Timeout), FButton(Button)
  315. {
  316. OnTimer = DoTimer;
  317. Interval = MSecsPerSec;
  318. FOrigCaption = FButton->Caption;
  319. FOrigCursorPos = Mouse->CursorPos;
  320. UpdateButton();
  321. }
  322. //---------------------------------------------------------------------------
  323. void __fastcall TMessageTimeout::MouseMove()
  324. {
  325. TPoint CursorPos = Mouse->CursorPos;
  326. int Delta = std::max(std::abs(FOrigCursorPos.X - CursorPos.X), std::abs(FOrigCursorPos.Y - CursorPos.Y));
  327. int Threshold = 8;
  328. if (DebugAlwaysTrue(FButton != NULL))
  329. {
  330. Threshold = ScaleByTextHeight(FButton, Threshold);
  331. }
  332. if (Delta > Threshold)
  333. {
  334. FOrigCursorPos = CursorPos;
  335. const unsigned int SuspendTime = 30 * MSecsPerSec;
  336. FTimeout = std::max(FOrigTimeout, SuspendTime);
  337. UpdateButton();
  338. }
  339. }
  340. //---------------------------------------------------------------------------
  341. void __fastcall TMessageTimeout::Cancel()
  342. {
  343. Enabled = false;
  344. UpdateButton();
  345. }
  346. //---------------------------------------------------------------------------
  347. void __fastcall TMessageTimeout::UpdateButton()
  348. {
  349. DebugAssert(FButton != NULL);
  350. FButton->Caption =
  351. !Enabled ? FOrigCaption : FMTLOAD(TIMEOUT_BUTTON, (FOrigCaption, int(FTimeout / MSecsPerSec)));
  352. }
  353. //---------------------------------------------------------------------------
  354. void __fastcall TMessageTimeout::DoTimer(TObject * /*Sender*/)
  355. {
  356. if (FTimeout <= Interval)
  357. {
  358. DebugAssert(FButton != NULL);
  359. TForm * Dialog = dynamic_cast<TForm *>(FButton->Parent);
  360. DebugAssert(Dialog != NULL);
  361. Dialog->ModalResult = FButton->ModalResult;
  362. }
  363. else
  364. {
  365. FTimeout -= Interval;
  366. UpdateButton();
  367. }
  368. }
  369. //---------------------------------------------------------------------
  370. class TPublicControl : public TControl
  371. {
  372. friend void __fastcall MenuPopup(TObject * Sender, const TPoint & MousePos, bool & Handled);
  373. friend void __fastcall SetTimeoutEvents(TControl * Control, TMessageTimeout * Timeout);
  374. };
  375. //---------------------------------------------------------------------
  376. class TPublicWinControl : public TWinControl
  377. {
  378. friend void __fastcall SetTimeoutEvents(TControl * Control, TMessageTimeout * Timeout);
  379. };
  380. //---------------------------------------------------------------------------
  381. static void __fastcall MessageDialogMouseMove(void * Data, TObject * /*Sender*/,
  382. TShiftState /*Shift*/, int /*X*/, int /*Y*/)
  383. {
  384. DebugAssert(Data != NULL);
  385. TMessageTimeout * Timeout = static_cast<TMessageTimeout *>(Data);
  386. Timeout->MouseMove();
  387. }
  388. //---------------------------------------------------------------------------
  389. static void __fastcall MessageDialogMouseDown(void * Data, TObject * /*Sender*/,
  390. TMouseButton /*Button*/, TShiftState /*Shift*/, int /*X*/, int /*Y*/)
  391. {
  392. DebugAssert(Data != NULL);
  393. TMessageTimeout * Timeout = static_cast<TMessageTimeout *>(Data);
  394. Timeout->Cancel();
  395. }
  396. //---------------------------------------------------------------------------
  397. static void __fastcall MessageDialogKeyDownUp(void * Data, TObject * /*Sender*/,
  398. Word & /*Key*/, TShiftState /*Shift*/)
  399. {
  400. DebugAssert(Data != NULL);
  401. TMessageTimeout * Timeout = static_cast<TMessageTimeout *>(Data);
  402. Timeout->Cancel();
  403. }
  404. //---------------------------------------------------------------------------
  405. void __fastcall SetTimeoutEvents(TControl * Control, TMessageTimeout * Timeout)
  406. {
  407. TPublicControl * PublicControl = reinterpret_cast<TPublicControl *>(Control);
  408. DebugAssert(PublicControl->OnMouseMove == NULL);
  409. PublicControl->OnMouseMove = MakeMethod<TMouseMoveEvent>(Timeout, MessageDialogMouseMove);
  410. DebugAssert(PublicControl->OnMouseDown == NULL);
  411. PublicControl->OnMouseDown = MakeMethod<TMouseEvent>(Timeout, MessageDialogMouseDown);
  412. TWinControl * WinControl = dynamic_cast<TWinControl *>(Control);
  413. if (WinControl != NULL)
  414. {
  415. TPublicWinControl * PublicWinControl = reinterpret_cast<TPublicWinControl *>(Control);
  416. DebugAssert(PublicWinControl->OnKeyDown == NULL);
  417. PublicWinControl->OnKeyDown = MakeMethod<TKeyEvent>(Timeout, MessageDialogKeyDownUp);
  418. DebugAssert(PublicWinControl->OnKeyUp == NULL);
  419. PublicWinControl->OnKeyUp = MakeMethod<TKeyEvent>(Timeout, MessageDialogKeyDownUp);
  420. for (int Index = 0; Index < WinControl->ControlCount; Index++)
  421. {
  422. SetTimeoutEvents(WinControl->Controls[Index], Timeout);
  423. }
  424. }
  425. }
  426. //---------------------------------------------------------------------------
  427. // Merge with CreateMessageDialogEx
  428. TForm * __fastcall CreateMoreMessageDialogEx(const UnicodeString Message, TStrings * MoreMessages,
  429. TQueryType Type, unsigned int Answers, UnicodeString HelpKeyword, const TMessageParams * Params)
  430. {
  431. std::unique_ptr<TForm> Dialog;
  432. UnicodeString AMessage = Message;
  433. TMessageTimer * Timer = NULL;
  434. if ((Params != NULL) && (Params->Timer > 0))
  435. {
  436. Timer = new TMessageTimer(Application);
  437. Timer->Interval = Params->Timer;
  438. Timer->Event = Params->TimerEvent;
  439. if (Params->TimerAnswers > 0)
  440. {
  441. Answers = Params->TimerAnswers;
  442. }
  443. if (Params->TimerQueryType >= 0)
  444. {
  445. Type = Params->TimerQueryType;
  446. }
  447. if (!Params->TimerMessage.IsEmpty())
  448. {
  449. AMessage = Params->TimerMessage;
  450. }
  451. Timer->Name = L"MessageTimer";
  452. }
  453. TButton * TimeoutButton = NULL;
  454. Dialog.reset(
  455. CreateMessageDialogEx(
  456. AMessage, MoreMessages, Type, Answers, HelpKeyword, Params, TimeoutButton));
  457. if (Timer != NULL)
  458. {
  459. Timer->Dialog = Dialog.get();
  460. Dialog->InsertComponent(Timer);
  461. }
  462. if (Params != NULL)
  463. {
  464. if (Params->Timeout > 0)
  465. {
  466. TMessageTimeout * Timeout = new TMessageTimeout(Application, Params->Timeout, TimeoutButton);
  467. SetTimeoutEvents(Dialog.get(), Timeout);
  468. Timeout->Name = L"MessageTimeout";
  469. Dialog->InsertComponent(Timeout);
  470. }
  471. }
  472. return Dialog.release();
  473. }
  474. //---------------------------------------------------------------------------
  475. unsigned int __fastcall MoreMessageDialog(const UnicodeString Message, TStrings * MoreMessages,
  476. TQueryType Type, unsigned int Answers, UnicodeString HelpKeyword, const TMessageParams * Params)
  477. {
  478. std::unique_ptr<TForm> Dialog(CreateMoreMessageDialogEx(Message, MoreMessages, Type, Answers, HelpKeyword, Params));
  479. unsigned int Result = ExecuteMessageDialog(Dialog.get(), Answers, Params);
  480. return Result;
  481. }
  482. //---------------------------------------------------------------------------
  483. unsigned int __fastcall MessageDialog(const UnicodeString Msg, TQueryType Type,
  484. unsigned int Answers, UnicodeString HelpKeyword, const TMessageParams * Params)
  485. {
  486. return MoreMessageDialog(Msg, NULL, Type, Answers, HelpKeyword, Params);
  487. }
  488. //---------------------------------------------------------------------------
  489. unsigned int __fastcall SimpleErrorDialog(const UnicodeString Msg, const UnicodeString MoreMessages)
  490. {
  491. unsigned int Result;
  492. TStrings * More = NULL;
  493. try
  494. {
  495. if (!MoreMessages.IsEmpty())
  496. {
  497. More = TextToStringList(MoreMessages);
  498. }
  499. Result = MoreMessageDialog(Msg, More, qtError, qaOK, HELP_NONE);
  500. }
  501. __finally
  502. {
  503. delete More;
  504. }
  505. return Result;
  506. }
  507. //---------------------------------------------------------------------------
  508. static TStrings * __fastcall StackInfoListToStrings(
  509. TJclStackInfoList * StackInfoList)
  510. {
  511. std::unique_ptr<TStrings> StackTrace(new TStringList());
  512. StackInfoList->AddToStrings(StackTrace.get(), true, false, true, true);
  513. for (int Index = 0; Index < StackTrace->Count; Index++)
  514. {
  515. UnicodeString Frame = StackTrace->Strings[Index];
  516. // get rid of declarations "flags" that are included in .map
  517. Frame = ReplaceStr(Frame, L"__fastcall ", L"");
  518. Frame = ReplaceStr(Frame, L"__linkproc__ ", L"");
  519. if (DebugAlwaysTrue(!Frame.IsEmpty() && (Frame[1] == L'(')))
  520. {
  521. int Start = Frame.Pos(L"[");
  522. int End = Frame.Pos(L"]");
  523. if (DebugAlwaysTrue((Start > 1) && (End > Start) && (Frame[Start - 1] == L' ')))
  524. {
  525. // remove absolute address
  526. Frame.Delete(Start - 1, End - Start + 2);
  527. }
  528. }
  529. StackTrace->Strings[Index] = Frame;
  530. }
  531. return StackTrace.release();
  532. }
  533. //---------------------------------------------------------------------------
  534. static std::unique_ptr<TCriticalSection> StackTraceCriticalSection(TraceInitPtr(new TCriticalSection()));
  535. typedef std::map<DWORD, TStrings *> TStackTraceMap;
  536. static TStackTraceMap StackTraceMap;
  537. //---------------------------------------------------------------------------
  538. UnicodeString __fastcall GetExceptionDebugInfo()
  539. {
  540. UnicodeString Result;
  541. TGuard Guard(StackTraceCriticalSection.get());
  542. TStackTraceMap::iterator Iterator = StackTraceMap.find(GetCurrentThreadId());
  543. if (Iterator != StackTraceMap.end())
  544. {
  545. TStrings * StackTrace = Iterator->second;
  546. for (int Index = 0; Index < StackTrace->Count; Index++)
  547. {
  548. UnicodeString Frame = StackTrace->Strings[Index];
  549. // The last line might be empty
  550. if (!Frame.IsEmpty())
  551. {
  552. int P = Frame.Pos(L")");
  553. if (DebugAlwaysTrue(P > 0))
  554. {
  555. UnicodeString Symbol = Frame.SubString(P + 1, Frame.Length() - P).Trim();
  556. if ((Symbol != L"KERNELBASE.dll.RaiseException") &&
  557. (Symbol != L"Jclhookexcept::JclAddExceptNotifier") &&
  558. (Symbol != L"_ReThrowException") &&
  559. (Symbol != L"____ExceptionHandler") &&
  560. (Symbol != L"__ExceptionHandler") &&
  561. (Symbol != L"___doGlobalUnwind") &&
  562. (Symbol != L"_ThrowExceptionLDTC"))
  563. {
  564. AddToList(Result, Symbol, L";");
  565. }
  566. }
  567. }
  568. }
  569. }
  570. return Result;
  571. }
  572. //---------------------------------------------------------------------------
  573. bool __fastcall AppendExceptionStackTraceAndForget(TStrings *& MoreMessages)
  574. {
  575. bool Result = false;
  576. TGuard Guard(StackTraceCriticalSection.get());
  577. TStackTraceMap::iterator Iterator = StackTraceMap.find(GetCurrentThreadId());
  578. if (Iterator != StackTraceMap.end())
  579. {
  580. std::unique_ptr<TStrings> OwnedMoreMessages;
  581. if (MoreMessages == NULL)
  582. {
  583. OwnedMoreMessages.reset(new TStringList());
  584. MoreMessages = OwnedMoreMessages.get();
  585. Result = true;
  586. }
  587. if (!MoreMessages->Text.IsEmpty())
  588. {
  589. MoreMessages->Text = MoreMessages->Text + "\n";
  590. }
  591. MoreMessages->Text = MoreMessages->Text + LoadStr(STACK_TRACE) + "\n";
  592. MoreMessages->AddStrings(Iterator->second);
  593. delete Iterator->second;
  594. StackTraceMap.erase(Iterator);
  595. OwnedMoreMessages.release();
  596. }
  597. return Result;
  598. }
  599. //---------------------------------------------------------------------------
  600. unsigned int __fastcall ExceptionMessageDialog(Exception * E, TQueryType Type,
  601. const UnicodeString MessageFormat, unsigned int Answers, UnicodeString HelpKeyword,
  602. const TMessageParams * Params)
  603. {
  604. TStrings * MoreMessages = NULL;
  605. ExtException * EE = dynamic_cast<ExtException *>(E);
  606. if (EE != NULL)
  607. {
  608. MoreMessages = EE->MoreMessages;
  609. }
  610. UnicodeString Message;
  611. // this is always called from within ExceptionMessage check,
  612. // so it should never fail here
  613. DebugCheck(ExceptionMessageFormatted(E, Message));
  614. HelpKeyword = MergeHelpKeyword(HelpKeyword, GetExceptionHelpKeyword(E));
  615. std::unique_ptr<TStrings> OwnedMoreMessages;
  616. if (AppendExceptionStackTraceAndForget(MoreMessages))
  617. {
  618. OwnedMoreMessages.reset(MoreMessages);
  619. }
  620. return MoreMessageDialog(
  621. FORMAT(MessageFormat.IsEmpty() ? UnicodeString(L"%s") : MessageFormat, (Message)),
  622. MoreMessages, Type, Answers, HelpKeyword, Params);
  623. }
  624. //---------------------------------------------------------------------------
  625. unsigned int __fastcall FatalExceptionMessageDialog(Exception * E, TQueryType Type,
  626. int SessionReopenTimeout, const UnicodeString MessageFormat, unsigned int Answers,
  627. UnicodeString HelpKeyword, const TMessageParams * Params)
  628. {
  629. DebugAssert(FLAGCLEAR(Answers, qaRetry));
  630. Answers |= qaRetry;
  631. TQueryButtonAlias Aliases[1];
  632. Aliases[0].Button = qaRetry;
  633. Aliases[0].Alias = LoadStr(RECONNECT_BUTTON);
  634. TMessageParams AParams;
  635. if (Params != NULL)
  636. {
  637. AParams = *Params;
  638. }
  639. DebugAssert(AParams.Timeout == 0);
  640. // the condition is de facto excess
  641. if (SessionReopenTimeout > 0)
  642. {
  643. AParams.Timeout = SessionReopenTimeout;
  644. AParams.TimeoutAnswer = qaRetry;
  645. }
  646. DebugAssert(AParams.Aliases == NULL);
  647. AParams.Aliases = Aliases;
  648. AParams.AliasesCount = LENOF(Aliases);
  649. return ExceptionMessageDialog(E, Type, MessageFormat, Answers, HelpKeyword, &AParams);
  650. }
  651. //---------------------------------------------------------------------------
  652. //---------------------------------------------------------------------------
  653. static void __fastcall DoExceptNotify(TObject * ExceptObj, void * ExceptAddr,
  654. bool OSException, void * BaseOfStack)
  655. {
  656. if (ExceptObj != NULL)
  657. {
  658. Exception * E = dynamic_cast<Exception *>(ExceptObj);
  659. if ((E != NULL) && IsInternalException(E))
  660. {
  661. DoExceptionStackTrace(ExceptObj, ExceptAddr, OSException, BaseOfStack);
  662. TJclStackInfoList * StackInfoList = JclLastExceptStackList();
  663. if (DebugAlwaysTrue(StackInfoList != NULL))
  664. {
  665. std::unique_ptr<TStrings> StackTrace(StackInfoListToStrings(StackInfoList));
  666. DWORD ThreadID = GetCurrentThreadId();
  667. TGuard Guard(StackTraceCriticalSection.get());
  668. TStackTraceMap::iterator Iterator = StackTraceMap.find(ThreadID);
  669. if (Iterator != StackTraceMap.end())
  670. {
  671. Iterator->second->Add(L"");
  672. Iterator->second->AddStrings(StackTrace.get());
  673. }
  674. else
  675. {
  676. StackTraceMap.insert(std::make_pair(ThreadID, StackTrace.release()));
  677. }
  678. // this chains so that JclLastExceptStackList() returns NULL the next time
  679. // for the current thread
  680. delete StackInfoList;
  681. }
  682. }
  683. }
  684. }
  685. //---------------------------------------------------------------------------
  686. void * __fastcall BusyStart()
  687. {
  688. void * Token = reinterpret_cast<void *>(Screen->Cursor);
  689. Screen->Cursor = crHourGlass;
  690. return Token;
  691. }
  692. //---------------------------------------------------------------------------
  693. void __fastcall BusyEnd(void * Token)
  694. {
  695. Screen->Cursor = reinterpret_cast<TCursor>(Token);
  696. }
  697. //---------------------------------------------------------------------------
  698. //---------------------------------------------------------------------------
  699. static DWORD MainThread = 0;
  700. static TDateTime LastGUIUpdate = 0;
  701. static double GUIUpdateIntervalFrac = static_cast<double>(OneSecond/1000*GUIUpdateInterval); // 1/5 sec
  702. static bool NoGUI = false;
  703. //---------------------------------------------------------------------------
  704. void __fastcall SetNoGUI()
  705. {
  706. NoGUI = true;
  707. }
  708. //---------------------------------------------------------------------------
  709. bool __fastcall ProcessGUI(bool Force)
  710. {
  711. DebugAssert(MainThread != 0);
  712. bool Result = false;
  713. // Calling ProcessMessages in Azure WebJob causes access violation in VCL.
  714. // As we do not really need to call it in scripting/.NET, just skip it.
  715. if ((MainThread == GetCurrentThreadId()) && !NoGUI)
  716. {
  717. TDateTime N = Now();
  718. if (Force ||
  719. (double(N) - double(LastGUIUpdate) > GUIUpdateIntervalFrac))
  720. {
  721. LastGUIUpdate = N;
  722. Application->ProcessMessages();
  723. Result = true;
  724. }
  725. }
  726. return Result;
  727. }
  728. //---------------------------------------------------------------------------
  729. void __fastcall CopyParamListButton(TButton * Button)
  730. {
  731. if (!SupportsSplitButton())
  732. {
  733. MenuButton(Button);
  734. }
  735. }
  736. //---------------------------------------------------------------------------
  737. const int cpiDefault = -1;
  738. const int cpiConfigure = -2;
  739. const int cpiCustom = -3;
  740. const int cpiSaveSettings = -4;
  741. const int cpiGenerateCode = -5;
  742. //---------------------------------------------------------------------------
  743. void __fastcall CopyParamListPopup(TRect Rect, TPopupMenu * Menu,
  744. const TCopyParamType & Param, UnicodeString Preset, TNotifyEvent OnClick,
  745. int Options, int CopyParamAttrs, bool SaveSettings)
  746. {
  747. Menu->Items->Clear();
  748. TMenuItem * CustomizeItem = NULL;
  749. TMenuItem * Item;
  750. if (FLAGSET(Options, cplCustomize))
  751. {
  752. Item = new TMenuItem(Menu);
  753. Item->Caption = LoadStr(COPY_PARAM_CUSTOM);
  754. Item->Tag = cpiCustom;
  755. Item->Default = FLAGSET(Options, cplCustomizeDefault);
  756. Item->OnClick = OnClick;
  757. Menu->Items->Add(Item);
  758. CustomizeItem = Item;
  759. }
  760. if (FLAGSET(Options, cplSaveSettings))
  761. {
  762. Item = new TMenuItem(Menu);
  763. Item->Caption = LoadStr(COPY_PARAM_SAVE_SETTINGS);
  764. Item->Tag = cpiSaveSettings;
  765. Item->Checked = SaveSettings;
  766. Item->OnClick = OnClick;
  767. Menu->Items->Add(Item);
  768. }
  769. Item = new TMenuItem(Menu);
  770. Item->Caption = LoadStr(COPY_PARAM_PRESET_HEADER);
  771. Item->Visible = false;
  772. Item->Enabled = false;
  773. Menu->Items->Add(Item);
  774. bool AnyChecked = false;
  775. Item = new TMenuItem(Menu);
  776. Item->Caption = LoadStr(COPY_PARAM_DEFAULT);
  777. Item->Tag = cpiDefault;
  778. Item->Checked =
  779. Preset.IsEmpty() && (GUIConfiguration->CopyParamPreset[L""] == Param);
  780. AnyChecked = AnyChecked || Item->Checked;
  781. Item->OnClick = OnClick;
  782. Menu->Items->Add(Item);
  783. TCopyParamType DefaultParam;
  784. const TCopyParamList * CopyParamList = GUIConfiguration->CopyParamList;
  785. for (int i = 0; i < CopyParamList->Count; i++)
  786. {
  787. UnicodeString Name = CopyParamList->Names[i];
  788. TCopyParamType AParam = GUIConfiguration->CopyParamPreset[Name];
  789. if (AParam.AnyUsableCopyParam(CopyParamAttrs) ||
  790. // This makes "Binary" preset visible,
  791. // as long as we care about transfer mode
  792. ((AParam == DefaultParam) &&
  793. FLAGCLEAR(CopyParamAttrs, cpaIncludeMaskOnly) &&
  794. FLAGCLEAR(CopyParamAttrs, cpaNoTransferMode)))
  795. {
  796. Item = new TMenuItem(Menu);
  797. Item->Caption = Name;
  798. Item->Tag = i;
  799. Item->Checked =
  800. (Preset == Name) && (AParam == Param);
  801. AnyChecked = AnyChecked || Item->Checked;
  802. Item->OnClick = OnClick;
  803. Menu->Items->Add(Item);
  804. }
  805. }
  806. if (CustomizeItem != NULL)
  807. {
  808. CustomizeItem->Checked = !AnyChecked;
  809. }
  810. Item = new TMenuItem(Menu);
  811. Item->Caption = L"-";
  812. Menu->Items->Add(Item);
  813. Item = new TMenuItem(Menu);
  814. Item->Caption = LoadStr(COPY_PARAM_CONFIGURE);
  815. Item->Tag = cpiConfigure;
  816. Item->OnClick = OnClick;
  817. Menu->Items->Add(Item);
  818. if (FLAGSET(Options, cplGenerateCode))
  819. {
  820. Item = new TMenuItem(Menu);
  821. Item->Caption = L"-";
  822. Menu->Items->Add(Item);
  823. Item = new TMenuItem(Menu);
  824. Item->Caption = LoadStr(COPY_PARAM_GENERATE_CODE);
  825. Item->Tag = cpiGenerateCode;
  826. Item->OnClick = OnClick;
  827. Menu->Items->Add(Item);
  828. }
  829. MenuPopup(Menu, Rect, NULL);
  830. }
  831. //---------------------------------------------------------------------------
  832. int __fastcall CopyParamListPopupClick(TObject * Sender,
  833. TCopyParamType & Param, UnicodeString & Preset, int CopyParamAttrs,
  834. bool * SaveSettings)
  835. {
  836. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  837. DebugAssert(Item != NULL);
  838. DebugAssert((Item->Tag >= cpiGenerateCode) && (Item->Tag < GUIConfiguration->CopyParamList->Count));
  839. int Result;
  840. if (Item->Tag == cpiConfigure)
  841. {
  842. bool MatchedPreset = (GUIConfiguration->CopyParamPreset[Preset] == Param);
  843. DoPreferencesDialog(pmPresets);
  844. Result = (MatchedPreset && GUIConfiguration->HasCopyParamPreset[Preset]) ? 1 : 0;
  845. if (Result > 0)
  846. {
  847. // For cast, see a comment below
  848. Param = TCopyParamType(GUIConfiguration->CopyParamPreset[Preset]);
  849. }
  850. }
  851. else if (Item->Tag == cpiCustom)
  852. {
  853. Result = DoCopyParamCustomDialog(Param, CopyParamAttrs) ? 1 : 0;
  854. }
  855. else if (Item->Tag == cpiSaveSettings)
  856. {
  857. if (DebugAlwaysTrue(SaveSettings != NULL))
  858. {
  859. *SaveSettings = !*SaveSettings;
  860. }
  861. Result = 0;
  862. }
  863. else if (Item->Tag == cpiGenerateCode)
  864. {
  865. Result = -cplGenerateCode;
  866. }
  867. else
  868. {
  869. Preset = (Item->Tag >= 0) ?
  870. GUIConfiguration->CopyParamList->Names[Item->Tag] : UnicodeString();
  871. // The cast strips away the "queue" properties of the TGUICopyParamType
  872. // that are not configurable in presets
  873. Param = TCopyParamType(GUIConfiguration->CopyParamPreset[Preset]);
  874. Result = 1;
  875. }
  876. return Result;
  877. }
  878. //---------------------------------------------------------------------------
  879. //---------------------------------------------------------------------------
  880. class TCustomCommandPromptsDialog : public TCustomDialog
  881. {
  882. public:
  883. __fastcall TCustomCommandPromptsDialog(
  884. const UnicodeString & CustomCommandName, const UnicodeString & HelpKeyword,
  885. const TUnicodeStringVector & Prompts, const TUnicodeStringVector & Defaults);
  886. bool __fastcall Execute(TUnicodeStringVector & Values);
  887. private:
  888. UnicodeString __fastcall HistoryKey(int Index);
  889. std::vector<THistoryComboBox *> FEdits;
  890. TUnicodeStringVector FPrompts;
  891. UnicodeString FCustomCommandName;
  892. };
  893. //---------------------------------------------------------------------------
  894. __fastcall TCustomCommandPromptsDialog::TCustomCommandPromptsDialog(
  895. const UnicodeString & CustomCommandName, const UnicodeString & HelpKeyword,
  896. const TUnicodeStringVector & Prompts, const TUnicodeStringVector & Defaults) :
  897. TCustomDialog(HelpKeyword)
  898. {
  899. FCustomCommandName = CustomCommandName;
  900. Caption = FMTLOAD(CUSTOM_COMMANDS_PARAMS_TITLE, (FCustomCommandName));
  901. FPrompts = Prompts;
  902. DebugAssert(FPrompts.size() == Defaults.size());
  903. for (size_t Index = 0; Index < FPrompts.size(); Index++)
  904. {
  905. UnicodeString Prompt = FPrompts[Index];
  906. if (Prompt.IsEmpty())
  907. {
  908. Prompt = LoadStr(CUSTOM_COMMANDS_PARAM_PROMPT2);
  909. }
  910. THistoryComboBox * ComboBox = new THistoryComboBox(this);
  911. ComboBox->AutoComplete = false;
  912. AddComboBox(ComboBox, CreateLabel(Prompt));
  913. ComboBox->Items = CustomWinConfiguration->History[HistoryKey(Index)];
  914. ComboBox->Text = Defaults[Index];
  915. FEdits.push_back(ComboBox);
  916. }
  917. }
  918. //---------------------------------------------------------------------------
  919. UnicodeString __fastcall TCustomCommandPromptsDialog::HistoryKey(int Index)
  920. {
  921. UnicodeString Result = FPrompts[Index];
  922. if (Result.IsEmpty())
  923. {
  924. Result = IntToStr(Index);
  925. }
  926. Result = FORMAT(L"%s_%s", (FCustomCommandName, Result));
  927. Result = CustomWinConfiguration->GetValidHistoryKey(Result);
  928. return L"CustomCommandParam_" + Result;
  929. }
  930. //---------------------------------------------------------------------------
  931. bool __fastcall TCustomCommandPromptsDialog::Execute(TUnicodeStringVector & Values)
  932. {
  933. bool Result = TCustomDialog::Execute();
  934. if (Result)
  935. {
  936. for (size_t Index = 0; Index < FEdits.size(); Index++)
  937. {
  938. Values.push_back(FEdits[Index]->Text);
  939. FEdits[Index]->SaveToHistory();
  940. CustomWinConfiguration->History[HistoryKey(Index)] = FEdits[Index]->Items;
  941. }
  942. }
  943. return Result;
  944. }
  945. //---------------------------------------------------------------------------
  946. //---------------------------------------------------------------------------
  947. TWinInteractiveCustomCommand::TWinInteractiveCustomCommand(
  948. TCustomCommand * ChildCustomCommand, const UnicodeString CustomCommandName, const UnicodeString HelpKeyword) :
  949. TInteractiveCustomCommand(ChildCustomCommand)
  950. {
  951. FCustomCommandName = StripEllipsis(StripHotkey(CustomCommandName));
  952. FHelpKeyword = HelpKeyword;
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TWinInteractiveCustomCommand::PatternHint(int Index, const UnicodeString & Pattern)
  956. {
  957. if (IsPromptPattern(Pattern))
  958. {
  959. UnicodeString Prompt;
  960. UnicodeString Default;
  961. bool Delimit = false;
  962. ParsePromptPattern(Pattern, Prompt, Default, Delimit);
  963. FIndexes.insert(std::make_pair(Index, FPrompts.size()));
  964. FPrompts.push_back(Prompt);
  965. FDefaults.push_back(Default);
  966. }
  967. }
  968. //---------------------------------------------------------------------------
  969. void __fastcall TWinInteractiveCustomCommand::Prompt(
  970. int Index, const UnicodeString & /*Prompt*/, UnicodeString & Value)
  971. {
  972. if (DebugAlwaysTrue(FIndexes.find(Index) != FIndexes.end()))
  973. {
  974. size_t PromptIndex = FIndexes[Index];
  975. if (FValues.empty())
  976. {
  977. UnicodeString HelpKeyword = FHelpKeyword;
  978. if (HelpKeyword.IsEmpty())
  979. {
  980. HelpKeyword = HELP_CUSTOM_COMMAND_PARAM;
  981. }
  982. std::unique_ptr<TCustomCommandPromptsDialog> Dialog(
  983. new TCustomCommandPromptsDialog(FCustomCommandName, HelpKeyword, FPrompts, FDefaults));
  984. if (!Dialog->Execute(FValues))
  985. {
  986. Abort();
  987. }
  988. }
  989. if (DebugAlwaysTrue(FValues.size() == FPrompts.size()) &&
  990. DebugAlwaysTrue(PromptIndex < FValues.size()))
  991. {
  992. Value = FValues[PromptIndex];
  993. }
  994. }
  995. }
  996. //---------------------------------------------------------------------------
  997. void __fastcall TWinInteractiveCustomCommand::Execute(
  998. const UnicodeString & Command, UnicodeString & Value)
  999. {
  1000. // inspired by
  1001. // http://forum.codecall.net/topic/72472-execute-a-console-program-and-capture-its-output/
  1002. HANDLE StdOutOutput;
  1003. HANDLE StdOutInput;
  1004. HANDLE StdInOutput;
  1005. HANDLE StdInInput;
  1006. SECURITY_ATTRIBUTES SecurityAttributes;
  1007. SecurityAttributes.nLength = sizeof(SecurityAttributes);
  1008. SecurityAttributes.lpSecurityDescriptor = NULL;
  1009. SecurityAttributes.bInheritHandle = TRUE;
  1010. try
  1011. {
  1012. if (!CreatePipe(&StdOutOutput, &StdOutInput, &SecurityAttributes, 0))
  1013. {
  1014. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"out")));
  1015. }
  1016. else if (!CreatePipe(&StdInOutput, &StdInInput, &SecurityAttributes, 0))
  1017. {
  1018. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"in")));
  1019. }
  1020. else
  1021. {
  1022. STARTUPINFO StartupInfo;
  1023. PROCESS_INFORMATION ProcessInformation;
  1024. FillMemory(&StartupInfo, sizeof(StartupInfo), 0);
  1025. StartupInfo.cb = sizeof(StartupInfo);
  1026. StartupInfo.wShowWindow = SW_HIDE;
  1027. StartupInfo.hStdInput = StdInOutput;
  1028. StartupInfo.hStdOutput = StdOutInput;
  1029. StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
  1030. if (!CreateProcess(NULL, Command.c_str(), &SecurityAttributes, &SecurityAttributes,
  1031. TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartupInfo, &ProcessInformation))
  1032. {
  1033. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"process")));
  1034. }
  1035. else
  1036. {
  1037. try
  1038. {
  1039. // wait until the console program terminated
  1040. bool Running = true;
  1041. while (Running)
  1042. {
  1043. switch (WaitForSingleObject(ProcessInformation.hProcess, 200))
  1044. {
  1045. case WAIT_TIMEOUT:
  1046. Application->ProcessMessages();
  1047. break;
  1048. case WAIT_OBJECT_0:
  1049. Running = false;
  1050. break;
  1051. default:
  1052. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"wait")));
  1053. }
  1054. }
  1055. char Buffer[1024];
  1056. unsigned long Read;
  1057. while (PeekNamedPipe(StdOutOutput, NULL, 0, NULL, &Read, NULL) &&
  1058. (Read > 0))
  1059. {
  1060. if (!ReadFile(StdOutOutput, &Buffer, Read, &Read, NULL))
  1061. {
  1062. throw Exception(FMTLOAD(SHELL_PATTERN_ERROR, (Command, L"read")));
  1063. }
  1064. else if (Read > 0)
  1065. {
  1066. Value += AnsiToString(Buffer, Read);
  1067. }
  1068. }
  1069. // trim trailing cr/lf
  1070. Value = TrimRight(Value);
  1071. }
  1072. __finally
  1073. {
  1074. CloseHandle(ProcessInformation.hProcess);
  1075. CloseHandle(ProcessInformation.hThread);
  1076. }
  1077. }
  1078. }
  1079. }
  1080. __finally
  1081. {
  1082. if (StdOutOutput != INVALID_HANDLE_VALUE)
  1083. {
  1084. CloseHandle(StdOutOutput);
  1085. }
  1086. if (StdOutInput != INVALID_HANDLE_VALUE)
  1087. {
  1088. CloseHandle(StdOutInput);
  1089. }
  1090. if (StdInOutput != INVALID_HANDLE_VALUE)
  1091. {
  1092. CloseHandle(StdInOutput);
  1093. }
  1094. if (StdInInput != INVALID_HANDLE_VALUE)
  1095. {
  1096. CloseHandle(StdInInput);
  1097. }
  1098. }
  1099. }
  1100. //---------------------------------------------------------------------------
  1101. void __fastcall MenuPopup(TPopupMenu * Menu, TButton * Button)
  1102. {
  1103. MenuPopup(Menu, CalculatePopupRect(Button), Button);
  1104. }
  1105. //---------------------------------------------------------------------------
  1106. void __fastcall MenuPopup(TObject * Sender, const TPoint & MousePos, bool & Handled)
  1107. {
  1108. TControl * Control = dynamic_cast<TControl *>(Sender);
  1109. DebugAssert(Control != NULL);
  1110. TPoint Point;
  1111. if ((MousePos.x == -1) && (MousePos.y == -1))
  1112. {
  1113. Point = Control->ClientToScreen(TPoint(0, 0));
  1114. }
  1115. else
  1116. {
  1117. Point = Control->ClientToScreen(MousePos);
  1118. }
  1119. TPopupMenu * PopupMenu = (reinterpret_cast<TPublicControl *>(Control))->PopupMenu;
  1120. DebugAssert(PopupMenu != NULL);
  1121. TRect Rect(Point, Point);
  1122. MenuPopup(PopupMenu, Rect, Control);
  1123. Handled = true;
  1124. }
  1125. //---------------------------------------------------------------------------
  1126. TComponent * __fastcall GetPopupComponent(TObject * Sender)
  1127. {
  1128. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  1129. DebugAssert(Item != NULL);
  1130. TPopupMenu * PopupMenu = dynamic_cast<TPopupMenu *>(Item->Owner);
  1131. DebugAssert(PopupMenu != NULL);
  1132. DebugAssert(PopupMenu->PopupComponent != NULL);
  1133. return PopupMenu->PopupComponent;
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. static void __fastcall SetMenuButtonImages(TButton * Button)
  1137. {
  1138. Button->Images = GetButtonImages(Button);
  1139. }
  1140. //---------------------------------------------------------------------------
  1141. static void __fastcall MenuButtonRescale(TComponent * Sender, TObject * /*Token*/)
  1142. {
  1143. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  1144. SetMenuButtonImages(Button);
  1145. }
  1146. //---------------------------------------------------------------------------
  1147. void __fastcall MenuButton(TButton * Button)
  1148. {
  1149. SetMenuButtonImages(Button);
  1150. Button->ImageIndex = 0;
  1151. Button->DisabledImageIndex = 1;
  1152. Button->ImageAlignment = iaRight;
  1153. SetRescaleFunction(Button, MenuButtonRescale);
  1154. }
  1155. //---------------------------------------------------------------------------
  1156. TRect __fastcall CalculatePopupRect(TButton * Button)
  1157. {
  1158. TPoint UpPoint = Button->ClientToScreen(TPoint(0, 0));
  1159. TPoint DownPoint = Button->ClientToScreen(TPoint(Button->Width, Button->Height));
  1160. TRect Rect(UpPoint, DownPoint);
  1161. // With themes enabled, button are rendered 1 pixel smaller than their actual size
  1162. int Offset = UseThemes() ? -1 : 0;
  1163. Rect.Inflate(Offset, Offset);
  1164. return Rect;
  1165. }
  1166. //---------------------------------------------------------------------------
  1167. TRect __fastcall CalculatePopupRect(TControl * Control, TPoint MousePos)
  1168. {
  1169. MousePos = Control->ClientToScreen(MousePos);
  1170. TRect Rect(MousePos, MousePos);
  1171. return Rect;
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. void __fastcall FixButtonImage(TButton * Button)
  1175. {
  1176. // with themes enabled, button image is by default drawn too high
  1177. if (UseThemes())
  1178. {
  1179. Button->ImageMargins->Top = 1;
  1180. }
  1181. }
  1182. //---------------------------------------------------------------------------
  1183. void __fastcall CenterButtonImage(TButton * Button)
  1184. {
  1185. // with themes disabled, the text seems to be drawn over the icon,
  1186. // so that the padding spaces hide away most of the icon
  1187. if (UseThemes())
  1188. {
  1189. Button->ImageAlignment = iaCenter;
  1190. int ImageWidth = Button->Images->Width;
  1191. std::unique_ptr<TControlCanvas> Canvas(new TControlCanvas());
  1192. Canvas->Control = Button;
  1193. UnicodeString Caption = Button->Caption;
  1194. UnicodeString Padding;
  1195. while (Canvas->TextWidth(Padding) < ImageWidth)
  1196. {
  1197. Padding += L" ";
  1198. }
  1199. if (Button->IsRightToLeft())
  1200. {
  1201. Caption = Caption + Padding;
  1202. }
  1203. else
  1204. {
  1205. Caption = Padding + Caption;
  1206. }
  1207. Button->Caption = Caption;
  1208. int CaptionWidth = Canvas->TextWidth(Caption);
  1209. // The margins seem to extend the area over which the image is centered,
  1210. // so we have to set it to a double of desired padding.
  1211. // The original formula is - 2 * ((CaptionWidth / 2) - (ImageWidth / 2) + ScaleByTextHeight(Button, 2))
  1212. // the one below is equivalent, but with reduced rouding.
  1213. // Without the change, the rouding caused the space between icon and caption too
  1214. // small on 200% zoom.
  1215. // Note that (CaptionWidth / 2) - (ImageWidth / 2)
  1216. // is approximatelly same as half of caption width before padding.
  1217. Button->ImageMargins->Left = -(CaptionWidth - ImageWidth + ScaleByTextHeight(Button, 4));
  1218. }
  1219. else
  1220. {
  1221. // at least do not draw it so near to the edge
  1222. Button->ImageMargins->Left = 1;
  1223. }
  1224. }
  1225. //---------------------------------------------------------------------------
  1226. int __fastcall AdjustLocaleFlag(const UnicodeString & S, TLocaleFlagOverride LocaleFlagOverride, bool Recommended, int On, int Off)
  1227. {
  1228. int Result = !S.IsEmpty() && StrToInt(S);
  1229. switch (LocaleFlagOverride)
  1230. {
  1231. default:
  1232. case lfoLanguageIfRecommended:
  1233. if (!Recommended)
  1234. {
  1235. Result = Off;
  1236. }
  1237. break;
  1238. case lfoLanguage:
  1239. // noop = as configured in locale
  1240. break;
  1241. case lfoAlways:
  1242. Result = On;
  1243. break;
  1244. case lfoNever:
  1245. Result = Off;
  1246. break;
  1247. }
  1248. return Result;
  1249. }
  1250. //---------------------------------------------------------------------------
  1251. void __fastcall SetGlobalMinimizeHandler(TCustomForm * /*Form*/, TNotifyEvent OnMinimize)
  1252. {
  1253. if (GlobalOnMinimize == NULL)
  1254. {
  1255. GlobalOnMinimize = OnMinimize;
  1256. }
  1257. }
  1258. //---------------------------------------------------------------------------
  1259. void __fastcall ClearGlobalMinimizeHandler(TNotifyEvent OnMinimize)
  1260. {
  1261. if (GlobalOnMinimize == OnMinimize)
  1262. {
  1263. GlobalOnMinimize = NULL;
  1264. }
  1265. }
  1266. //---------------------------------------------------------------------------
  1267. void __fastcall CallGlobalMinimizeHandler(TObject * Sender)
  1268. {
  1269. Configuration->Usage->Inc(L"OperationMinimizations");
  1270. if (DebugAlwaysTrue(GlobalOnMinimize != NULL))
  1271. {
  1272. GlobalOnMinimize(Sender);
  1273. }
  1274. }
  1275. //---------------------------------------------------------------------------
  1276. static void __fastcall DoApplicationMinimizeRestore(bool Minimize)
  1277. {
  1278. // WORKAROUND
  1279. // When main window is hidden (command-line operation),
  1280. // we do not want it to be shown by TApplication.Restore,
  1281. // so we temporarily detach it from an application.
  1282. // Probably not really necessary for minimizing phase,
  1283. // but we do it for consistency anyway.
  1284. TForm * MainForm = Application->MainForm;
  1285. bool RestoreMainForm = false;
  1286. if (DebugAlwaysTrue(MainForm != NULL) &&
  1287. !MainForm->Visible)
  1288. {
  1289. SetAppMainForm(NULL);
  1290. RestoreMainForm = true;
  1291. }
  1292. try
  1293. {
  1294. if (Minimize)
  1295. {
  1296. Application->Minimize();
  1297. }
  1298. else
  1299. {
  1300. Application->Restore();
  1301. }
  1302. }
  1303. __finally
  1304. {
  1305. if (RestoreMainForm)
  1306. {
  1307. SetAppMainForm(MainForm);
  1308. }
  1309. }
  1310. }
  1311. //---------------------------------------------------------------------------
  1312. void __fastcall ApplicationMinimize()
  1313. {
  1314. DoApplicationMinimizeRestore(true);
  1315. }
  1316. //---------------------------------------------------------------------------
  1317. void __fastcall ApplicationRestore()
  1318. {
  1319. DoApplicationMinimizeRestore(false);
  1320. }
  1321. //---------------------------------------------------------------------------
  1322. bool __fastcall IsApplicationMinimized()
  1323. {
  1324. // VCL help recommends handling Application->OnMinimize/OnRestore
  1325. // for tracking state, but OnRestore is actually not called
  1326. // (OnMinimize is), when app is minimized from e.g. Progress window
  1327. bool AppMinimized = IsIconic(Application->Handle);
  1328. bool MainFormMinimized = IsIconic(Application->MainFormHandle);
  1329. return AppMinimized || MainFormMinimized;
  1330. }
  1331. //---------------------------------------------------------------------------
  1332. bool __fastcall HandleMinimizeSysCommand(TMessage & Message)
  1333. {
  1334. TWMSysCommand & SysCommand = reinterpret_cast<TWMSysCommand &>(Message);
  1335. unsigned int Cmd = (SysCommand.CmdType & 0xFFF0);
  1336. bool Result = (Cmd == SC_MINIMIZE);
  1337. if (Result)
  1338. {
  1339. ApplicationMinimize();
  1340. SysCommand.Result = 1;
  1341. }
  1342. return Result;
  1343. }
  1344. //---------------------------------------------------------------------------
  1345. void __fastcall ClickToolbarItem(TTBCustomItem * Item, bool PositionCursor)
  1346. {
  1347. TTBCustomItem * TopItem = Item;
  1348. while (TopItem->Parent != NULL)
  1349. {
  1350. TopItem = TopItem->Parent;
  1351. }
  1352. TTBCustomToolbar * Toolbar = dynamic_cast<TTBCustomToolbar *>(TopItem->ParentComponent);
  1353. DebugAssert(Toolbar != NULL);
  1354. TTBItemViewer * Viewer = Toolbar->View->Find(Item);
  1355. DebugAssert(Viewer != NULL);
  1356. int X = Viewer->BoundsRect.Left + (Viewer->BoundsRect.Width() / 2);
  1357. int Y = Viewer->BoundsRect.Top + (Viewer->BoundsRect.Height() / 2);
  1358. if (PositionCursor)
  1359. {
  1360. Mouse->CursorPos = Toolbar->ClientToScreen(TPoint(X, Y));
  1361. }
  1362. PostMessage(Toolbar->Handle, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(X, Y));
  1363. }
  1364. //---------------------------------------------------------------------------
  1365. void __fastcall WinInitialize()
  1366. {
  1367. if (JclHookExceptions())
  1368. {
  1369. JclStackTrackingOptions << stAllModules;
  1370. JclAddExceptNotifier(DoExceptNotify, npFirstChain);
  1371. }
  1372. SetErrorMode(SEM_FAILCRITICALERRORS);
  1373. OnApiPath = ApiPath;
  1374. MainThread = GetCurrentThreadId();
  1375. #pragma warn -8111
  1376. #pragma warn .8111
  1377. }
  1378. //---------------------------------------------------------------------------
  1379. void __fastcall WinFinalize()
  1380. {
  1381. JclRemoveExceptNotifier(DoExceptNotify);
  1382. }
  1383. //---------------------------------------------------------------------------
  1384. __fastcall ::TTrayIcon::TTrayIcon(unsigned int Id)
  1385. {
  1386. FVisible = false;
  1387. FOnClick = NULL;
  1388. FOnBalloonClick = NULL;
  1389. FBalloonUserData = NULL;
  1390. FTrayIcon = new NOTIFYICONDATA;
  1391. memset(FTrayIcon, 0, sizeof(*FTrayIcon));
  1392. FTrayIcon->cbSize = sizeof(*FTrayIcon);
  1393. FTrayIcon->uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  1394. // LoadIconMetric is available from Windows Vista only
  1395. HMODULE ComCtl32Dll = GetModuleHandle(comctl32);
  1396. if (DebugAlwaysTrue(ComCtl32Dll))
  1397. {
  1398. typedef HRESULT WINAPI (* TLoadIconMetric)(HINSTANCE hinst, PCWSTR pszName, int lims, __out HICON *phico);
  1399. TLoadIconMetric LoadIconMetric = (TLoadIconMetric)GetProcAddress(ComCtl32Dll, "LoadIconMetric");
  1400. if (LoadIconMetric != NULL)
  1401. {
  1402. // Prefer not to use Application->Icon->Handle as that shows 32x32 scaled down to 16x16 for some reason
  1403. LoadIconMetric(MainInstance, L"MAINICON", LIM_SMALL, &FTrayIcon->hIcon);
  1404. }
  1405. }
  1406. if (FTrayIcon->hIcon == 0)
  1407. {
  1408. FTrayIcon->hIcon = Application->Icon->Handle;
  1409. }
  1410. FTrayIcon->uID = Id;
  1411. FTrayIcon->hWnd = AllocateHWnd(WndProc);
  1412. FTrayIcon->uCallbackMessage = WM_TRAY_ICON;
  1413. FTaskbarCreatedMsg = RegisterWindowMessage(L"TaskbarCreated");
  1414. }
  1415. //---------------------------------------------------------------------------
  1416. __fastcall ::TTrayIcon::~TTrayIcon()
  1417. {
  1418. // make sure we hide icon even in case it was shown just to pop up the balloon
  1419. // (in which case Visible == false)
  1420. CancelBalloon();
  1421. Visible = false;
  1422. DeallocateHWnd(FTrayIcon->hWnd);
  1423. delete FTrayIcon;
  1424. }
  1425. //---------------------------------------------------------------------------
  1426. void __fastcall ::TTrayIcon::PopupBalloon(UnicodeString Title,
  1427. const UnicodeString & Str, TQueryType QueryType, unsigned int Timeout,
  1428. TNotifyEvent OnBalloonClick, TObject * BalloonUserData)
  1429. {
  1430. if (Timeout > 30000)
  1431. {
  1432. // this is probably system limit, do not try more, especially for
  1433. // the timeout-driven hiding of the tray icon (for Win2k)
  1434. Timeout = 30000;
  1435. }
  1436. FTrayIcon->uFlags |= NIF_INFO;
  1437. Title = FORMAT(L"%s - %s", (Title, AppNameString()));
  1438. StrPLCopy(FTrayIcon->szInfoTitle, Title, LENOF(FTrayIcon->szInfoTitle) - 1);
  1439. UnicodeString Info = Str;
  1440. // When szInfo is empty, balloon is not shown
  1441. // (or actually it means the balloon should be deleted, if any)
  1442. if (Info.IsEmpty())
  1443. {
  1444. Info = L" ";
  1445. }
  1446. StrPLCopy(FTrayIcon->szInfo, Info, LENOF(FTrayIcon->szInfo) - 1);
  1447. FTrayIcon->uTimeout = Timeout;
  1448. switch (QueryType)
  1449. {
  1450. case qtError:
  1451. FTrayIcon->dwInfoFlags = NIIF_ERROR;
  1452. break;
  1453. case qtInformation:
  1454. case qtConfirmation:
  1455. FTrayIcon->dwInfoFlags = NIIF_INFO;
  1456. break;
  1457. case qtWarning:
  1458. default:
  1459. FTrayIcon->dwInfoFlags = NIIF_WARNING;
  1460. break;
  1461. }
  1462. KillTimer(FTrayIcon->hWnd, 1);
  1463. if (Visible)
  1464. {
  1465. Update();
  1466. }
  1467. else
  1468. {
  1469. Notify(NIM_ADD);
  1470. }
  1471. FOnBalloonClick = OnBalloonClick;
  1472. delete FBalloonUserData;
  1473. FBalloonUserData = BalloonUserData;
  1474. // Clearing the flag ensures that subsequent updates does not hide the baloon
  1475. // unless CancelBalloon is called explicitly
  1476. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  1477. }
  1478. //---------------------------------------------------------------------------
  1479. void __fastcall ::TTrayIcon::BalloonCancelled()
  1480. {
  1481. FOnBalloonClick = NULL;
  1482. delete FBalloonUserData;
  1483. FBalloonUserData = NULL;
  1484. }
  1485. //---------------------------------------------------------------------------
  1486. void __fastcall ::TTrayIcon::CancelBalloon()
  1487. {
  1488. KillTimer(FTrayIcon->hWnd, 1);
  1489. if (Visible)
  1490. {
  1491. FTrayIcon->uFlags |= NIF_INFO;
  1492. FTrayIcon->szInfo[0] = L'\0';
  1493. Update();
  1494. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  1495. }
  1496. else
  1497. {
  1498. Notify(NIM_DELETE);
  1499. }
  1500. BalloonCancelled();
  1501. }
  1502. //---------------------------------------------------------------------------
  1503. bool __fastcall ::TTrayIcon::Notify(unsigned int Message)
  1504. {
  1505. bool Result = SUCCEEDED(Shell_NotifyIcon(Message, (NOTIFYICONDATA*)FTrayIcon));
  1506. if (Result && (Message == NIM_ADD))
  1507. {
  1508. UINT Timeout = FTrayIcon->uTimeout;
  1509. try
  1510. {
  1511. FTrayIcon->uVersion = NOTIFYICON_VERSION;
  1512. Result = SUCCEEDED(Shell_NotifyIcon(NIM_SETVERSION, (NOTIFYICONDATA*)FTrayIcon));
  1513. }
  1514. __finally
  1515. {
  1516. FTrayIcon->uTimeout = Timeout;
  1517. }
  1518. }
  1519. return Result;
  1520. }
  1521. //---------------------------------------------------------------------------
  1522. void __fastcall ::TTrayIcon::Update()
  1523. {
  1524. if (Visible)
  1525. {
  1526. Notify(NIM_MODIFY);
  1527. }
  1528. }
  1529. //---------------------------------------------------------------------------
  1530. void __fastcall ::TTrayIcon::SetVisible(bool value)
  1531. {
  1532. if (Visible != value)
  1533. {
  1534. if (value)
  1535. {
  1536. FVisible = Notify(NIM_ADD);
  1537. }
  1538. else
  1539. {
  1540. FVisible = false;
  1541. KillTimer(FTrayIcon->hWnd, 1);
  1542. Notify(NIM_DELETE);
  1543. BalloonCancelled();
  1544. }
  1545. }
  1546. }
  1547. //---------------------------------------------------------------------------
  1548. void __fastcall ::TTrayIcon::WndProc(TMessage & Message)
  1549. {
  1550. try
  1551. {
  1552. if (Message.Msg == WM_TRAY_ICON)
  1553. {
  1554. DebugAssert(Message.WParam == 0);
  1555. switch (Message.LParam)
  1556. {
  1557. // old shell32
  1558. case WM_LBUTTONUP:
  1559. case WM_RBUTTONUP:
  1560. // new shell32:
  1561. case WM_CONTEXTMENU:
  1562. if (OnClick != NULL)
  1563. {
  1564. OnClick(NULL);
  1565. }
  1566. Message.Result = true;
  1567. break;
  1568. }
  1569. if (Message.LParam == NIN_BALLOONUSERCLICK)
  1570. {
  1571. if (FOnBalloonClick != NULL)
  1572. {
  1573. // prevent the user data from being freed by possible call
  1574. // to CancelBalloon or PopupBalloon during call to OnBalloonClick
  1575. std::unique_ptr<TObject> UserData(FBalloonUserData);
  1576. FBalloonUserData = NULL;
  1577. FOnBalloonClick(UserData.get());
  1578. }
  1579. else if (OnClick != NULL)
  1580. {
  1581. OnClick(NULL);
  1582. }
  1583. }
  1584. switch (Message.LParam)
  1585. {
  1586. case NIN_BALLOONHIDE:
  1587. case NIN_BALLOONTIMEOUT:
  1588. case NIN_BALLOONUSERCLICK:
  1589. KillTimer(FTrayIcon->hWnd, 1);
  1590. // if icon was shown just to display balloon, hide it with the balloon
  1591. if (!Visible)
  1592. {
  1593. Notify(NIM_DELETE);
  1594. }
  1595. BalloonCancelled();
  1596. break;
  1597. }
  1598. }
  1599. else if (Message.Msg == WM_TIMER)
  1600. {
  1601. // sanity check
  1602. Notify(NIM_DELETE);
  1603. BalloonCancelled();
  1604. }
  1605. else if (Message.Msg == FTaskbarCreatedMsg)
  1606. {
  1607. if (Visible)
  1608. {
  1609. // force recreation
  1610. Visible = false;
  1611. Visible = true;
  1612. }
  1613. }
  1614. else
  1615. {
  1616. Message.Result = DefWindowProc(FTrayIcon->hWnd, Message.Msg, Message.WParam, Message.LParam);
  1617. }
  1618. }
  1619. catch(Exception & E)
  1620. {
  1621. Application->HandleException(&E);
  1622. }
  1623. }
  1624. //---------------------------------------------------------------------------
  1625. UnicodeString __fastcall ::TTrayIcon::GetHint()
  1626. {
  1627. return FTrayIcon->szTip;
  1628. }
  1629. //---------------------------------------------------------------------------
  1630. void __fastcall ::TTrayIcon::SetHint(UnicodeString value)
  1631. {
  1632. if (Hint != value)
  1633. {
  1634. unsigned int Max = LENOF(FTrayIcon->szTip);
  1635. StrPLCopy(FTrayIcon->szTip, value, Max - 1);
  1636. Update();
  1637. }
  1638. }
  1639. //---------------------------------------------------------------------------