fix typo, add instructions about lowercase named strings
[gnash.git] / libcore / event_id.h
blob84116dab347a3ec49ff560660db6d4d28daf4ccd
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
3 // Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef GNASH_EVENT_ID_H
22 #define GNASH_EVENT_ID_H
24 #include <string>
25 #include "string_table.h"
26 #include "GnashKey.h"
28 namespace gnash {
31 /// A class to identify 'static' SWF events (system events).
33 /// The event process in a SWF comprises the raising of the event itself and
34 /// its receipt by a handler. Events may be either dynamically or statically
35 /// defined. A dynamic event is handled in ActionScript: an AS-defined
36 /// function is called when the event is raised. Dynamic events do not need
37 /// event_id.
39 /// Event handlers may also be defined statically, for instance in a
40 /// PlaceObject2 tag, or by default. System events such as mouse handling,
41 /// load milestones, or keyboard events should be sent to appropriate
42 /// DisplayObjects. This process uses event_id.
44 /// Static events may additionally be handled dynamically (using ActionScript).
46 /// The event_id class is used as an identifier for actual events and
47 /// and for the signature of an expected event.
48 class event_id
50 public:
52 /// The types of events that are handled by DisplayObjects.
53 enum EventCode
55 INVALID,
57 // These are for buttons and sprites.
58 PRESS,
59 RELEASE,
60 RELEASE_OUTSIDE,
61 ROLL_OVER,
62 ROLL_OUT,
63 DRAG_OVER,
64 DRAG_OUT,
65 KEY_PRESS,
67 // These are for sprites only.
68 INITIALIZE,
69 LOAD,
70 UNLOAD,
71 ENTER_FRAME,
72 MOUSE_DOWN,
73 MOUSE_UP,
74 MOUSE_MOVE,
75 KEY_DOWN,
76 KEY_UP,
77 DATA,
78 CONSTRUCT
81 /// Construct an invalid event_id.
83 /// This is not useful until its values have been set.
84 event_id()
86 _id(INVALID),
87 _keyCode(key::INVALID)
90 /// Construct an event_id.
92 /// @param id The type of event
93 /// @param c The key associated with an event (only if this
94 /// is a keyboard event).
95 event_id(EventCode id, key::code c = key::INVALID)
97 _id(id),
98 _keyCode(c)
100 // We do have a testcase with _id == KEY_PRESS,
101 // and keyCode==0(KEY_INVALID)
102 // see key_event_test.swf(produced by Ming)
105 /// Set the key associated with this event.
107 /// @param SWFKey The SWF code matched to the event. This
108 /// must be converted to a unique gnash::key::code.
109 void setKeyCode(boost::uint8_t SWFkey)
111 // Lookup the SWFcode in the gnash::key::code table.
112 // Some are not unique (keypad numbers are the
113 // same as normal numbers), so we take the first match.
114 // As long as we can work out the SWFCode from the
115 // gnash::key::code it's all right.
116 int i = 0;
117 while (key::codeMap[i][key::SWF] != SWFkey && i < key::KEYCOUNT) i++;
119 if (i == key::KEYCOUNT) _keyCode = key::INVALID;
120 else _keyCode = static_cast<key::code>(i);
123 /// Return whether two event_ids are equal
125 /// event_ids are equal if both id and keycode match. Keycode is only
126 /// relevant for keyboard events, and must be key::INVALID for other
127 /// event types.
128 bool operator==(const event_id& id) const {
129 return _id == id._id && _keyCode == id._keyCode;
132 /// Comparator for use in stdlib containers.
133 bool operator< (const event_id& id) const {
134 if ( _id < id._id ) return true;
135 if ( _id > id._id ) return false;
137 // Same event, check key code
138 if (_keyCode < id._keyCode ) return true;
139 return false;
142 /// Return the name of a method-handler function
143 /// corresponding to this event.
144 const std::string& functionName() const;
146 /// Return the string_table key of a method-handler function
147 /// corresponding to this event.
148 string_table::key functionKey() const;
150 /// Return the keycode associated with this event_id.
152 /// This should be key::INVALID if the event_id is not a keyboard event.
153 key::code keyCode() const { return _keyCode; }
155 /// Return the identifier for this event type.
156 EventCode id() const { return _id; }
158 private:
160 EventCode _id;
162 // keyCode must be the unique gnash key identifier
163 // gnash::key::code.
164 // TextField has to be able to work out the
165 // ASCII value from keyCode, while other users need
166 // the SWF code or the Flash key code.
167 key::code _keyCode;
173 /// Check whether an event is a button-like event.
175 /// @param e The event to check
176 /// @return True if it is
177 bool isButtonEvent(const event_id& e);
179 /// Check whether an event is a keyboard event.
181 /// @param e The event to check
182 /// @return True if it is
183 bool isKeyEvent(const event_id& e);
185 std::ostream& operator<< (std::ostream& o, const event_id& ev);
187 } // namespace gnash
190 #endif
193 // Local Variables:
194 // mode: C++
195 // indent-tabs-mode: t
196 // End: