UserInterface.cpp 48 KB

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