2 * Copyright 2010, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Ithamar R. Adema <ithamar.adema@team-embedded.nl>
11 #include "SelectPrinterDialog.h"
13 #include <GroupLayout.h>
14 #include <GroupLayoutBuilder.h>
18 #include <Directory.h>
23 #include <ScrollBar.h>
24 #include <ScrollView.h>
26 #include <StringItem.h>
28 #include "GPBinding.h"
35 kMsgManufacturerSelected
= 'msel',
36 kMsgPrinterSelected
= 'psel',
40 class StringValueItem
: public BStringItem
{
42 StringValueItem(const BString
& text
, const BString
& value
)
44 BStringItem(text
.String()),
53 SelectPrinterDialog::SelectPrinterDialog(GPData
* data
)
55 DialogWindow(BRect(100, 100, 500, 500),
56 "Select Printer", B_TITLED_WINDOW_LOOK
, B_MODAL_APP_WINDOW_FEEL
,
57 B_NOT_MINIMIZABLE
| B_NOT_ZOOMABLE
| B_ASYNCHRONOUS_CONTROLS
66 ok
= new BButton("btn:ok", "OK", new BMessage(kMsgOK
));
67 ok
->MakeDefault(true);
68 ok
->SetEnabled(false);
71 cancel
= new BButton("btn:cancel", "Cancel", new BMessage(kMsgCancel
));
73 BScrollView
* manuScroller
, *printerScroller
;
74 fManufacturersListView
= new BListView("olv:manufacturers");
75 manuScroller
= new BScrollView("scr:manufacturers", fManufacturersListView
,
77 fPrintersListView
= new BListView("olv:printers");
78 printerScroller
= new BScrollView("scr:printers", fPrintersListView
, 0,
81 fPrintersListView
->SetSelectionMessage(new BMessage(kMsgPrinterSelected
));
82 fManufacturersListView
->SetSelectionMessage(
83 new BMessage(kMsgManufacturerSelected
));
84 PopulateManufacturers();
87 SetLayout(new BGroupLayout(B_VERTICAL
));
89 AddChild(BGroupLayoutBuilder(B_VERTICAL
, 10)
90 .AddGroup(B_HORIZONTAL
, 5, 1.0f
)
94 .AddGroup(B_HORIZONTAL
, 5, 2.0f
)
99 .SetInsets(10, 10, 10, 10)
105 SelectPrinterDialog::PopulateManufacturers()
107 BMessage manufacturers
;
109 binding
.GetPrinterManufacturers(manufacturers
);
113 for (int32 index
= 0; binding
.ExtractManufacturer(manufacturers
, index
, id
,
114 displayName
); index
++) {
115 fManufacturersListView
->AddItem(new StringValueItem(id
, displayName
));
121 SelectPrinterDialog::PopulatePrinters()
123 BString manufacturer
= GetSelectedItemValue(fManufacturersListView
).String();
124 if (manufacturer
.Length() == 0)
129 binding
.GetPrinterModels(manufacturer
, models
);
133 for (int32 index
= 0; binding
.ExtractModel(models
, index
, displayName
, driver
);
135 StringValueItem
* item
= new StringValueItem(displayName
, driver
);
136 fPrintersListView
->AddItem(item
);
142 SelectPrinterDialog::GetSelectedItemValue(BListView
* listView
)
144 int32 index
= listView
->CurrentSelection();
146 // Bail out if no manufacturer is selected
149 BListItem
* item
= listView
->ItemAt(index
);
150 StringValueItem
* valueItem
= dynamic_cast<StringValueItem
*>(item
);
151 ASSERT(valueItem
!= NULL
);
153 return valueItem
->fValue
;
157 SelectPrinterDialog::PrinterSelected()
159 int32 index
= fPrintersListView
->CurrentSelection();
160 fOKButton
->SetEnabled(index
>= 0);
165 SelectPrinterDialog::Save()
167 BString driverName
= GetSelectedItemValue(fPrintersListView
);
168 fData
->fGutenprintDriverName
= driverName
;
174 SelectPrinterDialog::MessageReceived(BMessage
* msg
)
177 case kMsgManufacturerSelected
:
178 fPrintersListView
->MakeEmpty();
182 case kMsgPrinterSelected
:
187 SetResult(B_NO_ERROR
);
188 PostMessage(B_QUIT_REQUESTED
);
191 PostMessage(B_QUIT_REQUESTED
);
194 DialogWindow::MessageReceived(msg
);