UserInterface.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 "ProgParams.h"
  10. #include "WinConfiguration.h"
  11. #include "TerminalManager.h"
  12. #include "TextsWin.h"
  13. #include "TBXThemes.hpp"
  14. #include "TBXOfficeXPTheme.hpp"
  15. #include "TBXOffice2003Theme.hpp"
  16. //---------------------------------------------------------------------------
  17. #pragma package(smart_init)
  18. //---------------------------------------------------------------------------
  19. const AnsiString AppName = "WinSCP";
  20. const AnsiString AppNameVersion = "WinSCP";
  21. //---------------------------------------------------------------------------
  22. TConfiguration * __fastcall CreateConfiguration()
  23. {
  24. TConfiguration * Configuration = new TWinConfiguration();
  25. TProgramParams * Params = TProgramParams::Instance();
  26. AnsiString IniFileName = Params->SwitchValue("ini");
  27. if (!IniFileName.IsEmpty())
  28. {
  29. IniFileName = ExpandFileName(ExpandEnvironmentVariables(IniFileName));
  30. Configuration->IniFileStorageName = IniFileName;
  31. }
  32. return Configuration;
  33. }
  34. //---------------------------------------------------------------------------
  35. TCustomScpExplorerForm * __fastcall CreateScpExplorer()
  36. {
  37. TCustomScpExplorerForm * ScpExplorer;
  38. if (WinConfiguration->Interface == ifExplorer)
  39. Application->CreateForm(__classid(TScpExplorerForm), &ScpExplorer);
  40. else
  41. Application->CreateForm(__classid(TScpCommanderForm), &ScpExplorer);
  42. ScpExplorer->Icon->Assign(Application->Icon);
  43. return ScpExplorer;
  44. }
  45. //---------------------------------------------------------------------------
  46. AnsiString __fastcall SshVersionString()
  47. {
  48. return FORMAT("WinSCP-release-%s", (Configuration->Version));
  49. }
  50. //---------------------------------------------------------------------------
  51. AnsiString __fastcall GetRegistryKey()
  52. {
  53. return "Software\\Martin Prikryl\\WinSCP 2";
  54. }
  55. //---------------------------------------------------------------------------
  56. static bool ForcedOnForeground = false;
  57. void __fastcall SetOnForeground(bool OnForeground)
  58. {
  59. ForcedOnForeground = OnForeground;
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall FlashOnBackground()
  63. {
  64. assert(Application);
  65. if (!ForcedOnForeground && !ForegroundTask())
  66. {
  67. FlashWindow(Application->Handle, true);
  68. }
  69. }
  70. //---------------------------------------------------------------------------
  71. void __fastcall ShowExtendedException(Exception * E)
  72. {
  73. ShowExtendedExceptionEx(NULL, E);
  74. }
  75. //---------------------------------------------------------------------------
  76. void __fastcall ShowExtendedExceptionEx(TTerminal * Terminal,
  77. Exception * E)
  78. {
  79. if (!E->Message.IsEmpty())
  80. {
  81. if (E->InheritsFrom(__classid(Exception)))
  82. {
  83. if (!E->InheritsFrom(__classid(EAbort)))
  84. {
  85. TTerminalManager * Manager = TTerminalManager::Instance(false);
  86. TQueryType Type;
  87. bool CloseOnCompletion = (dynamic_cast<ESshTerminate*>(E) != NULL);
  88. Type = CloseOnCompletion ? qtInformation : qtError;
  89. if (E->InheritsFrom(__classid(EFatal)) && (Terminal != NULL) &&
  90. (Manager != NULL) && (Manager->ActiveTerminal == Terminal))
  91. {
  92. if (CloseOnCompletion)
  93. {
  94. Manager->DisconnectActiveTerminal();
  95. }
  96. int Result;
  97. if (CloseOnCompletion)
  98. {
  99. if (WinConfiguration->ConfirmExitOnCompletion)
  100. {
  101. TMessageParams Params(mpNeverAskAgainCheck);
  102. Result = FatalExceptionMessageDialog(E, Type,
  103. Manager->Count > 1 ?
  104. FMTLOAD(DISCONNECT_ON_COMPLETION, (Manager->Count - 1)) :
  105. LoadStr(EXIT_ON_COMPLETION),
  106. qaYes | qaNo, HELP_NONE, &Params);
  107. if (Result == qaNeverAskAgain)
  108. {
  109. Result = qaYes;
  110. WinConfiguration->ConfirmExitOnCompletion = false;
  111. }
  112. }
  113. else
  114. {
  115. Result = qaYes;
  116. }
  117. }
  118. else
  119. {
  120. Result = FatalExceptionMessageDialog(E, Type);
  121. }
  122. if (Result == qaYes)
  123. {
  124. Application->Terminate();
  125. }
  126. else if (Result == qaRetry)
  127. {
  128. Manager->ReconnectActiveTerminal();
  129. }
  130. else
  131. {
  132. Manager->FreeActiveTerminal();
  133. }
  134. }
  135. else
  136. {
  137. if (CloseOnCompletion)
  138. {
  139. if (WinConfiguration->ConfirmExitOnCompletion)
  140. {
  141. TMessageParams Params(mpNeverAskAgainCheck);
  142. if (ExceptionMessageDialog(E, Type, "", qaOK, HELP_NONE, &Params) ==
  143. qaNeverAskAgain)
  144. {
  145. WinConfiguration->ConfirmExitOnCompletion = false;
  146. }
  147. }
  148. }
  149. else
  150. {
  151. ExceptionMessageDialog(E, Type);
  152. }
  153. }
  154. }
  155. }
  156. else
  157. {
  158. FlashOnBackground();
  159. ShowException(ExceptObject(), ExceptAddr());
  160. }
  161. }
  162. }
  163. //---------------------------------------------------------------------------
  164. void __fastcall ConfigureInterface()
  165. {
  166. AnsiString S;
  167. S = LoadStr(MIDDLE_EAST);
  168. if (!S.IsEmpty())
  169. {
  170. SysLocale.MiddleEast = static_cast<bool>(StrToInt(S));
  171. }
  172. else
  173. {
  174. SysLocale.MiddleEast = false;
  175. }
  176. S = LoadStr(BIDI_MODE);
  177. if (!S.IsEmpty())
  178. {
  179. Application->BiDiMode = static_cast<TBiDiMode>(StrToInt(bdRightToLeft));
  180. }
  181. else
  182. {
  183. Application->BiDiMode = bdLeftToRight;
  184. }
  185. SetTBXSysParam(TSP_XPVISUALSTYLE, XPVS_AUTOMATIC);
  186. // Can be called during configuration creation.
  187. // Skip now, will be called again later.
  188. if (Configuration != NULL)
  189. {
  190. TBXSetTheme(WinConfiguration->Theme);
  191. }
  192. }
  193. //---------------------------------------------------------------------------
  194. // dummy function to force linking of TBXOfficeXPTheme.pas
  195. void __fastcall CreateThemes()
  196. {
  197. new TTBXOfficeXPTheme("OfficeXP");
  198. new TTBXOffice2003Theme("Office2003");
  199. }
  200. //---------------------------------------------------------------------------
  201. void __fastcall DoAboutDialog(TConfiguration *Configuration)
  202. {
  203. DoAboutDialog(Configuration, true, NULL);
  204. }
  205. //---------------------------------------------------------------------
  206. void __fastcall DoProductLicence()
  207. {
  208. DoLicenceDialog(lcWinScp);
  209. }
  210. //---------------------------------------------------------------------
  211. static inline void __fastcall GetToolbarKey(const AnsiString & ToolbarName,
  212. const AnsiString & Value, AnsiString & ToolbarKey)
  213. {
  214. int ToolbarNameLen;
  215. if ((ToolbarName.Length() > 7) &&
  216. (ToolbarName.SubString(ToolbarName.Length() - 7 + 1, 7) == "Toolbar"))
  217. {
  218. ToolbarNameLen = ToolbarName.Length() - 7;
  219. }
  220. else
  221. {
  222. ToolbarNameLen = ToolbarName.Length();
  223. }
  224. ToolbarKey = ToolbarName.SubString(1, ToolbarNameLen) + "_" + Value;
  225. }
  226. //---------------------------------------------------------------------------
  227. static int __fastcall ToolbarReadInt(const AnsiString ToolbarName,
  228. const AnsiString Value, const int Default, const void * ExtraData)
  229. {
  230. int Result;
  231. if (Value == "Rev")
  232. {
  233. Result = 2000;
  234. }
  235. else
  236. {
  237. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  238. AnsiString ToolbarKey;
  239. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  240. if (Storage->IndexOfName(ToolbarKey) >= 0)
  241. {
  242. Result = StrToIntDef(Storage->Values[ToolbarKey], Default);
  243. }
  244. else
  245. {
  246. Result = Default;
  247. }
  248. }
  249. return Result;
  250. }
  251. //---------------------------------------------------------------------------
  252. static AnsiString __fastcall ToolbarReadString(const AnsiString ToolbarName,
  253. const AnsiString Value, const AnsiString Default, const void * ExtraData)
  254. {
  255. AnsiString Result;
  256. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  257. AnsiString ToolbarKey;
  258. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  259. if (Storage->IndexOfName(ToolbarKey) >= 0)
  260. {
  261. Result = Storage->Values[ToolbarKey];
  262. }
  263. else
  264. {
  265. Result = Default;
  266. }
  267. return Result;
  268. }
  269. //---------------------------------------------------------------------------
  270. static void __fastcall ToolbarWriteInt(const AnsiString ToolbarName,
  271. const AnsiString Value, const int Data, const void * ExtraData)
  272. {
  273. if (Value != "Rev")
  274. {
  275. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  276. AnsiString ToolbarKey;
  277. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  278. assert(Storage->IndexOfName(ToolbarKey) < 0);
  279. Storage->Values[ToolbarKey] = IntToStr(Data);
  280. }
  281. }
  282. //---------------------------------------------------------------------------
  283. static void __fastcall ToolbarWriteString(const AnsiString ToolbarName,
  284. const AnsiString Value, const AnsiString Data, const void * ExtraData)
  285. {
  286. TStrings * Storage = static_cast<TStrings *>(const_cast<void*>(ExtraData));
  287. AnsiString ToolbarKey;
  288. GetToolbarKey(ToolbarName, Value, ToolbarKey);
  289. assert(Storage->IndexOfName(ToolbarKey) < 0);
  290. Storage->Values[ToolbarKey] = Data;
  291. }
  292. //---------------------------------------------------------------------------
  293. AnsiString __fastcall GetToolbarsLayoutStr(const TComponent * OwnerComponent)
  294. {
  295. AnsiString Result;
  296. TStrings * Storage = new TStringList();
  297. try
  298. {
  299. TBCustomSavePositions(OwnerComponent, ToolbarWriteInt, ToolbarWriteString,
  300. Storage);
  301. Result = Storage->CommaText;
  302. }
  303. __finally
  304. {
  305. delete Storage;
  306. }
  307. return Result;
  308. }
  309. //---------------------------------------------------------------------------
  310. void __fastcall LoadToolbarsLayoutStr(const TComponent * OwnerComponent, AnsiString LayoutStr)
  311. {
  312. TStrings * Storage = new TStringList();
  313. try
  314. {
  315. Storage->CommaText = LayoutStr;
  316. TBCustomLoadPositions(OwnerComponent, ToolbarReadInt, ToolbarReadString,
  317. Storage);
  318. }
  319. __finally
  320. {
  321. delete Storage;
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. void __fastcall AddMenuSeparator(TTBCustomItem * Menu)
  326. {
  327. TTBXSeparatorItem * Item = new TTBXSeparatorItem(Menu);
  328. Item->Caption = "-";
  329. Item->Hint = "E";
  330. Menu->Add(Item);
  331. }
  332. //---------------------------------------------------------------------------
  333. void __fastcall MenuPopup(TPopupMenu * AMenu, TPoint Point,
  334. TComponent * PopupComponent)
  335. {
  336. static TComponent * LastPopupComponent = NULL;
  337. static TDateTime LastCloseUp;
  338. // pressing the same button within 200ms after closing its popup menu
  339. // does nothing.
  340. // it is to immitate close-by-click behaviour. note that menu closes itself
  341. // before onclick handler of button occurs
  342. if ((PopupComponent == LastPopupComponent) &&
  343. (Now() - LastCloseUp < TDateTime(0, 0, 0, 200)))
  344. {
  345. LastPopupComponent = NULL;
  346. }
  347. else
  348. {
  349. TTBXPopupMenu * Menu = CreateTBXPopupMenu(AMenu->Owner);
  350. Menu->OnPopup = AMenu->OnPopup;
  351. Menu->Items->SubMenuImages = AMenu->Images;
  352. for (int Index = 0; Index < AMenu->Items->Count; Index++)
  353. {
  354. TMenuItem * AItem = AMenu->Items->Items[Index];
  355. TTBCustomItem * Item;
  356. // recurse not implemented yet
  357. assert(AItem->Count == 0);
  358. // see TB2DsgnConverter.pas DoConvert
  359. if (AItem->Caption == "-")
  360. {
  361. Item = new TTBXSeparatorItem(Menu);
  362. }
  363. else
  364. {
  365. Item = new TTBXItem(Menu);
  366. Item->Action = AItem->Action;
  367. Item->AutoCheck = AItem->AutoCheck;
  368. Item->Caption = AItem->Caption;
  369. Item->Checked = AItem->Checked;
  370. if (AItem->Default)
  371. {
  372. Item->Options = Item->Options << tboDefault;
  373. }
  374. Item->Enabled = AItem->Enabled;
  375. Item->GroupIndex = AItem->GroupIndex;
  376. Item->HelpContext = AItem->HelpContext;
  377. Item->ImageIndex = AItem->ImageIndex;
  378. Item->RadioItem = AItem->RadioItem;
  379. Item->ShortCut = AItem->ShortCut;
  380. Item->SubMenuImages = AItem->SubMenuImages;
  381. Item->OnClick = AItem->OnClick;
  382. }
  383. Item->Hint = AItem->Hint;
  384. Item->Tag = AItem->Tag;
  385. Item->Visible = AItem->Visible;
  386. Menu->Items->Add(Item);
  387. }
  388. Menu->PopupComponent = PopupComponent;
  389. Menu->Popup(Point.x, Point.y);
  390. LastPopupComponent = PopupComponent;
  391. LastCloseUp = Now();
  392. }
  393. }
  394. //---------------------------------------------------------------------------
  395. void __fastcall UpgradeSpeedButton(TSpeedButton * /*Button*/)
  396. {
  397. // no-op yet
  398. }