Ticket #18319: clip.cpp

File clip.cpp, 1.6 KB (added by madmax, 14 months ago)

Test app

Line 
1#include <Application.h>
2#include <Window.h>
3#include <View.h>
4#include <Region.h>
5
6
7enum OP {
8 CONSTRAIN,
9 CLIP,
10 NONE
11};
12
13class App : public BApplication {
14public:
15 App();
16};
17
18class Window : public BWindow {
19public:
20 Window();
21};
22
23class View : public BView {
24public:
25 View(BRect frame, bool, OP);
26 void Draw(BRect);
27 void _DrawRect(BRect);
28
29public:
30 bool fSetScale;
31 OP fOp;
32};
33
34int main()
35{
36 App app;
37 app.Run();
38 return 0;
39}
40
41App::App()
42 :BApplication("application/x-vnd.some-test-app")
43{
44 Window *window = new Window();
45 window->Show();
46}
47
48Window::Window()
49 :BWindow(BRect(100,100,200,200), "clipping test", B_TITLED_WINDOW, B_QUIT_ON_WINDOW_CLOSE)
50{
51 BView* b = new BView(Bounds(), "background", B_FOLLOW_ALL, 0);
52 b->SetViewColor(128,128,128);
53 AddChild(b);
54 AddChild(new View(BRect(0,0,20,20), false, NONE));
55 AddChild(new View(BRect(25,0,45,20), false, CONSTRAIN));
56 AddChild(new View(BRect(50,0,70,20), false, CLIP));
57 AddChild(new View(BRect(0,25,20,45), true, NONE));
58 AddChild(new View(BRect(25,25,45,45), true, CONSTRAIN));
59 AddChild(new View(BRect(50,25,70,45), true, CLIP));
60}
61
62View::View(BRect frame, bool setScale, OP op)
63 :BView(frame, "", B_FOLLOW_NONE, B_WILL_DRAW)
64 ,fSetScale(setScale)
65 ,fOp(op)
66{
67}
68
69
70void
71View::_DrawRect(BRect r) {
72 PushState();
73 SetHighColor(255,0,0);
74 FillRect(r);
75 if (fOp != NONE) {
76 if (fOp == CONSTRAIN) {
77 BRegion region(r);
78 ConstrainClippingRegion(&region);
79 } else {
80 //RotateBy(0.1);
81 ClipToRect(r);
82 }
83 SetHighColor(0,0,255);
84 FillRect(Bounds());
85 }
86 PopState();
87}
88
89
90void
91View::Draw(BRect update)
92{
93 if (fSetScale)
94 SetScale(3);
95 else
96 ScaleBy(3,3);
97 _DrawRect(BRect(3,3,5,5));
98 _DrawRect(BRect(1,1,1,1));
99}