Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / libs / print / libprint / StatusWindow.cpp
blob450c32e4cff049120e3666977f0ff1bcc31098a5
1 /*
2 * Copyright 2005-2006, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Dr.H.Reh
7 */
9 #include "StatusWindow.h"
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>
21 #define CANCEL_MSG 'canM'
22 #define HIDE_MSG 'hidM'
25 StatusWindow::StatusWindow(bool oddPages, bool evenPages, uint32 firstPage,
26 uint32 numPages, uint32 numCopies, uint32 nup)
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)
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
41 // the status bar
42 fStatusBar = new BStatusBar("statusBar", "Page: ");
44 // the cancel button
45 fHideButton = new BButton("hideButton", "Hide Status",
46 new BMessage(HIDE_MSG));
48 fCancelButton = new BButton("cancelButton", "Cancel",
49 new BMessage(CANCEL_MSG));
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)
60 fCancelled = false;
62 // calculate the real number of pages
63 fNops = numPages;
65 bool evenFirstPage = (firstPage % 2) == 0;
66 bool evenNumPages = (numPages % 2) == 0;
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;
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
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;
98 fDocumentCopy = fDocCopies > 1;
100 fDocCopies++;
102 ResetStatusBar();
103 Show();
107 StatusWindow::~StatusWindow(void)
112 void
113 StatusWindow::ResetStatusBar(void)
115 Lock();
116 fStatusBar->Reset("Page: ");
117 InvalidateLayout(true);
118 Unlock();
122 bool
123 StatusWindow::UpdateStatusBar(uint32 page, uint32 copy)
125 Lock();
126 Activate(true);
127 // Frontmost Window
128 char buffer[20];
130 sprintf(buffer,"%d", (int)(page + 1));
131 BString string1(buffer);
133 sprintf(buffer,"%d",(int)fNops );
134 BString string2(buffer);
135 string1.Append(BString(" / "));
136 string1.Append(string2);
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);
151 fStatusBar->Update(fStatusDelta * 100.0 / fNops,
152 string1.String(), string3.String());
153 if (fStatusBar->MaxValue() == fStatusBar->CurrentValue())
154 fCancelButton->SetEnabled(false);
156 InvalidateLayout(true);
157 Unlock();
158 return fCancelled;
162 void
163 StatusWindow::SetPageCopies(uint32 copies)
165 fCopies = copies;
166 fStatusDelta = fDelta / (float)fCopies;
167 fDocCopies--;
171 // Handling of user interface and other events
172 void
173 StatusWindow::MessageReceived(BMessage *message)
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;
184 case HIDE_MSG: // 'HideButton' is pressed...
185 Hide();
186 break;
188 default:
189 BWindow::MessageReceived(message);
190 break;