UserInterface.cpp 50 KB

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