]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
no message
[wxWidgets.git] / src / common / event.cpp
CommitLineData
c801d85f
KB
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
35IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
36IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
37IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
38IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
39IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
40IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
41IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
42IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
43IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
44IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
45IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
46IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
47IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
48IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
49IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
50IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
51IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
52IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
53IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
54IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
55IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
56IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
73fb82f3 57IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
b09dcbf5 58IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
f7bd2698
JS
59IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
60IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
c801d85f
KB
61
62const wxEventTable *wxEvtHandler::GetEventTable() const { return &wxEvtHandler::sm_eventTable; }
63
64const wxEventTable wxEvtHandler::sm_eventTable =
c67daf87 65 { (const wxEventTable *) NULL, &wxEvtHandler::sm_eventTableEntries[0] };
c801d85f 66
c67daf87
UR
67const 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};
c801d85f
KB
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
87wxEvent::wxEvent(int theId)
88{
7798a18e 89 m_eventType = wxEVT_NULL;
c67daf87
UR
90 m_eventObject = (wxObject *) NULL;
91 m_eventHandle = (char *) NULL;
c801d85f
KB
92 m_timeStamp = 0;
93 m_id = theId;
94 m_skipped = FALSE;
c67daf87 95 m_callbackUserData = (wxObject *) NULL;
c801d85f
KB
96}
97
98/*
99 * Command events
100 *
101 */
102
7798a18e 103wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
c801d85f
KB
104{
105 m_eventType = commandType;
c67daf87 106 m_clientData = (char *) NULL;
c801d85f
KB
107 m_extraLong = 0;
108 m_commandInt = 0;
109 m_id = theId;
c67daf87 110 m_commandString = (char *) NULL;
c801d85f
KB
111}
112
113/*
114 * Scroll events
115 */
116
7798a18e 117wxScrollEvent::wxScrollEvent(wxEventType commandType, int id, int pos, int orient):
c801d85f
KB
118 wxCommandEvent(commandType, id)
119{
120 m_extraLong = orient;
121 m_commandInt = pos;
122}
123
124
125/*
126 * Mouse events
127 *
128 */
129
7798a18e 130wxMouseEvent::wxMouseEvent(wxEventType commandType)
c801d85f
KB
131{
132 m_eventType = commandType;
133 m_metaDown = FALSE;
134 m_altDown = FALSE;
135 m_controlDown = FALSE;
136 m_shiftDown = FALSE;
a4294b78
JS
137 m_leftDown = FALSE;
138 m_rightDown = FALSE;
139 m_middleDown = FALSE;
140 m_x = 0;
141 m_y = 0;
c801d85f
KB
142}
143
144// True if was a button dclick event (1 = left, 2 = middle, 3 = right)
145// or any button dclick event (but = -1)
146bool wxMouseEvent::ButtonDClick(int but) const
147{
148 switch (but) {
149 case -1:
150 return (LeftDClick() || MiddleDClick() || RightDClick());
151 case 1:
152 return LeftDClick();
153 case 2:
154 return MiddleDClick();
155 case 3:
156 return RightDClick();
157 default:
158 return FALSE;
159 }
160 // NOTREACHED
161}
162
163// True if was a button down event (1 = left, 2 = middle, 3 = right)
164// or any button down event (but = -1)
165bool wxMouseEvent::ButtonDown(int but) const
166{
167 switch (but) {
168 case -1:
169 return (LeftDown() || MiddleDown() || RightDown());
170 case 1:
171 return LeftDown();
172 case 2:
173 return MiddleDown();
174 case 3:
175 return RightDown();
176 default:
177 return FALSE;
178 }
179 // NOTREACHED
180}
181
182// True if was a button up event (1 = left, 2 = middle, 3 = right)
183// or any button up event (but = -1)
184bool wxMouseEvent::ButtonUp(int but) const
185{
186 switch (but) {
187 case -1:
188 return (LeftUp() || MiddleUp() || RightUp());
189 case 1:
190 return LeftUp();
191 case 2:
192 return MiddleUp();
193 case 3:
194 return RightUp();
195 default:
196 return FALSE;
197 }
198 // NOTREACHED
199}
200
201// True if the given button is currently changing state
202bool wxMouseEvent::Button(int but) const
203{
204 switch (but) {
205 case -1:
206 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
207 case 1:
208 return (LeftDown() || LeftUp() || LeftDClick());
209 case 2:
210 return (MiddleDown() || MiddleUp() || MiddleDClick());
211 case 3:
212 return (RightDown() || RightUp() || RightDClick());
213 default:
214 return FALSE;
215 }
216 // NOTREACHED
217}
218
219bool wxMouseEvent::ButtonIsDown(int but) const
220{
221 switch (but) {
222 case -1:
223 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
224 case 1:
225 return LeftIsDown();
226 case 2:
227 return MiddleIsDown();
228 case 3:
229 return RightIsDown();
230 default:
231 return FALSE;
232 }
233 // NOTREACHED
234}
235
236// Find the logical position of the event given the DC
237wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
238{
0757d27c
JS
239 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
240 return pt;
c801d85f
KB
241}
242
243
244/*
245 * Keyboard events
246 *
247 */
248
7798a18e 249wxKeyEvent::wxKeyEvent(wxEventType type)
c801d85f
KB
250{
251 m_eventType = type;
252 m_shiftDown = FALSE;
253 m_controlDown = FALSE;
254 m_metaDown = FALSE;
255 m_altDown = FALSE;
256 m_keyCode = 0;
257}
258
259/*
260 * Event handler
261 */
262
263wxEvtHandler::wxEvtHandler(void)
264{
c67daf87
UR
265 m_clientData = (char *) NULL;
266 m_nextHandler = (wxEvtHandler *) NULL;
267 m_previousHandler = (wxEvtHandler *) NULL;
c801d85f 268 m_enabled = TRUE;
c67daf87 269 m_dynamicEvents = (wxList *) NULL;
c801d85f
KB
270}
271
272wxEvtHandler::~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;
fe71f65c
RR
280
281 if (m_dynamicEvents)
282 {
283 wxNode *node = m_dynamicEvents->First();
284 while (node)
285 {
286 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
7f4dc78d 287 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
fe71f65c
RR
288 delete entry;
289 node = node->Next();
290 }
291 delete m_dynamicEvents;
292 };
c801d85f
KB
293}
294
295/*
296 * Event table stuff
297 */
298
299bool wxEvtHandler::ProcessEvent(wxEvent& event)
300{
301 // An event handler can be enabled or disabled
302 if ( GetEvtHandlerEnabled() )
303 {
fe71f65c
RR
304 // Handle per-instance dynamic event tables first
305
306 if (SearchDynamicEventTable( event )) return TRUE;
307
308 // Then static per-class event tables
309
c801d85f
KB
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 {
0757d27c
JS
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;
c801d85f
KB
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 {
0757d27c
JS
344 if ( GetNextHandler()->ProcessEvent(event) )
345 return TRUE;
c801d85f
KB
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;
0757d27c 354 wxWindow *parent = win->GetParent();
c801d85f
KB
355 if (parent && !parent->IsBeingDeleted())
356 return win->GetParent()->GetEventHandler()->ProcessEvent(event);
357 }
358
5de76427
JS
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))
c801d85f
KB
364 return TRUE;
365 else
366 return FALSE;
367}
368
369bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
370{
371 int i = 0;
372 int commandId = event.GetId();
373
c67daf87
UR
374 while (table.entries[i].m_fn !=
375#ifdef __SGI_CC__
376 0L
377#else
378 NULL
379#endif
380 )
c801d85f 381 {
46dc76ba 382// wxEventType eventType = (wxEventType) table.entries[i].m_eventType;
6c6f51f6 383
c801d85f
KB
384 if ((event.GetEventType() == table.entries[i].m_eventType) &&
385 (table.entries[i].m_id == -1 || // Match, if event spec says any id will do (id == -1)
386 (table.entries[i].m_lastId == -1 && commandId == table.entries[i].m_id) ||
387 (table.entries[i].m_lastId != -1 &&
388 (commandId >= table.entries[i].m_id && commandId <= table.entries[i].m_lastId))))
389 {
0757d27c 390 event.Skip(FALSE);
fe71f65c 391 event.m_callbackUserData = table.entries[i].m_callbackUserData;
0757d27c 392
c801d85f
KB
393 (this->*((wxEventFunction) (table.entries[i].m_fn)))(event);
394
0757d27c
JS
395 if ( event.GetSkipped() )
396 return FALSE;
397 else
c801d85f
KB
398 return TRUE;
399 }
400 i ++;
401 }
402 return FALSE;
403}
404
debe6624
JS
405void wxEvtHandler::Connect( int id, int lastId,
406 int eventType,
0757d27c
JS
407 wxObjectEventFunction func,
408 wxObject *userData )
fe71f65c
RR
409{
410 wxEventTableEntry *entry = new wxEventTableEntry;
411 entry->m_id = id;
412 entry->m_lastId = lastId;
413 entry->m_eventType = eventType;
414 entry->m_fn = func;
415 entry->m_callbackUserData = userData;
416
417 if (!m_dynamicEvents)
418 m_dynamicEvents = new wxList;
419
420 m_dynamicEvents->Append( (wxObject*) entry );
421}
422
423bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
424{
425 if (!m_dynamicEvents) return FALSE;
426
427 int commandId = event.GetId();
6c6f51f6 428
fe71f65c
RR
429 wxNode *node = m_dynamicEvents->First();
430 while (node)
431 {
432 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
46dc76ba
RR
433
434// wxEventType eventType = (wxEventType) entry->m_eventType;
6c6f51f6 435
fe71f65c
RR
436 if (entry->m_fn)
437 {
438 if ((event.GetEventType() == entry->m_eventType) &&
439 (entry->m_id == -1 || // Match, if event spec says any id will do (id == -1)
440 (entry->m_lastId == -1 && commandId == entry->m_id) ||
441 (entry->m_lastId != -1 &&
442 (commandId >= entry->m_id && commandId <= entry->m_lastId))))
443 {
0757d27c 444 event.Skip(FALSE);
fe71f65c 445 event.m_callbackUserData = entry->m_callbackUserData;
0757d27c 446
fe71f65c
RR
447 (this->*((wxEventFunction) (entry->m_fn)))(event);
448
0757d27c
JS
449 if (event.GetSkipped())
450 return FALSE;
451 else
fe71f65c
RR
452 return TRUE;
453 }
454 };
455 node = node->Next();
456 }
457 return FALSE;
458};
459
c801d85f
KB
460bool wxEvtHandler::OnClose(void)
461{
462 if (GetNextHandler()) return GetNextHandler()->OnClose();
463 else return FALSE;
464}
465
466