WinInterface.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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 <Interface.h>
  12. #include <VCLCommon.h>
  13. #include "WinInterface.h"
  14. #include "CustomWinConfiguration.h"
  15. #include "GUITools.h"
  16. //---------------------------------------------------------------------------
  17. #pragma package(smart_init)
  18. //---------------------------------------------------------------------------
  19. #define WM_TRAY_ICON (WM_WINSCP_USER + 5)
  20. //---------------------------------------------------------------------
  21. TNotifyEvent GlobalOnMinimize = NULL;
  22. //---------------------------------------------------------------------
  23. void __fastcall FormHelp(TForm * Form)
  24. {
  25. InvokeHelp(Form->ActiveControl != NULL ? Form->ActiveControl : Form);
  26. }
  27. //---------------------------------------------------------------------------
  28. TMessageParams::TMessageParams(unsigned int AParams)
  29. {
  30. Reset();
  31. Params = AParams;
  32. }
  33. //---------------------------------------------------------------------------
  34. TMessageParams::TMessageParams(const TQueryParams * AParams)
  35. {
  36. Reset();
  37. if (AParams != NULL)
  38. {
  39. Aliases = AParams->Aliases;
  40. AliasesCount = AParams->AliasesCount;
  41. Timer = AParams->Timer;
  42. TimerEvent = AParams->TimerEvent;
  43. TimerMessage = AParams->TimerMessage;
  44. TimerAnswers = AParams->TimerAnswers;
  45. Timeout = AParams->Timeout;
  46. TimeoutAnswer = AParams->TimeoutAnswer;
  47. if (FLAGSET(AParams->Params, qpNeverAskAgainCheck))
  48. {
  49. Params |= mpNeverAskAgainCheck;
  50. }
  51. if (FLAGSET(AParams->Params, qpAllowContinueOnError))
  52. {
  53. Params |= mpAllowContinueOnError;
  54. }
  55. }
  56. }
  57. //---------------------------------------------------------------------------
  58. inline void TMessageParams::Reset()
  59. {
  60. Params = 0;
  61. Aliases = NULL;
  62. AliasesCount = 0;
  63. Timer = 0;
  64. TimerEvent = NULL;
  65. TimerMessage = "";
  66. TimerAnswers = 0;
  67. Timeout = 0;
  68. TimeoutAnswer = 0;
  69. NewerAskAgainTitle = "";
  70. NewerAskAgainAnswer = 0;
  71. NewerAskAgainCheckedInitially = false;
  72. AllowHelp = true;
  73. }
  74. //---------------------------------------------------------------------------
  75. inline bool MapButton(unsigned int Answer, TMsgDlgBtn & Button)
  76. {
  77. bool Result = true;
  78. #define MAP_BUTTON(TYPE) if (Answer == qa ## TYPE) Button = mb ## TYPE; else
  79. MAP_BUTTON(Yes)
  80. MAP_BUTTON(No)
  81. MAP_BUTTON(OK)
  82. MAP_BUTTON(Cancel)
  83. MAP_BUTTON(Abort)
  84. MAP_BUTTON(Retry)
  85. MAP_BUTTON(Ignore)
  86. MAP_BUTTON(All)
  87. MAP_BUTTON(NoToAll)
  88. MAP_BUTTON(YesToAll)
  89. MAP_BUTTON(Help)
  90. #undef MAP_BUTTON
  91. if (Answer == qaSkip) Button = mbIgnore;
  92. else
  93. Result = false;
  94. return Result;
  95. }
  96. //---------------------------------------------------------------------------
  97. static void __fastcall NeverAskAgainCheckClick(void * /*Data*/, TObject * Sender)
  98. {
  99. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(Sender);
  100. assert(CheckBox != NULL);
  101. TForm * Dialog = dynamic_cast<TForm *>(CheckBox->Owner);
  102. assert(Dialog != NULL);
  103. TModalResult PositiveAnswer = mrNone;
  104. if (CheckBox->Checked)
  105. {
  106. if (CheckBox->Tag > 0)
  107. {
  108. PositiveAnswer = CheckBox->Tag - 1;
  109. }
  110. else
  111. {
  112. TModalResult PositiveAnswers[] = { mrYes, mrOk, mrYesToAll };
  113. for (int i = 0; i < LENOF(PositiveAnswers); i++)
  114. {
  115. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  116. {
  117. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  118. if (Button != NULL)
  119. {
  120. if (Button->ModalResult == PositiveAnswers[i])
  121. {
  122. PositiveAnswer = PositiveAnswers[i];
  123. break;
  124. }
  125. }
  126. }
  127. if (PositiveAnswer != mrNone)
  128. {
  129. break;
  130. }
  131. }
  132. }
  133. assert(PositiveAnswer != mrNone);
  134. }
  135. for (int ii = 0; ii < Dialog->ControlCount; ii++)
  136. {
  137. TButton * Button = dynamic_cast<TButton *>(Dialog->Controls[ii]);
  138. if ((Button != NULL) && (Button->ModalResult != mrNone) &&
  139. (Button->ModalResult != mrCancel))
  140. {
  141. Button->Enabled = !CheckBox->Checked || (Button->ModalResult == PositiveAnswer);
  142. }
  143. }
  144. }
  145. //---------------------------------------------------------------------------
  146. int MapResult(int Result, unsigned int Answers)
  147. {
  148. int Answer;
  149. switch (Result)
  150. {
  151. #define MAP_RESULT(RESULT) case mr ## RESULT: Answer = qa ## RESULT; break;
  152. MAP_RESULT(Abort);
  153. MAP_RESULT(All);
  154. MAP_RESULT(NoToAll);
  155. MAP_RESULT(YesToAll);
  156. MAP_RESULT(Yes);
  157. MAP_RESULT(No);
  158. MAP_RESULT(Retry);
  159. #undef MAP_RESULT
  160. case mrCancel:
  161. // mrCancel is returned always when X button is pressed, despite
  162. // no Cancel button was on the dialog. Find valid "cancel" answer.
  163. Answer = CancelAnswer(Answers);
  164. break;
  165. case mrOk:
  166. Answer = qaOK;
  167. break;
  168. case mrIgnore:
  169. Answer = (Answers & qaSkip) ? qaSkip : qaIgnore;
  170. break;
  171. default:
  172. assert(false);
  173. Answer = CancelAnswer(Answers);
  174. break;
  175. }
  176. return Answer;
  177. }
  178. //---------------------------------------------------------------------------
  179. TForm * __fastcall CreateMessageDialogEx(const AnsiString Msg,
  180. TStrings * MoreMessages, TQueryType Type, int Answers, AnsiString HelpKeyword,
  181. const TMessageParams * Params, TButton *& TimeoutButton)
  182. {
  183. TMsgDlgBtn TimeoutResult = mbHelp;
  184. TMsgDlgButtons Buttons;
  185. TMsgDlgType DlgType;
  186. switch (Type) {
  187. case qtConfirmation: DlgType = mtConfirmation; break;
  188. case qtInformation: DlgType = mtInformation; break;
  189. case qtError: DlgType = mtError; break;
  190. case qtWarning: DlgType = mtWarning; break;
  191. default: assert(false);
  192. }
  193. int AAnswers = Answers;
  194. unsigned int Answer = 0x01;
  195. while (AAnswers > 0)
  196. {
  197. if ((AAnswers & Answer) != 0)
  198. {
  199. TMsgDlgBtn Button;
  200. if (MapButton(Answer, Button))
  201. {
  202. Buttons << Button;
  203. if ((Params != NULL) && (Params->Timeout > 0) &&
  204. (Params->TimeoutAnswer == Answer))
  205. {
  206. TimeoutResult = Button;
  207. }
  208. }
  209. AAnswers &= ~Answer;
  210. }
  211. Answer <<= 1;
  212. }
  213. assert(!Buttons.Empty());
  214. if ((Params == NULL) || Params->AllowHelp)
  215. {
  216. Buttons << mbHelp;
  217. }
  218. if ((MoreMessages != NULL) && (MoreMessages->Count == 0))
  219. {
  220. MoreMessages = NULL;
  221. }
  222. TForm * Dialog;
  223. if ((Params == NULL) || (Params->Aliases == NULL))
  224. {
  225. Dialog = CreateMoreMessageDialog(Msg, MoreMessages, DlgType, Buttons,
  226. NULL, 0, TimeoutResult, &TimeoutButton);
  227. }
  228. else
  229. {
  230. TQueryButtonAlias * Aliases = new TQueryButtonAlias[Params->AliasesCount];
  231. try
  232. {
  233. for (unsigned int i = 0; i < Params->AliasesCount; i++)
  234. {
  235. TMsgDlgBtn Button;
  236. CHECK(MapButton(Params->Aliases[i].Button, Button));
  237. Aliases[i].Button = Button;
  238. Aliases[i].Alias = Params->Aliases[i].Alias;
  239. Aliases[i].OnClick = Params->Aliases[i].OnClick;
  240. }
  241. Dialog = CreateMoreMessageDialog(Msg, MoreMessages, DlgType, Buttons,
  242. Aliases, Params->AliasesCount, TimeoutResult, &TimeoutButton);
  243. }
  244. __finally
  245. {
  246. delete[] Aliases;
  247. }
  248. }
  249. try
  250. {
  251. if (Answers & qaSkip)
  252. {
  253. TButton * IgnoreButton = dynamic_cast<TButton *>(Dialog->FindComponent("Ignore"));
  254. assert(IgnoreButton);
  255. IgnoreButton->Caption = LoadStr(SKIP_BUTTON);
  256. }
  257. if ((Params != NULL) && (Params->Params & mpNeverAskAgainCheck))
  258. {
  259. static const int VertSpace = 20;
  260. Dialog->ClientHeight = Dialog->ClientHeight + VertSpace;
  261. for (int Index = 0; Index < Dialog->ControlCount; Index++)
  262. {
  263. TControl * Control = Dialog->Controls[Index];
  264. if (Control->Anchors.Contains(akBottom))
  265. {
  266. if (Control->Anchors.Contains(akTop))
  267. {
  268. Control->Height = Control->Height - VertSpace;
  269. }
  270. else
  271. {
  272. Control->Top = Control->Top - VertSpace;
  273. }
  274. }
  275. }
  276. TCheckBox * NeverAskAgainCheck = new TCheckBox(Dialog);
  277. NeverAskAgainCheck->Name = "NeverAskAgainCheck";
  278. NeverAskAgainCheck->Parent = Dialog;
  279. NeverAskAgainCheck->BoundsRect = TRect(60, Dialog->ClientHeight - 27,
  280. Dialog->ClientWidth - 10, Dialog->ClientHeight - 5);
  281. NeverAskAgainCheck->Caption =
  282. !Params->NewerAskAgainTitle.IsEmpty() ?
  283. Params->NewerAskAgainTitle :
  284. // qaOK | qaIgnore is used, when custom "non-answer" button is required
  285. LoadStr(((Answers == qaOK) || (Answers == (qaOK | qaIgnore))) ?
  286. NEVER_SHOW_AGAIN : NEVER_ASK_AGAIN);
  287. NeverAskAgainCheck->Checked = Params->NewerAskAgainCheckedInitially;
  288. NeverAskAgainCheck->Anchors = TAnchors() << akBottom << akLeft;
  289. if (Params->NewerAskAgainAnswer > 0)
  290. {
  291. TMsgDlgBtn Button;
  292. if (MapButton(Params->NewerAskAgainAnswer, Button))
  293. {
  294. extern const int ModalResults[];
  295. NeverAskAgainCheck->Tag = ModalResults[Button] + 1;
  296. }
  297. }
  298. TNotifyEvent OnClick;
  299. ((TMethod*)&OnClick)->Code = NeverAskAgainCheckClick;
  300. NeverAskAgainCheck->OnClick = OnClick;
  301. }
  302. Dialog->HelpKeyword = HelpKeyword;
  303. ResetSystemSettings(Dialog);
  304. }
  305. catch(...)
  306. {
  307. delete Dialog;
  308. throw;
  309. }
  310. return Dialog;
  311. }
  312. //---------------------------------------------------------------------------
  313. int __fastcall ExecuteMessageDialog(TForm * Dialog, int Answers, const TMessageParams * Params)
  314. {
  315. FlashOnBackground();
  316. int Answer = MapResult(Dialog->ShowModal(), Answers);
  317. if ((Params != NULL) && (Params->Params & mpNeverAskAgainCheck))
  318. {
  319. TCheckBox * NeverAskAgainCheck =
  320. dynamic_cast<TCheckBox *>(Dialog->FindComponent("NeverAskAgainCheck"));
  321. assert(NeverAskAgainCheck);
  322. if (NeverAskAgainCheck->Checked)
  323. {
  324. bool PossitiveAnswer =
  325. (Params->NewerAskAgainAnswer > 0) ?
  326. (Answer == Params->NewerAskAgainAnswer) :
  327. (Answer == qaYes || Answer == qaOK || Answer == qaYesToAll);
  328. if (PossitiveAnswer)
  329. {
  330. Answer = qaNeverAskAgain;
  331. }
  332. }
  333. }
  334. return Answer;
  335. }
  336. //---------------------------------------------------------------------------
  337. class TMessageTimer : public TTimer
  338. {
  339. public:
  340. TQueryParamsTimerEvent Event;
  341. unsigned int Result;
  342. TForm * Dialog;
  343. __fastcall TMessageTimer(TComponent * AOwner);
  344. protected:
  345. void __fastcall DoTimer(TObject * Sender);
  346. };
  347. //---------------------------------------------------------------------------
  348. __fastcall TMessageTimer::TMessageTimer(TComponent * AOwner) : TTimer(AOwner)
  349. {
  350. Result = 0;
  351. Event = NULL;
  352. OnTimer = DoTimer;
  353. Dialog = NULL;
  354. }
  355. //---------------------------------------------------------------------------
  356. void __fastcall TMessageTimer::DoTimer(TObject * /*Sender*/)
  357. {
  358. if (Event != NULL)
  359. {
  360. Event(Result);
  361. if (Result != 0)
  362. {
  363. Dialog->ModalResult = mrCancel;
  364. }
  365. }
  366. }
  367. //---------------------------------------------------------------------------
  368. class TMessageTimeout : public TTimer
  369. {
  370. public:
  371. __fastcall TMessageTimeout(TComponent * AOwner, unsigned int Timeout,
  372. TButton * Button);
  373. protected:
  374. unsigned int FTimeout;
  375. TButton * FButton;
  376. AnsiString FOrigCaption;
  377. void __fastcall DoTimer(TObject * Sender);
  378. void __fastcall UpdateButton();
  379. };
  380. //---------------------------------------------------------------------------
  381. __fastcall TMessageTimeout::TMessageTimeout(TComponent * AOwner,
  382. unsigned int Timeout, TButton * Button) :
  383. TTimer(AOwner), FTimeout(Timeout), FButton(Button)
  384. {
  385. OnTimer = DoTimer;
  386. Interval = 1000;
  387. FOrigCaption = FButton->Caption;
  388. UpdateButton();
  389. }
  390. //---------------------------------------------------------------------------
  391. void __fastcall TMessageTimeout::UpdateButton()
  392. {
  393. assert(FButton != NULL);
  394. FButton->Caption = FMTLOAD(TIMEOUT_BUTTON, (FOrigCaption, int(FTimeout / 1000)));
  395. }
  396. //---------------------------------------------------------------------------
  397. void __fastcall TMessageTimeout::DoTimer(TObject * /*Sender*/)
  398. {
  399. if (FTimeout <= 1000)
  400. {
  401. assert(FButton != NULL);
  402. TForm * Dialog = dynamic_cast<TForm *>(FButton->Parent);
  403. assert(Dialog != NULL);
  404. Dialog->ModalResult = FButton->ModalResult;
  405. }
  406. else
  407. {
  408. FTimeout -= 1000;
  409. UpdateButton();
  410. }
  411. }
  412. //---------------------------------------------------------------------------
  413. int __fastcall MoreMessageDialog(const AnsiString Message, TStrings * MoreMessages,
  414. TQueryType Type, int Answers, AnsiString HelpKeyword, const TMessageParams * Params)
  415. {
  416. int Result;
  417. TForm * Dialog = NULL;
  418. TMessageTimer * Timer = NULL;
  419. TMessageTimeout * Timeout = NULL;
  420. try
  421. {
  422. AnsiString AMessage = Message;
  423. if ((Params != NULL) && (Params->Timer > 0))
  424. {
  425. Timer = new TMessageTimer(Application);
  426. Timer->Interval = Params->Timer;
  427. Timer->Event = Params->TimerEvent;
  428. if (Params->TimerAnswers > 0)
  429. {
  430. Answers = Params->TimerAnswers;
  431. }
  432. if (!Params->TimerMessage.IsEmpty())
  433. {
  434. AMessage = Params->TimerMessage;
  435. }
  436. }
  437. TButton * TimeoutButton = NULL;
  438. Dialog = CreateMessageDialogEx(AMessage, MoreMessages, Type, Answers,
  439. HelpKeyword, Params, TimeoutButton);
  440. if (Timer != NULL)
  441. {
  442. Timer->Dialog = Dialog;
  443. }
  444. if (Params != NULL)
  445. {
  446. if (Params->Timeout > 0)
  447. {
  448. Timeout = new TMessageTimeout(Application, Params->Timeout, TimeoutButton);
  449. }
  450. }
  451. Result = ExecuteMessageDialog(Dialog, Answers, Params);
  452. if ((Timer != NULL) && (Timer->Result != 0))
  453. {
  454. Result = Timer->Result;
  455. }
  456. }
  457. __finally
  458. {
  459. delete Dialog;
  460. delete Timer;
  461. delete Timeout;
  462. }
  463. return Result;
  464. }
  465. //---------------------------------------------------------------------------
  466. int __fastcall MessageDialog(const AnsiString Msg, TQueryType Type,
  467. int Answers, AnsiString HelpKeyword, const TMessageParams * Params)
  468. {
  469. return MoreMessageDialog(Msg, NULL, Type, Answers, HelpKeyword, Params);
  470. }
  471. //---------------------------------------------------------------------------
  472. int __fastcall SimpleErrorDialog(const AnsiString Msg, const AnsiString MoreMessages)
  473. {
  474. int Result;
  475. TStrings * More = NULL;
  476. try
  477. {
  478. if (!MoreMessages.IsEmpty())
  479. {
  480. More = new TStringList();
  481. More->Text = MoreMessages;
  482. }
  483. Result = MoreMessageDialog(Msg, More, qtError, qaOK, HELP_NONE);
  484. }
  485. __finally
  486. {
  487. delete More;
  488. }
  489. return Result;
  490. }
  491. //---------------------------------------------------------------------------
  492. int __fastcall ExceptionMessageDialog(Exception * E, TQueryType Type,
  493. const AnsiString MessageFormat, int Answers, AnsiString HelpKeyword,
  494. const TMessageParams * Params)
  495. {
  496. TStrings * MoreMessages = NULL;
  497. ExtException * EE = dynamic_cast<ExtException *>(E);
  498. if (EE != NULL)
  499. {
  500. MoreMessages = EE->MoreMessages;
  501. if (!EE->HelpKeyword.IsEmpty())
  502. {
  503. // we have to yet decide what to do now
  504. assert(HelpKeyword.IsEmpty());
  505. HelpKeyword = EE->HelpKeyword;
  506. }
  507. }
  508. AnsiString Message;
  509. // this is always called from within ExceptionMessage check,
  510. // so it should never fail here
  511. CHECK(ExceptionMessage(E, Message));
  512. return MoreMessageDialog(
  513. FORMAT(MessageFormat.IsEmpty() ? AnsiString("%s") : MessageFormat, (Message)),
  514. MoreMessages, Type, Answers, HelpKeyword, Params);
  515. }
  516. //---------------------------------------------------------------------------
  517. int __fastcall FatalExceptionMessageDialog(Exception * E, TQueryType Type,
  518. int SessionReopenTimeout, const AnsiString MessageFormat, int Answers,
  519. AnsiString HelpKeyword, const TMessageParams * Params)
  520. {
  521. assert(FLAGCLEAR(Answers, qaRetry));
  522. Answers |= qaRetry;
  523. TQueryButtonAlias Aliases[1];
  524. Aliases[0].Button = qaRetry;
  525. Aliases[0].Alias = LoadStr(RECONNECT_BUTTON);
  526. TMessageParams AParams;
  527. if (Params != NULL)
  528. {
  529. AParams = *Params;
  530. }
  531. assert(AParams.Timeout == 0);
  532. // the condition is de facto excess
  533. if (SessionReopenTimeout > 0)
  534. {
  535. AParams.Timeout = SessionReopenTimeout;
  536. AParams.TimeoutAnswer = qaRetry;
  537. }
  538. assert(AParams.Aliases == NULL);
  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_TRANSFER_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. LCID __fastcall GetDefaultLCID()
  784. {
  785. return Is2000() ? GetUserDefaultLCID() : GetThreadLocale();
  786. }
  787. //---------------------------------------------------------------------------
  788. AnsiString __fastcall TranslateDateFormat(AnsiString FormatStr)
  789. {
  790. AnsiString Result;
  791. CALTYPE CalendarType = StrToIntDef(GetLocaleStr(GetDefaultLCID(), LOCALE_ICALENDARTYPE, "1"), 1);
  792. if ((CalendarType != CAL_JAPAN) && (CalendarType != CAL_TAIWAN) && (CalendarType != CAL_KOREA))
  793. {
  794. bool RemoveEra =
  795. (SysLocale.PriLangID == LANG_JAPANESE) ||
  796. (SysLocale.PriLangID == LANG_CHINESE) ||
  797. (SysLocale.PriLangID == LANG_KOREAN);
  798. if (RemoveEra)
  799. {
  800. int I = 1;
  801. while (I <= FormatStr.Length())
  802. {
  803. if ((FormatStr[I] != 'g') && (FormatStr[I] != 'G'))
  804. {
  805. Result += FormatStr[I];
  806. }
  807. I++;
  808. }
  809. }
  810. else
  811. {
  812. Result = FormatStr;
  813. }
  814. }
  815. else
  816. {
  817. int I = 1;
  818. while (I <= FormatStr.Length())
  819. {
  820. if (FormatStr.IsLeadByte(I))
  821. {
  822. int L = CharLength(FormatStr, I);
  823. Result += FormatStr.SubString(I, L);
  824. I += L;
  825. }
  826. else
  827. {
  828. if (StrLIComp(FormatStr.c_str() + I - 1, "gg", 2) == 0)
  829. {
  830. Result += "ggg";
  831. I++;
  832. }
  833. else if (StrLIComp(FormatStr.c_str() + I - 1, "yyyy", 4) == 0)
  834. {
  835. Result += "eeee";
  836. I += 4 - 1;
  837. }
  838. else if (StrLIComp(FormatStr.c_str() + I - 1, "yy", 2) == 0)
  839. {
  840. Result += "ee";
  841. I += 2 - 1;
  842. }
  843. else if ((FormatStr[I] == 'y') || (FormatStr[I] == 'Y'))
  844. {
  845. Result += "e";
  846. }
  847. else
  848. {
  849. Result += FormatStr[I];
  850. }
  851. I++;
  852. }
  853. }
  854. }
  855. return Result;
  856. }
  857. //---------------------------------------------------------------------------
  858. void __fastcall GetFormatSettingsFix()
  859. {
  860. // todo InitSysLocale
  861. // todo GetMonthDayNames
  862. // todo GetEraNamesAndYearOffsets
  863. LCID DefaultLCID = GetDefaultLCID();
  864. CurrencyString = GetLocaleStr(DefaultLCID, LOCALE_SCURRENCY, "");
  865. CurrencyFormat = (Byte) StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ICURRENCY, "0"), 0);
  866. NegCurrFormat = (Byte) StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_INEGCURR, "0"), 0);
  867. ThousandSeparator = GetLocaleChar(DefaultLCID, LOCALE_STHOUSAND, ',');
  868. DecimalSeparator = GetLocaleChar(DefaultLCID, LOCALE_SDECIMAL, '.');
  869. CurrencyDecimals = (Byte) StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ICURRDIGITS, "0"), 0);
  870. DateSeparator = GetLocaleChar(DefaultLCID, LOCALE_SDATE, '/');
  871. ShortDateFormat = TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SSHORTDATE, "m/d/yy"));
  872. LongDateFormat = TranslateDateFormat(GetLocaleStr(DefaultLCID, LOCALE_SLONGDATE, "mmmm d, yyyy"));
  873. TimeSeparator = GetLocaleChar(DefaultLCID, LOCALE_STIME, ':');
  874. TimeAMString = GetLocaleStr(DefaultLCID, LOCALE_S1159, "am");
  875. TimePMString = GetLocaleStr(DefaultLCID, LOCALE_S2359, "pm");
  876. AnsiString HourFormat;
  877. if (StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ITLZERO, "0"), 0) == 0)
  878. {
  879. HourFormat = "h";
  880. }
  881. else
  882. {
  883. HourFormat = "hh";
  884. }
  885. AnsiString TimePrefix, TimePostfix;
  886. if (StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ITIME, "0"), 0) == 0)
  887. {
  888. if (StrToIntDef(GetLocaleStr(DefaultLCID, LOCALE_ITIMEMARKPOSN, "0"), 0) == 0)
  889. {
  890. TimePostfix = " AMPM";
  891. }
  892. else
  893. {
  894. TimePrefix = "AMPM ";
  895. }
  896. }
  897. ShortTimeFormat = TimePrefix + HourFormat + ":mm" + TimePostfix;
  898. LongTimeFormat = TimePrefix + HourFormat + ":mm:ss" + TimePostfix;
  899. ListSeparator = GetLocaleChar(DefaultLCID, LOCALE_SLIST, ',');
  900. }
  901. //---------------------------------------------------------------------------
  902. void __fastcall WinInitialize()
  903. {
  904. if (IsWin7())
  905. {
  906. GetFormatSettingsFix();
  907. }
  908. }
  909. //---------------------------------------------------------------------------
  910. struct TNotifyIconData5
  911. {
  912. DWORD cbSize;
  913. HWND hWnd;
  914. UINT uID;
  915. UINT uFlags;
  916. UINT uCallbackMessage;
  917. HICON hIcon;
  918. CHAR szTip[128];
  919. DWORD dwState;
  920. DWORD dwStateMask;
  921. CHAR szInfo[256];
  922. union {
  923. UINT uTimeout;
  924. UINT uVersion;
  925. } DUMMYUNIONNAME;
  926. CHAR szInfoTitle[64];
  927. DWORD dwInfoFlags;
  928. };
  929. //---------------------------------------------------------------------------
  930. #undef NOTIFYICONDATA_V1_SIZE
  931. #define NOTIFYICONDATA_V1_SIZE FIELD_OFFSET(TNotifyIconData5, szTip[64])
  932. //---------------------------------------------------------------------------
  933. __fastcall TTrayIcon::TTrayIcon(unsigned int Id)
  934. {
  935. FVisible = false;
  936. FOnClick = NULL;
  937. FTrayIcon = new TNotifyIconData5;
  938. memset(FTrayIcon, 0, sizeof(*FTrayIcon));
  939. FTrayIcon->cbSize = ((ShellDllVersion() >= 5) ? sizeof(*FTrayIcon) : NOTIFYICONDATA_V1_SIZE);
  940. FTrayIcon->uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  941. FTrayIcon->hIcon = Application->Icon->Handle;
  942. FTrayIcon->uID = Id;
  943. FTrayIcon->hWnd = AllocateHWnd(WndProc);
  944. FTrayIcon->uCallbackMessage = WM_TRAY_ICON;
  945. }
  946. //---------------------------------------------------------------------------
  947. __fastcall TTrayIcon::~TTrayIcon()
  948. {
  949. // make sure we hide icon even in case it was shown just to pop up the balloon
  950. // (in which case Visible == false)
  951. CancelBalloon();
  952. Visible = false;
  953. DeallocateHWnd(FTrayIcon->hWnd);
  954. delete FTrayIcon;
  955. }
  956. //---------------------------------------------------------------------------
  957. bool __fastcall TTrayIcon::SupportsBalloons()
  958. {
  959. return (ShellDllVersion() >= 5);
  960. }
  961. //---------------------------------------------------------------------------
  962. void __fastcall TTrayIcon::PopupBalloon(AnsiString Title,
  963. const AnsiString & Str, TQueryType QueryType, unsigned int Timeout)
  964. {
  965. if (SupportsBalloons())
  966. {
  967. if (Timeout > 30000)
  968. {
  969. // this is probably system limit, do not try more, especially for
  970. // the timeout-driven hiding of the tray icon (for Win2k)
  971. Timeout = 30000;
  972. }
  973. FTrayIcon->uFlags |= NIF_INFO;
  974. Title = FORMAT("%s - %s", (Title, AppNameString()));
  975. StrPLCopy(FTrayIcon->szInfoTitle, Title, sizeof(FTrayIcon->szInfoTitle) - 1);
  976. StrPLCopy(FTrayIcon->szInfo, Str, sizeof(FTrayIcon->szInfo) - 1);
  977. FTrayIcon->uTimeout = Timeout;
  978. switch (QueryType)
  979. {
  980. case qtError:
  981. FTrayIcon->dwInfoFlags = NIIF_ERROR;
  982. break;
  983. case qtInformation:
  984. case qtConfirmation:
  985. FTrayIcon->dwInfoFlags = NIIF_INFO;
  986. break;
  987. case qtWarning:
  988. default:
  989. FTrayIcon->dwInfoFlags = NIIF_WARNING;
  990. break;
  991. }
  992. KillTimer(FTrayIcon->hWnd, 1);
  993. if (Visible)
  994. {
  995. Update();
  996. }
  997. else
  998. {
  999. Notify(NIM_ADD);
  1000. // can be 5 only anyway, no older version supports balloons
  1001. if (ShellDllVersion() <= 5)
  1002. {
  1003. // version 5 does not notify us when the balloon is hidden, so we must
  1004. // remove the icon ourselves after own timeout
  1005. SetTimer(FTrayIcon->hWnd, 1, Timeout, NULL);
  1006. }
  1007. }
  1008. // Clearing the flag ensures that subsequent updates does not hide the baloon
  1009. // unless CancelBalloon is called explicitly
  1010. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  1011. }
  1012. }
  1013. //---------------------------------------------------------------------------
  1014. void __fastcall TTrayIcon::CancelBalloon()
  1015. {
  1016. if (SupportsBalloons())
  1017. {
  1018. KillTimer(FTrayIcon->hWnd, 1);
  1019. if (Visible)
  1020. {
  1021. FTrayIcon->uFlags |= NIF_INFO;
  1022. FTrayIcon->szInfo[0] = '\0';
  1023. Update();
  1024. FTrayIcon->uFlags = FTrayIcon->uFlags & ~NIF_INFO;
  1025. }
  1026. else
  1027. {
  1028. Notify(NIM_DELETE);
  1029. }
  1030. }
  1031. }
  1032. //---------------------------------------------------------------------------
  1033. bool __fastcall TTrayIcon::Notify(unsigned int Message)
  1034. {
  1035. bool Result = SUCCEEDED(Shell_NotifyIcon(Message, (NOTIFYICONDATA*)FTrayIcon));
  1036. if (Result && (Message == NIM_ADD))
  1037. {
  1038. UINT Timeout = FTrayIcon->uTimeout;
  1039. try
  1040. {
  1041. FTrayIcon->uVersion = NOTIFYICON_VERSION;
  1042. Result = SUCCEEDED(Shell_NotifyIcon(NIM_SETVERSION, (NOTIFYICONDATA*)FTrayIcon));
  1043. }
  1044. __finally
  1045. {
  1046. FTrayIcon->uTimeout = Timeout;
  1047. }
  1048. }
  1049. return Result;
  1050. }
  1051. //---------------------------------------------------------------------------
  1052. void __fastcall TTrayIcon::Update()
  1053. {
  1054. if (Visible)
  1055. {
  1056. Notify(NIM_MODIFY);
  1057. }
  1058. }
  1059. //---------------------------------------------------------------------------
  1060. void __fastcall TTrayIcon::SetVisible(bool value)
  1061. {
  1062. if (Visible != value)
  1063. {
  1064. if (value)
  1065. {
  1066. FVisible = Notify(NIM_ADD);
  1067. }
  1068. else
  1069. {
  1070. FVisible = false;
  1071. KillTimer(FTrayIcon->hWnd, 1);
  1072. Notify(NIM_DELETE);
  1073. }
  1074. }
  1075. }
  1076. //---------------------------------------------------------------------------
  1077. void __fastcall TTrayIcon::WndProc(TMessage & Message)
  1078. {
  1079. try
  1080. {
  1081. if (Message.Msg == WM_TRAY_ICON)
  1082. {
  1083. assert(Message.WParam == 0);
  1084. switch (Message.LParam)
  1085. {
  1086. // old shell32
  1087. case WM_LBUTTONUP:
  1088. case WM_RBUTTONUP:
  1089. // new shell32:
  1090. case WM_CONTEXTMENU:
  1091. case NIN_BALLOONUSERCLICK:
  1092. if (OnClick != NULL)
  1093. {
  1094. OnClick(NULL);
  1095. }
  1096. Message.Result = true;
  1097. break;
  1098. }
  1099. switch (Message.LParam)
  1100. {
  1101. case NIN_BALLOONHIDE:
  1102. case NIN_BALLOONTIMEOUT:
  1103. case NIN_BALLOONUSERCLICK:
  1104. KillTimer(FTrayIcon->hWnd, 1);
  1105. // if icon was shown just to display balloon, hide it with the balloon
  1106. if (!Visible)
  1107. {
  1108. Notify(NIM_DELETE);
  1109. }
  1110. break;
  1111. }
  1112. }
  1113. else if (Message.Msg == WM_TIMER)
  1114. {
  1115. // sanity check
  1116. Notify(NIM_DELETE);
  1117. }
  1118. else
  1119. {
  1120. Message.Result = DefWindowProc(FTrayIcon->hWnd, Message.Msg, Message.WParam, Message.LParam);
  1121. }
  1122. }
  1123. catch(Exception & E)
  1124. {
  1125. Application->HandleException(&E);
  1126. }
  1127. }
  1128. //---------------------------------------------------------------------------
  1129. AnsiString __fastcall TTrayIcon::GetHint()
  1130. {
  1131. return FTrayIcon->szTip;
  1132. }
  1133. //---------------------------------------------------------------------------
  1134. void __fastcall TTrayIcon::SetHint(AnsiString value)
  1135. {
  1136. if (Hint != value)
  1137. {
  1138. unsigned int Max = ((ShellDllVersion() >= 5) ? sizeof(FTrayIcon->szTip) : 64);
  1139. StrPLCopy(FTrayIcon->szTip, value, Max - 1);
  1140. Update();
  1141. }
  1142. }
  1143. //---------------------------------------------------------------------------