UserInterface.cpp 46 KB

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