WinInterface.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <shlwapi.h>
  5. #include <MoreButton.hpp>
  6. #include <Common.h>
  7. #include <Exceptions.h>
  8. #include <CoreMain.h>
  9. #include <TextsCore.h>
  10. #include <TextsWin.h>
  11. #include <HelpWin.h>
  12. #include <Interface.h>
  13. #include <VCLCommon.h>
  14. #include "WinInterface.h"
  15. #include "CustomWinConfiguration.h"
  16. #include "GUITools.h"
  17. #include "ProgParams.h"
  18. //---------------------------------------------------------------------------
  19. #pragma package(smart_init)
  20. //---------------------------------------------------------------------------
  21. #define WM_TRAY_ICON (WM_WINSCP_USER + 5)
  22. //---------------------------------------------------------------------
  23. // initialize an instance
  24. TProgramParams ProgramParams;
  25. TNotifyEvent GlobalOnMinimize = NULL;
  26. //---------------------------------------------------------------------
  27. void __fastcall FormHelp(TForm * Form)
  28. {
  29. InvokeHelp(Form->ActiveControl != NULL ? Form->ActiveControl : Form);
  30. }
  31. //---------------------------------------------------------------------------
  32. TMessageParams::TMessageParams(unsigned int AParams)
  33. {
  34. Reset();
  35. Params = AParams;
  36. }
  37. //---------------------------------------------------------------------------
  38. TMessageParams::TMessageParams(const TQueryParams * AParams)
  39. {
  40. Reset();
  41. if (AParams != NULL)
  42. {
  43. Aliases = AParams->Aliases;
  44. AliasesCount = AParams->AliasesCount;
  45. Timer = AParams->Timer;
  46. TimerEvent = AParams->TimerEvent;
  47. TimerMessage = AParams->TimerMessage;
  48. TimerAnswers = AParams->TimerAnswers;
  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 = "";
  70. TimerAnswers = 0;
  71. NewerAskAgainAnswer = 0;
  72. Timeout = 0;
  73. TimeoutAnswer = 0;
  74. }
  75. //---------------------------------------------------------------------------
  76. inline bool MapButton(unsigned int Answer, TMsgDlgBtn & Button)
  77. {
  78. bool Result = true;
  79. #define MAP_BUTTON(TYPE) if (Answer == qa ## TYPE) Button = mb ## TYPE; else
  80. MAP_BUTTON(Yes)
  81. MAP_BUTTON(No)
  82. MAP_BUTTON(OK)
  83. MAP_BUTTON(Cancel)
  84. MAP_BUTTON(Abort)
  85. MAP_BUTTON(Retry)
  86. MAP_BUTTON(Ignore)
  87. MAP_BUTTON(All)
  88. MAP_BUTTON(NoToAll)
  89. MAP_BUTTON(YesToAll)
  90. MAP_BUTTON(Help)
  91. #undef MAP_BUTTON
  92. if (Answer == qaSkip) Button = mbIgnore;
  93. else
  94. Result = false;
  95. return Result;
  96. }
  97. //---------------------------------------------------------------------------
  98. static void __fastcall NeverAskAgainCheckClick(void * /*Data*/, TObject * Sender)
  99. {
  100. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(Sender);
  101. assert(CheckBox != NULL);
  102. TForm * Dialog = dynamic_cast<TForm *>(CheckBox->Owner);
  103. assert(Dialog != NULL);
  104. TModalResult PositiveAnswer = mrNone;
  105. if (CheckBox->Checked)
  106. {
  107. if (CheckBox->Tag > 0)
  108. {
  109. PositiveAnswer = CheckBox->Tag - 1;
  110. }
  111. else
  112. {
  113. TModalResult PositiveAnswers[] = { mrYes, mrOk, mrYesToAll };
  114. for (int i = 0; i < LENOF(PositiveAnswers); i++)
  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 == PositiveAnswers[i])
  122. {
  123. PositiveAnswer = PositiveAnswers[i];
  124. break;
  125. }
  126. }
  127. }
  128. if (PositiveAnswer != mrNone)
  129. {
  130. break;
  131. }
  132. }
  133. }
  134. assert(PositiveAnswer != mrNone);
  135. }
  136. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  137. {
  138. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  139. if ((Button != NULL) && (Button->ModalResult != mrNone) &&
  140. (Button->ModalResult != mrCancel))
  141. {
  142. Button->Enabled = !CheckBox->Checked || (Button->ModalResult == PositiveAnswer);
  143. }
  144. }
  145. }
  146. //---------------------------------------------------------------------------
  147. int MapResult(int Result, unsigned int Answers)
  148. {
  149. int Answer;
  150. switch (Result)
  151. {
  152. #define MAP_RESULT(RESULT) case mr ## RESULT: Answer = qa ## RESULT; break;
  153. MAP_RESULT(Abort);
  154. MAP_RESULT(All);
  155. MAP_RESULT(NoToAll);
  156. MAP_RESULT(YesToAll);
  157. MAP_RESULT(Yes);
  158. MAP_RESULT(No);
  159. MAP_RESULT(Retry);
  160. #undef MAP_RESULT
  161. case mrCancel:
  162. // mrCancel is returned always when X button is pressed, despite
  163. // no Cancel button was on the dialog. Find valid "cancel" answer.
  164. Answer = CancelAnswer(Answers);
  165. break;
  166. case mrOk:
  167. Answer = qaOK;
  168. break;
  169. case mrIgnore:
  170. Answer = (Answers & qaSkip) ? qaSkip : qaIgnore;
  171. break;
  172. default:
  173. assert(false);
  174. Answer = CancelAnswer(Answers);
  175. break;
  176. }
  177. return Answer;
  178. }
  179. //---------------------------------------------------------------------------
  180. TForm * __fastcall CreateMessageDialogEx(const AnsiString Msg,
  181. TStrings * MoreMessages, TQueryType Type, int Answers, AnsiString HelpKeyword,
  182. const TMessageParams * Params, TButton *& TimeoutButton)
  183. {
  184. TMsgDlgBtn TimeoutResult = mbHelp;
  185. TMsgDlgButtons Buttons;
  186. TMsgDlgType DlgType;
  187. switch (Type) {
  188. case qtConfirmation: DlgType = mtConfirmation; break;
  189. case qtInformation: DlgType = mtInformation; break;
  190. case qtError: DlgType = mtError; break;
  191. case qtWarning: DlgType = mtWarning; break;
  192. default: assert(false);
  193. }
  194. int AAnswers = Answers;
  195. unsigned int Answer = 0x01;
  196. while (AAnswers > 0)
  197. {
  198. if ((AAnswers & Answer) != 0)
  199. {
  200. TMsgDlgBtn Button;
  201. if (MapButton(Answer, Button))
  202. {
  203. Buttons << Button;
  204. if ((Params != NULL) && (Params->Timeout > 0) &&
  205. (Params->TimeoutAnswer == Answer))
  206. {
  207. TimeoutResult = Button;
  208. }
  209. }
  210. AAnswers &= ~Answer;
  211. }
  212. Answer <<= 1;
  213. }
  214. assert(!Buttons.Empty());
  215. if (!HelpKeyword.IsEmpty())
  216. {
  217. Buttons << mbHelp;
  218. }
  219. if ((MoreMessages != NULL) && (MoreMessages->Count == 0))
  220. {
  221. MoreMessages = NULL;
  222. }
  223. TForm * Dialog;
  224. if ((Params == NULL) || (Params->Aliases == NULL))
  225. {
  226. Dialog = CreateMoreMessageDialog(Msg, MoreMessages, DlgType, Buttons,
  227. NULL, 0, TimeoutResult, &TimeoutButton);
  228. }
  229. else
  230. {
  231. TQueryButtonAlias * Aliases = new TQueryButtonAlias[Params->AliasesCount];
  232. try
  233. {
  234. for (unsigned int i = 0; i < Params->AliasesCount; i++)
  235. {
  236. TMsgDlgBtn Button;
  237. CHECK(MapButton(Params->Aliases[i].Button, Button));
  238. Aliases[i].Button = Button;
  239. Aliases[i].Alias = Params->Aliases[i].Alias;
  240. Aliases[i].OnClick = Params->Aliases[i].OnClick;
  241. }
  242. Dialog = CreateMoreMessageDialog(Msg, MoreMessages, DlgType, Buttons,
  243. Aliases, Params->AliasesCount, TimeoutResult, &TimeoutButton);
  244. }
  245. __finally
  246. {
  247. delete[] Aliases;
  248. }
  249. }
  250. try
  251. {
  252. if (Answers & qaSkip)
  253. {
  254. TButton * IgnoreButton = dynamic_cast<TButton *>(Dialog->FindComponent("Ignore"));
  255. assert(IgnoreButton);
  256. IgnoreButton->Caption = LoadStr(SKIP_BUTTON);
  257. }
  258. if ((Params != NULL) && (Params->Params & mpNeverAskAgainCheck))
  259. {
  260. static const int VertSpace = 20;
  261. Dialog->ClientHeight = Dialog->ClientHeight + VertSpace;
  262. for (int Index = 0; Index < Dialog->ControlCount; Index++)
  263. {
  264. TControl * Control = Dialog->Controls[Index];
  265. if (Control->Anchors.Contains(akBottom))
  266. {
  267. if (Control->Anchors.Contains(akTop))
  268. {
  269. Control->Height = Control->Height - VertSpace;
  270. }
  271. else
  272. {
  273. Control->Top = Control->Top - VertSpace;
  274. }
  275. }
  276. }
  277. TCheckBox * NeverAskAgainCheck = new TCheckBox(Dialog);
  278. NeverAskAgainCheck->Name = "NeverAskAgainCheck";
  279. NeverAskAgainCheck->Parent = Dialog;
  280. NeverAskAgainCheck->BoundsRect = TRect(60, Dialog->ClientHeight - 27,
  281. Dialog->ClientWidth - 10, Dialog->ClientHeight - 5);
  282. NeverAskAgainCheck->Caption =
  283. ((Params != NULL) && !Params->NewerAskAgainTitle.IsEmpty()) ?
  284. Params->NewerAskAgainTitle :
  285. // qaOK | qaIgnore is used, when custom "non-answer" button is required
  286. LoadStr(((Answers == qaOK) || (Answers == (qaOK | qaIgnore))) ?
  287. NEVER_SHOW_AGAIN : NEVER_ASK_AGAIN);
  288. NeverAskAgainCheck->Checked = false;
  289. NeverAskAgainCheck->Anchors = TAnchors() << akBottom << akLeft;
  290. if ((Params != NULL) && (Params->NewerAskAgainAnswer > 0))
  291. {
  292. TMsgDlgBtn Button;
  293. if (MapButton(Params->NewerAskAgainAnswer, Button))
  294. {
  295. extern const int ModalResults[];
  296. NeverAskAgainCheck->Tag = ModalResults[Button] + 1;
  297. }
  298. }
  299. TNotifyEvent OnClick;
  300. ((TMethod*)&OnClick)->Code = NeverAskAgainCheckClick;
  301. NeverAskAgainCheck->OnClick = OnClick;
  302. }
  303. Dialog->HelpKeyword = HelpKeyword;
  304. Dialog->Position = poMainFormCenter;
  305. // must be called after setting Position
  306. ResetSystemSettings(Dialog);
  307. }
  308. catch(...)
  309. {
  310. delete Dialog;
  311. throw;
  312. }
  313. return Dialog;
  314. }
  315. //---------------------------------------------------------------------------
  316. int __fastcall ExecuteMessageDialog(TForm * Dialog, int Answers, const TMessageParams * Params)
  317. {
  318. TMoreButton * MoreButton = dynamic_cast<TMoreButton *>(Dialog->FindComponent("MoreButton"));
  319. if (MoreButton != NULL)
  320. {
  321. // WinConfiguration may be destroyed already, if called from
  322. // try ... catch statement of main()
  323. MoreButton->Expanded = (GUIConfiguration != NULL) &&
  324. GUIConfiguration->ErrorDialogExpanded;
  325. }
  326. FlashOnBackground();
  327. int Answer = MapResult(Dialog->ShowModal(), Answers);
  328. if ((Params != NULL) && (Params->Params & mpNeverAskAgainCheck))
  329. {
  330. TCheckBox * NeverAskAgainCheck =
  331. dynamic_cast<TCheckBox *>(Dialog->FindComponent("NeverAskAgainCheck"));
  332. assert(NeverAskAgainCheck);
  333. if (NeverAskAgainCheck->Checked)
  334. {
  335. bool PossitiveAnswer =
  336. (Params->NewerAskAgainAnswer > 0) ?
  337. (Answer == Params->NewerAskAgainAnswer) :
  338. (Answer == qaYes || Answer == qaOK || Answer == qaYesToAll);
  339. if (PossitiveAnswer)
  340. {
  341. Answer = qaNeverAskAgain;
  342. }
  343. }
  344. }
  345. if ((MoreButton != NULL) && (GUIConfiguration != NULL))
  346. {
  347. // store state even when user selects 'Cancel'?
  348. GUIConfiguration->ErrorDialogExpanded = MoreButton->Expanded;
  349. }
  350. return Answer;
  351. }
  352. //---------------------------------------------------------------------------
  353. class TMessageTimer : public TTimer
  354. {
  355. public:
  356. TQueryParamsTimerEvent Event;
  357. unsigned int Result;
  358. TForm * Dialog;
  359. __fastcall TMessageTimer(TComponent * AOwner);
  360. protected:
  361. void __fastcall DoTimer(TObject * Sender);
  362. };
  363. //---------------------------------------------------------------------------
  364. __fastcall TMessageTimer::TMessageTimer(TComponent * AOwner) : TTimer(AOwner)
  365. {
  366. Result = 0;
  367. Event = NULL;
  368. OnTimer = DoTimer;
  369. Dialog = NULL;
  370. }
  371. //---------------------------------------------------------------------------
  372. void __fastcall TMessageTimer::DoTimer(TObject * /*Sender*/)
  373. {
  374. if (Event != NULL)
  375. {
  376. Event(Result);
  377. if (Result != 0)
  378. {
  379. Dialog->ModalResult = mrCancel;
  380. }
  381. }
  382. }
  383. //---------------------------------------------------------------------------
  384. class TMessageTimeout : public TTimer
  385. {
  386. public:
  387. __fastcall TMessageTimeout(TComponent * AOwner, unsigned int Timeout,
  388. TButton * Button);
  389. protected:
  390. unsigned int FTimeout;
  391. TButton * FButton;
  392. AnsiString FOrigCaption;
  393. void __fastcall DoTimer(TObject * Sender);
  394. void __fastcall UpdateButton();
  395. };
  396. //---------------------------------------------------------------------------
  397. __fastcall TMessageTimeout::TMessageTimeout(TComponent * AOwner,
  398. unsigned int Timeout, TButton * Button) :
  399. TTimer(AOwner), FTimeout(Timeout), FButton(Button)
  400. {
  401. OnTimer = DoTimer;
  402. Interval = 1000;
  403. FOrigCaption = FButton->Caption;
  404. UpdateButton();
  405. }
  406. //---------------------------------------------------------------------------
  407. void __fastcall TMessageTimeout::UpdateButton()
  408. {
  409. assert(FButton != NULL);
  410. FButton->Caption = FMTLOAD(TIMEOUT_BUTTON, (FOrigCaption, int(FTimeout / 1000)));
  411. }
  412. //---------------------------------------------------------------------------
  413. void __fastcall TMessageTimeout::DoTimer(TObject * /*Sender*/)
  414. {
  415. if (FTimeout <= 1000)
  416. {
  417. assert(FButton != NULL);
  418. TForm * Dialog = dynamic_cast<TForm *>(FButton->Parent);
  419. assert(Dialog != NULL);
  420. Dialog->ModalResult = FButton->ModalResult;
  421. }
  422. else
  423. {
  424. FTimeout -= 1000;
  425. UpdateButton();
  426. }
  427. }
  428. //---------------------------------------------------------------------------
  429. int __fastcall MoreMessageDialog(const AnsiString Message, TStrings * MoreMessages,
  430. TQueryType Type, int Answers, AnsiString HelpKeyword, const TMessageParams * Params)
  431. {
  432. int Result;
  433. TForm * Dialog = NULL;
  434. TMessageTimer * Timer = NULL;
  435. TMessageTimeout * Timeout = NULL;
  436. try
  437. {
  438. AnsiString AMessage = Message;
  439. if ((Params != NULL) && (Params->Timer > 0))
  440. {
  441. Timer = new TMessageTimer(Application);
  442. Timer->Interval = Params->Timer;
  443. Timer->Event = Params->TimerEvent;
  444. if (Params->TimerAnswers > 0)
  445. {
  446. Answers = Params->TimerAnswers;
  447. }
  448. if (!Params->TimerMessage.IsEmpty())
  449. {
  450. AMessage = Params->TimerMessage;
  451. }
  452. }
  453. TButton * TimeoutButton = NULL;
  454. Dialog = CreateMessageDialogEx(AMessage, MoreMessages, Type, Answers,
  455. HelpKeyword, Params, TimeoutButton);
  456. if (Timer != NULL)
  457. {
  458. Timer->Dialog = Dialog;
  459. }
  460. if (Params != NULL)
  461. {
  462. if (Params->Timeout > 0)
  463. {
  464. Timeout = new TMessageTimeout(Application, Params->Timeout, TimeoutButton);
  465. }
  466. }
  467. Result = ExecuteMessageDialog(Dialog, Answers, Params);
  468. if ((Timer != NULL) && (Timer->Result != 0))
  469. {
  470. Result = Timer->Result;
  471. }
  472. }
  473. __finally
  474. {
  475. delete Dialog;
  476. delete Timer;
  477. delete Timeout;
  478. }
  479. return Result;
  480. }
  481. //---------------------------------------------------------------------------
  482. int __fastcall MessageDialog(const AnsiString Msg, TQueryType Type,
  483. int Answers, AnsiString HelpKeyword, const TMessageParams * Params)
  484. {
  485. return MoreMessageDialog(Msg, NULL, Type, Answers, HelpKeyword, Params);
  486. }
  487. //---------------------------------------------------------------------------
  488. int __fastcall SimpleErrorDialog(const AnsiString Msg, const AnsiString MoreMessages)
  489. {
  490. int Result;
  491. TStrings * More = NULL;
  492. try
  493. {
  494. if (!MoreMessages.IsEmpty())
  495. {
  496. More = new TStringList();
  497. More->Text = 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. int __fastcall ExceptionMessageDialog(Exception * E, TQueryType Type,
  509. const AnsiString MessageFormat, int Answers, AnsiString HelpKeyword,
  510. const TMessageParams * Params)
  511. {
  512. TStrings * MoreMessages = NULL;
  513. ExtException * EE = dynamic_cast<ExtException *>(E);
  514. if (EE != NULL)
  515. {
  516. MoreMessages = EE->MoreMessages;
  517. }
  518. AnsiString Message = TranslateExceptionMessage(E);
  519. return MoreMessageDialog(
  520. FORMAT(MessageFormat.IsEmpty() ? AnsiString("%s") : MessageFormat, (Message)),
  521. MoreMessages, Type, Answers, HelpKeyword, Params);
  522. }
  523. //---------------------------------------------------------------------------
  524. int __fastcall FatalExceptionMessageDialog(Exception * E, TQueryType Type,
  525. const AnsiString MessageFormat, int Answers, AnsiString HelpKeyword,
  526. const TMessageParams * Params)
  527. {
  528. assert(FLAGCLEAR(Answers, qaRetry));
  529. Answers |= qaRetry;
  530. TQueryButtonAlias Aliases[1];
  531. Aliases[0].Button = qaRetry;
  532. Aliases[0].Alias = LoadStr(RECONNECT_BUTTON);
  533. TMessageParams AParams;
  534. if (Params != NULL)
  535. {
  536. assert(Params->Aliases == NULL);
  537. AParams = *Params;
  538. }
  539. AParams.Aliases = Aliases;
  540. AParams.AliasesCount = LENOF(Aliases);
  541. return ExceptionMessageDialog(E, Type, MessageFormat, Answers, HelpKeyword, &AParams);
  542. }
  543. //---------------------------------------------------------------------------
  544. void __fastcall Busy(bool Start)
  545. {
  546. static int Busy = 0;
  547. static TCursor PrevCursor;
  548. if (Start)
  549. {
  550. if (!Busy)
  551. {
  552. PrevCursor = Screen->Cursor;
  553. Screen->Cursor = crHourGlass;
  554. }
  555. Busy++;
  556. assert(Busy < 10);
  557. }
  558. else
  559. {
  560. assert(Busy > 0);
  561. Busy--;
  562. if (!Busy)
  563. {
  564. Screen->Cursor = PrevCursor;
  565. }
  566. }
  567. }
  568. //---------------------------------------------------------------------------
  569. static void __fastcall CopyParamListSaveSettingsClick(TMenuItem * Item)
  570. {
  571. bool * SaveSettings = reinterpret_cast<bool *>(Item->Tag);
  572. *SaveSettings = !*SaveSettings;
  573. Item->Checked = *SaveSettings;
  574. }
  575. //---------------------------------------------------------------------------
  576. bool __fastcall DoRemoteMoveDialog(AnsiString & Target, AnsiString & FileMask)
  577. {
  578. AnsiString Value = UnixIncludeTrailingBackslash(Target) + FileMask;
  579. TStrings * History = CustomWinConfiguration->History["RemoteTarget"];
  580. bool Result = InputDialog(
  581. LoadStr(REMOTE_MOVE_TITLE), LoadStr(REMOTE_MOVE_PROMPT),
  582. Value, HELP_REMOTE_MOVE, History, true);
  583. if (Result)
  584. {
  585. CustomWinConfiguration->History["RemoteTarget"] = History;
  586. Target = UnixExtractFilePath(Value);
  587. FileMask = UnixExtractFileName(Value);
  588. }
  589. return Result;
  590. }
  591. //---------------------------------------------------------------------------
  592. void __fastcall CopyParamListPopup(TPoint P, TPopupMenu * Menu,
  593. const TCopyParamType & Param, AnsiString Preset, TNotifyEvent OnClick,
  594. int Options, bool * SaveSettings)
  595. {
  596. Menu->Items->Clear();
  597. bool AnyChecked = false;
  598. TMenuItem * Item = new TMenuItem(Menu);
  599. Item->Caption = LoadStr(COPY_PARAM_DEFAULT);
  600. Item->Tag = -1;
  601. Item->Checked =
  602. Preset.IsEmpty() && (GUIConfiguration->CopyParamPreset[""] == Param);
  603. AnyChecked = AnyChecked || Item->Checked;
  604. Item->OnClick = OnClick;
  605. Menu->Items->Add(Item);
  606. const TCopyParamList * CopyParamList = GUIConfiguration->CopyParamList;
  607. for (int i = 0; i < CopyParamList->Count; i++)
  608. {
  609. Item = new TMenuItem(Menu);
  610. AnsiString Name = CopyParamList->Names[i];
  611. Item->Caption = Name;
  612. Item->Tag = i;
  613. Item->Checked =
  614. (Preset == Name) && (GUIConfiguration->CopyParamPreset[Name] == Param);
  615. AnyChecked = AnyChecked || Item->Checked;
  616. Item->OnClick = OnClick;
  617. Menu->Items->Add(Item);
  618. }
  619. if (FLAGSET(Options, cplCustomize))
  620. {
  621. Item = new TMenuItem(Menu);
  622. Item->Caption = LoadStr(COPY_PARAM_CUSTOM);
  623. Item->Tag = -3;
  624. Item->Checked = !AnyChecked;
  625. Item->Default = FLAGSET(Options, cplCustomizeDefault);
  626. Item->OnClick = OnClick;
  627. Menu->Items->Add(Item);
  628. }
  629. if (FLAGSET(Options, cplSaveSettings))
  630. {
  631. assert(SaveSettings != NULL);
  632. Item = new TMenuItem(Menu);
  633. Item->Caption = LoadStr(COPY_PARAM_SAVE_SETTINGS);
  634. Item->Tag = int(SaveSettings);
  635. Item->Checked = *SaveSettings;
  636. TNotifyEvent SaveSettingsOnClick;
  637. ((TMethod*)&SaveSettingsOnClick)->Data = Item;
  638. ((TMethod*)&SaveSettingsOnClick)->Code = CopyParamListSaveSettingsClick;
  639. Item->OnClick = SaveSettingsOnClick;
  640. Menu->Items->Add(Item);
  641. }
  642. Item = new TMenuItem(Menu);
  643. Item->Caption = "-";
  644. Menu->Items->Add(Item);
  645. Item = new TMenuItem(Menu);
  646. Item->Caption = LoadStr(COPY_PARAM_CONFIGURE);
  647. Item->Tag = -2;
  648. Item->OnClick = OnClick;
  649. Menu->Items->Add(Item);
  650. MenuPopup(Menu, P, NULL);
  651. }
  652. //---------------------------------------------------------------------------
  653. bool __fastcall CopyParamListPopupClick(TObject * Sender,
  654. TCopyParamType & Param, AnsiString & Preset, int CopyParamAttrs)
  655. {
  656. TComponent * Item = dynamic_cast<TComponent *>(Sender);
  657. assert(Item != NULL);
  658. assert((Item->Tag >= -3) && (Item->Tag < GUIConfiguration->CopyParamList->Count));
  659. bool Result;
  660. if (Item->Tag == -2)
  661. {
  662. DoPreferencesDialog(pmPresets);
  663. Result = false;
  664. }
  665. else if (Item->Tag == -3)
  666. {
  667. Result = DoCopyParamCustomDialog(Param, CopyParamAttrs);
  668. }
  669. else
  670. {
  671. Preset = (Item->Tag >= 0) ?
  672. GUIConfiguration->CopyParamList->Names[Item->Tag] : AnsiString();
  673. Param = GUIConfiguration->CopyParamPreset[Preset];
  674. Result = true;
  675. }
  676. return Result;
  677. }
  678. //---------------------------------------------------------------------------
  679. TWinInteractiveCustomCommand::TWinInteractiveCustomCommand(
  680. TCustomCommand * ChildCustomCommand, const AnsiString CustomCommandName) :
  681. TInteractiveCustomCommand(ChildCustomCommand)
  682. {
  683. FCustomCommandName = StripHotkey(CustomCommandName);
  684. }
  685. //---------------------------------------------------------------------------
  686. void __fastcall TWinInteractiveCustomCommand::Prompt(int /*Index*/,
  687. const AnsiString & Prompt, AnsiString & Value)
  688. {
  689. AnsiString APrompt = Prompt;
  690. if (APrompt.IsEmpty())
  691. {
  692. APrompt = FMTLOAD(CUSTOM_COMMANDS_PARAM_PROMPT, (FCustomCommandName));
  693. }
  694. TStrings * History = CustomWinConfiguration->History["CustomCommandParam"];
  695. if (InputDialog(FMTLOAD(CUSTOM_COMMANDS_PARAM_TITLE, (FCustomCommandName)),
  696. APrompt, Value, HELP_CUSTOM_COMMAND_PARAM, History))
  697. {
  698. CustomWinConfiguration->History["CustomCommandParam"] = History;
  699. }
  700. else
  701. {
  702. Abort();
  703. }
  704. }
  705. //---------------------------------------------------------------------------
  706. static unsigned int __fastcall ShellDllVersion()
  707. {
  708. static unsigned int Result = 0;
  709. if (Result == 0)
  710. {
  711. Result = 4;
  712. HINSTANCE ShellDll = LoadLibrary("shell32.dll");
  713. if (ShellDll != NULL)
  714. {
  715. try
  716. {
  717. DLLGETVERSIONPROC DllGetVersion =
  718. (DLLGETVERSIONPROC)GetProcAddress(ShellDll, "DllGetVersion");
  719. if(DllGetVersion != NULL)
  720. {
  721. DLLVERSIONINFO VersionInfo;
  722. ZeroMemory(&VersionInfo, sizeof(VersionInfo));
  723. VersionInfo.cbSize = sizeof(VersionInfo);
  724. if (SUCCEEDED(DllGetVersion(&VersionInfo)))
  725. {
  726. Result = VersionInfo.dwMajorVersion;
  727. }
  728. }
  729. }
  730. __finally
  731. {
  732. FreeLibrary(ShellDll);
  733. }
  734. }
  735. }
  736. return Result;
  737. }
  738. //---------------------------------------------------------------------------
  739. void __fastcall MenuPopup(TPopupMenu * Menu, TButtonControl * Button)
  740. {
  741. TPoint Point = Button->ClientToScreen(TPoint(2, Button->Height));
  742. MenuPopup(Menu, Point, Button);
  743. }
  744. //---------------------------------------------------------------------------
  745. void __fastcall MenuPopup(TObject * Sender, const TPoint & MousePos, bool & Handled)
  746. {
  747. class TPublicControl : public TControl
  748. {
  749. friend void __fastcall MenuPopup(TObject * Sender, const TPoint & MousePos, bool & Handled);
  750. };
  751. TControl * Control = dynamic_cast<TControl *>(Sender);
  752. TPoint Point;
  753. if ((MousePos.x == -1) && (MousePos.y == -1))
  754. {
  755. Point = Control->ClientToScreen(TPoint(0, 0));
  756. }
  757. else
  758. {
  759. Point = Control->ClientToScreen(MousePos);
  760. }
  761. assert(Control != NULL);
  762. TPopupMenu * PopupMenu = (reinterpret_cast<TPublicControl *>(Control))->PopupMenu;
  763. assert(PopupMenu != NULL);
  764. MenuPopup(PopupMenu, Point, Control);
  765. Handled = true;
  766. }
  767. //---------------------------------------------------------------------------
  768. void __fastcall SetGlobalMinimizeHandler(TNotifyEvent OnMinimize)
  769. {
  770. GlobalOnMinimize = OnMinimize;
  771. }
  772. //---------------------------------------------------------------------------
  773. TNotifyEvent __fastcall GetGlobalMinimizeHandler()
  774. {
  775. return GlobalOnMinimize;
  776. }
  777. //---------------------------------------------------------------------------
  778. bool __fastcall IsGlobalMinimizeHandler()
  779. {
  780. return (GlobalOnMinimize != NULL);
  781. }
  782. //---------------------------------------------------------------------------
  783. unsigned long __fastcall GetSpeedLimit(const AnsiString & Text)
  784. {
  785. unsigned long Speed;
  786. if (AnsiSameText(Text, LoadStr(SPEED_UNLIMITED)))
  787. {
  788. Speed = 0;
  789. }
  790. else
  791. {
  792. int SSpeed;
  793. if (!TryStrToInt(Text, SSpeed) ||
  794. (SSpeed < 0))
  795. {
  796. throw Exception(FMTLOAD(SPEED_INVALID, (Text)));
  797. }
  798. Speed = SSpeed;
  799. }
  800. return Speed * 1024;
  801. }
  802. //---------------------------------------------------------------------------
  803. AnsiString __fastcall SetSpeedLimit(unsigned long Limit)
  804. {
  805. AnsiString Text;
  806. if (Limit == 0)
  807. {
  808. Text = LoadStr(SPEED_UNLIMITED);
  809. }
  810. else
  811. {
  812. Text = IntToStr(Limit / 1024);
  813. }
  814. return Text;
  815. }
  816. //---------------------------------------------------------------------------
  817. struct TNotifyIconData5
  818. {
  819. DWORD cbSize;
  820. HWND hWnd;
  821. UINT uID;
  822. UINT uFlags;
  823. UINT uCallbackMessage;
  824. HICON hIcon;
  825. CHAR szTip[128];
  826. DWORD dwState;
  827. DWORD dwStateMask;
  828. CHAR szInfo[256];
  829. union {
  830. UINT uTimeout;
  831. UINT uVersion;
  832. } DUMMYUNIONNAME;
  833. CHAR szInfoTitle[64];
  834. DWORD dwInfoFlags;
  835. };
  836. //---------------------------------------------------------------------------
  837. #undef NOTIFYICONDATA_V1_SIZE
  838. #define NOTIFYICONDATA_V1_SIZE FIELD_OFFSET(TNotifyIconData5, szTip[64])
  839. //---------------------------------------------------------------------------
  840. __fastcall TTrayIcon::TTrayIcon(unsigned int Id)
  841. {
  842. FVisible = false;
  843. FOnClick = NULL;
  844. FTrayIcon = new TNotifyIconData5;
  845. memset(FTrayIcon, 0, sizeof(*FTrayIcon));
  846. FTrayIcon->cbSize = ((ShellDllVersion() >= 5) ? sizeof(*FTrayIcon) : NOTIFYICONDATA_V1_SIZE);
  847. FTrayIcon->uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  848. FTrayIcon->hIcon = Application->Icon->Handle;
  849. FTrayIcon->uID = Id;
  850. FTrayIcon->hWnd = AllocateHWnd(WndProc);
  851. FTrayIcon->uCallbackMessage = WM_TRAY_ICON;
  852. }
  853. //---------------------------------------------------------------------------
  854. __fastcall TTrayIcon::~TTrayIcon()
  855. {
  856. Visible = false;
  857. DeallocateHWnd(FTrayIcon->hWnd);
  858. delete FTrayIcon;
  859. }
  860. //---------------------------------------------------------------------------
  861. bool __fastcall TTrayIcon::SupportsBalloons()
  862. {
  863. return (ShellDllVersion() >= 5);
  864. }
  865. //---------------------------------------------------------------------------
  866. void __fastcall TTrayIcon::PopupBalloon(const AnsiString & Title,
  867. const AnsiString & Str, TQueryType QueryType, unsigned int Timeout)
  868. {
  869. if (SupportsBalloons())
  870. {
  871. if (Timeout > 30000)
  872. {
  873. // this is probably system limit, do not try more, espcially for
  874. // the timeout-driven hiding of the tray icon (for Win2k)
  875. Timeout = 30000;
  876. }
  877. FTrayIcon->uFlags |= NIF_INFO;
  878. StrPLCopy(FTrayIcon->szInfoTitle, Title, sizeof(FTrayIcon->szInfoTitle) - 1);
  879. StrPLCopy(FTrayIcon->szInfo, Str, sizeof(FTrayIcon->szInfo) - 1);
  880. FTrayIcon->uTimeout = Timeout;
  881. switch (QueryType)
  882. {
  883. case qtError:
  884. FTrayIcon->dwInfoFlags = NIIF_ERROR;
  885. break;
  886. case qtInformation:
  887. case qtConfirmation:
  888. FTrayIcon->dwInfoFlags = NIIF_INFO;
  889. break;
  890. case qtWarning:
  891. default:
  892. FTrayIcon->dwInfoFlags = NIIF_WARNING;
  893. break;
  894. }
  895. KillTimer(FTrayIcon->hWnd, 1);
  896. if (Visible)
  897. {
  898. Update();
  899. }
  900. else
  901. {
  902. Notify(NIM_ADD);
  903. // can be 5 only anyway, no older version supports balloons
  904. if (ShellDllVersion() <= 5)
  905. {
  906. // version 5 does not notify us when the balloon is hidden, so we must
  907. // remove the icon ourselves after own timeout
  908. SetTimer(FTrayIcon->hWnd, 1, Timeout, NULL);
  909. }
  910. }
  911. // Clearing the flag ensures that subsequent updates does not hide the baloon
  912. // unless CancelBalloon is called explicitly
  913. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  914. }
  915. }
  916. //---------------------------------------------------------------------------
  917. void __fastcall TTrayIcon::CancelBalloon()
  918. {
  919. if (SupportsBalloons())
  920. {
  921. KillTimer(FTrayIcon->hWnd, 1);
  922. if (Visible)
  923. {
  924. FTrayIcon->uFlags |= NIF_INFO;
  925. FTrayIcon->szInfo[0] = '\0';
  926. Update();
  927. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  928. }
  929. else
  930. {
  931. Notify(NIM_DELETE);
  932. }
  933. }
  934. }
  935. //---------------------------------------------------------------------------
  936. bool __fastcall TTrayIcon::Notify(unsigned int Message)
  937. {
  938. bool Result = SUCCEEDED(Shell_NotifyIcon(Message, (NOTIFYICONDATA*)FTrayIcon));
  939. if (Result && (Message == NIM_ADD))
  940. {
  941. UINT Timeout = FTrayIcon->uTimeout;
  942. try
  943. {
  944. FTrayIcon->uVersion = NOTIFYICON_VERSION;
  945. Result = SUCCEEDED(Shell_NotifyIcon(NIM_SETVERSION, (NOTIFYICONDATA*)FTrayIcon));
  946. }
  947. __finally
  948. {
  949. FTrayIcon->uTimeout = Timeout;
  950. }
  951. }
  952. return Result;
  953. }
  954. //---------------------------------------------------------------------------
  955. void __fastcall TTrayIcon::Update()
  956. {
  957. if (Visible)
  958. {
  959. Notify(NIM_MODIFY);
  960. }
  961. }
  962. //---------------------------------------------------------------------------
  963. void __fastcall TTrayIcon::SetVisible(bool value)
  964. {
  965. if (Visible != value)
  966. {
  967. if (value)
  968. {
  969. FVisible = Notify(NIM_ADD);
  970. }
  971. else
  972. {
  973. FVisible = false;
  974. KillTimer(FTrayIcon->hWnd, 1);
  975. Notify(NIM_DELETE);
  976. }
  977. }
  978. }
  979. //---------------------------------------------------------------------------
  980. void __fastcall TTrayIcon::WndProc(TMessage & Message)
  981. {
  982. try
  983. {
  984. if (Message.Msg == WM_TRAY_ICON)
  985. {
  986. assert(Message.WParam == 0);
  987. switch (Message.LParam)
  988. {
  989. // old shell32
  990. case WM_LBUTTONUP:
  991. case WM_RBUTTONUP:
  992. // new shell32:
  993. case WM_CONTEXTMENU:
  994. case NIN_BALLOONUSERCLICK:
  995. if (OnClick != NULL)
  996. {
  997. OnClick(NULL);
  998. }
  999. Message.Result = true;
  1000. break;
  1001. }
  1002. switch (Message.LParam)
  1003. {
  1004. case NIN_BALLOONHIDE:
  1005. case NIN_BALLOONTIMEOUT:
  1006. case NIN_BALLOONUSERCLICK:
  1007. KillTimer(FTrayIcon->hWnd, 1);
  1008. // if icon was shown just to display balloon, hide it with the balloon
  1009. if (!Visible)
  1010. {
  1011. Notify(NIM_DELETE);
  1012. }
  1013. break;
  1014. }
  1015. }
  1016. else if (Message.Msg == WM_TIMER)
  1017. {
  1018. // sanity check
  1019. Notify(NIM_DELETE);
  1020. }
  1021. else
  1022. {
  1023. Message.Result = DefWindowProc(FTrayIcon->hWnd, Message.Msg, Message.WParam, Message.LParam);
  1024. }
  1025. }
  1026. catch(Exception & E)
  1027. {
  1028. Application->HandleException(&E);
  1029. }
  1030. }
  1031. //---------------------------------------------------------------------------
  1032. AnsiString __fastcall TTrayIcon::GetHint()
  1033. {
  1034. return FTrayIcon->szTip;
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. void __fastcall TTrayIcon::SetHint(AnsiString value)
  1038. {
  1039. if (Hint != value)
  1040. {
  1041. unsigned int Max = ((ShellDllVersion() >= 5) ? sizeof(FTrayIcon->szTip) : 64);
  1042. StrPLCopy(FTrayIcon->szTip, value, Max - 1);
  1043. Update();
  1044. }
  1045. }
  1046. //---------------------------------------------------------------------------