vfs: check userland buffers before reading them.
[haiku.git] / src / tests / servers / app / following / main.cpp
blob955a8a18952e8b5aa40049e7e20fd4ef989b227c
1 // main.cpp
3 #include <stdio.h>
4 #include <stdlib.h>
6 #include <Application.h>
7 #include <Message.h>
8 #include <Button.h>
9 #include <View.h>
10 #include <Window.h>
12 enum {
13 TRACKING_NONE = 0,
14 TRACKING_ALL,
15 TRACKING_RIGHT,
16 TRACKING_BOTTOM,
17 TRACKING_RIGHT_BOTTOM,
20 class TestView : public BView {
22 public:
23 TestView(BRect frame, const char* name,
24 uint32 resizeFlags, uint32 flags)
25 : BView(frame, name, resizeFlags, flags),
26 fTracking(TRACKING_NONE),
27 fLastMousePos(0.0, 0.0)
29 rgb_color color;
30 color.red = rand() / 256;
31 color.green = rand() / 256;
32 color.blue = rand() / 256;
33 color.alpha = 255;
34 SetViewColor(color);
35 SetLowColor(color);
38 virtual void Draw(BRect updateRect);
40 virtual void MouseDown(BPoint where);
41 virtual void MouseUp(BPoint where);
42 virtual void MouseMoved(BPoint where, uint32 transit,
43 const BMessage* dragMessage);
45 private:
46 uint32 fTracking;
48 BPoint fLastMousePos;
51 // Draw
52 void
53 TestView::Draw(BRect updateRect)
55 // text
56 SetHighColor(0, 0, 0, 255);
57 const char* message = "Click and drag to move this view!";
58 DrawString(message, BPoint(20.0, 30.0));
60 BRect r(Bounds());
61 r.right -= 15.0;
62 r.bottom -= 15.0;
63 StrokeLine(r.RightTop(), BPoint(r.right, Bounds().bottom));
64 StrokeLine(r.LeftBottom(), BPoint(Bounds().right, r.bottom));
67 // MouseDown
68 void
69 TestView::MouseDown(BPoint where)
71 BRect r(Bounds());
72 r.right -= 15.0;
73 r.bottom -= 15.0;
74 if (r.Contains(where))
75 fTracking = TRACKING_ALL;
76 else if (r.bottom < where.y && r.right < where.x)
77 fTracking = TRACKING_RIGHT_BOTTOM;
78 else if (r.bottom < where.y)
79 fTracking = TRACKING_BOTTOM;
80 else if (r.right < where.x)
81 fTracking = TRACKING_RIGHT;
83 fLastMousePos = where;
84 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
87 // MouseUp
88 void
89 TestView::MouseUp(BPoint where)
91 fTracking = TRACKING_NONE;
94 // MouseMoved
95 void
96 TestView::MouseMoved(BPoint where, uint32 transit,
97 const BMessage* dragMessage)
99 BPoint offset = where - fLastMousePos;
100 switch (fTracking) {
101 case TRACKING_ALL:
102 MoveBy(offset.x, offset.y);
103 // fLastMousePos stays fixed
104 break;
105 case TRACKING_RIGHT:
106 ResizeBy(offset.x, 0.0);
107 fLastMousePos = where;
108 break;
109 case TRACKING_BOTTOM:
110 ResizeBy(0.0, offset.y);
111 fLastMousePos = where;
112 break;
113 case TRACKING_RIGHT_BOTTOM:
114 ResizeBy(offset.x, offset.y);
115 fLastMousePos = where;
116 break;
122 // show_window
123 void
124 show_window(BRect frame, const char* name)
126 BWindow* window = new BWindow(frame, name,
127 B_TITLED_WINDOW,
128 B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
130 BView* view = new TestView(window->Bounds(), "test 1", B_FOLLOW_ALL,
131 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE);
133 window->AddChild(view);
135 BRect bounds = view->Bounds();
136 bounds.InsetBy(20, 20);
137 BView* view1 = new TestView(bounds, "test 2", B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM,
138 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE);
139 view->AddChild(view1);
141 bounds = view1->Bounds();
142 bounds.InsetBy(20, 20);
143 BView* view2 = new TestView(bounds, "test 3", B_FOLLOW_NONE,
144 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE);
145 view1->AddChild(view2);
148 window->Show();
151 // main
153 main(int argc, char** argv)
155 BApplication* app = new BApplication("application/x.vnd-Haiku.Following");
157 BRect frame(50.0, 50.0, 300.0, 250.0);
158 show_window(frame, "Following Test");
160 app->Run();
162 delete app;
163 return 0;