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); }
/************************************************************************ * * Initialize * ************************************************************************/ static void Initialize( Widget req_widget, Widget new_widget, ArgList args, Cardinal *num_args ) { int event_base_return, error_base_return ; XmPrintShellWidget print_shell = (XmPrintShellWidget) new_widget ; XPContext pcontext ; Screen * pscreen ; _XmProcessLock(); /* mark the screen of this print shell in this context */ XSaveContext(XtDisplay(new_widget), (XID)XtScreen(new_widget), _XmPrintScreenToShellContext, (XPointer) new_widget); /* also maintain a counter of all shell */ _XmPrintShellCounter ++ ; _XmProcessUnlock(); print_shell->print.xp_connected = False ; print_shell->print.print_resolution = 100 ; /*** first check if the Print extension is present at all */ if (!XpQueryExtension (XtDisplay(new_widget), &event_base_return, &error_base_return)) { return ; } /* need to check that the print context is set for the screen of this print shell (i.e. it's a print screen, not just a screen on a display that support printing and for which a context was establish on a different screen - which can be common in preview case */ pcontext = XpGetContext(XtDisplay(new_widget)); if (!pcontext || XpGetScreenOfContext(XtDisplay(new_widget),pcontext) != XtScreen(new_widget)) { return ; } print_shell->print.xp_connected = True ; /*** add an element to a widget/XPContext table, used in the event dispatch mechanism */ AddToTable(pcontext, new_widget); /*** add extension event Handler for Start/End Job/Page Notify */ XtInsertEventTypeHandler(new_widget, event_base_return + XPPrintNotify, (XtPointer)XPPrintMask, PrintNotifyHandler, NULL, XtListTail); /*** also add extension event Handler for tracking attributes change */ XtInsertEventTypeHandler(new_widget, event_base_return + XPAttributeNotify, (XtPointer)XPAttributeMask, AttributesNotifyHandler, NULL, XtListTail); /* always register the extension selector, and the event dispatcher, it will override itself accordingly and works ok for different display connection */ XtRegisterExtensionSelector(XtDisplay(new_widget), event_base_return + XPPrintNotify, event_base_return + XPAttributeNotify, SelectXpEvents, NULL); (void) XtSetEventDispatcher(XtDisplay(new_widget), event_base_return + XPPrintNotify, PrintDispatchEvent); (void) XtSetEventDispatcher(XtDisplay(new_widget), event_base_return + XPAttributeNotify, PrintDispatchEvent); /* go and get resolution and initial sizes */ ResourcesUpdate(new_widget, pcontext, &(new_widget->core.width), &(new_widget->core.height)); }
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; }