2 * Copyright 2014 Stephan Aßmus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
10 #include <Application.h>
13 #include <LayoutBuilder.h>
15 #include <PopUpMenu.h>
16 #include <ScrollView.h>
18 #include <StringView.h>
23 static const char* kAppSignature
= "application/x.vnd-Haiku.ClipToPicture";
28 virtual void Draw(BView
* view
, BRect updateRect
) = 0;
34 Test(const char* name
,
35 TestRenderer
* clippingTest
,
36 TestRenderer
* validateTest
);
39 const char* Name() const
40 { return fName
.String(); }
42 TestRenderer
* ClippingTest() const
43 { return fClippingTest
; }
44 TestRenderer
* ValidateTest() const
45 { return fValidateTest
; }
49 TestRenderer
* fClippingTest
;
50 TestRenderer
* fValidateTest
;
54 Test::Test(const char* name
, TestRenderer
* clippingTest
,
55 TestRenderer
* validateTest
)
58 fClippingTest(clippingTest
),
59 fValidateTest(validateTest
)
71 // #pragma mark - TestView
74 class TestView
: public BView
{
79 virtual void Draw(BRect updateRect
);
81 void SetTestRenderer(TestRenderer
* renderer
);
84 TestRenderer
* fTestRenderer
;
90 BView(NULL
, B_WILL_DRAW
),
102 TestView::Draw(BRect updateRect
)
104 if (fTestRenderer
!= NULL
)
105 fTestRenderer
->Draw(this, updateRect
);
110 TestView::SetTestRenderer(TestRenderer
* renderer
)
112 fTestRenderer
= renderer
;
117 // #pragma mark - TestWindow
121 MSG_SELECT_TEST
= 'stst'
125 class TestWindow
: public BWindow
{
128 virtual ~TestWindow();
130 virtual void MessageReceived(BMessage
* message
);
132 void AddTest(Test
* test
);
133 void SetToTest(int32 index
);
136 TestView
* fClippingTestView
;
137 TestView
* fValidateTestView
;
139 BMenuField
* fTestSelectionField
;
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",
164 leftLabel
->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED
, B_SIZE_UNSET
));
166 BStringView
* rightLabel
= new BStringView("right label",
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
)
177 .SetInsets(B_USE_DEFAULT_SPACING
)
179 .AddGroup(B_HORIZONTAL
)
182 .SetInsets(B_USE_DEFAULT_SPACING
)
189 TestWindow::~TestWindow()
191 for (int32 i
= fTests
.CountItems() - 1; i
>= 0; i
++)
192 delete (Test
*)fTests
.ItemAt(i
);
197 TestWindow::MessageReceived(BMessage
* message
)
199 switch (message
->what
) {
200 case MSG_SELECT_TEST
:
203 if (message
->FindInt32("index", &index
) == B_OK
)
209 BWindow::MessageReceived(message
);
215 TestWindow::AddTest(Test
* test
)
217 if (test
== NULL
|| fTests
.HasItem(test
))
220 if (!fTests
.AddItem(test
)) {
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);
236 if (fTests
.CountItems() == 1)
242 TestWindow::SetToTest(int32 index
)
244 Test
* test
= (Test
*)fTests
.ItemAt(index
);
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
)
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));
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
)
292 view
->BeginPicture(&picture
);
293 view
->SetDrawingMode(B_OP_ALPHA
);
294 view
->SetBlendingMode(B_PIXEL_ALPHA
, B_ALPHA_COMPOSITE
);
296 view
->DrawString("Scaled clipping", BPoint(10, 15));
297 view
->DrawString("to text.", BPoint(10, 25));
302 view
->ClipToPicture(&picture
);
304 view
->FillRect(view
->Bounds());
309 class Test2Validate
: public TestRenderer
{
310 virtual void Draw(BView
* view
, BRect updateRect
)
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
)
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));
333 view
->ClipToPicture(&picture
);
336 view
->FillRect(view
->Bounds());
341 class Test3Validate
: public TestRenderer
{
342 virtual void Draw(BView
* view
, BRect updateRect
)
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
);
360 view
->BeginPicture(&rect
);
361 view
->FillRect(BRect(20, 20, 50, 50));
364 view
->ClipToPicture(&rect
);
368 view
->BeginPicture(&circle
);
369 view
->FillEllipse(BRect(20, 20, 50, 50));
372 view
->ClipToInversePicture(&circle
);
374 view
->FillRect(view
->Bounds());
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
);
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);