UserInterface.cpp 44 KB

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