diff --git a/include/Mcde/McdeSys.h b/include/Mcde/McdeSys.h index 53c1fb81..d65d97f5 100644 --- a/include/Mcde/McdeSys.h +++ b/include/Mcde/McdeSys.h @@ -732,6 +732,11 @@ int mcde_sys_printers(McdeSysPrinter *list, size_t max); /* the default queue for new jobs (root: needs the administrator) */ int mcde_sys_printer_default(const char *name); +/* Spool PATH to PRINTER (NULL or "" = the default queue) with lp(1), as a + * job for the event loop (mcde_sys_job_fd/_finish/_cancel). MCDE_PRINT_COMMAND + * names the tool instead, for tests with a fake lp. */ +McdeSysJob *mcde_sys_print_file(const char *path, const char *printer); + /* a queue for URI with the driverless model (lpadmin -p NAME -E -v URI -m * everywhere); root, a name without spaces or slashes, an ipp:// URI */ int mcde_sys_printer_add(const char *name, const char *uri); diff --git a/lib/McdeSys/printers.c b/lib/McdeSys/printers.c index 4a9dc86f..909fff99 100644 --- a/lib/McdeSys/printers.c +++ b/lib/McdeSys/printers.c @@ -142,6 +142,39 @@ mcde_sys_printer_default(const char *name) return MCDE_SYS_OK; } +McdeSysJob * +mcde_sys_print_file(const char *path, const char *printer) +{ + static const char *const where[] = { "/usr/bin/lp", + "/usr/local/bin/lp", "/usr/sbin/lp", MCDE_PKG_PREFIX "/bin/lp", + NULL }; + const char *lp = getenv("MCDE_PRINT_COMMAND"); + char *argv[5]; + int n = 0; + + if (path == NULL || path[0] == '\0' || access(path, R_OK) != 0) { + (void) mcde_sys_fail(MCDE_SYS_ENOTSUP, + mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), + path != NULL ? path : ""); + return NULL; + } + if (lp == NULL) + lp = Tool(where); + if (lp == NULL) { + (void) mcde_sys_fail(MCDE_SYS_ENOTSUP, + mcde_sys_msg(MSG_NOT_THERE, "%s is not there."), "lp"); + return NULL; + } + argv[n++] = (char *) lp; + if (printer != NULL && printer[0] != '\0') { + argv[n++] = (char *) "-d"; + argv[n++] = (char *) printer; + } + argv[n++] = (char *) path; + argv[n] = NULL; + return mcde_sys_job_start(argv, NULL); +} + /* the checks every name and URI pass before they reach a command line */ static int NameOk(const char *name) diff --git a/programs/dtpaint/Makefile.am b/programs/dtpaint/Makefile.am index cd9b2359..c574bf77 100644 --- a/programs/dtpaint/Makefile.am +++ b/programs/dtpaint/Makefile.am @@ -5,7 +5,7 @@ bin_PROGRAMS = dtpaint dtpaint_CPPFLAGS = -I$(top_srcdir)/include \ -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" -dtpaint_LDADD = $(DTCLIENTLIBS) $(XTOOLLIB) +dtpaint_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XTOOLLIB) if SOLARIS dtpaint_LDADD += -lintl diff --git a/programs/dtpaint/dtpaint.c b/programs/dtpaint/dtpaint.c index a099f886..c4b9ec74 100644 --- a/programs/dtpaint/dtpaint.c +++ b/programs/dtpaint/dtpaint.c @@ -36,6 +36,8 @@ #include #include +#include + #include #include #include @@ -108,6 +110,7 @@ static const char *const toolNames[] = { }; static Display *dpy; +static XtAppContext appctx; static Screen *scr; static Visual *vis; static Colormap cmap; @@ -2146,7 +2149,7 @@ SwatchActivate(Widget w, XtPointer data, XtPointer call) /* ------------------------------------------------------------------ menus */ enum { - C_NEW, C_OPEN, C_SAVE, C_SAVEAS, C_EXIT, + C_NEW, C_OPEN, C_SAVE, C_SAVEAS, C_PRINT, C_EXIT, C_UNDO, C_CLEAR, C_CLEARSEL, C_SELALL, C_INVERT, C_FLIP, C_TOOLS, C_COLORS, C_STATUS, C_HELP, C_ABOUT, C_ZNORMAL, C_ZLARGE, C_ATTR, C_EDITCOLORS @@ -2189,10 +2192,10 @@ ShowError(int msgid, const char *path) } static void -ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb) +ShowText(const char *body, int titleId, const char *titleFb) { Widget d = XmCreateInformationDialog(top, "info", NULL, 0); - XmString s = Msg(bodyId, bodyFb); + XmString s = XmStringCreateLocalized((String) body); XtVaSetValues(d, XmNmessageString, s, NULL); XmStringFree(s); @@ -2205,6 +2208,68 @@ ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb) XtManageChild(d); } +static void +ShowInfo(int bodyId, const char *bodyFb, int titleId, const char *titleFb) +{ + ShowText(PaintMsg(bodyId, bodyFb), titleId, titleFb); +} + +/* ---------------------------------------------------------------- printing */ + +static McdeSysJob *printJob; +static char printPath[256]; + +static void +PrintDone(XtPointer cd, int *fd, XtInputId *id) +{ + McdeSysJob *job = (McdeSysJob *) cd; + int rc = mcde_sys_job_finish(job); + + (void) fd; + XtRemoveInput(*id); + if (rc != MCDE_SYS_OK) + ShowText(mcde_sys_error(), 51, "Print"); + unlink(printPath); + printJob = NULL; +} + +static void +PrintImage(void) +{ + McdeSysPrinter list[32]; + const char *printer = NULL; + int n, i; + + if (printJob != NULL) + return; + n = mcde_sys_printers(list, 32); + if (n <= 0) { + ShowInfo(50, "No printer is set up.", 51, "Print"); + return; + } + for (i = 0; i < n; i++) + if (list[i].is_default) { + printer = list[i].name; + break; + } + if (printer == NULL) + printer = list[0].name; + snprintf(printPath, sizeof printPath, "/tmp/dtpaint-%d.bmp", + (int) getpid()); + if (SaveBMP(printPath) != 0) { + ShowError(27, printPath); + return; + } + printJob = mcde_sys_print_file(printPath, printer); + if (printJob == NULL) { + unlink(printPath); + ShowText(mcde_sys_error(), 51, "Print"); + return; + } + XtAppAddInput(appctx, mcde_sys_job_fd(printJob), + (XtPointer) XtInputReadMask, PrintDone, printJob); +} + /* ------------------------------------------------- image size and colours */ static void @@ -2526,6 +2591,9 @@ Command(Widget w, XtPointer data, XtPointer call) case C_SAVEAS: FileDialog(1); break; + case C_PRINT: + PrintImage(); + break; case C_EXIT: MaybeDiscard(A_EXIT); break; @@ -2791,6 +2859,7 @@ main(int argc, char **argv) selftest = 1; top = XtVaAppInitialize(&app, "Dtpaint", NULL, 0, &argc, argv, NULL, NULL); + appctx = app; dpy = XtDisplay(top); (void) srand((unsigned) getpid()); InitColor(); @@ -2884,6 +2953,8 @@ main(int argc, char **argv) AddItem(m, PaintMsg(9, "Save"), "CtrlS", "Ctrl+S", C_SAVE, 0, True); AddItem(m, PaintMsg(10, "Save As..."), NULL, NULL, C_SAVEAS, 0, True); AddItem(m, NULL, NULL, NULL, 0, 1, True); + AddItem(m, PaintMsg(53, "Print..."), "CtrlP", "Ctrl+P", + C_PRINT, 0, True); AddItem(m, PaintMsg(11, "Exit"), "AltF4", "Alt+F4", C_EXIT, 0, True); m = AddMenu(menubarW, PaintMsg(2, "Edit")); AddItem(m, PaintMsg(12, "Undo"), "CtrlZ", "Ctrl+Z", C_UNDO, 0, True); diff --git a/programs/dtpaint/dtpaint.msg b/programs/dtpaint/dtpaint.msg index 8872bb6b..10b0964b 100644 --- a/programs/dtpaint/dtpaint.msg +++ b/programs/dtpaint/dtpaint.msg @@ -51,3 +51,6 @@ $set 1 47 Blue: 48 OK 49 Cancel +50 No printer is set up. +51 Print +53 Print... diff --git a/programs/localized/de_DE.UTF-8/msg/dtpaint.msg b/programs/localized/de_DE.UTF-8/msg/dtpaint.msg index 8aff9038..c89b23af 100644 --- a/programs/localized/de_DE.UTF-8/msg/dtpaint.msg +++ b/programs/localized/de_DE.UTF-8/msg/dtpaint.msg @@ -52,3 +52,6 @@ $set 1 47 Blau: 48 OK 49 Abbrechen +50 Es ist kein Drucker eingerichtet. +51 Drucken +53 Drucken...