xref: /haiku/src/kits/print/PrintPanel.cpp (revision 4b7da592ee3b0a5f48f0a0bd212400223ae9e802)
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 
17 namespace BPrivate {
18 	namespace Print {
19 
20 
21 // #pragma mark -- _BPrintPanelFilter_
22 
23 
_BPrintPanelFilter_(BPrintPanel * panel)24 BPrintPanel::_BPrintPanelFilter_::_BPrintPanelFilter_(BPrintPanel* panel)
25 	: BMessageFilter(B_KEY_DOWN)
26 	, fPrintPanel(panel)
27 {
28 }
29 
30 
31 filter_result
Filter(BMessage * msg,BHandler ** target)32 BPrintPanel::_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 
BPrintPanel(const BString & title)47 BPrintPanel::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 
~BPrintPanel()76 BPrintPanel::~BPrintPanel()
77 {
78 	if (fPrintPanelSem > 0)
79 		delete_sem(fPrintPanelSem);
80 }
81 
82 
BPrintPanel(BMessage * data)83 BPrintPanel::BPrintPanel(BMessage* data)
84 	: BWindow(data)
85 {
86 	// TODO: implement
87 }
88 
89 
90 BArchivable*
Instantiate(BMessage * data)91 BPrintPanel::Instantiate(BMessage* data)
92 {
93 	// TODO: implement
94 	return NULL;
95 }
96 
97 
98 status_t
Archive(BMessage * data,bool deep) const99 BPrintPanel::Archive(BMessage* data, bool deep) const
100 {
101 	// TODO: implement
102 	return B_ERROR;
103 }
104 
105 
106 BView*
Panel() const107 BPrintPanel::Panel() const
108 {
109 	return fPanel->ChildAt(0);
110 }
111 
112 
113 void
AddPanel(BView * panel)114 BPrintPanel::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 
129 bool
RemovePanel(BView * child)130 BPrintPanel::RemovePanel(BView* child)
131 {
132 	BView* panel = Panel();
133 	if (child == panel)
134 		return fPanel->RemoveChild(child);
135 
136 	return false;
137 }
138 
139 
140 void
MessageReceived(BMessage * message)141 BPrintPanel::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 
159 void
FrameResized(float newWidth,float newHeight)160 BPrintPanel::FrameResized(float newWidth, float newHeight)
161 {
162 	BWindow::FrameResized(newWidth, newHeight);
163 }
164 
165 
166 BHandler*
ResolveSpecifier(BMessage * message,int32 index,BMessage * specifier,int32 form,const char * property)167 BPrintPanel::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 
174 status_t
GetSupportedSuites(BMessage * data)175 BPrintPanel::GetSupportedSuites(BMessage* data)
176 {
177 	return BWindow::GetSupportedSuites(data);
178 }
179 
180 
181 status_t
Perform(perform_code d,void * arg)182 BPrintPanel::Perform(perform_code d, void* arg)
183 {
184 	return BWindow::Perform(d, arg);
185 }
186 
187 
188 void
Quit()189 BPrintPanel::Quit()
190 {
191 	BWindow::Quit();
192 }
193 
194 
195 bool
QuitRequested()196 BPrintPanel::QuitRequested()
197 {
198 	return BWindow::QuitRequested();
199 }
200 
201 
202 void
DispatchMessage(BMessage * message,BHandler * handler)203 BPrintPanel::DispatchMessage(BMessage* message, BHandler* handler)
204 {
205 	BWindow::DispatchMessage(message, handler);
206 }
207 
208 
209 status_t
ShowPanel()210 BPrintPanel::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 
248 void
AddChild(BView * child,BView * before)249 BPrintPanel::AddChild(BView* child, BView* before)
250 {
251 	BWindow::AddChild(child, before);
252 }
253 
254 
255 bool
RemoveChild(BView * child)256 BPrintPanel::RemoveChild(BView* child)
257 {
258 	return BWindow::RemoveChild(child);
259 }
260 
261 
262 BView*
ChildAt(int32 index) const263 BPrintPanel::ChildAt(int32 index) const
264 {
265 	return BWindow::ChildAt(index);
266 }
267 
268 
_ReservedBPrintPanel1()269 void BPrintPanel::_ReservedBPrintPanel1() {}
_ReservedBPrintPanel2()270 void BPrintPanel::_ReservedBPrintPanel2() {}
_ReservedBPrintPanel3()271 void BPrintPanel::_ReservedBPrintPanel3() {}
_ReservedBPrintPanel4()272 void BPrintPanel::_ReservedBPrintPanel4() {}
_ReservedBPrintPanel5()273 void BPrintPanel::_ReservedBPrintPanel5() {}
274 
275 
276 	}	// namespace Print
277 }	// namespace BPrivate
278