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