vfs: check userland buffers before reading them.
[haiku.git] / src / tests / servers / app / clip_to_picture / main.cpp
blob702704ca339f3255715ea1d1c5917dde640dfdf7
1 /*
2 * Copyright 2014 Stephan Aßmus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
7 #include <stdio.h>
8 #include <string.h>
10 #include <Application.h>
11 #include <Message.h>
12 #include <Picture.h>
13 #include <LayoutBuilder.h>
14 #include <List.h>
15 #include <PopUpMenu.h>
16 #include <ScrollView.h>
17 #include <String.h>
18 #include <StringView.h>
19 #include <View.h>
20 #include <Window.h>
23 static const char* kAppSignature = "application/x.vnd-Haiku.ClipToPicture";
26 class TestRenderer {
27 public:
28 virtual void Draw(BView* view, BRect updateRect) = 0;
32 class Test {
33 public:
34 Test(const char* name,
35 TestRenderer* clippingTest,
36 TestRenderer* validateTest);
37 ~Test();
39 const char* Name() const
40 { return fName.String(); }
42 TestRenderer* ClippingTest() const
43 { return fClippingTest; }
44 TestRenderer* ValidateTest() const
45 { return fValidateTest; }
47 private:
48 BString fName;
49 TestRenderer* fClippingTest;
50 TestRenderer* fValidateTest;
54 Test::Test(const char* name, TestRenderer* clippingTest,
55 TestRenderer* validateTest)
57 fName(name),
58 fClippingTest(clippingTest),
59 fValidateTest(validateTest)
64 Test::~Test()
66 delete fClippingTest;
67 delete fValidateTest;
71 // #pragma mark - TestView
74 class TestView : public BView {
75 public:
76 TestView();
77 virtual ~TestView();
79 virtual void Draw(BRect updateRect);
81 void SetTestRenderer(TestRenderer* renderer);
83 private:
84 TestRenderer* fTestRenderer;
88 TestView::TestView()
90 BView(NULL, B_WILL_DRAW),
91 fTestRenderer(NULL)
96 TestView::~TestView()
101 void
102 TestView::Draw(BRect updateRect)
104 if (fTestRenderer != NULL)
105 fTestRenderer->Draw(this, updateRect);
109 void
110 TestView::SetTestRenderer(TestRenderer* renderer)
112 fTestRenderer = renderer;
113 Invalidate();
117 // #pragma mark - TestWindow
120 enum {
121 MSG_SELECT_TEST = 'stst'
125 class TestWindow : public BWindow {
126 public:
127 TestWindow();
128 virtual ~TestWindow();
130 virtual void MessageReceived(BMessage* message);
132 void AddTest(Test* test);
133 void SetToTest(int32 index);
135 private:
136 TestView* fClippingTestView;
137 TestView* fValidateTestView;
139 BMenuField* fTestSelectionField;
141 BList fTests;
145 TestWindow::TestWindow()
147 BWindow(BRect(50.0, 50.0, 450.0, 250.0), "ClipToPicture Test",
148 B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE
149 | B_AUTO_UPDATE_SIZE_LIMITS)
151 fClippingTestView = new TestView();
152 fValidateTestView = new TestView();
154 BGroupView* group = new BGroupView(B_HORIZONTAL);
155 BLayoutBuilder::Group<>(group, B_HORIZONTAL, 0.0f)
156 .Add(fClippingTestView)
157 .Add(fValidateTestView)
160 BScrollView* scrollView = new BScrollView("scroll", group, 0, true, true);
162 BStringView* leftLabel = new BStringView("left label",
163 "ClipToPicture:");
164 leftLabel->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
166 BStringView* rightLabel = new BStringView("right label",
167 "Validation:");
168 rightLabel->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
170 fTestSelectionField = new BMenuField("test selection",
171 "Select test:", new BPopUpMenu("select"));
173 BLayoutBuilder::Group<>(this, B_VERTICAL, 0.0f)
174 .AddGroup(B_HORIZONTAL)
175 .Add(fTestSelectionField)
176 .AddGlue()
177 .SetInsets(B_USE_DEFAULT_SPACING)
178 .End()
179 .AddGroup(B_HORIZONTAL)
180 .Add(leftLabel)
181 .Add(rightLabel)
182 .SetInsets(B_USE_DEFAULT_SPACING)
183 .End()
184 .Add(scrollView)
189 TestWindow::~TestWindow()
191 for (int32 i = fTests.CountItems() - 1; i >= 0; i++)
192 delete (Test*)fTests.ItemAt(i);
196 void
197 TestWindow::MessageReceived(BMessage* message)
199 switch (message->what) {
200 case MSG_SELECT_TEST:
202 int32 index;
203 if (message->FindInt32("index", &index) == B_OK)
204 SetToTest(index);
205 break;
208 default:
209 BWindow::MessageReceived(message);
214 void
215 TestWindow::AddTest(Test* test)
217 if (test == NULL || fTests.HasItem(test))
218 return;
220 if (!fTests.AddItem(test)) {
221 delete test;
222 return;
225 BMessage* message = new BMessage(MSG_SELECT_TEST);
226 message->AddInt32("index", fTests.CountItems() - 1);
228 BMenuItem* item = new BMenuItem(test->Name(), message);
229 if (!fTestSelectionField->Menu()->AddItem(item)) {
230 fTests.RemoveItem(fTests.CountItems() - 1);
231 delete test;
232 delete item;
233 return;
236 if (fTests.CountItems() == 1)
237 SetToTest(0);
241 void
242 TestWindow::SetToTest(int32 index)
244 Test* test = (Test*)fTests.ItemAt(index);
245 if (test == NULL)
246 return;
248 fTestSelectionField->Menu()->ItemAt(index)->SetMarked(true);
250 fClippingTestView->SetTestRenderer(test->ClippingTest());
251 fValidateTestView->SetTestRenderer(test->ValidateTest());
255 // #pragma mark - Test1
258 class Test1Clipping : public TestRenderer {
259 virtual void Draw(BView* view, BRect updateRect)
261 BPicture picture;
262 view->BeginPicture(&picture);
263 view->SetDrawingMode(B_OP_ALPHA);
264 view->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_COMPOSITE);
265 view->DrawString("Clipping to text.", BPoint(20, 30));
266 view->EndPicture();
268 view->ClipToPicture(&picture);
270 view->FillRect(view->Bounds());
275 class Test1Validate : public TestRenderer {
276 virtual void Draw(BView* view, BRect updateRect)
278 view->DrawString("Clipping to text.", BPoint(20, 30));
283 // #pragma mark - Test2
286 class Test2Clipping : public TestRenderer {
287 virtual void Draw(BView* view, BRect updateRect)
289 view->PushState();
291 BPicture picture;
292 view->BeginPicture(&picture);
293 view->SetDrawingMode(B_OP_ALPHA);
294 view->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_COMPOSITE);
295 view->SetScale(2.0);
296 view->DrawString("Scaled clipping", BPoint(10, 15));
297 view->DrawString("to text.", BPoint(10, 25));
298 view->EndPicture();
300 view->PopState();
302 view->ClipToPicture(&picture);
304 view->FillRect(view->Bounds());
309 class Test2Validate : public TestRenderer {
310 virtual void Draw(BView* view, BRect updateRect)
312 view->SetScale(2.0);
313 view->DrawString("Scaled clipping", BPoint(10, 15));
314 view->DrawString("to text.", BPoint(10, 25));
319 // #pragma mark - Test2
322 class Test3Clipping : public TestRenderer {
323 virtual void Draw(BView* view, BRect updateRect)
325 BPicture picture;
326 view->BeginPicture(&picture);
327 view->SetDrawingMode(B_OP_ALPHA);
328 view->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_COMPOSITE);
329 view->DrawString("Scaled picture", BPoint(10, 15));
330 view->EndPicture();
332 view->SetScale(2.0);
333 view->ClipToPicture(&picture);
335 view->SetScale(1.0);
336 view->FillRect(view->Bounds());
341 class Test3Validate : public TestRenderer {
342 virtual void Draw(BView* view, BRect updateRect)
344 view->SetScale(2.0);
345 view->DrawString("Scaled picture", BPoint(10, 15));
350 // #pragma mark - Test2
353 class Test4Clipping : public TestRenderer {
354 virtual void Draw(BView* view, BRect updateRect)
356 view->SetDrawingMode(B_OP_ALPHA);
357 view->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_COMPOSITE);
359 BPicture rect;
360 view->BeginPicture(&rect);
361 view->FillRect(BRect(20, 20, 50, 50));
362 view->EndPicture();
364 view->ClipToPicture(&rect);
366 view->PushState();
367 BPicture circle;
368 view->BeginPicture(&circle);
369 view->FillEllipse(BRect(20, 20, 50, 50));
370 view->EndPicture();
372 view->ClipToInversePicture(&circle);
374 view->FillRect(view->Bounds());
376 view->PopState();
381 class Test4Validate : public TestRenderer {
382 virtual void Draw(BView* view, BRect updateRect)
384 view->FillRect(BRect(20, 20, 50, 50));
385 view->FillEllipse(BRect(20, 20, 50, 50), B_SOLID_LOW);
390 // #pragma mark -
394 main(int argc, char** argv)
396 BApplication app(kAppSignature);
398 TestWindow* window = new TestWindow();
400 window->AddTest(new Test("Simple clipping",
401 new Test1Clipping(), new Test1Validate()));
403 window->AddTest(new Test("Scaled clipping 1",
404 new Test2Clipping(), new Test2Validate()));
406 window->AddTest(new Test("Scaled clipping 2",
407 new Test3Clipping(), new Test3Validate()));
409 window->AddTest(new Test("Nested states",
410 new Test4Clipping(), new Test4Validate()));
412 window->SetToTest(3);
413 window->Show();
415 app.Run();
416 return 0;