xref: /haiku/src/libs/print/libprint/AddPrinterDlg.cpp (revision 5f050a9e7a76e187d085f9f2fc256227be5e25a9)
112fd6cc2SKarsten Heimrich #include "AddPrinterDlg.h"
212fd6cc2SKarsten Heimrich 
3bc966575SMichael Pfeiffer #include <GroupLayoutBuilder.h>
4bc966575SMichael Pfeiffer 
512fd6cc2SKarsten Heimrich #include "PrinterCap.h"
612fd6cc2SKarsten Heimrich #include "PrinterData.h"
712fd6cc2SKarsten Heimrich 
812fd6cc2SKarsten Heimrich 
912fd6cc2SKarsten Heimrich enum MSGS {
1012fd6cc2SKarsten Heimrich 	kMsgCancel = 1,
1112fd6cc2SKarsten Heimrich 	kMsgOK,
1212fd6cc2SKarsten Heimrich 	kMsgProtocolClassChanged,
1312fd6cc2SKarsten Heimrich };
1412fd6cc2SKarsten Heimrich 
15299710a7SMichael Pfeiffer 
ProtocolClassItem(const ProtocolClassCap * cap)1612fd6cc2SKarsten Heimrich ProtocolClassItem::ProtocolClassItem(const ProtocolClassCap* cap)
17a7dde370SMichael Pfeiffer 	: BStringItem(cap->fLabel.c_str())
1812fd6cc2SKarsten Heimrich 	, fProtocolClassCap(cap)
1912fd6cc2SKarsten Heimrich {
2012fd6cc2SKarsten Heimrich }
2112fd6cc2SKarsten Heimrich 
22299710a7SMichael Pfeiffer 
2312fd6cc2SKarsten Heimrich int
GetProtocolClass() const244410ee12SMichael Pfeiffer ProtocolClassItem::GetProtocolClass() const
2512fd6cc2SKarsten Heimrich {
26a7dde370SMichael Pfeiffer 	return fProtocolClassCap->fProtocolClass;
2712fd6cc2SKarsten Heimrich }
2812fd6cc2SKarsten Heimrich 
2912fd6cc2SKarsten Heimrich const char *
GetDescription() const304410ee12SMichael Pfeiffer ProtocolClassItem::GetDescription() const
3112fd6cc2SKarsten Heimrich {
32a7dde370SMichael Pfeiffer 	return fProtocolClassCap->fDescription.c_str();
3312fd6cc2SKarsten Heimrich }
3412fd6cc2SKarsten Heimrich 
3512fd6cc2SKarsten Heimrich 
AddPrinterView(PrinterData * printerData,const PrinterCap * printerCap)36bc966575SMichael Pfeiffer AddPrinterView::AddPrinterView(PrinterData* printerData,
37299710a7SMichael Pfeiffer 	const PrinterCap* printerCap)
38299710a7SMichael Pfeiffer 	:
39bc966575SMichael Pfeiffer 	BView("addPrinter", B_WILL_DRAW),
40299710a7SMichael Pfeiffer 	fPrinterData(printerData),
41299710a7SMichael Pfeiffer 	fPrinterCap(printerCap)
4212fd6cc2SKarsten Heimrich {
4312fd6cc2SKarsten Heimrich }
4412fd6cc2SKarsten Heimrich 
45299710a7SMichael Pfeiffer 
~AddPrinterView()4612fd6cc2SKarsten Heimrich AddPrinterView::~AddPrinterView()
4712fd6cc2SKarsten Heimrich {
4812fd6cc2SKarsten Heimrich }
4912fd6cc2SKarsten Heimrich 
50299710a7SMichael Pfeiffer 
5112fd6cc2SKarsten Heimrich void
AttachedToWindow()5212fd6cc2SKarsten Heimrich AddPrinterView::AttachedToWindow()
5312fd6cc2SKarsten Heimrich {
54299710a7SMichael Pfeiffer 	// protocol class box
55bc966575SMichael Pfeiffer 	BBox* protocolClassBox = new BBox("protocolClass");
56*5f050a9eSHumdinger 	protocolClassBox->SetLabel("Protocol classes:");
5712fd6cc2SKarsten Heimrich 
58299710a7SMichael Pfeiffer 	// protocol class
59bc966575SMichael Pfeiffer 	fProtocolClassList = new BListView("protocolClassList");
60bc966575SMichael Pfeiffer 	fProtocolClassList->SetExplicitMinSize(BSize(500, 200));
61bc966575SMichael Pfeiffer 	BScrollView* protocolClassScroller = new BScrollView(
6212fd6cc2SKarsten Heimrich 		"protocolClassListScroller",
6312fd6cc2SKarsten Heimrich 		fProtocolClassList,
6412fd6cc2SKarsten Heimrich 		0,
6512fd6cc2SKarsten Heimrich 		false,
66bc966575SMichael Pfeiffer 		true,
67bc966575SMichael Pfeiffer 		B_NO_BORDER);
68299710a7SMichael Pfeiffer 	fProtocolClassList->SetSelectionMessage(
69299710a7SMichael Pfeiffer 		new BMessage(kMsgProtocolClassChanged));
7012fd6cc2SKarsten Heimrich 	fProtocolClassList->SetTarget(this);
7112fd6cc2SKarsten Heimrich 
72bc966575SMichael Pfeiffer 	protocolClassBox->AddChild(protocolClassScroller);
73bc966575SMichael Pfeiffer 
744410ee12SMichael Pfeiffer 	int count = fPrinterCap->CountCap(PrinterCap::kProtocolClass);
75299710a7SMichael Pfeiffer 	ProtocolClassCap **protocolClasses =
764410ee12SMichael Pfeiffer 		(ProtocolClassCap **)fPrinterCap->GetCaps(PrinterCap::kProtocolClass);
7712fd6cc2SKarsten Heimrich 	while (count--) {
7812fd6cc2SKarsten Heimrich 		const ProtocolClassCap *protocolClass = *protocolClasses;
7912fd6cc2SKarsten Heimrich 
8012fd6cc2SKarsten Heimrich 		BStringItem* item = new ProtocolClassItem(protocolClass);
8112fd6cc2SKarsten Heimrich 		fProtocolClassList->AddItem(item);
82a7dde370SMichael Pfeiffer 		if (protocolClass->fIsDefault) {
8312fd6cc2SKarsten Heimrich 			int index = fProtocolClassList->IndexOf(item);
8412fd6cc2SKarsten Heimrich 			fProtocolClassList->Select(index);
8512fd6cc2SKarsten Heimrich 		}
8612fd6cc2SKarsten Heimrich 		protocolClasses ++;
8712fd6cc2SKarsten Heimrich 	}
8812fd6cc2SKarsten Heimrich 
89299710a7SMichael Pfeiffer 	// description of protocol class box
90bc966575SMichael Pfeiffer 	BBox* descriptionBox = new BBox("descriptionBox");
91bc966575SMichael Pfeiffer 	descriptionBox->SetLabel("Description:");
9212fd6cc2SKarsten Heimrich 
93299710a7SMichael Pfeiffer 	// description of protocol class
94bc966575SMichael Pfeiffer 	fDescription = new BTextView("description");
95bc966575SMichael Pfeiffer 	fDescription->SetExplicitMinSize(BSize(200, 200));
96bc966575SMichael Pfeiffer 	fDescription->SetViewColor(descriptionBox->ViewColor());
97bc966575SMichael Pfeiffer 	BScrollView* descriptionScroller = new BScrollView("descriptionScroller",
98bc966575SMichael Pfeiffer 			fDescription,
99bc966575SMichael Pfeiffer 			0,
100bc966575SMichael Pfeiffer 			false,
101bc966575SMichael Pfeiffer 			true,
102bc966575SMichael Pfeiffer 			B_NO_BORDER);
10312fd6cc2SKarsten Heimrich 	fDescription->MakeEditable(false);
10412fd6cc2SKarsten Heimrich 
105bc966575SMichael Pfeiffer 	descriptionBox->AddChild(descriptionScroller);
10612fd6cc2SKarsten Heimrich 
107bc966575SMichael Pfeiffer 	// separator line
108bc966575SMichael Pfeiffer 	BBox *separator = new BBox("separator");
109bc966575SMichael Pfeiffer 	separator->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
110bc966575SMichael Pfeiffer 
111bc966575SMichael Pfeiffer 	// buttons
112bc966575SMichael Pfeiffer 	BButton* cancel = new BButton("cancel", "Cancel",
113bc966575SMichael Pfeiffer 		new BMessage(kMsgCancel));
114bc966575SMichael Pfeiffer 	BButton* ok = new BButton("ok", "OK", new BMessage(kMsgOK));
115bc966575SMichael Pfeiffer 	ok->MakeDefault(true);
116bc966575SMichael Pfeiffer 
117bc966575SMichael Pfeiffer 	SetLayout(new BGroupLayout(B_VERTICAL));
118bc966575SMichael Pfeiffer 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
119bc966575SMichael Pfeiffer 		.Add(protocolClassBox)
120bc966575SMichael Pfeiffer 		.Add(descriptionBox)
121bc966575SMichael Pfeiffer 		.AddGlue()
122bc966575SMichael Pfeiffer 		.Add(separator)
123bc966575SMichael Pfeiffer 		.AddGroup(B_HORIZONTAL, 10, 1.0f)
124bc966575SMichael Pfeiffer 			.AddGlue()
125bc966575SMichael Pfeiffer 			.Add(cancel)
126bc966575SMichael Pfeiffer 			.Add(ok)
127bc966575SMichael Pfeiffer 		.End()
128bc966575SMichael Pfeiffer 		.SetInsets(0, 0, 0, 0)
129bc966575SMichael Pfeiffer 	);
130bc966575SMichael Pfeiffer 
13112fd6cc2SKarsten Heimrich 
13212fd6cc2SKarsten Heimrich 	// update description
13312fd6cc2SKarsten Heimrich 	BMessage updateDescription(kMsgProtocolClassChanged);
13412fd6cc2SKarsten Heimrich 	MessageReceived(&updateDescription);
13512fd6cc2SKarsten Heimrich }
13612fd6cc2SKarsten Heimrich 
137299710a7SMichael Pfeiffer 
13812fd6cc2SKarsten Heimrich ProtocolClassItem*
CurrentSelection()13912fd6cc2SKarsten Heimrich AddPrinterView::CurrentSelection()
14012fd6cc2SKarsten Heimrich {
14112fd6cc2SKarsten Heimrich 	int selected = fProtocolClassList->CurrentSelection();
14212fd6cc2SKarsten Heimrich 	if (selected >= 0) {
14312fd6cc2SKarsten Heimrich 		return (ProtocolClassItem*)fProtocolClassList->ItemAt(selected);
14412fd6cc2SKarsten Heimrich 	}
14512fd6cc2SKarsten Heimrich 	return NULL;
14612fd6cc2SKarsten Heimrich }
14712fd6cc2SKarsten Heimrich 
148299710a7SMichael Pfeiffer 
14912fd6cc2SKarsten Heimrich void
MessageReceived(BMessage * msg)15012fd6cc2SKarsten Heimrich AddPrinterView::MessageReceived(BMessage* msg)
15112fd6cc2SKarsten Heimrich {
15212fd6cc2SKarsten Heimrich 	if (msg->what == kMsgProtocolClassChanged) {
15312fd6cc2SKarsten Heimrich 		ProtocolClassItem *item = CurrentSelection();
15412fd6cc2SKarsten Heimrich 		if (item != NULL) {
1554410ee12SMichael Pfeiffer 			fDescription->SetText(item->GetDescription());
15612fd6cc2SKarsten Heimrich 		}
15712fd6cc2SKarsten Heimrich 	} else {
15812fd6cc2SKarsten Heimrich 		BView::MessageReceived(msg);
15912fd6cc2SKarsten Heimrich 	}
16012fd6cc2SKarsten Heimrich }
16112fd6cc2SKarsten Heimrich 
162299710a7SMichael Pfeiffer 
16312fd6cc2SKarsten Heimrich void
Save()16412fd6cc2SKarsten Heimrich AddPrinterView::Save()
16512fd6cc2SKarsten Heimrich {
16612fd6cc2SKarsten Heimrich 	ProtocolClassItem* item = CurrentSelection();
16712fd6cc2SKarsten Heimrich 	if (item != NULL) {
1684410ee12SMichael Pfeiffer 		fPrinterData->SetProtocolClass(item->GetProtocolClass());
1694410ee12SMichael Pfeiffer 		fPrinterData->Save();
17012fd6cc2SKarsten Heimrich 	}
17112fd6cc2SKarsten Heimrich }
17212fd6cc2SKarsten Heimrich 
173299710a7SMichael Pfeiffer 
AddPrinterDlg(PrinterData * printerData,const PrinterCap * printerCap)174299710a7SMichael Pfeiffer AddPrinterDlg::AddPrinterDlg(PrinterData* printerData,
175299710a7SMichael Pfeiffer 	const PrinterCap *printerCap)
176299710a7SMichael Pfeiffer 	:
177bc966575SMichael Pfeiffer 	DialogWindow(BRect(100, 100, 120, 120),
178*5f050a9eSHumdinger 		"Add printer", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
179bc966575SMichael Pfeiffer 		B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS
180bc966575SMichael Pfeiffer 			| B_AUTO_UPDATE_SIZE_LIMITS)
18112fd6cc2SKarsten Heimrich {
18212fd6cc2SKarsten Heimrich 	SetResult(B_ERROR);
18312fd6cc2SKarsten Heimrich 
184bc966575SMichael Pfeiffer 	fAddPrinterView = new AddPrinterView(printerData, printerCap);
185bc966575SMichael Pfeiffer 	SetLayout(new BGroupLayout(B_VERTICAL));
186bc966575SMichael Pfeiffer 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
187bc966575SMichael Pfeiffer 		.Add(fAddPrinterView)
188bc966575SMichael Pfeiffer 		.SetInsets(10, 10, 10, 10)
189bc966575SMichael Pfeiffer 	);
19012fd6cc2SKarsten Heimrich }
19112fd6cc2SKarsten Heimrich 
192299710a7SMichael Pfeiffer 
19312fd6cc2SKarsten Heimrich void
MessageReceived(BMessage * msg)19412fd6cc2SKarsten Heimrich AddPrinterDlg::MessageReceived(BMessage* msg)
19512fd6cc2SKarsten Heimrich {
19612fd6cc2SKarsten Heimrich 	switch (msg->what) {
19712fd6cc2SKarsten Heimrich 		case kMsgOK:
19812fd6cc2SKarsten Heimrich 			fAddPrinterView->Save();
19912fd6cc2SKarsten Heimrich 			SetResult(B_NO_ERROR);
20012fd6cc2SKarsten Heimrich 			PostMessage(B_QUIT_REQUESTED);
20112fd6cc2SKarsten Heimrich 			break;
20212fd6cc2SKarsten Heimrich 
20312fd6cc2SKarsten Heimrich 		case kMsgCancel:
20412fd6cc2SKarsten Heimrich 			PostMessage(B_QUIT_REQUESTED);
20512fd6cc2SKarsten Heimrich 			break;
20612fd6cc2SKarsten Heimrich 
20712fd6cc2SKarsten Heimrich 		default:
20812fd6cc2SKarsten Heimrich 			DialogWindow::MessageReceived(msg);
20912fd6cc2SKarsten Heimrich 			break;
21012fd6cc2SKarsten Heimrich 	}
21112fd6cc2SKarsten Heimrich }
212