UserInterface.cpp 47 KB

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