UserInterface.cpp 44 KB

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