From e6a3f4ad891ee4e5c798b60901b1b142c56a591a Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 20 Nov 2026 22:14:14 +0000 Subject: [PATCH] Add Copy, Capture and Print to the Windows terminals The terminal control keeps a selection: drag with the left button, double-click a word, Shift with the mouse when the remote takes it. Copy (Ctrl+Insert), Paste to Host (Shift+Insert) and Select All are in a new Edit menu and on the right button's menu, as HyperTerminal has them. capture.cpp adds Capture Text (the line's bytes to a file), Capture to Printer (its text printed when you stop), each with Pause, and Modem > Print (the screen or the selection). What the remote sends to its printer port (CSI 5 i) now goes to the printer; before, the app discarded it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRRekyRUhR43KbmGNXK --- README.md | 1 + docs/design.md | 35 +++ windows/res/app.rc | 16 ++ windows/sources.cmake | 3 +- windows/src/app.h | 30 ++ windows/src/capture.cpp | 586 +++++++++++++++++++++++++++++++++++++++ windows/src/main.cpp | 68 +++++ windows/src/remote.cpp | 8 +- windows/src/resource.h | 9 + windows/src/term.cpp | 5 +- windows/src/termview.cpp | 391 +++++++++++++++++++++++++- 11 files changed, 1136 insertions(+), 16 deletions(-) create mode 100644 windows/src/capture.cpp diff --git a/README.md b/README.md index 3fa69fb..5e15f81 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ libdmodemctl. Thus they operate in the same way. graphics, and the font of each machine (the SyncTERM copies of their ROMs, LGPL-2.0-or-later: `core/licenses/syncterm-fonts-LGPL-2.0.txt`) - **File transfers** (Windows app): send and receive files on the modem line or on a Telnet, rlogin or SSH login. The protocols are the same as the D-Modem app: ZMODEM (with Automatic Receive when the remote side starts sz, rz or kermit -s), YMODEM, XMODEM, XMODEM-1K, Kermit, Intel HEX, Motorola S-records and plain text. Use the Transfer menu, or the Send and Receive buttons of the Terminal page +- **Copy and capture** (Windows app): select text with the mouse (Shift and the mouse when the remote side uses the mouse), then copy it. Paste to Host, Select All, Capture Text (to a file), Capture to Printer, and Print (the screen or the selection). Text that the remote side sends to its printer port goes to the printer. Use the Edit and Transfer menus, or click the right mouse button on the terminal - **Settings**: each modem setting as a native control, from AT!M - **Call Stats**: the rate, modulation, error control and line figures of the last call (AT#UD, ATI11) - **Line settings**: a SIP account, a USB modem, or a modem that already runs (dmodemd, or a hardware modem such as the MultiTech MT9234MU) diff --git a/docs/design.md b/docs/design.md index 049b99f..a38f825 100644 --- a/docs/design.md +++ b/docs/design.md @@ -119,6 +119,41 @@ to fill the window, by whole pixels where they fit. | Linux | `termview.c`: a GtkDrawingArea drawn with Cairo and Pango (the app does not use VTE now) | | macOS | `TerminalView.swift`: an NSView drawn with Core Graphics | +### Copy, capture and print (Windows app) + +The Windows terminal control (`termview.cpp`) keeps a selection. The +selection uses the rows of `dmc_vt_cell` (a row below 0 is in the +scrollback). The selection moves up with its text when new lines push the +text into the scrollback. + +- Drag with the left mouse button to select. Double-click to select a word. + When the remote side uses the mouse, hold Shift. +- Copy (Ctrl+Insert, or the Edit menu, or the menu of the right mouse + button) puts the text on the clipboard. The text comes from + `dmc_vt_text_range`. The app removes the blanks at the end of each line. + The clipboard gets Unicode text and the text of the ANSI code page. +- Paste to Host (Shift+Insert) types the clipboard text. + +`capture.cpp` has the captures. Each capture is of one line (the modem or +the Remote login). The capture does not get the bytes of a file transfer. + +- Capture Text writes the bytes from the line to a file, as they come + (with the escape sequences). The app adds the bytes to the end of a file + that exists, as HyperTerminal does. `dmodem.ini` keeps the folder in the + `[capture]` section. +- Capture to Printer keeps the text from the line. The app removes the + escape sequences and the control characters (but not tabs, line ends and + form feeds). When you stop the capture, the app prints the text on the + default printer. +- Print (Modem menu) prints the screen or the selection, with the print + dialog. +- When the remote side prints (`CSI 5 i`, `dmc_vt_printed`), the app prints + the text on the default printer after the remote side stops for 3 + seconds. During Capture to Printer, this text goes into the capture. + +The app prints with GDI: Courier New at 10 points, with margins of half an +inch. The app wraps long lines, and a form feed starts a new page. + ## File transfers The file transfer protocols are in libdmodemctl (`core/src/xfer*.c`). They diff --git a/windows/res/app.rc b/windows/res/app.rc index e86145f..862fb4c 100644 --- a/windows/res/app.rc +++ b/windows/res/app.rc @@ -20,8 +20,17 @@ BEGIN MENUITEM "&Start or Stop", IDM_START MENUITEM "&Line Settings...", IDM_LINE MENUITEM SEPARATOR + MENUITEM "&Print...", IDM_FPRINT + MENUITEM SEPARATOR MENUITEM "E&xit", IDM_EXIT END + POPUP "&Edit" + BEGIN + MENUITEM "&Copy\tCtrl+Insert", IDM_EDCOPY + MENUITEM "&Paste to Host\tShift+Insert", IDM_EDPASTE + MENUITEM SEPARATOR + MENUITEM "Select &All", IDM_SELALL + END POPUP "&Transfer" BEGIN MENUITEM "&Send File...", IDM_SEND @@ -30,6 +39,13 @@ BEGIN MENUITEM "&Automatic Receive (ZMODEM, Kermit)", IDM_AUTORECV MENUITEM SEPARATOR MENUITEM "&Cancel Transfer", IDM_XCANCEL + MENUITEM SEPARATOR + MENUITEM "Capture &Text...", IDM_CAPTURE + MENUITEM "Capture Text: Pa&use", IDM_CAPPAUSE + MENUITEM "Capture Text: St&op", IDM_CAPSTOP + MENUITEM SEPARATOR + MENUITEM "Capture to &Printer", IDM_CAPPRINT + MENUITEM "Capture to Printer: Pau&se", IDM_PRNPAUSE END POPUP "&Help" BEGIN diff --git a/windows/sources.cmake b/windows/sources.cmake index b01df64..8d4fc31 100644 --- a/windows/sources.cmake +++ b/windows/sources.cmake @@ -12,5 +12,6 @@ set(DMODEM_WIN_SOURCES audio.cpp termview.cpp remote.cpp - xfer.cpp) + xfer.cpp + capture.cpp) list(TRANSFORM DMODEM_WIN_SOURCES PREPEND "${CMAKE_CURRENT_LIST_DIR}/src/") diff --git a/windows/src/app.h b/windows/src/app.h index 9462a16..724e4b0 100644 --- a/windows/src/app.h +++ b/windows/src/app.h @@ -183,6 +183,17 @@ void termview_reset(HWND w); void termview_size(HWND w, int *cols, int *rows); /* what Backspace sends (DEL unless set) */ void termview_set_backspace(HWND w, char b); +/* the selection (drag with the left button, double-click a word): to + the clipboard; the clipboard's text typed; all the lines selected */ +bool termview_has_selection(HWND w); +bool termview_copy(HWND w); +void termview_paste(HWND w); +void termview_select_all(HWND w); +/* the selection's text, or the screen's (UTF-8, malloc'ed) */ +char *termview_text(HWND w, bool selection); +/* what the remote sends to its printer (UTF-8) goes to fn */ +typedef void (*TermPrintedFn)(const char *data, int n, void *ctx); +void termview_set_printer(HWND w, TermPrintedFn fn, void *ctx); /* the kind of terminal it is (dmc_vt_set_profile()'s id) */ void termview_set_profile(HWND w, const char *id); const char *termview_profile(HWND w); @@ -219,6 +230,25 @@ void xfer_set_auto(bool on); /* the transfer's dialog's keys (IsDialogMessage) */ bool xfer_dialog_key(MSG *m); +/* capture.cpp: Capture Text, Capture to Printer and Print, for a line */ +enum { CAPTURE_OFF, CAPTURE_ON, CAPTURE_PAUSED }; +/* what came on the line for its terminal (not a transfer's bytes) */ +void capture_feed(XferLine *l, const char *data, int n); +/* the remote's printing (a TermPrintedFn; ctx its XferLine) */ +void capture_printed(const char *data, int n, void *ctx); +/* the remote's printing printed once it stops (the main window's timer) */ +void capture_tick(); +void capture_text_start(XferLine *l); +void capture_text_stop(XferLine *l); +void capture_text_pause(XferLine *l, bool pause); +int capture_text_state(XferLine *l); +void capture_printer(XferLine *l, bool on); +void capture_printer_pause(XferLine *l, bool pause); +int capture_printer_state(XferLine *l); +/* the screen or the selection to a printer (the print dialog) */ +void capture_print(XferLine *l); +void capture_close_all(); + /* remote.cpp: the Remote page, a login by SSH, Telnet or rlogin */ HWND remote_page_new(HWND parent); void remote_page_layout(HWND page, int w, int h); diff --git a/windows/src/capture.cpp b/windows/src/capture.cpp new file mode 100644 index 0000000..f61fb92 --- /dev/null +++ b/windows/src/capture.cpp @@ -0,0 +1,586 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (C) 2026 D-Modem Authors + * + * capture.cpp - what comes on a terminal's line kept, as HyperTerminal's + * Transfer menu has it: Capture Text (the bytes to a file, + * as they come) and Capture to Printer (the text to the + * printer when you stop), each with Pause and Resume. Also + * File > Print (the screen or the selection), and the + * remote's own printing (its printer port: the VT's media + * copy sequences), printed when it stops for some seconds. + * + * A capture is of one line (term_line or remote_line, as xfer.cpp has + * them), and a transfer's bytes are not in it. The capture folder is + * kept in dmodem.ini ([capture] folder). + */ +#include +#include +#include + +#include "app.h" +#include "resource.h" + +#include + +#ifndef OFN_EXPLORER +#define OFN_EXPLORER 0x00080000 +#endif + +/* the remote's printing is printed once it is quiet for this long */ +#define REMOTE_QUIET_MS 3000 + +/* text for the printer, as UTF-16 */ +struct WText { + WCHAR *p; + int n, cap; +}; + +static void wt_add(WText *t, WCHAR c) +{ + if (t->n + 1 >= t->cap) { + int cap = t->cap ? t->cap * 2 : 4096; + WCHAR *p = (WCHAR *)realloc(t->p, (size_t)cap * sizeof(WCHAR)); + if (!p) + return; + t->p = p; + t->cap = cap; + } + t->p[t->n++] = c; + t->p[t->n] = 0; +} + +static void wt_free(WText *t) +{ + free(t->p); + t->p = NULL; + t->n = t->cap = 0; +} + +static void wt_add_cp(WText *t, unsigned long c) +{ + if (c >= 0x10000) { + c -= 0x10000; + wt_add(t, (WCHAR)(0xd800 + (c >> 10))); + wt_add(t, (WCHAR)(0xdc00 + (c & 0x3ff))); + } else { + wt_add(t, (WCHAR)c); + } +} + +/* UTF-8 text, LF for each line end */ +static void wt_add_utf8(WText *t, const char *s, int n) +{ + const unsigned char *p = (const unsigned char *)s, *e = p + n; + while (p < e) { + unsigned long c = *p++; + int more = 0; + if (c >= 0xf0) { + c &= 7; + more = 3; + } else if (c >= 0xe0) { + c &= 15; + more = 2; + } else if (c >= 0xc0) { + c &= 31; + more = 1; + } + while (more-- > 0 && p < e && (*p & 0xc0) == 0x80) + c = (c << 6) | (*p++ & 0x3f); + if (c == '\r') + continue; + wt_add_cp(t, c); + } +} + +/* the bytes of the line as text: the escape sequences and controls + dropped (but tabs, line ends and form feeds), UTF-8 where it is + UTF-8, else the OEM code page (CP437 on a US Windows, as BBSes) */ +struct TextFilter { + int esc; /* 0 none, 1 after ESC, 2 in CSI, 3 in a string */ + bool str_esc; /* an ESC in a string (ST next) */ + char csi[16]; /* the CSI's parameters */ + int csin; + bool printer; /* in the remote's printer mode (CSI 5 i): the + printer's text, which capture_printed() gets */ + unsigned char u[4]; + int un, uwant; /* a UTF-8 sequence being read */ +}; + +static void oem_char(WText *t, unsigned char b) +{ + WCHAR w; + char c = (char)b; + if (MultiByteToWideChar(CP_OEMCP, 0, &c, 1, &w, 1) == 1) + wt_add(t, w); + else + wt_add(t, '?'); +} + +static void filter_bytes(TextFilter *f, WText *t, const char *data, int n) +{ + for (int i = 0; i < n; i++) { + unsigned char b = (unsigned char)data[i]; + if (f->uwant) { + if ((b & 0xc0) == 0x80) { + f->u[f->un++] = b; + if (f->un == f->uwant) { + unsigned long c = f->u[0] & + (f->uwant == 2 ? 31 : f->uwant == 3 ? 15 : 7); + for (int k = 1; k < f->un; k++) + c = (c << 6) | (f->u[k] & 0x3f); + wt_add_cp(t, c); + f->uwant = f->un = 0; + } + continue; + } + /* not UTF-8 after all: the bytes as the OEM's */ + for (int k = 0; k < f->un; k++) + oem_char(t, f->u[k]); + f->uwant = f->un = 0; + } + if (f->esc == 1) { + f->csin = 0; + f->esc = b == '[' ? 2 : + (b == ']' || b == 'P' || b == '_' || b == '^' || + b == 'X') ? 3 : 0; + continue; + } + if (f->esc == 2) { + if (b >= 0x40 && b <= 0x7e) { + f->esc = 0; + f->csi[f->csin] = '\0'; + if (b == 'i' && !strcmp(f->csi, "5")) + f->printer = true; + else if (b == 'i' && !strcmp(f->csi, "4")) + f->printer = false; + } else if (f->csin < (int)sizeof(f->csi) - 1) { + f->csi[f->csin++] = (char)b; + } + continue; + } + if (f->esc == 3) { + if (b == 7 || (f->str_esc && b == '\\')) + f->esc = 0; + f->str_esc = b == 0x1b; + continue; + } + if (b == 0x1b) { + f->esc = 1; + continue; + } + if (f->printer) + continue; + switch (b) { + case '\n': + case '\t': + case '\f': + wt_add(t, b); + continue; + case 0x08: + if (t->n > 0 && t->p[t->n - 1] != '\n') + t->p[--t->n] = 0; + continue; + } + if (b < 0x20 || b == 0x7f) + continue; + if (b < 0x80) { + wt_add(t, b); + } else if (b >= 0xc2 && b <= 0xf4) { + f->u[0] = b; + f->un = 1; + f->uwant = b >= 0xf0 ? 4 : b >= 0xe0 ? 3 : 2; + } else { + oem_char(t, b); + } + } +} + +/* ------------------------------------------------------------------ */ +/* printing */ + +/* the printer's DC: the print dialog, or the default printer with no + dialog; *selection: the user chose to print the selection */ +static HDC printer_dc(bool ask, bool has_selection, bool *selection) +{ + PRINTDLG pd; + memset(&pd, 0, sizeof(pd)); + pd.lStructSize = sizeof(pd); + pd.hwndOwner = app.main; + pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | + (has_selection ? PD_SELECTION : PD_NOSELECTION) | + (ask ? 0 : PD_RETURNDEFAULT); + pd.nCopies = 1; + if (!PrintDlg(&pd)) + return NULL; + if (selection) + *selection = (pd.Flags & PD_SELECTION) != 0; + if (pd.hDevMode) + GlobalFree(pd.hDevMode); + if (pd.hDevNames) + GlobalFree(pd.hDevNames); + return pd.hDC; +} + +/* text on pages: a fixed font of 10 points, margins of half an inch, + long lines wrapped, a form feed a new page; false if it failed */ +static bool print_text(HDC dc, const char *title, const WCHAR *text, int n) +{ + int dpix = GetDeviceCaps(dc, LOGPIXELSX), dpiy = GetDeviceCaps(dc, LOGPIXELSY); + int pw = GetDeviceCaps(dc, HORZRES), ph = GetDeviceCaps(dc, VERTRES); + if (dpix <= 0 || dpiy <= 0 || pw <= 0 || ph <= 0) + return false; + HFONT font = CreateFont(-MulDiv(10, dpiy, 72), 0, 0, 0, FW_NORMAL, 0, 0, + 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, + CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, + FIXED_PITCH | FF_MODERN, "Courier New"); + HGDIOBJ old = SelectObject(dc, font); + TEXTMETRIC tm; + GetTextMetrics(dc, &tm); + int lh = tm.tmHeight + tm.tmExternalLeading, cw = tm.tmAveCharWidth; + if (lh <= 0) + lh = dpiy / 6; + if (cw <= 0) + cw = dpix / 12; + int mx = dpix / 2, my = dpiy / 2; + int cols = (pw - 2 * mx) / cw, rows = (ph - 2 * my) / lh; + if (cols < 20) + cols = 20; + if (rows < 5) + rows = 5; + DOCINFO di; + memset(&di, 0, sizeof(di)); + di.cbSize = sizeof(di); + di.lpszDocName = title; + bool ok = StartDoc(dc, &di) > 0; + if (ok) + ok = StartPage(dc) > 0; + int row = 0; + WCHAR line[512]; + int ln = 0, col = 0; + for (int i = 0; ok && i <= n; i++) { + WCHAR c = i < n ? text[i] : '\n'; + bool end = c == '\n' || c == '\f' || i == n; + if (c == '\t') { + do { + if (ln < (int)(sizeof(line) / sizeof(line[0]))) + line[ln++] = ' '; + col++; + } while (col % 8 && col < cols); + } else if (!end) { + if (ln < (int)(sizeof(line) / sizeof(line[0]))) + line[ln++] = c; + /* (a surrogate pair's first half takes no column) */ + if (c < 0xd800 || c >= 0xdc00) + col++; + } + if (end || col >= cols) { + if (row >= rows) { + ok = EndPage(dc) > 0 && StartPage(dc) > 0; + SelectObject(dc, font); + row = 0; + } + if (ln) + TextOutW(dc, mx, my + row * lh, line, ln); + row++; + ln = col = 0; + if (c == '\f' && ok) { + ok = EndPage(dc) > 0 && StartPage(dc) > 0; + SelectObject(dc, font); + row = 0; + } + } + } + if (ok) { + EndPage(dc); + EndDoc(dc); + } else { + AbortDoc(dc); + } + SelectObject(dc, old); + DeleteObject(font); + return ok; +} + +/* text to the default printer (no dialog) */ +static bool print_default(const char *title, const WText *t) +{ + HDC dc = printer_dc(false, false, NULL); + if (!dc) + return false; + bool ok = print_text(dc, title, t->p ? t->p : L"", t->n); + DeleteDC(dc); + return ok; +} + +/* ------------------------------------------------------------------ */ +/* the captures of a line */ + +struct Capture { + XferLine *line; + FILE *f; /* Capture Text's file, or NULL */ + bool f_paused; + char path[MAX_PATH]; + bool prn, prn_paused; /* Capture to Printer */ + TextFilter filter; + WText prn_text; + WText remote; /* what the remote printed */ + DWORD remote_at; /* when it last printed */ +}; + +static Capture caps[2]; + +static Capture *of(XferLine *l) +{ + if (!l) + return NULL; + for (int i = 0; i < 2; i++) + if (caps[i].line == l) + return &caps[i]; + for (int i = 0; i < 2; i++) + if (!caps[i].line) { + caps[i].line = l; + return &caps[i]; + } + return NULL; +} + +static void ini(char *out, int n) +{ + char dir[MAX_PATH]; + app_data_dir(dir, sizeof(dir)); + _snprintf(out, n, "%s" DIR_SEP "dmodem.ini", dir); + out[n - 1] = '\0'; +} + +/* the folder of the captures: the last one used, else Captures in the + app's folder */ +static void folder(char *out, int n) +{ + char path[MAX_PATH]; + ini(path, sizeof(path)); + GetPrivateProfileString("capture", "folder", "", out, n, path); + if (out[0]) + return; + char dir[MAX_PATH]; + app_data_dir(dir, sizeof(dir)); + _snprintf(out, n, "%s" DIR_SEP "Captures", dir); + out[n - 1] = '\0'; + CreateDirectory(out, NULL); +} + +void capture_feed(XferLine *l, const char *data, int n) +{ + Capture *c = of(l); + if (!c || n <= 0) + return; + if (c->f && !c->f_paused) { + if (fwrite(data, 1, (size_t)n, c->f) != (size_t)n) { + fclose(c->f); + c->f = NULL; + app_message("Capture Text stopped: the file cannot be written"); + } + } + if (c->prn && !c->prn_paused) + filter_bytes(&c->filter, &c->prn_text, data, n); +} + +void capture_printed(const char *data, int n, void *ctx) +{ + Capture *c = of((XferLine *)ctx); + if (!c) + return; + /* during Capture to Printer: in its text, where it came */ + if (c->prn && !c->prn_paused) { + wt_add_utf8(&c->prn_text, data, n); + return; + } + wt_add_utf8(&c->remote, data, n); + c->remote_at = GetTickCount(); +} + +void capture_tick() +{ + for (int i = 0; i < 2; i++) { + Capture *c = &caps[i]; + /* (the file as it comes, for a viewer of it) */ + if (c->f) + fflush(c->f); + if (!c->remote.n || + GetTickCount() - c->remote_at < REMOTE_QUIET_MS) + continue; + /* during Capture to Printer, with what it prints */ + if (c->prn) { + for (int k = 0; k < c->remote.n; k++) + wt_add(&c->prn_text, c->remote.p[k]); + } else if (print_default("D-Modem: printed by the remote", &c->remote)) { + app_message("The remote's printing went to the printer"); + } else { + app_message("The remote printed, but there is no printer"); + } + wt_free(&c->remote); + } +} + +/* Capture Text: a file to start it to (text added to one that exists) */ +void capture_text_start(XferLine *l) +{ + Capture *c = of(l); + if (!c || c->f) + return; + char dir[MAX_PATH], file[MAX_PATH]; + folder(dir, sizeof(dir)); + if (c->path[0]) + _snprintf(file, sizeof(file), "%s", c->path); + else + _snprintf(file, sizeof(file), "%s" DIR_SEP "capture.txt", dir); + file[sizeof(file) - 1] = '\0'; + OPENFILENAME o; + memset(&o, 0, sizeof(o)); + o.lStructSize = sizeof(o); + o.hwndOwner = app.main; + o.lpstrFilter = "Text (*.txt)\0*.txt\0ANSI (*.ans)\0*.ans\0" + "All files (*.*)\0*.*\0"; + o.lpstrFile = file; + o.nMaxFile = sizeof(file); + o.lpstrInitialDir = dir; + o.lpstrTitle = "Capture Text (added to the end of a file that exists)"; + o.lpstrDefExt = "txt"; + o.Flags = OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST; + if (!GetSaveFileName(&o)) + return; + c->f = fopen(file, "ab"); + if (!c->f) { + MessageBox(app.main, "D-Modem cannot open the file for the capture.", + "Capture Text", MB_OK | MB_ICONEXCLAMATION); + return; + } + c->f_paused = false; + _snprintf(c->path, sizeof(c->path), "%s", file); + c->path[sizeof(c->path) - 1] = '\0'; + /* the folder kept for the next time */ + char *slash = strrchr(file, DIR_SEP[0]); + if (slash) { + *slash = '\0'; + char path[MAX_PATH]; + ini(path, sizeof(path)); + WritePrivateProfileString("capture", "folder", file, path); + } + char m[MAX_PATH + 64]; + _snprintf(m, sizeof(m), "Capturing %s to %s", l->what, c->path); + m[sizeof(m) - 1] = '\0'; + app_message(m); +} + +void capture_text_stop(XferLine *l) +{ + Capture *c = of(l); + if (!c || !c->f) + return; + fclose(c->f); + c->f = NULL; + char m[MAX_PATH + 64]; + _snprintf(m, sizeof(m), "Capture Text stopped: %s", c->path); + m[sizeof(m) - 1] = '\0'; + app_message(m); +} + +void capture_text_pause(XferLine *l, bool pause) +{ + Capture *c = of(l); + if (c && c->f) { + c->f_paused = pause; + fflush(c->f); + app_message(pause ? "Capture Text paused" : "Capture Text resumed"); + } +} + +int capture_text_state(XferLine *l) +{ + Capture *c = of(l); + return !c || !c->f ? CAPTURE_OFF : c->f_paused ? CAPTURE_PAUSED : CAPTURE_ON; +} + +/* Capture to Printer: on, or off and the text printed */ +void capture_printer(XferLine *l, bool on) +{ + Capture *c = of(l); + if (!c || c->prn == on) + return; + if (on) { + c->prn = true; + c->prn_paused = false; + memset(&c->filter, 0, sizeof(c->filter)); + app_message("Capture to Printer started: the text prints when you stop"); + return; + } + c->prn = false; + if (c->prn_text.n) { + if (print_default("D-Modem: Capture to Printer", &c->prn_text)) + app_message("Capture to Printer stopped: the text went to the printer"); + else + MessageBox(app.main, "D-Modem cannot print the capture. " + "Make sure that a printer is installed.", + "Capture to Printer", MB_OK | MB_ICONEXCLAMATION); + } else { + app_message("Capture to Printer stopped: there was no text"); + } + wt_free(&c->prn_text); +} + +void capture_printer_pause(XferLine *l, bool pause) +{ + Capture *c = of(l); + if (c && c->prn) { + c->prn_paused = pause; + app_message(pause ? "Capture to Printer paused" : + "Capture to Printer resumed"); + } +} + +int capture_printer_state(XferLine *l) +{ + Capture *c = of(l); + return !c || !c->prn ? CAPTURE_OFF : + c->prn_paused ? CAPTURE_PAUSED : CAPTURE_ON; +} + +/* File > Print: the screen, or the selection */ +void capture_print(XferLine *l) +{ + HWND v = l && l->view ? *l->view : NULL; + if (!v) + return; + bool sel = false; + HDC dc = printer_dc(true, termview_has_selection(v), &sel); + if (!dc) { + /* (the user cancelled, or there is no printer) */ + if (CommDlgExtendedError()) + MessageBox(app.main, "D-Modem cannot find a printer.", + "Print", MB_OK | MB_ICONEXCLAMATION); + return; + } + char *s = termview_text(v, sel); + WText t = { NULL, 0, 0 }; + if (s) { + wt_add_utf8(&t, s, (int)strlen(s)); + free(s); + } + if (!print_text(dc, sel ? "D-Modem: selection" : "D-Modem: screen", + t.p ? t.p : L"", t.n)) + MessageBox(app.main, "D-Modem cannot print.", "Print", + MB_OK | MB_ICONEXCLAMATION); + DeleteDC(dc); + wt_free(&t); +} + +void capture_close_all() +{ + for (int i = 0; i < 2; i++) { + if (caps[i].f) + fclose(caps[i].f); + caps[i].f = NULL; + wt_free(&caps[i].prn_text); + wt_free(&caps[i].remote); + } +} diff --git a/windows/src/main.cpp b/windows/src/main.cpp index ea7b672..9175813 100644 --- a/windows/src/main.cpp +++ b/windows/src/main.cpp @@ -199,6 +199,16 @@ static XferLine *line_now() return page_now == PAGE_REMOTE ? &remote_line : &term_line; } +/* the terminal of the page shown, for the Edit menu: NULL on a page + with none */ +static HWND view_now() +{ + if (page_now != PAGE_TERM && page_now != PAGE_REMOTE) + return NULL; + XferLine *l = line_now(); + return l->view ? *l->view : NULL; +} + static void show_page(int i) { page_now = i; @@ -328,6 +338,39 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) case IDM_XCANCEL: xfer_cancel(); return 0; + case IDM_EDCOPY: + if (view_now()) + termview_copy(view_now()); + return 0; + case IDM_EDPASTE: + if (view_now()) + termview_paste(view_now()); + return 0; + case IDM_SELALL: + if (view_now()) + termview_select_all(view_now()); + return 0; + case IDM_FPRINT: + capture_print(line_now()); + return 0; + case IDM_CAPTURE: + capture_text_start(line_now()); + return 0; + case IDM_CAPPAUSE: + capture_text_pause(line_now(), + capture_text_state(line_now()) == CAPTURE_ON); + return 0; + case IDM_CAPSTOP: + capture_text_stop(line_now()); + return 0; + case IDM_CAPPRINT: + capture_printer(line_now(), + capture_printer_state(line_now()) == CAPTURE_OFF); + return 0; + case IDM_PRNPAUSE: + capture_printer_pause(line_now(), + capture_printer_state(line_now()) == CAPTURE_ON); + return 0; } break; case WM_INITMENUPOPUP: { @@ -338,6 +381,29 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) EnableMenuItem(m, IDM_XCANCEL, MF_BYCOMMAND | (busy ? MF_ENABLED : MF_GRAYED)); CheckMenuItem(m, IDM_AUTORECV, MF_BYCOMMAND | (xfer_auto() ? MF_CHECKED : MF_UNCHECKED)); + HWND v = view_now(); + UINT has_view = v ? MF_ENABLED : MF_GRAYED; + EnableMenuItem(m, IDM_EDCOPY, MF_BYCOMMAND | + (v && termview_has_selection(v) ? MF_ENABLED : MF_GRAYED)); + EnableMenuItem(m, IDM_EDPASTE, MF_BYCOMMAND | has_view); + EnableMenuItem(m, IDM_SELALL, MF_BYCOMMAND | has_view); + EnableMenuItem(m, IDM_FPRINT, MF_BYCOMMAND | has_view); + XferLine *l = line_now(); + int ct = capture_text_state(l), cp = capture_printer_state(l); + EnableMenuItem(m, IDM_CAPTURE, MF_BYCOMMAND | + (ct == CAPTURE_OFF ? MF_ENABLED : MF_GRAYED)); + EnableMenuItem(m, IDM_CAPPAUSE, MF_BYCOMMAND | + (ct != CAPTURE_OFF ? MF_ENABLED : MF_GRAYED)); + CheckMenuItem(m, IDM_CAPPAUSE, MF_BYCOMMAND | + (ct == CAPTURE_PAUSED ? MF_CHECKED : MF_UNCHECKED)); + EnableMenuItem(m, IDM_CAPSTOP, MF_BYCOMMAND | + (ct != CAPTURE_OFF ? MF_ENABLED : MF_GRAYED)); + CheckMenuItem(m, IDM_CAPPRINT, MF_BYCOMMAND | + (cp != CAPTURE_OFF ? MF_CHECKED : MF_UNCHECKED)); + EnableMenuItem(m, IDM_PRNPAUSE, MF_BYCOMMAND | + (cp != CAPTURE_OFF ? MF_ENABLED : MF_GRAYED)); + CheckMenuItem(m, IDM_PRNPAUSE, MF_BYCOMMAND | + (cp == CAPTURE_PAUSED ? MF_CHECKED : MF_UNCHECKED)); break; } case WM_TIMER: @@ -346,6 +412,7 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) else if (wp == TIMER_REMOTE) remote_tick(); xfer_pump(); + capture_tick(); return 0; case WM_APP_XFER: xfer_message(wp, lp); @@ -364,6 +431,7 @@ static LRESULT CALLBACK main_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) KillTimer(w, TIMER_VOICE); audio_stop(); remote_stop(); + capture_close_all(); if (link_running()) link_stop(); PostQuitMessage(0); diff --git a/windows/src/remote.cpp b/windows/src/remote.cpp index 71ba709..62e068a 100644 --- a/windows/src/remote.cpp +++ b/windows/src/remote.cpp @@ -172,13 +172,16 @@ XferLine remote_line = { "the remote server", &view, raw_write, line_room, static void show(const char *s, int n) { - if (!xfer_take(&remote_line, s, n)) + if (!xfer_take(&remote_line, s, n)) { + capture_feed(&remote_line, s, n); termview_feed(view, s, n); + } } +/* the app's own words on the screen (not captured) */ static void show_line(const char *s) { - show(s, (int)strlen(s)); + termview_feed(view, s, (int)strlen(s)); } static void ended(const char *why) @@ -1399,6 +1402,7 @@ HWND remote_page_new(HWND parent) login_btn = make_child("BUTTON", "&Login...", WS_TABSTOP, page, IDC_R_LOGIN); #endif view = termview_new(page, IDC_R_TERM, typed, NULL); + termview_set_printer(view, capture_printed, &remote_line); status_txt = make_child("STATIC", "Not connected", SS_LEFTNOWORDWRAP, page, IDC_R_STATUS); make_child("STATIC", "&Terminal:", SS_RIGHT, page, IDC_R_TYPELABEL); diff --git a/windows/src/resource.h b/windows/src/resource.h index 6f17647..0076c26 100644 --- a/windows/src/resource.h +++ b/windows/src/resource.h @@ -18,6 +18,15 @@ #define IDM_RECEIVE 205 #define IDM_AUTORECV 206 #define IDM_XCANCEL 207 +#define IDM_EDCOPY 208 +#define IDM_EDPASTE 209 +#define IDM_SELALL 210 +#define IDM_FPRINT 211 +#define IDM_CAPTURE 212 +#define IDM_CAPPAUSE 213 +#define IDM_CAPSTOP 214 +#define IDM_CAPPRINT 215 +#define IDM_PRNPAUSE 216 #define IDC_STATUS 300 #define IDC_TABS 301 diff --git a/windows/src/term.cpp b/windows/src/term.cpp index ccfad08..4959d5a 100644 --- a/windows/src/term.cpp +++ b/windows/src/term.cpp @@ -49,8 +49,10 @@ static void typed(const char *data, int n, void *ctx) void term_feed(const char *data, int n) { - if (term && !xfer_take(&term_line, data, n)) + if (term && !xfer_take(&term_line, data, n)) { + capture_feed(&term_line, data, n); termview_feed(term, data, n); + } } void term_state() @@ -121,6 +123,7 @@ HWND term_page_new(HWND parent) make_child("BUTTON", "&Receive...", WS_TABSTOP, page, IDC_TERM_RECV); make_child("BUTTON", "C&lear", WS_TABSTOP, page, IDC_CLEAR); term = termview_new(page, IDC_TERM, typed, NULL); + termview_set_printer(term, capture_printed, &term_line); /* the modem's configurator wants Backspace as BS */ termview_set_backspace(term, 0x08); char path[MAX_PATH], v[64]; diff --git a/windows/src/termview.cpp b/windows/src/termview.cpp index afc3245..c1c611e 100644 --- a/windows/src/termview.cpp +++ b/windows/src/termview.cpp @@ -46,6 +46,12 @@ struct TermView { int buttons; /* the mouse buttons down, for reports */ TermTypedFn fn; void *ctx; + /* the selection: from the anchor to the end, in rows as + dmc_vt_cell() has them (below 0 the scrollback) */ + bool sel, selecting; + int ar, ac, er, ec; + TermPrintedFn pfn; /* what the remote prints goes to it */ + void *pctx; HFONT fonts[4]; /* regular, bold, italic, bold italic */ /* a row being drawn */ HDC row_dc; @@ -227,6 +233,7 @@ static void layout(TermView *t) t->oy = 0; dmc_vt_set_cell_pixels(t->vt, cw, ch); if (cols != dmc_vt_cols(t->vt) || rows != dmc_vt_rows(t->vt)) { + t->sel = t->selecting = false; dmc_vt_resize(t->vt, cols, rows); update_scrollbar(t); } @@ -534,6 +541,253 @@ static void draw_row(TermView *t, int vrow, int ncols, bool cursor_here) } } +/* ------------------------------------------------------------------ */ +/* the selection */ + +/* the selection from its first cell to its last */ +static void sel_range(TermView *t, int *r0, int *c0, int *r1, int *c1) +{ + if (t->ar < t->er || (t->ar == t->er && t->ac <= t->ec)) { + *r0 = t->ar; *c0 = t->ac; *r1 = t->er; *c1 = t->ec; + } else { + *r0 = t->er; *c0 = t->ec; *r1 = t->ar; *c1 = t->ac; + } +} + +static void sel_clear(TermView *t) +{ + if (t->sel) + InvalidateRect(t->w, NULL, FALSE); + t->sel = t->selecting = false; +} + +/* the selection's cells of each row on the screen, inverted */ +static void paint_selection(TermView *t, HDC mem) +{ + if (!t->sel) + return; + int r0, c0, r1, c1; + sel_range(t, &r0, &c0, &r1, &c1); + int rows = dmc_vt_rows(t->vt), cols = dmc_vt_cols(t->vt); + for (int y = 0; y < rows; y++) { + int vrow = y + t->scroll; + if (vrow < r0 || vrow > r1) + continue; + int from = vrow == r0 ? c0 : 0, to = vrow == r1 ? c1 : cols - 1; + int k = dmc_vt_line_size(t->vt, vrow) == DMC_LINE_SINGLE ? 1 : 2; + int x0 = from * k * t->cw, x1 = (to + 1) * k * t->cw; + if (x1 > cols * t->cw) + x1 = cols * t->cw; + if (x1 > x0) + PatBlt(mem, t->ox + x0, t->oy + y * t->ch, x1 - x0, t->ch, + DSTINVERT); + } +} + +/* the blanks at the ends of the lines dropped, in place */ +static void trim_ends(char *s) +{ + char *o = s, *blank = NULL; + for (const char *p = s; *p; p++) { + if (*p == ' ') { + if (!blank) + blank = o; + } else if (*p == '\n') { + if (blank) + o = blank; + blank = NULL; + } else { + blank = NULL; + } + *o++ = *p; + } + if (blank) + o = blank; + *o = '\0'; +} + +/* UTF-8 to UTF-16 (malloc'ed, CR LF for each line end) */ +static WCHAR *wide_lines(const char *s, int *len) +{ + int n = 0; + for (const char *p = s; *p; p++) + n += *p == '\n' ? 2 : 1; + WCHAR *w = (WCHAR *)malloc(((size_t)n + 1) * sizeof(WCHAR)); + if (!w) + return NULL; + int k = 0; + const unsigned char *p = (const unsigned char *)s; + while (*p) { + unsigned long c = *p++; + int more = 0; + if (c >= 0xf0) { + c &= 7; + more = 3; + } else if (c >= 0xe0) { + c &= 15; + more = 2; + } else if (c >= 0xc0) { + c &= 31; + more = 1; + } + while (more-- > 0 && (*p & 0xc0) == 0x80) + c = (c << 6) | (*p++ & 0x3f); + if (c == '\n') + w[k++] = '\r'; + if (c >= 0x10000) { + c -= 0x10000; + w[k++] = (WCHAR)(0xd800 + (c >> 10)); + w[k++] = (WCHAR)(0xdc00 + (c & 0x3ff)); + } else { + w[k++] = (WCHAR)c; + } + } + w[k] = 0; + if (len) + *len = k; + return w; +} + +static HGLOBAL global_copy(const void *data, size_t n) +{ + HGLOBAL g = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, n); + if (!g) + return NULL; + void *p = GlobalLock(g); + memcpy(p, data, n); + GlobalUnlock(g); + return g; +} + +/* the selection's text to the clipboard: Unicode, and the ANSI code + page's for Windows 9x programs */ +static bool copy_selection(TermView *t) +{ + if (!t->sel) + return false; + int r0, c0, r1, c1; + sel_range(t, &r0, &c0, &r1, &c1); + const char *r = dmc_vt_text_range(t->vt, r0, c0, r1, c1); + char *text = (char *)malloc(strlen(r) + 1); + if (!text) + return false; + strcpy(text, r); + trim_ends(text); + int len = 0; + WCHAR *w = wide_lines(text, &len); + free(text); + if (!w) + return false; + bool ok = false; + if (OpenClipboard(t->w)) { + EmptyClipboard(); + HGLOBAL g = global_copy(w, ((size_t)len + 1) * sizeof(WCHAR)); + if (g && SetClipboardData(CF_UNICODETEXT, g)) + ok = true; + int an = WideCharToMultiByte(CP_ACP, 0, w, len + 1, NULL, 0, + NULL, NULL); + char *a = an > 0 ? (char *)malloc((size_t)an) : NULL; + if (a && WideCharToMultiByte(CP_ACP, 0, w, len + 1, a, an, NULL, + NULL) > 0) { + g = global_copy(a, (size_t)an); + if (g && SetClipboardData(CF_TEXT, g)) + ok = true; + } + free(a); + CloseClipboard(); + } + free(w); + return ok; +} + +static void select_all(TermView *t) +{ + t->ar = -dmc_vt_history(t->vt); + t->ac = 0; + t->er = dmc_vt_rows(t->vt) - 1; + t->ec = dmc_vt_cols(t->vt) - 1; + t->sel = true; + t->selecting = false; + InvalidateRect(t->w, NULL, FALSE); +} + +/* the cell under a point as a row of dmc_vt_cell(), in the screen */ +static void sel_cell(TermView *t, int x, int y, int *row, int *col) +{ + int rows = dmc_vt_rows(t->vt), cols = dmc_vt_cols(t->vt); + int r = (y - t->oy) / (t->ch > 0 ? t->ch : 1); + if (y < t->oy) + r = 0; + if (r >= rows) + r = rows - 1; + *row = r + t->scroll; + int k = dmc_vt_line_size(t->vt, *row) == DMC_LINE_SINGLE ? 1 : 2; + int c = (x - t->ox) / (t->cw * k > 0 ? t->cw * k : 1); + if (x < t->ox) + c = 0; + if (c >= cols) + c = cols - 1; + *col = c; +} + +/* the word (no blanks) under a point */ +static void select_word(TermView *t, int x, int y) +{ + int row, col, cols = dmc_vt_cols(t->vt); + sel_cell(t, x, y, &row, &col); + uint32_t c = dmc_vt_char(t->vt, row, col); + if (c == ' ' || c == 0) { + sel_clear(t); + return; + } + int a = col, b = col; + while (a > 0 && dmc_vt_char(t->vt, row, a - 1) != ' ') + a--; + while (b < cols - 1 && dmc_vt_char(t->vt, row, b + 1) != ' ') + b++; + t->ar = t->er = row; + t->ac = a; + t->ec = b; + t->sel = true; + InvalidateRect(t->w, NULL, FALSE); +} + +static void paste(TermView *t); + +/* the right button's menu, as HyperTerminal has it */ +static void context_menu(TermView *t, LPARAM lp) +{ + enum { CM_COPY = 1, CM_PASTE, CM_ALL }; + HMENU m = CreatePopupMenu(); + AppendMenu(m, MF_STRING | (t->sel ? 0 : MF_GRAYED), CM_COPY, + "&Copy\tCtrl+Insert"); + AppendMenu(m, MF_STRING | + (IsClipboardFormatAvailable(CF_TEXT) || + IsClipboardFormatAvailable(CF_UNICODETEXT) ? 0 : MF_GRAYED), + CM_PASTE, "&Paste to Host\tShift+Insert"); + AppendMenu(m, MF_SEPARATOR, 0, NULL); + AppendMenu(m, MF_STRING, CM_ALL, "Select &All"); + POINT p; + p.x = (short)LOWORD(lp); + p.y = (short)HIWORD(lp); + ClientToScreen(t->w, &p); + int cmd = (int)TrackPopupMenu(m, TPM_RETURNCMD | TPM_RIGHTBUTTON, + p.x, p.y, 0, t->w, NULL); + DestroyMenu(m); + switch (cmd) { + case CM_COPY: + copy_selection(t); + sel_clear(t); + break; + case CM_PASTE: + paste(t); + break; + case CM_ALL: + select_all(t); + break; + } +} + static void paint(TermView *t, HDC dc, RECT *area) { RECT r; @@ -574,6 +828,7 @@ static void paint(TermView *t, HDC dc, RECT *area) size == DMC_LINE_DOUBLE_TOP ? 0 : ch / 2, hw, ch / 2, SRCCOPY); } + paint_selection(t, mem); /* the status line under the screen */ if (t->status_rows) { const char *s = dmc_vt_status_line(vt); @@ -744,6 +999,10 @@ static int vk_key(WPARAM wp, int mods, TermView *t, HWND w) paste(t); return -1; } + if (mods == DMC_MOD_CTRL) { + copy_selection(t); + return -1; + } return DMC_KEY_INSERT; case VK_DELETE: return DMC_KEY_DELETE; case VK_TAB: return mods & DMC_MOD_SHIFT ? DMC_KEY_BACKTAB : 0; @@ -775,21 +1034,37 @@ static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) return 0; } case WM_LBUTTONDOWN: + case WM_LBUTTONDBLCLK: case WM_MBUTTONDOWN: { SetFocus(w); SetCapture(w); - int button = m == WM_LBUTTONDOWN ? 0 : 1; + int button = m == WM_MBUTTONDOWN ? 1 : 0; t->buttons |= 1 << button; - if (mouse_report(t, button, lp, 1, 0)) + /* (Shift: a selection, also when the remote takes the mouse) */ + bool shift = (wp & MK_SHIFT) != 0; + if (!shift && mouse_report(t, button, lp, 1, 0)) return 0; - if (m == WM_LBUTTONDOWN) { + if (button) + return 0; + int x = (short)LOWORD(lp), y = (short)HIWORD(lp); + if (m == WM_LBUTTONDBLCLK) { + select_word(t, x, y); + return 0; + } + sel_clear(t); + sel_cell(t, x, y, &t->ar, &t->ac); + t->er = t->ar; + t->ec = t->ac; + t->selecting = true; + if (!shift) { /* a RIPscrip button's */ int cols = dmc_vt_cols(t->vt), rows = dmc_vt_rows(t->vt); - double fx = ((short)LOWORD(lp) - t->ox) / (double)(cols * t->cw); - double fy = ((short)HIWORD(lp) - t->oy) / (double)(rows * t->ch); + double fx = (x - t->ox) / (double)(cols * t->cw); + double fy = (y - t->oy) / (double)(rows * t->ch); char b[256]; int n = dmc_vt_tap(t->vt, fx, fy, b, sizeof(b)); if (n > 0) { + t->selecting = false; to_bottom(t); send_text(t, b, n); } @@ -802,11 +1077,15 @@ static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) t->buttons &= ~(1 << button); if (!t->buttons) ReleaseCapture(); + if (!button && t->selecting) { + t->selecting = false; + return 0; + } mouse_report(t, button, lp, 0, 0); return 0; } case WM_RBUTTONDOWN: - if (mouse_report(t, 2, lp, 1, 0)) { + if (!(wp & MK_SHIFT) && mouse_report(t, 2, lp, 1, 0)) { t->buttons |= 4; return 0; } @@ -817,9 +1096,28 @@ static LRESULT CALLBACK view_proc(HWND w, UINT m, WPARAM wp, LPARAM lp) mouse_report(t, 2, lp, 0, 0); return 0; } - paste(t); + context_menu(t, lp); return 0; case WM_MOUSEMOVE: { + if (t->selecting) { + int x = (short)LOWORD(lp), y = (short)HIWORD(lp); + RECT r; + GetClientRect(w, &r); + /* above or under the screen: the scrollback scrolled */ + if (y < 0) + SendMessage(w, WM_VSCROLL, SB_LINEUP, 0); + else if (y >= r.bottom) + SendMessage(w, WM_VSCROLL, SB_LINEDOWN, 0); + int row, col; + sel_cell(t, x, y, &row, &col); + if (row != t->er || col != t->ec || !t->sel) { + t->er = row; + t->ec = col; + t->sel = t->er != t->ar || t->ec != t->ac || t->sel; + InvalidateRect(w, NULL, FALSE); + } + return 0; + } int b = t->buttons & 1 ? 0 : t->buttons & 2 ? 1 : t->buttons & 4 ? 2 : 3; mouse_report(t, b, lp, 1, 1); return 0; @@ -960,7 +1258,7 @@ HWND termview_new(HWND parent, int id, TermTypedFn fn, void *ctx) if (!registered) { WNDCLASS wc; memset(&wc, 0, sizeof(wc)); - wc.style = CS_HREDRAW | CS_VREDRAW; + wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; wc.lpfnWndProc = view_proc; wc.hInstance = app.inst; wc.hCursor = LoadCursor(NULL, IDC_IBEAM); @@ -994,17 +1292,26 @@ void termview_feed(HWND w, const char *data, int n) return; int before = dmc_vt_history(t->vt); dmc_vt_feed(t->vt, data, n); + int moved = dmc_vt_history(t->vt) - before; if (t->scroll) - t->scroll -= dmc_vt_history(t->vt) - before; + t->scroll -= moved; + /* the selection stays on its text as the lines go up */ + if (t->sel || t->selecting) { + t->ar -= moved; + t->er -= moved; + if (t->ar < -dmc_vt_history(t->vt) || t->er < -dmc_vt_history(t->vt)) + sel_clear(t); + } if (t->scroll < -dmc_vt_history(t->vt)) t->scroll = -dmc_vt_history(t->vt); char r[256]; int k; while ((k = dmc_vt_reply(t->vt, r, sizeof(r))) > 0) send_text(t, r, k); - /* (what went to the printer: not kept) */ - while (dmc_vt_printed(t->vt, r, sizeof(r)) > 0) - ; + /* what went to the printer: to the page's printer */ + while ((k = dmc_vt_printed(t->vt, r, sizeof(r))) > 0) + if (t->pfn) + t->pfn(r, k, t->pctx); if (dmc_vt_bells(t->vt)) MessageBeep(MB_OK); play_music(t->vt); @@ -1021,6 +1328,7 @@ void termview_reset(HWND w) return; dmc_vt_reset(t->vt); t->scroll = 0; + sel_clear(t); layout(t); update_scrollbar(t); InvalidateRect(w, NULL, FALSE); @@ -1077,3 +1385,62 @@ const char *term_types_chosen(HWND combo) const char *id = dmc_vt_profile_id(i); return id ? id : dmc_vt_profile_id(0); } + +bool termview_has_selection(HWND w) +{ + TermView *t = of(w); + return t && t->sel; +} + +bool termview_copy(HWND w) +{ + TermView *t = of(w); + return t && copy_selection(t); +} + +void termview_paste(HWND w) +{ + TermView *t = of(w); + if (t) + paste(t); +} + +void termview_select_all(HWND w) +{ + TermView *t = of(w); + if (t) + select_all(t); +} + +char *termview_text(HWND w, bool selection) +{ + TermView *t = of(w); + if (!t) + return NULL; + const char *s; + if (selection && t->sel) { + int r0, c0, r1, c1; + sel_range(t, &r0, &c0, &r1, &c1); + s = dmc_vt_text_range(t->vt, r0, c0, r1, c1); + } else { + s = dmc_vt_text_range(t->vt, t->scroll, 0, + t->scroll + dmc_vt_rows(t->vt) - 1, + dmc_vt_cols(t->vt) - 1); + } + size_t n = strlen(s); + char *c = (char *)malloc(n + 1); + if (c) { + memcpy(c, s, n + 1); + trim_ends(c); + } + return c; +} + +void termview_set_printer(HWND w, TermPrintedFn fn, void *ctx) +{ + TermView *t = of(w); + if (t) { + t->pfn = fn; + t->pctx = ctx; + } +}