]> git.saurik.com Git - wxWidgets.git/blob - src/common/event.cpp
Removed a few old wxDebugMsg (now wxLogDebug)
[wxWidgets.git] / src / common / event.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: event.cpp
3 // Purpose: Event classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "event.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/defs.h"
25 #include "wx/control.h"
26 #include "wx/utils.h"
27 #include "wx/app.h"
28 #include "wx/dc.h"
29 #endif
30
31 #include "wx/event.h"
32 #include "wx/validate.h"
33
34 #if !USE_SHARED_LIBRARY
35 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
36 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
37 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
38 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
39 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
40 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
41 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
42 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
43 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
44 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
45 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
46 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
47 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
48 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
49 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
50 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
51 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
52 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
53 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
54 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
55 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
56 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
57 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
58 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
59 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
60 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
61
62 const wxEventTable *wxEvtHandler::GetEventTable() const { return &wxEvtHandler::sm_eventTable; }
63
64 const wxEventTable wxEvtHandler::sm_eventTable =
65 { (const wxEventTable *) NULL, &wxEvtHandler::sm_eventTableEntries[0] };
66
67 const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] = { { 0, 0, 0,
68 #ifdef __SGI_CC__
69 // stupid SGI compiler --- offer aug 98
70 0L }
71 #else
72 NULL }
73 #endif
74 };
75
76 #endif
77
78 /*
79 * General wxWindows events, covering
80 * all interesting things that might happen (button clicking, resizing,
81 * setting text in widgets, etc.).
82 *
83 * For each completely new event type, derive a new event class.
84 *
85 */
86
87 wxEvent::wxEvent(int theId)
88 {
89 m_eventType = wxEVT_NULL;
90 m_eventObject = (wxObject *) NULL;
91 m_eventHandle = (char *) NULL;
92 m_timeStamp = 0;
93 m_id = theId;
94 m_skipped = FALSE;
95 m_callbackUserData = (wxObject *) NULL;
96 }
97
98 /*
99 * Command events
100 *
101 */
102
103 wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
104 {
105 m_eventType = commandType;
106 m_clientData = (char *) NULL;
107 m_clientObject = (wxClientData *) NULL;
108 m_extraLong = 0;
109 m_commandInt = 0;
110 m_id = theId;
111 m_commandString = (char *) NULL;
112 }
113
114 /*
115 * Scroll events
116 */
117
118 wxScrollEvent::wxScrollEvent(wxEventType commandType, int id, int pos, int orient):
119 wxCommandEvent(commandType, id)
120 {
121 m_extraLong = orient;
122 m_commandInt = pos;
123 }
124
125
126 /*
127 * Mouse events
128 *
129 */
130
131 wxMouseEvent::wxMouseEvent(wxEventType commandType)
132 {
133 m_eventType = commandType;
134 m_metaDown = FALSE;
135 m_altDown = FALSE;
136 m_controlDown = FALSE;
137 m_shiftDown = FALSE;
138 m_leftDown = FALSE;
139 m_rightDown = FALSE;
140 m_middleDown = FALSE;
141 m_x = 0;
142 m_y = 0;
143 }
144
145 // True if was a button dclick event (1 = left, 2 = middle, 3 = right)
146 // or any button dclick event (but = -1)
147 bool wxMouseEvent::ButtonDClick(int but) const
148 {
149 switch (but) {
150 case -1:
151 return (LeftDClick() || MiddleDClick() || RightDClick());
152 case 1:
153 return LeftDClick();
154 case 2:
155 return MiddleDClick();
156 case 3:
157 return RightDClick();
158 default:
159 return FALSE;
160 }
161 // NOTREACHED
162 }
163
164 // True if was a button down event (1 = left, 2 = middle, 3 = right)
165 // or any button down event (but = -1)
166 bool wxMouseEvent::ButtonDown(int but) const
167 {
168 switch (but) {
169 case -1:
170 return (LeftDown() || MiddleDown() || RightDown());
171 case 1:
172 return LeftDown();
173 case 2:
174 return MiddleDown();
175 case 3:
176 return RightDown();
177 default:
178 return FALSE;
179 }
180 // NOTREACHED
181 }
182
183 // True if was a button up event (1 = left, 2 = middle, 3 = right)
184 // or any button up event (but = -1)
185 bool wxMouseEvent::ButtonUp(int but) const
186 {
187 switch (but) {
188 case -1:
189 return (LeftUp() || MiddleUp() || RightUp());
190 case 1:
191 return LeftUp();
192 case 2:
193 return MiddleUp();
194 case 3:
195 return RightUp();
196 default:
197 return FALSE;
198 }
199 // NOTREACHED
200 }
201
202 // True if the given button is currently changing state
203 bool wxMouseEvent::Button(int but) const
204 {
205 switch (but) {
206 case -1:
207 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
208 case 1:
209 return (LeftDown() || LeftUp() || LeftDClick());
210 case 2:
211 return (MiddleDown() || MiddleUp() || MiddleDClick());
212 case 3:
213 return (RightDown() || RightUp() || RightDClick());
214 default:
215 return FALSE;
216 }
217 // NOTREACHED
218 }
219
220 bool wxMouseEvent::ButtonIsDown(int but) const
221 {
222 switch (but) {
223 case -1:
224 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
225 case 1:
226 return LeftIsDown();
227 case 2:
228 return MiddleIsDown();
229 case 3:
230 return RightIsDown();
231 default:
232 return FALSE;
233 }
234 // NOTREACHED
235 }
236
237 // Find the logical position of the event given the DC
238 wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
239 {
240 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
241 return pt;
242 }
243
244
245 /*
246 * Keyboard events
247 *
248 */
249
250 wxKeyEvent::wxKeyEvent(wxEventType type)
251 {
252 m_eventType = type;
253 m_shiftDown = FALSE;
254 m_controlDown = FALSE;
255 m_metaDown = FALSE;
256 m_altDown = FALSE;
257 m_keyCode = 0;
258 }
259
260 /*
261 * Event handler
262 */
263
264 wxEvtHandler::wxEvtHandler(void)
265 {
266 m_nextHandler = (wxEvtHandler *) NULL;
267 m_previousHandler = (wxEvtHandler *) NULL;
268 m_enabled = TRUE;
269 m_dynamicEvents = (wxList *) NULL;
270 }
271
272 wxEvtHandler::~wxEvtHandler(void)
273 {
274 // Takes itself out of the list of handlers
275 if (m_previousHandler)
276 m_previousHandler->m_nextHandler = m_nextHandler;
277
278 if (m_nextHandler)
279 m_nextHandler->m_previousHandler = m_previousHandler;
280
281 if (m_dynamicEvents)
282 {
283 wxNode *node = m_dynamicEvents->First();
284 while (node)
285 {
286 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
287 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
288 delete entry;
289 node = node->Next();
290 }
291 delete m_dynamicEvents;
292 };
293 }
294
295 /*
296 * Event table stuff
297 */
298
299 bool wxEvtHandler::ProcessEvent(wxEvent& event)
300 {
301 // An event handler can be enabled or disabled
302 if ( GetEvtHandlerEnabled() )
303 {
304 // Handle per-instance dynamic event tables first
305
306 if (SearchDynamicEventTable( event )) return TRUE;
307
308 // Then static per-class event tables
309
310 const wxEventTable *table = GetEventTable();
311
312 // Try the associated validator first, if this is a window.
313 // Problem: if the event handler of the window has been replaced,
314 // this wxEvtHandler may no longer be a window.
315 // Therefore validators won't be processed if the handler
316 // has been replaced with SetEventHandler.
317 // THIS CAN BE CURED if PushEventHandler is used instead of
318 // SetEventHandler, and then processing will be passed down the
319 // chain of event handlers.
320 if (IsKindOf(CLASSINFO(wxWindow)))
321 {
322 wxWindow *win = (wxWindow *)this;
323
324 // Can only use the validator of the window which
325 // is receiving the event
326 if ( (win == event.GetEventObject()) &&
327 win->GetValidator() &&
328 win->GetValidator()->ProcessEvent(event))
329 return TRUE;
330 }
331
332 // Search upwards through the inheritance hierarchy
333 while (table)
334 {
335 if (SearchEventTable((wxEventTable&)*table, event))
336 return TRUE;
337 table = table->baseTable;
338 }
339 }
340
341 // Try going down the event handler chain
342 if ( GetNextHandler() )
343 {
344 if ( GetNextHandler()->ProcessEvent(event) )
345 return TRUE;
346 }
347
348 // Carry on up the parent-child hierarchy,
349 // but only if event is a command event: it wouldn't
350 // make sense for a parent to receive a child's size event, for example
351 if (IsKindOf(CLASSINFO(wxWindow)) && event.IsKindOf(CLASSINFO(wxCommandEvent)))
352 {
353 wxWindow *win = (wxWindow *)this;
354 wxWindow *parent = win->GetParent();
355 if (parent && !parent->IsBeingDeleted())
356 return win->GetParent()->GetEventHandler()->ProcessEvent(event);
357 }
358
359 // Last try - application object.
360 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always swallow it.
361 // wxEVT_IDLE is sent explicitly to wxApp so it will be processed appropriately
362 // via SearchEventTable.
363 if (wxTheApp && this != wxTheApp && (event.GetEventType() != wxEVT_IDLE) && wxTheApp->ProcessEvent(event))
364 return TRUE;
365 else
366 return FALSE;
367 }
368
369 bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
370 {
371 int i = 0;
372 int commandId = event.GetId();
373
374 // BC++ doesn't like while (table.entries[i].m_fn)
375
376 while (table.entries[i].m_fn != 0L)
377 {
378 if ((event.GetEventType() == table.entries[i].m_eventType) &&
379 (table.entries[i].m_id == -1 || // Match, if event spec says any id will do (id == -1)
380 (table.entries[i].m_lastId == -1 && commandId == table.entries[i].m_id) ||
381 (table.entries[i].m_lastId != -1 &&
382 (commandId >= table.entries[i].m_id && commandId <= table.entries[i].m_lastId))))
383 {
384 event.Skip(FALSE);
385 event.m_callbackUserData = table.entries[i].m_callbackUserData;
386
387 (this->*((wxEventFunction) (table.entries[i].m_fn)))(event);
388
389 if ( event.GetSkipped() )
390 return FALSE;
391 else
392 return TRUE;
393 }
394 i ++;
395 }
396 return FALSE;
397 }
398
399 void wxEvtHandler::Connect( int id, int lastId,
400 int eventType,
401 wxObjectEventFunction func,
402 wxObject *userData )
403 {
404 wxEventTableEntry *entry = new wxEventTableEntry;
405 entry->m_id = id;
406 entry->m_lastId = lastId;
407 entry->m_eventType = eventType;
408 entry->m_fn = func;
409 entry->m_callbackUserData = userData;
410
411 if (!m_dynamicEvents)
412 m_dynamicEvents = new wxList;
413
414 m_dynamicEvents->Append( (wxObject*) entry );
415 }
416
417 bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
418 {
419 if (!m_dynamicEvents) return FALSE;
420
421 int commandId = event.GetId();
422
423 wxNode *node = m_dynamicEvents->First();
424 while (node)
425 {
426 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
427
428 if (entry->m_fn)
429 {
430 if ((event.GetEventType() == entry->m_eventType) &&
431 (entry->m_id == -1 || // Match, if event spec says any id will do (id == -1)
432 (entry->m_lastId == -1 && commandId == entry->m_id) ||
433 (entry->m_lastId != -1 &&
434 (commandId >= entry->m_id && commandId <= entry->m_lastId))))
435 {
436 event.Skip(FALSE);
437 event.m_callbackUserData = entry->m_callbackUserData;
438
439 (this->*((wxEventFunction) (entry->m_fn)))(event);
440
441 if (event.GetSkipped())
442 return FALSE;
443 else
444 return TRUE;
445 }
446 }
447 node = node->Next();
448 }
449 return FALSE;
450 };
451
452 bool wxEvtHandler::OnClose(void)
453 {
454 if (GetNextHandler()) return GetNextHandler()->OnClose();
455 else return FALSE;
456 }
457
458