vfs: check userland buffers before reading them.
[haiku.git] / src / kits / print / JobSetupPanel.cpp
blob67743dc8bca9d05eb801b0d55a2aa86e0c2fce86
1 /*
2 * Copyright 2008-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Julun, <host.haiku@gmx.de>
7 */
9 #include <JobSetupPanel.h>
11 #include <Box.h>
12 #include <Button.h>
13 #include <CheckBox.h>
14 #include <GridLayoutBuilder.h>
15 #include <GroupLayoutBuilder.h>
16 #include <GroupView.h>
17 #include <MenuField.h>
18 #include <MenuItem.h>
19 #include <PopUpMenu.h>
20 #include <Printer.h>
21 #include <PrinterRoster.h>
22 #include <RadioButton.h>
23 #include <StringView.h>
24 #include <TextControl.h>
27 #include <stdlib.h>
30 namespace BPrivate {
31 namespace Print {
34 BJobSetupPanel::BJobSetupPanel(BPrinter* printer)
35 : BPrintPanel("Print document")
36 , fPrinter(printer)
37 , fPrinterRoster(NULL)
38 , fPrintRange(B_ALL_PAGES)
39 , fJobPanelFlags(B_NO_OPTIONS)
41 _InitObject();
42 _SetupInterface();
46 BJobSetupPanel::BJobSetupPanel(BPrinter* printer, uint32 flags)
47 : BPrintPanel("Print document")
48 , fPrinter(printer)
49 , fPrinterRoster(NULL)
50 , fPrintRange(B_ALL_PAGES)
51 , fJobPanelFlags(flags)
53 _InitObject();
54 _SetupInterface();
58 BJobSetupPanel::~BJobSetupPanel()
60 delete fPrinterRoster;
64 BJobSetupPanel::BJobSetupPanel(BMessage* data)
65 : BPrintPanel(data)
67 // TODO: implement
71 BArchivable*
72 BJobSetupPanel::Instantiate(BMessage* data)
74 // TODO: implement
75 return NULL;
79 status_t
80 BJobSetupPanel::Archive(BMessage* data, bool deep) const
82 // TODO: implement
83 return B_ERROR;
87 void
88 BJobSetupPanel::MessageReceived(BMessage* message)
91 BPrintPanel::MessageReceived(message);
95 status_t
96 BJobSetupPanel::Go()
98 status_t status = ShowPanel();
100 if (status == B_OK) {
101 // TODO: check if we did work on an real printer
102 // TODO: set all selected values on printer object
105 if (Lock())
106 Quit();
108 return status;
112 BPrinter*
113 BJobSetupPanel::Printer() const
115 return fPrinter;
119 void
120 BJobSetupPanel::SetPrinter(BPrinter* printer, bool keepSettings)
122 // TODO: implement
126 print_range
127 BJobSetupPanel::PrintRange() const
129 return fPrintRange;
133 void
134 BJobSetupPanel::SetPrintRange(print_range range)
136 switch (range) {
137 case B_ALL_PAGES: {
138 fPrintRange = range;
139 fPrintAll->SetValue(B_CONTROL_ON);
140 } break;
142 case B_SELECTION: {
143 fPrintRange = range;
144 SetOptionFlags(OptionFlags() | B_PRINT_SELECTION);
145 fSelection->SetValue(B_CONTROL_ON);
146 } break;
148 case B_PAGE_RANGE: {
149 fPrintRange = range;
150 SetOptionFlags(OptionFlags() | B_PRINT_PAGE_RANGE);
151 fPagesFrom->SetValue(B_CONTROL_ON);
152 } break;
157 int32
158 BJobSetupPanel::FirstPage() const
160 BString text(fFirstPage->Text());
161 if (text.Length() <= 0)
162 return 0;
164 return atoi(text.String());
168 int32
169 BJobSetupPanel::LastPage() const
171 BString text(fLastPage->Text());
172 if (text.Length() <= 0)
173 return LONG_MAX;
175 return atoi(text.String());
179 void
180 BJobSetupPanel::SetPageRange(int32 firstPage, int32 lastPage)
182 BString text;
183 SetPrintRange(B_PAGE_RANGE);
185 text << firstPage;
186 fFirstPage->SetText(text.String());
188 text << lastPage;
189 fLastPage->SetText(text.String());
193 uint32
194 BJobSetupPanel::OptionFlags() const
196 return fJobPanelFlags;
200 void
201 BJobSetupPanel::SetOptionFlags(uint32 flags)
203 bool value = false;
204 if (flags & B_PRINT_TO_FILE)
205 value = true;
206 fPrintToFile->SetEnabled(value);
208 value = false;
209 if (flags & B_PRINT_PAGE_RANGE)
210 value = true;
211 fPagesFrom->SetEnabled(value);
212 fFirstPage->SetEnabled(value);
213 fLastPage->SetEnabled(value);
215 value = false;
216 if (flags & B_PRINT_SELECTION)
217 value = true;
218 fSelection->SetEnabled(value);
220 value = false;
221 if (flags & B_PRINT_COLLATE_COPIES)
222 value = true;
223 fCollate->SetEnabled(value);
225 fJobPanelFlags = flags;
229 void
230 BJobSetupPanel::_InitObject()
232 fPrinterRoster = new BPrinterRoster();
233 fPrinterRoster->StartWatching(this);
235 if (!fPrinter->IsValid()) {
236 BPrinter defaultPrinter;
237 fPrinterRoster->GetDefaultPrinter(&defaultPrinter);
238 *fPrinter = defaultPrinter;
243 void
244 BJobSetupPanel::_SetupInterface()
246 BGroupView* groupView = new BGroupView(B_VERTICAL, 10.0);
248 // printers
249 fPrinterPopUp = new BPopUpMenu("");
250 fPrinterPopUp->SetRadioMode(true);
251 fPrinterMenuField = new BMenuField("", fPrinterPopUp);
252 fPrinterMenuField->Menu()->SetLabelFromMarked(true);
254 BPrinter printer;
255 while (fPrinterRoster->GetNextPrinter(&printer) == B_OK) {
256 BMenuItem* item = new BMenuItem(printer.Name().String(), NULL);
257 fPrinterPopUp->AddItem(item);
258 if (printer == *fPrinter)
259 item->SetMarked(true);
262 if (fPrinterRoster->CountPrinters() > 0)
263 fPrinterPopUp->AddItem(new BSeparatorItem);
265 BMenuItem* pdf = new BMenuItem("Save as PDF file" , NULL);
266 fPrinterPopUp->AddItem(pdf);
267 if (fPrinterPopUp->FindMarked() == NULL)
268 pdf->SetMarked(true);
270 fProperties = new BButton("Properties" B_UTF8_ELLIPSIS , new BMessage('prop'));
271 fPrinterInfo = new BStringView("label", "");
272 fPrinterInfo->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
273 BBox* divider = new BBox(B_FANCY_BORDER, NULL);
274 divider->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
275 fPrintToFile = new BCheckBox("Print to file");
277 BView* view = BGroupLayoutBuilder(B_VERTICAL, 5.0)
278 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 10.0)
279 .Add(fPrinterMenuField->CreateMenuBarLayoutItem())
280 .Add(fProperties))
281 .Add(BGroupLayoutBuilder(B_HORIZONTAL,5.0)
282 .Add(new BStringView("label", "Printer info:"))
283 .Add(fPrinterInfo))
284 .Add(divider)
285 .Add(fPrintToFile)
286 .SetInsets(10.0, 5.0, 10.0, 5.0);
288 BBox *box = new BBox(B_FANCY_BORDER, view);
289 box->SetLabel(BGroupLayoutBuilder()
290 .Add(new BStringView("", "Printer"))
291 .SetInsets(2.0, 0.0, 2.0, 0.0));
292 groupView->AddChild(box);
294 // page range
295 fPrintAll = new BRadioButton("Print all", new BMessage('prrg'));
296 fPrintAll->SetValue(B_CONTROL_ON);
297 fPagesFrom = new BRadioButton("Pages from:", new BMessage('prrg'));
298 fFirstPage = new BTextControl("", "", NULL);
299 _DisallowChar(fFirstPage->TextView());
300 fLastPage = new BTextControl("to:", "", NULL);
301 _DisallowChar(fLastPage->TextView());
302 fSelection = new BRadioButton("Print selection", new BMessage('prrg'));
304 fFirstPage->CreateLabelLayoutItem();
305 view = BGroupLayoutBuilder(B_VERTICAL, 5.0)
306 .Add(fPrintAll)
307 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 5.0)
308 .Add(fPagesFrom)
309 .Add(fFirstPage->CreateTextViewLayoutItem())
310 .Add(fLastPage->CreateLabelLayoutItem())
311 .Add(fLastPage->CreateTextViewLayoutItem()))
312 .Add(fSelection)
313 .SetInsets(10.0, 5.0, 10.0, 5.0);
315 box = new BBox(B_FANCY_BORDER, view);
316 box->SetLabel(BGroupLayoutBuilder()
317 .Add(new BStringView("", "Page range"))
318 .SetInsets(2.0, 0.0, 2.0, 0.0));
320 // copies
321 fNumberOfCopies = new BTextControl("Number of copies:", "1", NULL);
322 _DisallowChar(fNumberOfCopies->TextView());
323 fCollate = new BCheckBox("Collate");
324 fReverse = new BCheckBox("Reverse");
326 BView* view2 = BGroupLayoutBuilder(B_VERTICAL, 5.0)
327 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 5.0)
328 .Add(fNumberOfCopies->CreateLabelLayoutItem())
329 .Add(fNumberOfCopies->CreateTextViewLayoutItem()))
330 .Add(fCollate)
331 .Add(fReverse)
332 .SetInsets(10.0, 5.0, 10.0, 5.0);
334 BBox* box2 = new BBox(B_FANCY_BORDER, view2);
335 box2->SetLabel(BGroupLayoutBuilder()
336 .Add(new BStringView("", "Copies"))
337 .SetInsets(2.0, 0.0, 2.0, 0.0));
339 groupView->AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10.0)
340 .Add(box)
341 .Add(box2));
343 // other
344 fColor = new BCheckBox("Print in color");
345 fDuplex = new BCheckBox("Double side printing");
347 view = BGroupLayoutBuilder(B_VERTICAL, 5.0)
348 .Add(fColor)
349 .Add(fDuplex)
350 .SetInsets(10.0, 5.0, 10.0, 5.0);
352 box = new BBox(B_FANCY_BORDER, view);
353 box->SetLabel(BGroupLayoutBuilder()
354 .Add(new BStringView("", "Other"))
355 .SetInsets(2.0, 0.0, 2.0, 0.0));
356 groupView->AddChild(box);
358 AddPanel(groupView);
359 SetOptionFlags(fJobPanelFlags);
363 void
364 BJobSetupPanel::_DisallowChar(BTextView* textView)
366 for (uint32 i = 0; i < '0'; ++i)
367 textView->DisallowChar(i);
369 for (uint32 i = '9' + 1; i < 255; ++i)
370 textView->DisallowChar(i);
374 } // namespace Print
375 } // namespace BPrivate