xref: /haiku/src/add-ons/print/drivers/preview/PrinterDriver.cpp (revision c663ca2171fe8fee125a8d1fd64500b0deb5e814)
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 
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 %" B_PRId32
82 			"/ %" B_PRId32 "\n", 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(): %" B_PRId32 "\n",
91 		reader.NumberOfPages());
92 	printf("PrintJobReader::FirstPage(): %" B_PRId32 "\n", reader.FirstPage());
93 	printf("PrintJobReader::LastPage(): %" B_PRId32 "\n", reader.LastPage());
94 
95 	BRect rect = reader.PaperRect();
96 	printf("PrintJobReader::PaperRect(): BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n",
97 		rect.left, rect.top, rect.right, rect.bottom);
98 
99 	rect = reader.PrintableRect();
100 	printf("PrintJobReader::PrintableRect(): BRect(l:%.1f, t:%.1f, r:%.1f, b:%.1f)\n",
101 		rect.left, rect.top, rect.right, rect.bottom);
102 
103 	int32 xdpi, ydpi;
104 	reader.GetResolution(&xdpi, &ydpi);
105 	printf("PrintJobReader::GetResolution(): xdpi:%" B_PRId32 ", ydpi:%"
106 		B_PRId32 "\n", xdpi, ydpi);
107 	printf("PrintJobReader::GetScale(): %.1f\n", reader.GetScale());
108 
109 	fPrinting = false;
110 	EndJob();
111 
112 	return status;
113 }
114 
115 
116 void
117 PrinterDriver::StopPrinting()
118 {
119 	fPrinting = false;
120 }
121 
122 
123 status_t
124 PrinterDriver::BeginJob()
125 {
126 	return B_OK;
127 }
128 
129 
130 status_t
131 PrinterDriver::PrintPage(int32 pageNumber, int32 pageCount)
132 {
133 	return B_OK;
134 }
135 
136 
137 status_t
138 PrinterDriver::EndJob()
139 {
140 	return B_OK;
141 }
142 
143 
144 BlockingWindow*
145 PrinterDriver::NewPageSetupWindow(BMessage *setupMsg, const char *printerName)
146 {
147 	return new PageSetupWindow(setupMsg, printerName);
148 }
149 
150 
151 BlockingWindow*
152 PrinterDriver::NewJobSetupWindow(BMessage *jobMsg, const char *printerName)
153 {
154 	return new JobSetupWindow(jobMsg, printerName);
155 }
156 
157 
158 status_t
159 PrinterDriver::PageSetup(BMessage *setupMsg, const char *printerName)
160 {
161 	BlockingWindow* w = NewPageSetupWindow(setupMsg, printerName);
162 	return w->Go();
163 }
164 
165 
166 status_t
167 PrinterDriver::JobSetup(BMessage *jobMsg, const char *printerName)
168 {
169 	// set default value if property not set
170 	if (!jobMsg->HasInt32("copies"))
171 		jobMsg->AddInt32("copies", 1);
172 
173 	if (!jobMsg->HasInt32("first_page"))
174 		jobMsg->AddInt32("first_page", 1);
175 
176 	if (!jobMsg->HasInt32("last_page"))
177 		jobMsg->AddInt32("last_page", INT32_MAX);
178 
179 	BlockingWindow* w = NewJobSetupWindow(jobMsg, printerName);
180 	return w->Go();
181 }
182 
183 
184 /* copied from PDFlib.h: */
185 #define letter_width	 (float) 612.0
186 #define letter_height	 (float) 792.0
187 
188 
189 BMessage*
190 PrinterDriver::GetDefaultSettings()
191 {
192 	BMessage* msg = new BMessage();
193 	BRect paperRect(0, 0, letter_width, letter_height);
194 	BRect printableRect(paperRect);
195 	printableRect.InsetBy(10, 10);
196 	msg->AddRect("paper_rect", paperRect);
197 	msg->AddRect("printable_rect", printableRect);
198 	msg->AddInt32("orientation", 0);
199 	msg->AddInt32("xres", 300);
200 	msg->AddInt32("yres", 300);
201 	return msg;
202 }
203