1 /// @file 2 /// Canvas Event for event model similar to DOM Level 3 Event Model 3 // 4 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com> 5 // 6 // This library is free software; you can redistribute it and/or 7 // modify it under the terms of the GNU Library General Public 8 // License as published by the Free Software Foundation; either 9 // version 2 of the License, or (at your option) any later version. 10 // 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 // Library General Public License for more details. 15 // 16 // You should have received a copy of the GNU Library General Public 17 // License along with this library; if not, write to the Free Software 18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 19 20 #ifndef CANVAS_EVENT_HXX_ 21 #define CANVAS_EVENT_HXX_ 22 23 #include "canvas_fwd.hxx" 24 #include <boost/bimap.hpp> 25 26 namespace simgear 27 { 28 namespace canvas 29 { 30 31 /** 32 * Base class for all Canvas events. 33 * 34 * The event system is closely following the specification of the DOM Level 3 35 * Event Model. 36 */ 37 class Event: 38 public SGReferenced 39 { 40 public: 41 42 /// Event type identifier 43 enum Type 44 { 45 UNKNOWN, 46 # define ENUM_MAPPING(name, str, class_name)\ 47 name, /*!< class_name (type=str) */ 48 # include "CanvasEventTypes.hxx" 49 # undef ENUM_MAPPING 50 CUSTOM_EVENT ///< First event type id available for user defined event 51 /// type. 52 /// @see CustomEvent 53 }; 54 55 int type; 56 ElementWeakPtr target, 57 current_target; 58 double time; 59 bool propagation_stopped, 60 default_prevented; 61 62 Event(); 63 64 // We need a vtable to allow nasal::Ghost to determine the dynamic type 65 // of the actual event instances. 66 virtual ~Event(); 67 68 /** 69 * Clone event and set to the given type (Same type if not specified) 70 */ 71 virtual Event* clone(int type = 0) const = 0; 72 73 /** 74 * Get whether this events support bubbling 75 */ 76 virtual bool canBubble() const; 77 78 /** 79 * Set type of event. 80 * 81 * If no such type exists it is registered. 82 */ 83 void setType(const std::string& type); 84 85 int getType() const; 86 std::string getTypeString() const; 87 88 ElementWeakPtr getTarget() const; 89 ElementWeakPtr getCurrentTarget() const; 90 91 /** 92 * Get time at which the event was generated. 93 */ 94 double getTime() const; 95 96 /** 97 * Prevent further propagation of the event during event flow. 98 * 99 * @note This does not prevent calling further event handlers registered 100 * on the current event target. 101 */ 102 void stopPropagation(); 103 104 /** 105 * Cancel any default action normally taken as result of this event. 106 * 107 * @note For event handlers registered on the DesktopGroup (Nasal: 108 * canvas.getDesktop()) this stops the event from being further 109 * propagated to the normal FlightGear input event handling code. 110 */ 111 void preventDefault(); 112 113 /** 114 * Get if preventDefault() has been called. 115 */ 116 bool defaultPrevented() const; 117 118 /** 119 * Register a new type string or get the id of an existing type string 120 * 121 * @param type Type string 122 * @return Id of the given @a type 123 */ 124 static int getOrRegisterType(const std::string& type); 125 126 static int strToType(const std::string& type); 127 static std::string typeToStr(int type); 128 129 protected: 130 struct name {}; 131 struct id {}; 132 typedef boost::bimaps::bimap< 133 boost::bimaps::tagged<std::string, name>, 134 boost::bimaps::tagged<int, id> 135 > TypeMap; 136 137 static TypeMap& getTypeMap(); 138 139 }; 140 141 } // namespace canvas 142 } // namespace simgear 143 144 #endif /* CANVAS_EVENT_HXX_ */ 145