1 /* 2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <Application.h> 8 #include <Box.h> 9 #include <LayoutBuilder.h> 10 #include <MessageRunner.h> 11 #include <StringView.h> 12 #include <Window.h> 13 14 #include <ToolTip.h> 15 16 #include <stdio.h> 17 18 19 class CustomToolTip : public BToolTip { 20 public: 21 CustomToolTip() 22 { 23 fView = new BStringView("", "Custom tool tip!"); 24 fView->SetFont(be_bold_font); 25 fView->SetHighColor(255, 0, 0); 26 } 27 28 virtual ~CustomToolTip() 29 { 30 delete fView; 31 } 32 33 virtual BView* View() const 34 { 35 return fView; 36 } 37 38 private: 39 BStringView* fView; 40 }; 41 42 43 class ChangingView : public BStringView { 44 public: 45 ChangingView() 46 : 47 BStringView("changing", ""), 48 fNumber(0) 49 { 50 } 51 52 virtual ~ChangingView() 53 { 54 } 55 56 virtual void AttachedToWindow() 57 { 58 BStringView::AttachedToWindow(); 59 60 BMessage update('updt'); 61 fRunner = new BMessageRunner(this, &update, 100000); 62 } 63 64 virtual void DetachedFromWindow() 65 { 66 delete fRunner; 67 } 68 69 virtual void MessageReceived(BMessage* message) 70 { 71 if (message->what == 'updt') { 72 char text[42]; 73 snprintf(text, sizeof(text), "%d", ++fNumber); 74 SetText(text); 75 } 76 } 77 78 private: 79 BMessageRunner* fRunner; 80 int fNumber; 81 }; 82 83 84 class ChangingToolTip : public BToolTip { 85 public: 86 ChangingToolTip() 87 { 88 fView = new ChangingView(); 89 fView->SetFont(be_bold_font); 90 } 91 92 virtual ~ChangingToolTip() 93 { 94 delete fView; 95 } 96 97 virtual BView* View() const 98 { 99 return fView; 100 } 101 102 private: 103 BStringView* fView; 104 }; 105 106 107 class MouseView : public BStringView { 108 public: 109 MouseView() 110 : 111 BStringView("mouse", "x:y") 112 { 113 } 114 115 virtual ~MouseView() 116 { 117 } 118 119 virtual void AttachedToWindow() 120 { 121 BStringView::AttachedToWindow(); 122 SetEventMask(B_POINTER_EVENTS, 0); 123 } 124 125 virtual void MouseMoved(BPoint where, uint32 transit, 126 const BMessage* dragMessage) 127 { 128 ConvertToScreen(&where); 129 130 char text[42]; 131 snprintf(text, sizeof(text), "%g:%g", where.x, where.y); 132 SetText(text); 133 } 134 }; 135 136 137 class MouseToolTip : public BToolTip { 138 public: 139 MouseToolTip() 140 { 141 fView = new MouseView(); 142 SetSticky(true); 143 } 144 145 virtual ~MouseToolTip() 146 { 147 delete fView; 148 } 149 150 virtual BView* View() const 151 { 152 return fView; 153 } 154 155 private: 156 BStringView* fView; 157 }; 158 159 160 class ImmediateView : public BStringView { 161 public: 162 ImmediateView(const char* name, const char* label) 163 : 164 BStringView(name, label) 165 { 166 SetToolTip("Easy but immediate!"); 167 ToolTip()->SetSticky(true); 168 } 169 170 virtual void MouseMoved(BPoint where, uint32 transit, 171 const BMessage* dragMessage) 172 { 173 if (transit == B_ENTERED_VIEW) 174 ShowToolTip(ToolTip()); 175 } 176 }; 177 178 179 class Window : public BWindow { 180 public: 181 Window(); 182 183 virtual bool QuitRequested(); 184 }; 185 186 187 class Application : public BApplication { 188 public: 189 Application(); 190 191 virtual void ReadyToRun(); 192 }; 193 194 195 // #pragma mark - 196 197 198 Window::Window() 199 : 200 BWindow(BRect(100, 100, 520, 430), "ToolTip-Test", 201 B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS) 202 { 203 BView* simple = new BStringView("1", "Simple Tool Tip"); 204 simple->SetToolTip("This is a really\nsimple tool tip!"); 205 206 BView* custom = new BStringView("2", "Custom Tool Tip"); 207 custom->SetToolTip(new CustomToolTip()); 208 209 BView* changing = new BStringView("3", "Changing Tool Tip"); 210 changing->SetToolTip(new ChangingToolTip()); 211 212 BView* mouse = new BStringView("3", "Mouse Tool Tip (sticky)"); 213 mouse->SetToolTip(new MouseToolTip()); 214 215 BView* immediate = new ImmediateView("3", "Immediate Tool Tip (sticky)"); 216 217 BLayoutBuilder::Group<>(this, B_VERTICAL) 218 .Add(simple) 219 .Add(custom) 220 .Add(changing) 221 .Add(mouse) 222 .Add(immediate); 223 } 224 225 226 bool 227 Window::QuitRequested() 228 { 229 be_app->PostMessage(B_QUIT_REQUESTED); 230 return true; 231 } 232 233 234 // #pragma mark - 235 236 237 Application::Application() 238 : BApplication("application/x-vnd.haiku-tooltiptest") 239 { 240 } 241 242 243 void 244 Application::ReadyToRun() 245 { 246 BWindow *window = new Window(); 247 window->Show(); 248 } 249 250 251 // #pragma mark - 252 253 254 int 255 main(int argc, char **argv) 256 { 257 Application app; 258 259 app.Run(); 260 return 0; 261 } 262 263