UserInterface.cpp 50 KB

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