xref: /haiku/src/add-ons/print/drivers/preview/PrinterDriver.cpp (revision d9823ef5a07f365e5d438eeadb32a5a6c157a01e)
1 /*
2  * Copyright 2003-2008, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Philippe Houdoin
7  *		Simon Gauvin
8  *		Michael Pfeiffer
9  *		Dr. Hartmut Reh
10  */
11 
12 #include "PrinterDriver.h"
13 
14 #include "JobSetupWindow.h"
15 #include "PageSetupWindow.h"
16 #include "PrintJobReader.h"
17 
18 
19 #include <File.h>
20 #include <Node.h>
21 #include <Message.h>
22 #include <PrintJob.h>
23 
24 
25 #include <stdio.h>
26 
27 
PrinterDriver(BNode * printerNode)28 PrinterDriver::PrinterDriver(BNode* printerNode)
29 	: fPrinting(false)
30 	, fJobFile(NULL)
31 	, fPrinterNode(printerNode)
32 {
33 }
34 
35 
~PrinterDriver()36 PrinterDriver::~PrinterDriver()
37 {
38 }
39 
40 
41 status_t
PrintJob(BFile * spoolFile,BMessage * jobMsg)42 PrinterDriver::PrintJob(BFile *spoolFile, BMessage *jobMsg)
43 {
44 	if (!spoolFile || !fPrinterNode)
45 		return B_ERROR;
46 
47 	fJobFile = spoolFile;
48 	print_file_header pfh;
49 
50 	// read print file header
51 	fJobFile->Seek(0, SEEK_SET);
52 	fJobFile->Read(&pfh, sizeof(pfh));
53 
54 	// read job message
55 	BMessage msg;
56 	msg.Unflatten(fJobFile);
57 
58 	BeginJob();
59 	fPrinting = true;
60 
61 	printf("PrinterDriver::PrintJob, print massage read from spool file\n");
62 	jobMsg->PrintToStream();
63 
64 	printf("\nPrinterDriver::PrintJob, print massage passed to print job\n");
65 	msg.PrintToStream();
66 	printf("\n");
67 
68 	for (int32 page = 1; page <= pfh.page_count; ++page) {
69 		printf("PrinterDriver::PrintPage(): Faking print of page %" B_PRId32
70 			"/ %" B_PRId32 "\n", page, pfh.page_count);
71 	}
72 
73 	fJobFile->Seek(0, SEEK_SET);
74 	PrintJobReader reader(fJobFile);
75 
76 	status_t status = reader.InitCheck();
77 	printf("\nPrintJobReader::InitCheck(): %s\n", status == B_OK ? "B_OK" : "B_ERROR");
78 	printf("PrintJobReader::NumberOfPages(): %" B_PRId32 "\n",
79 		reader.NumberOfPages());
80 	printf("PrintJobReader::FirstPage(): %" B_PRId32 "\n", reader.FirstPage());
81 	printf("PrintJobReader::LastPage(): %" B_PRId32 "\n", reader.LastPage());
82 
83 	BRect rect = reader.PaperRect();
84 	printf("PrintJobReader::PaperRect(): BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n",
85 		rect.left, rect.top, rect.right, rect.bottom);
86 
87 	rect = reader.PrintableRect();
88 	printf("PrintJobReader::PrintableRect(): BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n",
89 		rect.left, rect.top, rect.right, rect.bottom);
90 
91 	int32 xdpi, ydpi;
92 	reader.GetResolution(&xdpi, &ydpi);
93 	printf("PrintJobReader::GetResolution(): xdpi:%" B_PRId32 ", ydpi:%"
94 		B_PRId32 "\n", xdpi, ydpi);
95 	printf("PrintJobReader::GetScale(): %.1f\n", reader.GetScale());
96 
97 	fPrinting = false;
98 	EndJob();
99 
100 	return status;
101 }
102 
103 
104 void
StopPrinting()105 PrinterDriver::StopPrinting()
106 {
107 	fPrinting = false;
108 }
109 
110 
111 status_t
BeginJob()112 PrinterDriver::BeginJob()
113 {
114 	return B_OK;
115 }
116 
117 
118 status_t
PrintPage(int32 pageNumber,int32 pageCount)119 PrinterDriver::PrintPage(int32 pageNumber, int32 pageCount)
120 {
121 	return B_OK;
122 }
123 
124 
125 status_t
EndJob()126 PrinterDriver::EndJob()
127 {
128 	return B_OK;
129 }
130 
131 
132 BlockingWindow*
NewPageSetupWindow(BMessage * setupMsg,const char * printerName)133 PrinterDriver::NewPageSetupWindow(BMessage *setupMsg, const char *printerName)
134 {
135 	return new PageSetupWindow(setupMsg, printerName);
136 }
137 
138 
139 BlockingWindow*
NewJobSetupWindow(BMessage * jobMsg,const char * printerName)140 PrinterDriver::NewJobSetupWindow(BMessage *jobMsg, const char *printerName)
141 {
142 	return new JobSetupWindow(jobMsg, printerName);
143 }
144 
145 
146 status_t
PageSetup(BMessage * setupMsg,const char * printerName)147 PrinterDriver::PageSetup(BMessage *setupMsg, const char *printerName)
148 {
149 	BlockingWindow* w = NewPageSetupWindow(setupMsg, printerName);
150 	return w->Go();
151 }
152 
153 
154 status_t
JobSetup(BMessage * jobMsg,const char * printerName)155 PrinterDriver::JobSetup(BMessage *jobMsg, const char *printerName)
156 {
157 	// set default value if property not set
158 	if (!jobMsg->HasInt32("copies"))
159 		jobMsg->AddInt32("copies", 1);
160 
161 	if (!jobMsg->HasInt32("first_page"))
162 		jobMsg->AddInt32("first_page", 1);
163 
164 	if (!jobMsg->HasInt32("last_page"))
165 		jobMsg->AddInt32("last_page", INT32_MAX);
166 
167 	BlockingWindow* w = NewJobSetupWindow(jobMsg, printerName);
168 	return w->Go();
169 }
170 
171 
172 /* copied from PDFlib.h: */
173 #define letter_width	 (float) 612.0
174 #define letter_height	 (float) 792.0
175 
176 
177 BMessage*
GetDefaultSettings()178 PrinterDriver::GetDefaultSettings()
179 {
180 	BMessage* msg = new BMessage();
181 	BRect paperRect(0, 0, letter_width, letter_height);
182 	BRect printableRect(paperRect);
183 	printableRect.InsetBy(10, 10);
184 	msg->AddRect("paper_rect", paperRect);
185 	msg->AddRect("printable_rect", printableRect);
186 	msg->AddInt32("orientation", 0);
187 	msg->AddInt32("xres", 300);
188 	msg->AddInt32("yres", 300);
189 	return msg;
190 }
191