xref: /haiku/src/libs/print/libprint/AddPrinterDlg.cpp (revision b31cb92f29fe89eaca84d173d0f70d38bf0c6a3d)
1 #include "AddPrinterDlg.h"
2 
3 #include <GroupLayoutBuilder.h>
4 
5 #include "PrinterCap.h"
6 #include "PrinterData.h"
7 
8 
9 enum MSGS {
10 	kMsgCancel = 1,
11 	kMsgOK,
12 	kMsgProtocolClassChanged,
13 };
14 
15 
16 ProtocolClassItem::ProtocolClassItem(const ProtocolClassCap* cap)
17 	: BStringItem(cap->fLabel.c_str())
18 	, fProtocolClassCap(cap)
19 {
20 }
21 
22 
23 int
24 ProtocolClassItem::GetProtocolClass() const
25 {
26 	return fProtocolClassCap->fProtocolClass;
27 }
28 
29 const char *
30 ProtocolClassItem::GetDescription() const
31 {
32 	return fProtocolClassCap->fDescription.c_str();
33 }
34 
35 
36 AddPrinterView::AddPrinterView(PrinterData* printerData,
37 	const PrinterCap* printerCap)
38 	:
39 	BView("addPrinter", B_WILL_DRAW),
40 	fPrinterData(printerData),
41 	fPrinterCap(printerCap)
42 {
43 }
44 
45 
46 AddPrinterView::~AddPrinterView()
47 {
48 }
49 
50 
51 void
52 AddPrinterView::AttachedToWindow()
53 {
54 	// protocol class box
55 	BBox* protocolClassBox = new BBox("protocolClass");
56 	protocolClassBox->SetLabel("Protocol classes:");
57 
58 	// protocol class
59 	fProtocolClassList = new BListView("protocolClassList");
60 	fProtocolClassList->SetExplicitMinSize(BSize(500, 200));
61 	BScrollView* protocolClassScroller = new BScrollView(
62 		"protocolClassListScroller",
63 		fProtocolClassList,
64 		0,
65 		false,
66 		true,
67 		B_NO_BORDER);
68 	fProtocolClassList->SetSelectionMessage(
69 		new BMessage(kMsgProtocolClassChanged));
70 	fProtocolClassList->SetTarget(this);
71 
72 	protocolClassBox->AddChild(protocolClassScroller);
73 
74 	int count = fPrinterCap->CountCap(PrinterCap::kProtocolClass);
75 	ProtocolClassCap **protocolClasses =
76 		(ProtocolClassCap **)fPrinterCap->GetCaps(PrinterCap::kProtocolClass);
77 	while (count--) {
78 		const ProtocolClassCap *protocolClass = *protocolClasses;
79 
80 		BStringItem* item = new ProtocolClassItem(protocolClass);
81 		fProtocolClassList->AddItem(item);
82 		if (protocolClass->fIsDefault) {
83 			int index = fProtocolClassList->IndexOf(item);
84 			fProtocolClassList->Select(index);
85 		}
86 		protocolClasses ++;
87 	}
88 
89 	// description of protocol class box
90 	BBox* descriptionBox = new BBox("descriptionBox");
91 	descriptionBox->SetLabel("Description:");
92 
93 	// description of protocol class
94 	fDescription = new BTextView("description");
95 	fDescription->SetExplicitMinSize(BSize(200, 200));
96 	fDescription->SetViewColor(descriptionBox->ViewColor());
97 	BScrollView* descriptionScroller = new BScrollView("descriptionScroller",
98 			fDescription,
99 			0,
100 			false,
101 			true,
102 			B_NO_BORDER);
103 	fDescription->MakeEditable(false);
104 
105 	descriptionBox->AddChild(descriptionScroller);
106 
107 	// separator line
108 	BBox *separator = new BBox("separator");
109 	separator->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
110 
111 	// buttons
112 	BButton* cancel = new BButton("cancel", "Cancel",
113 		new BMessage(kMsgCancel));
114 	BButton* ok = new BButton("ok", "OK", new BMessage(kMsgOK));
115 	ok->MakeDefault(true);
116 
117 	SetLayout(new BGroupLayout(B_VERTICAL));
118 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
119 		.Add(protocolClassBox)
120 		.Add(descriptionBox)
121 		.AddGlue()
122 		.Add(separator)
123 		.AddGroup(B_HORIZONTAL, 10, 1.0f)
124 			.AddGlue()
125 			.Add(cancel)
126 			.Add(ok)
127 		.End()
128 		.SetInsets(0, 0, 0, 0)
129 	);
130 
131 
132 	// update description
133 	BMessage updateDescription(kMsgProtocolClassChanged);
134 	MessageReceived(&updateDescription);
135 }
136 
137 
138 ProtocolClassItem*
139 AddPrinterView::CurrentSelection()
140 {
141 	int selected = fProtocolClassList->CurrentSelection();
142 	if (selected >= 0) {
143 		return (ProtocolClassItem*)fProtocolClassList->ItemAt(selected);
144 	}
145 	return NULL;
146 }
147 
148 
149 void
150 AddPrinterView::MessageReceived(BMessage* msg)
151 {
152 	if (msg->what == kMsgProtocolClassChanged) {
153 		ProtocolClassItem *item = CurrentSelection();
154 		if (item != NULL) {
155 			fDescription->SetText(item->GetDescription());
156 		}
157 	} else {
158 		BView::MessageReceived(msg);
159 	}
160 }
161 
162 
163 void
164 AddPrinterView::Save()
165 {
166 	ProtocolClassItem* item = CurrentSelection();
167 	if (item != NULL) {
168 		fPrinterData->SetProtocolClass(item->GetProtocolClass());
169 		fPrinterData->Save();
170 	}
171 }
172 
173 
174 AddPrinterDlg::AddPrinterDlg(PrinterData* printerData,
175 	const PrinterCap *printerCap)
176 	:
177 	DialogWindow(BRect(100, 100, 120, 120),
178 		"Add printer", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
179 		B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS
180 			| B_AUTO_UPDATE_SIZE_LIMITS)
181 {
182 	SetResult(B_ERROR);
183 
184 	fAddPrinterView = new AddPrinterView(printerData, printerCap);
185 	SetLayout(new BGroupLayout(B_VERTICAL));
186 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
187 		.Add(fAddPrinterView)
188 		.SetInsets(10, 10, 10, 10)
189 	);
190 }
191 
192 
193 void
194 AddPrinterDlg::MessageReceived(BMessage* msg)
195 {
196 	switch (msg->what) {
197 		case kMsgOK:
198 			fAddPrinterView->Save();
199 			SetResult(B_NO_ERROR);
200 			PostMessage(B_QUIT_REQUESTED);
201 			break;
202 
203 		case kMsgCancel:
204 			PostMessage(B_QUIT_REQUESTED);
205 			break;
206 
207 		default:
208 			DialogWindow::MessageReceived(msg);
209 			break;
210 	}
211 }
212