UserInterface.cpp 44 KB

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