xref: /haiku/src/add-ons/print/drivers/preview/PrinterDriver.cpp (revision d9823ef5a07f365e5d438eeadb32a5a6c157a01e)
1d9b3e69eSMichael Pfeiffer /*
21879dc35SKarsten Heimrich  * Copyright 2003-2008, Haiku. All rights reserved.
3fcccd8b7SMichael Pfeiffer  * Distributed under the terms of the MIT License.
4fcccd8b7SMichael Pfeiffer  *
5fcccd8b7SMichael Pfeiffer  * Authors:
6fcccd8b7SMichael Pfeiffer  *		Philippe Houdoin
7fcccd8b7SMichael Pfeiffer  *		Simon Gauvin
8fcccd8b7SMichael Pfeiffer  *		Michael Pfeiffer
9fcccd8b7SMichael Pfeiffer  *		Dr. Hartmut Reh
10d9b3e69eSMichael Pfeiffer  */
11d9b3e69eSMichael Pfeiffer 
12d9b3e69eSMichael Pfeiffer #include "PrinterDriver.h"
131879dc35SKarsten Heimrich 
14d9b3e69eSMichael Pfeiffer #include "JobSetupWindow.h"
151879dc35SKarsten Heimrich #include "PageSetupWindow.h"
16df24dedeSKarsten Heimrich #include "PrintJobReader.h"
17d9b3e69eSMichael Pfeiffer 
18d9b3e69eSMichael Pfeiffer 
191879dc35SKarsten Heimrich #include <File.h>
201879dc35SKarsten Heimrich #include <Node.h>
211879dc35SKarsten Heimrich #include <Message.h>
221bf69d91SKarsten Heimrich #include <PrintJob.h>
23d9b3e69eSMichael Pfeiffer 
24d9b3e69eSMichael Pfeiffer 
251879dc35SKarsten Heimrich #include <stdio.h>
261879dc35SKarsten Heimrich 
271879dc35SKarsten Heimrich 
PrinterDriver(BNode * printerNode)28d9b3e69eSMichael Pfeiffer PrinterDriver::PrinterDriver(BNode* printerNode)
291879dc35SKarsten Heimrich 	: fPrinting(false)
301879dc35SKarsten Heimrich 	, fJobFile(NULL)
311879dc35SKarsten Heimrich 	, fPrinterNode(printerNode)
32d9b3e69eSMichael Pfeiffer {
33d9b3e69eSMichael Pfeiffer }
34d9b3e69eSMichael Pfeiffer 
35d9b3e69eSMichael Pfeiffer 
~PrinterDriver()36d9b3e69eSMichael Pfeiffer PrinterDriver::~PrinterDriver()
37d9b3e69eSMichael Pfeiffer {
38d9b3e69eSMichael Pfeiffer }
39d9b3e69eSMichael Pfeiffer 
40d9b3e69eSMichael Pfeiffer 
41d9b3e69eSMichael Pfeiffer status_t
PrintJob(BFile * spoolFile,BMessage * jobMsg)421879dc35SKarsten Heimrich PrinterDriver::PrintJob(BFile *spoolFile, BMessage *jobMsg)
43d9b3e69eSMichael Pfeiffer {
441879dc35SKarsten Heimrich 	if (!spoolFile || !fPrinterNode)
45d9b3e69eSMichael Pfeiffer 		return B_ERROR;
46d9b3e69eSMichael Pfeiffer 
471879dc35SKarsten Heimrich 	fJobFile = spoolFile;
481879dc35SKarsten Heimrich 	print_file_header pfh;
491879dc35SKarsten Heimrich 
50d9b3e69eSMichael Pfeiffer 	// read print file header
51d9b3e69eSMichael Pfeiffer 	fJobFile->Seek(0, SEEK_SET);
52d9b3e69eSMichael Pfeiffer 	fJobFile->Read(&pfh, sizeof(pfh));
53d9b3e69eSMichael Pfeiffer 
54d9b3e69eSMichael Pfeiffer 	// read job message
551879dc35SKarsten Heimrich 	BMessage msg;
561879dc35SKarsten Heimrich 	msg.Unflatten(fJobFile);
57d9b3e69eSMichael Pfeiffer 
581879dc35SKarsten Heimrich 	BeginJob();
59d9b3e69eSMichael Pfeiffer 	fPrinting = true;
601879dc35SKarsten Heimrich 
615226ccb6SKarsten Heimrich 	printf("PrinterDriver::PrintJob, print massage read from spool file\n");
625226ccb6SKarsten Heimrich 	jobMsg->PrintToStream();
635226ccb6SKarsten Heimrich 
645226ccb6SKarsten Heimrich 	printf("\nPrinterDriver::PrintJob, print massage passed to print job\n");
655226ccb6SKarsten Heimrich 	msg.PrintToStream();
665226ccb6SKarsten Heimrich 	printf("\n");
675226ccb6SKarsten Heimrich 
681879dc35SKarsten Heimrich 	for (int32 page = 1; page <= pfh.page_count; ++page) {
69*14bf5c32SJérôme Duval 		printf("PrinterDriver::PrintPage(): Faking print of page %" B_PRId32
70*14bf5c32SJérôme Duval 			"/ %" B_PRId32 "\n", page, pfh.page_count);
71d9b3e69eSMichael Pfeiffer 	}
72d9b3e69eSMichael Pfeiffer 
731879dc35SKarsten Heimrich 	fJobFile->Seek(0, SEEK_SET);
741879dc35SKarsten Heimrich 	PrintJobReader reader(fJobFile);
75d9b3e69eSMichael Pfeiffer 
761879dc35SKarsten Heimrich 	status_t status = reader.InitCheck();
775226ccb6SKarsten Heimrich 	printf("\nPrintJobReader::InitCheck(): %s\n", status == B_OK ? "B_OK" : "B_ERROR");
78*14bf5c32SJérôme Duval 	printf("PrintJobReader::NumberOfPages(): %" B_PRId32 "\n",
79*14bf5c32SJérôme Duval 		reader.NumberOfPages());
80*14bf5c32SJérôme Duval 	printf("PrintJobReader::FirstPage(): %" B_PRId32 "\n", reader.FirstPage());
81*14bf5c32SJérôme Duval 	printf("PrintJobReader::LastPage(): %" B_PRId32 "\n", reader.LastPage());
82d9b3e69eSMichael Pfeiffer 
831879dc35SKarsten Heimrich 	BRect rect = reader.PaperRect();
841879dc35SKarsten Heimrich 	printf("PrintJobReader::PaperRect(): BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n",
851879dc35SKarsten Heimrich 		rect.left, rect.top, rect.right, rect.bottom);
861879dc35SKarsten Heimrich 
871879dc35SKarsten Heimrich 	rect = reader.PrintableRect();
881879dc35SKarsten Heimrich 	printf("PrintJobReader::PrintableRect(): BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n",
891879dc35SKarsten Heimrich 		rect.left, rect.top, rect.right, rect.bottom);
901879dc35SKarsten Heimrich 
911879dc35SKarsten Heimrich 	int32 xdpi, ydpi;
921879dc35SKarsten Heimrich 	reader.GetResolution(&xdpi, &ydpi);
93*14bf5c32SJérôme Duval 	printf("PrintJobReader::GetResolution(): xdpi:%" B_PRId32 ", ydpi:%"
94*14bf5c32SJérôme Duval 		B_PRId32 "\n", xdpi, ydpi);
951879dc35SKarsten Heimrich 	printf("PrintJobReader::GetScale(): %.1f\n", reader.GetScale());
961879dc35SKarsten Heimrich 
971879dc35SKarsten Heimrich 	fPrinting = false;
981879dc35SKarsten Heimrich 	EndJob();
99d9b3e69eSMichael Pfeiffer 
100d9b3e69eSMichael Pfeiffer 	return status;
101d9b3e69eSMichael Pfeiffer }
102d9b3e69eSMichael Pfeiffer 
1031879dc35SKarsten Heimrich 
104d9b3e69eSMichael Pfeiffer void
StopPrinting()105d9b3e69eSMichael Pfeiffer PrinterDriver::StopPrinting()
106d9b3e69eSMichael Pfeiffer {
107d9b3e69eSMichael Pfeiffer 	fPrinting = false;
108d9b3e69eSMichael Pfeiffer }
109d9b3e69eSMichael Pfeiffer 
110d9b3e69eSMichael Pfeiffer 
111d9b3e69eSMichael Pfeiffer status_t
BeginJob()112d9b3e69eSMichael Pfeiffer PrinterDriver::BeginJob()
113d9b3e69eSMichael Pfeiffer {
114d9b3e69eSMichael Pfeiffer 	return B_OK;
115d9b3e69eSMichael Pfeiffer }
116d9b3e69eSMichael Pfeiffer 
117d9b3e69eSMichael Pfeiffer 
118d9b3e69eSMichael Pfeiffer status_t
PrintPage(int32 pageNumber,int32 pageCount)119d9b3e69eSMichael Pfeiffer PrinterDriver::PrintPage(int32 pageNumber, int32 pageCount)
120d9b3e69eSMichael Pfeiffer {
121d9b3e69eSMichael Pfeiffer 	return B_OK;
122d9b3e69eSMichael Pfeiffer }
123d9b3e69eSMichael Pfeiffer 
124d9b3e69eSMichael Pfeiffer 
125d9b3e69eSMichael Pfeiffer status_t
EndJob()126d9b3e69eSMichael Pfeiffer PrinterDriver::EndJob()
127d9b3e69eSMichael Pfeiffer {
128d9b3e69eSMichael Pfeiffer 	return B_OK;
129d9b3e69eSMichael Pfeiffer }
130d9b3e69eSMichael Pfeiffer 
131d9b3e69eSMichael Pfeiffer 
1321879dc35SKarsten Heimrich BlockingWindow*
NewPageSetupWindow(BMessage * setupMsg,const char * printerName)1331879dc35SKarsten Heimrich PrinterDriver::NewPageSetupWindow(BMessage *setupMsg, const char *printerName)
1341879dc35SKarsten Heimrich {
135d9b3e69eSMichael Pfeiffer 	return new PageSetupWindow(setupMsg, printerName);
136d9b3e69eSMichael Pfeiffer }
137d9b3e69eSMichael Pfeiffer 
1381879dc35SKarsten Heimrich 
1391879dc35SKarsten Heimrich BlockingWindow*
NewJobSetupWindow(BMessage * jobMsg,const char * printerName)1401879dc35SKarsten Heimrich PrinterDriver::NewJobSetupWindow(BMessage *jobMsg, const char *printerName)
1411879dc35SKarsten Heimrich {
142d9b3e69eSMichael Pfeiffer 	return new JobSetupWindow(jobMsg, printerName);
143d9b3e69eSMichael Pfeiffer }
144d9b3e69eSMichael Pfeiffer 
145d9b3e69eSMichael Pfeiffer 
146d9b3e69eSMichael Pfeiffer status_t
PageSetup(BMessage * setupMsg,const char * printerName)147d9b3e69eSMichael Pfeiffer PrinterDriver::PageSetup(BMessage *setupMsg, const char *printerName)
148d9b3e69eSMichael Pfeiffer {
1491879dc35SKarsten Heimrich 	BlockingWindow* w = NewPageSetupWindow(setupMsg, printerName);
1501879dc35SKarsten Heimrich 	return w->Go();
151d9b3e69eSMichael Pfeiffer }
152d9b3e69eSMichael Pfeiffer 
153d9b3e69eSMichael Pfeiffer 
154d9b3e69eSMichael Pfeiffer status_t
JobSetup(BMessage * jobMsg,const char * printerName)155d9b3e69eSMichael Pfeiffer PrinterDriver::JobSetup(BMessage *jobMsg, const char *printerName)
156d9b3e69eSMichael Pfeiffer {
157d9b3e69eSMichael Pfeiffer 	// set default value if property not set
158d9b3e69eSMichael Pfeiffer 	if (!jobMsg->HasInt32("copies"))
159d9b3e69eSMichael Pfeiffer 		jobMsg->AddInt32("copies", 1);
160d9b3e69eSMichael Pfeiffer 
161d9b3e69eSMichael Pfeiffer 	if (!jobMsg->HasInt32("first_page"))
162d9b3e69eSMichael Pfeiffer 		jobMsg->AddInt32("first_page", 1);
163d9b3e69eSMichael Pfeiffer 
164d9b3e69eSMichael Pfeiffer 	if (!jobMsg->HasInt32("last_page"))
165*14bf5c32SJérôme Duval 		jobMsg->AddInt32("last_page", INT32_MAX);
166d9b3e69eSMichael Pfeiffer 
1671879dc35SKarsten Heimrich 	BlockingWindow* w = NewJobSetupWindow(jobMsg, printerName);
1681879dc35SKarsten Heimrich 	return w->Go();
169d9b3e69eSMichael Pfeiffer }
170d9b3e69eSMichael Pfeiffer 
1711879dc35SKarsten Heimrich 
1721879dc35SKarsten Heimrich /* copied from PDFlib.h: */
1731879dc35SKarsten Heimrich #define letter_width	 (float) 612.0
1741879dc35SKarsten Heimrich #define letter_height	 (float) 792.0
1751879dc35SKarsten Heimrich 
1761879dc35SKarsten Heimrich 
177d9b3e69eSMichael Pfeiffer BMessage*
GetDefaultSettings()178d9b3e69eSMichael Pfeiffer PrinterDriver::GetDefaultSettings()
179d9b3e69eSMichael Pfeiffer {
180d9b3e69eSMichael Pfeiffer 	BMessage* msg = new BMessage();
181d9b3e69eSMichael Pfeiffer 	BRect paperRect(0, 0, letter_width, letter_height);
182d9b3e69eSMichael Pfeiffer 	BRect printableRect(paperRect);
183d9b3e69eSMichael Pfeiffer 	printableRect.InsetBy(10, 10);
184d9b3e69eSMichael Pfeiffer 	msg->AddRect("paper_rect", paperRect);
185d9b3e69eSMichael Pfeiffer 	msg->AddRect("printable_rect", printableRect);
186d9b3e69eSMichael Pfeiffer 	msg->AddInt32("orientation", 0);
187d9b3e69eSMichael Pfeiffer 	msg->AddInt32("xres", 300);
188d9b3e69eSMichael Pfeiffer 	msg->AddInt32("yres", 300);
189d9b3e69eSMichael Pfeiffer 	return msg;
190d9b3e69eSMichael Pfeiffer }
191