UserInterface.cpp 47 KB

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