UserInterface.cpp 48 KB

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