UserInterface.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  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(L"ini");
  36. if (!IniFileName.IsEmpty())
  37. {
  38. if (AnsiSameText(IniFileName, L"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. FAIL;
  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. ALWAYS_TRUE(!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. void __fastcall AddMenuSeparator(TTBCustomItem * Menu)
  498. {
  499. TTBXSeparatorItem * Item = new TTBXSeparatorItem(Menu);
  500. Menu->Add(Item);
  501. }
  502. //---------------------------------------------------------------------------
  503. static TComponent * LastPopupComponent = NULL;
  504. static TRect LastPopupRect(-1, -1, -1, -1);
  505. static TDateTime LastCloseUp;
  506. //---------------------------------------------------------------------------
  507. static void __fastcall ConvertMenu(TMenuItem * AItems, TTBCustomItem * Items)
  508. {
  509. for (int Index = 0; Index < AItems->Count; Index++)
  510. {
  511. TMenuItem * AItem = AItems->Items[Index];
  512. TTBCustomItem * Item;
  513. if (!AItem->Enabled && !AItem->Visible && (AItem->Action == NULL) &&
  514. (AItem->OnClick == NULL) && ALWAYS_TRUE(AItem->Count == 0))
  515. {
  516. TTBXLabelItem * LabelItem = new TTBXLabelItem(Items->Owner);
  517. // TTBXLabelItem has it's own Caption
  518. LabelItem->Caption = AItem->Caption;
  519. LabelItem->SectionHeader = true;
  520. Item = LabelItem;
  521. }
  522. else
  523. {
  524. // see TB2DsgnConverter.pas DoConvert
  525. if (AItem->Caption == L"-")
  526. {
  527. Item = new TTBXSeparatorItem(Items->Owner);
  528. }
  529. else
  530. {
  531. if (AItem->Count > 0)
  532. {
  533. Item = new TTBXSubmenuItem(Items->Owner);
  534. }
  535. else
  536. {
  537. Item = new TTBXItem(Items->Owner);
  538. }
  539. Item->Action = AItem->Action;
  540. Item->AutoCheck = AItem->AutoCheck;
  541. Item->Caption = AItem->Caption;
  542. Item->Checked = AItem->Checked;
  543. if (AItem->Default)
  544. {
  545. Item->Options = Item->Options << tboDefault;
  546. }
  547. Item->Enabled = AItem->Enabled;
  548. Item->GroupIndex = AItem->GroupIndex;
  549. Item->HelpContext = AItem->HelpContext;
  550. Item->ImageIndex = AItem->ImageIndex;
  551. Item->RadioItem = AItem->RadioItem;
  552. Item->ShortCut = AItem->ShortCut;
  553. Item->SubMenuImages = AItem->SubMenuImages;
  554. Item->OnClick = AItem->OnClick;
  555. }
  556. Item->Hint = AItem->Hint;
  557. Item->Tag = AItem->Tag;
  558. Item->Visible = AItem->Visible;
  559. // recurse is supported only for empty submenus (as used for custom commands)
  560. if (AItem->Count > 0)
  561. {
  562. ConvertMenu(AItem, Item);
  563. }
  564. }
  565. Items->Add(Item);
  566. }
  567. }
  568. //---------------------------------------------------------------------------
  569. void __fastcall MenuPopup(TPopupMenu * AMenu, TRect Rect,
  570. TComponent * PopupComponent)
  571. {
  572. // Pressing the same button within 200ms after closing its popup menu
  573. // does nothing.
  574. // It is to immitate close-by-click behavior. Note that menu closes itself
  575. // before onclick handler of button occurs.
  576. // To support content menu popups, we have to check for the popup location too,
  577. // to allow poping menu on different location (such as different node of TTreeView),
  578. // even if there's another popup opened already (so that the time interval
  579. // below does not elapse).
  580. TDateTime N = Now();
  581. TDateTime Diff = N - LastCloseUp;
  582. if ((PopupComponent == LastPopupComponent) &&
  583. (Rect == LastPopupRect) &&
  584. (Diff < TDateTime(0, 0, 0, 200)))
  585. {
  586. LastPopupComponent = NULL;
  587. }
  588. else
  589. {
  590. TTBXPopupMenu * Menu = dynamic_cast<TTBXPopupMenu *>(AMenu);
  591. if (Menu == NULL)
  592. {
  593. Menu = CreateTBXPopupMenu(AMenu->Owner);
  594. Menu->OnPopup = AMenu->OnPopup;
  595. Menu->Items->SubMenuImages = AMenu->Images;
  596. ConvertMenu(AMenu->Items, Menu->Items);
  597. }
  598. Menu->PopupComponent = PopupComponent;
  599. Menu->PopupEx(Rect);
  600. LastPopupComponent = PopupComponent;
  601. LastPopupRect = Rect;
  602. LastCloseUp = Now();
  603. }
  604. }
  605. //---------------------------------------------------------------------------
  606. const int ColorCols = 8;
  607. const int StandardColorRows = 2;
  608. const int StandardColorCount = ColorCols * StandardColorRows;
  609. const int UserColorRows = 1;
  610. const int UserColorCount = UserColorRows * ColorCols;
  611. const wchar_t ColorSeparator = L',';
  612. //---------------------------------------------------------------------------
  613. static void __fastcall GetStandardSessionColorInfo(
  614. int Col, int Row, TColor & Color, UnicodeString & Name)
  615. {
  616. #define COLOR_INFO(COL, ROW, NAME, COLOR) \
  617. if ((Col == COL) && (Row == ROW)) { Name = NAME; Color = TColor(COLOR); } else
  618. // bottom row of default TBX color set
  619. COLOR_INFO(0, 0, L"Rose", 0xCC99FF)
  620. COLOR_INFO(1, 0, L"Tan", 0x99CCFF)
  621. COLOR_INFO(2, 0, L"Light Yellow", 0x99FFFF)
  622. COLOR_INFO(3, 0, L"Light Green", 0xCCFFCC)
  623. COLOR_INFO(4, 0, L"Light Turquoise", 0xFFFFCC)
  624. COLOR_INFO(5, 0, L"Pale Blue", 0xFFCC99)
  625. COLOR_INFO(6, 0, L"Lavender", 0xFF99CC)
  626. // second row of Excel 2010 palette with second color (Lighter Black) skipped
  627. COLOR_INFO(7, 0, L"Light Orange", 0xB5D5FB)
  628. COLOR_INFO(0, 1, L"Darker White", 0xD8D8D8)
  629. COLOR_INFO(1, 1, L"Darker Tan", 0x97BDC4)
  630. COLOR_INFO(2, 1, L"Lighter Blue", 0xE2B38D)
  631. COLOR_INFO(3, 1, L"Light Blue", 0xE4CCB8)
  632. COLOR_INFO(4, 1, L"Lighter Red", 0xB7B9E5)
  633. COLOR_INFO(5, 1, L"Light Olive Green", 0xBCE3D7)
  634. COLOR_INFO(6, 1, L"Light Purple", 0xD9C1CC)
  635. COLOR_INFO(7, 1, L"Light Aqua", 0xE8DDB7)
  636. FAIL;
  637. #undef COLOR_INFO
  638. }
  639. //---------------------------------------------------------------------------
  640. static void __fastcall SessionColorSetGetColorInfo(
  641. void * /*Data*/, TTBXCustomColorSet * /*Sender*/, int Col, int Row, TColor & Color, UnicodeString & Name)
  642. {
  643. GetStandardSessionColorInfo(Col, Row, Color, Name);
  644. }
  645. //---------------------------------------------------------------------------
  646. static TColor __fastcall RestoreColor(UnicodeString CStr)
  647. {
  648. return TColor(StrToInt(UnicodeString(L"$") + CStr));
  649. }
  650. //---------------------------------------------------------------------------
  651. static UnicodeString __fastcall StoreColor(TColor Color)
  652. {
  653. return IntToHex(Color, 6);
  654. }
  655. //---------------------------------------------------------------------------
  656. static UnicodeString __fastcall ExtractColorStr(UnicodeString & Colors)
  657. {
  658. return CutToChar(Colors, ColorSeparator, true);
  659. }
  660. //---------------------------------------------------------------------------
  661. static bool __fastcall IsStandardColor(TColor Color)
  662. {
  663. for (int Row = 0; Row < StandardColorRows; Row++)
  664. {
  665. for (int Col = 0; Col < ColorCols; Col++)
  666. {
  667. TColor StandardColor;
  668. UnicodeString Name; // unused
  669. GetStandardSessionColorInfo(Col, Row, StandardColor, Name);
  670. if (StandardColor == Color)
  671. {
  672. return true;
  673. }
  674. }
  675. }
  676. return false;
  677. }
  678. //---------------------------------------------------------------------------
  679. class TColorChangeData : public TComponent
  680. {
  681. public:
  682. __fastcall TColorChangeData(TColorChangeEvent OnColorChange, TColor Color, bool SessionColors);
  683. static TColorChangeData * __fastcall Retrieve(TObject * Object);
  684. void __fastcall ColorChange(TColor Color);
  685. __property TColor Color = { read = FColor };
  686. __property bool SessionColors = { read = FSessionColors };
  687. private:
  688. TColorChangeEvent FOnColorChange;
  689. TColor FColor;
  690. bool FSessionColors;
  691. };
  692. //---------------------------------------------------------------------------
  693. __fastcall TColorChangeData::TColorChangeData(
  694. TColorChangeEvent OnColorChange, TColor Color, bool SessionColors) :
  695. TComponent(NULL)
  696. {
  697. Name = QualifiedClassName();
  698. FOnColorChange = OnColorChange;
  699. FColor = Color;
  700. FSessionColors = SessionColors;
  701. }
  702. //---------------------------------------------------------------------------
  703. TColorChangeData * __fastcall TColorChangeData::Retrieve(TObject * Object)
  704. {
  705. TComponent * Component = NOT_NULL(dynamic_cast<TComponent *>(Object));
  706. TComponent * ColorChangeDataComponent = Component->FindComponent(QualifiedClassName());
  707. return NOT_NULL(dynamic_cast<TColorChangeData *>(ColorChangeDataComponent));
  708. }
  709. //---------------------------------------------------------------------------
  710. void __fastcall TColorChangeData::ColorChange(TColor Color)
  711. {
  712. // Color palette returns clNone when no color is selected,
  713. // though it should not really happen.
  714. // See also CreateColorPalette
  715. if (Color == Vcl::Graphics::clNone)
  716. {
  717. Color = TColor(0);
  718. }
  719. if (SessionColors &&
  720. (Color != TColor(0)) &&
  721. !IsStandardColor(Color))
  722. {
  723. UnicodeString SessionColors = StoreColor(Color);
  724. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  725. while (!Temp.IsEmpty())
  726. {
  727. UnicodeString CStr = ExtractColorStr(Temp);
  728. if (RestoreColor(CStr) != Color)
  729. {
  730. SessionColors += UnicodeString(ColorSeparator) + CStr;
  731. }
  732. }
  733. CustomWinConfiguration->SessionColors = SessionColors;
  734. }
  735. FOnColorChange(Color);
  736. }
  737. //---------------------------------------------------------------------------
  738. static void __fastcall ColorDefaultClick(void * /*Data*/, TObject * Sender)
  739. {
  740. TColorChangeData::Retrieve(Sender)->ColorChange(TColor(0));
  741. }
  742. //---------------------------------------------------------------------------
  743. static void __fastcall ColorPaletteChange(void * /*Data*/, TObject * Sender)
  744. {
  745. TTBXColorPalette * ColorPalette = NOT_NULL(dynamic_cast<TTBXColorPalette *>(Sender));
  746. TColorChangeData::Retrieve(Sender)->ColorChange(GetNonZeroColor(ColorPalette->Color));
  747. }
  748. //---------------------------------------------------------------------------
  749. static UnicodeString __fastcall CustomColorName(int Index)
  750. {
  751. return UnicodeString(L"Color") + wchar_t(L'A' + Index);
  752. }
  753. //---------------------------------------------------------------------------
  754. static void __fastcall ColorPickClick(void * /*Data*/, TObject * Sender)
  755. {
  756. TColorChangeData * ColorChangeData = TColorChangeData::Retrieve(Sender);
  757. std::unique_ptr<TColorDialog> Dialog(new TColorDialog(Application));
  758. Dialog->Options = Dialog->Options << cdFullOpen << cdAnyColor;
  759. Dialog->Color = (ColorChangeData->Color != 0 ? ColorChangeData->Color : clSkyBlue);
  760. if (ColorChangeData->SessionColors)
  761. {
  762. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  763. int StandardColorIndex = 0;
  764. int CustomColors = Min(MaxCustomColors, StandardColorCount);
  765. for (int Index = 0; Index < CustomColors; Index++)
  766. {
  767. TColor CustomColor;
  768. if (!Temp.IsEmpty())
  769. {
  770. CustomColor = RestoreColor(ExtractColorStr(Temp));
  771. }
  772. else
  773. {
  774. UnicodeString Name; // not used
  775. DebugAssert(StandardColorIndex < StandardColorCount);
  776. GetStandardSessionColorInfo(
  777. StandardColorIndex % ColorCols, StandardColorIndex / ColorCols,
  778. CustomColor, Name);
  779. StandardColorIndex++;
  780. }
  781. Dialog->CustomColors->Values[CustomColorName(Index)] = StoreColor(CustomColor);
  782. }
  783. }
  784. if (Dialog->Execute())
  785. {
  786. if (ColorChangeData->SessionColors)
  787. {
  788. // so that we do not have to try to preserve the excess colors
  789. DebugAssert(UserColorCount <= MaxCustomColors);
  790. UnicodeString SessionColors;
  791. for (int Index = 0; Index < MaxCustomColors; Index++)
  792. {
  793. UnicodeString CStr = Dialog->CustomColors->Values[CustomColorName(Index)];
  794. if (!CStr.IsEmpty())
  795. {
  796. TColor CustomColor = RestoreColor(CStr);
  797. if (!IsStandardColor(CustomColor))
  798. {
  799. AddToList(SessionColors, StoreColor(CustomColor), ColorSeparator);
  800. }
  801. }
  802. }
  803. CustomWinConfiguration->SessionColors = SessionColors;
  804. }
  805. // call color change only after copying custom colors back,
  806. // so that it can add selected color to the user list
  807. ColorChangeData->ColorChange(GetNonZeroColor(Dialog->Color));
  808. }
  809. }
  810. //---------------------------------------------------------------------------
  811. TPopupMenu * __fastcall CreateSessionColorPopupMenu(TColor Color,
  812. TColorChangeEvent OnColorChange)
  813. {
  814. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  815. CreateSessionColorMenu(PopupMenu->Items, Color, OnColorChange);
  816. return PopupMenu.release();
  817. }
  818. //---------------------------------------------------------------------------
  819. static void __fastcall UserSessionColorSetGetColorInfo(
  820. void * /*Data*/, TTBXCustomColorSet * Sender, int Col, int Row, TColor & Color, UnicodeString & /*Name*/)
  821. {
  822. int Index = (Row * Sender->ColCount) + Col;
  823. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  824. while ((Index > 0) && !Temp.IsEmpty())
  825. {
  826. ExtractColorStr(Temp);
  827. Index--;
  828. }
  829. if (!Temp.IsEmpty())
  830. {
  831. Color = RestoreColor(ExtractColorStr(Temp));
  832. }
  833. else
  834. {
  835. // hide the trailing cells
  836. Color = Vcl::Graphics::clNone;
  837. }
  838. }
  839. //---------------------------------------------------------------------------
  840. void __fastcall CreateColorPalette(TTBCustomItem * Owner, TColor Color, int Rows,
  841. TCSGetColorInfo OnGetColorInfo, TColorChangeEvent OnColorChange, bool SessionColors)
  842. {
  843. TTBXColorPalette * ColorPalette = new TTBXColorPalette(Owner);
  844. if (OnGetColorInfo != NULL)
  845. {
  846. TTBXCustomColorSet * ColorSet = new TTBXCustomColorSet(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. }
  854. // clNone = no selection, see also ColorChange
  855. ColorPalette->Color = (Color != 0) ? Color : Vcl::Graphics::clNone;
  856. ColorPalette->OnChange = MakeMethod<TNotifyEvent>(NULL, ColorPaletteChange);
  857. ColorPalette->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  858. Owner->Add(ColorPalette);
  859. Owner->Add(new TTBXSeparatorItem(Owner));
  860. }
  861. //---------------------------------------------------------------------------
  862. static void __fastcall CreateColorMenu(TComponent * AOwner, TColor Color,
  863. TColorChangeEvent OnColorChange, bool SessionColors,
  864. const UnicodeString & DefaultColorCaption, const UnicodeString & DefaultColorHint,
  865. const UnicodeString & HelpKeyword,
  866. const UnicodeString & ColorPickHint)
  867. {
  868. TTBCustomItem * Owner = dynamic_cast<TTBCustomItem *>(AOwner);
  869. if (ALWAYS_TRUE(Owner != NULL))
  870. {
  871. Owner->Clear();
  872. TTBCustomItem * Item;
  873. Item = new TTBXItem(Owner);
  874. Item->Caption = DefaultColorCaption;
  875. Item->Hint = DefaultColorHint;
  876. Item->HelpKeyword = HelpKeyword;
  877. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorDefaultClick);
  878. Item->Checked = (Color == TColor(0));
  879. Item->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  880. Owner->Add(Item);
  881. Owner->Add(new TTBXSeparatorItem(Owner));
  882. if (SessionColors)
  883. {
  884. int SessionColorCount = 0;
  885. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  886. while (!Temp.IsEmpty())
  887. {
  888. SessionColorCount++;
  889. ExtractColorStr(Temp);
  890. }
  891. if (SessionColorCount > 0)
  892. {
  893. SessionColorCount = Min(SessionColorCount, UserColorCount);
  894. int RowCount = ((SessionColorCount + ColorCols - 1) / ColorCols);
  895. DebugAssert(RowCount <= UserColorRows);
  896. CreateColorPalette(Owner, Color, RowCount,
  897. MakeMethod<TCSGetColorInfo>(NULL, UserSessionColorSetGetColorInfo),
  898. OnColorChange, SessionColors);
  899. }
  900. CreateColorPalette(Owner, Color, StandardColorRows,
  901. MakeMethod<TCSGetColorInfo>(NULL, SessionColorSetGetColorInfo),
  902. OnColorChange, SessionColors);
  903. }
  904. else
  905. {
  906. CreateColorPalette(Owner, Color, -1, NULL, OnColorChange, SessionColors);
  907. }
  908. Owner->Add(new TTBXSeparatorItem(Owner));
  909. Item = new TTBXItem(Owner);
  910. Item->Caption = LoadStr(COLOR_PICK_CAPTION);
  911. Item->Hint = ColorPickHint;
  912. Item->HelpKeyword = HelpKeyword;
  913. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorPickClick);
  914. Item->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  915. Owner->Add(Item);
  916. }
  917. }
  918. //---------------------------------------------------------------------------
  919. void __fastcall CreateSessionColorMenu(TComponent * AOwner, TColor Color,
  920. TColorChangeEvent OnColorChange)
  921. {
  922. CreateColorMenu(
  923. AOwner, Color, OnColorChange, true,
  924. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), LoadStr(EDITOR_BACKGROUND_COLOR_HINT),
  925. HELP_COLOR, LoadStr(COLOR_PICK_HINT));
  926. }
  927. //---------------------------------------------------------------------------
  928. void __fastcall CreateEditorBackgroundColorMenu(TComponent * AOwner, TColor Color,
  929. TColorChangeEvent OnColorChange)
  930. {
  931. CreateColorMenu(
  932. AOwner, Color, OnColorChange, true,
  933. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), LoadStr(EDITOR_BACKGROUND_COLOR_HINT),
  934. HELP_COLOR, LoadStr(EDITOR_BACKGROUND_COLOR_PICK_HINT));
  935. }
  936. //---------------------------------------------------------------------------
  937. TPopupMenu * __fastcall CreateColorPopupMenu(TColor Color,
  938. TColorChangeEvent OnColorChange)
  939. {
  940. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  941. CreateColorMenu(
  942. PopupMenu->Items, Color, OnColorChange, false,
  943. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), UnicodeString(),
  944. HELP_NONE, UnicodeString());
  945. return PopupMenu.release();
  946. }
  947. //---------------------------------------------------------------------------
  948. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  949. {
  950. // no-op yet
  951. }
  952. //---------------------------------------------------------------------------
  953. struct TThreadParam
  954. {
  955. TThreadFunc ThreadFunc;
  956. void * Parameter;
  957. };
  958. //---------------------------------------------------------------------------
  959. static int __fastcall ThreadProc(void * AParam)
  960. {
  961. TThreadParam * Param = reinterpret_cast<TThreadParam *>(AParam);
  962. unsigned int Result = Param->ThreadFunc(Param->Parameter);
  963. delete Param;
  964. EndThread(Result);
  965. return Result;
  966. }
  967. //---------------------------------------------------------------------------
  968. int __fastcall StartThread(void * SecurityAttributes, unsigned StackSize,
  969. TThreadFunc ThreadFunc, void * Parameter, unsigned CreationFlags,
  970. TThreadID & ThreadId)
  971. {
  972. TThreadParam * Param = new TThreadParam;
  973. Param->ThreadFunc = ThreadFunc;
  974. Param->Parameter = Parameter;
  975. return BeginThread(SecurityAttributes, StackSize, ThreadProc, Param,
  976. CreationFlags, ThreadId);
  977. }
  978. //---------------------------------------------------------------------------
  979. static TShortCut FirstCtrlNumberShortCut = ShortCut(L'0', TShiftState() << ssCtrl);
  980. static TShortCut LastCtrlNumberShortCut = ShortCut(L'9', TShiftState() << ssCtrl);
  981. static TShortCut FirstShiftCtrlAltLetterShortCut = ShortCut(L'A', TShiftState() << ssShift << ssCtrl << ssAlt);
  982. static TShortCut LastShiftCtrlAltLetterShortCut = ShortCut(L'Z', TShiftState() << ssShift << ssCtrl << ssAlt);
  983. //---------------------------------------------------------------------------
  984. void __fastcall InitializeShortCutCombo(TComboBox * ComboBox,
  985. const TShortCuts & ShortCuts)
  986. {
  987. ComboBox->Items->BeginUpdate();
  988. try
  989. {
  990. ComboBox->Items->Clear();
  991. ComboBox->Items->AddObject(LoadStr(SHORTCUT_NONE), reinterpret_cast<TObject* >(0));
  992. for (TShortCut AShortCut = FirstCtrlNumberShortCut; AShortCut <= LastCtrlNumberShortCut; AShortCut++)
  993. {
  994. if (!ShortCuts.Has(AShortCut))
  995. {
  996. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  997. }
  998. }
  999. for (TShortCut AShortCut = FirstShiftCtrlAltLetterShortCut; AShortCut <= LastShiftCtrlAltLetterShortCut; AShortCut++)
  1000. {
  1001. if (!ShortCuts.Has(AShortCut))
  1002. {
  1003. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  1004. }
  1005. }
  1006. }
  1007. __finally
  1008. {
  1009. ComboBox->Items->EndUpdate();
  1010. }
  1011. ComboBox->Style = csDropDownList;
  1012. ComboBox->DropDownCount = Max(ComboBox->DropDownCount, 16);
  1013. }
  1014. //---------------------------------------------------------------------------
  1015. void __fastcall SetShortCutCombo(TComboBox * ComboBox, TShortCut Value)
  1016. {
  1017. for (int Index = ComboBox->Items->Count - 1; Index >= 0; Index--)
  1018. {
  1019. TShortCut AShortCut = TShortCut(ComboBox->Items->Objects[Index]);
  1020. if (AShortCut == Value)
  1021. {
  1022. ComboBox->ItemIndex = Index;
  1023. break;
  1024. }
  1025. else if (AShortCut < Value)
  1026. {
  1027. DebugAssert(Value != 0);
  1028. ComboBox->Items->InsertObject(Index + 1, ShortCutToText(Value),
  1029. reinterpret_cast<TObject* >(Value));
  1030. ComboBox->ItemIndex = Index + 1;
  1031. break;
  1032. }
  1033. DebugAssert(Index > 0);
  1034. }
  1035. }
  1036. //---------------------------------------------------------------------------
  1037. TShortCut __fastcall GetShortCutCombo(TComboBox * ComboBox)
  1038. {
  1039. return TShortCut(ComboBox->Items->Objects[ComboBox->ItemIndex]);
  1040. }
  1041. //---------------------------------------------------------------------------
  1042. bool __fastcall IsCustomShortCut(TShortCut ShortCut)
  1043. {
  1044. return
  1045. ((FirstCtrlNumberShortCut <= ShortCut) && (ShortCut <= LastCtrlNumberShortCut)) ||
  1046. ((FirstShiftCtrlAltLetterShortCut <= ShortCut) && (ShortCut <= LastShiftCtrlAltLetterShortCut));
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. //---------------------------------------------------------------------------
  1050. class TMasterPasswordDialog : public TCustomDialog
  1051. {
  1052. public:
  1053. __fastcall TMasterPasswordDialog(bool Current);
  1054. bool __fastcall Execute(UnicodeString & CurrentPassword, UnicodeString & NewPassword);
  1055. protected:
  1056. virtual void __fastcall DoValidate();
  1057. virtual void __fastcall DoChange(bool & CanSubmit);
  1058. private:
  1059. TPasswordEdit * CurrentEdit;
  1060. TPasswordEdit * NewEdit;
  1061. TPasswordEdit * ConfirmEdit;
  1062. };
  1063. //---------------------------------------------------------------------------
  1064. __fastcall TMasterPasswordDialog::TMasterPasswordDialog(bool Current) :
  1065. TCustomDialog(Current ? HELP_MASTER_PASSWORD_CURRENT : HELP_MASTER_PASSWORD_CHANGE)
  1066. {
  1067. Caption = LoadStr(MASTER_PASSWORD_CAPTION);
  1068. CurrentEdit = new TPasswordEdit(this);
  1069. AddEdit(CurrentEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CURRENT)));
  1070. EnableControl(CurrentEdit, Current || WinConfiguration->UseMasterPassword);
  1071. CurrentEdit->MaxLength = PasswordMaxLength();
  1072. if (!Current)
  1073. {
  1074. NewEdit = new TPasswordEdit(this);
  1075. AddEdit(NewEdit, CreateLabel(LoadStr(MASTER_PASSWORD_NEW)));
  1076. NewEdit->MaxLength = CurrentEdit->MaxLength;
  1077. if (!WinConfiguration->UseMasterPassword)
  1078. {
  1079. ActiveControl = NewEdit;
  1080. }
  1081. ConfirmEdit = new TPasswordEdit(this);
  1082. AddEdit(ConfirmEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CONFIRM)));
  1083. ConfirmEdit->MaxLength = CurrentEdit->MaxLength;
  1084. }
  1085. else
  1086. {
  1087. NewEdit = NULL;
  1088. ConfirmEdit = NULL;
  1089. }
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. bool __fastcall TMasterPasswordDialog::Execute(
  1093. UnicodeString & CurrentPassword, UnicodeString & NewPassword)
  1094. {
  1095. bool Result = TCustomDialog::Execute();
  1096. if (Result)
  1097. {
  1098. if (CurrentEdit->Enabled)
  1099. {
  1100. CurrentPassword = CurrentEdit->Text;
  1101. }
  1102. if (NewEdit != NULL)
  1103. {
  1104. NewPassword = NewEdit->Text;
  1105. }
  1106. }
  1107. return Result;
  1108. }
  1109. //---------------------------------------------------------------------------
  1110. void __fastcall TMasterPasswordDialog::DoChange(bool & CanSubmit)
  1111. {
  1112. CanSubmit =
  1113. (!WinConfiguration->UseMasterPassword || (IsValidPassword(CurrentEdit->Text) >= 0)) &&
  1114. ((NewEdit == NULL) || (IsValidPassword(NewEdit->Text) >= 0)) &&
  1115. ((ConfirmEdit == NULL) || (IsValidPassword(ConfirmEdit->Text) >= 0));
  1116. TCustomDialog::DoChange(CanSubmit);
  1117. }
  1118. //---------------------------------------------------------------------------
  1119. void __fastcall TMasterPasswordDialog::DoValidate()
  1120. {
  1121. TCustomDialog::DoValidate();
  1122. if (WinConfiguration->UseMasterPassword &&
  1123. !WinConfiguration->ValidateMasterPassword(CurrentEdit->Text))
  1124. {
  1125. CurrentEdit->SetFocus();
  1126. CurrentEdit->SelectAll();
  1127. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_INCORRECT)));
  1128. }
  1129. if (NewEdit != NULL)
  1130. {
  1131. if (NewEdit->Text != ConfirmEdit->Text)
  1132. {
  1133. ConfirmEdit->SetFocus();
  1134. ConfirmEdit->SelectAll();
  1135. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_DIFFERENT)));
  1136. }
  1137. int Valid = IsValidPassword(NewEdit->Text);
  1138. if (Valid <= 0)
  1139. {
  1140. DebugAssert(Valid == 0);
  1141. if (MessageDialog(LoadStr(MASTER_PASSWORD_SIMPLE2), qtWarning,
  1142. qaOK | qaCancel, HELP_MASTER_PASSWORD_SIMPLE) == qaCancel)
  1143. {
  1144. NewEdit->SetFocus();
  1145. NewEdit->SelectAll();
  1146. Abort();
  1147. }
  1148. }
  1149. }
  1150. }
  1151. //---------------------------------------------------------------------------
  1152. static bool __fastcall DoMasterPasswordDialog(bool Current,
  1153. UnicodeString & NewPassword)
  1154. {
  1155. bool Result;
  1156. TMasterPasswordDialog * Dialog = new TMasterPasswordDialog(Current);
  1157. try
  1158. {
  1159. UnicodeString CurrentPassword;
  1160. Result = Dialog->Execute(CurrentPassword, NewPassword);
  1161. if (Result)
  1162. {
  1163. if ((Current || WinConfiguration->UseMasterPassword) &&
  1164. ALWAYS_TRUE(!CurrentPassword.IsEmpty()))
  1165. {
  1166. WinConfiguration->SetMasterPassword(CurrentPassword);
  1167. }
  1168. }
  1169. }
  1170. __finally
  1171. {
  1172. delete Dialog;
  1173. }
  1174. return Result;
  1175. }
  1176. //---------------------------------------------------------------------------
  1177. bool __fastcall DoMasterPasswordDialog()
  1178. {
  1179. UnicodeString NewPassword;
  1180. bool Result = DoMasterPasswordDialog(true, NewPassword);
  1181. DebugAssert(NewPassword.IsEmpty());
  1182. return Result;
  1183. }
  1184. //---------------------------------------------------------------------------
  1185. bool __fastcall DoChangeMasterPasswordDialog(UnicodeString & NewPassword)
  1186. {
  1187. bool Result = DoMasterPasswordDialog(false, NewPassword);
  1188. return Result;
  1189. }
  1190. //---------------------------------------------------------------------------
  1191. void __fastcall MessageWithNoHelp(const UnicodeString & Message)
  1192. {
  1193. TMessageParams Params;
  1194. Params.AllowHelp = false; // to avoid recursion
  1195. if (MessageDialog(LoadStr(HELP_SEND_MESSAGE2), qtConfirmation,
  1196. qaOK | qaCancel, HELP_NONE, &Params) == qaOK)
  1197. {
  1198. SearchHelp(Message);
  1199. }
  1200. }
  1201. //---------------------------------------------------------------------------
  1202. bool __fastcall CheckXmlLogParam(TProgramParams * Params)
  1203. {
  1204. UnicodeString LogFile;
  1205. bool Result =
  1206. Params->FindSwitch(L"XmlLog", LogFile) &&
  1207. CheckSafe(Params);
  1208. if (Result)
  1209. {
  1210. Configuration->Usage->Inc(L"ScriptXmlLog");
  1211. Configuration->TemporaryActionsLogging(LogFile);
  1212. }
  1213. return Result;
  1214. }
  1215. //---------------------------------------------------------------------------
  1216. bool __fastcall CheckSafe(TProgramParams * Params)
  1217. {
  1218. // Originally we warned when the test didn't pass,
  1219. // but it would actually be helping hackers, so let's be silent.
  1220. // Earlier we tested presence of any URL on command-line.
  1221. // That was added to prevent abusing URL handler in 3.8.2 (2006).
  1222. // Later in 4.0.4 (2007) an /Unsafe switch was added to URL handler registration.
  1223. // So by now, we can check for the switch only and
  1224. // do not limit user from combining URL with say
  1225. // /rawconfig
  1226. return !Params->FindSwitch(UNSAFE_SWITCH);
  1227. }