Ticket #16098: MinApp.cpp

File MinApp.cpp, 3.0 KB (added by X512, 4 years ago)

Test program.

Line 
1#include <Application.h>
2#include <Window.h>
3#include <View.h>
4#include <Shape.h>
5#include <Rect.h>
6#include <stdio.h>
7#include <math.h>
8
9class StateGroup
10{
11private:
12 BView *fView;
13public:
14 StateGroup(BView *view): fView(view) {fView->PushState();}
15 ~StateGroup() {fView->PopState();}
16};
17
18class LayerGroup
19{
20private:
21 BView *fView;
22public:
23 LayerGroup(BView *view, uint8 opacity): fView(view) {fView->BeginLayer(opacity);}
24 ~LayerGroup() {fView->EndLayer();}
25};
26
27void GetRoundRectShape(BShape &sh, BRect rc, float r)
28{
29 sh.MoveTo(rc.RightTop() - BPoint(r, 0));
30 sh.ArcTo(r, r, M_PI/2, false, false, rc.RightTop() + BPoint(0, r));
31 sh.LineTo(rc.RightBottom() - BPoint(0, r));
32 sh.ArcTo(r, r, M_PI/2, false, false, rc.RightBottom() - BPoint(r, 0));
33 sh.LineTo(rc.LeftBottom() + BPoint(r, 0));
34 sh.ArcTo(r, r, M_PI/2, false, false, rc.LeftBottom() - BPoint(0, r));
35 sh.LineTo(rc.LeftTop() + BPoint(0, r));
36 sh.ArcTo(r, r, M_PI/2, false, false, rc.LeftTop() + BPoint(r, 0));
37 sh.Close();
38}
39
40class TestView: public BView
41{
42private:
43 int32 fMode;
44 BPoint fPos;
45 float fInset;
46public:
47 TestView(BRect frame, const char *name): BView(frame, name, B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE | B_WILL_DRAW | B_SUBPIXEL_PRECISE),
48 fMode(0),
49 fPos(0, 0),
50 fInset(0)
51 {
52 }
53
54 void Draw(BRect dirty)
55 {
56 switch (fMode) {
57 case 0: {
58 {StateGroup sg(this);
59 TranslateBy(fPos.x, fPos.y);
60 BShape sh;
61 GetRoundRectShape(sh, BRect(0, 0, 63, 63), 8);
62 ClipToShape(&sh);
63 FillRect(BRect(0, 0, 63, 63).InsetByCopy(fInset, fInset));
64 }
65 break;
66 }
67 case 1: {
68 {StateGroup sg(this);
69 TranslateBy(fPos.x, fPos.y);
70 BShape sh;
71 GetRoundRectShape(sh, BRect(0, 0, 63, 63), 8);
72 {LayerGroup lg(this, 0x66);
73 ClipToShape(&sh);
74 FillRect(BRect(0, 0, 63, 63).InsetByCopy(fInset, fInset));
75 }
76 }
77 break;
78 }
79 }
80 }
81
82 void KeyDown(const char* bytes, int32 numBytes)
83 {
84 switch (bytes[0]) {
85 case B_LEFT_ARROW: fPos.x--; Invalidate(); break;
86 case B_RIGHT_ARROW: fPos.x++; Invalidate(); break;
87 case B_UP_ARROW: fPos.y--; Invalidate(); break;
88 case B_DOWN_ARROW: fPos.y++; Invalidate(); break;
89 case ',': fInset--; Invalidate(); break;
90 case '.': fInset++; Invalidate(); break;
91 case '1': fMode = 0; Invalidate(); break;
92 case '2': fMode = 1; Invalidate(); break;
93 }
94 }
95};
96
97class TestWindow: public BWindow
98{
99private:
100 TestView *fView;
101public:
102 TestWindow(BRect frame): BWindow(frame, "TestApp", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
103 {
104 fView = new TestView(frame.OffsetToCopy(B_ORIGIN), "view");
105 fView->SetResizingMode(B_FOLLOW_ALL);
106 AddChild(fView, NULL);
107 fView->MakeFocus(true);
108 }
109
110 void Quit()
111 {
112 be_app_messenger.SendMessage(B_QUIT_REQUESTED);
113 BWindow::Quit();
114 }
115
116};
117
118class TestApplication: public BApplication
119{
120private:
121 TestWindow *fWnd;
122public:
123 TestApplication(): BApplication("application/x-vnd.test-app")
124 {
125 }
126
127 void ReadyToRun() {
128 fWnd = new TestWindow(BRect(0, 0, 255, 255).OffsetByCopy(64, 64));
129 fWnd->Show();
130 }
131};
132
133
134int main()
135{
136 TestApplication app;
137 app.Run();
138 return 0;
139}