UserInterface.cpp 49 KB

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