UserInterface.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "ScpCommander.h"
  5. #include "ScpExplorer.h"
  6. #include <CoreMain.h>
  7. #include <Common.h>
  8. #include <Exceptions.h>
  9. #include <Cryptography.h>
  10. #include "ProgParams.h"
  11. #include "VCLCommon.h"
  12. #include "WinConfiguration.h"
  13. #include "TerminalManager.h"
  14. #include "TextsWin.h"
  15. #include "TBXThemes.hpp"
  16. #include "TBXOfficeXPTheme.hpp"
  17. #include "TBXOffice2003Theme.hpp"
  18. #include "PasswordEdit.hpp"
  19. #include "ProgParams.h"
  20. #include "Tools.h"
  21. #include "Custom.h"
  22. #include "HelpWin.h"
  23. #include <Math.hpp>
  24. #include <PasTools.hpp>
  25. #include <GUITools.h>
  26. //---------------------------------------------------------------------------
  27. #pragma package(smart_init)
  28. //---------------------------------------------------------------------------
  29. const UnicodeString AppName = L"WinSCP";
  30. //---------------------------------------------------------------------------
  31. TConfiguration * __fastcall CreateConfiguration()
  32. {
  33. WinConfiguration = new TWinConfiguration();
  34. CustomWinConfiguration = WinConfiguration;
  35. GUIConfiguration = CustomWinConfiguration;
  36. TProgramParams * Params = TProgramParams::Instance();
  37. UnicodeString IniFileName = Params->SwitchValue(L"ini");
  38. if (!IniFileName.IsEmpty())
  39. {
  40. if (AnsiSameText(IniFileName, L"nul"))
  41. {
  42. WinConfiguration->SetNulStorage();
  43. }
  44. else if (CheckSafe(Params))
  45. {
  46. IniFileName = ExpandFileName(ExpandEnvironmentVariables(IniFileName));
  47. WinConfiguration->IniFileStorageName = IniFileName;
  48. }
  49. }
  50. if (CheckSafe(Params))
  51. {
  52. std::unique_ptr<TStrings> RawConfig(new TStringList());
  53. if (Params->FindSwitch(L"rawconfig", RawConfig.get()))
  54. {
  55. WinConfiguration->OptionsStorage = RawConfig.get();
  56. }
  57. }
  58. return WinConfiguration;
  59. }
  60. //---------------------------------------------------------------------------
  61. TOptions * __fastcall GetGlobalOptions()
  62. {
  63. return TProgramParams::Instance();
  64. }
  65. //---------------------------------------------------------------------------
  66. TCustomScpExplorerForm * __fastcall CreateScpExplorer()
  67. {
  68. TCustomScpExplorerForm * ScpExplorer;
  69. if (WinConfiguration->Interface == ifExplorer)
  70. {
  71. ScpExplorer = SafeFormCreate<TScpExplorerForm>();
  72. }
  73. else
  74. {
  75. ScpExplorer = SafeFormCreate<TScpCommanderForm>();
  76. }
  77. return ScpExplorer;
  78. }
  79. //---------------------------------------------------------------------------
  80. UnicodeString __fastcall SshVersionString()
  81. {
  82. return FORMAT(L"WinSCP-release-%s", (Configuration->Version));
  83. }
  84. //---------------------------------------------------------------------------
  85. UnicodeString __fastcall AppNameString()
  86. {
  87. return L"WinSCP";
  88. }
  89. //---------------------------------------------------------------------------
  90. UnicodeString __fastcall GetCompanyRegistryKey()
  91. {
  92. return L"Software\\Martin Prikryl";
  93. }
  94. //---------------------------------------------------------------------------
  95. UnicodeString __fastcall GetRegistryKey()
  96. {
  97. return GetCompanyRegistryKey() + L"\\WinSCP 2";
  98. }
  99. //---------------------------------------------------------------------------
  100. static bool ForcedOnForeground = false;
  101. void __fastcall SetOnForeground(bool OnForeground)
  102. {
  103. ForcedOnForeground = OnForeground;
  104. }
  105. //---------------------------------------------------------------------------
  106. void __fastcall FlashOnBackground()
  107. {
  108. assert(Application);
  109. if (!ForcedOnForeground && !ForegroundTask())
  110. {
  111. FlashWindow(Application->MainFormHandle, true);
  112. }
  113. }
  114. //---------------------------------------------------------------------------
  115. void __fastcall LocalSystemSettings(TCustomForm * /*Control*/)
  116. {
  117. // noop
  118. }
  119. //---------------------------------------------------------------------------
  120. void __fastcall ShowExtendedException(Exception * E)
  121. {
  122. ShowExtendedExceptionEx(NULL, E);
  123. }
  124. //---------------------------------------------------------------------------
  125. void __fastcall ShowExtendedExceptionEx(TTerminal * Terminal,
  126. Exception * E)
  127. {
  128. bool Show = ShouldDisplayException(E);
  129. bool DoNotDisplay = false;
  130. try
  131. {
  132. // This is special case used particularly when called from .NET assembly
  133. // (which always uses /nointeractiveinput),
  134. // but can be useful for other console runs too
  135. TProgramParams * Params = TProgramParams::Instance();
  136. if (Params->FindSwitch(L"nointeractiveinput"))
  137. {
  138. DoNotDisplay = true;
  139. if (Show && CheckXmlLogParam(Params))
  140. {
  141. std::unique_ptr<TActionLog> ActionLog(new TActionLog(Configuration));
  142. ActionLog->AddFailure(E);
  143. // unnecessary explicit release
  144. ActionLog.reset(NULL);
  145. }
  146. }
  147. }
  148. catch (Exception & E)
  149. {
  150. // swallow
  151. }
  152. if (!DoNotDisplay)
  153. {
  154. TTerminalManager * Manager = TTerminalManager::Instance(false);
  155. TQueryType Type;
  156. ESshTerminate * Terminate = dynamic_cast<ESshTerminate*>(E);
  157. bool CloseOnCompletion = (Terminate != NULL);
  158. Type = CloseOnCompletion ? qtInformation : qtError;
  159. bool ConfirmExitOnCompletion =
  160. CloseOnCompletion &&
  161. ((Terminate->Operation == odoDisconnect) || (Terminate->Operation == odoSuspend)) &&
  162. WinConfiguration->ConfirmExitOnCompletion;
  163. if (E->InheritsFrom(__classid(EFatal)) && (Terminal != NULL) &&
  164. (Manager != NULL) && (Manager->ActiveTerminal == Terminal))
  165. {
  166. int SessionReopenTimeout = 0;
  167. TManagedTerminal * ManagedTerminal = dynamic_cast<TManagedTerminal *>(Terminal);
  168. if ((ManagedTerminal != NULL) &&
  169. ((Configuration->SessionReopenTimeout == 0) ||
  170. ((double)ManagedTerminal->ReopenStart == 0) ||
  171. (int(double(Now() - ManagedTerminal->ReopenStart) * MSecsPerDay) < Configuration->SessionReopenTimeout)))
  172. {
  173. SessionReopenTimeout = GUIConfiguration->SessionReopenAutoIdle;
  174. }
  175. unsigned int Result;
  176. if (CloseOnCompletion)
  177. {
  178. Manager->DisconnectActiveTerminal();
  179. if (Terminate->Operation == odoSuspend)
  180. {
  181. // suspend, so that exit prompt is shown only after windows resume
  182. SuspendWindows();
  183. }
  184. assert(Show);
  185. if (ConfirmExitOnCompletion)
  186. {
  187. TMessageParams Params(mpNeverAskAgainCheck);
  188. UnicodeString MessageFormat =
  189. MainInstructions((Manager->Count > 1) ?
  190. FMTLOAD(DISCONNECT_ON_COMPLETION, (Manager->Count - 1)) :
  191. LoadStr(EXIT_ON_COMPLETION));
  192. Result = FatalExceptionMessageDialog(E, Type, 0,
  193. MessageFormat,
  194. qaYes | qaNo, HELP_NONE, &Params);
  195. if (Result == qaNeverAskAgain)
  196. {
  197. Result = qaYes;
  198. WinConfiguration->ConfirmExitOnCompletion = false;
  199. }
  200. }
  201. else
  202. {
  203. Result = qaYes;
  204. }
  205. }
  206. else
  207. {
  208. if (Show)
  209. {
  210. Result = FatalExceptionMessageDialog(E, Type, SessionReopenTimeout);
  211. }
  212. else
  213. {
  214. Result = qaOK;
  215. }
  216. }
  217. if (Result == qaYes)
  218. {
  219. assert(CloseOnCompletion);
  220. assert(Terminate != NULL);
  221. assert(Terminate->Operation != odoIdle);
  222. Application->Terminate();
  223. switch (Terminate->Operation)
  224. {
  225. case odoDisconnect:
  226. break;
  227. case odoSuspend:
  228. // suspended before already
  229. break;
  230. case odoShutDown:
  231. ShutDownWindows();
  232. break;
  233. default:
  234. FAIL;
  235. }
  236. }
  237. else if (Result == qaRetry)
  238. {
  239. Manager->ReconnectActiveTerminal();
  240. }
  241. else
  242. {
  243. Manager->FreeActiveTerminal();
  244. }
  245. }
  246. else
  247. {
  248. // this should not happen as we never use Terminal->CloseOnCompletion
  249. // on inactive terminal
  250. if (CloseOnCompletion)
  251. {
  252. assert(Show);
  253. if (ConfirmExitOnCompletion)
  254. {
  255. TMessageParams Params(mpNeverAskAgainCheck);
  256. if (ExceptionMessageDialog(E, Type, L"", qaOK, HELP_NONE, &Params) ==
  257. qaNeverAskAgain)
  258. {
  259. WinConfiguration->ConfirmExitOnCompletion = false;
  260. }
  261. }
  262. }
  263. else
  264. {
  265. if (Show)
  266. {
  267. ExceptionMessageDialog(E, Type);
  268. }
  269. }
  270. }
  271. }
  272. }
  273. //---------------------------------------------------------------------------
  274. void __fastcall ShowNotification(TTerminal * Terminal, const UnicodeString & Str,
  275. TQueryType Type)
  276. {
  277. TTerminalManager * Manager = TTerminalManager::Instance(false);
  278. assert(Manager != NULL);
  279. Manager->ScpExplorer->PopupTrayBalloon(Terminal, Str, Type);
  280. }
  281. //---------------------------------------------------------------------------
  282. void __fastcall ConfigureInterface()
  283. {
  284. UnicodeString S;
  285. S = LoadStr(MIDDLE_EAST);
  286. if (!S.IsEmpty())
  287. {
  288. SysLocale.MiddleEast = static_cast<bool>(StrToInt(S));
  289. }
  290. else
  291. {
  292. SysLocale.MiddleEast = false;
  293. }
  294. S = LoadStr(BIDI_MODE);
  295. if (!S.IsEmpty())
  296. {
  297. Application->BiDiMode = static_cast<TBiDiMode>(StrToInt(bdRightToLeft));
  298. }
  299. else
  300. {
  301. Application->BiDiMode = bdLeftToRight;
  302. }
  303. SetTBXSysParam(TSP_XPVISUALSTYLE, XPVS_AUTOMATIC);
  304. // Can be called during configuration creation.
  305. // Skip now, will be called again later.
  306. if (Configuration != NULL)
  307. {
  308. TBXSetTheme(WinConfiguration->Theme);
  309. }
  310. // Has any effect on Wine only
  311. // (otherwise initial UserDocumentDirectory is equivalent to GetPersonalFolder())
  312. UserDocumentDirectory = GetPersonalFolder();
  313. }
  314. //---------------------------------------------------------------------------
  315. // dummy function to force linking of TBXOfficeXPTheme.pas
  316. void __fastcall CreateThemes()
  317. {
  318. new TTBXOfficeXPTheme(L"OfficeXP");
  319. new TTBXOffice2003Theme(L"Office2003");
  320. }
  321. //---------------------------------------------------------------------------
  322. void __fastcall DoAboutDialog(TConfiguration *Configuration)
  323. {
  324. DoAboutDialog(Configuration, true, NULL);
  325. }
  326. //---------------------------------------------------------------------
  327. void __fastcall DoProductLicense()
  328. {
  329. DoLicenseDialog(lcWinScp);
  330. }
  331. //---------------------------------------------------------------------------
  332. const UnicodeString PixelsPerInchKey = L"PixelsPerInch";
  333. //---------------------------------------------------------------------
  334. int __fastcall GetToolbarLayoutPixelsPerInch(TStrings * Storage)
  335. {
  336. int Result;
  337. if (Storage->IndexOfName(PixelsPerInchKey))
  338. {
  339. Result = LoadPixelsPerInch(Storage->Values[PixelsPerInchKey]);
  340. }
  341. else
  342. {
  343. Result = -1;
  344. }
  345. return Result;
  346. }
  347. //---------------------------------------------------------------------
  348. static inline void __fastcall GetToolbarKey(const UnicodeString & ToolbarName,
  349. const UnicodeString & Value, UnicodeString & ToolbarKey)
  350. {
  351. int ToolbarNameLen;
  352. if ((ToolbarName.Length() > 7) &&
  353. (ToolbarName.SubString(ToolbarName.Length() - 7 + 1, 7) == L"Toolbar"))
  354. {
  355. ToolbarNameLen = ToolbarName.Length() - 7;
  356. }
  357. else
  358. {
  359. ToolbarNameLen = ToolbarName.Length();
  360. }
  361. ToolbarKey = ToolbarName.SubString(1, ToolbarNameLen) + L"_" + Value;
  362. }
  363. //---------------------------------------------------------------------------
  364. static int __fastcall ToolbarReadInt(const UnicodeString ToolbarName,
  365. const UnicodeString Value, const int Default, const void * ExtraData)
  366. {
  367. int Result;
  368. if (Value == L"Rev")
  369. {
  370. Result = 2000;
  371. }
  372. else
  373. {
  374. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  375. UnicodeString ToolbarKey;
  376. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  377. if (Storage->IndexOfName(ToolbarKey) >= 0)
  378. {
  379. Result = StrToIntDef(Storage->Values[ToolbarKey], Default);
  380. #if 0
  381. // this does not work well, as it scales down the stretched
  382. // toolbars (path toolbars) too much, it has to be reimplemented smarter
  383. if (Value == L"DockPos")
  384. {
  385. int PixelsPerInch = GetToolbarLayoutPixelsPerInch(Storage);
  386. // When DPI has decreased since the last time, scale down
  387. // toolbar position to get rid of gaps caused by smaller labels.
  388. // Do not do this when DPI has increased as it would introduce gaps,
  389. // as toolbars consists mostly of icons only, that do not scale.
  390. // The toolbars shift themselves anyway, when other toolbars to the left
  391. // get wider. We also risk a bit that toolbar order changes,
  392. // as with very small toolbars (History) we can get scaled down position
  393. // of the following toolbar to the left of it.
  394. // There's special handling (also for scaling-up) stretched toolbars
  395. // in LoadToolbarsLayoutStr
  396. if ((PixelsPerInch > 0) && (Screen->PixelsPerInch < PixelsPerInch))
  397. {
  398. Result = LoadDimension(Result, PixelsPerInch);
  399. }
  400. }
  401. #endif
  402. }
  403. else
  404. {
  405. Result = Default;
  406. }
  407. }
  408. return Result;
  409. }
  410. //---------------------------------------------------------------------------
  411. static UnicodeString __fastcall ToolbarReadString(const UnicodeString ToolbarName,
  412. const UnicodeString Value, const UnicodeString Default, const void * ExtraData)
  413. {
  414. UnicodeString Result;
  415. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  416. UnicodeString ToolbarKey;
  417. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  418. if (Storage->IndexOfName(ToolbarKey) >= 0)
  419. {
  420. Result = Storage->Values[ToolbarKey];
  421. }
  422. else
  423. {
  424. Result = Default;
  425. }
  426. return Result;
  427. }
  428. //---------------------------------------------------------------------------
  429. static void __fastcall ToolbarWriteInt(const UnicodeString ToolbarName,
  430. const UnicodeString Value, const int Data, const void * ExtraData)
  431. {
  432. if (Value != L"Rev")
  433. {
  434. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  435. UnicodeString ToolbarKey;
  436. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  437. assert(Storage->IndexOfName(ToolbarKey) < 0);
  438. Storage->Values[ToolbarKey] = IntToStr(Data);
  439. }
  440. }
  441. //---------------------------------------------------------------------------
  442. static void __fastcall ToolbarWriteString(const UnicodeString ToolbarName,
  443. const UnicodeString Value, const UnicodeString Data, const void * ExtraData)
  444. {
  445. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  446. UnicodeString ToolbarKey;
  447. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  448. assert(Storage->IndexOfName(ToolbarKey) < 0);
  449. Storage->Values[ToolbarKey] = Data;
  450. }
  451. //---------------------------------------------------------------------------
  452. UnicodeString __fastcall GetToolbarsLayoutStr(TComponent * OwnerComponent)
  453. {
  454. UnicodeString Result;
  455. TStrings * Storage = new TStringList();
  456. try
  457. {
  458. TBCustomSavePositions(OwnerComponent, ToolbarWriteInt, ToolbarWriteString,
  459. Storage);
  460. Storage->Values[PixelsPerInchKey] = SavePixelsPerInch();
  461. Result = Storage->CommaText;
  462. }
  463. __finally
  464. {
  465. delete Storage;
  466. }
  467. return Result;
  468. }
  469. //---------------------------------------------------------------------------
  470. void __fastcall LoadToolbarsLayoutStr(TComponent * OwnerComponent, UnicodeString LayoutStr)
  471. {
  472. TStrings * Storage = new TStringList();
  473. try
  474. {
  475. Storage->CommaText = LayoutStr;
  476. TBCustomLoadPositions(OwnerComponent, ToolbarReadInt, ToolbarReadString,
  477. Storage);
  478. int PixelsPerInch = GetToolbarLayoutPixelsPerInch(Storage);
  479. // Scale toolbars stretched to the first other toolbar to the right
  480. if ((PixelsPerInch > 0) && (PixelsPerInch != Screen->PixelsPerInch)) // optimization
  481. {
  482. for (int Index = 0; Index < OwnerComponent->ComponentCount; Index++)
  483. {
  484. TTBXToolbar * Toolbar =
  485. dynamic_cast<TTBXToolbar *>(OwnerComponent->Components[Index]);
  486. if ((Toolbar != NULL) && Toolbar->Stretch &&
  487. (Toolbar->OnGetBaseSize != NULL) &&
  488. // we do not support floating of stretched toolbars
  489. ALWAYS_TRUE(!Toolbar->Floating))
  490. {
  491. TTBXToolbar * FollowingToolbar = NULL;
  492. for (int Index2 = 0; Index2 < OwnerComponent->ComponentCount; Index2++)
  493. {
  494. TTBXToolbar * Toolbar2 =
  495. dynamic_cast<TTBXToolbar *>(OwnerComponent->Components[Index2]);
  496. if ((Toolbar2 != NULL) && !Toolbar2->Floating &&
  497. (Toolbar2->Parent == Toolbar->Parent) &&
  498. (Toolbar2->DockRow == Toolbar->DockRow) &&
  499. (Toolbar2->DockPos > Toolbar->DockPos) &&
  500. ((FollowingToolbar == NULL) || (FollowingToolbar->DockPos > Toolbar2->DockPos)))
  501. {
  502. FollowingToolbar = Toolbar2;
  503. }
  504. }
  505. if (FollowingToolbar != NULL)
  506. {
  507. int NewWidth = LoadDimension(Toolbar->Width, PixelsPerInch);
  508. FollowingToolbar->DockPos += NewWidth - Toolbar->Width;
  509. }
  510. }
  511. }
  512. }
  513. }
  514. __finally
  515. {
  516. delete Storage;
  517. }
  518. }
  519. //---------------------------------------------------------------------------
  520. void __fastcall AddMenuSeparator(TTBCustomItem * Menu)
  521. {
  522. TTBXSeparatorItem * Item = new TTBXSeparatorItem(Menu);
  523. Menu->Add(Item);
  524. }
  525. //---------------------------------------------------------------------------
  526. static TComponent * LastPopupComponent = NULL;
  527. static TRect LastPopupRect(-1, -1, -1, -1);
  528. static TDateTime LastCloseUp;
  529. //---------------------------------------------------------------------------
  530. void __fastcall MenuPopup(TPopupMenu * AMenu, TRect Rect,
  531. TComponent * PopupComponent)
  532. {
  533. // Pressing the same button within 200ms after closing its popup menu
  534. // does nothing.
  535. // It is to immitate close-by-click behavior. Note that menu closes itself
  536. // before onclick handler of button occurs.
  537. // To support content menu popups, we have to check for the popup location too,
  538. // to allow poping menu on different location (such as different node of TTreeView),
  539. // even if there's another popup opened already (so that the time interval
  540. // below does not elapse).
  541. TDateTime N = Now();
  542. TDateTime Diff = N - LastCloseUp;
  543. if ((PopupComponent == LastPopupComponent) &&
  544. (Rect == LastPopupRect) &&
  545. (Diff < TDateTime(0, 0, 0, 200)))
  546. {
  547. LastPopupComponent = NULL;
  548. }
  549. else
  550. {
  551. TTBXPopupMenu * Menu = dynamic_cast<TTBXPopupMenu *>(AMenu);
  552. if (Menu == NULL)
  553. {
  554. Menu = CreateTBXPopupMenu(AMenu->Owner);
  555. Menu->OnPopup = AMenu->OnPopup;
  556. Menu->Items->SubMenuImages = AMenu->Images;
  557. for (int Index = 0; Index < AMenu->Items->Count; Index++)
  558. {
  559. TMenuItem * AItem = AMenu->Items->Items[Index];
  560. TTBCustomItem * Item;
  561. if (!AItem->Enabled && !AItem->Visible && (AItem->Action == NULL) &&
  562. (AItem->OnClick == NULL) && ALWAYS_TRUE(AItem->Count == 0))
  563. {
  564. TTBXLabelItem * LabelItem = new TTBXLabelItem(Menu);
  565. // TTBXLabelItem has it's own Caption
  566. LabelItem->Caption = AItem->Caption;
  567. LabelItem->SectionHeader = true;
  568. Item = LabelItem;
  569. }
  570. else
  571. {
  572. // see TB2DsgnConverter.pas DoConvert
  573. if (AItem->Caption == L"-")
  574. {
  575. Item = new TTBXSeparatorItem(Menu);
  576. }
  577. else
  578. {
  579. if (AItem->Count > 0)
  580. {
  581. Item = new TTBXSubmenuItem(Menu);
  582. }
  583. else
  584. {
  585. Item = new TTBXItem(Menu);
  586. }
  587. Item->Action = AItem->Action;
  588. Item->AutoCheck = AItem->AutoCheck;
  589. Item->Caption = AItem->Caption;
  590. Item->Checked = AItem->Checked;
  591. if (AItem->Default)
  592. {
  593. Item->Options = Item->Options << tboDefault;
  594. }
  595. Item->Enabled = AItem->Enabled;
  596. Item->GroupIndex = AItem->GroupIndex;
  597. Item->HelpContext = AItem->HelpContext;
  598. Item->ImageIndex = AItem->ImageIndex;
  599. Item->RadioItem = AItem->RadioItem;
  600. Item->ShortCut = AItem->ShortCut;
  601. Item->SubMenuImages = AItem->SubMenuImages;
  602. Item->OnClick = AItem->OnClick;
  603. }
  604. Item->Hint = AItem->Hint;
  605. Item->Tag = AItem->Tag;
  606. Item->Visible = AItem->Visible;
  607. // recurse is supported only for empty submenus (as used for custom commands)
  608. if ((AItem->Count > 0) &&
  609. ALWAYS_TRUE((AItem->Count == 1) && (AItem->Items[0]->Caption == L"")))
  610. {
  611. Item->Add(new TTBXItem(Menu));
  612. }
  613. }
  614. Menu->Items->Add(Item);
  615. }
  616. }
  617. Menu->PopupComponent = PopupComponent;
  618. Menu->PopupEx(Rect);
  619. LastPopupComponent = PopupComponent;
  620. LastPopupRect = Rect;
  621. LastCloseUp = Now();
  622. }
  623. }
  624. //---------------------------------------------------------------------------
  625. const int ColorCols = 8;
  626. const int StandardColorRows = 2;
  627. const int StandardColorCount = ColorCols * StandardColorRows;
  628. const int UserColorRows = 1;
  629. const int UserColorCount = UserColorRows * ColorCols;
  630. const wchar_t ColorSeparator = L',';
  631. //---------------------------------------------------------------------------
  632. static void __fastcall GetStandardSessionColorInfo(
  633. int Col, int Row, TColor & Color, UnicodeString & Name)
  634. {
  635. #define COLOR_INFO(COL, ROW, NAME, COLOR) \
  636. if ((Col == COL) && (Row == ROW)) { Name = NAME; Color = TColor(COLOR); } else
  637. // bottom row of default TBX color set
  638. COLOR_INFO(0, 0, L"Rose", 0xCC99FF)
  639. COLOR_INFO(1, 0, L"Tan", 0x99CCFF)
  640. COLOR_INFO(2, 0, L"Light Yellow", 0x99FFFF)
  641. COLOR_INFO(3, 0, L"Light Green", 0xCCFFCC)
  642. COLOR_INFO(4, 0, L"Light Turquoise", 0xFFFFCC)
  643. COLOR_INFO(5, 0, L"Pale Blue", 0xFFCC99)
  644. COLOR_INFO(6, 0, L"Lavender", 0xFF99CC)
  645. // second row of Excel 2010 palette with second color (Lighter Black) skipped
  646. COLOR_INFO(7, 0, L"Light Orange", 0xB5D5FB)
  647. COLOR_INFO(0, 1, L"Darker White", 0xD8D8D8)
  648. COLOR_INFO(1, 1, L"Darker Tan", 0x97BDC4)
  649. COLOR_INFO(2, 1, L"Lighter Blue", 0xE2B38D)
  650. COLOR_INFO(3, 1, L"Light Blue", 0xE4CCB8)
  651. COLOR_INFO(4, 1, L"Lighter Red", 0xB7B9E5)
  652. COLOR_INFO(5, 1, L"Light Olive Green", 0xBCE3D7)
  653. COLOR_INFO(6, 1, L"Light Purple", 0xD9C1CC)
  654. COLOR_INFO(7, 1, L"Light Aqua", 0xE8DDB7)
  655. FAIL;
  656. #undef COLOR_INFO
  657. }
  658. //---------------------------------------------------------------------------
  659. static void __fastcall SessionColorSetGetColorInfo(
  660. void * /*Data*/, TTBXCustomColorSet * /*Sender*/, int Col, int Row, TColor & Color, UnicodeString & Name)
  661. {
  662. GetStandardSessionColorInfo(Col, Row, Color, Name);
  663. }
  664. //---------------------------------------------------------------------------
  665. static TColor __fastcall RestoreColor(UnicodeString CStr)
  666. {
  667. return TColor(StrToInt(UnicodeString(L"$") + CStr));
  668. }
  669. //---------------------------------------------------------------------------
  670. static UnicodeString __fastcall StoreColor(TColor Color)
  671. {
  672. return IntToHex(Color, 6);
  673. }
  674. //---------------------------------------------------------------------------
  675. static UnicodeString __fastcall ExtractColorStr(UnicodeString & Colors)
  676. {
  677. return ::CutToChar(Colors, ColorSeparator, true);
  678. }
  679. //---------------------------------------------------------------------------
  680. static bool __fastcall IsStandardColor(TColor Color)
  681. {
  682. for (int Row = 0; Row < StandardColorRows; Row++)
  683. {
  684. for (int Col = 0; Col < ColorCols; Col++)
  685. {
  686. TColor StandardColor;
  687. UnicodeString Name; // unused
  688. GetStandardSessionColorInfo(Col, Row, StandardColor, Name);
  689. if (StandardColor == Color)
  690. {
  691. return true;
  692. }
  693. }
  694. }
  695. return false;
  696. }
  697. //---------------------------------------------------------------------------
  698. class TColorChangeData : public TComponent
  699. {
  700. public:
  701. __fastcall TColorChangeData(TColorChangeEvent OnColorChange, TColor Color);
  702. static TColorChangeData * __fastcall Retrieve(TObject * Object);
  703. void __fastcall ColorChange(TColor Color);
  704. __property TColor Color = { read = FColor };
  705. private:
  706. TColorChangeEvent FOnColorChange;
  707. TColor FColor;
  708. };
  709. //---------------------------------------------------------------------------
  710. __fastcall TColorChangeData::TColorChangeData(TColorChangeEvent OnColorChange, TColor Color) :
  711. TComponent(NULL)
  712. {
  713. Name = QualifiedClassName();
  714. FOnColorChange = OnColorChange;
  715. FColor = Color;
  716. }
  717. //---------------------------------------------------------------------------
  718. TColorChangeData * __fastcall TColorChangeData::Retrieve(TObject * Object)
  719. {
  720. TComponent * Component = NOT_NULL(dynamic_cast<TComponent *>(Object));
  721. TComponent * ColorChangeDataComponent = Component->FindComponent(QualifiedClassName());
  722. return NOT_NULL(dynamic_cast<TColorChangeData *>(ColorChangeDataComponent));
  723. }
  724. //---------------------------------------------------------------------------
  725. void __fastcall TColorChangeData::ColorChange(TColor Color)
  726. {
  727. if ((Color != TColor(0)) &&
  728. !IsStandardColor(Color))
  729. {
  730. UnicodeString SessionColors = StoreColor(Color);
  731. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  732. while (!Temp.IsEmpty())
  733. {
  734. UnicodeString CStr = ExtractColorStr(Temp);
  735. if (RestoreColor(CStr) != Color)
  736. {
  737. SessionColors += UnicodeString(ColorSeparator) + CStr;
  738. }
  739. }
  740. CustomWinConfiguration->SessionColors = SessionColors;
  741. }
  742. FOnColorChange(Color);
  743. }
  744. //---------------------------------------------------------------------------
  745. static void __fastcall ColorDefaultClick(void * /*Data*/, TObject * Sender)
  746. {
  747. TColorChangeData::Retrieve(Sender)->ColorChange(TColor(0));
  748. }
  749. //---------------------------------------------------------------------------
  750. static void __fastcall ColorPaletteChange(void * /*Data*/, TObject * Sender)
  751. {
  752. TTBXColorPalette * ColorPalette = NOT_NULL(dynamic_cast<TTBXColorPalette *>(Sender));
  753. TColor Color = (ColorPalette->Color != Vcl::Graphics::clNone ? ColorPalette->Color : (TColor)0);
  754. TColorChangeData::Retrieve(Sender)->ColorChange(Color);
  755. }
  756. //---------------------------------------------------------------------------
  757. static UnicodeString __fastcall CustomColorName(int Index)
  758. {
  759. return UnicodeString(L"Color") + wchar_t(L'A' + Index);
  760. }
  761. //---------------------------------------------------------------------------
  762. static void __fastcall ColorPickClick(void * /*Data*/, TObject * Sender)
  763. {
  764. TColorChangeData * ColorChangeData = TColorChangeData::Retrieve(Sender);
  765. std::unique_ptr<TColorDialog> Dialog(new TColorDialog(Application));
  766. Dialog->Options = Dialog->Options << cdFullOpen << cdAnyColor;
  767. Dialog->Color = (ColorChangeData->Color != 0 ? ColorChangeData->Color : clSkyBlue);
  768. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  769. int StandardColorIndex = 0;
  770. int CustomColors = Min(MaxCustomColors, StandardColorCount);
  771. for (int Index = 0; Index < CustomColors; Index++)
  772. {
  773. TColor CustomColor;
  774. if (!Temp.IsEmpty())
  775. {
  776. CustomColor = RestoreColor(ExtractColorStr(Temp));
  777. }
  778. else
  779. {
  780. UnicodeString Name; // not used
  781. assert(StandardColorIndex < StandardColorCount);
  782. GetStandardSessionColorInfo(
  783. StandardColorIndex % ColorCols, StandardColorIndex / ColorCols,
  784. CustomColor, Name);
  785. StandardColorIndex++;
  786. }
  787. Dialog->CustomColors->Values[CustomColorName(Index)] = StoreColor(CustomColor);
  788. }
  789. if (Dialog->Execute())
  790. {
  791. // so that we do not have to try to preserve the excess colors
  792. assert(UserColorCount <= MaxCustomColors);
  793. UnicodeString SessionColors;
  794. for (int Index = 0; Index < MaxCustomColors; Index++)
  795. {
  796. UnicodeString CStr = Dialog->CustomColors->Values[CustomColorName(Index)];
  797. if (!CStr.IsEmpty())
  798. {
  799. TColor CustomColor = RestoreColor(CStr);
  800. if (!IsStandardColor(CustomColor))
  801. {
  802. AddToList(SessionColors, StoreColor(CustomColor), ColorSeparator);
  803. }
  804. }
  805. }
  806. CustomWinConfiguration->SessionColors = SessionColors;
  807. // call color change only after copying custom colors back,
  808. // so that it can add selected color to the user list
  809. ColorChangeData->ColorChange(Dialog->Color);
  810. }
  811. }
  812. //---------------------------------------------------------------------------
  813. TPopupMenu * __fastcall CreateSessionColorPopupMenu(TColor Color,
  814. TColorChangeEvent OnColorChange)
  815. {
  816. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  817. CreateSessionColorMenu(PopupMenu->Items, Color, OnColorChange);
  818. return PopupMenu.release();
  819. }
  820. //---------------------------------------------------------------------------
  821. static void __fastcall UserSessionColorSetGetColorInfo(
  822. void * /*Data*/, TTBXCustomColorSet * Sender, int Col, int Row, TColor & Color, UnicodeString & /*Name*/)
  823. {
  824. int Index = (Row * Sender->ColCount) + Col;
  825. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  826. while ((Index > 0) && !Temp.IsEmpty())
  827. {
  828. ExtractColorStr(Temp);
  829. Index--;
  830. }
  831. if (!Temp.IsEmpty())
  832. {
  833. Color = RestoreColor(ExtractColorStr(Temp));
  834. }
  835. else
  836. {
  837. // hide the trailing cells
  838. Color = Vcl::Graphics::clNone;
  839. }
  840. }
  841. //---------------------------------------------------------------------------
  842. void __fastcall CreateColorPalette(TTBCustomItem * Owner, TColor Color, int Rows,
  843. TCSGetColorInfo OnGetColorInfo, TColorChangeEvent OnColorChange)
  844. {
  845. TTBXCustomColorSet * ColorSet = new TTBXCustomColorSet(Owner);
  846. TTBXColorPalette * ColorPalette = new TTBXColorPalette(Owner);
  847. ColorPalette->InsertComponent(ColorSet);
  848. ColorPalette->ColorSet = ColorSet;
  849. // has to be set only after it's assigned to color palette
  850. ColorSet->ColCount = ColorCols;
  851. ColorSet->RowCount = Rows;
  852. ColorSet->OnGetColorInfo = OnGetColorInfo;
  853. ColorPalette->Color = (Color != 0) ? Color : Vcl::Graphics::clNone;
  854. ColorPalette->OnChange = MakeMethod<TNotifyEvent>(NULL, ColorPaletteChange);
  855. ColorPalette->InsertComponent(new TColorChangeData(OnColorChange, Color));
  856. Owner->Add(ColorPalette);
  857. Owner->Add(new TTBXSeparatorItem(Owner));
  858. }
  859. //---------------------------------------------------------------------------
  860. void __fastcall CreateSessionColorMenu(TComponent * AOwner, TColor Color,
  861. TColorChangeEvent OnColorChange)
  862. {
  863. TTBCustomItem * Owner = dynamic_cast<TTBCustomItem *>(AOwner);
  864. if (ALWAYS_TRUE(Owner != NULL))
  865. {
  866. Owner->Clear();
  867. TTBCustomItem * Item;
  868. Item = new TTBXItem(Owner);
  869. Item->Caption = LoadStr(COLOR_DEFAULT_CAPTION);
  870. Item->Hint = LoadStr(COLOR_DEFAULT_HINT);
  871. Item->HelpKeyword = HELP_COLOR;
  872. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorDefaultClick);
  873. Item->Checked = (Color == TColor(0));
  874. Item->InsertComponent(new TColorChangeData(OnColorChange, Color));
  875. Owner->Add(Item);
  876. Owner->Add(new TTBXSeparatorItem(Owner));
  877. int SessionColorCount = 0;
  878. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  879. while (!Temp.IsEmpty())
  880. {
  881. SessionColorCount++;
  882. ExtractColorStr(Temp);
  883. }
  884. if (SessionColorCount > 0)
  885. {
  886. SessionColorCount = Min(SessionColorCount, UserColorCount);
  887. int RowCount = ((SessionColorCount + ColorCols - 1) / ColorCols);
  888. assert(RowCount <= UserColorRows);
  889. CreateColorPalette(Owner, Color, RowCount,
  890. MakeMethod<TCSGetColorInfo>(NULL, UserSessionColorSetGetColorInfo),
  891. OnColorChange);
  892. }
  893. CreateColorPalette(Owner, Color, StandardColorRows,
  894. MakeMethod<TCSGetColorInfo>(NULL, SessionColorSetGetColorInfo),
  895. OnColorChange);
  896. Owner->Add(new TTBXSeparatorItem(Owner));
  897. Item = new TTBXItem(Owner);
  898. Item->Caption = LoadStr(COLOR_PICK_CAPTION);
  899. Item->Hint = LoadStr(COLOR_PICK_HINT);
  900. Item->HelpKeyword = HELP_COLOR;
  901. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorPickClick);
  902. Item->InsertComponent(new TColorChangeData(OnColorChange, Color));
  903. Owner->Add(Item);
  904. }
  905. }
  906. //---------------------------------------------------------------------------
  907. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  908. {
  909. // no-op yet
  910. }
  911. //---------------------------------------------------------------------------
  912. struct TThreadParam
  913. {
  914. TThreadFunc ThreadFunc;
  915. void * Parameter;
  916. };
  917. //---------------------------------------------------------------------------
  918. static int __fastcall ThreadProc(void * AParam)
  919. {
  920. TThreadParam * Param = reinterpret_cast<TThreadParam *>(AParam);
  921. unsigned int Result = Param->ThreadFunc(Param->Parameter);
  922. delete Param;
  923. EndThread(Result);
  924. return Result;
  925. }
  926. //---------------------------------------------------------------------------
  927. int __fastcall StartThread(void * SecurityAttributes, unsigned StackSize,
  928. TThreadFunc ThreadFunc, void * Parameter, unsigned CreationFlags,
  929. TThreadID & ThreadId)
  930. {
  931. TThreadParam * Param = new TThreadParam;
  932. Param->ThreadFunc = ThreadFunc;
  933. Param->Parameter = Parameter;
  934. return BeginThread(SecurityAttributes, StackSize, ThreadProc, Param,
  935. CreationFlags, ThreadId);
  936. }
  937. //---------------------------------------------------------------------------
  938. static TShortCut FirstCtrlNumberShortCut = ShortCut(L'0', TShiftState() << ssCtrl);
  939. static TShortCut LastCtrlNumberShortCut = ShortCut(L'9', TShiftState() << ssCtrl);
  940. static TShortCut FirstShiftCtrlAltLetterShortCut = ShortCut(L'A', TShiftState() << ssShift << ssCtrl << ssAlt);
  941. static TShortCut LastShiftCtrlAltLetterShortCut = ShortCut(L'Z', TShiftState() << ssShift << ssCtrl << ssAlt);
  942. //---------------------------------------------------------------------------
  943. void __fastcall InitializeShortCutCombo(TComboBox * ComboBox,
  944. const TShortCuts & ShortCuts)
  945. {
  946. ComboBox->Items->BeginUpdate();
  947. try
  948. {
  949. ComboBox->Items->Clear();
  950. ComboBox->Items->AddObject(LoadStr(SHORTCUT_NONE), reinterpret_cast<TObject* >(0));
  951. for (TShortCut AShortCut = FirstCtrlNumberShortCut; AShortCut <= LastCtrlNumberShortCut; AShortCut++)
  952. {
  953. if (!ShortCuts.Has(AShortCut))
  954. {
  955. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  956. }
  957. }
  958. for (TShortCut AShortCut = FirstShiftCtrlAltLetterShortCut; AShortCut <= LastShiftCtrlAltLetterShortCut; AShortCut++)
  959. {
  960. if (!ShortCuts.Has(AShortCut))
  961. {
  962. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  963. }
  964. }
  965. }
  966. __finally
  967. {
  968. ComboBox->Items->EndUpdate();
  969. }
  970. ComboBox->Style = csDropDownList;
  971. ComboBox->DropDownCount = Max(ComboBox->DropDownCount, 16);
  972. }
  973. //---------------------------------------------------------------------------
  974. void __fastcall SetShortCutCombo(TComboBox * ComboBox, TShortCut Value)
  975. {
  976. for (int Index = ComboBox->Items->Count - 1; Index >= 0; Index--)
  977. {
  978. TShortCut AShortCut = TShortCut(ComboBox->Items->Objects[Index]);
  979. if (AShortCut == Value)
  980. {
  981. ComboBox->ItemIndex = Index;
  982. break;
  983. }
  984. else if (AShortCut < Value)
  985. {
  986. assert(Value != 0);
  987. ComboBox->Items->InsertObject(Index + 1, ShortCutToText(Value),
  988. reinterpret_cast<TObject* >(Value));
  989. ComboBox->ItemIndex = Index + 1;
  990. break;
  991. }
  992. assert(Index > 0);
  993. }
  994. }
  995. //---------------------------------------------------------------------------
  996. TShortCut __fastcall GetShortCutCombo(TComboBox * ComboBox)
  997. {
  998. return TShortCut(ComboBox->Items->Objects[ComboBox->ItemIndex]);
  999. }
  1000. //---------------------------------------------------------------------------
  1001. bool __fastcall IsCustomShortCut(TShortCut ShortCut)
  1002. {
  1003. return
  1004. ((FirstCtrlNumberShortCut <= ShortCut) && (ShortCut <= LastCtrlNumberShortCut)) ||
  1005. ((FirstShiftCtrlAltLetterShortCut <= ShortCut) && (ShortCut <= LastShiftCtrlAltLetterShortCut));
  1006. }
  1007. //---------------------------------------------------------------------------
  1008. //---------------------------------------------------------------------------
  1009. class TMasterPasswordDialog : public TCustomDialog
  1010. {
  1011. public:
  1012. __fastcall TMasterPasswordDialog(bool Current);
  1013. bool __fastcall Execute(UnicodeString & CurrentPassword, UnicodeString & NewPassword);
  1014. protected:
  1015. virtual void __fastcall DoValidate();
  1016. virtual void __fastcall DoChange(bool & CanSubmit);
  1017. private:
  1018. TPasswordEdit * CurrentEdit;
  1019. TPasswordEdit * NewEdit;
  1020. TPasswordEdit * ConfirmEdit;
  1021. };
  1022. //---------------------------------------------------------------------------
  1023. __fastcall TMasterPasswordDialog::TMasterPasswordDialog(bool Current) :
  1024. TCustomDialog(Current ? HELP_MASTER_PASSWORD_CURRENT : HELP_MASTER_PASSWORD_CHANGE)
  1025. {
  1026. Caption = LoadStr(MASTER_PASSWORD_CAPTION);
  1027. CurrentEdit = new TPasswordEdit(this);
  1028. AddEdit(CurrentEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CURRENT)));
  1029. EnableControl(CurrentEdit, Current || WinConfiguration->UseMasterPassword);
  1030. CurrentEdit->MaxLength = PasswordMaxLength();
  1031. if (!Current)
  1032. {
  1033. NewEdit = new TPasswordEdit(this);
  1034. AddEdit(NewEdit, CreateLabel(LoadStr(MASTER_PASSWORD_NEW)));
  1035. NewEdit->MaxLength = CurrentEdit->MaxLength;
  1036. if (!WinConfiguration->UseMasterPassword)
  1037. {
  1038. ActiveControl = NewEdit;
  1039. }
  1040. ConfirmEdit = new TPasswordEdit(this);
  1041. AddEdit(ConfirmEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CONFIRM)));
  1042. ConfirmEdit->MaxLength = CurrentEdit->MaxLength;
  1043. }
  1044. else
  1045. {
  1046. NewEdit = NULL;
  1047. ConfirmEdit = NULL;
  1048. }
  1049. }
  1050. //---------------------------------------------------------------------------
  1051. bool __fastcall TMasterPasswordDialog::Execute(
  1052. UnicodeString & CurrentPassword, UnicodeString & NewPassword)
  1053. {
  1054. bool Result = TCustomDialog::Execute();
  1055. if (Result)
  1056. {
  1057. if (CurrentEdit->Enabled)
  1058. {
  1059. CurrentPassword = CurrentEdit->Text;
  1060. }
  1061. if (NewEdit != NULL)
  1062. {
  1063. NewPassword = NewEdit->Text;
  1064. }
  1065. }
  1066. return Result;
  1067. }
  1068. //---------------------------------------------------------------------------
  1069. void __fastcall TMasterPasswordDialog::DoChange(bool & CanSubmit)
  1070. {
  1071. CanSubmit =
  1072. (!WinConfiguration->UseMasterPassword || (IsValidPassword(CurrentEdit->Text) >= 0)) &&
  1073. ((NewEdit == NULL) || (IsValidPassword(NewEdit->Text) >= 0)) &&
  1074. ((ConfirmEdit == NULL) || (IsValidPassword(ConfirmEdit->Text) >= 0));
  1075. TCustomDialog::DoChange(CanSubmit);
  1076. }
  1077. //---------------------------------------------------------------------------
  1078. void __fastcall TMasterPasswordDialog::DoValidate()
  1079. {
  1080. TCustomDialog::DoValidate();
  1081. if (WinConfiguration->UseMasterPassword &&
  1082. !WinConfiguration->ValidateMasterPassword(CurrentEdit->Text))
  1083. {
  1084. CurrentEdit->SetFocus();
  1085. CurrentEdit->SelectAll();
  1086. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_INCORRECT)));
  1087. }
  1088. if (NewEdit != NULL)
  1089. {
  1090. if (NewEdit->Text != ConfirmEdit->Text)
  1091. {
  1092. ConfirmEdit->SetFocus();
  1093. ConfirmEdit->SelectAll();
  1094. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_DIFFERENT)));
  1095. }
  1096. int Valid = IsValidPassword(NewEdit->Text);
  1097. if (Valid <= 0)
  1098. {
  1099. assert(Valid == 0);
  1100. if (MessageDialog(LoadStr(MASTER_PASSWORD_SIMPLE2), qtWarning,
  1101. qaOK | qaCancel, HELP_MASTER_PASSWORD_SIMPLE) == qaCancel)
  1102. {
  1103. NewEdit->SetFocus();
  1104. NewEdit->SelectAll();
  1105. Abort();
  1106. }
  1107. }
  1108. }
  1109. }
  1110. //---------------------------------------------------------------------------
  1111. static bool __fastcall DoMasterPasswordDialog(bool Current,
  1112. UnicodeString & NewPassword)
  1113. {
  1114. bool Result;
  1115. TMasterPasswordDialog * Dialog = new TMasterPasswordDialog(Current);
  1116. try
  1117. {
  1118. UnicodeString CurrentPassword;
  1119. Result = Dialog->Execute(CurrentPassword, NewPassword);
  1120. if (Result)
  1121. {
  1122. if ((Current || WinConfiguration->UseMasterPassword) &&
  1123. ALWAYS_TRUE(!CurrentPassword.IsEmpty()))
  1124. {
  1125. WinConfiguration->SetMasterPassword(CurrentPassword);
  1126. }
  1127. }
  1128. }
  1129. __finally
  1130. {
  1131. delete Dialog;
  1132. }
  1133. return Result;
  1134. }
  1135. //---------------------------------------------------------------------------
  1136. bool __fastcall DoMasterPasswordDialog()
  1137. {
  1138. UnicodeString NewPassword;
  1139. bool Result = DoMasterPasswordDialog(true, NewPassword);
  1140. assert(NewPassword.IsEmpty());
  1141. return Result;
  1142. }
  1143. //---------------------------------------------------------------------------
  1144. bool __fastcall DoChangeMasterPasswordDialog(UnicodeString & NewPassword)
  1145. {
  1146. bool Result = DoMasterPasswordDialog(false, NewPassword);
  1147. return Result;
  1148. }
  1149. //---------------------------------------------------------------------------
  1150. void __fastcall MessageWithNoHelp(const UnicodeString & Message)
  1151. {
  1152. TMessageParams Params;
  1153. Params.AllowHelp = false; // to avoid recursion
  1154. if (MessageDialog(LoadStr(HELP_SEND_MESSAGE2), qtConfirmation,
  1155. qaOK | qaCancel, HELP_NONE, &Params) == qaOK)
  1156. {
  1157. SearchHelp(Message);
  1158. }
  1159. }
  1160. //---------------------------------------------------------------------------
  1161. bool __fastcall CheckXmlLogParam(TProgramParams * Params)
  1162. {
  1163. UnicodeString LogFile;
  1164. bool Result =
  1165. Params->FindSwitch(L"XmlLog", LogFile) &&
  1166. CheckSafe(Params);
  1167. if (Result)
  1168. {
  1169. Configuration->Usage->Inc(L"ScriptXmlLog");
  1170. Configuration->TemporaryActionsLogging(LogFile);
  1171. }
  1172. return Result;
  1173. }
  1174. //---------------------------------------------------------------------------
  1175. bool __fastcall CheckSafe(TProgramParams * Params)
  1176. {
  1177. // Originally we warned when the test didn't pass,
  1178. // but it would actually be helping hackers, so let's be silent.
  1179. // Earlier we tested presence of any URL on command-line.
  1180. // That was added to prevent abusing URL handler in 3.8.2 (2006).
  1181. // Later in 4.0.4 (2007) an /Unsafe switch was added to URL handler registration.
  1182. // So by now, we can check for the switch only and
  1183. // do not limit user from combining URL with say
  1184. // /rawconfig
  1185. return !Params->FindSwitch(UNSAFE_SWITCH);
  1186. }