xref: /haiku/src/libs/print/libprint/StatusWindow.cpp (revision 42c36e9fd3f26b10500300a77f2be30062a59c6f)
1 /*
2  * Copyright 2005-2006, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Dr.H.Reh
7  */
8 
9 #include "StatusWindow.h"
10 #include <Message.h>
11 #include <View.h>
12 #include <StatusBar.h>
13 #include <Button.h>
14 #include <stdio.h>
15 #include <StringView.h>
16 
17 
18 #define CANCEL_MSG 	'canM'
19 #define HIDE_MSG 	'hidM'
20 
21 
22 StatusWindow::StatusWindow(bool oddPages, bool evenPages, uint32 firstPage,
23 	uint32 numPages, uint32 numCopies, uint32 nup)
24 	:
25 	BWindow(BRect(200, 200, 650, 270),
26 		"Print Status",
27 		B_DOCUMENT_WINDOW, B_NOT_RESIZABLE | B_NOT_CLOSABLE | B_NOT_ZOOMABLE)
28 {
29 	//	oddPages	- if true, only print odd numbered pages
30 	//	evenPages	- if true, only print even numbered pages
31 	//  firstPage	- number of first page
32 	//  numPages	- total number of pages (must be recalculate if odd/even is
33 	//                used)
34 	//  numCopies   - total number of document copies
35 
36 	BRect frame = Frame();
37 	// the status view
38 	frame.OffsetTo(B_ORIGIN);
39 	fStatusView = new BView(frame, "Status View", B_FOLLOW_ALL, B_WILL_DRAW);
40 	fStatusView->SetViewColor(216, 216, 216);
41 	AddChild(fStatusView);
42 
43 	// the status bar
44 	fStatusBar = new BStatusBar(BRect(10, 15, 245, 50), "Status Bar", "Page: ");
45 	fStatusView->AddChild(fStatusBar);
46 
47 	// the cancel button
48 	fHideButton = new BButton(BRect(260, 25, 330, 50), "Hide Button",
49 		"Hide Status", new BMessage(HIDE_MSG));
50 	fHideButton->ResizeToPreferred();
51 	fStatusView->AddChild(fHideButton);
52 
53 	fCancelButton = new BButton(BRect(260, 25, 330,50), "Cancel Button",
54 		"Cancel", new BMessage(CANCEL_MSG));
55 	fCancelButton->ResizeToPreferred();
56 	fCancelButton->MoveBy(90,0);
57 
58 	fStatusView->AddChild(fCancelButton);
59 
60 	fCancelled = false;
61 
62 	// calculate the real number of pages
63 	fNops = numPages;
64 
65 	bool evenFirstPage 	= (firstPage % 2) == 0;
66 	bool evenNumPages 	= (numPages  % 2) == 0;
67 
68 	// recalculate page numbers if even or odd is used
69 	if (oddPages || evenPages) {
70 		if  (evenNumPages) {
71 			fNops = numPages / 2;
72 		} else if (evenFirstPage) {
73 			if (oddPages)
74 				fNops = (numPages - 1) / 2;
75 			if (evenPages)
76 				fNops = (numPages + 1) / 2;
77 		} else {
78 			if (oddPages)
79 				fNops = (numPages + 1) / 2;
80 			if (evenPages)
81 				fNops = (numPages - 1) / 2;
82 		}
83 	}
84 
85 	uint32 addPage = 0;
86 	if (fNops % nup > 0)
87 		addPage = 1;
88 	fNops = (uint32)(fNops / (float)nup) + addPage;
89 		// recalculate page numbers nup-pages-up
90 
91 	fStatusBar->SetMaxValue((float)fNops);
92 		// max value of status bar = real number of pages
93 	fDelta = 1.0/numCopies;
94 		// reduce step width of status bar
95 	fStatusDelta = fDelta;
96 	fDocCopies = numCopies;
97 
98 	fDocumentCopy = fDocCopies > 1;
99 
100 	fDocCopies++;
101 
102 	ResetStatusBar();
103 	Show();
104 }
105 
106 
107 StatusWindow::~StatusWindow(void)
108 {
109 }
110 
111 
112 void
113 StatusWindow::ResetStatusBar(void)
114 {
115 	Lock();
116 		fStatusBar->Reset("Page:  ");
117 	Unlock();
118 }
119 
120 
121 bool
122 StatusWindow::UpdateStatusBar(uint32 page, uint32 copy)
123 {
124 	Lock();
125 		Activate(true);
126 			// Frontmost Window
127 		char buffer[20];
128 
129 		sprintf(buffer,"%d", (int)(page + 1));
130 		BString string1(buffer);
131 
132 		sprintf(buffer,"%d",(int)fNops );
133 		BString string2(buffer);
134 		string1.Append(BString(" / "));
135 		string1.Append(string2);
136 
137 		BString string3 = BString("Remaining Document Copies:  ");
138 		if (fDocumentCopy == true) {
139 			sprintf(buffer, "%d", (int)(fDocCopies));
140 			BString string4(buffer);
141 			string3.Append(string4);
142 		} else {
143 			string3 = BString("Remaining Page Copies:  ");
144 			char buffer[20];
145 			sprintf(buffer,"%d",(int)(fCopies - copy) );
146 			BString string4(buffer);
147 			string3.Append(string4);
148 		}
149 
150 		fStatusBar->Update(fStatusDelta * 100.0 / fNops,
151 			string1.String(), string3.String());
152 		if (fStatusBar->MaxValue() == fStatusBar->CurrentValue())
153 				fCancelButton->SetEnabled(false);
154 	Unlock();
155 	return fCancelled;
156 }
157 
158 
159 void
160 StatusWindow::SetPageCopies(uint32 copies)
161 {
162 	fCopies = copies;
163 	fStatusDelta = fDelta / (float)fCopies;
164 	fDocCopies--;
165 }
166 
167 
168 // Handling of user interface and other events
169 void
170 StatusWindow::MessageReceived(BMessage *message)
171 {
172 
173 	switch(message->what) {
174 		case CANCEL_MSG:	// 'CancelButton' is pressed...
175 			fCancelled = true;
176 			fCancelButton->SetEnabled(false);
177 			fCancelButton->SetLabel("Job cancelled");
178 			break;
179 
180 		case HIDE_MSG:	// 'HideButton' is pressed...
181 			Hide();
182 			break;
183 
184 		default:
185 			BWindow::MessageReceived(message);
186 			break;
187 	}
188 
189 }
190