xref: /haiku/src/add-ons/print/drivers/preview/JobSetupWindow.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 /*
2 
3 Preview printer driver.
4 
5 Copyright (c) 2003 OpenBeOS.
6 
7 Authors:
8 	Philippe Houdoin
9 	Simon Gauvin
10 	Michael Pfeiffer
11 
12 Permission is hereby granted, free of charge, to any person obtaining a copy of
13 this software and associated documentation files (the "Software"), to deal in
14 the Software without restriction, including without limitation the rights to
15 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is furnished to do
17 so, subject to the following conditions:
18 
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21 
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 THE SOFTWARE.
29 
30 */
31 
32 #include <InterfaceKit.h>
33 #include <SupportKit.h>
34 #include <stdlib.h>
35 
36 #include "PrinterDriver.h"
37 #include "JobSetupWindow.h"
38 
39 // --------------------------------------------------
40 JobSetupWindow::JobSetupWindow(BMessage *msg, const char * printerName)
41 	:	BlockingWindow(BRect(0, 0, 320, 160), "Job Setup", B_TITLED_WINDOW_LOOK,
42  			B_MODAL_APP_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_MINIMIZABLE |
43  			B_NOT_ZOOMABLE)
44 {
45 	MoveTo(300, 300);
46 
47 	fSetupMsg	= msg;
48 
49 	if (printerName) {
50 		BString	title;
51 		title << printerName << " Job Setup";
52 		SetTitle(title.String());
53 		fPrinterName = printerName;
54 	}
55 
56 	// ---- Ok, build a default job setup user interface
57 	BRect			r;
58 	BBox			*panel;
59 	BBox			*line;
60 	BButton	 		*ok;
61 	BButton			*cancel;
62 	BStringView		*sv;
63 	float			x, y, w, h;
64 	float			indent;
65 	int32           copies;
66 	int32           firstPage;
67 	int32           lastPage;
68 	bool            allPages;
69 	char            buffer[80];
70 
71 	// PrinterDriver ensures that property exists
72 	fSetupMsg->FindInt32("copies",     &copies);
73 	fSetupMsg->FindInt32("first_page", &firstPage);
74 	fSetupMsg->FindInt32("last_page",  &lastPage);
75 
76 	allPages = firstPage == 1 && lastPage == MAX_INT32;
77 
78 	r = Bounds();
79 
80 	// add a *dialog* background
81 	panel = new BBox(r, "top_panel", B_FOLLOW_ALL,
82 		B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP,
83 		B_PLAIN_BORDER);
84 
85 	const int kMargin = 6;
86 
87 	//const char *kCopiesLabel				= "Copies:";
88 	const char *kCopiesLabelExtraSpace		= "Copies:##";
89 	const char *kPagesRangeLabel			= "Pages:";
90 	const char *kAllPagesLabel				= "All";
91 	const char *kPagesRangeSelectionLabel	= "";
92 	const char *kFromLabel					= "From:";
93 	const char *kFromLabelExtraSpace		= "From:##";
94 	const char *kToLabel					= "To:";
95 	const char *kToLabelExtraSpace			= "To:##";
96 
97 	r = panel->Bounds();
98 
99 	x = r.left + kMargin;
100 	y = r.top + kMargin;
101 
102 
103 	// add a "copies" input field
104 
105 /* Simon: temporarily removed this code
106 	sprintf(buffer, "%d", (int)copies);
107 	fCopies = new BTextControl(BRect(x, y, x+100, y+20), "copies", kCopiesLabel,
108 								buffer, new BMessage(NB_COPIES_MSG));
109 	fCopies->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
110 	fCopies->ResizeToPreferred();
111 	fCopies->GetPreferredSize(&w, &h);
112 	panel->AddChild(fCopies);
113 
114 	y += h + kMargin;	// "new line"
115 */
116 	// add a "pages" label
117 	sv = new BStringView(BRect(x, y, x+100, y+20), "pages_range", kPagesRangeLabel);
118 	panel->AddChild(sv);
119 	sv->ResizeToPreferred();
120 	sv->GetPreferredSize(&w, &h);
121 
122 	// align "copies" textcontrol field on the "allPages" radiobutton bellow...
123 	indent = be_plain_font->StringWidth(kCopiesLabelExtraSpace);
124 	w += kMargin;
125 	if ( w > indent )
126 		indent = w;
127 	// fCopies->SetDivider(indent);
128 
129 	x += indent;
130 
131 	// add a "all" radiobutton
132 	fAll = new BRadioButton(BRect(x, y, x+100, y+20), "all_pages", kAllPagesLabel,
133 						new BMessage(ALL_PAGES_MGS));
134 	fAll->ResizeToPreferred();
135 	fAll->GetPreferredSize(&w, &h);
136 	fAll->SetValue(allPages);
137 	panel->AddChild(fAll);
138 
139 	y += h + kMargin;	// "new line"
140 
141 	// add a range selection raddiobutton
142 	fRange = new BRadioButton(BRect(x, y, x+100, y+20), "pages_range_selection", kPagesRangeSelectionLabel,
143 						new BMessage(RANGE_SELECTION_MSG));
144 	fRange->ResizeToPreferred();
145 	fRange->GetPreferredSize(&w, &h);
146 	fRange->SetValue(!allPages);
147 	panel->AddChild(fRange);
148 
149 	x += w + kMargin;
150 
151 	// add a "from" field
152 	if (allPages) {
153 		buffer[0] = 0;
154 	} else {
155 		sprintf(buffer, "%d", (int)firstPage);
156 	}
157 	fFrom = new BTextControl(BRect(x, y, x+100, y+20), "from_field", kFromLabel, buffer,
158 							new BMessage(RANGE_FROM_MSG));
159 	fFrom->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
160 	fFrom->SetDivider(be_plain_font->StringWidth(kFromLabelExtraSpace));
161 	fFrom->ResizeToPreferred();
162 	fFrom->GetPreferredSize(&w, &h);
163 	panel->AddChild(fFrom);
164 
165 	x += w + kMargin;
166 
167 	// add a "to" field
168 	if (allPages) {
169 		buffer[0] = 0;
170 	} else {
171 		sprintf(buffer, "%d", (int)lastPage);
172 	}
173 	fTo = new BTextControl(BRect(x, y, x+100, y+20), "to_field", kToLabel, buffer,
174 							new BMessage(RANGE_TO_MSG));
175 	fTo->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
176 	fTo->SetDivider(be_plain_font->StringWidth(kToLabelExtraSpace));
177 	fTo->ResizeToPreferred();
178 	fTo->GetPreferredSize(&w, &h);
179 	panel->AddChild(fTo);
180 
181 	y += h + kMargin + kMargin;	// "new line"
182 	x = r.left + kMargin;
183 
184 	// add a separator line...
185 	line = new BBox(BRect(r.left, y - 1, r.right, y), NULL,
186 						 B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP );
187 	panel->AddChild(line);
188 
189 	y += 2 + kMargin + kMargin;	// "new line"
190 
191 	// add a "OK" button, and make it default
192 	ok 	= new BButton(BRect(x, y, x+100, y+20), NULL, "OK", new BMessage(OK_MSG), B_FOLLOW_RIGHT | B_FOLLOW_TOP);
193 	ok->MakeDefault(true);
194 	ok->ResizeToPreferred();
195 	ok->GetPreferredSize(&w, &h);
196 	x = r.right - w - kMargin;
197 	ok->MoveTo(x, ok->Frame().top);	// put the ok bottom at bottom right corner
198 	panel->AddChild(ok);
199 
200 	// add a "Cancel" button
201 	cancel 	= new BButton(BRect(x, y, x + 100, y + 20), NULL, "Cancel", new BMessage(CANCEL_MSG), B_FOLLOW_RIGHT | B_FOLLOW_TOP);
202 	cancel->ResizeToPreferred();
203 	cancel->GetPreferredSize(&w, &h);
204 	cancel->MoveTo(x - w - kMargin, y);	// put cancel button left next the ok button
205 	panel->AddChild(cancel);
206 
207 	// Finally, add our panel to window
208 	AddChild(panel);
209 
210 	// Auto resize window
211 	ResizeTo(ok->Frame().right + kMargin, ok->Frame().bottom + kMargin);
212 }
213 
214 
215 // --------------------------------------------------
216 void
217 JobSetupWindow::UpdateJobMessage()
218 {
219 	int32 copies = 1;
220 
221 	int32 from;
222 	int32 to;
223 	if (fAll->Value() == B_CONTROL_ON) {
224 		from = 1; to = MAX_INT32;
225 	} else {
226 		from = atoi(fFrom->Text());
227 		to   = atoi(fTo->Text());
228 		if (from <= 0) from = 1;
229 		if (to < from) to = from;
230 	}
231 
232 	if (fSetupMsg->HasInt32("copies")) {
233 		fSetupMsg->ReplaceInt32("copies", copies);
234 	} else {
235 		fSetupMsg->AddInt32("copies", copies);
236 	}
237 	if (fSetupMsg->HasInt32("first_page")) {
238 		fSetupMsg->ReplaceInt32("first_page", from);
239 	} else {
240 		fSetupMsg->AddInt32("first_page", from);
241 	}
242 	if (fSetupMsg->HasInt32("last_page")) {
243 		fSetupMsg->ReplaceInt32("last_page", to);
244 	} else {
245 		fSetupMsg->AddInt32("last_page", to);
246 	}
247 }
248 
249 
250 // --------------------------------------------------
251 void
252 JobSetupWindow::MessageReceived(BMessage *msg)
253 {
254 	switch (msg->what) {
255 		case OK_MSG:
256 			UpdateJobMessage();
257 			Quit(B_OK);
258 			break;
259 
260 		case CANCEL_MSG:
261 			Quit(B_ERROR);
262 			break;
263 
264 		case RANGE_FROM_MSG:
265 		case RANGE_TO_MSG:
266 			fRange->SetValue(B_CONTROL_ON);
267 			break;
268 
269 		default:
270 			inherited::MessageReceived(msg);
271 			break;
272 	}
273 }
274 
275 
276 
277