UserInterface.cpp 49 KB

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