UserInterface.cpp 44 KB

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