1/*
2 * Copyright 2008 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Julun, <host.haiku@gmx.de
7 */
8
9#include <PrintPanel.h>
10
11#include <Button.h>
12#include <GroupLayoutBuilder.h>
13#include <GroupView.h>
14#include <Screen.h>
15
16
17namespace BPrivate {
18	namespace Print {
19
20
21// #pragma mark -- _BPrintPanelFilter_
22
23
24BPrintPanel::_BPrintPanelFilter_::_BPrintPanelFilter_(BPrintPanel* panel)
25	: BMessageFilter(B_KEY_DOWN)
26	, fPrintPanel(panel)
27{
28}
29
30
31filter_result
32BPrintPanel::_BPrintPanelFilter_::Filter(BMessage* msg, BHandler** target)
33{
34	int32 key;
35	filter_result result = B_DISPATCH_MESSAGE;
36	if (msg->FindInt32("key", &key) == B_OK && key == 1) {
37		fPrintPanel->PostMessage(B_QUIT_REQUESTED);
38		result = B_SKIP_MESSAGE;
39	}
40	return result;
41}
42
43
44// #pragma mark -- BPrintPanel
45
46
47BPrintPanel::BPrintPanel(const BString& title)
48	: BWindow(BRect(0, 0, 640, 480), title.String(), B_TITLED_WINDOW_LOOK,
49		B_MODAL_APP_WINDOW_FEEL, B_NOT_ZOOMABLE | B_NOT_RESIZABLE |
50		B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE)
51	, fPanel(new BGroupView)
52	, fPrintPanelSem(-1)
53	, fPrintPanelResult(B_CANCEL)
54{
55	BButton* ok = new BButton("OK", new BMessage('_ok_'));
56	BButton* cancel = new BButton("Cancel", new BMessage('_cl_'));
57
58	BGroupLayout *layout = new BGroupLayout(B_HORIZONTAL);
59	SetLayout(layout);
60
61	AddChild(BGroupLayoutBuilder(B_VERTICAL, 10.0)
62			.Add(fPanel)
63			.Add(BGroupLayoutBuilder(B_HORIZONTAL, 10.0)
64				.AddGlue()
65				.Add(cancel)
66				.Add(ok)
67				.SetInsets(0.0, 0.0, 0.0, 0.0))
68			.SetInsets(10.0, 10.0, 10.0, 10.0)
69		);
70
71	ok->MakeDefault(true);
72	AddCommonFilter(new _BPrintPanelFilter_(this));
73}
74
75
76BPrintPanel::~BPrintPanel()
77{
78	if (fPrintPanelSem > 0)
79		delete_sem(fPrintPanelSem);
80}
81
82
83BPrintPanel::BPrintPanel(BMessage* data)
84	: BWindow(data)
85{
86	// TODO: implement
87}
88
89
90BArchivable*
91BPrintPanel::Instantiate(BMessage* data)
92{
93	// TODO: implement
94	return NULL;
95}
96
97
98status_t
99BPrintPanel::Archive(BMessage* data, bool deep) const
100{
101	// TODO: implement
102	return B_ERROR;
103}
104
105
106BView*
107BPrintPanel::Panel() const
108{
109	return fPanel->ChildAt(0);
110}
111
112
113void
114BPrintPanel::AddPanel(BView* panel)
115{
116	BView* child = Panel();
117	if (child) {
118		RemovePanel(child);
119		delete child;
120	}
121
122	fPanel->AddChild(panel);
123
124	BSize size = GetLayout()->PreferredSize();
125	ResizeTo(size.Width(), size.Height());
126}
127
128
129bool
130BPrintPanel::RemovePanel(BView* child)
131{
132	BView* panel = Panel();
133	if (child == panel)
134		return fPanel->RemoveChild(child);
135
136	return false;
137}
138
139
140void
141BPrintPanel::MessageReceived(BMessage* message)
142{
143	switch (message->what) {
144		case '_ok_': {
145			fPrintPanelResult = B_OK;
146
147		// fall through
148		case '_cl_':
149			delete_sem(fPrintPanelSem);
150			fPrintPanelSem = -1;
151		}	break;
152
153		default:
154			BWindow::MessageReceived(message);
155	}
156}
157
158
159void
160BPrintPanel::FrameResized(float newWidth, float newHeight)
161{
162	BWindow::FrameResized(newWidth, newHeight);
163}
164
165
166BHandler*
167BPrintPanel::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier,
168	int32 form, const char* property)
169{
170	return BWindow::ResolveSpecifier(message, index, specifier, form, property);
171}
172
173
174status_t
175BPrintPanel::GetSupportedSuites(BMessage* data)
176{
177	return BWindow::GetSupportedSuites(data);
178}
179
180
181status_t
182BPrintPanel::Perform(perform_code d, void* arg)
183{
184	return BWindow::Perform(d, arg);
185}
186
187
188void
189BPrintPanel::Quit()
190{
191	BWindow::Quit();
192}
193
194
195bool
196BPrintPanel::QuitRequested()
197{
198	return BWindow::QuitRequested();
199}
200
201
202void
203BPrintPanel::DispatchMessage(BMessage* message, BHandler* handler)
204{
205	BWindow::DispatchMessage(message, handler);
206}
207
208
209status_t
210BPrintPanel::ShowPanel()
211{
212	fPrintPanelSem = create_sem(0, "PrintPanel");
213	if (fPrintPanelSem < 0) {
214		Quit();
215		return B_CANCEL;
216	}
217
218	BWindow* window = dynamic_cast<BWindow*> (BLooper::LooperForThread(find_thread(NULL)));
219
220	{
221		BRect bounds(Bounds());
222		BRect frame(BScreen(B_MAIN_SCREEN_ID).Frame());
223		MoveTo((frame.Width() - bounds.Width()) / 2.0,
224			(frame.Height() - bounds.Height()) / 2.0);
225	}
226
227	Show();
228
229	if (window) {
230		status_t err;
231		while (true) {
232			do {
233				err = acquire_sem_etc(fPrintPanelSem, 1, B_RELATIVE_TIMEOUT, 50000);
234			} while (err == B_INTERRUPTED);
235
236			if (err == B_BAD_SEM_ID)
237				break;
238			window->UpdateIfNeeded();
239		}
240	} else {
241		while (acquire_sem(fPrintPanelSem) == B_INTERRUPTED) {}
242	}
243
244	return fPrintPanelResult;
245}
246
247
248void
249BPrintPanel::AddChild(BView* child, BView* before)
250{
251	BWindow::AddChild(child, before);
252}
253
254
255bool
256BPrintPanel::RemoveChild(BView* child)
257{
258	return BWindow::RemoveChild(child);
259}
260
261
262BView*
263BPrintPanel::ChildAt(int32 index) const
264{
265	return BWindow::ChildAt(index);
266}
267
268
269void BPrintPanel::_ReservedBPrintPanel1() {}
270void BPrintPanel::_ReservedBPrintPanel2() {}
271void BPrintPanel::_ReservedBPrintPanel3() {}
272void BPrintPanel::_ReservedBPrintPanel4() {}
273void BPrintPanel::_ReservedBPrintPanel5() {}
274
275
276	}	// namespace Print
277}	// namespace BPrivate
278