GenerateUrl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "VCLCommon.h"
  5. #include "GenerateUrl.h"
  6. #include "CoreMain.h"
  7. #include "WinConfiguration.h"
  8. #include <StrUtils.hpp>
  9. #include <Tools.h>
  10. #include <PuttyTools.h>
  11. #include <TextsWin.h>
  12. #include <ProgParams.h>
  13. //---------------------------------------------------------------------------
  14. #pragma package(smart_init)
  15. #ifndef NO_RESOURCES
  16. #pragma resource "*.dfm"
  17. #endif
  18. //---------------------------------------------------------------------------
  19. void __fastcall DoGenerateUrlDialog(TSessionData * Data, TStrings * Paths)
  20. {
  21. std::unique_ptr<TGenerateUrlDialog> Dialog(new TGenerateUrlDialog(GetFormOwner(), Data, Paths));
  22. Dialog->Execute();
  23. }
  24. //---------------------------------------------------------------------------
  25. // Rich edit 4.1 supports "Friendly name hyperlinks"
  26. class TRichEdit41 : public TRichEdit
  27. {
  28. public:
  29. virtual __fastcall TRichEdit41(TComponent * AOwner);
  30. protected:
  31. virtual void __fastcall CreateWnd();
  32. virtual void __fastcall CreateParams(TCreateParams & Params);
  33. virtual void __fastcall DestroyWnd();
  34. void __fastcall Dispatch(void * Message);
  35. private:
  36. HINSTANCE FLibrary;
  37. };
  38. //---------------------------------------------------------------------------
  39. __fastcall TRichEdit41::TRichEdit41(TComponent * AOwner) :
  40. TRichEdit(AOwner),
  41. FLibrary(0)
  42. {
  43. }
  44. //---------------------------------------------------------------------------
  45. void __fastcall TRichEdit41::CreateParams(TCreateParams & Params)
  46. {
  47. UnicodeString RichEditModuleName(L"MSFTEDIT.DLL");
  48. long int OldError;
  49. OldError = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  50. FLibrary = LoadLibrary(RichEditModuleName.c_str());
  51. SetErrorMode(OldError);
  52. TCustomMemo::CreateParams(Params);
  53. // Should not happen as
  54. if (FLibrary != 0)
  55. {
  56. // MSDN says that we should use MSFTEDIT_CLASS to load Rich Edit 4.1:
  57. // https://msdn.microsoft.com/en-us/library/windows/desktop/bb787873.aspx
  58. // But MSFTEDIT_CLASS is defined as "RICHEDIT50W",
  59. // so not sure what version we are loading.
  60. // Seem to work on Windows XP SP3.
  61. CreateSubClass(Params, MSFTEDIT_CLASS);
  62. }
  63. }
  64. //---------------------------------------------------------------------------
  65. void __fastcall TRichEdit41::CreateWnd()
  66. {
  67. TRichEdit::CreateWnd();
  68. int Mask = SendMessage(Handle, EM_GETEVENTMASK, 0, 0);
  69. SendMessage(Handle, EM_SETEVENTMASK, 0, Mask | ENM_LINK);
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TRichEdit41::Dispatch(void * AMessage)
  73. {
  74. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  75. if (Message.Msg == CN_NOTIFY)
  76. {
  77. TWMNotify & WMNotify = *reinterpret_cast<TWMNotify *>(AMessage);
  78. if (WMNotify.NMHdr->code == EN_LINK)
  79. {
  80. TENLink & ENLink = *reinterpret_cast<TENLink *>(Message.LParam);
  81. if (ENLink.msg == WM_LBUTTONDOWN)
  82. {
  83. UnicodeString AText = Text;
  84. // The cpMin and cpMax refer to indexes in a script with a single-byte EOL,
  85. // while the Text (GetWindowText) uses two-byte EOL
  86. AText = ReplaceStr(AText, L"\r\n", L"\n");
  87. if (DebugAlwaysTrue(ENLink.chrg.cpMax < AText.Length()))
  88. {
  89. UnicodeString Url = AText.SubString(ENLink.chrg.cpMin + 1, ENLink.chrg.cpMax - ENLink.chrg.cpMin);
  90. ShowHelp(Url);
  91. }
  92. }
  93. }
  94. TRichEdit::Dispatch(AMessage);
  95. }
  96. else
  97. {
  98. TRichEdit::Dispatch(AMessage);
  99. }
  100. }
  101. //---------------------------------------------------------------------------
  102. void __fastcall TRichEdit41::DestroyWnd()
  103. {
  104. TRichEdit::DestroyWnd();
  105. if (FLibrary != 0)
  106. {
  107. FreeLibrary(FLibrary);
  108. }
  109. }
  110. //---------------------------------------------------------------------------
  111. //---------------------------------------------------------------------------
  112. __fastcall TGenerateUrlDialog::TGenerateUrlDialog(
  113. TComponent * Owner, TSessionData * Data, TStrings * Paths)
  114. : TForm(Owner)
  115. {
  116. UseSystemSettings(this);
  117. FData = Data;
  118. FPaths = Paths;
  119. FChanging = false;
  120. FResultMemo41 = new TRichEdit41(this);
  121. FResultMemo41->Parent = ResultMemo->Parent;
  122. FResultMemo41->SetBounds(ResultMemo->Left, ResultMemo->Top, ResultMemo->Width, ResultMemo->Height);
  123. FResultMemo41->Anchors = ResultMemo->Anchors;
  124. FResultMemo41->BevelInner = ResultMemo->BevelInner;
  125. FResultMemo41->BevelOuter = ResultMemo->BevelOuter;
  126. FResultMemo41->BorderStyle = ResultMemo->BorderStyle;
  127. FResultMemo41->PopupMenu = ResultMemo->PopupMenu;
  128. FResultMemo41->TabOrder = ResultMemo->TabOrder;
  129. FResultMemo41->PlainText = false;
  130. ResultMemo->Visible = false;
  131. ReadOnlyControl(FResultMemo41);
  132. }
  133. //---------------------------------------------------------------------------
  134. bool __fastcall TGenerateUrlDialog::IsFileUrl()
  135. {
  136. return (FPaths != NULL);
  137. }
  138. //---------------------------------------------------------------------------
  139. UnicodeString __fastcall TGenerateUrlDialog::GenerateUrl(UnicodeString Path)
  140. {
  141. UnicodeString Url =
  142. FData->GenerateSessionUrl(
  143. FLAGMASK(WinSCPSpecificCheck->Checked, sufSpecific) |
  144. FLAGMASK(UserNameCheck->Enabled && UserNameCheck->Checked, sufUserName) |
  145. FLAGMASK(PasswordCheck->Enabled && PasswordCheck->Checked, sufPassword) |
  146. FLAGMASK(HostKeyCheck->Enabled && HostKeyCheck->Checked, sufHostKey));
  147. if ((RemoteDirectoryCheck->Enabled && RemoteDirectoryCheck->Checked) ||
  148. IsFileUrl())
  149. {
  150. if (StartsStr(L"/", Path));
  151. {
  152. Path.Delete(1, 1);
  153. }
  154. Url += EncodeUrlPath(Path);
  155. }
  156. if (SaveExtensionCheck->Enabled && SaveExtensionCheck->Checked)
  157. {
  158. Url += UnicodeString(UrlParamSeparator) + UrlSaveParamName;
  159. }
  160. return Url;
  161. }
  162. //---------------------------------------------------------------------------
  163. static UnicodeString __fastcall RtfColorEntry(int Color)
  164. {
  165. return FORMAT(L"\\red%d\\green%d\\blue%d;", ((Color & 0xFF0000) >> 16, (Color & 0x00FF00) >> 8, (Color & 0x0000FF) >> 0));
  166. }
  167. //---------------------------------------------------------------------
  168. static UnicodeString __fastcall RtfScriptComment(const UnicodeString & Text)
  169. {
  170. return RtfColorItalicText(7, Text);
  171. }
  172. //---------------------------------------------------------------------
  173. static UnicodeString __fastcall RtfScriptPlaceholder(const UnicodeString & Text)
  174. {
  175. return RtfColorText(7, Text);
  176. }
  177. //---------------------------------------------------------------------
  178. static UnicodeString __fastcall RtfScriptCommand(const UnicodeString & Command)
  179. {
  180. return RtfLink(L"scriptcommand_" + Command, RtfKeyword(Command));
  181. }
  182. //---------------------------------------------------------------------
  183. UnicodeString __fastcall RtfCommandlineSwitch(const UnicodeString & Switch, const UnicodeString & Anchor)
  184. {
  185. return RtfLink(L"commandline#" + Anchor, RtfParameter(TProgramParams::FormatSwitch(Switch.LowerCase())));
  186. }
  187. //---------------------------------------------------------------------------
  188. void __fastcall TGenerateUrlDialog::UpdateControls()
  189. {
  190. if (!FChanging)
  191. {
  192. UnicodeString ExeName = Application->ExeName;
  193. Caption = LoadStr(IsFileUrl() ? GENERATE_URL_FILE_TITLE : GENERATE_URL_SESSION_TITLE);
  194. ScriptSheet->TabVisible = !IsFileUrl();
  195. AssemblySheet->TabVisible = !IsFileUrl();
  196. bool HostKeyUnknown = FData->UsesSsh && FData->HostKey.IsEmpty();
  197. UnicodeString ResultGroupCaption;
  198. if (OptionsPageControl->ActivePage == UrlSheet)
  199. {
  200. ResultGroupCaption = LoadStr(GENERATE_URL_URL);
  201. }
  202. else if (OptionsPageControl->ActivePage == ScriptSheet)
  203. {
  204. UnicodeString ScriptDescription;
  205. if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  206. {
  207. ResultGroupCaption = LoadStr(GENERATE_URL_COMMANDLINE_LABEL);
  208. ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (ExeName)))) + L"\n";
  209. }
  210. else
  211. {
  212. ResultGroupCaption = ScriptFormatCombo->Items->Strings[ScriptFormatCombo->ItemIndex];
  213. }
  214. if (HostKeyUnknown)
  215. {
  216. ScriptDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  217. }
  218. ScriptDescriptionLabel->Caption = ScriptDescription;
  219. }
  220. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  221. {
  222. ResultGroupCaption = LoadStr(GENERATE_URL_CODE);
  223. UnicodeString AssemblyDescription;
  224. if (HostKeyUnknown)
  225. {
  226. AssemblyDescription += LoadStr(GENERATE_URL_HOSTKEY_UNKNOWN) + L"\n";
  227. }
  228. AssemblyDescriptionLabel->Caption = AssemblyDescription;
  229. }
  230. ResultGroup->Caption = ResultGroupCaption;
  231. EnableControl(UserNameCheck, !FData->UserNameExpanded.IsEmpty());
  232. bool UserNameIncluded = UserNameCheck->Enabled && UserNameCheck->Checked;
  233. EnableControl(PasswordCheck, UserNameIncluded && FData->HasPassword());
  234. EnableControl(HostKeyCheck, UserNameIncluded && !FData->HostKey.IsEmpty());
  235. EnableControl(RemoteDirectoryCheck, !FData->RemoteDirectory.IsEmpty() && !IsFileUrl());
  236. EnableControl(SaveExtensionCheck, !IsFileUrl());
  237. UnicodeString Result;
  238. bool WordWrap = false;
  239. bool FixedWidth = false;
  240. if (OptionsPageControl->ActivePage == UrlSheet)
  241. {
  242. if (!IsFileUrl())
  243. {
  244. UnicodeString Path = FData->RemoteDirectory;
  245. if (!Path.IsEmpty() && !EndsStr(L"/", Path))
  246. {
  247. Path += L"/";
  248. }
  249. Result = RtfText(GenerateUrl(Path));
  250. }
  251. else
  252. {
  253. for (int Index = 0; Index < FPaths->Count; Index++)
  254. {
  255. UnicodeString Url = GenerateUrl(FPaths->Strings[Index]);
  256. Result += RtfText(Url) + RtfPara;
  257. }
  258. }
  259. WordWrap = true;
  260. }
  261. else if (OptionsPageControl->ActivePage == ScriptSheet)
  262. {
  263. UnicodeString BaseExeName = ExtractFileBaseName(ExeName);
  264. UnicodeString OpenCommand = FData->GenerateOpenCommandArgs();
  265. UnicodeString CommandPlaceholder1 = FMTLOAD(GENERATE_URL_COMMAND, (1));
  266. UnicodeString CommandPlaceholder2 = FMTLOAD(GENERATE_URL_COMMAND, (2));
  267. UnicodeString LogParameter =
  268. RtfCommandlineSwitch(LOG_SWITCH, L"logging") + RtfText(L"=") +
  269. RtfScriptPlaceholder(L"\"" + LoadStr(GENERATE_URL_WRITABLE_PATH_TO_LOG) + RtfText(BaseExeName + L".log") + L"\"");
  270. UnicodeString IniParameter =
  271. RtfCommandlineSwitch(INI_SWITCH, L"configuration") + RtfText(UnicodeString(L"=") + INI_NUL);
  272. UnicodeString CommandParameter = RtfCommandlineSwitch(COMMAND_SWITCH, L"scripting");
  273. if (ScriptFormatCombo->ItemIndex == sfScriptFile)
  274. {
  275. Result =
  276. FORMAT(
  277. RtfScriptCommand(L"open") + L" %s" + RtfPara +
  278. RtfPara +
  279. RtfScriptComment("# %s") + RtfPara +
  280. RtfScriptComment("# %s") + RtfPara +
  281. RtfPara +
  282. RtfScriptCommand(L"exit") + RtfPara,
  283. (OpenCommand, CommandPlaceholder1, CommandPlaceholder2));
  284. WordWrap = false;
  285. FixedWidth = true;
  286. }
  287. else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
  288. {
  289. UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
  290. Result =
  291. RtfScriptPlaceholder(L"@echo off") + RtfPara +
  292. RtfPara +
  293. RtfText(L"\"" + ComExeName + "\" ^") + RtfPara +
  294. RtfText(L" ") + LogParameter + L" " + IniParameter + RtfText(L" ^") + RtfPara +
  295. RtfText(L" ") + CommandParameter + RtfText(L" ^") + RtfPara +
  296. RtfText(L" \"") + RtfScriptCommand(L"open") + RtfText(L" ") + EscapeParam(ReplaceStr(OpenCommand, L"%", L"%%")) + RtfText(L"\" ^") + RtfPara +
  297. RtfText(L" \"") + RtfScriptPlaceholder(CommandPlaceholder1) + RtfText(L"\" ^") + RtfPara +
  298. RtfText(L" \"") + RtfScriptPlaceholder(CommandPlaceholder2) + RtfText(L"\" ^") + RtfPara +
  299. RtfText(L" \"") + RtfScriptCommand(L"exit") + RtfText(L"\"") + RtfPara +
  300. RtfPara +
  301. RtfKeyword(L"set") + RtfText(L" WINSCP_RESULT=%ERRORLEVEL%") + RtfPara +
  302. RtfKeyword(L"if") + RtfText(L" %WINSCP_RESULT% ") + RtfKeyword(L"equ") + RtfText(L" 0 (") + RtfPara +
  303. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Success") + RtfPara +
  304. RtfText(L") ") + RtfKeyword(L"else") + RtfText(L" (") + RtfPara +
  305. RtfText(L" ") + RtfKeyword(L"echo") + RtfText(L" Error") + RtfPara +
  306. RtfText(L")") + RtfPara +
  307. RtfPara +
  308. RtfKeyword(L"exit") + RtfText(L" /b %WINSCP_RESULT%") + RtfPara;
  309. WordWrap = false;
  310. FixedWidth = true;
  311. }
  312. else if (ScriptFormatCombo->ItemIndex == sfCommandLine)
  313. {
  314. Result =
  315. LogParameter + L" " +
  316. IniParameter + L" " +
  317. CommandParameter + L" " +
  318. RtfText(L"\"") + RtfScriptCommand(L"open") + RtfText(L" ") + EscapeParam(OpenCommand) + RtfText(L"\" ") +
  319. RtfText(L"\"") + RtfScriptPlaceholder(CommandPlaceholder1) + RtfText(L"\" ") +
  320. RtfText(L"\"") + RtfScriptPlaceholder(CommandPlaceholder2) + RtfText(L"\" ") +
  321. RtfText(L"\"") + RtfScriptCommand(L"exit") + RtfText(L"\"");
  322. WordWrap = true;
  323. FixedWidth = false;
  324. }
  325. }
  326. else if (DebugAlwaysTrue(OptionsPageControl->ActivePage == AssemblySheet))
  327. {
  328. Result = FData->GenerateAssemblyCode(static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex));
  329. WordWrap = false;
  330. FixedWidth = true;
  331. }
  332. if (FixedWidth)
  333. {
  334. FResultMemo41->Font->Name = CustomWinConfiguration->DefaultFixedWidthFontName;
  335. }
  336. else
  337. {
  338. FResultMemo41->ParentFont = true;
  339. }
  340. Result =
  341. L"{\\rtf1\n"
  342. "{\\colortbl ;" +
  343. // The same RGB as on wiki
  344. RtfColorEntry(0x010101) + // near-black fake color to be used with no-style link to ovreride the default blue underline
  345. RtfColorEntry(0x008000) + // code comment (green)
  346. RtfColorEntry(0x008080) + // class (teal)
  347. RtfColorEntry(0x800000) + // string (maroon)
  348. RtfColorEntry(0x0000FF) + // keyword (blue)
  349. RtfColorEntry(0x993333) + // command-line argument (reddish)
  350. RtfColorEntry(0x808080) + // script command (gray)
  351. L"}\n"
  352. "{\\fonttbl{\\f0\\fnil\\fcharset0 " + FResultMemo41->Font->Name + L";}}\n"
  353. "\\f0\\fs" + IntToStr(FResultMemo41->Font->Size * 2) + L" " +
  354. Result +
  355. "}";
  356. FResultMemo41->WordWrap = WordWrap;
  357. FResultMemo41->ScrollBars = WordWrap ? ssVertical : ssBoth;
  358. std::unique_ptr<TMemoryStream> Stream(new TMemoryStream());
  359. UTF8String ResultUtf = Result;
  360. Stream->Write(ResultUtf.c_str(), ResultUtf.Length());
  361. Stream->Position = 0;
  362. FResultMemo41->Lines->LoadFromStream(Stream.get(), TEncoding::UTF8);
  363. }
  364. }
  365. //---------------------------------------------------------------------------
  366. void __fastcall TGenerateUrlDialog::Execute()
  367. {
  368. int Components = WinConfiguration->GenerateUrlComponents;
  369. if (Components < 0)
  370. {
  371. Components = UserNameCheck->Tag | RemoteDirectoryCheck->Tag;
  372. }
  373. TGenerateUrlCodeTarget Target = WinConfiguration->GenerateUrlCodeTarget;
  374. {
  375. TAutoFlag ChangingFlag(FChanging);
  376. if (IsFileUrl())
  377. {
  378. OptionsPageControl->ActivePage = UrlSheet;
  379. }
  380. else
  381. {
  382. switch (Target)
  383. {
  384. case guctUrl:
  385. OptionsPageControl->ActivePage = UrlSheet;
  386. break;
  387. case guctScript:
  388. OptionsPageControl->ActivePage = ScriptSheet;
  389. break;
  390. case guctAssembly:
  391. OptionsPageControl->ActivePage = AssemblySheet;
  392. break;
  393. default:
  394. DebugFail();
  395. }
  396. }
  397. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  398. {
  399. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  400. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)))
  401. {
  402. CheckBox->Checked = FLAGSET(Components, CheckBox->Tag);
  403. }
  404. }
  405. ScriptFormatCombo->ItemIndex = WinConfiguration->GenerateUrlScriptFormat;
  406. AssemblyLanguageCombo->ItemIndex = WinConfiguration->GenerateUrlAssemblyLanguage;
  407. }
  408. UpdateControls();
  409. ShowModal();
  410. // Do not save the selection for files as the "URL" was selected implicitly
  411. if (!IsFileUrl())
  412. {
  413. if (OptionsPageControl->ActivePage == UrlSheet)
  414. {
  415. Target = guctUrl;
  416. }
  417. else if (OptionsPageControl->ActivePage == ScriptSheet)
  418. {
  419. Target = guctScript;
  420. }
  421. else if (OptionsPageControl->ActivePage == AssemblySheet)
  422. {
  423. Target = guctAssembly;
  424. }
  425. else
  426. {
  427. DebugFail();
  428. }
  429. WinConfiguration->GenerateUrlCodeTarget = Target;
  430. }
  431. if (Target == guctUrl)
  432. {
  433. Components = 0;
  434. for (int Index = 0; Index < UrlSheet->ControlCount; Index++)
  435. {
  436. TCheckBox * CheckBox = dynamic_cast<TCheckBox *>(UrlSheet->Controls[Index]);
  437. if (DebugAlwaysTrue((CheckBox != NULL) && (CheckBox->Tag != 0)) &&
  438. CheckBox->Checked)
  439. {
  440. Components |= CheckBox->Tag;
  441. }
  442. }
  443. WinConfiguration->GenerateUrlComponents = Components;
  444. }
  445. else if (Target == guctScript)
  446. {
  447. WinConfiguration->GenerateUrlScriptFormat = static_cast<TScriptFormat>(ScriptFormatCombo->ItemIndex);
  448. }
  449. else if (Target == guctAssembly)
  450. {
  451. WinConfiguration->GenerateUrlAssemblyLanguage = static_cast<TAssemblyLanguage>(AssemblyLanguageCombo->ItemIndex);
  452. }
  453. }
  454. //---------------------------------------------------------------------------
  455. void __fastcall TGenerateUrlDialog::ControlChange(TObject * /*Sender*/)
  456. {
  457. UpdateControls();
  458. }
  459. //---------------------------------------------------------------------------
  460. void __fastcall TGenerateUrlDialog::ClipboardButtonClick(TObject * /*Sender*/)
  461. {
  462. TInstantOperationVisualizer Visualizer;
  463. // Cannot read the text from FResultMemo41->Lines as TRichEdit (as opposite to TMemo)
  464. // breaks wrapped lines
  465. UnicodeString Text = FResultMemo41->Text;
  466. UnicodeString EOL = sLineBreak;
  467. int P = Pos(EOL, Text);
  468. // Trim the EOL of the only string, what CopyToClipbaord(FResultMemo41->Lines) would have done.
  469. // It probably never happens as rich edit does not return EOL on the last line.
  470. if (DebugAlwaysFalse(P == Text.Length() - EOL.Length() + 1))
  471. {
  472. Text.SetLength(Text.Length() - EOL.Length());
  473. }
  474. // Add trailing EOL, if there are multiple lines (see above)
  475. else if ((P > 0) && !EndsStr(EOL, Text))
  476. {
  477. Text += EOL;
  478. }
  479. // Remove all tags HYPERLINK "http://www.example.com"
  480. int Index = 1;
  481. while ((P = PosFrom(RtfHyperlinkFieldPrefix, Text, Index)) > 0)
  482. {
  483. int Index2 = P + RtfHyperlinkFieldPrefix.Length();
  484. UnicodeString RtfHyperlinkFieldSuffix = L"\" ";
  485. int P2 = PosFrom(RtfHyperlinkFieldSuffix, Text, Index2);
  486. if (P2 > 0)
  487. {
  488. Text.Delete(P, P2 - P + RtfHyperlinkFieldSuffix.Length());
  489. }
  490. else
  491. {
  492. Index = Index2;
  493. }
  494. }
  495. CopyToClipboard(Text);
  496. }
  497. //---------------------------------------------------------------------------
  498. void __fastcall TGenerateUrlDialog::HelpButtonClick(TObject * /*Sender*/)
  499. {
  500. FormHelp(this);
  501. }
  502. //---------------------------------------------------------------------------
  503. void __fastcall TGenerateUrlDialog::WMNCCreate(TWMNCCreate & Message)
  504. {
  505. // bypass TForm::WMNCCreate to prevent disabling "resize"
  506. // (which is done for bsDialog, see comments in CreateParams)
  507. DefaultHandler(&Message);
  508. }
  509. //---------------------------------------------------------------------------
  510. void __fastcall TGenerateUrlDialog::Dispatch(void * AMessage)
  511. {
  512. TMessage & Message = *reinterpret_cast<TMessage *>(AMessage);
  513. if (Message.Msg == WM_NCCREATE)
  514. {
  515. WMNCCreate(*reinterpret_cast<TWMNCCreate *>(AMessage));
  516. }
  517. else
  518. {
  519. TForm::Dispatch(AMessage);
  520. }
  521. }
  522. //---------------------------------------------------------------------------
  523. void __fastcall TGenerateUrlDialog::CreateParams(TCreateParams & Params)
  524. {
  525. TForm::CreateParams(Params);
  526. // Allow resizing of the window, even if this is bsDialog.
  527. // This makes it more close to bsSizeable, but bsSizeable cannot for some
  528. // reason receive focus, if window is shown atop non-main window
  529. // (like editor)
  530. Params.Style = Params.Style | WS_THICKFRAME;
  531. }
  532. //---------------------------------------------------------------------------
  533. void __fastcall TGenerateUrlDialog::ResultMemoContextPopup(TObject * Sender,
  534. TPoint & MousePos, bool & Handled)
  535. {
  536. MenuPopup(Sender, MousePos, Handled);
  537. }
  538. //---------------------------------------------------------------------------
  539. void __fastcall TGenerateUrlDialog::FormShow(TObject * /*Sender*/)
  540. {
  541. UpdateControls();
  542. }
  543. //---------------------------------------------------------------------------