UserInterface.cpp 45 KB

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