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