UserInterface.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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(TCustomForm * /*Control*/)
  114. {
  115. // noop
  116. }
  117. //---------------------------------------------------------------------------
  118. void __fastcall ShowExtendedException(Exception * E)
  119. {
  120. ShowExtendedExceptionEx(NULL, E);
  121. }
  122. //---------------------------------------------------------------------------
  123. void __fastcall TerminateApplication()
  124. {
  125. Application->Terminate();
  126. }
  127. //---------------------------------------------------------------------------
  128. struct TOpenLocalPathHandler
  129. {
  130. UnicodeString LocalPath;
  131. UnicodeString LocalFileName;
  132. void __fastcall Open(TObject * Sender, unsigned int & /*Answer*/)
  133. {
  134. TButton * Button = DebugNotNull(dynamic_cast<TButton *>(Sender));
  135. // Reason for separate AMenu variable is given in TPreferencesDialog::EditorFontColorButtonClick
  136. TPopupMenu * AMenu = new TPopupMenu(Application);
  137. // Popup menu has to survive the popup as TBX calls click handler asynchronously (post).
  138. Menu.reset(AMenu);
  139. TMenuItem * Item;
  140. Item = new TMenuItem(Menu.get());
  141. Menu->Items->Add(Item);
  142. Item->Caption = LoadStr(OPEN_TARGET_FOLDER);
  143. Item->OnClick = OpenFolderClick;
  144. if (!LocalFileName.IsEmpty())
  145. {
  146. Item = new TMenuItem(Menu.get());
  147. Menu->Items->Add(Item);
  148. Item->Caption = LoadStr(OPEN_DOWNLOADED_FILE);
  149. Item->OnClick = OpenFileClick;
  150. }
  151. MenuPopup(Menu.get(), Button);
  152. }
  153. private:
  154. std::unique_ptr<TPopupMenu> Menu;
  155. void __fastcall OpenFolderClick(TObject * /*Sender*/)
  156. {
  157. if (LocalFileName.IsEmpty())
  158. {
  159. OpenFolderInExplorer(LocalPath);
  160. }
  161. else
  162. {
  163. OpenFileInExplorer(LocalFileName);
  164. }
  165. }
  166. void __fastcall OpenFileClick(TObject * /*Sender*/)
  167. {
  168. ExecuteShellChecked(LocalFileName, L"");
  169. }
  170. };
  171. //---------------------------------------------------------------------------
  172. void __fastcall ShowExtendedExceptionEx(TTerminal * Terminal,
  173. Exception * E)
  174. {
  175. bool Show = ShouldDisplayException(E);
  176. bool DoNotDisplay = false;
  177. try
  178. {
  179. // This is special case used particularly when called from .NET assembly
  180. // (which always uses /nointeractiveinput),
  181. // but can be useful for other console runs too
  182. TProgramParams * Params = TProgramParams::Instance();
  183. if (Params->FindSwitch(NOINTERACTIVEINPUT_SWITCH))
  184. {
  185. DoNotDisplay = true;
  186. if (Show && CheckXmlLogParam(Params))
  187. {
  188. // The Started argument won't be used with .NET assembly, as it never uses patterns in XML log file name.
  189. // But it theoretically can be used, when started manually.
  190. std::unique_ptr<TActionLog> ActionLog(new TActionLog(Now(), Configuration));
  191. ActionLog->AddFailure(E);
  192. // unnecessary explicit release
  193. ActionLog.reset(NULL);
  194. }
  195. }
  196. }
  197. catch (Exception & E)
  198. {
  199. // swallow
  200. }
  201. TTerminalManager * Manager = TTerminalManager::Instance(false);
  202. bool HookedDialog = false;
  203. try
  204. {
  205. if (!DoNotDisplay)
  206. {
  207. ESshTerminate * Terminate = dynamic_cast<ESshTerminate*>(E);
  208. bool CloseOnCompletion = (Terminate != NULL);
  209. bool ForActiveTerminal =
  210. E->InheritsFrom(__classid(EFatal)) && (Terminal != NULL) &&
  211. (Manager != NULL) && (Manager->ActiveTerminal == Terminal);
  212. unsigned int Result;
  213. if (CloseOnCompletion)
  214. {
  215. if (ForActiveTerminal)
  216. {
  217. DebugAssert(!Terminal->Active);
  218. Manager->DisconnectActiveTerminal();
  219. }
  220. if (Terminate->Operation == odoSuspend)
  221. {
  222. // suspend, so that exit prompt is shown only after windows resume
  223. SuspendWindows();
  224. }
  225. DebugAssert(Show);
  226. bool ConfirmExitOnCompletion =
  227. CloseOnCompletion &&
  228. ((Terminate->Operation == odoDisconnect) || (Terminate->Operation == odoSuspend)) &&
  229. WinConfiguration->ConfirmExitOnCompletion;
  230. if (ConfirmExitOnCompletion)
  231. {
  232. TMessageParams Params(mpNeverAskAgainCheck);
  233. unsigned int Answers = 0;
  234. TQueryButtonAlias Aliases[1];
  235. TOpenLocalPathHandler OpenLocalPathHandler;
  236. if (!Terminate->TargetLocalPath.IsEmpty() && !ForActiveTerminal)
  237. {
  238. OpenLocalPathHandler.LocalPath = Terminate->TargetLocalPath;
  239. OpenLocalPathHandler.LocalFileName = Terminate->DestLocalFileName;
  240. Aliases[0].Button = qaIgnore;
  241. Aliases[0].Alias = LoadStr(OPEN_BUTTON);
  242. Aliases[0].OnSubmit = OpenLocalPathHandler.Open;
  243. Aliases[0].MenuButton = true;
  244. Answers |= Aliases[0].Button;
  245. Params.Aliases = Aliases;
  246. Params.AliasesCount = LENOF(Aliases);
  247. }
  248. if (ForActiveTerminal)
  249. {
  250. UnicodeString MessageFormat =
  251. (Manager->Count > 1) ?
  252. FMTLOAD(DISCONNECT_ON_COMPLETION, (Manager->Count - 1)) :
  253. LoadStr(EXIT_ON_COMPLETION);
  254. // Remove the leading "%s\n\n" (not to change the translation originals - previously the error message was prepended)
  255. MessageFormat = FORMAT(MessageFormat, (UnicodeString())).Trim();
  256. MessageFormat = MainInstructions(MessageFormat) + L"\n\n%s";
  257. Result = FatalExceptionMessageDialog(E, qtInformation,
  258. MessageFormat,
  259. Answers | qaYes | qaNo, HELP_NONE, &Params);
  260. }
  261. else
  262. {
  263. Result =
  264. ExceptionMessageDialog(E, qtInformation, L"", Answers | qaOK, HELP_NONE, &Params);
  265. }
  266. }
  267. else
  268. {
  269. Result = qaYes;
  270. }
  271. }
  272. else
  273. {
  274. if (Show)
  275. {
  276. if (ForActiveTerminal)
  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. else
  292. {
  293. Result = ExceptionMessageDialog(E, qtError);
  294. }
  295. }
  296. else
  297. {
  298. Result = qaOK;
  299. }
  300. }
  301. if (Result == qaNeverAskAgain)
  302. {
  303. DebugAssert(CloseOnCompletion);
  304. Result = qaYes;
  305. WinConfiguration->ConfirmExitOnCompletion = false;
  306. }
  307. if (Result == qaYes)
  308. {
  309. DebugAssert(CloseOnCompletion);
  310. DebugAssert(Terminate != NULL);
  311. DebugAssert(Terminate->Operation != odoIdle);
  312. TerminateApplication();
  313. switch (Terminate->Operation)
  314. {
  315. case odoDisconnect:
  316. break;
  317. case odoSuspend:
  318. // suspended before already
  319. break;
  320. case odoShutDown:
  321. ShutDownWindows();
  322. break;
  323. default:
  324. DebugFail();
  325. }
  326. }
  327. else if (Result == qaRetry)
  328. {
  329. // qaRetry is used by FatalExceptionMessageDialog
  330. if (DebugAlwaysTrue(ForActiveTerminal))
  331. {
  332. Manager->ReconnectActiveTerminal();
  333. }
  334. }
  335. else
  336. {
  337. if (ForActiveTerminal)
  338. {
  339. Manager->DisconnectActiveTerminalIfPermanentFreeOtherwise();
  340. }
  341. }
  342. }
  343. }
  344. __finally
  345. {
  346. if (HookedDialog)
  347. {
  348. Manager->UnhookFatalExceptionMessageDialog();
  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 it's 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. // recurse is supported only for empty submenus (as used for custom commands)
  647. if (AItem->Count > 0)
  648. {
  649. ConvertMenu(AItem, Item);
  650. }
  651. }
  652. Items->Add(Item);
  653. }
  654. }
  655. //---------------------------------------------------------------------------
  656. void __fastcall MenuPopup(TPopupMenu * AMenu, TRect Rect,
  657. TComponent * PopupComponent)
  658. {
  659. // Pressing the same button within 200ms after closing its popup menu
  660. // does nothing.
  661. // It is to immitate close-by-click behavior. Note that menu closes itself
  662. // before onclick handler of button occurs.
  663. // To support content menu popups, we have to check for the popup location too,
  664. // to allow poping menu on different location (such as different node of TTreeView),
  665. // even if there's another popup opened already (so that the time interval
  666. // below does not elapse).
  667. TDateTime N = Now();
  668. TDateTime Diff = N - LastCloseUp;
  669. if ((PopupComponent == LastPopupComponent) &&
  670. (Rect == LastPopupRect) &&
  671. (Diff < TDateTime(0, 0, 0, 200)))
  672. {
  673. LastPopupComponent = NULL;
  674. }
  675. else
  676. {
  677. TTBXPopupMenu * Menu = dynamic_cast<TTBXPopupMenu *>(AMenu);
  678. if (Menu == NULL)
  679. {
  680. Menu = CreateTBXPopupMenu(AMenu->Owner);
  681. Menu->OnPopup = AMenu->OnPopup;
  682. Menu->Items->SubMenuImages = AMenu->Images;
  683. ConvertMenu(AMenu->Items, Menu->Items);
  684. }
  685. Menu->PopupComponent = PopupComponent;
  686. Menu->PopupEx(Rect);
  687. LastPopupComponent = PopupComponent;
  688. LastPopupRect = Rect;
  689. LastCloseUp = Now();
  690. }
  691. }
  692. //---------------------------------------------------------------------------
  693. const int ColorCols = 8;
  694. const int StandardColorRows = 2;
  695. const int StandardColorCount = ColorCols * StandardColorRows;
  696. const int UserColorRows = 1;
  697. const int UserColorCount = UserColorRows * ColorCols;
  698. const wchar_t ColorSeparator = L',';
  699. //---------------------------------------------------------------------------
  700. static void __fastcall GetStandardSessionColorInfo(
  701. int Col, int Row, TColor & Color, UnicodeString & Name)
  702. {
  703. #define COLOR_INFO(COL, ROW, NAME, COLOR) \
  704. if ((Col == COL) && (Row == ROW)) { Name = NAME; Color = TColor(COLOR); } else
  705. // bottom row of default TBX color set
  706. COLOR_INFO(0, 0, L"Rose", 0xCC99FF)
  707. COLOR_INFO(1, 0, L"Tan", 0x99CCFF)
  708. COLOR_INFO(2, 0, L"Light Yellow", 0x99FFFF)
  709. COLOR_INFO(3, 0, L"Light Green", 0xCCFFCC)
  710. COLOR_INFO(4, 0, L"Light Turquoise", 0xFFFFCC)
  711. COLOR_INFO(5, 0, L"Pale Blue", 0xFFCC99)
  712. COLOR_INFO(6, 0, L"Lavender", 0xFF99CC)
  713. // second row of Excel 2010 palette with second color (Lighter Black) skipped
  714. COLOR_INFO(7, 0, L"Light Orange", 0xB5D5FB)
  715. COLOR_INFO(0, 1, L"Darker White", 0xD8D8D8)
  716. COLOR_INFO(1, 1, L"Darker Tan", 0x97BDC4)
  717. COLOR_INFO(2, 1, L"Lighter Blue", 0xE2B38D)
  718. COLOR_INFO(3, 1, L"Light Blue", 0xE4CCB8)
  719. COLOR_INFO(4, 1, L"Lighter Red", 0xB7B9E5)
  720. COLOR_INFO(5, 1, L"Light Olive Green", 0xBCE3D7)
  721. COLOR_INFO(6, 1, L"Light Purple", 0xD9C1CC)
  722. COLOR_INFO(7, 1, L"Light Aqua", 0xE8DDB7)
  723. DebugFail();
  724. #undef COLOR_INFO
  725. }
  726. //---------------------------------------------------------------------------
  727. static void __fastcall SessionColorSetGetColorInfo(
  728. void * /*Data*/, TTBXCustomColorSet * /*Sender*/, int Col, int Row, TColor & Color, UnicodeString & Name)
  729. {
  730. GetStandardSessionColorInfo(Col, Row, Color, Name);
  731. }
  732. //---------------------------------------------------------------------------
  733. TColor __fastcall RestoreColor(const UnicodeString & CStr)
  734. {
  735. return TColor(StrToInt(UnicodeString(L"$") + CStr));
  736. }
  737. //---------------------------------------------------------------------------
  738. UnicodeString __fastcall StoreColor(TColor Color)
  739. {
  740. return IntToHex(Color, 6);
  741. }
  742. //---------------------------------------------------------------------------
  743. static UnicodeString __fastcall ExtractColorStr(UnicodeString & Colors)
  744. {
  745. return CutToChar(Colors, ColorSeparator, true);
  746. }
  747. //---------------------------------------------------------------------------
  748. static bool __fastcall IsStandardColor(bool SessionColors, TColor Color)
  749. {
  750. if (SessionColors)
  751. {
  752. for (int Row = 0; Row < StandardColorRows; Row++)
  753. {
  754. for (int Col = 0; Col < ColorCols; Col++)
  755. {
  756. TColor StandardColor;
  757. UnicodeString Name; // unused
  758. GetStandardSessionColorInfo(Col, Row, StandardColor, Name);
  759. if (StandardColor == Color)
  760. {
  761. return true;
  762. }
  763. }
  764. }
  765. return false;
  766. }
  767. else
  768. {
  769. std::unique_ptr<TTBXColorPalette> DefaultColorPalette(new TTBXColorPalette(NULL));
  770. return (DefaultColorPalette->FindCell(Color).X >= 0);
  771. }
  772. }
  773. //---------------------------------------------------------------------------
  774. class TColorChangeData : public TComponent
  775. {
  776. public:
  777. __fastcall TColorChangeData(TColorChangeEvent OnColorChange, TColor Color, bool SessionColors);
  778. static TColorChangeData * __fastcall Retrieve(TObject * Object);
  779. void __fastcall ColorChange(TColor Color);
  780. __property TColor Color = { read = FColor };
  781. __property bool SessionColors = { read = FSessionColors };
  782. private:
  783. TColorChangeEvent FOnColorChange;
  784. TColor FColor;
  785. bool FSessionColors;
  786. };
  787. //---------------------------------------------------------------------------
  788. __fastcall TColorChangeData::TColorChangeData(
  789. TColorChangeEvent OnColorChange, TColor Color, bool SessionColors) :
  790. TComponent(NULL)
  791. {
  792. Name = QualifiedClassName();
  793. FOnColorChange = OnColorChange;
  794. FColor = Color;
  795. FSessionColors = SessionColors;
  796. }
  797. //---------------------------------------------------------------------------
  798. TColorChangeData * __fastcall TColorChangeData::Retrieve(TObject * Object)
  799. {
  800. TComponent * Component = DebugNotNull(dynamic_cast<TComponent *>(Object));
  801. TComponent * ColorChangeDataComponent = Component->FindComponent(QualifiedClassName());
  802. return DebugNotNull(dynamic_cast<TColorChangeData *>(ColorChangeDataComponent));
  803. }
  804. //---------------------------------------------------------------------------
  805. static void SaveCustomColors(bool SessionColors, const UnicodeString & Colors)
  806. {
  807. if (SessionColors)
  808. {
  809. CustomWinConfiguration->SessionColors = Colors;
  810. }
  811. else
  812. {
  813. CustomWinConfiguration->FontColors = Colors;
  814. }
  815. }
  816. //---------------------------------------------------------------------------
  817. static UnicodeString LoadCustomColors(bool SessionColors)
  818. {
  819. return SessionColors ? CustomWinConfiguration->SessionColors : CustomWinConfiguration->FontColors;
  820. }
  821. //---------------------------------------------------------------------------
  822. void __fastcall TColorChangeData::ColorChange(TColor Color)
  823. {
  824. // Color palette returns clNone when no color is selected,
  825. // though it should not really happen.
  826. // See also CreateColorPalette
  827. if (Color == Vcl::Graphics::clNone)
  828. {
  829. Color = TColor(0);
  830. }
  831. if ((Color != TColor(0)) &&
  832. !IsStandardColor(SessionColors, Color))
  833. {
  834. UnicodeString Colors = StoreColor(Color);
  835. UnicodeString Temp = LoadCustomColors(SessionColors);
  836. while (!Temp.IsEmpty())
  837. {
  838. UnicodeString CStr = ExtractColorStr(Temp);
  839. if (RestoreColor(CStr) != Color)
  840. {
  841. Colors += UnicodeString(ColorSeparator) + CStr;
  842. }
  843. }
  844. SaveCustomColors(SessionColors, Colors);
  845. }
  846. FOnColorChange(Color);
  847. }
  848. //---------------------------------------------------------------------------
  849. static void __fastcall ColorDefaultClick(void * /*Data*/, TObject * Sender)
  850. {
  851. TColorChangeData::Retrieve(Sender)->ColorChange(TColor(0));
  852. }
  853. //---------------------------------------------------------------------------
  854. static void __fastcall ColorPaletteChange(void * /*Data*/, TObject * Sender)
  855. {
  856. TTBXColorPalette * ColorPalette = DebugNotNull(dynamic_cast<TTBXColorPalette *>(Sender));
  857. TColorChangeData::Retrieve(Sender)->ColorChange(GetNonZeroColor(ColorPalette->Color));
  858. }
  859. //---------------------------------------------------------------------------
  860. static UnicodeString __fastcall CustomColorName(int Index)
  861. {
  862. return UnicodeString(L"Color") + wchar_t(L'A' + Index);
  863. }
  864. //---------------------------------------------------------------------------
  865. static void __fastcall ColorPickClick(void * /*Data*/, TObject * Sender)
  866. {
  867. TColorChangeData * ColorChangeData = TColorChangeData::Retrieve(Sender);
  868. std::unique_ptr<TColorDialog> Dialog(new TColorDialog(Application));
  869. Dialog->Options = Dialog->Options << cdFullOpen << cdAnyColor;
  870. Dialog->Color = (ColorChangeData->Color != 0 ? ColorChangeData->Color : clSkyBlue);
  871. UnicodeString Temp = LoadCustomColors(ColorChangeData->SessionColors);
  872. int StandardColorIndex = 0;
  873. for (int Index = 0; Index < MaxCustomColors; Index++)
  874. {
  875. TColor CustomColor;
  876. if (!Temp.IsEmpty())
  877. {
  878. CustomColor = RestoreColor(ExtractColorStr(Temp));
  879. }
  880. else
  881. {
  882. if (ColorChangeData->SessionColors)
  883. {
  884. if (StandardColorIndex < StandardColorCount)
  885. {
  886. UnicodeString Name; // not used
  887. GetStandardSessionColorInfo(
  888. StandardColorIndex % ColorCols, StandardColorIndex / ColorCols,
  889. CustomColor, Name);
  890. StandardColorIndex++;
  891. }
  892. else
  893. {
  894. break;
  895. }
  896. }
  897. else
  898. {
  899. // no standard font colors
  900. break;
  901. }
  902. }
  903. Dialog->CustomColors->Values[CustomColorName(Index)] = StoreColor(CustomColor);
  904. }
  905. if (Dialog->Execute())
  906. {
  907. // so that we do not have to try to preserve the excess colors
  908. DebugAssert(UserColorCount <= MaxCustomColors);
  909. UnicodeString Colors;
  910. for (int Index = 0; Index < MaxCustomColors; Index++)
  911. {
  912. UnicodeString CStr = Dialog->CustomColors->Values[CustomColorName(Index)];
  913. if (!CStr.IsEmpty())
  914. {
  915. TColor CustomColor = RestoreColor(CStr);
  916. if ((CustomColor != static_cast<TColor>(-1)) &&
  917. !IsStandardColor(ColorChangeData->SessionColors, CustomColor))
  918. {
  919. AddToList(Colors, StoreColor(CustomColor), ColorSeparator);
  920. }
  921. }
  922. }
  923. SaveCustomColors(ColorChangeData->SessionColors, Colors);
  924. // call color change only after copying custom colors back,
  925. // so that it can add selected color to the user list
  926. ColorChangeData->ColorChange(GetNonZeroColor(Dialog->Color));
  927. }
  928. }
  929. //---------------------------------------------------------------------------
  930. TPopupMenu * __fastcall CreateSessionColorPopupMenu(TColor Color,
  931. TColorChangeEvent OnColorChange)
  932. {
  933. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  934. CreateSessionColorMenu(PopupMenu->Items, Color, OnColorChange);
  935. return PopupMenu.release();
  936. }
  937. //---------------------------------------------------------------------------
  938. static void __fastcall UserCustomColorSetGetColorInfo(
  939. void * /*Data*/, TTBXCustomColorSet * Sender, int Col, int Row, TColor & Color, UnicodeString & /*Name*/)
  940. {
  941. int Index = (Row * Sender->ColCount) + Col;
  942. bool SessionColors = static_cast<bool>(Sender->Tag);
  943. UnicodeString Temp = LoadCustomColors(SessionColors);
  944. while ((Index > 0) && !Temp.IsEmpty())
  945. {
  946. ExtractColorStr(Temp);
  947. Index--;
  948. }
  949. if (!Temp.IsEmpty())
  950. {
  951. Color = RestoreColor(ExtractColorStr(Temp));
  952. }
  953. else
  954. {
  955. // hide the trailing cells
  956. Color = Vcl::Graphics::clNone;
  957. }
  958. }
  959. //---------------------------------------------------------------------------
  960. void __fastcall CreateColorPalette(TTBCustomItem * Owner, TColor Color, int Rows,
  961. TCSGetColorInfo OnGetColorInfo, TColorChangeEvent OnColorChange, bool SessionColors)
  962. {
  963. TTBXColorPalette * ColorPalette = new TTBXColorPalette(Owner);
  964. if (OnGetColorInfo != NULL)
  965. {
  966. TTBXCustomColorSet * ColorSet = new TTBXCustomColorSet(Owner);
  967. ColorPalette->InsertComponent(ColorSet);
  968. ColorPalette->ColorSet = ColorSet;
  969. // has to be set only after it's assigned to color palette
  970. ColorSet->ColCount = ColorCols;
  971. ColorSet->RowCount = Rows;
  972. ColorSet->OnGetColorInfo = OnGetColorInfo;
  973. ColorSet->Tag = static_cast<int>(SessionColors);
  974. }
  975. // clNone = no selection, see also ColorChange
  976. ColorPalette->Color = (Color != 0) ? Color : Vcl::Graphics::clNone;
  977. ColorPalette->OnChange = MakeMethod<TNotifyEvent>(NULL, ColorPaletteChange);
  978. ColorPalette->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  979. Owner->Add(ColorPalette);
  980. Owner->Add(new TTBXSeparatorItem(Owner));
  981. }
  982. //---------------------------------------------------------------------------
  983. static void __fastcall CreateColorMenu(TComponent * AOwner, TColor Color,
  984. TColorChangeEvent OnColorChange, bool SessionColors,
  985. const UnicodeString & DefaultColorCaption, const UnicodeString & DefaultColorHint,
  986. const UnicodeString & HelpKeyword,
  987. const UnicodeString & ColorPickHint)
  988. {
  989. TTBCustomItem * Owner = dynamic_cast<TTBCustomItem *>(AOwner);
  990. if (DebugAlwaysTrue(Owner != NULL))
  991. {
  992. Owner->Clear();
  993. TTBCustomItem * Item;
  994. Item = new TTBXItem(Owner);
  995. Item->Caption = DefaultColorCaption;
  996. Item->Hint = DefaultColorHint;
  997. Item->HelpKeyword = HelpKeyword;
  998. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorDefaultClick);
  999. Item->Checked = (Color == TColor(0));
  1000. Item->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  1001. Owner->Add(Item);
  1002. Owner->Add(new TTBXSeparatorItem(Owner));
  1003. int CustomColorCount = 0;
  1004. UnicodeString Temp = LoadCustomColors(SessionColors);
  1005. while (!Temp.IsEmpty())
  1006. {
  1007. CustomColorCount++;
  1008. ExtractColorStr(Temp);
  1009. }
  1010. if (CustomColorCount > 0)
  1011. {
  1012. CustomColorCount = Min(CustomColorCount, UserColorCount);
  1013. int RowCount = ((CustomColorCount + ColorCols - 1) / ColorCols);
  1014. DebugAssert(RowCount <= UserColorRows);
  1015. CreateColorPalette(Owner, Color, RowCount,
  1016. MakeMethod<TCSGetColorInfo>(NULL, UserCustomColorSetGetColorInfo),
  1017. OnColorChange, SessionColors);
  1018. }
  1019. if (SessionColors)
  1020. {
  1021. CreateColorPalette(Owner, Color, StandardColorRows,
  1022. MakeMethod<TCSGetColorInfo>(NULL, SessionColorSetGetColorInfo),
  1023. OnColorChange, SessionColors);
  1024. }
  1025. else
  1026. {
  1027. CreateColorPalette(Owner, Color, -1, NULL, OnColorChange, SessionColors);
  1028. }
  1029. Owner->Add(new TTBXSeparatorItem(Owner));
  1030. Item = new TTBXItem(Owner);
  1031. Item->Caption = LoadStr(COLOR_PICK_CAPTION);
  1032. Item->Hint = ColorPickHint;
  1033. Item->HelpKeyword = HelpKeyword;
  1034. Item->OnClick = MakeMethod<TNotifyEvent>(NULL, ColorPickClick);
  1035. Item->InsertComponent(new TColorChangeData(OnColorChange, Color, SessionColors));
  1036. Owner->Add(Item);
  1037. }
  1038. }
  1039. //---------------------------------------------------------------------------
  1040. void __fastcall CreateSessionColorMenu(TComponent * AOwner, TColor Color,
  1041. TColorChangeEvent OnColorChange)
  1042. {
  1043. CreateColorMenu(
  1044. AOwner, Color, OnColorChange, true,
  1045. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), LoadStr(COLOR_DEFAULT_HINT),
  1046. HELP_COLOR, LoadStr(COLOR_PICK_HINT));
  1047. }
  1048. //---------------------------------------------------------------------------
  1049. void __fastcall CreateEditorBackgroundColorMenu(TComponent * AOwner, TColor Color,
  1050. TColorChangeEvent OnColorChange)
  1051. {
  1052. CreateColorMenu(
  1053. AOwner, Color, OnColorChange, true,
  1054. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), LoadStr(EDITOR_BACKGROUND_COLOR_HINT),
  1055. HELP_COLOR, LoadStr(EDITOR_BACKGROUND_COLOR_PICK_HINT));
  1056. }
  1057. //---------------------------------------------------------------------------
  1058. TPopupMenu * __fastcall CreateColorPopupMenu(TColor Color,
  1059. TColorChangeEvent OnColorChange)
  1060. {
  1061. std::unique_ptr<TTBXPopupMenu> PopupMenu(new TTBXPopupMenu(Application));
  1062. CreateColorMenu(
  1063. PopupMenu->Items, Color, OnColorChange, false,
  1064. LoadStr(COLOR_TRUE_DEFAULT_CAPTION), UnicodeString(),
  1065. HELP_NONE, UnicodeString());
  1066. return PopupMenu.release();
  1067. }
  1068. //---------------------------------------------------------------------------
  1069. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  1070. {
  1071. // no-op yet
  1072. }
  1073. //---------------------------------------------------------------------------
  1074. struct TThreadParam
  1075. {
  1076. TThreadFunc ThreadFunc;
  1077. void * Parameter;
  1078. };
  1079. //---------------------------------------------------------------------------
  1080. static int __fastcall ThreadProc(void * AParam)
  1081. {
  1082. TThreadParam * Param = reinterpret_cast<TThreadParam *>(AParam);
  1083. unsigned int Result = Param->ThreadFunc(Param->Parameter);
  1084. delete Param;
  1085. EndThread(Result);
  1086. return Result;
  1087. }
  1088. //---------------------------------------------------------------------------
  1089. int __fastcall StartThread(void * SecurityAttributes, unsigned StackSize,
  1090. TThreadFunc ThreadFunc, void * Parameter, unsigned CreationFlags,
  1091. TThreadID & ThreadId)
  1092. {
  1093. TThreadParam * Param = new TThreadParam;
  1094. Param->ThreadFunc = ThreadFunc;
  1095. Param->Parameter = Parameter;
  1096. return BeginThread(SecurityAttributes, StackSize, ThreadProc, Param,
  1097. CreationFlags, ThreadId);
  1098. }
  1099. //---------------------------------------------------------------------------
  1100. static TShortCut FirstCtrlNumberShortCut = ShortCut(L'0', TShiftState() << ssCtrl);
  1101. static TShortCut LastCtrlNumberShortCut = ShortCut(L'9', TShiftState() << ssCtrl);
  1102. static TShortCut FirstCtrlKeyPadShortCut = ShortCut(VK_NUMPAD0, TShiftState() << ssCtrl);
  1103. static TShortCut LastCtrlKeyPadShortCut = ShortCut(VK_NUMPAD9, TShiftState() << ssCtrl);
  1104. static TShortCut FirstShiftCtrlAltLetterShortCut = ShortCut(L'A', TShiftState() << ssShift << ssCtrl << ssAlt);
  1105. static TShortCut LastShiftCtrlAltLetterShortCut = ShortCut(L'Z', TShiftState() << ssShift << ssCtrl << ssAlt);
  1106. //---------------------------------------------------------------------------
  1107. void __fastcall InitializeShortCutCombo(TComboBox * ComboBox,
  1108. const TShortCuts & ShortCuts)
  1109. {
  1110. ComboBox->Items->BeginUpdate();
  1111. try
  1112. {
  1113. ComboBox->Items->Clear();
  1114. ComboBox->Items->AddObject(LoadStr(SHORTCUT_NONE), reinterpret_cast<TObject* >(0));
  1115. for (TShortCut AShortCut = FirstCtrlNumberShortCut; AShortCut <= LastCtrlNumberShortCut; AShortCut++)
  1116. {
  1117. if (!ShortCuts.Has(AShortCut))
  1118. {
  1119. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  1120. }
  1121. }
  1122. for (TShortCut AShortCut = FirstShiftCtrlAltLetterShortCut; AShortCut <= LastShiftCtrlAltLetterShortCut; AShortCut++)
  1123. {
  1124. if (!ShortCuts.Has(AShortCut))
  1125. {
  1126. ComboBox->Items->AddObject(ShortCutToText(AShortCut), reinterpret_cast<TObject* >(AShortCut));
  1127. }
  1128. }
  1129. }
  1130. __finally
  1131. {
  1132. ComboBox->Items->EndUpdate();
  1133. }
  1134. ComboBox->Style = csDropDownList;
  1135. ComboBox->DropDownCount = Max(ComboBox->DropDownCount, 16);
  1136. }
  1137. //---------------------------------------------------------------------------
  1138. void __fastcall SetShortCutCombo(TComboBox * ComboBox, TShortCut Value)
  1139. {
  1140. for (int Index = ComboBox->Items->Count - 1; Index >= 0; Index--)
  1141. {
  1142. TShortCut AShortCut = TShortCut(ComboBox->Items->Objects[Index]);
  1143. if (AShortCut == Value)
  1144. {
  1145. ComboBox->ItemIndex = Index;
  1146. break;
  1147. }
  1148. else if (AShortCut < Value)
  1149. {
  1150. DebugAssert(Value != 0);
  1151. ComboBox->Items->InsertObject(Index + 1, ShortCutToText(Value),
  1152. reinterpret_cast<TObject* >(Value));
  1153. ComboBox->ItemIndex = Index + 1;
  1154. break;
  1155. }
  1156. DebugAssert(Index > 0);
  1157. }
  1158. }
  1159. //---------------------------------------------------------------------------
  1160. TShortCut __fastcall GetShortCutCombo(TComboBox * ComboBox)
  1161. {
  1162. return TShortCut(ComboBox->Items->Objects[ComboBox->ItemIndex]);
  1163. }
  1164. //---------------------------------------------------------------------------
  1165. TShortCut __fastcall NormalizeCustomShortCut(TShortCut ShortCut)
  1166. {
  1167. if ((FirstCtrlKeyPadShortCut <= ShortCut) && (ShortCut <= LastCtrlKeyPadShortCut))
  1168. {
  1169. ShortCut = FirstCtrlNumberShortCut + (ShortCut - FirstCtrlKeyPadShortCut);
  1170. }
  1171. return ShortCut;
  1172. }
  1173. //---------------------------------------------------------------------------
  1174. bool __fastcall IsCustomShortCut(TShortCut ShortCut)
  1175. {
  1176. return
  1177. ((FirstCtrlNumberShortCut <= ShortCut) && (ShortCut <= LastCtrlNumberShortCut)) ||
  1178. ((FirstShiftCtrlAltLetterShortCut <= ShortCut) && (ShortCut <= LastShiftCtrlAltLetterShortCut));
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. //---------------------------------------------------------------------------
  1182. class TMasterPasswordDialog : public TCustomDialog
  1183. {
  1184. public:
  1185. __fastcall TMasterPasswordDialog(TComponent * AOwner);
  1186. void Init(bool Current);
  1187. bool __fastcall Execute(UnicodeString & CurrentPassword, UnicodeString & NewPassword);
  1188. protected:
  1189. virtual void __fastcall DoValidate();
  1190. virtual void __fastcall DoChange(bool & CanSubmit);
  1191. private:
  1192. TPasswordEdit * CurrentEdit;
  1193. TPasswordEdit * NewEdit;
  1194. TPasswordEdit * ConfirmEdit;
  1195. };
  1196. //---------------------------------------------------------------------------
  1197. // Need to have an Owner argument for SafeFormCreate
  1198. __fastcall TMasterPasswordDialog::TMasterPasswordDialog(TComponent *) :
  1199. TCustomDialog(EmptyStr)
  1200. {
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. void TMasterPasswordDialog::Init(bool Current)
  1204. {
  1205. HelpKeyword = Current ? HELP_MASTER_PASSWORD_CURRENT : HELP_MASTER_PASSWORD_CHANGE;
  1206. Caption = LoadStr(MASTER_PASSWORD_CAPTION);
  1207. CurrentEdit = new TPasswordEdit(this);
  1208. AddEdit(CurrentEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CURRENT)));
  1209. EnableControl(CurrentEdit, Current || WinConfiguration->UseMasterPassword);
  1210. CurrentEdit->MaxLength = PasswordMaxLength();
  1211. if (!Current)
  1212. {
  1213. NewEdit = new TPasswordEdit(this);
  1214. AddEdit(NewEdit, CreateLabel(LoadStr(MASTER_PASSWORD_NEW)));
  1215. NewEdit->MaxLength = CurrentEdit->MaxLength;
  1216. if (!WinConfiguration->UseMasterPassword)
  1217. {
  1218. ActiveControl = NewEdit;
  1219. }
  1220. ConfirmEdit = new TPasswordEdit(this);
  1221. AddEdit(ConfirmEdit, CreateLabel(LoadStr(MASTER_PASSWORD_CONFIRM)));
  1222. ConfirmEdit->MaxLength = CurrentEdit->MaxLength;
  1223. }
  1224. else
  1225. {
  1226. NewEdit = NULL;
  1227. ConfirmEdit = NULL;
  1228. }
  1229. }
  1230. //---------------------------------------------------------------------------
  1231. bool __fastcall TMasterPasswordDialog::Execute(
  1232. UnicodeString & CurrentPassword, UnicodeString & NewPassword)
  1233. {
  1234. bool Result = TCustomDialog::Execute();
  1235. if (Result)
  1236. {
  1237. if (CurrentEdit->Enabled)
  1238. {
  1239. CurrentPassword = CurrentEdit->Text;
  1240. }
  1241. if (NewEdit != NULL)
  1242. {
  1243. NewPassword = NewEdit->Text;
  1244. }
  1245. }
  1246. return Result;
  1247. }
  1248. //---------------------------------------------------------------------------
  1249. void __fastcall TMasterPasswordDialog::DoChange(bool & CanSubmit)
  1250. {
  1251. CanSubmit =
  1252. (!WinConfiguration->UseMasterPassword || (IsValidPassword(CurrentEdit->Text) >= 0)) &&
  1253. ((NewEdit == NULL) || (IsValidPassword(NewEdit->Text) >= 0)) &&
  1254. ((ConfirmEdit == NULL) || (IsValidPassword(ConfirmEdit->Text) >= 0));
  1255. TCustomDialog::DoChange(CanSubmit);
  1256. }
  1257. //---------------------------------------------------------------------------
  1258. void __fastcall TMasterPasswordDialog::DoValidate()
  1259. {
  1260. TCustomDialog::DoValidate();
  1261. if (WinConfiguration->UseMasterPassword &&
  1262. !WinConfiguration->ValidateMasterPassword(CurrentEdit->Text))
  1263. {
  1264. CurrentEdit->SetFocus();
  1265. CurrentEdit->SelectAll();
  1266. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_INCORRECT)));
  1267. }
  1268. if (NewEdit != NULL)
  1269. {
  1270. if (NewEdit->Text != ConfirmEdit->Text)
  1271. {
  1272. ConfirmEdit->SetFocus();
  1273. ConfirmEdit->SelectAll();
  1274. throw Exception(MainInstructions(LoadStr(MASTER_PASSWORD_DIFFERENT)));
  1275. }
  1276. int Valid = IsValidPassword(NewEdit->Text);
  1277. if (Valid <= 0)
  1278. {
  1279. DebugAssert(Valid == 0);
  1280. if (MessageDialog(LoadStr(MASTER_PASSWORD_SIMPLE2), qtWarning,
  1281. qaOK | qaCancel, HELP_MASTER_PASSWORD_SIMPLE) == qaCancel)
  1282. {
  1283. NewEdit->SetFocus();
  1284. NewEdit->SelectAll();
  1285. Abort();
  1286. }
  1287. }
  1288. }
  1289. }
  1290. //---------------------------------------------------------------------------
  1291. static bool __fastcall DoMasterPasswordDialog(bool Current,
  1292. UnicodeString & NewPassword)
  1293. {
  1294. bool Result;
  1295. // This can be a standalone dialog when opening session from commandline
  1296. TMasterPasswordDialog * Dialog = SafeFormCreate<TMasterPasswordDialog>();
  1297. try
  1298. {
  1299. Dialog->Init(Current);
  1300. UnicodeString CurrentPassword;
  1301. Result = Dialog->Execute(CurrentPassword, NewPassword);
  1302. if (Result)
  1303. {
  1304. if ((Current || WinConfiguration->UseMasterPassword) &&
  1305. DebugAlwaysTrue(!CurrentPassword.IsEmpty()))
  1306. {
  1307. WinConfiguration->SetMasterPassword(CurrentPassword);
  1308. }
  1309. }
  1310. }
  1311. __finally
  1312. {
  1313. delete Dialog;
  1314. }
  1315. return Result;
  1316. }
  1317. //---------------------------------------------------------------------------
  1318. bool __fastcall DoMasterPasswordDialog()
  1319. {
  1320. UnicodeString NewPassword;
  1321. bool Result = DoMasterPasswordDialog(true, NewPassword);
  1322. DebugAssert(NewPassword.IsEmpty());
  1323. return Result;
  1324. }
  1325. //---------------------------------------------------------------------------
  1326. bool __fastcall DoChangeMasterPasswordDialog(UnicodeString & NewPassword)
  1327. {
  1328. bool Result = DoMasterPasswordDialog(false, NewPassword);
  1329. return Result;
  1330. }
  1331. //---------------------------------------------------------------------------
  1332. void __fastcall MessageWithNoHelp(const UnicodeString & Message)
  1333. {
  1334. TMessageParams Params;
  1335. Params.AllowHelp = false; // to avoid recursion
  1336. if (MessageDialog(LoadStr(HELP_SEND_MESSAGE2), qtConfirmation,
  1337. qaOK | qaCancel, HELP_NONE, &Params) == qaOK)
  1338. {
  1339. SearchHelp(Message);
  1340. }
  1341. }
  1342. //---------------------------------------------------------------------------
  1343. void __fastcall CheckLogParam(TProgramParams * Params)
  1344. {
  1345. UnicodeString LogFile;
  1346. if (Params->FindSwitch(LOG_SWITCH, LogFile) && CheckSafe(Params))
  1347. {
  1348. Configuration->Usage->Inc(L"ScriptLog");
  1349. Configuration->TemporaryLogging(LogFile);
  1350. }
  1351. }
  1352. //---------------------------------------------------------------------------
  1353. bool __fastcall CheckXmlLogParam(TProgramParams * Params)
  1354. {
  1355. UnicodeString LogFile;
  1356. bool Result =
  1357. Params->FindSwitch(L"XmlLog", LogFile) &&
  1358. CheckSafe(Params);
  1359. if (Result)
  1360. {
  1361. Configuration->Usage->Inc(L"ScriptXmlLog");
  1362. Configuration->TemporaryActionsLogging(LogFile);
  1363. if (Params->FindSwitch(L"XmlLogRequired"))
  1364. {
  1365. Configuration->LogActionsRequired = true;
  1366. }
  1367. }
  1368. return Result;
  1369. }
  1370. //---------------------------------------------------------------------------
  1371. bool __fastcall CheckSafe(TProgramParams * Params)
  1372. {
  1373. // Originally we warned when the test didn't pass,
  1374. // but it would actually be helping hackers, so let's be silent.
  1375. // Earlier we tested presence of any URL on command-line.
  1376. // That was added to prevent abusing URL handler in 3.8.2 (2006).
  1377. // Later in 4.0.4 (2007) an /Unsafe switch was added to URL handler registration.
  1378. // So by now, we can check for the switch only and
  1379. // do not limit user from combining URL with say
  1380. // /rawconfig
  1381. return !Params->FindSwitch(UNSAFE_SWITCH);
  1382. }