Ticket #16097: MinApp.cpp

File MinApp.cpp, 2.7 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;
45public:
46 TestView(BRect frame, const char *name): BView(frame, name, B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE | B_WILL_DRAW | B_SUBPIXEL_PRECISE),
47 fMode(0),
48 fPos(0, 0)
49 {
50 }
51
52 void Draw(BRect dirty)
53 {
54 switch (fMode) {
55 case 0: {
56 {StateGroup sg(this);
57 TranslateBy(fPos.x, fPos.y);
58 BShape sh;
59 GetRoundRectShape(sh, BRect(0, 0, 63, 63), 8);
60 FillShape(&sh);
61 }
62 break;
63 }
64 case 1: {
65 {StateGroup sg(this);
66 TranslateBy(fPos.x, fPos.y);
67 BShape sh;
68 GetRoundRectShape(sh, BRect(0, 0, 63, 63), 8);
69 ClipToShape(&sh);
70 FillRect(BRect(0, 0, 63, 63));
71 }
72 break;
73 }
74 }
75 }
76
77 void KeyDown(const char* bytes, int32 numBytes)
78 {
79 switch (bytes[0]) {
80 case B_LEFT_ARROW: fPos.x--; Invalidate(); break;
81 case B_RIGHT_ARROW: fPos.x++; Invalidate(); break;
82 case B_UP_ARROW: fPos.y--; Invalidate(); break;
83 case B_DOWN_ARROW: fPos.y++; Invalidate(); break;
84 case '1': fMode = 0; Invalidate(); break;
85 case '2': fMode = 1; Invalidate(); break;
86 }
87 }
88};
89
90class TestWindow: public BWindow
91{
92private:
93 TestView *fView;
94public:
95 TestWindow(BRect frame): BWindow(frame, "TestApp", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
96 {
97 fView = new TestView(frame.OffsetToCopy(B_ORIGIN), "view");
98 fView->SetResizingMode(B_FOLLOW_ALL);
99 AddChild(fView, NULL);
100 fView->MakeFocus(true);
101 }
102
103 void Quit()
104 {
105 be_app_messenger.SendMessage(B_QUIT_REQUESTED);
106 BWindow::Quit();
107 }
108
109};
110
111class TestApplication: public BApplication
112{
113private:
114 TestWindow *fWnd;
115public:
116 TestApplication(): BApplication("application/x-vnd.test-app")
117 {
118 }
119
120 void ReadyToRun() {
121 fWnd = new TestWindow(BRect(0, 0, 255, 255).OffsetByCopy(64, 64));
122 fWnd->Show();
123 }
124};
125
126
127int main()
128{
129 TestApplication app;
130 app.Run();
131 return 0;
132}