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 <GridView.h>
22#include <GroupLayout.h>
23#include <GroupLayoutBuilder.h>
24#include <RadioButton.h>
25#include <Screen.h>
26#include <TextControl.h>
27
28
29JobSetupWindow::JobSetupWindow(BMessage *msg, const char * printerName)
30	: BlockingWindow(BRect(0, 0, 100, 100), "Job Setup",
31		B_TITLED_WINDOW_LOOK,
32 		B_MODAL_APP_WINDOW_FEEL, B_NOT_RESIZABLE | B_NOT_MINIMIZABLE |
33 			B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
34	fPrinterName(printerName),
35	fSetupMsg(msg)
36{
37	if (printerName)
38		SetTitle(BString(printerName).Append(" Job Setup").String());
39
40	int32 firstPage;
41	fSetupMsg->FindInt32("first_page", &firstPage);
42
43	int32 lastPage;
44	fSetupMsg->FindInt32("last_page", &lastPage);
45	bool allPages = firstPage == 1 && lastPage == INT32_MAX;
46
47	fAll = new BRadioButton("allPages", "Print all pages",
48		new BMessage(ALL_PAGES_MGS));
49	fAll->SetValue(allPages);
50
51	fRange = new BRadioButton("pagesRange", "Print selected pages:",
52		new BMessage(RANGE_SELECTION_MSG));
53	fRange->SetValue(!allPages);
54
55	fFrom = new BTextControl("from", "From:", "SomeSpaceHere", NULL);
56	fFrom->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
57	fFrom->SetEnabled(!allPages);
58
59	fTo = new BTextControl("to", "To:", "", NULL);
60	fTo->SetAlignment(B_ALIGN_LEFT, B_ALIGN_RIGHT);
61	fTo->SetEnabled(!allPages);
62
63	BString buffer;
64	buffer << firstPage;
65	fFrom->SetText(buffer.String());
66
67	buffer = "";
68	buffer << lastPage;
69	fTo->SetText(buffer.String());
70
71	for (uint32 i = 0; i < '0'; i++) {
72		fTo->TextView()->DisallowChar(i);
73		fFrom->TextView()->DisallowChar(i);
74	}
75
76	for (uint32 i = '9' + 1; i < 255; i++) {
77		fTo->TextView()->DisallowChar(i);
78		fFrom->TextView()->DisallowChar(i);
79	}
80
81	BBox *separator = new BBox("separator");
82	separator->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1));
83
84	BButton *cancel = new BButton("cancel", "Cancel", new BMessage(CANCEL_MSG));
85
86	BButton *ok = new BButton("ok", "OK", new BMessage(OK_MSG));
87	ok->MakeDefault(true);
88
89	BGridView* settings = new BGridView();
90	BGridLayout* settingsLayout = settings->GridLayout();
91	settingsLayout->AddItem(fFrom->CreateLabelLayoutItem(), 0, 0);
92	settingsLayout->AddItem(fFrom->CreateTextViewLayoutItem(), 1, 0);
93	settingsLayout->AddItem(fTo->CreateLabelLayoutItem(), 0, 1);
94	settingsLayout->AddItem(fTo->CreateTextViewLayoutItem(), 1, 1);
95	settingsLayout->SetSpacing(0, 0);
96
97	SetLayout(new BGroupLayout(B_VERTICAL));
98	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
99		.Add(fAll)
100		.Add(fRange)
101		.Add(settings)
102		.AddGlue()
103		.Add(separator)
104		.AddGroup(B_HORIZONTAL, 10, 1.0f)
105			.AddGlue()
106			.Add(cancel)
107			.Add(ok)
108		.End()
109		.SetInsets(10, 10, 10, 10)
110	);
111
112
113	BRect winFrame(Frame());
114	BRect screenFrame(BScreen().Frame());
115	MoveTo((screenFrame.right - winFrame.right) / 2,
116		(screenFrame.bottom - winFrame.bottom) / 2);
117}
118
119
120void
121JobSetupWindow::UpdateJobMessage()
122{
123	int32 from = 1;
124	int32 to = INT32_MAX;
125	if (fAll->Value() == B_CONTROL_OFF) {
126		from = atoi(fFrom->Text());
127		to = atoi(fTo->Text());
128		if (from <= 0) from = 1;
129		if (to < from) to = from;
130	}
131
132	int32 copies = 1;
133	fSetupMsg->RemoveName("copies");
134	fSetupMsg->AddInt32("copies", copies);
135
136	fSetupMsg->RemoveName("first_page");
137	fSetupMsg->AddInt32("first_page", from);
138
139	fSetupMsg->RemoveName("last_page");
140	fSetupMsg->AddInt32("last_page", to);
141}
142
143
144void
145JobSetupWindow::MessageReceived(BMessage *msg)
146{
147	switch (msg->what) {
148		case OK_MSG:
149			UpdateJobMessage();
150			Quit(B_OK);
151			break;
152
153		case CANCEL_MSG:
154			Quit(B_ERROR);
155			break;
156
157		case ALL_PAGES_MGS:
158			fTo->SetEnabled(false);
159			fFrom->SetEnabled(false);
160			break;
161
162		case RANGE_SELECTION_MSG:
163			fTo->SetEnabled(true);
164			fFrom->SetEnabled(true);
165			break;
166
167		default:
168			BlockingWindow::MessageReceived(msg);
169			break;
170	}
171}
172