00001 /* 00002 libwt - Vassilis Virvilis Toolkit - a widget library 00003 Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Lesser General Public 00007 License as published by the Free Software Foundation; either 00008 version 2.1 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Lesser General Public License for more details. 00014 00015 You should have received a copy of the GNU Lesser General Public 00016 License along with this library; if not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, SA. 00019 */ 00020 00021 #ifndef WT_EVENT_H 00022 #define WT_EVENT_H 00023 00024 #include <wt/region.h> 00025 #include <wt/trace.h> 00026 00027 namespace Wt { 00028 00029 class Object; 00030 00031 class Event { 00032 public: 00033 /// events that are delivered per widget 00034 typedef enum { 00035 Nope, 00036 Timer, 00037 MouseButtonPress, 00038 MouseButtonRelease, 00039 MouseButtonDblClick, 00040 MouseMove, 00041 KeyPress, //6 00042 KeyRelease, 00043 FocusIn, 00044 FocusOut, 00045 Enter, 00046 Leave, //11 00047 Paint, 00048 Move, 00049 Resize, //14 00050 Wheel, 00051 ChildInserted, //16 00052 ChildRemoved, 00053 LayoutHint, //18 00054 Show, 00055 Hide, 00056 Close, 00057 User, 00058 MaxEvent 00059 } Type; 00060 00061 /// 00062 Event(int type) : type_(type) { 00063 trace("event") << "Creating event " << this << std::endl; 00064 } 00065 /// 00066 virtual ~Event() { 00067 trace("event") << "Destroying event " << this << std::endl; 00068 } 00069 00070 /// 00071 int type() const { 00072 return type_; 00073 } 00074 00075 private: 00076 int type_; 00077 }; 00078 00079 class TimerEvent : public Event { 00080 public: 00081 TimerEvent(int timer_id) 00082 : Event(Timer), 00083 timer_id_(timer_id) {} 00084 00085 int timerid() const { 00086 return timer_id_; 00087 } 00088 00089 private: 00090 int timer_id_; 00091 }; 00092 00093 class PaintEvent : public Event { 00094 public: 00095 PaintEvent(const Rect& rect, bool erased = true) 00096 : Event(Paint), region_(rect), rect_(rect), erased_(erased) {} 00097 00098 PaintEvent(const Region& region, bool erased = true) 00099 : Event(Paint), region_(region), 00100 rect_(region.boundingRect()), erased_(erased) {} 00101 00102 const Region& region() const { 00103 return region_; 00104 } 00105 00106 const Rect& rect() const { 00107 return rect_; 00108 } 00109 00110 bool erased() const { 00111 return erased_; 00112 } 00113 00114 private: 00115 Region region_; 00116 Rect rect_; 00117 bool erased_; 00118 }; 00119 00120 class ResizeEvent : public Event { 00121 public: 00122 ResizeEvent(const Size& size, const Size& oldsize) 00123 : Event(Resize), size_(size), oldsize_(oldsize) {} 00124 00125 const Size& size() const { 00126 return size_; 00127 } 00128 00129 const Size& oldSize() const { 00130 return oldsize_; 00131 } 00132 00133 private: 00134 Size size_; 00135 Size oldsize_; 00136 }; 00137 00138 class MoveEvent : public Event { 00139 public: 00140 MoveEvent(const Point& pos, const Point& oldpos) 00141 : Event(Move), pos_(pos), oldpos_(oldpos) {} 00142 00143 const Point& pos() const { 00144 return pos_; 00145 } 00146 00147 const Point& oldPos() const { 00148 return oldpos_; 00149 } 00150 00151 private: 00152 Point pos_; 00153 Point oldpos_; 00154 }; 00155 00156 class LayoutHintEvent : public Event { 00157 public: 00158 LayoutHintEvent() 00159 : Event(LayoutHint) {} 00160 private: 00161 }; 00162 00163 class ShowEvent : public Event { 00164 public: 00165 ShowEvent() 00166 : Event(Show) {} 00167 private: 00168 }; 00169 00170 class HideEvent : public Event { 00171 public: 00172 HideEvent() 00173 : Event(Hide) {} 00174 private: 00175 }; 00176 00177 class ChildEvent : public Event { 00178 public: 00179 ChildEvent(int type, Object *child) 00180 : Event(type), 00181 inserted_((ChildInserted == type) ? true : false), 00182 child_(child) {} 00183 00184 Object *child() const { 00185 return child_; 00186 } 00187 00188 bool inserted() const { 00189 return inserted_; 00190 } 00191 00192 bool removed() const { 00193 return !inserted_; 00194 } 00195 00196 private: 00197 bool inserted_; 00198 Object *child_; 00199 }; 00200 00201 class CustomEvent : public Event { 00202 public: 00203 CustomEvent(int event_offset = 0) 00204 : Event(User + event_offset) { 00205 // void comment to trick astyle 00206 } 00207 }; 00208 00209 class InputEvent : public Event { 00210 public: 00211 InputEvent(int type, int state) : 00212 Event(type), 00213 state_(static_cast<ButtonState>(state)), 00214 accepted_(false) {} 00215 00216 ButtonState state() const { 00217 return state_; 00218 } 00219 /// \todo fix stateAfter() 00220 ButtonState stateAfter() const { 00221 return state_; 00222 } 00223 bool isAccepted() const { 00224 return accepted_; 00225 } 00226 void accept() { 00227 accepted_ = true; 00228 } 00229 void ignore() { 00230 accepted_ = false; 00231 } 00232 00233 private: 00234 ButtonState state_; 00235 bool accepted_; 00236 }; 00237 00238 class MouseEvent : public InputEvent { 00239 public: 00240 MouseEvent(int type, const Point& pos, int button, int state) : 00241 InputEvent(type, state), 00242 pos_(pos), 00243 button_(static_cast<ButtonState>(button)) {} 00244 MouseEvent(int type, const Point& pos, 00245 const Point& global_pos, int button, int state) : 00246 InputEvent(type, state), 00247 pos_(pos), 00248 global_pos_(global_pos), 00249 button_(static_cast<ButtonState>(button)) {} 00250 00251 const Point& pos() const { 00252 return pos_; 00253 } 00254 const Point& globalPos() const { 00255 return global_pos_; 00256 } 00257 int x() const { 00258 return pos_.x(); 00259 } 00260 int y() const { 00261 return pos_.y(); 00262 } 00263 int globalX() const { 00264 return global_pos_.x(); 00265 } 00266 int globalY() const { 00267 return global_pos_.y(); 00268 } 00269 ButtonState button() const { 00270 return button_; 00271 } 00272 00273 private: 00274 Point pos_; 00275 Point global_pos_; 00276 ButtonState button_; 00277 }; 00278 00279 class WheelEvent : public MouseEvent { 00280 public: 00281 WheelEvent(const Point& pos, int delta, int state) : 00282 MouseEvent(Wheel, pos, MidButton, state), 00283 delta_(delta) {} 00284 00285 WheelEvent(const Point& pos, const Point& global_pos, 00286 int delta, int state) : 00287 MouseEvent(Wheel, pos, global_pos, MidButton, state), 00288 delta_(delta) {} 00289 00290 int delta() const { 00291 return delta_; 00292 } 00293 private: 00294 int delta_; 00295 }; 00296 00297 class KeyEvent : public InputEvent { 00298 public: 00299 KeyEvent(int type, int key, int state) : 00300 InputEvent(type, state), 00301 key_(key) {} 00302 00303 int key() const { 00304 return key_; 00305 }; 00306 00307 /// \todo ascii chars 00308 int ascii() const { 00309 return 0; 00310 } 00311 00312 /// \todo unicode chars 00313 int text() const { 00314 return 0; 00315 } 00316 00317 /// we receive one event for every autoreapeat 00318 /// so it's not possible to distinguish it 00319 bool isAutoRepeat() const { 00320 return false; 00321 } 00322 00323 /// one char per event 00324 int count() const { 00325 return 1; 00326 } 00327 00328 private: 00329 int key_; 00330 }; 00331 00332 class FocusEvent : public Event { 00333 public: 00334 typedef enum { 00335 Mouse, 00336 Tab, 00337 Backtab, 00338 ActiveWindow, 00339 Popup, 00340 Shortcut, 00341 Other 00342 } Reason; 00343 00344 FocusEvent(int type, int reason = Other) : 00345 Event(type), 00346 reason_(reason) {} 00347 00348 bool gotFocus() const { 00349 return type() == FocusIn; 00350 } 00351 00352 bool lostFocus() const { 00353 return type() == FocusOut; 00354 } 00355 00356 int reason() { 00357 return reason_; 00358 } 00359 00360 private: 00361 int reason_; 00362 }; 00363 00364 class CloseEvent : public Event { 00365 public: 00366 CloseEvent() 00367 : Event(Close), 00368 accepted_(false) {} 00369 00370 bool isAccepted() const { 00371 return accepted_; 00372 } 00373 void accept() { 00374 accepted_ = true; 00375 } 00376 void ignore() { 00377 accepted_ = false; 00378 } 00379 00380 private: 00381 bool accepted_; 00382 }; 00383 00384 } 00385 #endif // WT_EVENT_H
This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.