vfs: check userland buffers before reading them.
[haiku.git] / src / kits / print / PrintPanel.cpp
blob9395566fc8bedaa153244e259bf1393cee54ccae
1 /*
2 * Copyright 2008 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Julun, <host.haiku@gmx.de
7 */
9 #include <PrintPanel.h>
11 #include <Button.h>
12 #include <GroupLayoutBuilder.h>
13 #include <GroupView.h>
14 #include <Screen.h>
17 namespace BPrivate {
18 namespace Print {
21 // #pragma mark -- _BPrintPanelFilter_
24 BPrintPanel::_BPrintPanelFilter_::_BPrintPanelFilter_(BPrintPanel* panel)
25 : BMessageFilter(B_KEY_DOWN)
26 , fPrintPanel(panel)
31 filter_result
32 BPrintPanel::_BPrintPanelFilter_::Filter(BMessage* msg, BHandler** target)
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;
40 return result;
44 // #pragma mark -- BPrintPanel
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)
55 BButton* ok = new BButton("OK", new BMessage('_ok_'));
56 BButton* cancel = new BButton("Cancel", new BMessage('_cl_'));
58 BGroupLayout *layout = new BGroupLayout(B_HORIZONTAL);
59 SetLayout(layout);
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)
71 ok->MakeDefault(true);
72 AddCommonFilter(new _BPrintPanelFilter_(this));
76 BPrintPanel::~BPrintPanel()
78 if (fPrintPanelSem > 0)
79 delete_sem(fPrintPanelSem);
83 BPrintPanel::BPrintPanel(BMessage* data)
84 : BWindow(data)
86 // TODO: implement
90 BArchivable*
91 BPrintPanel::Instantiate(BMessage* data)
93 // TODO: implement
94 return NULL;
98 status_t
99 BPrintPanel::Archive(BMessage* data, bool deep) const
101 // TODO: implement
102 return B_ERROR;
106 BView*
107 BPrintPanel::Panel() const
109 return fPanel->ChildAt(0);
113 void
114 BPrintPanel::AddPanel(BView* panel)
116 BView* child = Panel();
117 if (child) {
118 RemovePanel(child);
119 delete child;
122 fPanel->AddChild(panel);
124 BSize size = GetLayout()->PreferredSize();
125 ResizeTo(size.Width(), size.Height());
129 bool
130 BPrintPanel::RemovePanel(BView* child)
132 BView* panel = Panel();
133 if (child == panel)
134 return fPanel->RemoveChild(child);
136 return false;
140 void
141 BPrintPanel::MessageReceived(BMessage* message)
143 switch (message->what) {
144 case '_ok_': {
145 fPrintPanelResult = B_OK;
147 // fall through
148 case '_cl_':
149 delete_sem(fPrintPanelSem);
150 fPrintPanelSem = -1;
151 } break;
153 default:
154 BWindow::MessageReceived(message);
159 void
160 BPrintPanel::FrameResized(float newWidth, float newHeight)
162 BWindow::FrameResized(newWidth, newHeight);
166 BHandler*
167 BPrintPanel::ResolveSpecifier(BMessage* message, int32 index, BMessage* specifier,
168 int32 form, const char* property)
170 return BWindow::ResolveSpecifier(message, index, specifier, form, property);
174 status_t
175 BPrintPanel::GetSupportedSuites(BMessage* data)
177 return BWindow::GetSupportedSuites(data);
181 status_t
182 BPrintPanel::Perform(perform_code d, void* arg)
184 return BWindow::Perform(d, arg);
188 void
189 BPrintPanel::Quit()
191 BWindow::Quit();
195 bool
196 BPrintPanel::QuitRequested()
198 return BWindow::QuitRequested();
202 void
203 BPrintPanel::DispatchMessage(BMessage* message, BHandler* handler)
205 BWindow::DispatchMessage(message, handler);
209 status_t
210 BPrintPanel::ShowPanel()
212 fPrintPanelSem = create_sem(0, "PrintPanel");
213 if (fPrintPanelSem < 0) {
214 Quit();
215 return B_CANCEL;
218 BWindow* window = dynamic_cast<BWindow*> (BLooper::LooperForThread(find_thread(NULL)));
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);
227 Show();
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);
236 if (err == B_BAD_SEM_ID)
237 break;
238 window->UpdateIfNeeded();
240 } else {
241 while (acquire_sem(fPrintPanelSem) == B_INTERRUPTED) {}
244 return fPrintPanelResult;
248 void
249 BPrintPanel::AddChild(BView* child, BView* before)
251 BWindow::AddChild(child, before);
255 bool
256 BPrintPanel::RemoveChild(BView* child)
258 return BWindow::RemoveChild(child);
262 BView*
263 BPrintPanel::ChildAt(int32 index) const
265 return BWindow::ChildAt(index);
269 void BPrintPanel::_ReservedBPrintPanel1() {}
270 void BPrintPanel::_ReservedBPrintPanel2() {}
271 void BPrintPanel::_ReservedBPrintPanel3() {}
272 void BPrintPanel::_ReservedBPrintPanel4() {}
273 void BPrintPanel::_ReservedBPrintPanel5() {}
276 } // namespace Print
277 } // namespace BPrivate