xref: /haiku/src/add-ons/print/drivers/preview/JobSetupWindow.cpp (revision 93a78ecaa45114d68952d08c4778f073515102f2)
1 /*
2  * Copyright 2003-2007, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Philippe Houdoin
7  *		Simon Gauvin
8  *		Michael Pfeiffer
9  */
10 
11 #include <InterfaceKit.h>
12 #include <SupportKit.h>
13 #include <stdlib.h>
14 
15 #include "PrinterDriver.h"
16 #include "JobSetupWindow.h"
17 
18 // --------------------------------------------------
19 JobSetupWindow::JobSetupWindow(BMessage *msg, const char * printerName)
20 	:	BlockingWindow(BRect(0, 0, 320, 160), "Job Setup", B_TITLED_WINDOW_LOOK,
21  			B_MODAL_APP_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_MINIMIZABLE |
22  			B_NOT_ZOOMABLE)
23 {
24 	MoveTo(300, 300);
25 
26 	fSetupMsg	= msg;
27 
28 	if (printerName) {
29 		BString	title;
30 		title << printerName << " Job Setup";
31 		SetTitle(title.String());
32 		fPrinterName = printerName;
33 	}
34 
35 	// ---- Ok, build a default job setup user interface
36 	BRect			r;
37 	BBox			*panel;
38 	BBox			*line;
39 	BButton	 		*ok;
40 	BButton			*cancel;
41 	BStringView		*sv;
42 	float			x, y, w, h;
43 	float			indent;
44 	int32           copies;
45 	int32           firstPage;
46 	int32           lastPage;
47 	bool            allPages;
48 	char            buffer[80];
49 
50 	// PrinterDriver ensures that property exists
51 	fSetupMsg->FindInt32("copies",     &copies);
52 	fSetupMsg->FindInt32("first_page", &firstPage);
53 	fSetupMsg->FindInt32("last_page",  &lastPage);
54 
55 	allPages = firstPage == 1 && lastPage == MAX_INT32;
56 
57 	r = Bounds();
58 
59 	// add a *dialog* background
60 	panel = new BBox(r, "top_panel", B_FOLLOW_ALL,
61 		B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP,
62 		B_PLAIN_BORDER);
63 
64 	const int kMargin = 6;
65 
66 	//const char *kCopiesLabel				= "Copies:";
67 	const char *kCopiesLabelExtraSpace		= "Copies:##";
68 	const char *kPagesRangeLabel			= "Pages:";
69 	const char *kAllPagesLabel				= "All";
70 	const char *kPagesRangeSelectionLabel	= "";
71 	const char *kFromLabel					= "From:";
72 	const char *kFromLabelExtraSpace		= "From:##";
73 	const char *kToLabel					= "To:";
74 	const char *kToLabelExtraSpace			= "To:##";
75 
76 	r = panel->Bounds();
77 
78 	x = r.left + kMargin;
79 	y = r.top + kMargin;
80 
81 
82 	// add a "copies" input field
83 
84 /* Simon: temporarily removed this code
85 	sprintf(buffer, "%d", (int)copies);
86 	fCopies = new BTextControl(BRect(x, y, x+100, y+20), "copies", kCopiesLabel,
87 								buffer, new BMessage(NB_COPIES_MSG));
88 	fCopies->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
89 	fCopies->ResizeToPreferred();
90 	fCopies->GetPreferredSize(&w, &h);
91 	panel->AddChild(fCopies);
92 
93 	y += h + kMargin;	// "new line"
94 */
95 	// add a "pages" label
96 	sv = new BStringView(BRect(x, y, x+100, y+20), "pages_range", kPagesRangeLabel);
97 	panel->AddChild(sv);
98 	sv->ResizeToPreferred();
99 	sv->GetPreferredSize(&w, &h);
100 
101 	// align "copies" textcontrol field on the "allPages" radiobutton bellow...
102 	indent = be_plain_font->StringWidth(kCopiesLabelExtraSpace);
103 	w += kMargin;
104 	if ( w > indent )
105 		indent = w;
106 	// fCopies->SetDivider(indent);
107 
108 	x += indent;
109 
110 	// add a "all" radiobutton
111 	fAll = new BRadioButton(BRect(x, y, x+100, y+20), "all_pages", kAllPagesLabel,
112 						new BMessage(ALL_PAGES_MGS));
113 	fAll->ResizeToPreferred();
114 	fAll->GetPreferredSize(&w, &h);
115 	fAll->SetValue(allPages);
116 	panel->AddChild(fAll);
117 
118 	y += h + kMargin;	// "new line"
119 
120 	// add a range selection raddiobutton
121 	fRange = new BRadioButton(BRect(x, y, x+100, y+20), "pages_range_selection", kPagesRangeSelectionLabel,
122 						new BMessage(RANGE_SELECTION_MSG));
123 	fRange->ResizeToPreferred();
124 	fRange->GetPreferredSize(&w, &h);
125 	fRange->SetValue(!allPages);
126 	panel->AddChild(fRange);
127 
128 	x += w + kMargin;
129 
130 	// add a "from" field
131 	if (allPages) {
132 		buffer[0] = 0;
133 	} else {
134 		sprintf(buffer, "%d", (int)firstPage);
135 	}
136 	fFrom = new BTextControl(BRect(x, y, x+100, y+20), "from_field", kFromLabel, buffer,
137 							new BMessage(RANGE_FROM_MSG));
138 	fFrom->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
139 	fFrom->SetDivider(be_plain_font->StringWidth(kFromLabelExtraSpace));
140 	fFrom->ResizeToPreferred();
141 	fFrom->GetPreferredSize(&w, &h);
142 	panel->AddChild(fFrom);
143 
144 	x += w + kMargin;
145 
146 	// add a "to" field
147 	if (allPages) {
148 		buffer[0] = 0;
149 	} else {
150 		sprintf(buffer, "%d", (int)lastPage);
151 	}
152 	fTo = new BTextControl(BRect(x, y, x+100, y+20), "to_field", kToLabel, buffer,
153 							new BMessage(RANGE_TO_MSG));
154 	fTo->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
155 	fTo->SetDivider(be_plain_font->StringWidth(kToLabelExtraSpace));
156 	fTo->ResizeToPreferred();
157 	fTo->GetPreferredSize(&w, &h);
158 	panel->AddChild(fTo);
159 
160 	y += h + kMargin + kMargin;	// "new line"
161 	x = r.left + kMargin;
162 
163 	// add a separator line...
164 	line = new BBox(BRect(r.left, y - 1, r.right, y), NULL,
165 						 B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP );
166 	panel->AddChild(line);
167 
168 	y += 2 + kMargin + kMargin;	// "new line"
169 
170 	// add a "OK" button, and make it default
171 	ok 	= new BButton(BRect(x, y, x+100, y+20), NULL, "OK", new BMessage(OK_MSG), B_FOLLOW_RIGHT | B_FOLLOW_TOP);
172 	ok->MakeDefault(true);
173 	ok->ResizeToPreferred();
174 	ok->GetPreferredSize(&w, &h);
175 	x = r.right - w - kMargin;
176 	ok->MoveTo(x, ok->Frame().top);	// put the ok bottom at bottom right corner
177 	panel->AddChild(ok);
178 
179 	// add a "Cancel" button
180 	cancel 	= new BButton(BRect(x, y, x + 100, y + 20), NULL, "Cancel", new BMessage(CANCEL_MSG), B_FOLLOW_RIGHT | B_FOLLOW_TOP);
181 	cancel->ResizeToPreferred();
182 	cancel->GetPreferredSize(&w, &h);
183 	cancel->MoveTo(x - w - kMargin, y);	// put cancel button left next the ok button
184 	panel->AddChild(cancel);
185 
186 	// Finally, add our panel to window
187 	AddChild(panel);
188 
189 	// Auto resize window
190 	ResizeTo(ok->Frame().right + kMargin, ok->Frame().bottom + kMargin);
191 }
192 
193 
194 // --------------------------------------------------
195 void
196 JobSetupWindow::UpdateJobMessage()
197 {
198 	int32 copies = 1;
199 
200 	int32 from;
201 	int32 to;
202 	if (fAll->Value() == B_CONTROL_ON) {
203 		from = 1; to = MAX_INT32;
204 	} else {
205 		from = atoi(fFrom->Text());
206 		to   = atoi(fTo->Text());
207 		if (from <= 0) from = 1;
208 		if (to < from) to = from;
209 	}
210 
211 	if (fSetupMsg->HasInt32("copies")) {
212 		fSetupMsg->ReplaceInt32("copies", copies);
213 	} else {
214 		fSetupMsg->AddInt32("copies", copies);
215 	}
216 	if (fSetupMsg->HasInt32("first_page")) {
217 		fSetupMsg->ReplaceInt32("first_page", from);
218 	} else {
219 		fSetupMsg->AddInt32("first_page", from);
220 	}
221 	if (fSetupMsg->HasInt32("last_page")) {
222 		fSetupMsg->ReplaceInt32("last_page", to);
223 	} else {
224 		fSetupMsg->AddInt32("last_page", to);
225 	}
226 }
227 
228 
229 // --------------------------------------------------
230 void
231 JobSetupWindow::MessageReceived(BMessage *msg)
232 {
233 	switch (msg->what) {
234 		case OK_MSG:
235 			UpdateJobMessage();
236 			Quit(B_OK);
237 			break;
238 
239 		case CANCEL_MSG:
240 			Quit(B_ERROR);
241 			break;
242 
243 		case RANGE_FROM_MSG:
244 		case RANGE_TO_MSG:
245 			fRange->SetValue(B_CONTROL_ON);
246 			break;
247 
248 		default:
249 			inherited::MessageReceived(msg);
250 			break;
251 	}
252 }
253 
254 
255 
256