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