]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEvent.cxx
canvas::Text: add heightForWidth method.
[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   {
32
33   }
34
35   //----------------------------------------------------------------------------
36   Event::~Event()
37   {
38
39   }
40
41   //----------------------------------------------------------------------------
42   bool Event::canBubble() const
43   {
44     return true;
45   }
46
47   //----------------------------------------------------------------------------
48   int Event::getType() const
49   {
50     return type;
51   }
52
53   //----------------------------------------------------------------------------
54   std::string Event::getTypeString() const
55   {
56     return typeToStr(type);
57   }
58
59   //----------------------------------------------------------------------------
60   ElementWeakPtr Event::getTarget() const
61   {
62     return target;
63   }
64
65   //----------------------------------------------------------------------------
66   ElementWeakPtr Event::getCurrentTarget() const
67   {
68     return current_target;
69   }
70
71   //----------------------------------------------------------------------------
72   double Event::getTime() const
73   {
74     return time;
75   }
76
77   //----------------------------------------------------------------------------
78   void Event::stopPropagation()
79   {
80     propagation_stopped = true;
81   }
82
83   //----------------------------------------------------------------------------
84   int Event::getOrRegisterType(const std::string& type_str)
85   {
86     int type = strToType(type_str);
87
88     if( type == UNKNOWN )
89     {
90       // Register new type
91       TypeMap& type_map = getTypeMap();
92       type = type_map.size() + 1; // ids start with 1 (after UNKNOWN)
93       type_map.insert(TypeMap::value_type(type_str, type));
94     }
95
96     return type;
97   }
98
99   //----------------------------------------------------------------------------
100   int Event::strToType(const std::string& str)
101   {
102     TypeMap const& type_map = getTypeMap();
103
104     TypeMap::map_by<name>::const_iterator it = type_map.by<name>().find(str);
105     if( it == type_map.by<name>().end() )
106       return UNKNOWN;
107     return it->second;
108   }
109
110   //----------------------------------------------------------------------------
111   std::string Event::typeToStr(int type)
112   {
113     TypeMap const& type_map = getTypeMap();
114
115     TypeMap::map_by<id>::const_iterator it = type_map.by<id>().find(type);
116     if( it == type_map.by<id>().end() )
117       return "unknown";
118     return it->second;
119   }
120
121   //----------------------------------------------------------------------------
122   Event::TypeMap& Event::getTypeMap()
123   {
124     static TypeMap type_map;
125
126     if( type_map.empty() )
127     {
128 #   define ENUM_MAPPING(type, str)\
129       type_map.insert(TypeMap::value_type(str, type));
130 #     include "CanvasEventTypes.hxx"
131 #   undef ENUM_MAPPING
132     }
133
134     return type_map;
135   }
136
137 } // namespace canvas
138 } // namespace simgear