1 | #include <stdio.h>
|
---|
2 |
|
---|
3 | #include <Application.h>
|
---|
4 | #include <Window.h>
|
---|
5 | #include <View.h>
|
---|
6 | #include <Rect.h>
|
---|
7 |
|
---|
8 |
|
---|
9 | class TestView final: public BView {
|
---|
10 | private:
|
---|
11 | TestView *fSubViews[4] {};
|
---|
12 |
|
---|
13 | private:
|
---|
14 | void UpdateSubViews()
|
---|
15 | {
|
---|
16 | float width = Frame().Width();
|
---|
17 | float height = Frame().Height();
|
---|
18 |
|
---|
19 | float d = 4;
|
---|
20 | if (width >= 3*d && height >= 3*d) {
|
---|
21 | BRect rects[4];
|
---|
22 |
|
---|
23 | rects[0].Set(d, d, width/2 - d/2, height/2 - d/2);
|
---|
24 | rects[1].Set(width/2 + d/2, d, width - d, height/2 - d/2);
|
---|
25 | rects[2].Set(d, height/2 + d/2, width/2 - d/2, height - d);
|
---|
26 | rects[3].Set(width/2 + d/2, height/2 + d/2, width - d, height - d);
|
---|
27 |
|
---|
28 | if (fSubViews[0] == NULL) {
|
---|
29 | for (int32 i = 0; i < 4; i++) {
|
---|
30 | fSubViews[i] = new TestView(rects[i], "view");
|
---|
31 | AddChild(fSubViews[i]);
|
---|
32 | }
|
---|
33 | } else {
|
---|
34 | for (int32 i = 0; i < 4; i++) {
|
---|
35 | fSubViews[i]->MoveTo(rects[i].LeftTop());
|
---|
36 | fSubViews[i]->ResizeTo(rects[i].Width(), rects[i].Height());
|
---|
37 | }
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | if (fSubViews[0] != NULL) {
|
---|
41 | for (int32 i = 0; i < 4; i++) {
|
---|
42 | RemoveChild(fSubViews[i]);
|
---|
43 | delete fSubViews[i];
|
---|
44 | fSubViews[i] = NULL;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public:
|
---|
51 | TestView(BRect frame, const char *name, uint32 resizingMode = B_FOLLOW_NONE):
|
---|
52 | BView(frame, name, resizingMode, B_WILL_DRAW | B_SUBPIXEL_PRECISE | B_FULL_UPDATE_ON_RESIZE | B_FRAME_EVENTS)
|
---|
53 | {
|
---|
54 | UpdateSubViews();
|
---|
55 | }
|
---|
56 |
|
---|
57 | void Draw(BRect dirty) final
|
---|
58 | {
|
---|
59 | BRect rect = Frame().OffsetToCopy(B_ORIGIN);
|
---|
60 | SetLowColor(0xff, 0xff, 0xff);
|
---|
61 | SetHighColor(0, 0, 0);
|
---|
62 | FillRect(rect, B_SOLID_LOW);
|
---|
63 | StrokeRect(rect, B_SOLID_HIGH);
|
---|
64 | }
|
---|
65 |
|
---|
66 | void FrameResized(float width, float height) final
|
---|
67 | {
|
---|
68 | UpdateSubViews();
|
---|
69 | }
|
---|
70 |
|
---|
71 | };
|
---|
72 |
|
---|
73 |
|
---|
74 | int main()
|
---|
75 | {
|
---|
76 | BApplication app("application/x-vnd.Test-App");
|
---|
77 | BWindow *wnd = new BWindow(
|
---|
78 | BRect(0, 0, 256, 256).OffsetByCopy(64, 64),
|
---|
79 | "RecursiveViews",
|
---|
80 | B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
|
---|
81 | B_QUIT_ON_WINDOW_CLOSE | B_ASYNCHRONOUS_CONTROLS
|
---|
82 | );
|
---|
83 | TestView *view = new TestView(wnd->Frame().OffsetToCopy(B_ORIGIN), "view", B_FOLLOW_ALL);
|
---|
84 | wnd->AddChild(view);
|
---|
85 | wnd->Show();
|
---|
86 | app.Run();
|
---|
87 | return 0;
|
---|
88 | }
|
---|