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