]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEvent.hxx
Fix VS2010 lack of fminf
[simgear.git] / simgear / canvas / CanvasEvent.hxx
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        * Get whether this events support bubbling
70        */
71       virtual bool canBubble() const;
72
73       /**
74        * Set type of event.
75        *
76        * If no such type exists it is registered.
77        */
78       void setType(const std::string& type);
79
80       int getType() const;
81       std::string getTypeString() const;
82
83       ElementWeakPtr getTarget() const;
84       ElementWeakPtr getCurrentTarget() const;
85
86       /**
87        * Get time at which the event was generated.
88        */
89       double getTime() const;
90
91       /**
92        * Prevent further propagation of the event during event flow.
93        *
94        * @note This does not prevent calling further event handlers registered
95        *       on the current event target.
96        */
97       void stopPropagation();
98
99       /**
100        * Cancel any default action normally taken as result of this event.
101        *
102        * @note For event handlers registered on the DesktopGroup (Nasal:
103        *       canvas.getDesktop()) this stops the event from being further
104        *       propagated to the normal FlightGear input event handling code.
105        */
106       void preventDefault();
107
108       /**
109        * Get if preventDefault() has been called.
110        */
111       bool defaultPrevented() const;
112
113       static int getOrRegisterType(const std::string& type);
114       static int strToType(const std::string& type);
115       static std::string typeToStr(int type);
116
117     protected:
118       struct name {};
119       struct id {};
120       typedef boost::bimaps::bimap<
121         boost::bimaps::tagged<std::string, name>,
122         boost::bimaps::tagged<int, id>
123       > TypeMap;
124
125       static TypeMap& getTypeMap();
126
127   };
128
129 } // namespace canvas
130 } // namespace simgear
131
132 #endif /* CANVAS_EVENT_HXX_ */