UserInterface.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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);
  400. if (!Value.IsEmpty())
  401. {
  402. ToolbarKey += L"_" + Value;
  403. }
  404. }
  405. //---------------------------------------------------------------------------
  406. static int __fastcall ToolbarReadInt(const UnicodeString ToolbarName,
  407. const UnicodeString Value, const int Default, const void * ExtraData)
  408. {
  409. int Result;
  410. if (Value == L"Rev")
  411. {
  412. Result = 2000;
  413. }
  414. else
  415. {
  416. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  417. UnicodeString ToolbarKey;
  418. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  419. if (Storage->IndexOfName(ToolbarKey) >= 0)
  420. {
  421. Result = StrToIntDef(Storage->Values[ToolbarKey], Default);
  422. #if 0
  423. // this does not work well, as it scales down the stretched
  424. // toolbars (path toolbars) too much, it has to be reimplemented smarter
  425. if (Value == L"DockPos")
  426. {
  427. int PixelsPerInch = GetToolbarLayoutPixelsPerInch(Storage);
  428. // When DPI has decreased since the last time, scale down
  429. // toolbar position to get rid of gaps caused by smaller labels.
  430. // Do not do this when DPI has increased as it would introduce gaps,
  431. // as toolbars consists mostly of icons only, that do not scale.
  432. // The toolbars shift themselves anyway, when other toolbars to the left
  433. // get wider. We also risk a bit that toolbar order changes,
  434. // as with very small toolbars (History) we can get scaled down position
  435. // of the following toolbar to the left of it.
  436. // There's special handling (also for scaling-up) stretched toolbars
  437. // in LoadToolbarsLayoutStr
  438. if ((PixelsPerInch > 0) && (Screen->PixelsPerInch < PixelsPerInch))
  439. {
  440. Result = LoadDimension(Result, PixelsPerInch);
  441. }
  442. }
  443. #endif
  444. }
  445. else
  446. {
  447. Result = Default;
  448. }
  449. }
  450. return Result;
  451. }
  452. //---------------------------------------------------------------------------
  453. static UnicodeString __fastcall ToolbarReadString(const UnicodeString ToolbarName,
  454. const UnicodeString Value, const UnicodeString Default, const void * ExtraData)
  455. {
  456. UnicodeString Result;
  457. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  458. UnicodeString ToolbarKey;
  459. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  460. if (Storage->IndexOfName(ToolbarKey) >= 0)
  461. {
  462. Result = Storage->Values[ToolbarKey];
  463. }
  464. else
  465. {
  466. Result = Default;
  467. }
  468. return Result;
  469. }
  470. //---------------------------------------------------------------------------
  471. static void __fastcall ToolbarWriteInt(const UnicodeString ToolbarName,
  472. const UnicodeString Value, const int Data, const void * ExtraData)
  473. {
  474. DebugFail();
  475. if (Value != L"Rev")
  476. {
  477. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  478. UnicodeString ToolbarKey;
  479. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  480. DebugAssert(Storage->IndexOfName(ToolbarKey) < 0);
  481. Storage->Values[ToolbarKey] = IntToStr(Data);
  482. }
  483. }
  484. //---------------------------------------------------------------------------
  485. static void __fastcall ToolbarWriteString(const UnicodeString ToolbarName,
  486. const UnicodeString Value, const UnicodeString Data, const void * ExtraData)
  487. {
  488. DebugAssert(Value.IsEmpty());
  489. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  490. UnicodeString ToolbarKey;
  491. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  492. DebugAssert(Storage->IndexOfName(ToolbarKey) < 0);
  493. Storage->Values[ToolbarKey] = Data;
  494. }
  495. //---------------------------------------------------------------------------
  496. UnicodeString __fastcall GetToolbarsLayoutStr(TControl * OwnerControl)
  497. {
  498. UnicodeString Result;
  499. TStrings * Storage = new TStringList();
  500. try
  501. {
  502. TBCustomSavePositions(OwnerControl, ToolbarWriteInt, ToolbarWriteString,
  503. Storage);
  504. Storage->Values[PixelsPerInchKey] = SavePixelsPerInch(OwnerControl);
  505. Result = Storage->CommaText;
  506. }
  507. __finally
  508. {
  509. delete Storage;
  510. }
  511. return Result;
  512. }
  513. //---------------------------------------------------------------------------
  514. void __fastcall LoadToolbarsLayoutStr(TControl * OwnerControl, UnicodeString LayoutStr)
  515. {
  516. TStrings * Storage = new TStringList();
  517. try
  518. {
  519. Storage->CommaText = LayoutStr;
  520. TBCustomLoadPositions(OwnerControl, ToolbarReadInt, ToolbarReadString,
  521. Storage);
  522. int PixelsPerInch = GetToolbarLayoutPixelsPerInch(Storage, OwnerControl);
  523. // Scale toolbars stretched to the first other toolbar to the right
  524. if ((PixelsPerInch > 0) && (PixelsPerInch != GetControlPixelsPerInch(OwnerControl))) // optimization
  525. {
  526. for (int Index = 0; Index < OwnerControl->ComponentCount; Index++)
  527. {
  528. TTBXToolbar * Toolbar =
  529. dynamic_cast<TTBXToolbar *>(OwnerControl->Components[Index]);
  530. if ((Toolbar != NULL) && Toolbar->Stretch &&
  531. (Toolbar->OnGetBaseSize != NULL) &&
  532. // we do not support floating of stretched toolbars
  533. DebugAlwaysTrue(!Toolbar->Floating))
  534. {
  535. TTBXToolbar * FollowingToolbar = NULL;
  536. for (int Index2 = 0; Index2 < OwnerControl->ComponentCount; Index2++)
  537. {
  538. TTBXToolbar * Toolbar2 =
  539. dynamic_cast<TTBXToolbar *>(OwnerControl->Components[Index2]);
  540. if ((Toolbar2 != NULL) && !Toolbar2->Floating &&
  541. (Toolbar2->Parent == Toolbar->Parent) &&
  542. (Toolbar2->DockRow == Toolbar->DockRow) &&
  543. (Toolbar2->DockPos > Toolbar->DockPos) &&
  544. ((FollowingToolbar == NULL) || (FollowingToolbar->DockPos > Toolbar2->DockPos)))
  545. {
  546. FollowingToolbar = Toolbar2;
  547. }
  548. }
  549. if (FollowingToolbar != NULL)
  550. {
  551. int NewWidth = LoadDimension(Toolbar->Width, PixelsPerInch, Toolbar);
  552. FollowingToolbar->DockPos += NewWidth - Toolbar->Width;
  553. }
  554. }
  555. }
  556. }
  557. }
  558. __finally
  559. {
  560. delete Storage;
  561. }
  562. }
  563. //---------------------------------------------------------------------------
  564. TTBXSeparatorItem * __fastcall AddMenuSeparator(TTBCustomItem * Menu)
  565. {
  566. TTBXSeparatorItem * Item = new TTBXSeparatorItem(Menu);
  567. Menu->Add(Item);
  568. return Item;
  569. }
  570. //---------------------------------------------------------------------------
  571. static TComponent * LastPopupComponent = NULL;
  572. static TRect LastPopupRect(-1, -1, -1, -1);
  573. static TDateTime LastCloseUp;
  574. //---------------------------------------------------------------------------
  575. static void __fastcall ConvertMenu(TMenuItem * AItems, TTBCustomItem * Items)
  576. {
  577. for (int Index = 0; Index < AItems->Count; Index++)
  578. {
  579. TMenuItem * AItem = AItems->Items[Index];
  580. TTBCustomItem * Item;
  581. if (!AItem->Enabled && !AItem->Visible && (AItem->Action == NULL) &&
  582. (AItem->OnClick == NULL) && DebugAlwaysTrue(AItem->Count == 0))
  583. {
  584. TTBXLabelItem * LabelItem = new TTBXLabelItem(Items->Owner);
  585. // TTBXLabelItem has it's own Caption
  586. LabelItem->Caption = AItem->Caption;
  587. LabelItem->SectionHeader = true;
  588. Item = LabelItem;
  589. }
  590. else
  591. {
  592. // see TB2DsgnConverter.pas DoConvert
  593. if (AItem->Caption == L"-")
  594. {
  595. Item = new TTBXSeparatorItem(Items->Owner);
  596. }
  597. else
  598. {
  599. if (AItem->Count > 0)
  600. {
  601. Item = new TTBXSubmenuItem(Items->Owner);
  602. }
  603. else
  604. {
  605. Item = new TTBXItem(Items->Owner);
  606. }
  607. Item->Action = AItem->Action;
  608. Item->AutoCheck = AItem->AutoCheck;
  609. Item->Caption = AItem->Caption;
  610. Item->Checked = AItem->Checked;
  611. if (AItem->Default)
  612. {
  613. Item->Options = Item->Options << tboDefault;
  614. }
  615. Item->Enabled = AItem->Enabled;
  616. Item->GroupIndex = AItem->GroupIndex;
  617. Item->HelpContext = AItem->HelpContext;
  618. Item->ImageIndex = AItem->ImageIndex;
  619. Item->RadioItem = AItem->RadioItem;
  620. Item->ShortCut = AItem->ShortCut;
  621. Item->SubMenuImages = AItem->SubMenuImages;
  622. Item->OnClick = AItem->OnClick;
  623. }
  624. Item->Hint = AItem->Hint;
  625. Item->Tag = AItem->Tag;
  626. Item->Visible = AItem->Visible;
  627. // recurse is supported only for empty submenus (as used for custom commands)
  628. if (AItem->Count > 0)
  629. {
  630. ConvertMenu(AItem, Item);
  631. }
  632. }
  633. Items->Add(Item);
  634. }
  635. }
  636. //---------------------------------------------------------------------------
  637. void __fastcall MenuPopup(TPopupMenu * AMenu, TRect Rect,
  638. TComponent * PopupComponent)
  639. {
  640. // Pressing the same button within 200ms after closing its popup menu
  641. // does nothing.
  642. // It is to immitate close-by-click behavior. Note that menu closes itself
  643. // before onclick handler of button occurs.
  644. // To support content menu popups, we have to check for the popup location too,
  645. // to allow poping menu on different location (such as different node of TTreeView),
  646. // even if there's another popup opened already (so that the time interval
  647. // below does not elapse).
  648. TDateTime N = Now();
  649. TDateTime Diff = N - LastCloseUp;
  650. if ((PopupComponent == LastPopupComponent) &&
  651. (Rect == LastPopupRect) &&
  652. (Diff < TDateTime(0, 0, 0, 200)))
  653. {
  654. LastPopupComponent = NULL;
  655. }
  656. else
  657. {
  658. TTBXPopupMenu * Menu = dynamic_cast<TTBXPopupMenu *>(AMenu);
  659. if (Menu == NULL)
  660. {
  661. Menu = CreateTBXPopupMenu(AMenu->Owner);
  662. Menu->OnPopup = AMenu->OnPopup;
  663. Menu->Items->SubMenuImages = AMenu->Images;
  664. ConvertMenu(AMenu->Items, Menu->Items);
  665. }
  666. Menu->PopupComponent = PopupComponent;
  667. Menu->PopupEx(Rect);
  668. LastPopupComponent = PopupComponent;
  669. LastPopupRect = Rect;
  670. LastCloseUp = Now();
  671. }
  672. }
  673. //---------------------------------------------------------------------------
  674. const int ColorCols = 8;
  675. const int StandardColorRows = 2;
  676. const int StandardColorCount = ColorCols * StandardColorRows;
  677. const int UserColorRows = 1;
  678. const int UserColorCount = UserColorRows * ColorCols;
  679. const wchar_t ColorSeparator = L',';
  680. //---------------------------------------------------------------------------
  681. static void __fastcall GetStandardSessionColorInfo(
  682. int Col, int Row, TColor & Color, UnicodeString & Name)
  683. {
  684. #define COLOR_INFO(COL, ROW, NAME, COLOR) \
  685. if ((Col == COL) && (Row == ROW)) { Name = NAME; Color = TColor(COLOR); } else
  686. // bottom row of default TBX color set
  687. COLOR_INFO(0, 0, L"Rose", 0xCC99FF)
  688. COLOR_INFO(1, 0, L"Tan", 0x99CCFF)
  689. COLOR_INFO(2, 0, L"Light Yellow", 0x99FFFF)
  690. COLOR_INFO(3, 0, L"Light Green", 0xCCFFCC)
  691. COLOR_INFO(4, 0, L"Light Turquoise", 0xFFFFCC)
  692. COLOR_INFO(5, 0, L"Pale Blue", 0xFFCC99)
  693. COLOR_INFO(6, 0, L"Lavender", 0xFF99CC)
  694. // second row of Excel 2010 palette with second color (Lighter Black) skipped
  695. COLOR_INFO(7, 0, L"Light Orange", 0xB5D5FB)
  696. COLOR_INFO(0, 1, L"Darker White", 0xD8D8D8)
  697. COLOR_INFO(1, 1, L"Darker Tan", 0x97BDC4)
  698. COLOR_INFO(2, 1, L"Lighter Blue", 0xE2B38D)
  699. COLOR_INFO(3, 1, L"Light Blue", 0xE4CCB8)
  700. COLOR_INFO(4, 1, L"Lighter Red", 0xB7B9E5)
  701. COLOR_INFO(5, 1, L"Light Olive Green", 0xBCE3D7)
  702. COLOR_INFO(6, 1, L"Light Purple", 0xD9C1CC)
  703. COLOR_INFO(7, 1, L"Light Aqua", 0xE8DDB7)
  704. DebugFail();
  705. #undef COLOR_INFO
  706. }
  707. //---------------------------------------------------------------------------
  708. static void __fastcall SessionColorSetGetColorInfo(
  709. void * /*Data*/, TTBXCustomColorSet * /*Sender*/, int Col, int Row, TColor & Color, UnicodeString & Name)
  710. {
  711. GetStandardSessionColorInfo(Col, Row, Color, Name);
  712. }
  713. //---------------------------------------------------------------------------
  714. static TColor __fastcall RestoreColor(UnicodeString CStr)
  715. {
  716. return TColor(StrToInt(UnicodeString(L"$") + CStr));
  717. }
  718. //---------------------------------------------------------------------------
  719. static UnicodeString __fastcall StoreColor(TColor Color)
  720. {
  721. return IntToHex(Color, 6);
  722. }
  723. //---------------------------------------------------------------------------
  724. static UnicodeString __fastcall ExtractColorStr(UnicodeString & Colors)
  725. {
  726. return CutToChar(Colors, ColorSeparator, true);
  727. }
  728. //---------------------------------------------------------------------------
  729. static bool __fastcall IsStandardColor(TColor Color)
  730. {
  731. for (int Row = 0; Row < StandardColorRows; Row++)
  732. {
  733. for (int Col = 0; Col < ColorCols; Col++)
  734. {
  735. TColor StandardColor;
  736. UnicodeString Name; // unused
  737. GetStandardSessionColorInfo(Col, Row, StandardColor, Name);
  738. if (StandardColor == Color)
  739. {
  740. return true;
  741. }
  742. }
  743. }
  744. return false;
  745. }
  746. //---------------------------------------------------------------------------
  747. class TColorChangeData : public TComponent
  748. {
  749. public:
  750. __fastcall TColorChangeData(TColorChangeEvent OnColorChange, TColor Color, bool SessionColors);
  751. static TColorChangeData * __fastcall Retrieve(TObject * Object);
  752. void __fastcall ColorChange(TColor Color);
  753. __property TColor Color = { read = FColor };
  754. __property bool SessionColors = { read = FSessionColors };
  755. private:
  756. TColorChangeEvent FOnColorChange;
  757. TColor FColor;
  758. bool FSessionColors;
  759. };
  760. //---------------------------------------------------------------------------
  761. __fastcall TColorChangeData::TColorChangeData(
  762. TColorChangeEvent OnColorChange, TColor Color, bool SessionColors) :
  763. TComponent(NULL)
  764. {
  765. Name = QualifiedClassName();
  766. FOnColorChange = OnColorChange;
  767. FColor = Color;
  768. FSessionColors = SessionColors;
  769. }
  770. //---------------------------------------------------------------------------
  771. TColorChangeData * __fastcall TColorChangeData::Retrieve(TObject * Object)
  772. {
  773. TComponent * Component = DebugNotNull(dynamic_cast<TComponent *>(Object));
  774. TComponent * ColorChangeDataComponent = Component->FindComponent(QualifiedClassName());
  775. return DebugNotNull(dynamic_cast<TColorChangeData *>(ColorChangeDataComponent));
  776. }
  777. //---------------------------------------------------------------------------
  778. void __fastcall TColorChangeData::ColorChange(TColor Color)
  779. {
  780. // Color palette returns clNone when no color is selected,
  781. // though it should not really happen.
  782. // See also CreateColorPalette
  783. if (Color == Vcl::Graphics::clNone)
  784. {
  785. Color = TColor(0);
  786. }
  787. if (SessionColors &&
  788. (Color != TColor(0)) &&
  789. !IsStandardColor(Color))
  790. {
  791. UnicodeString SessionColors = StoreColor(Color);
  792. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  793. while (!Temp.IsEmpty())
  794. {
  795. UnicodeString CStr = ExtractColorStr(Temp);
  796. if (RestoreColor(CStr) != Color)
  797. {
  798. SessionColors += UnicodeString(ColorSeparator) + CStr;
  799. }
  800. }
  801. CustomWinConfiguration->SessionColors = SessionColors;
  802. }
  803. FOnColorChange(Color);
  804. }
  805. //---------------------------------------------------------------------------
  806. static void __fastcall ColorDefaultClick(void * /*Data*/, TObject * Sender)
  807. {
  808. TColorChangeData::Retrieve(Sender)->ColorChange(TColor(0));
  809. }
  810. //---------------------------------------------------------------------------
  811. static void __fastcall ColorPaletteChange(void * /*Data*/, TObject * Sender)
  812. {
  813. TTBXColorPalette * ColorPalette = DebugNotNull(dynamic_cast<TTBXColorPalette *>(Sender));
  814. TColorChangeData::Retrieve(Sender)->ColorChange(GetNonZeroColor(ColorPalette->Color));
  815. }
  816. //---------------------------------------------------------------------------
  817. static UnicodeString __fastcall CustomColorName(int Index)
  818. {
  819. return UnicodeString(L"Color") + wchar_t(L'A' + Index);
  820. }
  821. //---------------------------------------------------------------------------
  822. static void __fastcall ColorPickClick(void * /*Data*/, TObject * Sender)
  823. {
  824. TColorChangeData * ColorChangeData = TColorChangeData::Retrieve(Sender);
  825. std::unique_ptr<TColorDialog> Dialog(new TColorDialog(Application));
  826. Dialog->Options = Dialog->Options << cdFullOpen << cdAnyColor;
  827. Dialog->Color = (ColorChangeData->Color != 0 ? ColorChangeData->Color : clSkyBlue);
  828. if (ColorChangeData->SessionColors)
  829. {
  830. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  831. int StandardColorIndex = 0;
  832. int CustomColors = Min(MaxCustomColors, StandardColorCount);
  833. for (int Index = 0; Index < CustomColors; Index++)
  834. {
  835. TColor CustomColor;
  836. if (!Temp.IsEmpty())
  837. {
  838. CustomColor = RestoreColor(ExtractColorStr(Temp));
  839. }
  840. else
  841. {
  842. UnicodeString Name; // not used
  843. DebugAssert(StandardColorIndex < StandardColorCount);
  844. GetStandardSessionColorInfo(
  845. StandardColorIndex % ColorCols, StandardColorIndex / ColorCols,
  846. CustomColor, Name);
  847. StandardColorIndex++;
  848. }
  849. Dialog->CustomColors->Values[CustomColorName(Index)] = StoreColor(CustomColor);
  850. }
  851. }
  852. if (Dialog->Execute())
  853. {
  854. if (ColorChangeData->SessionColors)
  855. {
  856. // so that we do not have to try to preserve the excess colors
  857. DebugAssert(UserColorCount <= MaxCustomColors);
  858. UnicodeString SessionColors;
  859. for (int Index = 0; Index < MaxCustomColors; Index++)
  860. {
  861. UnicodeString CStr = Dialog->CustomColors->Values[CustomColorName(Index)];
  862. if (!CStr.IsEmpty())
  863. {
  864. TColor CustomColor = RestoreColor(CStr);
  865. if (!IsStandardColor(CustomColor))
  866. {
  867. AddToList(SessionColors, StoreColor(CustomColor), ColorSeparator);
  868. }
  869. }
  870. }
  871. CustomWinConfiguration->SessionColors = SessionColors;
  872. }
  873. // call color change only after copying custom colors back,
  874. // so that it can add selected color to the user list
  875. ColorChangeData->ColorChange(GetNonZeroColor(Dialog->Color));
  876. }
  877. }
  878. //---------------------------------------------------------------------------
  879. TPopupMenu * __fastcall CreateSessionColorPopupMenu(TColor Color,
  880. TColorChangeEvent OnColorChange)
  881. {
  882. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  883. CreateSessionColorMenu(PopupMenu->Items, Color, OnColorChange);
  884. return PopupMenu.release();
  885. }
  886. //---------------------------------------------------------------------------
  887. static void __fastcall UserSessionColorSetGetColorInfo(
  888. void * /*Data*/, TTBXCustomColorSet * Sender, int Col, int Row, TColor & Color, UnicodeString & /*Name*/)
  889. {
  890. int Index = (Row * Sender->ColCount) + Col;
  891. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  892. while ((Index > 0) && !Temp.IsEmpty())
  893. {
  894. ExtractColorStr(Temp);
  895. Index--;
  896. }
  897. if (!Temp.IsEmpty())
  898. {
  899. Color = RestoreColor(ExtractColorStr(Temp));
  900. }
  901. else
  902. {
  903. // hide the trailing cells
  904. Color = Vcl::Graphics::clNone;
  905. }
  906. }
  907. //---------------------------------------------------------------------------
  908. void __fastcall CreateColorPalette(TTBCustomItem * Owner, TColor Color, int Rows,
  909. TCSGetColorInfo OnGetColorInfo, TColorChangeEvent OnColorChange, bool SessionColors)
  910. {
  911. TTBXColorPalette * ColorPalette = new TTBXColorPalette(Owner);
  912. if (OnGetColorInfo != NULL)
  913. {
  914. TTBXCustomColorSet * ColorSet = new TTBXCustomColorSet(Owner);
  915. ColorPalette->InsertComponent(ColorSet);
  916. ColorPalette->ColorSet = ColorSet;
  917. // has to be set only after it's assigned to color palette
  918. ColorSet->ColCount = ColorCols;
  919. ColorSet->RowCount = Rows;
  920. ColorSet->OnGetColorInfo = OnGetColorInfo;
  921. }
  922. // clNone = no selection, see also ColorChange
  923. ColorPalette->Color = (Color != 0) ? Color : Vcl::Graphics::clNone;
  924. ColorPalette->OnChange = MakeMethod<TNotifyEvent>(NULL, ColorPaletteChange);
  925. ColorPalette->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  926. Owner->Add(ColorPalette);
  927. Owner->Add(new TTBXSeparatorItem(Owner));
  928. }
  929. //---------------------------------------------------------------------------
  930. static void __fastcall CreateColorMenu(TComponent * AOwner, TColor Color,
  931. TColorChangeEvent OnColorChange, bool SessionColors,
  932. const UnicodeString & DefaultColorCaption, const UnicodeString & DefaultColorHint,
  933. const UnicodeString & HelpKeyword,
  934. const UnicodeString & ColorPickHint)
  935. {
  936. TTBCustomItem * Owner = dynamic_cast<TTBCustomItem *>(AOwner);
  937. if (DebugAlwaysTrue(Owner != NULL))
  938. {
  939. Owner->Clear();
  940. TTBCustomItem * Item;
  941. Item = new TTBXItem(Owner);
  942. Item->Caption = DefaultColorCaption;
  943. Item->Hint = DefaultColorHint;
  944. Item->HelpKeyword = HelpKeyword;
  945. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorDefaultClick);
  946. Item->Checked = (Color == TColor(0));
  947. Item->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  948. Owner->Add(Item);
  949. Owner->Add(new TTBXSeparatorItem(Owner));
  950. if (SessionColors)
  951. {
  952. int SessionColorCount = 0;
  953. UnicodeString Temp = CustomWinConfiguration->SessionColors;
  954. while (!Temp.IsEmpty())
  955. {
  956. SessionColorCount++;
  957. ExtractColorStr(Temp);
  958. }
  959. if (SessionColorCount > 0)
  960. {
  961. SessionColorCount = Min(SessionColorCount, UserColorCount);
  962. int RowCount = ((SessionColorCount + ColorCols - 1) / ColorCols);
  963. DebugAssert(RowCount <= UserColorRows);
  964. CreateColorPalette(Owner, Color, RowCount,
  965. MakeMethod<TCSGetColorInfo>(NULL, UserSessionColorSetGetColorInfo),
  966. OnColorChange, SessionColors);
  967. }
  968. CreateColorPalette(Owner, Color, StandardColorRows,
  969. MakeMethod<TCSGetColorInfo>(NULL, SessionColorSetGetColorInfo),
  970. OnColorChange, SessionColors);
  971. }
  972. else
  973. {
  974. CreateColorPalette(Owner, Color, -1, NULL, OnColorChange, SessionColors);
  975. }
  976. Owner->Add(new TTBXSeparatorItem(Owner));
  977. Item = new TTBXItem(Owner);
  978. Item->Caption = LoadStr(COLOR_PICK_CAPTION);
  979. Item->Hint = ColorPickHint;
  980. Item->HelpKeyword = HelpKeyword;
  981. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorPickClick);
  982. Item->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  983. Owner->Add(Item);
  984. }
  985. }
  986. //---------------------------------------------------------------------------
  987. void __fastcall CreateSessionColorMenu(TComponent * AOwner, TColor Color,
  988. TColorChangeEvent OnColorChange)
  989. {
  990. CreateColorMenu(
  991. AOwner, Color, OnColorChange, true,
  992. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), LoadStr(EDITOR_BACKGROUND_COLOR_HINT),
  993. HELP_COLOR, LoadStr(COLOR_PICK_HINT));
  994. }
  995. //---------------------------------------------------------------------------
  996. void __fastcall CreateEditorBackgroundColorMenu(TComponent * AOwner, TColor Color,
  997. TColorChangeEvent OnColorChange)
  998. {
  999. CreateColorMenu(
  1000. AOwner, Color, OnColorChange, true,
  1001. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), LoadStr(EDITOR_BACKGROUND_COLOR_HINT),
  1002. HELP_COLOR, LoadStr(EDITOR_BACKGROUND_COLOR_PICK_HINT));
  1003. }
  1004. //---------------------------------------------------------------------------
  1005. TPopupMenu * __fastcall CreateColorPopupMenu(TColor Color,
  1006. TColorChangeEvent OnColorChange)
  1007. {
  1008. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  1009. CreateColorMenu(
  1010. PopupMenu->Items, Color, OnColorChange, false,
  1011. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), UnicodeString(),
  1012. HELP_NONE, UnicodeString());
  1013. return PopupMenu.release();
  1014. }
  1015. //---------------------------------------------------------------------------
  1016. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  1017. {
  1018. // no-op yet
  1019. }
  1020. //---------------------------------------------------------------------------
  1021. struct TThreadParam
  1022. {
  1023. TThreadFunc ThreadFunc;
  1024. void * Parameter;
  1025. };
  1026. //---------------------------------------------------------------------------
  1027. static int __fastcall ThreadProc(void * AParam)
  1028. {
  1029. TThreadParam * Param = reinterpret_cast<TThreadParam *>(AParam);
  1030. unsigned int Result = Param->ThreadFunc(Param->Parameter);
  1031. delete Param;
  1032. EndThread(Result);
  1033. return Result;
  1034. }
  1035. //---------------------------------------------------------------------------
  1036. int __fastcall StartThread(void * SecurityAttributes, unsigned StackSize,
  1037. TThreadFunc ThreadFunc, void * Parameter, unsigned CreationFlags,
  1038. TThreadID & ThreadId)
  1039. {
  1040. TThreadParam * Param = new TThreadParam;
  1041. Param->ThreadFunc = ThreadFunc;
  1042. Param->Parameter = Parameter;
  1043. return BeginThread(SecurityAttributes, StackSize, ThreadProc, Param,
  1044. CreationFlags, ThreadId);
  1045. }
  1046. //---------------------------------------------------------------------------
  1047. static TShortCut FirstCtrlNumberShortCut = ShortCut(L'0', TShiftState() << ssCtrl);
  1048. static TShortCut LastCtrlNumberShortCut = ShortCut(L'9', TShiftState() << ssCtrl);
  1049. static TShortCut FirstCtrlKeyPadShortCut = ShortCut(VK_NUMPAD0, TShiftState() << ssCtrl);
  1050. static TShortCut LastCtrlKeyPadShortCut = ShortCut(VK_NUMPAD9, TShiftState() << ssCtrl);
  1051. static TShortCut FirstShiftCtrlAltLetterShortCut = ShortCut(L'A', TShiftState() << ssShift << ssCtrl << ssAlt);
  1052. static TShortCut LastShiftCtrlAltLetterShortCut = ShortCut(L'Z', TShiftState() << ssShift << ssCtrl << ssAlt);
  1053. //---------------------------------------------------------------------------
  1054. void __fastcall InitializeShortCutCombo(TComboBox * ComboBox,
  1055. const TShortCuts & ShortCuts)
  1056. {
  1057. ComboBox->Items->BeginUpdate();
  1058. try
  1059. {
  1060. ComboBox->Items->Clear();
  1061. ComboBox->Items->AddObject(LoadStr(SHORTCUT_NONE), reinterpret_cast<TObject* >(0));
  1062. for (TShortCut AShortCut = FirstCtrlNumberShortCut; AShortCut <= LastCtrlNumberShortCut; AShortCut++)
  1063. {
  1064. if (!ShortCuts.Has(AShortCut))
  1065. {
  1066. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  1067. }
  1068. }
  1069. for (TShortCut AShortCut = FirstShiftCtrlAltLetterShortCut; AShortCut <= LastShiftCtrlAltLetterShortCut; AShortCut++)
  1070. {
  1071. if (!ShortCuts.Has(AShortCut))
  1072. {
  1073. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  1074. }
  1075. }
  1076. }
  1077. __finally
  1078. {
  1079. ComboBox->Items->EndUpdate();
  1080. }
  1081. ComboBox->Style = csDropDownList;
  1082. ComboBox->DropDownCount = Max(ComboBox->DropDownCount, 16);
  1083. }
  1084. //---------------------------------------------------------------------------
  1085. void __fastcall SetShortCutCombo(TComboBox * ComboBox, TShortCut Value)
  1086. {
  1087. for (int Index = ComboBox->Items->Count - 1; Index >= 0; Index--)
  1088. {
  1089. TShortCut AShortCut = TShortCut(ComboBox->Items->Objects[Index]);
  1090. if (AShortCut == Value)
  1091. {
  1092. ComboBox->ItemIndex = Index;
  1093. break;
  1094. }
  1095. else if (AShortCut < Value)
  1096. {
  1097. DebugAssert(Value != 0);
  1098. ComboBox->Items->InsertObject(Index + 1, ShortCutToText(Value),
  1099. reinterpret_cast<TObject* >(Value));
  1100. ComboBox->ItemIndex = Index + 1;
  1101. break;
  1102. }
  1103. DebugAssert(Index > 0);
  1104. }
  1105. }
  1106. //---------------------------------------------------------------------------
  1107. TShortCut __fastcall GetShortCutCombo(TComboBox * ComboBox)
  1108. {
  1109. return TShortCut(ComboBox->Items->Objects[ComboBox->ItemIndex]);
  1110. }
  1111. //---------------------------------------------------------------------------
  1112. TShortCut __fastcall NormalizeCustomShortCut(TShortCut ShortCut)
  1113. {
  1114. if ((FirstCtrlKeyPadShortCut <= ShortCut) && (ShortCut <= LastCtrlKeyPadShortCut))
  1115. {
  1116. ShortCut = FirstCtrlNumberShortCut + (ShortCut - FirstCtrlKeyPadShortCut);
  1117. }
  1118. return ShortCut;
  1119. }
  1120. //---------------------------------------------------------------------------
  1121. bool __fastcall IsCustomShortCut(TShortCut ShortCut)
  1122. {
  1123. return
  1124. ((FirstCtrlNumberShortCut <= ShortCut) && (ShortCut <= LastCtrlNumberShortCut)) ||
  1125. ((FirstShiftCtrlAltLetterShortCut <= ShortCut) && (ShortCut <= LastShiftCtrlAltLetterShortCut));
  1126. }
  1127. //---------------------------------------------------------------------------
  1128. //---------------------------------------------------------------------------
  1129. class TMasterPasswordDialog : public TCustomDialog
  1130. {
  1131. public:
  1132. __fastcall TMasterPasswordDialog(bool Current);
  1133. bool __fastcall Execute(UnicodeString & CurrentPassword, UnicodeString & NewPassword);
  1134. protected:
  1135. virtual void __fastcall DoValidate();
  1136. virtual void __fastcall DoChange(bool & CanSubmit);
  1137. private:
  1138. TPasswordEdit * CurrentEdit;
  1139. TPasswordEdit * NewEdit;
  1140. TPasswordEdit * ConfirmEdit;
  1141. };
  1142. //---------------------------------------------------------------------------
  1143. __fastcall TMasterPasswordDialog::TMasterPasswordDialog(bool Current) :
  1144. TCustomDialog(Current ? HELP_MASTER_PASSWORD_CURRENT : HELP_MASTER_PASSWORD_CHANGE)
  1145. {
  1146. Caption = LoadStr(MASTER_PASSWORD_CAPTION);
  1147. CurrentEdit = new TPasswordEdit(this);
  1148. AddEdit(CurrentEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CURRENT)));
  1149. EnableControl(CurrentEdit, Current || WinConfiguration->UseMasterPassword);
  1150. CurrentEdit->MaxLength = PasswordMaxLength();
  1151. if (!Current)
  1152. {
  1153. NewEdit = new TPasswordEdit(this);
  1154. AddEdit(NewEdit, CreateLabel(LoadStr(MASTER_PASSWORD_NEW)));
  1155. NewEdit->MaxLength = CurrentEdit->MaxLength;
  1156. if (!WinConfiguration->UseMasterPassword)
  1157. {
  1158. ActiveControl = NewEdit;
  1159. }
  1160. ConfirmEdit = new TPasswordEdit(this);
  1161. AddEdit(ConfirmEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CONFIRM)));
  1162. ConfirmEdit->MaxLength = CurrentEdit->MaxLength;
  1163. }
  1164. else
  1165. {
  1166. NewEdit = NULL;
  1167. ConfirmEdit = NULL;
  1168. }
  1169. }
  1170. //---------------------------------------------------------------------------
  1171. bool __fastcall TMasterPasswordDialog::Execute(
  1172. UnicodeString & CurrentPassword, UnicodeString & NewPassword)
  1173. {
  1174. bool Result = TCustomDialog::Execute();
  1175. if (Result)
  1176. {
  1177. if (CurrentEdit->Enabled)
  1178. {
  1179. CurrentPassword = CurrentEdit->Text;
  1180. }
  1181. if (NewEdit != NULL)
  1182. {
  1183. NewPassword = NewEdit->Text;
  1184. }
  1185. }
  1186. return Result;
  1187. }
  1188. //---------------------------------------------------------------------------
  1189. void __fastcall TMasterPasswordDialog::DoChange(bool & CanSubmit)
  1190. {
  1191. CanSubmit =
  1192. (!WinConfiguration->UseMasterPassword || (IsValidPassword(CurrentEdit->Text) >= 0)) &&
  1193. ((NewEdit == NULL) || (IsValidPassword(NewEdit->Text) >= 0)) &&
  1194. ((ConfirmEdit == NULL) || (IsValidPassword(ConfirmEdit->Text) >= 0));
  1195. TCustomDialog::DoChange(CanSubmit);
  1196. }
  1197. //---------------------------------------------------------------------------
  1198. void __fastcall TMasterPasswordDialog::DoValidate()
  1199. {
  1200. TCustomDialog::DoValidate();
  1201. if (WinConfiguration->UseMasterPassword &&
  1202. !WinConfiguration->ValidateMasterPassword(CurrentEdit->Text))
  1203. {
  1204. CurrentEdit->SetFocus();
  1205. CurrentEdit->SelectAll();
  1206. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_INCORRECT)));
  1207. }
  1208. if (NewEdit != NULL)
  1209. {
  1210. if (NewEdit->Text != ConfirmEdit->Text)
  1211. {
  1212. ConfirmEdit->SetFocus();
  1213. ConfirmEdit->SelectAll();
  1214. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_DIFFERENT)));
  1215. }
  1216. int Valid = IsValidPassword(NewEdit->Text);
  1217. if (Valid <= 0)
  1218. {
  1219. DebugAssert(Valid == 0);
  1220. if (MessageDialog(LoadStr(MASTER_PASSWORD_SIMPLE2), qtWarning,
  1221. qaOK | qaCancel, HELP_MASTER_PASSWORD_SIMPLE) == qaCancel)
  1222. {
  1223. NewEdit->SetFocus();
  1224. NewEdit->SelectAll();
  1225. Abort();
  1226. }
  1227. }
  1228. }
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. static bool __fastcall DoMasterPasswordDialog(bool Current,
  1232. UnicodeString & NewPassword)
  1233. {
  1234. bool Result;
  1235. TMasterPasswordDialog * Dialog = new TMasterPasswordDialog(Current);
  1236. try
  1237. {
  1238. UnicodeString CurrentPassword;
  1239. Result = Dialog->Execute(CurrentPassword, NewPassword);
  1240. if (Result)
  1241. {
  1242. if ((Current || WinConfiguration->UseMasterPassword) &&
  1243. DebugAlwaysTrue(!CurrentPassword.IsEmpty()))
  1244. {
  1245. WinConfiguration->SetMasterPassword(CurrentPassword);
  1246. }
  1247. }
  1248. }
  1249. __finally
  1250. {
  1251. delete Dialog;
  1252. }
  1253. return Result;
  1254. }
  1255. //---------------------------------------------------------------------------
  1256. bool __fastcall DoMasterPasswordDialog()
  1257. {
  1258. UnicodeString NewPassword;
  1259. bool Result = DoMasterPasswordDialog(true, NewPassword);
  1260. DebugAssert(NewPassword.IsEmpty());
  1261. return Result;
  1262. }
  1263. //---------------------------------------------------------------------------
  1264. bool __fastcall DoChangeMasterPasswordDialog(UnicodeString & NewPassword)
  1265. {
  1266. bool Result = DoMasterPasswordDialog(false, NewPassword);
  1267. return Result;
  1268. }
  1269. //---------------------------------------------------------------------------
  1270. void __fastcall MessageWithNoHelp(const UnicodeString & Message)
  1271. {
  1272. TMessageParams Params;
  1273. Params.AllowHelp = false; // to avoid recursion
  1274. if (MessageDialog(LoadStr(HELP_SEND_MESSAGE2), qtConfirmation,
  1275. qaOK | qaCancel, HELP_NONE, &Params) == qaOK)
  1276. {
  1277. SearchHelp(Message);
  1278. }
  1279. }
  1280. //---------------------------------------------------------------------------
  1281. void __fastcall CheckLogParam(TProgramParams * Params)
  1282. {
  1283. UnicodeString LogFile;
  1284. if (Params->FindSwitch(LOG_SWITCH, LogFile) && CheckSafe(Params))
  1285. {
  1286. Configuration->Usage->Inc(L"ScriptLog");
  1287. Configuration->TemporaryLogging(LogFile);
  1288. }
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. bool __fastcall CheckXmlLogParam(TProgramParams * Params)
  1292. {
  1293. UnicodeString LogFile;
  1294. bool Result =
  1295. Params->FindSwitch(L"XmlLog", LogFile) &&
  1296. CheckSafe(Params);
  1297. if (Result)
  1298. {
  1299. Configuration->Usage->Inc(L"ScriptXmlLog");
  1300. Configuration->TemporaryActionsLogging(LogFile);
  1301. if (Params->FindSwitch(L"XmlLogRequired"))
  1302. {
  1303. Configuration->LogActionsRequired = true;
  1304. }
  1305. }
  1306. return Result;
  1307. }
  1308. //---------------------------------------------------------------------------
  1309. bool __fastcall CheckSafe(TProgramParams * Params)
  1310. {
  1311. // Originally we warned when the test didn't pass,
  1312. // but it would actually be helping hackers, so let's be silent.
  1313. // Earlier we tested presence of any URL on command-line.
  1314. // That was added to prevent abusing URL handler in 3.8.2 (2006).
  1315. // Later in 4.0.4 (2007) an /Unsafe switch was added to URL handler registration.
  1316. // So by now, we can check for the switch only and
  1317. // do not limit user from combining URL with say
  1318. // /rawconfig
  1319. return !Params->FindSwitch(UNSAFE_SWITCH);
  1320. }