1#include "PrintTestApp.hpp"
2#include "PrintTestWindow.hpp"
3
4#include <PrintJob.h>
5
6#define your_document_last_page		100
7
8int main()
9{
10	new PrintTestApp;
11		be_app->Run();
12	delete be_app;
13
14	return 0;
15}
16
17PrintTestApp::PrintTestApp()
18	: Inherited(PRINTTEST_SIGNATURE)
19{
20}
21
22void PrintTestApp::MessageReceived(BMessage* msg)
23{
24	switch(msg->what) {
25		case 'PStp':
26			DoTestPageSetup();
27			break;
28
29		case 'Prnt':
30			Print();
31			break;
32	}
33}
34
35void PrintTestApp::ReadyToRun()
36{
37	(new PrintTestWindow)->Show();
38}
39
40
41status_t PrintTestApp::DoTestPageSetup()
42{
43	BPrintJob job("document's name");
44	status_t result = job.ConfigPage();
45	if (result == B_OK) {
46		// Get the user Settings
47		fSetupData = job.Settings();
48
49		// Use the new settings for your internal use
50		fPaperRect = job.PaperRect();
51		fPrintableRect = job.PrintableRect();
52	}
53
54	return result;
55}
56
57status_t PrintTestApp::Print()
58{
59	status_t result = B_OK;
60
61	// If we have no setup message, we should call ConfigPage()
62	// You must use the same instance of the BPrintJob object
63	// (you can't call the previous "PageSetup()" function, since it
64	// creates its own BPrintJob object).
65
66	if (!fSetupData) {
67		result = DoTestPageSetup();
68	}
69
70	BPrintJob job("document's name");
71	if (result == B_OK) {
72		// Setup the driver with user settings
73		job.SetSettings(fSetupData);
74
75		result = job.ConfigJob();
76		if (result == B_OK) {
77			// WARNING: here, setup CANNOT be NULL.
78			if (fSetupData == NULL) {
79				// something's wrong, handle the error and bail out
80			}
81			delete fSetupData;
82
83			// Get the user Settings
84			fSetupData = job.Settings();
85
86			// Use the new settings for your internal use
87			// They may have changed since the last time you read it
88			fPaperRect = job.PaperRect();
89			fPrintableRect = job.PrintableRect();
90
91			// Now you have to calculate the number of pages
92			// (note: page are zero-based)
93			int32 firstPage = job.FirstPage();
94			int32 lastPage = job.LastPage();
95
96			// Verify the range is correct
97			// 0 ... LONG_MAX -> Print all the document
98			// n ... LONG_MAX -> Print from page n to the end
99			// n ... m -> Print from page n to page m
100
101			if (lastPage > your_document_last_page)
102				lastPage = your_document_last_page;
103
104			int32 nbPages = lastPage - firstPage + 1;
105
106			// Verify the range is correct
107			if (nbPages <= 0)
108				return B_ERROR;
109
110			// Now you can print the page
111			job.BeginJob();
112
113			// Print all pages
114			bool can_continue = job.CanContinue();
115
116			for (int i=firstPage ; can_continue && i<=lastPage ; i++) {
117				// Draw all the needed views
118//IRA			job.DrawView(someView, viewRect, pointOnPage);
119//IRA			job.DrawView(anotherView, anotherRect, differentPoint);
120
121/*
122				// If the document have a lot of pages, you can update a BStatusBar, here
123				// or else what you want...
124				update_status_bar(i-firstPage, nbPages);
125*/
126
127				// Spool the page
128				job.SpoolPage();
129
130/*
131				// Cancel the job if needed.
132				// You can for exemple verify if the user pressed the ESC key
133				// or (SHIFT + '.')...
134				if (user_has_canceled)
135				{
136					// tell the print_server to cancel the printjob
137					job.CancelJob();
138					can_continue = false;
139					break;
140				}
141*/
142				// Verify that there was no error (disk full for example)
143				can_continue = job.CanContinue();
144			}
145
146			// Now, you just have to commit the job!
147			if (can_continue)
148				job.CommitJob();
149			else
150				result = B_ERROR;
151		}
152	}
153
154	return result;
155}
156