int main(int argc, char *argv[]) { XtAppContext appc; Widget top, arrow, print, fsb; XPPrinterList plist; int nlist, i; Screen *pscreen; Window win; Arg al[10]; int ac; Display *pdpy; XPContext pctxt; top = XtVaAppInitialize(&appc, "drawingArea", NULL, 0, &argc, argv, fallback, NULL); arrow = XtVaCreateManagedWidget("Button1", xmArrowButtonWidgetClass, top, XmNarrowDirection, XmARROW_UP, NULL); XtRealizeWidget(top); pdpy = XtOpenDisplay(appc, /* Display String */ ":1", "print", "Print", NULL, 0, &argc, argv); if (!pdpy) { fprintf(stderr, "Cannot connect to :1\n"); exit(1); } plist = XpGetPrinterList(pdpy, "", &nlist); if (nlist == 0) { fprintf(stderr, "XpGetPrinterList() : No printers\n"); exit(1); } pctxt = XpCreateContext(pdpy, plist[0].name); XpFreePrinterList(plist); XpSetContext(pdpy, pctxt); pscreen = XpGetScreenOfContext(pdpy, pctxt); CreateWindow(top, pdpy, pctxt, pscreen); LessTifTestMainLoop(top); exit(0); }
/* * Two different cases : * 1. Quick print button * Parameter "wid" is a DtPrintSetupBox. * DtPrintFillSetupData fills the print_data structure. The caller must * free allocated memory locations by calling DtPrintFreeSetupData(). * The X printer connection is managed by the DtPrintSetupBox. * If printer_name is NULL then DtNprinterName is used. If DtNprinterName * differs from printer_name, then DtNprinterName is updated. * Destination and dest_info will be updated. * If a connection cannot be established then the DtPrintSetupBox will be * managed, showing an error dialog. The user can then dismiss that * dialog and use the DtPrintSetupBox to select another printer and * restart the printing operation. * 2. GUI-less operation * Parameter "wid" is NULL. * The X printer connection returned, including the print context, is managed * by the caller. * If printer_name is NULL then DtPrintFillSetupData will determine a printer * and set printer_name to this value. * Destination and dest_info will be updated. * The caller can free allocated memory by calling DtPrintFreeSetupData(). */ XtEnum DtPrintFillSetupData(Widget wid, DtPrintSetupData *print_data) { char *name = NULL, *list = NULL, *p, *q, *d, *z, *at; Display *dpy = NULL, *pdpy = NULL; XPPrinterList plist; int count; XtAppContext appc = NULL; String argv[] = { "this" }; int argc = 1; XPContext ctx; list = getenv("XPSERVERLIST"); if (wid == NULL) { /* GUI - less case (or the DtPrintSetupBox widget itself wants this). */ DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData(NULL)\n")); appc = XtCreateApplicationContext(); /* FIX ME ?? */ /* Get a printer name */ if (print_data && print_data->printer_name) name = print_data->printer_name; else if ((name = getenv("XPRINTER"))) ; else if ((name = getenv("PDPRINTER"))) ; else if ((name = getenv("LPDEST"))) ; else name = getenv("PRINTER"); if (! name) { DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData: no name\n")); /* * Destroying the application context here seems to kill test2 * XtDestroyApplicationContext(appc); */ return DtPRINT_NO_PRINTER; } DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData(name %s)\n", name)); /* Create an X printing connection */ if (print_data && print_data->print_display) dpy = print_data->print_display; /* Maybe the printer name is fully qualified */ if ((p = strchr(name, '@'))) { d = XtNewString(p+1); if ((q = strchr(d, ':')) == NULL) { d = XtRealloc(d, strlen(d)+4); strcat(d, ":0"); } /* Now d contains a host:display string, try to use it */ pdpy = XtOpenDisplay(appc, d, "", "", NULL, 0, &argc, argv); DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData opened display %p\n", pdpy)); } /* Check this display */ if (dpy) { plist = XpGetPrinterList(dpy, name, &count); if (count > 0) { pdpy = dpy; print_data->print_display = pdpy; ctx = XpCreateContext(pdpy, plist[0].name); XpSetContext(pdpy, ctx); print_data->print_context = ctx; if (print_data->printer_name) XtFree(print_data->printer_name); print_data->printer_name = XtNewString(plist[0].name); if (print_data->dest_info) XtFree(print_data->dest_info); print_data->dest_info = XtNewString(plist[0].desc); DEBUGOUT(_LtDebug(__FILE__, wid, "Got %d entries on this display\n", count)); } } /* Check the list of print servers */ if (list && ! pdpy) { DEBUGOUT(_LtDebug(__FILE__, wid, "Try XpServerlist [%s]\n", list)); z = XtMalloc(strlen(list)+5); /* Cut the list into pieces */ for (p=list; *p; p++) { while (*p && isspace(*p)) p++; if (*p == '\0') break; for (q=p, d=z; *q && !isspace(*q); q++) *(d++) = *q; *d = '\0'; /* Now z contains a piece of the list */ if (! strchr(z, ':')) strcat(z, ":0"); DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData try to open display <%s>\n", z)); pdpy = XtOpenDisplay(appc, z, "", "", NULL, 0, &argc, argv); if (! pdpy) { if (*q) p = q; else break; continue; } DEBUGOUT(_LtDebug(__FILE__, wid, "... dpy %p\n", pdpy)); plist = XpGetPrinterList(pdpy, name, &count); if (count > 0) { print_data->print_display = pdpy; ctx = XpCreateContext(pdpy, plist[0].name); XpSetContext(pdpy, ctx); print_data->print_context = ctx; if (print_data->printer_name) { XtFree(print_data->printer_name); } print_data->printer_name = XtNewString(plist[0].name); if (print_data->dest_info) { XtFree(print_data->dest_info); } print_data->dest_info = XtNewString(plist[0].desc); DEBUGOUT(_LtDebug(__FILE__, wid, "Got %d entries on display %s\n", count, z)); } if (*q) p=q; else break; } XtFree(z); } /* * Destroying the application context here seems to kill test2 * XtDestroyApplicationContext(appc); */ } else { /* wid is the DtPrintSetupBox */ DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData()\n")); appc = XtWidgetToApplicationContext(wid); if (print_data && print_data->printer_name) name = print_data->printer_name; else if (PSB_PrinterName(wid)) name = PSB_PrinterName(wid); /* This is - mostly - copied from above, there must be a better way */ /* Check the list of print servers */ if (list && ! pdpy) { DEBUGOUT(_LtDebug(__FILE__, wid, "Try XpServerlist [%s]\n", list)); z = XtMalloc(strlen(list)+5); /* Cut the list into pieces */ for (p=list; *p; p++) { while (*p && isspace(*p)) p++; if (*p == '\0') break; for (q=p, d=z; *q && !isspace(*q); q++) *(d++) = *q; *d = '\0'; /* Now z contains a piece of the list */ if (! strchr(z, ':')) strcat(z, ":0"); DEBUGOUT(_LtDebug(__FILE__, wid, "DtPrintFillSetupData try to open display <%s>\n", z)); pdpy = XtOpenDisplay(appc, z, "", "", NULL, 0, &argc, argv); if (! pdpy) { if (*q) p = q; else break; continue; } DEBUGOUT(_LtDebug(__FILE__, wid, "... dpy %p\n", pdpy)); plist = XpGetPrinterList(pdpy, name, &count); if (count > 0) { print_data->print_display = pdpy; if (print_data->print_context == (XPContext)0) { ctx = XpCreateContext(pdpy, plist[0].name); print_data->print_context = ctx; } else { ctx = print_data->print_context; } XpSetContext(pdpy, ctx); if (print_data->printer_name) { XtFree(print_data->printer_name); } print_data->printer_name = XtNewString(plist[0].name); if (print_data->dest_info) { XtFree(print_data->dest_info); } print_data->dest_info = XtNewString(plist[0].desc); DEBUGOUT(_LtDebug(__FILE__, wid, "Got %d entries on display %s\n", count, z)); } if (*q) p=q; else break; } XtFree(z); } } return DtPRINT_SUCCESS; }
int do_hello_world( int argc, char *argv[], const char *printername, const char *toFile, const char *sample_string ) { XtAppContext app; Widget toplevel, shell, print_shell, hello; long dpi_x = 0L, dpi_y = 0L; char fontname[256]; /* BUG: is this really big enougth ? */ XFontStruct *textFont; XmFontList textFontList; Cardinal n; Arg args[10]; MyPrintCallbackData mpcd; if( doPrint ) { /* Get printer, either by "name" (foobar) or "name@display" (foobar@gaja:5) */ if( XpuGetPrinter(printername, &pdpy, &pcontext) != 1 ) Error(("XpuGetPrinter failure.\n")); /* Configure the print context (paper size, title etc.) * We must do this before creating any Xt widgets - otherwise they will * make wrong assuptions about fonts, resultions etc. ... */ XpuSetJobTitle(pdpy, pcontext, "Simple Xprint XawPrintShell widget demo"); /* Configuration done, set the context */ XpSetContext(pdpy, pcontext); /* Get default printer resolution */ if( XpuGetResolution(pdpy, pcontext, &dpi_x, &dpi_y) != 1 ) { fprintf(stderr, "No default resolution for printer '%s'\n", printername); XpuClosePrinterDisplay(pdpy, pcontext); return(EXIT_FAILURE); } pscreen = XpGetScreenOfContext(pdpy, pcontext); } else { pdpy = XOpenDisplay(NULL); if( !pdpy ) Error(("XOpenDisplay failure.\n")); dpi_x = dpi_y = 0L; pscreen = XDefaultScreenOfDisplay(pdpy); } toplevel = XawOpenApplication(&app, pdpy, pscreen, "xpawprintshelldemo", "XpXawPrintShellDemo", applicationShellWidgetClass, &argc, argv); if( !toplevel ) Error(("XawOpenApplication failure.\n")); if( doPrint ) { n = 0; /* Override any geometry resource settings as XawPrintShell adjusts it's size * to the current page siue when |XawPrintLAYOUTMODE_DRAWABLEAREA| or * |XawPrintLAYOUTMODE_PAGESIZE| are used. */ XtSetArg(args[n], XtNgeometry, "+0+0"); n++; XtSetArg(args[n], XawNlayoutMode, XawPrintLAYOUTMODE_DRAWABLEAREA); n++; print_shell = XtCreatePopupShell("myprintshell", xawPrintShellWidgetClass, toplevel, args, n); /* we're mapping/unmapping at start/end page time */ XtSetMappedWhenManaged(print_shell, False); shell = print_shell; } else { shell = toplevel; } sprintf(fontname, "-adobe-courier-medium-r-normal--40-*-%ld-%ld-*-*-iso8859-1", dpi_x, dpi_y); textFont = XLoadQueryFont(pdpy, fontname); if( !textFont ) { sprintf(fontname, "-*-*-*-*-*-*-*-160-%ld-%ld-*-*-iso8859-1", dpi_x, dpi_y); textFont = XLoadQueryFont(pdpy, fontname); } if( !textFont ) Error(("XLoadQueryFont failure.\n")); textFontList = XmFontListCreate(textFont, XmSTRING_DEFAULT_CHARSET); n = 0; /* Make sure the cursor is off, current Xprt servers do not seem to like * blinking cursors that much... ;-/ */ XtSetArg(args[n], XmNcursorPositionVisible, False); n++; XtSetArg(args[n], XmNvalue, sample_string); n++; XtSetArg(args[n], XmNfontList, textFontList); n++; XtSetArg(args[n], XmNeditMode, XmMULTI_LINE_EDIT); n++; hello = XmCreateText(shell, "mytext", args, n); if( !hello ) Error(("XmCreateText failure.\n")); XtManageChild(hello); XtRealizeWidget(toplevel); XtRealizeWidget(shell); if( doPrint ) { int num_total_rows; short num_visible_rows; int num_pages; pdpy = XtDisplay(toplevel); pdrawable = XtWindow(toplevel); if( !pdpy || !pdrawable ) Error(("No display.\n")); /* Make sure that the Xt machinery is really using the right screen (assertion) */ if( XpGetScreenOfContext(XtDisplay(toplevel), pcontext) != XtScreen(toplevel) ) Error(("Widget's screen != print screen. BAD.\n")); /* Get number of rows visible per page and the number of total rows * in the whole text widget... */ n = 0; XtSetArg(args[n], XmNrows, &num_visible_rows); n++ ; XtSetArg(args[n], XmNtotalLines, &num_total_rows); n++ ; XtGetValues(hello, args, n); /* Take away one row to match the one-line overlapping used by the * "next-page" action proc */ num_visible_rows -= 1; /* Calculate the number of pages */ num_pages = (num_total_rows+num_visible_rows-1) / num_visible_rows; Log(("Printing %d pages (num_total_rows=%d, num_visible_rows=%d)...\n", num_pages, num_total_rows, num_visible_rows)); /* Prepare our own context data for the print shell callbacks */ mpcd.num_pages = num_pages; mpcd.printshell_content = hello; mpcd.num_visible_rows = num_visible_rows; mpcd.appcontext = app; /* Setup the print shell callbacks... */ XtAddCallback(print_shell, XawNpageSetupCallback, PrintOnePageCB, (XtPointer)&mpcd); XtAddCallback(print_shell, XawNstartJobCallback, PrintStartJobCB, NULL); XtAddCallback(print_shell, XawNendJobCallback, PrintEndJobCB, (XtPointer)&mpcd); /* ... and finally start the print job. */ if( toFile ) { printtofile_handle = XpuStartJobToFile(pdpy, pcontext, toFile); if( !printtofile_handle ) { perror("XpuStartJobToFile failure"); Error(("XpuStartJobToFile failure.")); } } else { XpuStartJobToSpooler(pdpy); } } XtAppMainLoop(app); if( doPrint ) { if( toFile ) { if( XpuWaitForPrintFileChild(printtofile_handle) != XPGetDocFinished ) { fprintf(stderr, "%s: Error while printing to file.\n", ProgramName); } } PrintSpoolerCommandResults(pdpy, pcontext); /* We have to use XpDestroyContext() and XtCloseDisplay() instead * of XpuClosePrinterDisplay() to make libXt happy... */ if( pcontext != None ) XpDestroyContext(pdpy, pcontext); XtCloseDisplay(pdpy); } return EXIT_SUCCESS; }