]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEvent.cxx
Canvas: Support for preventDefault() on Events.
[simgear.git] / simgear / canvas / CanvasEvent.cxx
1 // Canvas Event for event model similar to DOM Level 3 Event Model
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #include "CanvasEvent.hxx"
20
21 namespace simgear
22 {
23 namespace canvas
24 {
25
26   //----------------------------------------------------------------------------
27   Event::Event():
28     type(UNKNOWN),
29     time(-1),
30     propagation_stopped(false),
31     default_prevented(false)
32   {
33
34   }
35
36   //----------------------------------------------------------------------------
37   Event::~Event()
38   {
39
40   }
41
42   //----------------------------------------------------------------------------
43   bool Event::canBubble() const
44   {
45     return true;
46   }
47
48   //----------------------------------------------------------------------------
49   int Event::getType() const
50   {
51     return type;
52   }
53
54   //----------------------------------------------------------------------------
55   std::string Event::getTypeString() const
56   {
57     return typeToStr(type);
58   }
59
60   //----------------------------------------------------------------------------
61   ElementWeakPtr Event::getTarget() const
62   {
63     return target;
64   }
65
66   //----------------------------------------------------------------------------
67   ElementWeakPtr Event::getCurrentTarget() const
68   {
69     return current_target;
70   }
71
72   //----------------------------------------------------------------------------
73   double Event::getTime() const
74   {
75     return time;
76   }
77
78   //----------------------------------------------------------------------------
79   void Event::stopPropagation()
80   {
81     propagation_stopped = true;
82   }
83
84   //----------------------------------------------------------------------------
85   void Event::preventDefault()
86   {
87     default_prevented = true;
88   }
89
90   //----------------------------------------------------------------------------
91   bool Event::defaultPrevented() const
92   {
93     return default_prevented;
94   }
95
96   //----------------------------------------------------------------------------
97   int Event::getOrRegisterType(const std::string& type_str)
98   {
99     int type = strToType(type_str);
100
101     if( type == UNKNOWN )
102     {
103       // Register new type
104       TypeMap& type_map = getTypeMap();
105       type = type_map.size() + 1; // ids start with 1 (after UNKNOWN)
106       type_map.insert(TypeMap::value_type(type_str, type));
107     }
108
109     return type;
110   }
111
112   //----------------------------------------------------------------------------
113   int Event::strToType(const std::string& str)
114   {
115     TypeMap const& type_map = getTypeMap();
116
117     TypeMap::map_by<name>::const_iterator it = type_map.by<name>().find(str);
118     if( it == type_map.by<name>().end() )
119       return UNKNOWN;
120     return it->second;
121   }
122
123   //----------------------------------------------------------------------------
124   std::string Event::typeToStr(int type)
125   {
126     TypeMap const& type_map = getTypeMap();
127
128     TypeMap::map_by<id>::const_iterator it = type_map.by<id>().find(type);
129     if( it == type_map.by<id>().end() )
130       return "unknown";
131     return it->second;
132   }
133
134   //----------------------------------------------------------------------------
135   Event::TypeMap& Event::getTypeMap()
136   {
137     static TypeMap type_map;
138
139     if( type_map.empty() )
140     {
141 #   define ENUM_MAPPING(type, str, class_name)\
142       type_map.insert(TypeMap::value_type(str, type));
143 #     include "CanvasEventTypes.hxx"
144 #   undef ENUM_MAPPING
145     }
146
147     return type_map;
148   }
149
150 } // namespace canvas
151 } // namespace simgear