xref: /haiku/src/add-ons/print/drivers/preview/JobSetupWindow.cpp (revision 0562493379cd52eb7103531f895f10bb8e77c085)
1 /*
2  * Copyright 2003-2008, 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  *		julun <host.haiku@gmx.de>
10  */
11 
12 #include "JobSetupWindow.h"
13 #include "PrinterDriver.h"
14 
15 
16 #include <stdlib.h>
17 
18 
19 #include <Box.h>
20 #include <Button.h>
21 #include <RadioButton.h>
22 #include <Screen.h>
23 #include <TextControl.h>
24 
25 
26 JobSetupWindow::JobSetupWindow(BMessage *msg, const char * printerName)
27 	:	BlockingWindow(BRect(0, 0, 300, 200), "Job Setup", B_TITLED_WINDOW_LOOK,
28  			B_MODAL_APP_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_MINIMIZABLE |
29  			B_NOT_ZOOMABLE),
30 	fPrinterName(printerName),
31 	fSetupMsg(msg)
32 {
33 	if (printerName)
34 		SetTitle(BString(printerName).Append(" Job Setup").String());
35 
36 	int32 firstPage;
37 	fSetupMsg->FindInt32("first_page", &firstPage);
38 
39 	int32 lastPage;
40 	fSetupMsg->FindInt32("last_page", &lastPage);
41 	bool allPages = firstPage == 1 && lastPage == LONG_MAX;
42 
43 	BRect bounds(Bounds());
44 	BBox *panel = new BBox(bounds, "background", B_FOLLOW_ALL,
45 		B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE_JUMP, B_PLAIN_BORDER);
46 	AddChild(panel);
47 
48 	bounds.InsetBy(10.0, 10.0);
49 
50 	fAll = new BRadioButton(bounds, "allPages", "Print all pages",
51 		new BMessage(ALL_PAGES_MGS));
52 	panel->AddChild(fAll);
53 	fAll->ResizeToPreferred();
54 	fAll->SetValue(allPages);
55 
56 	bounds.OffsetBy(0.0, fAll->Bounds().Height() + 10.0);
57 	fRange = new BRadioButton(bounds, "pagesRange", "Print pages:",
58 		new BMessage(RANGE_SELECTION_MSG));
59 	panel->AddChild(fRange);
60 	fRange->ResizeToPreferred();
61 	fRange->SetValue(!allPages);
62 
63 	bounds.OffsetBy(0.0, fRange->Bounds().Height() + 5.0);
64 	BRect rect(bounds);
65 	rect.right = be_plain_font->StringWidth("From: SomeSpaceHere");
66 	fFrom = new BTextControl(rect, "from", "From:", "SomeSpaceHere", NULL);
67 	panel->AddChild(fFrom);
68 	fFrom->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
69 	fFrom->ResizeToPreferred();
70 	fFrom->SetDivider(be_plain_font->StringWidth("From: "));
71 	fFrom->SetEnabled(!allPages);
72 
73 	rect = fFrom->Frame();
74 	fTo = new BTextControl(rect, "to", "To:", "SomeSpaceHere", NULL);
75 	panel->AddChild(fTo);
76 	fTo->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
77 	fTo->SetDivider(be_plain_font->StringWidth("To: "));
78 	fTo->MoveTo(fFrom->Frame().right + 10.0, fTo->Frame().top);
79 	fTo->SetEnabled(!allPages);
80 
81 	BString buffer;
82 	buffer << firstPage;
83 	fFrom->SetText(buffer.String());
84 
85 	buffer = "";
86 	buffer << lastPage;
87 	fTo->SetText(buffer.String());
88 
89 	for (uint32 i = 0; i < '0'; i++) {
90 		fTo->TextView()->DisallowChar(i);
91 		fFrom->TextView()->DisallowChar(i);
92 	}
93 
94 	for (uint32 i = '9' + 1; i < 255; i++) {
95 		fTo->TextView()->DisallowChar(i);
96 		fFrom->TextView()->DisallowChar(i);
97 	}
98 
99 	bounds.OffsetBy(0.0, fTo->Bounds().Height() + 10.0);
100 	BBox *line = new BBox(BRect(bounds.left - 5.0, bounds.top, bounds.right + 5.0,
101 		bounds.top + 1.0), NULL, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP );
102 	panel->AddChild(line);
103 
104 	bounds.OffsetBy(0.0, 11.0);
105 	BButton *cancel = new BButton(bounds, NULL, "Cancel", new BMessage(CANCEL_MSG));
106 	panel->AddChild(cancel);
107 	cancel->ResizeToPreferred();
108 
109 	BButton *ok = new BButton(bounds, NULL, "OK", new BMessage(OK_MSG));
110 	panel->AddChild(ok, cancel);
111 	ok->ResizeToPreferred();
112 
113 	bounds.right = fTo->Frame().right;
114 	ok->MoveTo(bounds.right - ok->Bounds().Width(), ok->Frame().top);
115 
116 	bounds = ok->Frame();
117 	cancel->MoveTo(bounds.left - cancel->Bounds().Width() - 10.0, bounds.top);
118 
119 	ok->MakeDefault(true);
120 	ResizeTo(bounds.right + 10.0, bounds.bottom + 10.0);
121 
122 	BRect winFrame(Frame());
123 	BRect screenFrame(BScreen().Frame());
124 	MoveTo((screenFrame.right - winFrame.right) / 2,
125 		(screenFrame.bottom - winFrame.bottom) / 2);
126 }
127 
128 
129 void
130 JobSetupWindow::UpdateJobMessage()
131 {
132 	int32 from = 1;
133 	int32 to = LONG_MAX;
134 	if (fAll->Value() == B_CONTROL_OFF) {
135 		from = atoi(fFrom->Text());
136 		to = atoi(fTo->Text());
137 		if (from <= 0) from = 1;
138 		if (to < from) to = from;
139 	}
140 
141 	int32 copies = 1;
142 	fSetupMsg->RemoveName("copies");
143 	fSetupMsg->AddInt32("copies", copies);
144 
145 	fSetupMsg->RemoveName("first_page");
146 	fSetupMsg->AddInt32("first_page", from);
147 
148 	fSetupMsg->RemoveName("last_page");
149 	fSetupMsg->AddInt32("last_page", to);
150 }
151 
152 
153 void
154 JobSetupWindow::MessageReceived(BMessage *msg)
155 {
156 	switch (msg->what) {
157 		case OK_MSG: {
158 			UpdateJobMessage();
159 			Quit(B_OK);
160 		}	break;
161 
162 		case CANCEL_MSG: {
163 			Quit(B_ERROR);
164 		}	break;
165 
166 		case ALL_PAGES_MGS : {
167 			fTo->SetEnabled(false);
168 			fFrom->SetEnabled(false);
169 		}	break;
170 
171 		case RANGE_SELECTION_MSG : {
172 			fTo->SetEnabled(true);
173 			fFrom->SetEnabled(true);
174 		}	break;
175 
176 		default: {
177 			BlockingWindow::MessageReceived(msg);
178 		}	break;
179 	}
180 }
181