1 | #include <Application.h>
|
---|
2 | #include <Window.h>
|
---|
3 | #include <View.h>
|
---|
4 | #include <Region.h>
|
---|
5 |
|
---|
6 |
|
---|
7 | enum OP {
|
---|
8 | CONSTRAIN,
|
---|
9 | CLIP,
|
---|
10 | NONE
|
---|
11 | };
|
---|
12 |
|
---|
13 | class App : public BApplication {
|
---|
14 | public:
|
---|
15 | App();
|
---|
16 | };
|
---|
17 |
|
---|
18 | class Window : public BWindow {
|
---|
19 | public:
|
---|
20 | Window();
|
---|
21 | };
|
---|
22 |
|
---|
23 | class View : public BView {
|
---|
24 | public:
|
---|
25 | View(BRect frame, bool, OP);
|
---|
26 | void Draw(BRect);
|
---|
27 | void _DrawRect(BRect);
|
---|
28 |
|
---|
29 | public:
|
---|
30 | bool fSetScale;
|
---|
31 | OP fOp;
|
---|
32 | };
|
---|
33 |
|
---|
34 | int main()
|
---|
35 | {
|
---|
36 | App app;
|
---|
37 | app.Run();
|
---|
38 | return 0;
|
---|
39 | }
|
---|
40 |
|
---|
41 | App::App()
|
---|
42 | :BApplication("application/x-vnd.some-test-app")
|
---|
43 | {
|
---|
44 | Window *window = new Window();
|
---|
45 | window->Show();
|
---|
46 | }
|
---|
47 |
|
---|
48 | Window::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 |
|
---|
62 | View::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 |
|
---|
70 | void
|
---|
71 | View::_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(®ion);
|
---|
79 | } else {
|
---|
80 | //RotateBy(0.1);
|
---|
81 | ClipToRect(r);
|
---|
82 | }
|
---|
83 | SetHighColor(0,0,255);
|
---|
84 | FillRect(Bounds());
|
---|
85 | }
|
---|
86 | PopState();
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | void
|
---|
91 | View::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 | }
|
---|