tbx  0.7.5
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 class EventRouter
56 {
57 public:
58  EventRouter();
59  ~EventRouter();
60 
61  static EventRouter *instance() {return _instance;}
62 
63  void poll();
64  void yield();
65 
66  // Turn on catching of uncaught exceptions (default to on)
67  void catch_exceptions(bool c) {_catch_exceptions = c;}
68  void uncaught_handler(UncaughtHandler *handler) {_uncaught_handler = handler;}
69 
70 private:
71  // Allow base listener adding classes access to low level listener adding routines
72  friend class Application;
73  friend class Object;
74  friend class Component;
75  friend class Window;
76 
77  // Toolbox events
78  void add_autocreate_listener(const char *template_name, AutoCreateListener *listener);
79  void remove_autocreate_listener(const char *template_name);
80 
81  void add_object_listener(ObjectId handle, ComponentId component_id, int action, Listener *listener, RawToolboxEventHandler handler);
82  void remove_object_listener(ObjectId handle, ComponentId component_id, int action, Listener *listener);
83  void set_object_handler(ObjectId handle, int action, Listener *listener, RawToolboxEventHandler handler);
84 
85  void add_window_event_listener(ObjectId handle, int event_code, Listener *listener);
86  void remove_window_event_listener(ObjectId handle, int event_code, Listener *listener);
87  void add_window_event_listener(ObjectId handle, ComponentId component_id, int event_code, Listener *listener);
88  void remove_window_event_listener(ObjectId handle, ComponentId component_id, int event_code, Listener *listener);
89 
90  void remove_all_listeners(ObjectId handle);
91  void remove_all_listeners(ObjectId handle, ComponentId component_id);
92 
93  void set_autocreate_listener(std::string template_name, AutoCreateListener *listener);
94  void clear_autocreate_listener(std::string template_name);
95 
96  void add_message_listener(int type, int message_id, WimpMessageListener *listener);
97  void remove_message_listener(int type, int message_id, WimpMessageListener *listener);
98 
99  void add_null_event_command(Command *command);
100  void remove_null_event_command(Command *command);
101 
102  void set_drag_handler(DragHandler *handler, int drag_stop_swi = 0);
103  void cancel_drag();
104 
105  void add_timer(int elapsed, Timer *timer);
106  void remove_timer(Timer *timer);
107 
108 private:
109  void route_event(int event_code);
110 
111  void process_toolbox_event();
112  bool process_toolbox_event(ObjectId object_id, ComponentId comp_id);
113 
114  void process_null_event();
115  void process_redraw_request();
116  void process_open_window_request();
117  void process_close_window_request();
118  void process_pointer_leaving_window();
119  void process_pointer_entering_window();
120  void process_mouse_click();
121  void process_key_pressed();
122  void process_scroll_request();
123  void process_lose_caret();
124  void process_gain_caret();
125 
126  void process_user_message();
127  void process_recorded_message();
128  void process_acknowledge_message();
129 
130 private:
131  IdBlock _id_block;
132  int _poll_mask;
133  PollBlock _poll_block;
134  int _reply_to;
135  bool _catch_exceptions;
136  UncaughtHandler *_uncaught_handler;
137 
138  PrePollListener *_pre_poll_listener;
139  PostPollListener *_post_poll_listener;
140  PostEventListener *_post_event_listener;
141 
142 
143  // List item for object/component toolbox events
144  struct ObjectListenerItem
145  {
146  int action;
147  ComponentId component_id;
148  RawToolboxEventHandler handler;
149  Listener *listener;
150  ObjectListenerItem *next;
151  };
152 
153  // List item for WIMP window events
154  struct WindowEventListenerItem
155  {
156  Listener *listener;
157  WindowEventListenerItem *next;
158  ComponentId component_id;
159  };
160 
161  // List item for wimp messages
162  struct WimpMessageListenerItem
163  {
164  int message_id;
165  WimpMessageListener *listener;
166  WimpMessageListenerItem *next;
167  };
168 
169 
170  // Running event delete helpers
171  bool _remove_running;
172  ObjectListenerItem *_running_object_item;
173  WindowEventListenerItem *_running_window_event_item;
174  WimpMessageListenerItem *_running_message_item;
175 
176  std::map<ObjectId, ObjectListenerItem *> _object_listeners;
177  std::map<std::string, AutoCreateListener*> *_autocreate_listeners;
178  std::map<int, WimpMessageListenerItem *> **_message_listeners;
179  std::map<ObjectId, WindowEventListenerItem **> *_window_event_listeners;
180 
181  // Internal class to look after null events
182  class NullCommandQueue
183  {
184  Command **_commands;
185  unsigned int _capacity;
186  unsigned int _size;
187  unsigned int _next;
188  unsigned int _end;
189  public:
190  NullCommandQueue();
191  ~NullCommandQueue();
192 
193  void add(Command *command);
194  void remove(Command *command);
195  Command *next();
196 
197  unsigned int size() const {return _size;}
198  } *_null_event_commands;
199 
200  DragHandler *_drag_handler;
201  int _drag_stop_swi;
202 
203  struct TimerInfo
204  {
205  unsigned int due;
206  unsigned int elapsed;
207  Timer *timer;
208  TimerInfo *next;
209  } *_first_timer;
210 
211 private:
212  // Listener list helpers
213  ObjectListenerItem *find_first_object_listener(ObjectId handle, int action);
214  WindowEventListenerItem *find_window_event_listener(ObjectId object_id, int event_code);
215  WindowEventListenerItem *find_window_event_component(WindowEventListenerItem *&item, ComponentId component_id);
216  void remove_running_window_event_listener(ObjectId object_id, int event_code);
217 
218  void add_timer_info(TimerInfo *info);
219 
220 private:
221  static EventRouter *_instance;
222 };
223 
224 
225 inline EventRouter *event_router() {return EventRouter::instance();}
226 
227 }
228 
230 
231 #endif
A library for creating RISC OS toolbox applications.
Definition: abouttobeshownlistener.cc:34
void(* RawToolboxEventHandler)(IdBlock &id_block, PollBlock &data, Listener *listener)
Function type for handlers of raw (unprocessed) Toolbox events.
Definition: pollinfo.h:86
int ComponentId
Type for underlying toolbox component id.
Definition: handles.h:33
unsigned int ObjectId
Type for underlying toolbox object id.
Definition: handles.h:31