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