UserInterface.cpp 50 KB

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