GUITools.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. //---------------------------------------------------------------------------
  2. #define NO_WIN32_LEAN_AND_MEAN
  3. #include <vcl.h>
  4. #pragma hdrstop
  5. #include <shlobj.h>
  6. #include <Common.h>
  7. #include "GUITools.h"
  8. #include "GUIConfiguration.h"
  9. #include <TextsCore.h>
  10. #include <CoreMain.h>
  11. #include <SessionData.h>
  12. #include <WinInterface.h>
  13. #include <TbxUtils.hpp>
  14. #include <Math.hpp>
  15. #include "PngImageList.hpp"
  16. //---------------------------------------------------------------------------
  17. #pragma package(smart_init)
  18. //---------------------------------------------------------------------------
  19. extern const UnicodeString PageantTool = L"pageant.exe";
  20. extern const UnicodeString PuttygenTool = L"puttygen.exe";
  21. //---------------------------------------------------------------------------
  22. bool __fastcall FindFile(UnicodeString & Path)
  23. {
  24. bool Result = FileExists(Path);
  25. if (!Result)
  26. {
  27. int Len = GetEnvironmentVariable(L"PATH", NULL, 0);
  28. if (Len > 0)
  29. {
  30. UnicodeString Paths;
  31. Paths.SetLength(Len - 1);
  32. GetEnvironmentVariable(L"PATH", Paths.c_str(), Len);
  33. UnicodeString NewPath = FileSearch(ExtractFileName(Path), Paths);
  34. Result = !NewPath.IsEmpty();
  35. if (Result)
  36. {
  37. Path = NewPath;
  38. }
  39. }
  40. }
  41. return Result;
  42. }
  43. //---------------------------------------------------------------------------
  44. bool __fastcall FileExistsEx(UnicodeString Path)
  45. {
  46. return FindFile(Path);
  47. }
  48. //---------------------------------------------------------------------------
  49. void __fastcall OpenSessionInPutty(const UnicodeString PuttyPath,
  50. TSessionData * SessionData, UnicodeString UserName, UnicodeString Password)
  51. {
  52. UnicodeString Program, AParams, Dir;
  53. SplitCommand(PuttyPath, Program, AParams, Dir);
  54. Program = ExpandEnvironmentVariables(Program);
  55. if (FindFile(Program))
  56. {
  57. AParams = ExpandEnvironmentVariables(AParams);
  58. TCustomCommandData Data(SessionData, UserName, Password);
  59. TRemoteCustomCommand RemoteCustomCommand(Data, SessionData->RemoteDirectory);
  60. TWinInteractiveCustomCommand InteractiveCustomCommand(
  61. &RemoteCustomCommand, L"PuTTY");
  62. UnicodeString Params =
  63. RemoteCustomCommand.Complete(InteractiveCustomCommand.Complete(AParams, false), true);
  64. if (!RemoteCustomCommand.IsSiteCommand(AParams))
  65. {
  66. UnicodeString SessionName;
  67. TRegistryStorage * Storage = NULL;
  68. TSessionData * ExportData = NULL;
  69. TRegistryStorage * SourceStorage = NULL;
  70. try
  71. {
  72. Storage = new TRegistryStorage(Configuration->PuttySessionsKey);
  73. Storage->AccessMode = smReadWrite;
  74. // make it compatible with putty
  75. Storage->MungeStringValues = false;
  76. Storage->ForceAnsi = true;
  77. if (Storage->OpenRootKey(true))
  78. {
  79. if (Storage->KeyExists(SessionData->StorageKey))
  80. {
  81. SessionName = SessionData->SessionName;
  82. }
  83. else
  84. {
  85. SourceStorage = new TRegistryStorage(Configuration->PuttySessionsKey);
  86. SourceStorage->MungeStringValues = false;
  87. SourceStorage->ForceAnsi = true;
  88. if (SourceStorage->OpenSubKey(StoredSessions->DefaultSettings->Name, false) &&
  89. Storage->OpenSubKey(GUIConfiguration->PuttySession, true))
  90. {
  91. Storage->Copy(SourceStorage);
  92. Storage->CloseSubKey();
  93. }
  94. ExportData = new TSessionData(L"");
  95. ExportData->Assign(SessionData);
  96. ExportData->Modified = true;
  97. ExportData->Name = GUIConfiguration->PuttySession;
  98. ExportData->Password = L"";
  99. if (SessionData->FSProtocol == fsFTP)
  100. {
  101. if (GUIConfiguration->TelnetForFtpInPutty)
  102. {
  103. ExportData->PuttyProtocol = PuttyTelnetProtocol;
  104. ExportData->PortNumber = TelnetPortNumber;
  105. // PuTTY does not allow -pw for telnet
  106. Password = L"";
  107. }
  108. else
  109. {
  110. ExportData->PuttyProtocol = PuttySshProtocol;
  111. ExportData->PortNumber = SshPortNumber;
  112. }
  113. }
  114. ExportData->Save(Storage, true);
  115. SessionName = GUIConfiguration->PuttySession;
  116. }
  117. }
  118. }
  119. __finally
  120. {
  121. delete Storage;
  122. delete ExportData;
  123. delete SourceStorage;
  124. }
  125. AddToList(Params, FORMAT(L"-load %s", (EscapePuttyCommandParam(SessionName))), L" ");
  126. }
  127. if (!Password.IsEmpty() && !RemoteCustomCommand.IsPasswordCommand(AParams))
  128. {
  129. AddToList(Params, FORMAT(L"-pw %s", (EscapePuttyCommandParam(Password))), L" ");
  130. }
  131. if (!ExecuteShell(Program, Params))
  132. {
  133. throw Exception(FMTLOAD(EXECUTE_APP_ERROR, (Program)));
  134. }
  135. }
  136. else
  137. {
  138. throw Exception(FMTLOAD(FILE_NOT_FOUND, (Program)));
  139. }
  140. }
  141. //---------------------------------------------------------------------------
  142. bool __fastcall FindTool(const UnicodeString & Name, UnicodeString & Path)
  143. {
  144. UnicodeString AppPath = IncludeTrailingBackslash(ExtractFilePath(Application->ExeName));
  145. Path = AppPath + Name;
  146. bool Result = true;
  147. if (!FileExists(Path))
  148. {
  149. Path = AppPath + L"PuTTY\\" + Name;
  150. if (!FileExists(Path))
  151. {
  152. Path = Name;
  153. if (!FindFile(Path))
  154. {
  155. Result = false;
  156. }
  157. }
  158. }
  159. return Result;
  160. }
  161. //---------------------------------------------------------------------------
  162. bool __fastcall ExecuteShell(const UnicodeString Path, const UnicodeString Params)
  163. {
  164. return ((int)ShellExecute(NULL, L"open", (wchar_t*)Path.data(),
  165. (wchar_t*)Params.data(), NULL, SW_SHOWNORMAL) > 32);
  166. }
  167. //---------------------------------------------------------------------------
  168. bool __fastcall ExecuteShell(const UnicodeString Path, const UnicodeString Params,
  169. HANDLE & Handle)
  170. {
  171. bool Result;
  172. TShellExecuteInfoW ExecuteInfo;
  173. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  174. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  175. ExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  176. ExecuteInfo.hwnd = Application->Handle;
  177. ExecuteInfo.lpFile = (wchar_t*)Path.data();
  178. ExecuteInfo.lpParameters = (wchar_t*)Params.data();
  179. ExecuteInfo.nShow = SW_SHOW;
  180. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  181. if (Result)
  182. {
  183. Handle = ExecuteInfo.hProcess;
  184. }
  185. return Result;
  186. }
  187. //---------------------------------------------------------------------------
  188. bool __fastcall ExecuteShellAndWait(HWND Handle, const UnicodeString Path,
  189. const UnicodeString Params, TProcessMessagesEvent ProcessMessages)
  190. {
  191. bool Result;
  192. TShellExecuteInfoW ExecuteInfo;
  193. memset(&ExecuteInfo, 0, sizeof(ExecuteInfo));
  194. ExecuteInfo.cbSize = sizeof(ExecuteInfo);
  195. ExecuteInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  196. ExecuteInfo.hwnd = Handle;
  197. ExecuteInfo.lpFile = (wchar_t*)Path.data();
  198. ExecuteInfo.lpParameters = (wchar_t*)Params.data();
  199. ExecuteInfo.nShow = SW_SHOW;
  200. Result = (ShellExecuteEx(&ExecuteInfo) != 0);
  201. if (Result)
  202. {
  203. if (ProcessMessages != NULL)
  204. {
  205. unsigned long WaitResult;
  206. do
  207. {
  208. WaitResult = WaitForSingleObject(ExecuteInfo.hProcess, 200);
  209. if (WaitResult == WAIT_FAILED)
  210. {
  211. throw Exception(LoadStr(DOCUMENT_WAIT_ERROR));
  212. }
  213. ProcessMessages();
  214. }
  215. while (WaitResult == WAIT_TIMEOUT);
  216. }
  217. else
  218. {
  219. WaitForSingleObject(ExecuteInfo.hProcess, INFINITE);
  220. }
  221. }
  222. return Result;
  223. }
  224. //---------------------------------------------------------------------------
  225. bool __fastcall ExecuteShellAndWait(HWND Handle, const UnicodeString Command,
  226. TProcessMessagesEvent ProcessMessages)
  227. {
  228. UnicodeString Program, Params, Dir;
  229. SplitCommand(Command, Program, Params, Dir);
  230. return ExecuteShellAndWait(Handle, Program, Params, ProcessMessages);
  231. }
  232. //---------------------------------------------------------------------------
  233. bool __fastcall SpecialFolderLocation(int PathID, UnicodeString & Path)
  234. {
  235. LPITEMIDLIST Pidl;
  236. wchar_t Buf[256];
  237. if (SHGetSpecialFolderLocation(NULL, PathID, &Pidl) == NO_ERROR &&
  238. SHGetPathFromIDList(Pidl, Buf))
  239. {
  240. Path = UnicodeString(Buf);
  241. return true;
  242. }
  243. return false;
  244. }
  245. //---------------------------------------------------------------------------
  246. static UnicodeString __fastcall GetWineHomeFolder()
  247. {
  248. UnicodeString Result;
  249. UnicodeString WineHostHome = GetEnvironmentVariable(L"WINE_HOST_HOME");
  250. if (!WineHostHome.IsEmpty())
  251. {
  252. Result = L"Z:" + FromUnixPath(WineHostHome);
  253. }
  254. else
  255. {
  256. // Should we use WinAPI GetUserName() instead?
  257. UnicodeString UserName = GetEnvironmentVariable(L"USERNAME");
  258. if (!UserName.IsEmpty())
  259. {
  260. Result = L"Z:\\home\\" + UserName;
  261. }
  262. }
  263. if (!DirectoryExists(Result))
  264. {
  265. Result = L"";
  266. }
  267. return Result;
  268. }
  269. //---------------------------------------------------------------------------
  270. UnicodeString __fastcall GetPersonalFolder()
  271. {
  272. UnicodeString Result;
  273. SpecialFolderLocation(CSIDL_PERSONAL, Result);
  274. if (IsWine())
  275. {
  276. UnicodeString WineHome = GetWineHomeFolder();
  277. if (!WineHome.IsEmpty())
  278. {
  279. // if at least home exists, use it
  280. Result = WineHome;
  281. // but try to go deeper to "Documents"
  282. UnicodeString WineDocuments =
  283. IncludeTrailingBackslash(WineHome) + L"Documents";
  284. if (DirectoryExists(WineDocuments))
  285. {
  286. Result = WineDocuments;
  287. }
  288. }
  289. }
  290. return Result;
  291. }
  292. //---------------------------------------------------------------------------
  293. UnicodeString __fastcall GetDesktopFolder()
  294. {
  295. UnicodeString Result;
  296. SpecialFolderLocation(CSIDL_DESKTOPDIRECTORY, Result);
  297. if (IsWine())
  298. {
  299. UnicodeString WineHome = GetWineHomeFolder();
  300. if (!WineHome.IsEmpty())
  301. {
  302. UnicodeString WineDesktop =
  303. IncludeTrailingBackslash(WineHome) + L"Desktop";
  304. if (DirectoryExists(WineHome))
  305. {
  306. Result = WineDesktop;
  307. }
  308. }
  309. }
  310. return Result;
  311. }
  312. //---------------------------------------------------------------------------
  313. UnicodeString __fastcall ItemsFormatString(const UnicodeString SingleItemFormat,
  314. const UnicodeString MultiItemsFormat, int Count, const UnicodeString FirstItem)
  315. {
  316. UnicodeString Result;
  317. if (Count == 1)
  318. {
  319. Result = FORMAT(SingleItemFormat, (FirstItem));
  320. }
  321. else
  322. {
  323. Result = FORMAT(MultiItemsFormat, (Count));
  324. }
  325. return Result;
  326. }
  327. //---------------------------------------------------------------------------
  328. UnicodeString __fastcall ItemsFormatString(const UnicodeString SingleItemFormat,
  329. const UnicodeString MultiItemsFormat, TStrings * Items)
  330. {
  331. return ItemsFormatString(SingleItemFormat, MultiItemsFormat,
  332. Items->Count, (Items->Count > 0 ? Items->Strings[0] : UnicodeString()));
  333. }
  334. //---------------------------------------------------------------------------
  335. UnicodeString __fastcall FileNameFormatString(const UnicodeString SingleFileFormat,
  336. const UnicodeString MultiFilesFormat, TStrings * Files, bool Remote)
  337. {
  338. assert(Files != NULL);
  339. UnicodeString Item;
  340. if (Files->Count > 0)
  341. {
  342. Item = Remote ? UnixExtractFileName(Files->Strings[0]) :
  343. ExtractFileName(Files->Strings[0]);
  344. }
  345. return ItemsFormatString(SingleFileFormat, MultiFilesFormat,
  346. Files->Count, Item);
  347. }
  348. //---------------------------------------------------------------------------
  349. UnicodeString __fastcall UniqTempDir(const UnicodeString BaseDir, const UnicodeString Identity,
  350. bool Mask)
  351. {
  352. UnicodeString TempDir;
  353. do
  354. {
  355. TempDir = BaseDir.IsEmpty() ? SystemTemporaryDirectory() : BaseDir;
  356. TempDir = IncludeTrailingBackslash(TempDir) + Identity;
  357. if (Mask)
  358. {
  359. TempDir += L"?????";
  360. }
  361. else
  362. {
  363. TempDir += IncludeTrailingBackslash(FormatDateTime(L"nnzzz", Now()));
  364. };
  365. }
  366. while (!Mask && DirectoryExists(TempDir));
  367. return TempDir;
  368. }
  369. //---------------------------------------------------------------------------
  370. bool __fastcall DeleteDirectory(const UnicodeString DirName)
  371. {
  372. TSearchRecChecked sr;
  373. bool retval = true;
  374. if (FindFirstUnchecked(DirName + L"\\*", faAnyFile, sr) == 0) // VCL Function
  375. {
  376. if (FLAGSET(sr.Attr, faDirectory))
  377. {
  378. if (sr.Name != L"." && sr.Name != L"..")
  379. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  380. }
  381. else
  382. {
  383. retval = DeleteFile(DirName + L"\\" + sr.Name);
  384. }
  385. if (retval)
  386. {
  387. while (FindNextChecked(sr) == 0)
  388. { // VCL Function
  389. if (FLAGSET(sr.Attr, faDirectory))
  390. {
  391. if (sr.Name != L"." && sr.Name != L"..")
  392. retval = DeleteDirectory(DirName + L"\\" + sr.Name);
  393. }
  394. else
  395. {
  396. retval = DeleteFile(DirName + L"\\" + sr.Name);
  397. }
  398. if (!retval) break;
  399. }
  400. }
  401. }
  402. FindClose(sr);
  403. if (retval) retval = RemoveDir(DirName); // VCL function
  404. return retval;
  405. }
  406. //---------------------------------------------------------------------------
  407. UnicodeString __fastcall FormatDateTimeSpan(const UnicodeString TimeFormat, TDateTime DateTime)
  408. {
  409. UnicodeString Result;
  410. if (int(DateTime) > 0)
  411. {
  412. Result = IntToStr(int(DateTime)) + L", ";
  413. }
  414. // days are decremented, because when there are to many of them,
  415. // "integer overflow" error occurs
  416. Result += FormatDateTime(TimeFormat, DateTime - int(DateTime));
  417. return Result;
  418. }
  419. //---------------------------------------------------------------------------
  420. void __fastcall AddSessionColorImage(
  421. TCustomImageList * ImageList, TColor Color, int MaskIndex)
  422. {
  423. // This overly complex drawing is here to support color button on SiteAdvanced
  424. // dialog. There we use plain TImageList, instead of TPngImageList,
  425. // TButton does not work with transparent images
  426. // (not even TBitmap with Transparent = true)
  427. std::unique_ptr<TBitmap> MaskBitmap(new TBitmap());
  428. ImageList->GetBitmap(MaskIndex, MaskBitmap.get());
  429. std::unique_ptr<TPngImage> MaskImage(new TPngImage());
  430. MaskImage->Assign(MaskBitmap.get());
  431. std::unique_ptr<TPngImage> ColorImage(new TPngImage(COLOR_RGB, 16, ImageList->Width, ImageList->Height));
  432. TColor MaskTransparentColor = MaskImage->Pixels[0][0];
  433. TColor TransparentColor = MaskTransparentColor;
  434. // Expecting that the color to be replaced is in the centre of the image (HACK)
  435. TColor MaskColor = MaskImage->Pixels[ImageList->Width / 2][ImageList->Height / 2];
  436. for (int Y = 0; Y < ImageList->Height; Y++)
  437. {
  438. for (int X = 0; X < ImageList->Width; X++)
  439. {
  440. TColor SourceColor = MaskImage->Pixels[X][Y];
  441. TColor DestColor;
  442. // this branch is pointless as long as MaskTransparentColor and
  443. // TransparentColor are the same
  444. if (SourceColor == MaskTransparentColor)
  445. {
  446. DestColor = TransparentColor;
  447. }
  448. else if (SourceColor == MaskColor)
  449. {
  450. DestColor = Color;
  451. }
  452. else
  453. {
  454. DestColor = SourceColor;
  455. }
  456. ColorImage->Pixels[X][Y] = DestColor;
  457. }
  458. }
  459. std::unique_ptr<TBitmap> Bitmap(new TBitmap());
  460. Bitmap->SetSize(ImageList->Width, ImageList->Height);
  461. ColorImage->AssignTo(Bitmap.get());
  462. ImageList->AddMasked(Bitmap.get(), TransparentColor);
  463. }
  464. //---------------------------------------------------------------------------
  465. bool __fastcall IsEligibleForApplyingTabs(
  466. UnicodeString Line, int & TabPos, UnicodeString & Start, UnicodeString & Remaining)
  467. {
  468. bool Result = false;
  469. TabPos = Line.Pos(L"\t");
  470. if (TabPos > 0)
  471. {
  472. Remaining = Line.SubString(TabPos + 1, Line.Length() - TabPos);
  473. // WORKAROUND
  474. // Some translations still use obsolete hack of consecutive tabs to aling the contents.
  475. // Delete these, so that the following check does not fail on this
  476. while (Remaining.SubString(1, 1) == L"\t")
  477. {
  478. Remaining.Delete(1, 1);
  479. }
  480. // We do not have, not support, mutiple tabs on a single line
  481. if (ALWAYS_TRUE(Remaining.Pos(L"\t") == 0))
  482. {
  483. Start = Line.SubString(1, TabPos - 1);
  484. // WORKAROUND
  485. // Previously we padded the string before tab with spaces,
  486. // to aling the contents across multiple lines
  487. Start = Start.TrimRight();
  488. // at least two normal spaces for separation
  489. Start += L" ";
  490. Result = true;
  491. }
  492. }
  493. return Result;
  494. }
  495. //---------------------------------------------------------------------------
  496. static int __fastcall CalculateWidthByLength(UnicodeString Text, void * /*Arg*/)
  497. {
  498. return Text.Length();
  499. }
  500. //---------------------------------------------------------------------------
  501. void __fastcall ApplyTabs(
  502. UnicodeString & Text, wchar_t Padding,
  503. TCalculateWidth CalculateWidth, void * CalculateWidthArg)
  504. {
  505. if (CalculateWidth == NULL)
  506. {
  507. assert(CalculateWidthArg == NULL);
  508. CalculateWidth = CalculateWidthByLength;
  509. }
  510. std::unique_ptr<TStringList> Lines(new TStringList());
  511. Lines->Text = Text;
  512. int MaxWidth = -1;
  513. for (int Index = 0; Index < Lines->Count; Index++)
  514. {
  515. UnicodeString Line = Lines->Strings[Index];
  516. int TabPos;
  517. UnicodeString Start;
  518. UnicodeString Remaining;
  519. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  520. {
  521. int Width = CalculateWidth(Start, CalculateWidthArg);
  522. MaxWidth = Max(MaxWidth, Width);
  523. }
  524. }
  525. // Optimization and also to prevent potential regression for texts without tabs
  526. if (MaxWidth >= 0)
  527. {
  528. for (int Index = 0; Index < Lines->Count; Index++)
  529. {
  530. UnicodeString Line = Lines->Strings[Index];
  531. int TabPos;
  532. UnicodeString Start;
  533. UnicodeString Remaining;
  534. if (IsEligibleForApplyingTabs(Line, TabPos, Start, Remaining))
  535. {
  536. int Width;
  537. while ((Width = CalculateWidth(Start, CalculateWidthArg)) < MaxWidth)
  538. {
  539. int Wider = CalculateWidth(Start + Padding, CalculateWidthArg);
  540. // If padded string is wider than max width by more pixels
  541. // than non-padded string is shorter than max width
  542. if ((Wider > MaxWidth) && ((Wider - MaxWidth) > (MaxWidth - Width)))
  543. {
  544. break;
  545. }
  546. Start += Padding;
  547. }
  548. Lines->Strings[Index] = Start + Remaining;
  549. }
  550. }
  551. Text = Lines->Text;
  552. // remove trailing newline
  553. Text = Text.TrimRight();
  554. }
  555. }
  556. //---------------------------------------------------------------------------
  557. TLocalCustomCommand::TLocalCustomCommand()
  558. {
  559. }
  560. //---------------------------------------------------------------------------
  561. TLocalCustomCommand::TLocalCustomCommand(const TCustomCommandData & Data,
  562. const UnicodeString & Path) :
  563. TFileCustomCommand(Data, Path)
  564. {
  565. }
  566. //---------------------------------------------------------------------------
  567. TLocalCustomCommand::TLocalCustomCommand(const TCustomCommandData & Data,
  568. const UnicodeString & Path, const UnicodeString & FileName,
  569. const UnicodeString & LocalFileName, const UnicodeString & FileList) :
  570. TFileCustomCommand(Data, Path, FileName, FileList)
  571. {
  572. FLocalFileName = LocalFileName;
  573. }
  574. //---------------------------------------------------------------------------
  575. int __fastcall TLocalCustomCommand::PatternLen(const UnicodeString & Command, int Index)
  576. {
  577. int Len;
  578. if ((Index < Command.Length()) && (Command[Index + 1] == L'^'))
  579. {
  580. Len = 3;
  581. }
  582. else
  583. {
  584. Len = TFileCustomCommand::PatternLen(Command, Index);
  585. }
  586. return Len;
  587. }
  588. //---------------------------------------------------------------------------
  589. bool __fastcall TLocalCustomCommand::PatternReplacement(
  590. const UnicodeString & Pattern, UnicodeString & Replacement, bool & Delimit)
  591. {
  592. bool Result;
  593. if (Pattern == L"!^!")
  594. {
  595. Replacement = FLocalFileName;
  596. Result = true;
  597. }
  598. else
  599. {
  600. Result = TFileCustomCommand::PatternReplacement(Pattern, Replacement, Delimit);
  601. }
  602. return Result;
  603. }
  604. //---------------------------------------------------------------------------
  605. void __fastcall TLocalCustomCommand::DelimitReplacement(
  606. UnicodeString & /*Replacement*/, wchar_t /*Quote*/)
  607. {
  608. // never delimit local commands
  609. }
  610. //---------------------------------------------------------------------------
  611. bool __fastcall TLocalCustomCommand::HasLocalFileName(const UnicodeString & Command)
  612. {
  613. return FindPattern(Command, L'^');
  614. }
  615. //---------------------------------------------------------------------------
  616. bool __fastcall TLocalCustomCommand::IsFileCommand(const UnicodeString & Command)
  617. {
  618. return TFileCustomCommand::IsFileCommand(Command) || HasLocalFileName(Command);
  619. }