xref: /haiku/src/libs/print/libprint/StatusWindow.cpp (revision 5f050a9e7a76e187d085f9f2fc256227be5e25a9)
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 
11 #include <Button.h>
12 #include <GroupLayout.h>
13 #include <GroupLayoutBuilder.h>
14 #include <Message.h>
15 #include <View.h>
16 #include <StatusBar.h>
17 #include <stdio.h>
18 #include <StringView.h>
19 
20 
21 #define CANCEL_MSG 	'canM'
22 #define HIDE_MSG 	'hidM'
23 
24 
StatusWindow(bool oddPages,bool evenPages,uint32 firstPage,uint32 numPages,uint32 numCopies,uint32 nup)25 StatusWindow::StatusWindow(bool oddPages, bool evenPages, uint32 firstPage,
26 	uint32 numPages, uint32 numCopies, uint32 nup)
27 	:
28 	BWindow(BRect(200, 200, 250, 250),
29 		"Print status",
30 		B_TITLED_WINDOW,
31 		B_NOT_RESIZABLE | B_NOT_CLOSABLE | B_NOT_ZOOMABLE
32 			| B_AUTO_UPDATE_SIZE_LIMITS)
33 {
34 	//	oddPages	- if true, only print odd numbered pages
35 	//	evenPages	- if true, only print even numbered pages
36 	//  firstPage	- number of first page
37 	//  numPages	- total number of pages (must be recalculate if odd/even is
38 	//                used)
39 	//  numCopies   - total number of document copies
40 
41 	// the status bar
42 	fStatusBar = new BStatusBar("statusBar", "Page: ");
43 
44 	// the cancel button
45 	fHideButton = new BButton("hideButton",	"Hide status",
46 		new BMessage(HIDE_MSG));
47 
48 	fCancelButton = new BButton("cancelButton", "Cancel",
49 		new BMessage(CANCEL_MSG));
50 
51 	SetLayout(new BGroupLayout(B_VERTICAL));
52 	AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10)
53 		.Add(fStatusBar)
54 		.Add(fHideButton)
55 		.Add(fCancelButton)
56 		.SetInsets(10, 10, 10, 10)
57 	);
58 
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 
~StatusWindow(void)107 StatusWindow::~StatusWindow(void)
108 {
109 }
110 
111 
112 void
ResetStatusBar(void)113 StatusWindow::ResetStatusBar(void)
114 {
115 	Lock();
116 		fStatusBar->Reset("Page:  ");
117 		InvalidateLayout(true);
118 	Unlock();
119 }
120 
121 
122 bool
UpdateStatusBar(uint32 page,uint32 copy)123 StatusWindow::UpdateStatusBar(uint32 page, uint32 copy)
124 {
125 	Lock();
126 		Activate(true);
127 			// Frontmost Window
128 		char buffer[20];
129 
130 		sprintf(buffer,"%d", (int)(page + 1));
131 		BString string1(buffer);
132 
133 		sprintf(buffer,"%d",(int)fNops );
134 		BString string2(buffer);
135 		string1.Append(BString(" / "));
136 		string1.Append(string2);
137 
138 		BString string3 = BString("Remaining document copies:  ");
139 		if (fDocumentCopy == true) {
140 			sprintf(buffer, "%d", (int)(fDocCopies));
141 			BString string4(buffer);
142 			string3.Append(string4);
143 		} else {
144 			string3 = BString("Remaining page copies:  ");
145 			char buffer[20];
146 			sprintf(buffer,"%d",(int)(fCopies - copy) );
147 			BString string4(buffer);
148 			string3.Append(string4);
149 		}
150 
151 		fStatusBar->Update(fStatusDelta * 100.0 / fNops,
152 			string1.String(), string3.String());
153 		if (fStatusBar->MaxValue() == fStatusBar->CurrentValue())
154 				fCancelButton->SetEnabled(false);
155 
156 		InvalidateLayout(true);
157 	Unlock();
158 	return fCancelled;
159 }
160 
161 
162 void
SetPageCopies(uint32 copies)163 StatusWindow::SetPageCopies(uint32 copies)
164 {
165 	fCopies = copies;
166 	fStatusDelta = fDelta / (float)fCopies;
167 	fDocCopies--;
168 }
169 
170 
171 // Handling of user interface and other events
172 void
MessageReceived(BMessage * message)173 StatusWindow::MessageReceived(BMessage *message)
174 {
175 
176 	switch(message->what) {
177 		case CANCEL_MSG:	// 'CancelButton' is pressed...
178 			fCancelled = true;
179 			fCancelButton->SetEnabled(false);
180 			fCancelButton->SetLabel("Job cancelled");
181 			InvalidateLayout(true);
182 			break;
183 
184 		case HIDE_MSG:	// 'HideButton' is pressed...
185 			Hide();
186 			break;
187 
188 		default:
189 			BWindow::MessageReceived(message);
190 			break;
191 	}
192 
193 }
194