1 // main.cpp 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 6 #include <Application.h> 7 #include <Message.h> 8 #include <Button.h> 9 #include <View.h> 10 #include <Window.h> 11 12 enum { 13 TRACKING_NONE = 0, 14 TRACKING_ALL, 15 TRACKING_RIGHT, 16 TRACKING_BOTTOM, 17 TRACKING_RIGHT_BOTTOM, 18 }; 19 20 class TestView : public BView { 21 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) 28 { 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); 36 } 37 38 virtual void Draw(BRect updateRect); 39 40 virtual void MouseDown(BPoint where); 41 virtual void MouseUp(BPoint where); 42 virtual void MouseMoved(BPoint where, uint32 transit, 43 const BMessage* dragMessage); 44 45 private: 46 uint32 fTracking; 47 48 BPoint fLastMousePos; 49 }; 50 51 // Draw 52 void 53 TestView::Draw(BRect updateRect) 54 { 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)); 59 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)); 65 } 66 67 // MouseDown 68 void 69 TestView::MouseDown(BPoint where) 70 { 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; 82 83 fLastMousePos = where; 84 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); 85 } 86 87 // MouseUp 88 void 89 TestView::MouseUp(BPoint where) 90 { 91 fTracking = TRACKING_NONE; 92 } 93 94 // MouseMoved 95 void 96 TestView::MouseMoved(BPoint where, uint32 transit, 97 const BMessage* dragMessage) 98 { 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; 117 } 118 } 119 120 121 122 // show_window 123 void 124 show_window(BRect frame, const char* name) 125 { 126 BWindow* window = new BWindow(frame, name, 127 B_TITLED_WINDOW, 128 B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE); 129 130 BView* view = new TestView(window->Bounds(), "test 1", B_FOLLOW_ALL, 131 B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE); 132 133 window->AddChild(view); 134 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); 140 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); 146 147 148 window->Show(); 149 } 150 151 // main 152 int 153 main(int argc, char** argv) 154 { 155 BApplication* app = new BApplication("application/x.vnd-Haiku.Following"); 156 157 BRect frame(50.0, 50.0, 300.0, 250.0); 158 show_window(frame, "Following Test"); 159 160 app->Run(); 161 162 delete app; 163 return 0; 164 } 165