UserInterface.cpp 48 KB

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