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 #include <simgear_config.h>
21 
22 #include "CanvasEvent.hxx"
23 
24 namespace simgear
25 {
26 namespace canvas
27 {
28 
29   //----------------------------------------------------------------------------
Event()30   Event::Event():
31     type(UNKNOWN),
32     time(-1),
33     propagation_stopped(false),
34     default_prevented(false)
35   {
36 
37   }
38 
39   //----------------------------------------------------------------------------
~Event()40   Event::~Event()
41   {
42 
43   }
44 
45   //----------------------------------------------------------------------------
canBubble() const46   bool Event::canBubble() const
47   {
48     return true;
49   }
50 
51   //----------------------------------------------------------------------------
getType() const52   int Event::getType() const
53   {
54     return type;
55   }
56 
57   //----------------------------------------------------------------------------
getTypeString() const58   std::string Event::getTypeString() const
59   {
60     return typeToStr(type);
61   }
62 
63   //----------------------------------------------------------------------------
getTarget() const64   ElementWeakPtr Event::getTarget() const
65   {
66     return target;
67   }
68 
69   //----------------------------------------------------------------------------
getCurrentTarget() const70   ElementWeakPtr Event::getCurrentTarget() const
71   {
72     return current_target;
73   }
74 
75   //----------------------------------------------------------------------------
getTime() const76   double Event::getTime() const
77   {
78     return time;
79   }
80 
81   //----------------------------------------------------------------------------
stopPropagation()82   void Event::stopPropagation()
83   {
84     propagation_stopped = true;
85   }
86 
87   //----------------------------------------------------------------------------
preventDefault()88   void Event::preventDefault()
89   {
90     default_prevented = true;
91   }
92 
93   //----------------------------------------------------------------------------
defaultPrevented() const94   bool Event::defaultPrevented() const
95   {
96     return default_prevented;
97   }
98 
99   //----------------------------------------------------------------------------
getOrRegisterType(const std::string & type_str)100   int Event::getOrRegisterType(const std::string& type_str)
101   {
102     int type = strToType(type_str);
103 
104     if( type == UNKNOWN )
105     {
106       // Register new type
107       TypeMap& type_map = getTypeMap();
108       type = type_map.size() + 1; // ids start with 1 (after UNKNOWN)
109       type_map.insert(TypeMap::value_type(type_str, type));
110     }
111 
112     return type;
113   }
114 
115   //----------------------------------------------------------------------------
strToType(const std::string & str)116   int Event::strToType(const std::string& str)
117   {
118     TypeMap const& type_map = getTypeMap();
119 
120     TypeMap::map_by<name>::const_iterator it = type_map.by<name>().find(str);
121     if( it == type_map.by<name>().end() )
122       return UNKNOWN;
123     return it->second;
124   }
125 
126   //----------------------------------------------------------------------------
typeToStr(int type)127   std::string Event::typeToStr(int type)
128   {
129     auto const& map_by_id = getTypeMap().by<id>();
130 
131     auto it = map_by_id.find(type);
132     if( it == map_by_id.end() )
133       return "unknown";
134     return it->second;
135   }
136 
137   //----------------------------------------------------------------------------
getTypeMap()138   Event::TypeMap& Event::getTypeMap()
139   {
140     static TypeMap type_map;
141 
142     if( type_map.empty() )
143     {
144 #   define ENUM_MAPPING(type, str, class_name)\
145       type_map.insert(TypeMap::value_type(str, type));
146 #     include "CanvasEventTypes.hxx"
147 #   undef ENUM_MAPPING
148     }
149 
150     return type_map;
151   }
152 
153 } // namespace canvas
154 } // namespace simgear
155