2 * Copyright 2001-2011, Haiku.
3 * Distributed under the terms of the MIT License.
11 #include "PrintersWindow.h"
15 #include <Application.h>
18 #include <ControlLook.h>
19 #include <FindDirectory.h>
20 #include <GroupLayout.h>
22 #include <LayoutBuilder.h>
26 #include <ScrollView.h>
28 #include "pr_server.h"
29 #include "AddPrinterDialog.h"
31 #include "JobListView.h"
33 #include "PrinterListView.h"
34 #include "TestPageView.h"
35 #include "SpoolFolder.h"
38 #undef B_TRANSLATION_CONTEXT
39 #define B_TRANSLATION_CONTEXT "PrintersWindow"
42 class TestPageWindow
: public BWindow
{
44 TestPageWindow(BPrintJob
* job
, PrinterItem
* printer
);
45 virtual ~TestPageWindow();
47 void MessageReceived(BMessage
* message
);
50 TestPageView
* fTestPage
;
54 TestPageWindow::TestPageWindow(BPrintJob
* job
, PrinterItem
* printer
)
55 : BWindow(job
->PaperRect().OffsetByCopy(-20000, -20000), B_TRANSLATE("Test page"),
56 B_TITLED_WINDOW
, 0), fJob(job
)
58 fTestPage
= new TestPageView(job
->PrintableRect(), printer
);
60 // SetLayout(new BGroupLayout(B_VERTICAL));
65 TestPageWindow::~TestPageWindow()
72 TestPageWindow::MessageReceived(BMessage
* message
)
74 if (message
->what
!= kMsgPrintTestPage
) {
75 BWindow::MessageReceived(message
);
81 fJob
->DrawView(fTestPage
, fTestPage
->Bounds(), B_ORIGIN
);
84 if (!fJob
->CanContinue())
93 // #pragma mark PrintersWindow main class
96 PrintersWindow::PrintersWindow(BRect frame
)
98 BWindow(BRect(78, 71, 761, 509), B_TRANSLATE_SYSTEM_NAME("Printers"),
99 B_TITLED_WINDOW
, B_AUTO_UPDATE_SIZE_LIMITS
),
100 fSelectedPrinter(NULL
),
101 fAddingPrinter(false)
108 PrintersWindow::QuitRequested()
110 bool result
= Inherited::QuitRequested();
112 be_app
->PostMessage(B_QUIT_REQUESTED
);
119 PrintersWindow::MessageReceived(BMessage
* msg
)
122 case kMsgPrinterSelected
:
124 fSelectedPrinter
= fPrinterListView
->SelectedItem();
125 if (fSelectedPrinter
) {
126 fJobsBox
->SetLabel((BString(B_TRANSLATE("Print jobs for "))
127 << fSelectedPrinter
->Name()).String());
128 fMakeDefault
->SetEnabled(true);
129 fRemove
->SetEnabled(true);
130 fJobListView
->SetSpoolFolder(fSelectedPrinter
->Folder());
133 B_TRANSLATE("Print jobs: No printer selected"));
134 fMakeDefault
->SetEnabled(false);
135 fRemove
->SetEnabled(false);
136 fSelectedPrinter
= NULL
;
137 fJobListView
->SetSpoolFolder(NULL
);
140 _UpdatePrinterButtons();
145 if (!fAddingPrinter
) {
146 fAddingPrinter
= true;
147 new AddPrinterDialog(this);
151 case kMsgAddPrinterClosed
:
152 fAddingPrinter
= false;
155 case kMsgRemovePrinter
:
157 fSelectedPrinter
= fPrinterListView
->SelectedItem();
158 if (fSelectedPrinter
)
159 fSelectedPrinter
->Remove(fPrinterListView
);
163 case kMsgMakeDefaultPrinter
:
165 PrinterItem
* printer
= fPrinterListView
->SelectedItem();
166 if (printer
&& printer
== fPrinterListView
->ActivePrinter())
169 if (printer
&& GetPrinterServerMessenger(msgr
) == B_OK
) {
170 BMessage
setActivePrinter(B_SET_PROPERTY
);
171 setActivePrinter
.AddSpecifier("ActivePrinter");
172 setActivePrinter
.AddString("data", printer
->Name());
173 msgr
.SendMessage(&setActivePrinter
);
174 _UpdatePrinterButtons();
179 case kMsgPrintTestPage
:
181 fSelectedPrinter
= fPrinterListView
->SelectedItem();
182 if (fSelectedPrinter
)
183 PrintTestPage(fSelectedPrinter
);
188 fJobListView
->CancelJob();
192 fJobListView
->RestartJob();
195 case kMsgJobSelected
:
199 case B_PRINTER_CHANGED
:
201 // active printer could have been changed, even outside of prefs
202 BString
activePrinterName(ActivePrinterName());
203 PrinterItem
* item
= fPrinterListView
->ActivePrinter();
204 if (item
&& item
->Name() != activePrinterName
)
205 fPrinterListView
->UpdateItem(item
);
207 for (int32 i
= 0; i
< fPrinterListView
->CountItems(); ++i
) {
208 item
= dynamic_cast<PrinterItem
*>(fPrinterListView
->ItemAt(i
));
209 if (item
&& item
->Name() == activePrinterName
) {
210 fPrinterListView
->UpdateItem(item
);
211 fPrinterListView
->SetActivePrinter(item
);
218 Inherited::MessageReceived(msg
);
224 PrintersWindow::PrintTestPage(PrinterItem
* printer
)
226 BPrintJob
* job
= new BPrintJob(B_TRANSLATE("Test page"));
231 BMessage
* settings
= job
->Settings();
232 if (settings
== NULL
) {
237 // enforce job config properties
238 settings
->AddInt32("copies", 1);
239 settings
->AddInt32("first_page", 1);
240 settings
->AddInt32("last_page", -1);
242 BWindow
* win
= new TestPageWindow(job
, printer
);
244 win
->PostMessage(kMsgPrintTestPage
);
249 PrintersWindow::AddJob(SpoolFolder
* folder
, Job
* job
)
251 if (_IsSelected(folder
->Item()))
252 fJobListView
->AddJob(job
);
253 fPrinterListView
->UpdateItem(folder
->Item());
254 _UpdatePrinterButtons();
259 PrintersWindow::RemoveJob(SpoolFolder
* folder
, Job
* job
)
261 if (_IsSelected(folder
->Item()))
262 fJobListView
->RemoveJob(job
);
263 fPrinterListView
->UpdateItem(folder
->Item());
264 _UpdatePrinterButtons();
269 PrintersWindow::UpdateJob(SpoolFolder
* folder
, Job
* job
)
271 if (_IsSelected(folder
->Item())) {
272 fJobListView
->UpdateJob(job
);
275 fPrinterListView
->UpdateItem(folder
->Item());
276 _UpdatePrinterButtons();
284 PrintersWindow::_BuildGUI()
286 // ------------------------ Next, build the printers overview box
287 BBox
* printersBox
= new BBox("printersBox");
288 printersBox
->SetFont(be_bold_font
);
289 printersBox
->SetLabel(B_TRANSLATE("Printers"));
292 BButton
* addButton
= new BButton("add",
293 B_TRANSLATE("Add" B_UTF8_ELLIPSIS
), new BMessage(kMsgAddPrinter
));
294 addButton
->SetExplicitMaxSize(
295 BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
298 fRemove
= new BButton("remove",
299 B_TRANSLATE("Remove"), new BMessage(kMsgRemovePrinter
));
300 fRemove
->SetExplicitMaxSize(
301 BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
303 // Make Default button
304 fMakeDefault
= new BButton("default",
305 B_TRANSLATE("Make default"), new BMessage(kMsgMakeDefaultPrinter
));
306 fMakeDefault
->SetExplicitMaxSize(
307 BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
309 // Print Test Page button
310 fPrintTestPage
= new BButton("print_test_page",
311 B_TRANSLATE("Print test page"), new BMessage(kMsgPrintTestPage
));
312 fPrintTestPage
->SetExplicitMaxSize(
313 BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
315 // Disable all selection-based buttons
316 fRemove
->SetEnabled(false);
317 fMakeDefault
->SetEnabled(false);
318 fPrintTestPage
->SetEnabled(false);
320 // Create listview with scroller
321 fPrinterListView
= new PrinterListView(BRect());
322 BScrollView
* printerScrollView
= new BScrollView("printer_scroller",
323 fPrinterListView
, B_FOLLOW_ALL
, B_WILL_DRAW
| B_FRAME_EVENTS
,
324 false, true, B_FANCY_BORDER
);
326 printerScrollView
->SetExplicitMinSize(
327 BSize(be_plain_font
->Size() * 30, B_SIZE_UNSET
));
329 float padding
= be_control_look
->DefaultItemSpacing();
331 BLayoutBuilder::Group
<>(printersBox
, B_HORIZONTAL
, padding
)
332 .SetInsets(padding
, padding
* 2, padding
, padding
)
333 .Add(printerScrollView
)
334 .AddGroup(B_VERTICAL
, padding
/ 2, 0.0f
)
342 // ------------------------ Lastly, build the jobs overview box
343 fJobsBox
= new BBox("jobsBox");
344 fJobsBox
->SetFont(be_bold_font
);
345 fJobsBox
->SetLabel(B_TRANSLATE("Print jobs: No printer selected"));
348 fCancel
= new BButton("cancel",
349 B_TRANSLATE("Cancel job"), new BMessage(kMsgCancelJob
));
350 fCancel
->SetExplicitMaxSize(
351 BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
353 // Restart Job button
354 fRestart
= new BButton("restart",
355 B_TRANSLATE("Restart job"), new BMessage(kMsgRestartJob
));
356 fRestart
->SetExplicitMaxSize(
357 BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
359 // Disable all selection-based buttons
360 fCancel
->SetEnabled(false);
361 fRestart
->SetEnabled(false);
363 // Create listview with scroller
364 fJobListView
= new JobListView(BRect());
365 BScrollView
* jobScrollView
= new BScrollView("jobs_scroller",
366 fJobListView
, B_FOLLOW_ALL
, B_WILL_DRAW
| B_FRAME_EVENTS
,
367 false, true, B_FANCY_BORDER
);
369 BLayoutBuilder::Group
<>(fJobsBox
, B_HORIZONTAL
, padding
)
370 .SetInsets(padding
, padding
* 2, padding
, padding
)
372 .AddGroup(B_VERTICAL
, padding
/ 2, 0.0f
)
378 BLayoutBuilder::Group
<>(this, B_VERTICAL
, 0)
379 .SetInsets(B_USE_WINDOW_SPACING
)
381 .AddStrut(B_USE_DEFAULT_SPACING
)
384 // There is a better solution?
386 if (fPrintTestPage
->Bounds().Width() > fRestart
->Bounds().Width())
387 fRestart
->SetExplicitMinSize(
388 BSize(fPrintTestPage
->Bounds().Width(), B_SIZE_UNSET
));
390 fPrintTestPage
->SetExplicitMinSize(
391 BSize(fRestart
->Bounds().Width(), B_SIZE_UNSET
));
396 PrintersWindow::_IsSelected(PrinterItem
* printer
)
398 return fSelectedPrinter
&& fSelectedPrinter
== printer
;
403 PrintersWindow::_UpdatePrinterButtons()
405 PrinterItem
* item
= fPrinterListView
->SelectedItem();
406 fRemove
->SetEnabled(item
&& !item
->HasPendingJobs());
407 fMakeDefault
->SetEnabled(item
&& !item
->IsActivePrinter());
408 fPrintTestPage
->SetEnabled(item
);
413 PrintersWindow::_UpdateJobButtons()
415 JobItem
* item
= fJobListView
->SelectedItem();
417 Job
* job
= item
->GetJob();
418 fCancel
->SetEnabled(job
->Status() != kProcessing
);
419 fRestart
->SetEnabled(job
->Status() == kFailed
);
421 fCancel
->SetEnabled(false);
422 fRestart
->SetEnabled(false);