#define NULLSTR(x) (((x)!=NULL)?(x):(""))
#define Error(x) { printf x ; exit(EXIT_FAILURE); }
#define Assertion(expr, msg) { if (!(expr)) { Error msg } }
#define Log(x) { if(True) printf x; }
#include "print.h"
#include "ScrollByL.h"
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Label.h>
#include <stdio.h>
#include <stdlib.h>
static Widget
CreatePrintShell(Widget videoshell,
Screen *pscreen,
Visual *pvisual,
String printshell_name,
ArgList args,
Cardinal numargs)
{
String videoname,
videoclass;
Widget pappshell,
printshell;
Display *pdpy = XDisplayOfScreen(pscreen);
int dummyc = 0;
String dummys = "";
Cardinal shell_n;
Arg shell_args[5];
XtGetApplicationNameAndClass(XtDisplay(videoshell),
&videoname, &videoclass);
XtDisplayInitialize(XtWidgetToApplicationContext(videoshell), pdpy,
videoname, videoclass,
NULL, 0,
&dummyc, &dummys);
shell_n = 0;
XtSetArg(shell_args[shell_n], XtNscreen, pscreen); shell_n++;
if (pvisual) {
XtSetArg(shell_args[shell_n], XtNvisual, pvisual); shell_n++;
}
pappshell = XtAppCreateShell(videoname, videoclass,
applicationShellWidgetClass,
pdpy,
shell_args, shell_n);
printshell = XtCreatePopupShell(printshell_name,
xawPrintShellWidgetClass,
pappshell, args, numargs);
XtSetMappedWhenManaged(printshell, False);
return printshell;
}
typedef struct
{
const char *programname;
Widget toplevel;
Bool isPrinting;
Widget printshell;
struct
{
Widget form;
Widget pageheaderlabel;
Widget manpage;
} content;
int numpages;
Display *pdpy;
Screen *pscreen;
XPContext pcontext;
XtCallbackProc pdpyDestroyCallback;
void *printtofile_handle;
const char *jobtitle;
} AppPrintData;
static AppPrintData apdx;
static AppPrintData *apd = &apdx;
static
long CountPages(Widget sblw)
{
long num_pages = 0;
Cardinal n;
Arg args[3];
int num_total_lines = 0,
num_visible_lines = 0;
n = 0;
XtSetArg(args[n], XtNNumTotalLines, &num_total_lines); n++;
XtSetArg(args[n], XtNNumVisibleLines, &num_visible_lines); n++;
XtGetValues(sblw, args, n);
#define DIV_ROUND_UP(a, b) (((a)+((b)-1)) / (b))
num_pages = DIV_ROUND_UP(num_total_lines, num_visible_lines-1);
#undef DIV_ROUND_UP
return num_pages;
}
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) {
int currpage;
char buffer[256];
XtVaGetValues(pshell, XawNcurrPageNumInJob, &currpage, NULL);
sprintf(buffer, "Title: %s / Page: %d/%d", p->jobtitle, currpage, p->numpages);
XtVaSetValues(apd->content.pageheaderlabel, XtNlabel, buffer, NULL);
if (currpage > 1) {
String params[] = { "Forward" };
Log(("pagedown %d\n", currpage));
XtCallActionProc(p->content.manpage, "Page", NULL, params, ONE);
}
else
{
Log(("first page\n"));
}
if (currpage >= p->numpages) {
psp->last_page_in_job = True;
}
}
}
static
void FinishPrinting(AppPrintData *p)
{
if (p->printtofile_handle) {
if (XpuWaitForPrintFileChild(p->printtofile_handle) != XPGetDocFinished) {
fprintf(stderr, "%s: Error while printing to file.\n", apd->programname);
}
p->printtofile_handle = NULL;
}
XtVaSetValues(p->content.manpage, XtNfile, NULL, NULL);
if (p->printshell) {
XtDestroyWidget(p->printshell);
p->printshell = NULL;
}
(*apd->pdpyDestroyCallback)(p->toplevel, NULL, NULL);
if (p->pcontext != None) {
XpDestroyContext(p->pdpy, p->pcontext);
}
XtCloseDisplay(p->pdpy);
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);
}
static
XFontStruct *GetPrintTextFont(Display *pdpy, const char *fontprefix, long dpi_x, long dpi_y)
{
XFontStruct *font;
char fontname[1024];
sprintf(fontname, "%s--*-120-%ld-%ld-*-*-iso8859-1", fontprefix, dpi_x, dpi_y);
font = XLoadQueryFont(pdpy, fontname);
if (!font) {
sprintf(fontname, "-adobe-courier-medium-r-normal--*-120-%ld-%ld-*-*-iso8859-1", dpi_x, dpi_y);
font = XLoadQueryFont(pdpy, fontname);
}
if (!font) {
sprintf(fontname, "-*-*-*-*-*-*-*-120-%ld-%ld-*-*-iso8859-1", dpi_x, dpi_y);
font = XLoadQueryFont(pdpy, fontname);
}
if (!font)
Error(("XLoadQueryFont failure.\n"));
return font;
}
void DoPrintManpage(const char *programname,
FILE *manpagefile, Widget toplevel,
Display *pdpy, XPContext pcontext,
XpuColorspaceRec *colorspace,
XtCallbackProc pdpyDestroyCB,
const char *jobtitle, const char *toFile)
{
long dpi_x = 0L,
dpi_y = 0L;
int n;
Arg args[20];
XFontStruct *printFontNormal;
XFontStruct *printFontBold;
XFontStruct *printFontItalic;
XFontStruct *printFontSymbol;
if (!manpagefile) {
Error(("DoPrintManpage: No FILE given."));
}
apd->programname = programname;
apd->pdpyDestroyCallback = pdpyDestroyCB;
if (apd->isPrinting) {
fprintf(stderr, "%s: Already busy with printing.\n", apd->programname);
return;
}
XpuSetJobTitle(pdpy, pcontext, jobtitle);
XpSetContext(pdpy, pcontext);
if (XpuGetResolution(pdpy, pcontext, &dpi_x, &dpi_y) != 1) {
fprintf(stderr, "%s: No default resolution for printer.\n", apd->programname);
XpuClosePrinterDisplay(pdpy, pcontext);
return;
}
apd->toplevel = toplevel;
apd->pdpy = pdpy;
apd->pcontext = pcontext;
apd->pscreen = XpGetScreenOfContext(pdpy, pcontext);
apd->jobtitle = jobtitle;
n = 0;
XtSetArg(args[n], XtNgeometry, "+0+0"); n++;
XtSetArg(args[n], XawNlayoutMode, XawPrintLAYOUTMODE_DRAWABLEAREA); n++;
if (colorspace) {
printf("Setting visual to id=0x%lx.\n", colorspace->visualinfo.visualid);
}
apd->printshell = CreatePrintShell(toplevel, apd->pscreen,
(colorspace?(colorspace->visualinfo.visual):(NULL)),
"printshell", args, n);
n = 0;
XtSetArg(args[n], XtNresizable, True); n++;
XtSetArg(args[n], XtNright, XtChainRight); n++;
apd->content.form = XtCreateManagedWidget("form", formWidgetClass, apd->printshell, args, n);
printFontNormal = GetPrintTextFont(pdpy, "-*-courier-medium-r-*", dpi_x, dpi_y);
printFontBold = GetPrintTextFont(pdpy, "-*-courier-bold-r-*", dpi_x, dpi_y);
printFontItalic = GetPrintTextFont(pdpy, "-*-courier-medium-o-*", dpi_x, dpi_y);
printFontSymbol = GetPrintTextFont(pdpy, "-*-symbol-*-*-*", dpi_x, dpi_y);
n = 0;
XtSetArg(args[n], XtNfromHoriz, NULL); n++;
XtSetArg(args[n], XtNfromVert, NULL); n++;
XtSetArg(args[n], XtNtop, XtChainTop); n++;
XtSetArg(args[n], XtNright, XtChainRight); n++;
XtSetArg(args[n], XtNresizable, True); n++;
XtSetArg(args[n], XtNfont, printFontNormal); n++;
XtSetArg(args[n], XtNlabel, "Page: n/n"); n++;
XtSetArg(args[n], XtNjustify, XtJustifyRight); n++;
apd->content.pageheaderlabel = XtCreateManagedWidget("pageinfo", labelWidgetClass, apd->content.form, args, n);
n = 0;
XtSetArg(args[n], XtNfile, manpagefile); n++;
#define WORKAROUND_FOR_SOMETHING_IS_WRONG 1
#ifdef WORKAROUND_FOR_SOMETHING_IS_WRONG
XtSetArg(args[n], XtNtop, XtChainTop); n++;
XtSetArg(args[n], XtNright, XtChainRight); n++;
XtSetArg(args[n], XtNvertDistance, (printFontNormal->ascent+printFontNormal->descent+2)*2); n++;
#else
XtSetArg(args[n], XtNfromHoriz, NULL); n++;
XtSetArg(args[n], XtNfromVert, apd->content.pageheaderlabel); n++;
#endif
XtSetArg(args[n], XtNmanualFontNormal, printFontNormal); n++;
XtSetArg(args[n], XtNmanualFontBold, printFontBold); n++;
XtSetArg(args[n], XtNmanualFontItalic, printFontItalic); n++;
XtSetArg(args[n], XtNmanualFontSymbol, printFontSymbol); n++;
apd->content.manpage = XtCreateManagedWidget("manpage", scrollByLineWidgetClass, apd->content.form, args, n);
XtAddCallback(apd->printshell, XawNpageSetupCallback, PageSetupCB, (XtPointer)apd);
XtAddCallback(apd->printshell, XawNendJobCallback, PrintEndJobCB, (XtPointer)apd);
XtRealizeWidget(apd->printshell);
apd->numpages = CountPages(apd->content.manpage);
if (XpGetScreenOfContext(XtDisplay(apd->printshell), apd->pcontext) != XtScreen(apd->printshell))
Error(("Widget's screen != print screen. BAD.\n"));
apd->isPrinting = True;
if (toFile) {
printf("%s: Printing to file '%s'...\n", apd->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...\n", apd->programname);
XpuStartJobToSpooler(pdpy);
}
}