]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
Committing in .
[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
e90c1d2a 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
8e193f38
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
0b746ba8 21 #pragma implementation "event.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
0b746ba8 28 #pragma hdrstop
c801d85f
KB
29#endif
30
31#ifndef WX_PRECOMP
0b746ba8 32 #include "wx/defs.h"
0b746ba8 33 #include "wx/app.h"
e90c1d2a
VZ
34 #include "wx/list.h"
35
36 #if wxUSE_GUI
37 #include "wx/control.h"
38 #include "wx/utils.h"
39 #include "wx/dc.h"
40 #endif // wxUSE_GUI
c801d85f
KB
41#endif
42
43#include "wx/event.h"
e90c1d2a
VZ
44
45#if wxUSE_GUI
46 #include "wx/validate.h"
47#endif // wxUSE_GUI
c801d85f 48
8e193f38
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
23f681ec
VZ
53IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
54IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
55IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
e90c1d2a 56
23f681ec
VZ
57#if wxUSE_GUI
58 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
59 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
60 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
61 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
62 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
63 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
64 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
65 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
66 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
67 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
69 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
70 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
78 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
85#endif // wxUSE_GUI
0b746ba8 86
23f681ec
VZ
87const wxEventTable *wxEvtHandler::GetEventTable() const
88 { return &wxEvtHandler::sm_eventTable; }
0b746ba8 89
23f681ec
VZ
90const wxEventTable wxEvtHandler::sm_eventTable =
91 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
0b746ba8 92
23f681ec
VZ
93const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
94 { { 0, 0, 0, (wxObjectEventFunction) NULL, (wxObject*) NULL } };
0b746ba8 95
c801d85f 96
8e193f38
VZ
97// ----------------------------------------------------------------------------
98// global variables
99// ----------------------------------------------------------------------------
100
101// To put pending event handlers
102wxList *wxPendingEvents = (wxList *)NULL;
103
7214297d 104#if wxUSE_THREADS
8e193f38
VZ
105 // protects wxPendingEvents list
106 wxCriticalSection *wxPendingEventsLocker = (wxCriticalSection *)NULL;
7214297d
GL
107#endif
108
8e193f38
VZ
109// ============================================================================
110// implementation
111// ============================================================================
112
113// ----------------------------------------------------------------------------
114// wxEvent
115// ----------------------------------------------------------------------------
116
c801d85f
KB
117/*
118 * General wxWindows events, covering
119 * all interesting things that might happen (button clicking, resizing,
120 * setting text in widgets, etc.).
121 *
122 * For each completely new event type, derive a new event class.
123 *
124 */
125
126wxEvent::wxEvent(int theId)
127{
0b746ba8
VZ
128 m_eventType = wxEVT_NULL;
129 m_eventObject = (wxObject *) NULL;
0b746ba8
VZ
130 m_timeStamp = 0;
131 m_id = theId;
132 m_skipped = FALSE;
133 m_callbackUserData = (wxObject *) NULL;
193bf013 134 m_isCommandEvent = FALSE;
c801d85f
KB
135}
136
aadbdf11 137void wxEvent::CopyObject(wxObject& object_dest) const
a737331d 138{
e90c1d2a 139 wxEvent *obj = (wxEvent *)&object_dest;
aadbdf11
GL
140 wxObject::CopyObject(object_dest);
141
142 obj->m_eventType = m_eventType;
143 obj->m_eventObject = m_eventObject;
aadbdf11
GL
144 obj->m_timeStamp = m_timeStamp;
145 obj->m_id = m_id;
146 obj->m_skipped = m_skipped;
147 obj->m_callbackUserData = m_callbackUserData;
148 obj->m_isCommandEvent = m_isCommandEvent;
a737331d
GL
149}
150
e90c1d2a
VZ
151#if wxUSE_GUI
152
c801d85f
KB
153/*
154 * Command events
155 *
156 */
157
7798a18e 158wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
c801d85f 159{
0b746ba8
VZ
160 m_eventType = commandType;
161 m_clientData = (char *) NULL;
162 m_clientObject = (wxClientData *) NULL;
163 m_extraLong = 0;
164 m_commandInt = 0;
165 m_id = theId;
04392d70 166 m_commandString = wxEmptyString;
193bf013 167 m_isCommandEvent = TRUE;
c801d85f
KB
168}
169
aadbdf11
GL
170void wxCommandEvent::CopyObject(wxObject& obj_d) const
171{
e90c1d2a 172 wxCommandEvent *obj = (wxCommandEvent *)&obj_d;
aadbdf11
GL
173
174 wxEvent::CopyObject(obj_d);
175
924ef850
RR
176 obj->m_clientData = m_clientData;
177 obj->m_clientObject = m_clientObject;
178 obj->m_extraLong = m_extraLong;
179 obj->m_commandInt = m_commandInt;
180 obj->m_commandString = m_commandString;
181}
182
183/*
184 * Notify events
185 */
186
187void wxNotifyEvent::CopyObject(wxObject& obj_d) const
188{
189 wxNotifyEvent *obj = (wxNotifyEvent *)&obj_d;
190
191 wxEvent::CopyObject(obj_d);
192
193 if (!m_bAllow) obj->Veto();
aadbdf11
GL
194}
195
c801d85f
KB
196/*
197 * Scroll events
198 */
199
0b746ba8
VZ
200wxScrollEvent::wxScrollEvent(wxEventType commandType,
201 int id,
202 int pos,
203 int orient)
204 : wxCommandEvent(commandType, id)
c801d85f 205{
0b746ba8
VZ
206 m_extraLong = orient;
207 m_commandInt = pos;
c801d85f
KB
208}
209
d1367c3d
RR
210/*
211 * ScrollWin events
212 */
213
214wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
215 int pos,
216 int orient)
d1367c3d 217{
c5b42c87 218 m_eventType = commandType;
d1367c3d
RR
219 m_extraLong = orient;
220 m_commandInt = pos;
221}
222
3c679789
RR
223void wxScrollWinEvent::CopyObject(wxObject& obj_d) const
224{
e90c1d2a 225 wxScrollWinEvent *obj = (wxScrollWinEvent*)&obj_d;
3c679789
RR
226
227 wxEvent::CopyObject(obj_d);
228
229 obj->m_extraLong = m_extraLong;
230 obj->m_commandInt = m_commandInt;
231}
232
c801d85f
KB
233/*
234 * Mouse events
235 *
236 */
237
7798a18e 238wxMouseEvent::wxMouseEvent(wxEventType commandType)
c801d85f 239{
0b746ba8
VZ
240 m_eventType = commandType;
241 m_metaDown = FALSE;
242 m_altDown = FALSE;
243 m_controlDown = FALSE;
244 m_shiftDown = FALSE;
245 m_leftDown = FALSE;
246 m_rightDown = FALSE;
247 m_middleDown = FALSE;
248 m_x = 0;
249 m_y = 0;
c801d85f
KB
250}
251
aadbdf11
GL
252void wxMouseEvent::CopyObject(wxObject& obj_d) const
253{
254 wxMouseEvent *obj = (wxMouseEvent *)&obj_d;
255
256 wxEvent::CopyObject(obj_d);
257
258 obj->m_metaDown = m_metaDown;
259 obj->m_altDown = m_altDown;
260 obj->m_controlDown = m_controlDown;
261 obj->m_shiftDown = m_shiftDown;
262 obj->m_leftDown = m_leftDown;
263 obj->m_rightDown = m_rightDown;
264 obj->m_middleDown = m_middleDown;
265 obj->m_x = m_x;
266 obj->m_y = m_y;
267}
268
c801d85f
KB
269// True if was a button dclick event (1 = left, 2 = middle, 3 = right)
270// or any button dclick event (but = -1)
271bool wxMouseEvent::ButtonDClick(int but) const
272{
0b746ba8
VZ
273 switch (but)
274 {
275 case -1:
276 return (LeftDClick() || MiddleDClick() || RightDClick());
277 case 1:
278 return LeftDClick();
279 case 2:
280 return MiddleDClick();
281 case 3:
282 return RightDClick();
283 default:
223d09f6 284 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
0b746ba8
VZ
285 }
286
287 return FALSE;
c801d85f
KB
288}
289
290// True if was a button down event (1 = left, 2 = middle, 3 = right)
291// or any button down event (but = -1)
292bool wxMouseEvent::ButtonDown(int but) const
293{
0b746ba8
VZ
294 switch (but)
295 {
296 case -1:
297 return (LeftDown() || MiddleDown() || RightDown());
298 case 1:
299 return LeftDown();
300 case 2:
301 return MiddleDown();
302 case 3:
303 return RightDown();
304 default:
223d09f6 305 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
0b746ba8
VZ
306 }
307
308 return FALSE;
c801d85f
KB
309}
310
311// True if was a button up event (1 = left, 2 = middle, 3 = right)
312// or any button up event (but = -1)
313bool wxMouseEvent::ButtonUp(int but) const
314{
0b746ba8
VZ
315 switch (but) {
316 case -1:
317 return (LeftUp() || MiddleUp() || RightUp());
318 case 1:
319 return LeftUp();
320 case 2:
321 return MiddleUp();
322 case 3:
323 return RightUp();
324 default:
223d09f6 325 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
0b746ba8
VZ
326 }
327
328 return FALSE;
c801d85f
KB
329}
330
331// True if the given button is currently changing state
332bool wxMouseEvent::Button(int but) const
333{
0b746ba8
VZ
334 switch (but) {
335 case -1:
72cdf4c9 336 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1));
0b746ba8
VZ
337 case 1:
338 return (LeftDown() || LeftUp() || LeftDClick());
339 case 2:
340 return (MiddleDown() || MiddleUp() || MiddleDClick());
341 case 3:
342 return (RightDown() || RightUp() || RightDClick());
343 default:
223d09f6 344 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
0b746ba8
VZ
345 }
346
347 return FALSE;
c801d85f
KB
348}
349
350bool wxMouseEvent::ButtonIsDown(int but) const
351{
0b746ba8
VZ
352 switch (but) {
353 case -1:
354 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
355 case 1:
356 return LeftIsDown();
357 case 2:
358 return MiddleIsDown();
359 case 3:
360 return RightIsDown();
361 default:
223d09f6 362 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
0b746ba8
VZ
363 }
364
365 return FALSE;
c801d85f
KB
366}
367
368// Find the logical position of the event given the DC
369wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
370{
0757d27c
JS
371 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
372 return pt;
c801d85f
KB
373}
374
375
376/*
377 * Keyboard events
378 *
379 */
380
7798a18e 381wxKeyEvent::wxKeyEvent(wxEventType type)
c801d85f 382{
0b746ba8
VZ
383 m_eventType = type;
384 m_shiftDown = FALSE;
385 m_controlDown = FALSE;
386 m_metaDown = FALSE;
387 m_altDown = FALSE;
388 m_keyCode = 0;
b0e813a0 389 m_scanCode = 0;
c801d85f
KB
390}
391
aadbdf11
GL
392void wxKeyEvent::CopyObject(wxObject& obj_d) const
393{
394 wxKeyEvent *obj = (wxKeyEvent *)&obj_d;
395 wxEvent::CopyObject(obj_d);
396
924ef850
RR
397 obj->m_x = m_x;
398 obj->m_y = m_y;
399 obj->m_keyCode = m_keyCode;
400
aadbdf11
GL
401 obj->m_shiftDown = m_shiftDown;
402 obj->m_controlDown = m_controlDown;
403 obj->m_metaDown = m_metaDown;
404 obj->m_altDown = m_altDown;
405 obj->m_keyCode = m_keyCode;
406}
407
408
409/*
410 * Misc events
411 */
412
413void wxSizeEvent::CopyObject(wxObject& obj_d) const
414{
415 wxSizeEvent *obj = (wxSizeEvent *)&obj_d;
416 wxEvent::CopyObject(obj_d);
417
418 obj->m_size = m_size;
419}
420
421void wxMoveEvent::CopyObject(wxObject& obj_d) const
422{
423 wxMoveEvent *obj = (wxMoveEvent *)&obj_d;
424 wxEvent::CopyObject(obj_d);
425
426 obj->m_pos = m_pos;
427}
428
429void wxEraseEvent::CopyObject(wxObject& obj_d) const
430{
431 wxEraseEvent *obj = (wxEraseEvent *)&obj_d;
432 wxEvent::CopyObject(obj_d);
433
434 obj->m_dc = m_dc;
435}
436
437void wxActivateEvent::CopyObject(wxObject& obj_d) const
438{
439 wxActivateEvent *obj = (wxActivateEvent *)&obj_d;
440 wxEvent::CopyObject(obj_d);
441
442 obj->m_active = m_active;
443}
444
445void wxMenuEvent::CopyObject(wxObject& obj_d) const
446{
447 wxMenuEvent *obj = (wxMenuEvent *)&obj_d;
448 wxEvent::CopyObject(obj_d);
449
450 obj->m_menuId = m_menuId;
451}
452
453void wxCloseEvent::CopyObject(wxObject& obj_d) const
454{
455 wxCloseEvent *obj = (wxCloseEvent *)&obj_d;
456 wxEvent::CopyObject(obj_d);
457
458 obj->m_loggingOff = m_loggingOff;
459 obj->m_veto = m_veto;
460#if WXWIN_COMPATIBILITY
461 obj->m_force = m_force;
462#endif
463 obj->m_canVeto = m_canVeto;
464}
e90c1d2a 465
aadbdf11
GL
466void wxShowEvent::CopyObject(wxObject& obj_d) const
467{
468 wxShowEvent *obj = (wxShowEvent *)&obj_d;
469 wxEvent::CopyObject(obj_d);
470
471 obj->m_show = m_show;
472}
f305c661
GL
473
474void wxJoystickEvent::CopyObject(wxObject& obj_d) const
475{
476 wxJoystickEvent *obj = (wxJoystickEvent *)&obj_d;
477 wxEvent::CopyObject(obj_d);
478
479 obj->m_pos = m_pos;
480 obj->m_zPosition = m_zPosition;
481 obj->m_buttonChange = m_buttonChange;
482 obj->m_buttonState = m_buttonState;
483 obj->m_joyStick = m_joyStick;
e90c1d2a 484}
f305c661
GL
485
486void wxDropFilesEvent::CopyObject(wxObject& obj_d) const
487{
488 wxDropFilesEvent *obj = (wxDropFilesEvent *)&obj_d;
489 wxEvent::CopyObject(obj_d);
490
491 obj->m_noFiles = m_noFiles;
492 obj->m_pos = m_pos;
493 // TODO: Problem with obj->m_files. It should be deallocated by the
e90c1d2a 494 // destructor of the event.
f305c661
GL
495}
496
497void wxUpdateUIEvent::CopyObject(wxObject &obj_d) const
498{
499 wxUpdateUIEvent *obj = (wxUpdateUIEvent *)&obj_d;
500 wxEvent::CopyObject(obj_d);
501
502 obj->m_checked = m_checked;
503 obj->m_enabled = m_enabled;
504 obj->m_text = m_text;
505 obj->m_setText = m_setText;
506 obj->m_setChecked = m_setChecked;
507 obj->m_setEnabled = m_setEnabled;
508}
509
510void wxPaletteChangedEvent::CopyObject(wxObject &obj_d) const
511{
512 wxPaletteChangedEvent *obj = (wxPaletteChangedEvent *)&obj_d;
513 wxEvent::CopyObject(obj_d);
514
515 obj->m_changedWindow = m_changedWindow;
516}
517
518void wxQueryNewPaletteEvent::CopyObject(wxObject& obj_d) const
519{
520 wxQueryNewPaletteEvent *obj = (wxQueryNewPaletteEvent *)&obj_d;
521 wxEvent::CopyObject(obj_d);
522
523 obj->m_paletteRealized = m_paletteRealized;
524}
aadbdf11 525
42e69d6b 526wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
cfe17b74 527 : wxEvent()
42e69d6b 528{
cfe17b74 529 SetEventType(wxEVT_CREATE);
42e69d6b
VZ
530 SetEventObject(win);
531}
532
533wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
cfe17b74 534 : wxEvent()
42e69d6b 535{
cfe17b74 536 SetEventType(wxEVT_DESTROY);
42e69d6b
VZ
537 SetEventObject(win);
538}
539
e90c1d2a
VZ
540#endif // wxUSE_GUI
541
542void wxIdleEvent::CopyObject(wxObject& obj_d) const
543{
544 wxIdleEvent *obj = (wxIdleEvent *)&obj_d;
545 wxEvent::CopyObject(obj_d);
546
547 obj->m_requestMore = m_requestMore;
548}
549
c801d85f
KB
550/*
551 * Event handler
552 */
553
0b746ba8 554wxEvtHandler::wxEvtHandler()
c801d85f 555{
0b746ba8
VZ
556 m_nextHandler = (wxEvtHandler *) NULL;
557 m_previousHandler = (wxEvtHandler *) NULL;
558 m_enabled = TRUE;
559 m_dynamicEvents = (wxList *) NULL;
193bf013 560 m_isWindow = FALSE;
8e193f38 561 m_pendingEvents = (wxList *) NULL;
7214297d 562#if wxUSE_THREADS
41404da7 563# if !defined(__VISAGECPP__)
8e193f38 564 m_eventsLocker = new wxCriticalSection;
41404da7 565# endif
7214297d 566#endif
c801d85f
KB
567}
568
0b746ba8 569wxEvtHandler::~wxEvtHandler()
c801d85f 570{
0b746ba8
VZ
571 // Takes itself out of the list of handlers
572 if (m_previousHandler)
573 m_previousHandler->m_nextHandler = m_nextHandler;
574
575 if (m_nextHandler)
576 m_nextHandler->m_previousHandler = m_previousHandler;
577
578 if (m_dynamicEvents)
fe71f65c 579 {
0b746ba8
VZ
580 wxNode *node = m_dynamicEvents->First();
581 while (node)
582 {
583 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
584 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
585 delete entry;
586 node = node->Next();
587 }
588 delete m_dynamicEvents;
589 };
7214297d 590
8e193f38 591 delete m_pendingEvents;
7214297d 592
8e193f38 593#if wxUSE_THREADS
41404da7 594# if !defined(__VISAGECPP__)
7214297d 595 delete m_eventsLocker;
41404da7 596# endif
7214297d 597#endif
c801d85f
KB
598}
599
7214297d 600#if wxUSE_THREADS
aadbdf11 601
7214297d
GL
602bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
603{
7214297d 604 // check that we are really in a child thread
8e193f38
VZ
605 wxASSERT_MSG( !wxThread::IsMain(),
606 wxT("use ProcessEvent() in main thread") );
607
608 AddPendingEvent(event);
7214297d 609
8e193f38
VZ
610 return TRUE;
611}
612
613#endif // wxUSE_THREADS
614
615void wxEvtHandler::AddPendingEvent(wxEvent& event)
616{
16c1f79c
RR
617 // 1) Add event to list of pending events of this event handler
618
619#if defined(__VISAGECPP__)
620 wxENTER_CRIT_SECT( m_eventsLocker);
621#else
622 wxENTER_CRIT_SECT( *m_eventsLocker);
623#endif
624
8e193f38
VZ
625 if ( !m_pendingEvents )
626 m_pendingEvents = new wxList;
7214297d 627
8e193f38 628 wxEvent *event2 = (wxEvent *)event.Clone();
7214297d 629
8e193f38 630 m_pendingEvents->Append(event2);
7214297d 631
16c1f79c
RR
632#if defined(__VISAGECPP__)
633 wxLEAVE_CRIT_SECT( m_eventsLocker);
634#else
635 wxLEAVE_CRIT_SECT( *m_eventsLocker);
636#endif
637
638 // 2) Add this event handler to list of event handlers that
639 // have pending events.
cfe17b74 640
b568d04f 641 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
72cdf4c9 642
8e193f38
VZ
643 if ( !wxPendingEvents )
644 wxPendingEvents = new wxList;
4d3a259a 645 wxPendingEvents->Append(this);
72cdf4c9 646
ce6d2511
RR
647 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
648
16c1f79c
RR
649 // 3) Inform the system that new pending events are somwehere,
650 // and that these should be processed in idle time.
bf9e3e73 651 wxWakeUpIdle();
7214297d
GL
652}
653
654void wxEvtHandler::ProcessPendingEvents()
655{
41404da7 656#if defined(__VISAGECPP__)
16c1f79c 657 wxENTER_CRIT_SECT( m_eventsLocker);
b568d04f 658#else
16c1f79c 659 wxENTER_CRIT_SECT( *m_eventsLocker);
41404da7 660#endif
8e193f38 661
7214297d 662 wxNode *node = m_pendingEvents->First();
8e193f38
VZ
663 while ( node )
664 {
16c1f79c
RR
665 wxEvent *event = (wxEvent *)node->Data();
666 delete node;
cfe17b74 667
16c1f79c 668 // In ProcessEvent, new events might get added and
ce6d2511 669 // we can safely leave the crtical section here.
16c1f79c
RR
670#if defined(__VISAGECPP__)
671 wxLEAVE_CRIT_SECT( m_eventsLocker);
672#else
673 wxLEAVE_CRIT_SECT( *m_eventsLocker);
674#endif
8e193f38 675 ProcessEvent(*event);
9779893b 676 delete event;
16c1f79c
RR
677#if defined(__VISAGECPP__)
678 wxENTER_CRIT_SECT( m_eventsLocker);
679#else
680 wxENTER_CRIT_SECT( *m_eventsLocker);
681#endif
cfe17b74 682
8e193f38 683 node = m_pendingEvents->First();
7214297d 684 }
cfe17b74 685
16c1f79c
RR
686#if defined(__VISAGECPP__)
687 wxLEAVE_CRIT_SECT( m_eventsLocker);
688#else
689 wxLEAVE_CRIT_SECT( *m_eventsLocker);
690#endif
7214297d 691}
7214297d 692
c801d85f
KB
693/*
694 * Event table stuff
695 */
696
697bool wxEvtHandler::ProcessEvent(wxEvent& event)
698{
e90c1d2a 699#if wxUSE_GUI
8e193f38 700 // check that our flag corresponds to reality
193bf013 701 wxASSERT( m_isWindow == IsKindOf(CLASSINFO(wxWindow)) );
e90c1d2a 702#endif // wxUSE_GUI
0b746ba8 703
8e193f38 704 // An event handler can be enabled or disabled
0b746ba8 705 if ( GetEvtHandlerEnabled() )
c801d85f 706 {
924ef850
RR
707
708#if 0
709/*
710 What is this? When using GUI threads, a non main
711 threads can send an event and process it itself.
712 This breaks GTK's GUI threads, so please explain.
713*/
714
8e193f38
VZ
715 // Check whether we are in a child thread.
716 if ( !wxThread::IsMain() )
7214297d 717 return ProcessThreadEvent(event);
924ef850 718#endif
0757d27c 719
8e193f38 720 // Handle per-instance dynamic event tables first
193bf013 721 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
0757d27c 722 return TRUE;
0b746ba8 723
8e193f38 724 // Then static per-class event tables
0b746ba8
VZ
725 const wxEventTable *table = GetEventTable();
726
e90c1d2a 727#if wxUSE_GUI && wxUSE_VALIDATORS
0b746ba8
VZ
728 // Try the associated validator first, if this is a window.
729 // Problem: if the event handler of the window has been replaced,
730 // this wxEvtHandler may no longer be a window.
731 // Therefore validators won't be processed if the handler
732 // has been replaced with SetEventHandler.
733 // THIS CAN BE CURED if PushEventHandler is used instead of
734 // SetEventHandler, and then processing will be passed down the
735 // chain of event handlers.
ce4169a4 736 if (m_isWindow)
0b746ba8
VZ
737 {
738 wxWindow *win = (wxWindow *)this;
739
740 // Can only use the validator of the window which
741 // is receiving the event
193bf013 742 if ( win == event.GetEventObject() )
0b746ba8 743 {
193bf013
VZ
744 wxValidator *validator = win->GetValidator();
745 if ( validator && validator->ProcessEvent(event) )
746 {
747 return TRUE;
748 }
0b746ba8
VZ
749 }
750 }
ce4169a4 751#endif
0b746ba8
VZ
752
753 // Search upwards through the inheritance hierarchy
ce4169a4 754 while (table)
0b746ba8 755 {
193bf013 756 if ( SearchEventTable((wxEventTable&)*table, event) )
0b746ba8
VZ
757 return TRUE;
758 table = table->baseTable;
759 }
c801d85f
KB
760 }
761
0b746ba8
VZ
762 // Try going down the event handler chain
763 if ( GetNextHandler() )
c801d85f 764 {
0b746ba8
VZ
765 if ( GetNextHandler()->ProcessEvent(event) )
766 return TRUE;
c801d85f 767 }
c801d85f 768
e90c1d2a 769#if wxUSE_GUI
0b746ba8
VZ
770 // Carry on up the parent-child hierarchy,
771 // but only if event is a command event: it wouldn't
772 // make sense for a parent to receive a child's size event, for example
193bf013 773 if ( m_isWindow && event.IsCommandEvent() )
0b746ba8
VZ
774 {
775 wxWindow *win = (wxWindow *)this;
776 wxWindow *parent = win->GetParent();
777 if (parent && !parent->IsBeingDeleted())
193bf013 778 return parent->GetEventHandler()->ProcessEvent(event);
0b746ba8 779 }
e90c1d2a 780#endif // wxUSE_GUI
0b746ba8
VZ
781
782 // Last try - application object.
193bf013 783 if ( wxTheApp && (this != wxTheApp) )
0b746ba8 784 {
193bf013
VZ
785 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
786 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
787 // processed appropriately via SearchEventTable.
788 if ( event.GetEventType() != wxEVT_IDLE )
789 {
790 if ( wxTheApp->ProcessEvent(event) )
791 return TRUE;
792 }
0b746ba8
VZ
793 }
794
c801d85f
KB
795 return FALSE;
796}
797
798bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
799{
0b746ba8
VZ
800 int i = 0;
801 int commandId = event.GetId();
802
803 // BC++ doesn't like while (table.entries[i].m_fn)
804
2432b92d
JS
805#ifdef __SC__
806 while (table.entries[i].m_fn != 0)
807#else
0b746ba8 808 while (table.entries[i].m_fn != 0L)
2432b92d 809#endif
c801d85f 810 {
0b746ba8
VZ
811 if ((event.GetEventType() == table.entries[i].m_eventType) &&
812 (table.entries[i].m_id == -1 || // Match, if event spec says any id will do (id == -1)
813 (table.entries[i].m_lastId == -1 && commandId == table.entries[i].m_id) ||
814 (table.entries[i].m_lastId != -1 &&
815 (commandId >= table.entries[i].m_id && commandId <= table.entries[i].m_lastId))))
816 {
817 event.Skip(FALSE);
818 event.m_callbackUserData = table.entries[i].m_callbackUserData;
819
820 (this->*((wxEventFunction) (table.entries[i].m_fn)))(event);
821
822 if ( event.GetSkipped() )
823 return FALSE;
824 else
825 return TRUE;
826 }
827 i++;
c801d85f 828 }
0b746ba8 829 return FALSE;
c801d85f 830}
97d7bfb8 831
debe6624 832void wxEvtHandler::Connect( int id, int lastId,
d4a23fee 833 wxEventType eventType,
0b746ba8
VZ
834 wxObjectEventFunction func,
835 wxObject *userData )
fe71f65c 836{
0b746ba8
VZ
837 wxEventTableEntry *entry = new wxEventTableEntry;
838 entry->m_id = id;
839 entry->m_lastId = lastId;
840 entry->m_eventType = eventType;
841 entry->m_fn = func;
842 entry->m_callbackUserData = userData;
843
844 if (!m_dynamicEvents)
845 m_dynamicEvents = new wxList;
846
847 m_dynamicEvents->Append( (wxObject*) entry );
fe71f65c 848}
97d7bfb8
RR
849
850bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
851 wxObjectEventFunction func,
852 wxObject *userData )
853{
854 if (!m_dynamicEvents)
855 return FALSE;
cfe17b74 856
97d7bfb8
RR
857 wxNode *node = m_dynamicEvents->First();
858 while (node)
859 {
860 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
861 if ((entry->m_id == id) &&
862 ((entry->m_lastId == lastId) || (lastId == -1)) &&
863 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
864 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
865 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
866 {
867 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
868 m_dynamicEvents->DeleteNode( node );
869 delete entry;
870 return TRUE;
871 }
872 node = node->Next();
873 }
874 return FALSE;
875}
fe71f65c
RR
876
877bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
878{
193bf013 879 wxCHECK_MSG( m_dynamicEvents, FALSE,
223d09f6 880 wxT("caller should check that we have dynamic events") );
0b746ba8
VZ
881
882 int commandId = event.GetId();
883
884 wxNode *node = m_dynamicEvents->First();
885 while (node)
fe71f65c 886 {
0b746ba8
VZ
887 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
888
889 if (entry->m_fn)
890 {
891 // Match, if event spec says any id will do (id == -1)
892 if ( (event.GetEventType() == entry->m_eventType) &&
893 (entry->m_id == -1 ||
894 (entry->m_lastId == -1 && commandId == entry->m_id) ||
895 (entry->m_lastId != -1 &&
896 (commandId >= entry->m_id && commandId <= entry->m_lastId))) )
897 {
898 event.Skip(FALSE);
899 event.m_callbackUserData = entry->m_callbackUserData;
900
901 (this->*((wxEventFunction) (entry->m_fn)))(event);
902
903 if (event.GetSkipped())
904 return FALSE;
905 else
906 return TRUE;
907 }
908 }
909 node = node->Next();
7b678698 910 }
0b746ba8 911 return FALSE;
fe71f65c
RR
912};
913
e3065973 914#if WXWIN_COMPATIBILITY
0b746ba8 915bool wxEvtHandler::OnClose()
c801d85f 916{
0b746ba8
VZ
917 if (GetNextHandler())
918 return GetNextHandler()->OnClose();
919 else
920 return FALSE;
c801d85f 921}
193bf013 922#endif // WXWIN_COMPATIBILITY
e3065973 923
e90c1d2a
VZ
924#if wxUSE_GUI
925
e702ff0f
JS
926// Find a window with the focus, that is also a descendant of the given window.
927// This is used to determine the window to initially send commands to.
928wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
929{
930 // Process events starting with the window with the focus, if any.
931 wxWindow* focusWin = wxWindow::FindFocus();
932 wxWindow* win = focusWin;
933
934 // Check if this is a descendant of this frame.
935 // If not, win will be set to NULL.
936 while (win)
937 {
938 if (win == ancestor)
939 break;
940 else
941 win = win->GetParent();
942 }
943 if (win == (wxWindow*) NULL)
944 focusWin = (wxWindow*) NULL;
945
946 return focusWin;
947}
948
e90c1d2a 949#endif // wxUSE_GUI