xref: /haiku/src/libs/print/libprint/AddPrinterDlg.cpp (revision 299710a73104f4bbd41f01e137758d1c53d7c4b9)
1 #include "AddPrinterDlg.h"
2 
3 #include "PrinterCap.h"
4 #include "PrinterData.h"
5 
6 #define DIALOG_WIDTH 400
7 
8 #define BUTTON_WIDTH		70
9 #define BUTTON_HEIGHT		20
10 #define TEXT_HEIGHT         16
11 
12 #define BOX_TOP_INSET       18
13 #define BOX_BOTTOM_INSET    10
14 #define BOX_H_INSET         10
15 
16 #define PROTOCOL_CLASS_BOX_H      10
17 #define PROTOCOL_CLASS_BOX_V      10
18 #define PROTOCOL_CLASS_BOX_WIDTH  DIALOG_WIDTH - 20
19 #define PROTOCOL_CLASS_BOX_HEIGHT 125
20 
21 #define PROTOCOL_CLASS_H          BOX_H_INSET
22 #define PROTOCOL_CLASS_V          BOX_TOP_INSET
23 #define PROTOCOL_CLASS_WIDTH      PROTOCOL_CLASS_BOX_WIDTH - 2 * BOX_H_INSET
24 #define PROTOCOL_CLASS_HEIGHT     PROTOCOL_CLASS_BOX_HEIGHT - BOX_TOP_INSET - BOX_BOTTOM_INSET
25 
26 #define DESCRIPTION_BOX_H         PROTOCOL_CLASS_BOX_H
27 #define DESCRIPTION_BOX_V         PROTOCOL_CLASS_BOX_V + PROTOCOL_CLASS_BOX_HEIGHT + 10
28 #define DESCRIPTION_BOX_WIDTH     PROTOCOL_CLASS_BOX_WIDTH
29 #define DESCRIPTION_BOX_HEIGHT    125
30 
31 #define DESCRIPTION_H         BOX_H_INSET
32 #define DESCRIPTION_V         BOX_TOP_INSET
33 #define DESCRIPTION_WIDTH     DESCRIPTION_BOX_WIDTH - 2 * BOX_H_INSET
34 #define DESCRIPTION_HEIGHT    DESCRIPTION_BOX_HEIGHT - BOX_TOP_INSET - BOX_BOTTOM_INSET
35 
36 #define DIALOG_HEIGHT         DESCRIPTION_BOX_V + DESCRIPTION_BOX_HEIGHT + BUTTON_HEIGHT + 20
37 
38 #define OK_H				(DIALOG_WIDTH  - BUTTON_WIDTH  - 11)
39 #define OK_V				(DIALOG_HEIGHT - BUTTON_HEIGHT - 11)
40 #define OK_TEXT				"OK"
41 
42 #define CANCEL_H			(OK_H - BUTTON_WIDTH - 12)
43 #define CANCEL_V			OK_V
44 #define CANCEL_TEXT			"Cancel"
45 
46 const BRect PROTOCOL_CLASS_BOX_RECT(
47 	PROTOCOL_CLASS_BOX_H,
48 	PROTOCOL_CLASS_BOX_V,
49 	PROTOCOL_CLASS_BOX_H + PROTOCOL_CLASS_BOX_WIDTH ,
50 	PROTOCOL_CLASS_BOX_V + PROTOCOL_CLASS_BOX_HEIGHT);
51 
52 const BRect PROTOCOL_CLASS_RECT(
53 	PROTOCOL_CLASS_H,
54 	PROTOCOL_CLASS_V,
55 	PROTOCOL_CLASS_H + PROTOCOL_CLASS_WIDTH - B_V_SCROLL_BAR_WIDTH,
56 	PROTOCOL_CLASS_V + PROTOCOL_CLASS_HEIGHT);
57 
58 const BRect DESCRIPTION_BOX_RECT(
59 	DESCRIPTION_BOX_H,
60 	DESCRIPTION_BOX_V,
61 	DESCRIPTION_BOX_H + DESCRIPTION_BOX_WIDTH,
62 	DESCRIPTION_BOX_V + DESCRIPTION_BOX_HEIGHT);
63 
64 const BRect DESCRIPTION_RECT(
65 	DESCRIPTION_H,
66 	DESCRIPTION_V,
67 	DESCRIPTION_H + DESCRIPTION_WIDTH - B_V_SCROLL_BAR_WIDTH,
68 	DESCRIPTION_V + DESCRIPTION_HEIGHT);
69 
70 const BRect OK_RECT(
71 	OK_H,
72 	OK_V,
73 	OK_H + BUTTON_WIDTH,
74 	OK_V + BUTTON_HEIGHT);
75 
76 const BRect CANCEL_RECT(
77 	CANCEL_H,
78 	CANCEL_V,
79 	CANCEL_H + BUTTON_WIDTH,
80 	CANCEL_V + BUTTON_HEIGHT);
81 
82 
83 enum MSGS {
84 	kMsgCancel = 1,
85 	kMsgOK,
86 	kMsgProtocolClassChanged,
87 };
88 
89 
90 ProtocolClassItem::ProtocolClassItem(const ProtocolClassCap* cap)
91 	: BStringItem(cap->fLabel.c_str())
92 	, fProtocolClassCap(cap)
93 {
94 }
95 
96 
97 int
98 ProtocolClassItem::getProtocolClass()
99 {
100 	return fProtocolClassCap->fProtocolClass;
101 }
102 
103 const char *
104 ProtocolClassItem::getDescription()
105 {
106 	return fProtocolClassCap->fDescription.c_str();
107 }
108 
109 
110 AddPrinterView::AddPrinterView(BRect frame, PrinterData* printerData,
111 	const PrinterCap* printerCap)
112 	:
113 	BView(frame, "", B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS),
114 	fPrinterData(printerData),
115 	fPrinterCap(printerCap)
116 {
117 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
118 }
119 
120 
121 AddPrinterView::~AddPrinterView()
122 {
123 }
124 
125 
126 void
127 AddPrinterView::AttachedToWindow()
128 {
129 	// protocol class box
130 	BBox *box;
131 	box = new BBox(PROTOCOL_CLASS_BOX_RECT, NULL,
132 		B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
133 	box->SetLabel("Protocol Classes:");
134 	AddChild(box);
135 
136 	// protocol class
137 	fProtocolClassList = new BListView(
138 		PROTOCOL_CLASS_RECT,
139 		"protocolClassList",
140 		B_SINGLE_SELECTION_LIST,
141 		B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP);
142 	box->AddChild(new BScrollView(
143 		"protocolClassListScroller",
144 		fProtocolClassList,
145 		B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP,
146 		0,
147 		false,
148 		true));
149 	fProtocolClassList->SetSelectionMessage(
150 		new BMessage(kMsgProtocolClassChanged));
151 	fProtocolClassList->SetTarget(this);
152 
153 	int count = fPrinterCap->countCap(PrinterCap::kProtocolClass);
154 	ProtocolClassCap **protocolClasses =
155 		(ProtocolClassCap **)fPrinterCap->enumCap(PrinterCap::kProtocolClass);
156 	while (count--) {
157 		const ProtocolClassCap *protocolClass = *protocolClasses;
158 
159 		BStringItem* item = new ProtocolClassItem(protocolClass);
160 		fProtocolClassList->AddItem(item);
161 		if (protocolClass->fIsDefault) {
162 			int index = fProtocolClassList->IndexOf(item);
163 			fProtocolClassList->Select(index);
164 		}
165 		protocolClasses ++;
166 	}
167 
168 	// description of protocol class box
169 	box = new BBox(DESCRIPTION_BOX_RECT, NULL, B_FOLLOW_ALL_SIDES);
170 	box->SetLabel("Description:");
171 	AddChild(box);
172 
173 	// description of protocol class
174 	BRect textRect(DESCRIPTION_RECT);
175 	textRect.OffsetTo(0, 0);
176 	fDescription = new BTextView(DESCRIPTION_RECT, "description", textRect,
177 		B_FOLLOW_ALL_SIDES, B_WILL_DRAW);
178 	fDescription->SetViewColor(box->ViewColor());
179 	box->AddChild(new BScrollView("descriptionScroller", fDescription,
180 		B_FOLLOW_ALL_SIDES, 0, false, true, B_NO_BORDER));
181 	fDescription->MakeEditable(false);
182 
183 	// cancel
184 	BButton *button;
185 	button = new BButton(CANCEL_RECT, "", CANCEL_TEXT, new BMessage(kMsgCancel),
186 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
187 	AddChild(button);
188 
189 	// ok
190 	button = new BButton(OK_RECT, "", OK_TEXT, new BMessage(kMsgOK),
191 		B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
192 	AddChild(button);
193 	button->MakeDefault(true);
194 
195 	// update description
196 	BMessage updateDescription(kMsgProtocolClassChanged);
197 	MessageReceived(&updateDescription);
198 }
199 
200 
201 void
202 AddPrinterView::FrameResized(float w, float h)
203 {
204 	BView::FrameResized(w, h);
205 	// update text rectangle
206 	BRect rect(fDescription->TextRect());
207 	rect.right = rect.left + fDescription->Frame().Width();
208 	fDescription->SetTextRect(rect);
209 }
210 
211 
212 ProtocolClassItem*
213 AddPrinterView::CurrentSelection()
214 {
215 	int selected = fProtocolClassList->CurrentSelection();
216 	if (selected >= 0) {
217 		return (ProtocolClassItem*)fProtocolClassList->ItemAt(selected);
218 	}
219 	return NULL;
220 }
221 
222 
223 void
224 AddPrinterView::MessageReceived(BMessage* msg)
225 {
226 	if (msg->what == kMsgProtocolClassChanged) {
227 		ProtocolClassItem *item = CurrentSelection();
228 		if (item != NULL) {
229 			fDescription->SetText(item->getDescription());
230 		}
231 	} else {
232 		BView::MessageReceived(msg);
233 	}
234 }
235 
236 
237 void
238 AddPrinterView::Save()
239 {
240 	ProtocolClassItem* item = CurrentSelection();
241 	if (item != NULL) {
242 		fPrinterData->setProtocolClass(item->getProtocolClass());
243 		fPrinterData->save();
244 	}
245 }
246 
247 
248 AddPrinterDlg::AddPrinterDlg(PrinterData* printerData,
249 	const PrinterCap *printerCap)
250 	:
251 	DialogWindow(BRect(100, 100, 100 + DIALOG_WIDTH, 100 + DIALOG_HEIGHT),
252 		"Add Printer", B_TITLED_WINDOW_LOOK, B_MODAL_APP_WINDOW_FEEL,
253 		B_NOT_MINIMIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS)
254 {
255 	SetResult(B_ERROR);
256 
257 	// increase min. window size
258 	float minWidth, maxWidth, minHeight, maxHeight;
259 	GetSizeLimits(&minWidth, &maxWidth, &minHeight, &maxHeight);
260 	minHeight = DIALOG_HEIGHT;
261 	minWidth = DIALOG_WIDTH;
262 	SetSizeLimits(minWidth, maxWidth, minHeight, maxHeight);
263 
264 	fAddPrinterView = new AddPrinterView(Bounds(), printerData, printerCap);
265 	AddChild(fAddPrinterView);
266 }
267 
268 
269 void
270 AddPrinterDlg::MessageReceived(BMessage* msg)
271 {
272 	switch (msg->what) {
273 		case kMsgOK:
274 			fAddPrinterView->Save();
275 			SetResult(B_NO_ERROR);
276 			PostMessage(B_QUIT_REQUESTED);
277 			break;
278 
279 		case kMsgCancel:
280 			PostMessage(B_QUIT_REQUESTED);
281 			break;
282 
283 		default:
284 			DialogWindow::MessageReceived(msg);
285 			break;
286 	}
287 }
288