#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "xlogo.h"
#include "Logo.h"
#include "print.h"
#include <stdio.h>
#include <stdlib.h>
static Widget
CreatePrintShell(Widget videoshell,
Screen *pscreen,
String printshell_name,
ArgList args,
Cardinal numargs)
{
String videoname,
videoclass;
Widget pappshell,
printshell;
Display *pdpy = XDisplayOfScreen(pscreen);
int dummyc = 0;
String dummys = "";
XtGetApplicationNameAndClass(XtDisplay(videoshell),
&videoname, &videoclass);
XtDisplayInitialize(XtWidgetToApplicationContext(videoshell), pdpy,
videoname, videoclass,
NULL, 0,
&dummyc, &dummys);
pappshell = XtVaAppCreateShell(videoname, videoclass,
applicationShellWidgetClass,
pdpy,
XtNscreen, pscreen,
NULL);
printshell = XtCreatePopupShell(printshell_name,
xawPrintShellWidgetClass,
pappshell, args, numargs);
XtSetMappedWhenManaged(printshell, False);
XtRealizeWidget(printshell);
return printshell;
}
typedef struct
{
Widget toplevel;
Bool isPrinting;
Widget printshell;
Display *pdpy;
Screen *pscreen;
XPContext pcontext;
void *printtofile_handle;
} AppPrintData;
static AppPrintData apdx;
static AppPrintData *apd = &apdx;
static void
PageSetupCB(Widget widget, XtPointer client_data, XtPointer call_data)
{
Widget pshell = widget;
XawPrintShellCallbackStruct *psp = (XawPrintShellCallbackStruct *)call_data;
AppPrintData *p = (AppPrintData *)client_data;
Log(("--> PageSetupCB\n"));
if (!psp->last_page_in_job) {
Widget plogo;
Log(("Creating print logo\n"));
plogo = XtCreateManagedWidget("xlogo", logoWidgetClass, pshell, NULL, ZERO);
if (XpGetScreenOfContext(XtDisplay(plogo), p->pcontext) != XtScreen(plogo))
Error(("Widget's screen != print screen. BAD.\n"));
psp->last_page_in_job = True;
}
}
void FinishPrinting(AppPrintData *p)
{
char *scr;
if (p->printtofile_handle) {
if (XpuWaitForPrintFileChild(p->printtofile_handle) != XPGetDocFinished) {
fprintf(stderr, "%s: Error while printing to file.\n", ProgramName);
}
p->printtofile_handle = NULL;
}
scr = XpGetOneAttribute(p->pdpy, p->pcontext, XPJobAttr, "xp-spooler-command-results");
if( scr )
{
if( strlen(scr) > 0 )
{
const char *msg = XpuCompoundTextToXmb(p->pdpy, scr);
if( msg )
{
Msg(("Spooler command returned '%s'.\n", msg));
XpuFreeXmbString(msg);
}
else
{
Msg(("Spooler command returned '%s' (unconverted).\n", scr));
}
}
XFree((void *)scr);
}
if (p->printshell) {
XtDestroyWidget(p->printshell);
p->printshell = NULL;
}
if (p->pdpy) {
if (p->pcontext != None)
XpDestroyContext(p->pdpy, p->pcontext);
XtCloseDisplay(p->pdpy);
}
if (userOptions.printAndExit) {
XtAppSetExitFlag(XtWidgetToApplicationContext(p->toplevel));
}
p->toplevel = NULL;
p->isPrinting = False;
p->pdpy = NULL;
p->pscreen = NULL;
p->pcontext = None;
}
static
void PrintEndJobCB(Widget pshell, XtPointer client_data, XtPointer call_data)
{
AppPrintData *p = (AppPrintData *)client_data;
Log(("--> PrintEndJobCB\n"));
FinishPrinting(p);
}
void DoPrint(Widget toplevel, const char *printername, const char *toFile)
{
XPPrinterList plist;
int plist_count;
Display *pdpy = NULL;
XPContext pcontext = None;
long dpi_x = 0L,
dpi_y = 0L;
if (apd->isPrinting) {
fprintf(stderr, "%s: Already busy with printing.\n", ProgramName);
return;
}
plist = XpuGetPrinterList(printername, &plist_count);
if (!plist) {
fprintf(stderr, "%s: no printers found for printer spec \"%s\".\n",
ProgramName, NULLSTR(printername));
return;
}
printername = strdup(plist[0].name);
XpuFreePrinterList(plist);
Log(("Using printer '%s'\n", printername));
if( XpuGetPrinter(printername, &pdpy, &pcontext) != 1 ) {
fprintf(stderr, "%s: Cannot open printer '%s'.\n", ProgramName, printername);
return;
}
XpuSetJobTitle(pdpy, pcontext, "Xprint XLogo");
XpSetContext(pdpy, pcontext);
if (XpuGetResolution(pdpy, pcontext, &dpi_x, &dpi_y) != 1) {
fprintf(stderr, "%s: No default resolution for printer '%s'\n", ProgramName, printername);
XpuClosePrinterDisplay(pdpy, pcontext);
return;
}
apd->toplevel = toplevel;
apd->pdpy = pdpy;
apd->pcontext = pcontext;
apd->pscreen = XpGetScreenOfContext(pdpy, pcontext);
apd->printshell = CreatePrintShell(toplevel, apd->pscreen, "Print", NULL, 0);
XtAddCallback(apd->printshell, XawNpageSetupCallback, PageSetupCB, (XtPointer)apd);
XtAddCallback(apd->printshell, XawNendJobCallback, PrintEndJobCB, (XtPointer)apd);
apd->isPrinting = True;
if (toFile) {
printf("%s: Printing to file '%s'...\n", ProgramName, toFile);
apd->printtofile_handle = XpuStartJobToFile(pdpy, pcontext, toFile);
if (!apd->printtofile_handle) {
perror("XpuStartJobToFile failure");
apd->isPrinting = False;
return;
}
}
else
{
printf("%s: Printing to printer '%s'...\n", ProgramName, printername);
XpuStartJobToSpooler(pdpy);
}
}