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