tbx  0.7.6
eventrouter.h
1 /*
2  * tbx RISC OS toolbox library
3  *
4  * Copyright (C) 2010-2012 Alan Buckley All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef EVENTROUTER_H
26 #define EVENTROUTER_H
27 
28 #include <map>
29 #include <vector>
30 #include <string>
31 #include "listener.h"
32 #include "autocreatelistener.h"
33 #include "pollinfo.h"
34 
35 namespace tbx
36 {
37 
38 // This class is internal to the workings of TBX
40 
41 class Application;
42 class Object;
43 class Component;
44 class Window;
45 class WimpMessageListener;
46 class RedrawListener;
47 class DragHandler;
48 class Command;
49 class Timer;
50 class PrePollListener;
51 class PostPollListener;
52 class PostEventListener;
53 class UncaughtHandler;
54 
55 /* Dummy window events to include component key presses/mouse click with those on the window */
56 const int WINDOW_AND_COMPONENT_KEY_PRESSED = 12;
57 const int WINDOW_AND_COMPONENT_MOUSE_CLICK = 13;
58 
59 class EventRouter
60 {
61 public:
62  EventRouter();
63  ~EventRouter();
64 
65  static EventRouter *instance() {return _instance;}
66 
67  void poll();
68  void yield();
69 
70  // Turn on catching of uncaught exceptions (default to on)
71  void catch_exceptions(bool c) {_catch_exceptions = c;}
72  void uncaught_handler(UncaughtHandler *handler) {_uncaught_handler = handler;}
73 
74 private:
75  // Allow base listener adding classes access to low level listener adding routines
76  friend class Application;
77  friend class Object;
78  friend class Component;
79  friend class Window;
80 
81  // Toolbox events
82  void add_autocreate_listener(const char *template_name, AutoCreateListener *listener);
83  void remove_autocreate_listener(const char *template_name);
84 
85  void add_object_listener(ObjectId handle, ComponentId component_id, int action, Listener *listener, RawToolboxEventHandler handler);
86  void remove_object_listener(ObjectId handle, ComponentId component_id, int action, Listener *listener);
87  void set_object_handler(ObjectId handle, int action, Listener *listener, RawToolboxEventHandler handler);
88 
89  void add_window_event_listener(ObjectId handle, int event_code, Listener *listener);
90  void remove_window_event_listener(ObjectId handle, int event_code, Listener *listener);
91  void add_window_event_listener(ObjectId handle, ComponentId component_id, int event_code, Listener *listener);
92  void remove_window_event_listener(ObjectId handle, ComponentId component_id, int event_code, Listener *listener);
93  void add_window_component_event_listener(ObjectId handle, int event_code, Listener *listener);
94  void remove_window_component_event_listener(ObjectId handle, int event_code, Listener *listener);
95 
96  void remove_all_listeners(ObjectId handle);
97  void remove_all_listeners(ObjectId handle, ComponentId component_id);
98 
99  void set_autocreate_listener(std::string template_name, AutoCreateListener *listener);
100  void clear_autocreate_listener(std::string template_name);
101 
102  void add_message_listener(int type, int message_id, WimpMessageListener *listener);
103  void remove_message_listener(int type, int message_id, WimpMessageListener *listener);
104 
105  void add_null_event_command(Command *command);
106  void remove_null_event_command(Command *command);
107 
108  void set_drag_handler(DragHandler *handler, int drag_stop_swi = 0);
109  void cancel_drag();
110 
111  void add_timer(int elapsed, Timer *timer);
112  void remove_timer(Timer *timer);
113 
114 private:
115  void route_event(int event_code);
116 
117  void process_toolbox_event();
118  bool process_toolbox_event(ObjectId object_id, ComponentId comp_id);
119 
120  void process_null_event();
121  void process_redraw_request();
122  void process_open_window_request();
123  void process_close_window_request();
124  void process_pointer_leaving_window();
125  void process_pointer_entering_window();
126  void process_mouse_click();
127  void process_key_pressed();
128  void process_scroll_request();
129  void process_lose_caret();
130  void process_gain_caret();
131 
132  void process_user_message();
133  void process_recorded_message();
134  void process_acknowledge_message();
135 
136 private:
137  IdBlock _id_block;
138  int _poll_mask;
139  PollBlock _poll_block;
140  int _reply_to;
141  bool _catch_exceptions;
142  UncaughtHandler *_uncaught_handler;
143 
144  PrePollListener *_pre_poll_listener;
145  PostPollListener *_post_poll_listener;
146  PostEventListener *_post_event_listener;
147 
148 
149  // List item for object/component toolbox events
150  struct ObjectListenerItem
151  {
152  int action;
153  ComponentId component_id;
154  RawToolboxEventHandler handler;
155  Listener *listener;
156  ObjectListenerItem *next;
157  };
158 
159  // List item for WIMP window events
160  struct WindowEventListenerItem
161  {
162  Listener *listener;
163  WindowEventListenerItem *next;
164  ComponentId component_id;
165  };
166 
167  // List item for wimp messages
168  struct WimpMessageListenerItem
169  {
170  int message_id;
171  WimpMessageListener *listener;
172  WimpMessageListenerItem *next;
173  };
174 
175 
176  // Running event delete helpers
177  bool _remove_running;
178  ObjectListenerItem *_running_object_item;
179  WindowEventListenerItem *_running_window_event_item;
180  WimpMessageListenerItem *_running_message_item;
181 
182  std::map<ObjectId, ObjectListenerItem *> _object_listeners;
183  std::map<std::string, AutoCreateListener*> *_autocreate_listeners;
184  std::map<int, WimpMessageListenerItem *> **_message_listeners;
185  std::map<ObjectId, WindowEventListenerItem **> *_window_event_listeners;
186 
187  // Internal class to look after null events
188  class NullCommandQueue
189  {
190  Command **_commands;
191  unsigned int _capacity;
192  unsigned int _size;
193  unsigned int _next;
194  unsigned int _end;
195  public:
196  NullCommandQueue();
197  ~NullCommandQueue();
198 
199  void add(Command *command);
200  void remove(Command *command);
201  Command *next();
202 
203  unsigned int size() const {return _size;}
204  } *_null_event_commands;
205 
206  DragHandler *_drag_handler;
207  int _drag_stop_swi;
208 
209  struct TimerInfo
210  {
211  unsigned int due;
212  unsigned int elapsed;
213  Timer *timer;
214  TimerInfo *next;
215  } *_first_timer;
216 
217 private:
218  // Listener list helpers
219  ObjectListenerItem *find_first_object_listener(ObjectId handle, int action);
220  WindowEventListenerItem *find_window_event_listener(ObjectId object_id, int event_code);
221  WindowEventListenerItem *find_window_event_component(WindowEventListenerItem *&item, ComponentId component_id);
222  void remove_running_window_event_listener(ObjectId object_id, int event_code);
223 
224  void add_timer_info(TimerInfo *info);
225 
226 private:
227  static EventRouter *_instance;
228 };
229 
230 
231 inline EventRouter *event_router() {return EventRouter::instance();}
232 
233 }
234 
236 
237 #endif
tbx
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:35
tbx::ComponentId
int ComponentId
Type for underlying toolbox component id.
Definition: handles.h:33
tbx::RawToolboxEventHandler
void(* RawToolboxEventHandler)(IdBlock &id_block, PollBlock &data, Listener *listener)
Function type for handlers of raw (unprocessed) Toolbox events.
Definition: pollinfo.h:86
tbx::ObjectId
unsigned int ObjectId
Type for underlying toolbox object id.
Definition: handles.h:31