]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/event.cpp
fixed wxTaskBarIcon compilation
[wxWidgets.git] / src / common / event.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: event.cpp
3// Purpose: Event classes
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
21 #pragma implementation "event.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
31#ifndef WX_PRECOMP
32 #include "wx/defs.h"
33 #include "wx/app.h"
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
41#endif
42
43#include "wx/event.h"
44
45#if wxUSE_GUI
46 #include "wx/validate.h"
47#endif // wxUSE_GUI
48
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
53IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
54IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
55
56#if wxUSE_GUI
57 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
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(wxNcPaintEvent, wxEvent)
67 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
69 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
70 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
78 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
88 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
89 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
90#endif // wxUSE_GUI
91
92const wxEventTable *wxEvtHandler::GetEventTable() const
93 { return &wxEvtHandler::sm_eventTable; }
94
95const wxEventTable wxEvtHandler::sm_eventTable =
96 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
97
98const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
99 { DECLARE_EVENT_TABLE_ENTRY(wxEVT_NULL, 0, 0, (wxObjectEventFunction)NULL, NULL) };
100
101// ----------------------------------------------------------------------------
102// global variables
103// ----------------------------------------------------------------------------
104
105// To put pending event handlers
106wxList *wxPendingEvents = (wxList *)NULL;
107
108#if wxUSE_THREADS
109 // protects wxPendingEvents list
110 wxCriticalSection *wxPendingEventsLocker = (wxCriticalSection *)NULL;
111#endif
112
113#if !WXWIN_COMPATIBILITY_EVENT_TYPES
114
115// common event types are defined here, other event types are defined by the
116// components which use them
117
118DEFINE_EVENT_TYPE(wxEVT_NULL)
119DEFINE_EVENT_TYPE(wxEVT_COMMAND_BUTTON_CLICKED)
120DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKBOX_CLICKED)
121DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHOICE_SELECTED)
122DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_SELECTED)
123DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED)
124DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED)
125DEFINE_EVENT_TYPE(wxEVT_COMMAND_MENU_SELECTED)
126DEFINE_EVENT_TYPE(wxEVT_COMMAND_SLIDER_UPDATED)
127DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBOX_SELECTED)
128DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBUTTON_SELECTED)
129DEFINE_EVENT_TYPE(wxEVT_COMMAND_SCROLLBAR_UPDATED)
130DEFINE_EVENT_TYPE(wxEVT_COMMAND_VLBOX_SELECTED)
131DEFINE_EVENT_TYPE(wxEVT_COMMAND_COMBOBOX_SELECTED)
132DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_RCLICKED)
133DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_ENTER)
134DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRL_UPDATED)
135
136// Sockets and timers send events, too
137DEFINE_EVENT_TYPE(wxEVT_SOCKET)
138DEFINE_EVENT_TYPE(wxEVT_TIMER)
139
140// Mouse event types
141DEFINE_EVENT_TYPE(wxEVT_LEFT_DOWN)
142DEFINE_EVENT_TYPE(wxEVT_LEFT_UP)
143DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DOWN)
144DEFINE_EVENT_TYPE(wxEVT_MIDDLE_UP)
145DEFINE_EVENT_TYPE(wxEVT_RIGHT_DOWN)
146DEFINE_EVENT_TYPE(wxEVT_RIGHT_UP)
147DEFINE_EVENT_TYPE(wxEVT_MOTION)
148DEFINE_EVENT_TYPE(wxEVT_ENTER_WINDOW)
149DEFINE_EVENT_TYPE(wxEVT_LEAVE_WINDOW)
150DEFINE_EVENT_TYPE(wxEVT_LEFT_DCLICK)
151DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DCLICK)
152DEFINE_EVENT_TYPE(wxEVT_RIGHT_DCLICK)
153DEFINE_EVENT_TYPE(wxEVT_SET_FOCUS)
154DEFINE_EVENT_TYPE(wxEVT_KILL_FOCUS)
155DEFINE_EVENT_TYPE(wxEVT_CHILD_FOCUS)
156DEFINE_EVENT_TYPE(wxEVT_MOUSEWHEEL)
157
158// Non-client mouse events
159DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DOWN)
160DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_UP)
161DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DOWN)
162DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_UP)
163DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DOWN)
164DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_UP)
165DEFINE_EVENT_TYPE(wxEVT_NC_MOTION)
166DEFINE_EVENT_TYPE(wxEVT_NC_ENTER_WINDOW)
167DEFINE_EVENT_TYPE(wxEVT_NC_LEAVE_WINDOW)
168DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DCLICK)
169DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DCLICK)
170DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DCLICK)
171
172// Character input event type
173DEFINE_EVENT_TYPE(wxEVT_CHAR)
174DEFINE_EVENT_TYPE(wxEVT_CHAR_HOOK)
175DEFINE_EVENT_TYPE(wxEVT_NAVIGATION_KEY)
176DEFINE_EVENT_TYPE(wxEVT_KEY_DOWN)
177DEFINE_EVENT_TYPE(wxEVT_KEY_UP)
178
179// Set cursor event
180DEFINE_EVENT_TYPE(wxEVT_SET_CURSOR)
181
182// wxScrollbar and wxSlider event identifiers
183DEFINE_EVENT_TYPE(wxEVT_SCROLL_TOP)
184DEFINE_EVENT_TYPE(wxEVT_SCROLL_BOTTOM)
185DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEUP)
186DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEDOWN)
187DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEUP)
188DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEDOWN)
189DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBTRACK)
190DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBRELEASE)
191
192// Scroll events from wxWindow
193DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_TOP)
194DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_BOTTOM)
195DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEUP)
196DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEDOWN)
197DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEUP)
198DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEDOWN)
199DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBTRACK)
200DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBRELEASE)
201
202// System events
203DEFINE_EVENT_TYPE(wxEVT_SIZE)
204DEFINE_EVENT_TYPE(wxEVT_MOVE)
205DEFINE_EVENT_TYPE(wxEVT_CLOSE_WINDOW)
206DEFINE_EVENT_TYPE(wxEVT_END_SESSION)
207DEFINE_EVENT_TYPE(wxEVT_QUERY_END_SESSION)
208DEFINE_EVENT_TYPE(wxEVT_ACTIVATE_APP)
209DEFINE_EVENT_TYPE(wxEVT_POWER)
210DEFINE_EVENT_TYPE(wxEVT_ACTIVATE)
211DEFINE_EVENT_TYPE(wxEVT_CREATE)
212DEFINE_EVENT_TYPE(wxEVT_DESTROY)
213DEFINE_EVENT_TYPE(wxEVT_SHOW)
214DEFINE_EVENT_TYPE(wxEVT_ICONIZE)
215DEFINE_EVENT_TYPE(wxEVT_MAXIMIZE)
216DEFINE_EVENT_TYPE(wxEVT_MOUSE_CAPTURE_CHANGED)
217DEFINE_EVENT_TYPE(wxEVT_PAINT)
218DEFINE_EVENT_TYPE(wxEVT_ERASE_BACKGROUND)
219DEFINE_EVENT_TYPE(wxEVT_NC_PAINT)
220DEFINE_EVENT_TYPE(wxEVT_PAINT_ICON)
221DEFINE_EVENT_TYPE(wxEVT_MENU_CHAR)
222DEFINE_EVENT_TYPE(wxEVT_MENU_INIT)
223DEFINE_EVENT_TYPE(wxEVT_MENU_HIGHLIGHT)
224DEFINE_EVENT_TYPE(wxEVT_POPUP_MENU_INIT)
225DEFINE_EVENT_TYPE(wxEVT_CONTEXT_MENU)
226DEFINE_EVENT_TYPE(wxEVT_SYS_COLOUR_CHANGED)
227DEFINE_EVENT_TYPE(wxEVT_SETTING_CHANGED)
228DEFINE_EVENT_TYPE(wxEVT_QUERY_NEW_PALETTE)
229DEFINE_EVENT_TYPE(wxEVT_PALETTE_CHANGED)
230DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_DOWN)
231DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_UP)
232DEFINE_EVENT_TYPE(wxEVT_JOY_MOVE)
233DEFINE_EVENT_TYPE(wxEVT_JOY_ZMOVE)
234DEFINE_EVENT_TYPE(wxEVT_DROP_FILES)
235DEFINE_EVENT_TYPE(wxEVT_DRAW_ITEM)
236DEFINE_EVENT_TYPE(wxEVT_MEASURE_ITEM)
237DEFINE_EVENT_TYPE(wxEVT_COMPARE_ITEM)
238DEFINE_EVENT_TYPE(wxEVT_INIT_DIALOG)
239DEFINE_EVENT_TYPE(wxEVT_IDLE)
240DEFINE_EVENT_TYPE(wxEVT_UPDATE_UI)
241
242// Generic command events
243// Note: a click is a higher-level event than button down/up
244DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_CLICK)
245DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_DCLICK)
246DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_CLICK)
247DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_DCLICK)
248DEFINE_EVENT_TYPE(wxEVT_COMMAND_SET_FOCUS)
249DEFINE_EVENT_TYPE(wxEVT_COMMAND_KILL_FOCUS)
250DEFINE_EVENT_TYPE(wxEVT_COMMAND_ENTER)
251
252// Help events
253DEFINE_EVENT_TYPE(wxEVT_HELP)
254DEFINE_EVENT_TYPE(wxEVT_DETAILED_HELP)
255
256#endif // !WXWIN_COMPATIBILITY_EVENT_TYPES
257
258// ============================================================================
259// implementation
260// ============================================================================
261
262// ----------------------------------------------------------------------------
263// event initialization
264// ----------------------------------------------------------------------------
265
266int wxNewEventType()
267{
268 // MT-FIXME
269 static int s_lastUsedEventType = wxEVT_FIRST;
270
271#if WXWIN_COMPATIBILITY_2
272 // check that we don't overlap with the user-defined types: if it does
273 // happen, the best solution is probably to update the existing code to
274 // use wxNewEventType() instead of wxEVT_USER_FIRST
275 //
276 // due to the uncertainty
277 wxASSERT_MSG( s_lastUsedEventType < wxEVT_USER_FIRST - 1,
278 _T("possible event type conflict") );
279#endif // WXWIN_COMPATIBILITY_2
280
281 return s_lastUsedEventType++;
282}
283
284// ----------------------------------------------------------------------------
285// wxEvent
286// ----------------------------------------------------------------------------
287
288/*
289 * General wxWindows events, covering
290 * all interesting things that might happen (button clicking, resizing,
291 * setting text in widgets, etc.).
292 *
293 * For each completely new event type, derive a new event class.
294 *
295 */
296
297wxEvent::wxEvent(int theId, wxEventType commandType )
298{
299 m_eventType = commandType;
300 m_eventObject = (wxObject *) NULL;
301 m_timeStamp = 0;
302 m_id = theId;
303 m_skipped = FALSE;
304 m_callbackUserData = (wxObject *) NULL;
305 m_isCommandEvent = FALSE;
306}
307
308wxEvent::wxEvent(const wxEvent &src)
309{
310 m_eventType = src.m_eventType;
311 m_eventObject = src.m_eventObject;
312 m_timeStamp = src.m_timeStamp;
313 m_id = src.m_id;
314 m_skipped = src.m_skipped;
315 m_callbackUserData = src.m_callbackUserData;
316 m_isCommandEvent = src.m_isCommandEvent;
317}
318
319#if wxUSE_GUI
320
321/*
322 * Command events
323 *
324 */
325
326wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
327 : wxEvent( theId, commandType )
328{
329 m_clientData = (char *) NULL;
330 m_clientObject = (wxClientData *) NULL;
331 m_extraLong = 0;
332 m_commandInt = 0;
333 m_commandString = wxEmptyString;
334 m_isCommandEvent = TRUE;
335}
336
337/*
338 * Scroll events
339 */
340
341wxScrollEvent::wxScrollEvent(wxEventType commandType,
342 int id,
343 int pos,
344 int orient)
345 : wxCommandEvent(commandType, id)
346{
347 m_extraLong = orient;
348 m_commandInt = pos;
349}
350
351/*
352 * ScrollWin events
353 */
354
355wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
356 int pos,
357 int orient)
358{
359 m_eventType = commandType;
360 m_extraLong = orient;
361 m_commandInt = pos;
362}
363
364/*
365 * Mouse events
366 *
367 */
368
369wxMouseEvent::wxMouseEvent(wxEventType commandType)
370{
371 m_eventType = commandType;
372 m_metaDown = FALSE;
373 m_altDown = FALSE;
374 m_controlDown = FALSE;
375 m_shiftDown = FALSE;
376 m_leftDown = FALSE;
377 m_rightDown = FALSE;
378 m_middleDown = FALSE;
379 m_x = 0;
380 m_y = 0;
381 m_wheelRotation = 0;
382 m_wheelDelta = 0;
383 m_linesPerAction = 0;
384}
385
386// True if was a button dclick event (1 = left, 2 = middle, 3 = right)
387// or any button dclick event (but = -1)
388bool wxMouseEvent::ButtonDClick(int but) const
389{
390 switch (but)
391 {
392 case -1:
393 return (LeftDClick() || MiddleDClick() || RightDClick());
394 case 1:
395 return LeftDClick();
396 case 2:
397 return MiddleDClick();
398 case 3:
399 return RightDClick();
400 default:
401 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
402 }
403
404 return FALSE;
405}
406
407// True if was a button down event (1 = left, 2 = middle, 3 = right)
408// or any button down event (but = -1)
409bool wxMouseEvent::ButtonDown(int but) const
410{
411 switch (but)
412 {
413 case -1:
414 return (LeftDown() || MiddleDown() || RightDown());
415 case 1:
416 return LeftDown();
417 case 2:
418 return MiddleDown();
419 case 3:
420 return RightDown();
421 default:
422 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
423 }
424
425 return FALSE;
426}
427
428// True if was a button up event (1 = left, 2 = middle, 3 = right)
429// or any button up event (but = -1)
430bool wxMouseEvent::ButtonUp(int but) const
431{
432 switch (but)
433 {
434 case -1:
435 return (LeftUp() || MiddleUp() || RightUp());
436 case 1:
437 return LeftUp();
438 case 2:
439 return MiddleUp();
440 case 3:
441 return RightUp();
442 default:
443 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
444 }
445
446 return FALSE;
447}
448
449// True if the given button is currently changing state
450bool wxMouseEvent::Button(int but) const
451{
452 switch (but)
453 {
454 case -1:
455 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1));
456 case 1:
457 return (LeftDown() || LeftUp() || LeftDClick());
458 case 2:
459 return (MiddleDown() || MiddleUp() || MiddleDClick());
460 case 3:
461 return (RightDown() || RightUp() || RightDClick());
462 default:
463 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
464 }
465
466 return FALSE;
467}
468
469bool wxMouseEvent::ButtonIsDown(int but) const
470{
471 switch (but)
472 {
473 case -1:
474 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
475 case 1:
476 return LeftIsDown();
477 case 2:
478 return MiddleIsDown();
479 case 3:
480 return RightIsDown();
481 default:
482 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
483 }
484
485 return FALSE;
486}
487
488int wxMouseEvent::GetButton() const
489{
490 for ( int i = 1; i <= 3; i++ )
491 {
492 if ( Button(i) )
493 {
494 return i;
495 }
496 }
497
498 return -1;
499}
500
501// Find the logical position of the event given the DC
502wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
503{
504 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
505 return pt;
506}
507
508
509/*
510 * Keyboard event
511 *
512 */
513
514wxKeyEvent::wxKeyEvent(wxEventType type)
515{
516 m_eventType = type;
517 m_shiftDown = FALSE;
518 m_controlDown = FALSE;
519 m_metaDown = FALSE;
520 m_altDown = FALSE;
521 m_keyCode = 0;
522 m_scanCode = 0;
523}
524
525
526wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
527{
528 SetEventType(wxEVT_CREATE);
529 SetEventObject(win);
530}
531
532wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
533{
534 SetEventType(wxEVT_DESTROY);
535 SetEventObject(win);
536}
537
538wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
539 : wxCommandEvent(wxEVT_CHILD_FOCUS)
540{
541 SetEventObject(win);
542}
543
544#endif // wxUSE_GUI
545
546// ----------------------------------------------------------------------------
547// wxEvtHandler
548// ----------------------------------------------------------------------------
549
550/*
551 * Event handler
552 */
553
554wxEvtHandler::wxEvtHandler()
555{
556 m_nextHandler = (wxEvtHandler *) NULL;
557 m_previousHandler = (wxEvtHandler *) NULL;
558 m_enabled = TRUE;
559 m_dynamicEvents = (wxList *) NULL;
560 m_isWindow = FALSE;
561 m_pendingEvents = (wxList *) NULL;
562#if wxUSE_THREADS
563# if !defined(__VISAGECPP__)
564 m_eventsLocker = new wxCriticalSection;
565# endif
566#endif
567 // no client data (yet)
568 m_clientData = NULL;
569 m_clientDataType = wxClientData_None;
570}
571
572wxEvtHandler::~wxEvtHandler()
573{
574 // Takes itself out of the list of handlers
575 if (m_previousHandler)
576 m_previousHandler->m_nextHandler = m_nextHandler;
577
578 if (m_nextHandler)
579 m_nextHandler->m_previousHandler = m_previousHandler;
580
581 if (m_dynamicEvents)
582 {
583 wxNode *node = m_dynamicEvents->First();
584 while (node)
585 {
586#if WXWIN_COMPATIBILITY_EVENT_TYPES
587 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
588#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
589 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
590#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
591
592 if (entry->m_callbackUserData)
593 delete entry->m_callbackUserData;
594 delete entry;
595 node = node->Next();
596 }
597 delete m_dynamicEvents;
598 };
599
600 delete m_pendingEvents;
601
602#if wxUSE_THREADS
603# if !defined(__VISAGECPP__)
604 delete m_eventsLocker;
605# endif
606#endif
607
608 // we only delete object data, not untyped
609 if ( m_clientDataType == wxClientData_Object )
610 delete m_clientObject;
611}
612
613#if wxUSE_THREADS
614
615bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
616{
617 // check that we are really in a child thread
618 wxASSERT_MSG( !wxThread::IsMain(),
619 wxT("use ProcessEvent() in main thread") );
620
621 AddPendingEvent(event);
622
623 return TRUE;
624}
625
626#endif // wxUSE_THREADS
627
628void wxEvtHandler::AddPendingEvent(wxEvent& event)
629{
630 // 1) Add event to list of pending events of this event handler
631
632#if defined(__VISAGECPP__)
633 wxENTER_CRIT_SECT( m_eventsLocker);
634#else
635 wxENTER_CRIT_SECT( *m_eventsLocker);
636#endif
637
638 if ( !m_pendingEvents )
639 m_pendingEvents = new wxList;
640
641 wxEvent *event2 = event.Clone();
642
643 m_pendingEvents->Append(event2);
644
645#if defined(__VISAGECPP__)
646 wxLEAVE_CRIT_SECT( m_eventsLocker);
647#else
648 wxLEAVE_CRIT_SECT( *m_eventsLocker);
649#endif
650
651 // 2) Add this event handler to list of event handlers that
652 // have pending events.
653
654 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
655
656 if ( !wxPendingEvents )
657 wxPendingEvents = new wxList;
658 wxPendingEvents->Append(this);
659
660 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
661
662 // 3) Inform the system that new pending events are somwehere,
663 // and that these should be processed in idle time.
664 wxWakeUpIdle();
665}
666
667void wxEvtHandler::ProcessPendingEvents()
668{
669#if defined(__VISAGECPP__)
670 wxENTER_CRIT_SECT( m_eventsLocker);
671#else
672 wxENTER_CRIT_SECT( *m_eventsLocker);
673#endif
674
675 wxNode *node = m_pendingEvents->First();
676 while ( node )
677 {
678 wxEvent *event = (wxEvent *)node->Data();
679 delete node;
680
681 // In ProcessEvent, new events might get added and
682 // we can safely leave the crtical section here.
683#if defined(__VISAGECPP__)
684 wxLEAVE_CRIT_SECT( m_eventsLocker);
685#else
686 wxLEAVE_CRIT_SECT( *m_eventsLocker);
687#endif
688 ProcessEvent(*event);
689 delete event;
690#if defined(__VISAGECPP__)
691 wxENTER_CRIT_SECT( m_eventsLocker);
692#else
693 wxENTER_CRIT_SECT( *m_eventsLocker);
694#endif
695
696 node = m_pendingEvents->First();
697 }
698
699#if defined(__VISAGECPP__)
700 wxLEAVE_CRIT_SECT( m_eventsLocker);
701#else
702 wxLEAVE_CRIT_SECT( *m_eventsLocker);
703#endif
704}
705
706/*
707 * Event table stuff
708 */
709
710bool wxEvtHandler::ProcessEvent(wxEvent& event)
711{
712#if wxUSE_GUI
713
714 // We have to use the actual window or processing events from
715 // wxWindowNative destructor won't work (we don't see the wxWindow class)
716#ifdef __WXDEBUG__
717 // check that our flag corresponds to reality
718 wxClassInfo* info = NULL;
719#ifdef __WXUNIVERSAL__
720# if defined(__WXMSW__)
721 info = CLASSINFO(wxWindowMSW);
722# elif defined(__WXGTK__)
723 info = CLASSINFO(wxWindowGTK);
724# elif defined(__WXMGL__)
725 info = CLASSINFO(wxWindowMGL);
726# elif defined(__WXPM__)
727 info = CLASSINFO(wxWindowOS2);
728# elif defined(__WXMAC__)
729 info = CLASSINFO(wxWindowMac);
730# elif defined(__WXMOTIF__)
731 info = CLASSINFO(wxWindowMotif);
732# endif
733#else
734 info = CLASSINFO(wxWindow);
735#endif
736
737 wxASSERT_MSG( m_isWindow == IsKindOf(info),
738 wxString(GetClassInfo()->GetClassName()) + _T(" should [not] be a window but it is [not]") );
739#endif
740
741#endif // wxUSE_GUI
742
743 // An event handler can be enabled or disabled
744 if ( GetEvtHandlerEnabled() )
745 {
746
747#if 0
748/*
749 What is this? When using GUI threads, a non main
750 threads can send an event and process it itself.
751 This breaks GTK's GUI threads, so please explain.
752*/
753
754 // Check whether we are in a child thread.
755 if ( !wxThread::IsMain() )
756 return ProcessThreadEvent(event);
757#endif
758
759 // Handle per-instance dynamic event tables first
760 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
761 return TRUE;
762
763 // Then static per-class event tables
764 const wxEventTable *table = GetEventTable();
765
766#if wxUSE_GUI && wxUSE_VALIDATORS
767 // Try the associated validator first, if this is a window.
768 // Problem: if the event handler of the window has been replaced,
769 // this wxEvtHandler may no longer be a window.
770 // Therefore validators won't be processed if the handler
771 // has been replaced with SetEventHandler.
772 // THIS CAN BE CURED if PushEventHandler is used instead of
773 // SetEventHandler, and then processing will be passed down the
774 // chain of event handlers.
775 if (m_isWindow)
776 {
777 wxWindow *win = (wxWindow *)this;
778
779 // Can only use the validator of the window which
780 // is receiving the event
781 if ( win == event.GetEventObject() )
782 {
783 wxValidator *validator = win->GetValidator();
784 if ( validator && validator->ProcessEvent(event) )
785 {
786 return TRUE;
787 }
788 }
789 }
790#endif
791
792 // Search upwards through the inheritance hierarchy
793 while (table)
794 {
795 if ( SearchEventTable((wxEventTable&)*table, event) )
796 return TRUE;
797 table = table->baseTable;
798 }
799 }
800
801 // Try going down the event handler chain
802 if ( GetNextHandler() )
803 {
804 if ( GetNextHandler()->ProcessEvent(event) )
805 return TRUE;
806 }
807
808#if wxUSE_GUI
809 // Carry on up the parent-child hierarchy, but only if event is a command
810 // event: it wouldn't make sense for a parent to receive a child's size
811 // event, for example
812 if ( m_isWindow && event.IsCommandEvent() )
813 {
814 wxWindow *win = (wxWindow *)this;
815
816 // honour the requests to stop propagation at this window: this is
817 // used by the dialogs, for example, to prevent processing the events
818 // from the dialog controls in the parent frame which rarely, if ever,
819 // makes sense
820 if ( !(win->GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
821 {
822 wxWindow *parent = win->GetParent();
823 if ( parent && !parent->IsBeingDeleted() )
824 return parent->GetEventHandler()->ProcessEvent(event);
825 }
826 }
827#endif // wxUSE_GUI
828
829 // Last try - application object.
830 if ( wxTheApp && (this != wxTheApp) )
831 {
832 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
833 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
834 // processed appropriately via SearchEventTable.
835 if ( event.GetEventType() != wxEVT_IDLE )
836 {
837 if ( wxTheApp->ProcessEvent(event) )
838 return TRUE;
839 }
840 }
841
842 return FALSE;
843}
844
845bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
846{
847 wxEventType eventType = event.GetEventType();
848 int eventId = event.GetId();
849
850 // BC++ doesn't like testing for m_fn without != 0
851 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
852 {
853 // the line using reference exposes a bug in gcc: although it _seems_
854 // to work, it leads to weird crashes later on during program
855 // execution
856#ifdef __GNUG__
857 wxEventTableEntry entry = table.entries[i];
858#else
859 const wxEventTableEntry& entry = table.entries[i];
860#endif
861
862 // match only if the event type is the same and the id is either -1 in
863 // the event table (meaning "any") or the event id matches the id
864 // specified in the event table either exactly or by falling into
865 // range between first and last
866 if ( eventType == entry.m_eventType )
867 {
868 int tableId1 = entry.m_id,
869 tableId2 = entry.m_lastId;
870
871 if ( (tableId1 == -1) ||
872 (tableId2 == -1 && eventId == tableId1) ||
873 (tableId2 != -1 &&
874 (eventId >= tableId1 && eventId <= tableId2)) )
875 {
876 event.Skip(FALSE);
877 event.m_callbackUserData = entry.m_callbackUserData;
878
879 (this->*((wxEventFunction) (entry.m_fn)))(event);
880
881 return !event.GetSkipped();
882 }
883 }
884 }
885
886 return FALSE;
887}
888
889void wxEvtHandler::Connect( int id, int lastId,
890 int eventType,
891 wxObjectEventFunction func,
892 wxObject *userData )
893{
894#if WXWIN_COMPATIBILITY_EVENT_TYPES
895 wxEventTableEntry *entry = new wxEventTableEntry;
896 entry->m_eventType = eventType;
897 entry->m_id = id;
898 entry->m_lastId = lastId;
899 entry->m_fn = func;
900 entry->m_callbackUserData = userData;
901#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
902 wxDynamicEventTableEntry *entry =
903 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData);
904#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
905
906 if (!m_dynamicEvents)
907 m_dynamicEvents = new wxList;
908
909 m_dynamicEvents->Append( (wxObject*) entry );
910}
911
912bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
913 wxObjectEventFunction func,
914 wxObject *userData )
915{
916 if (!m_dynamicEvents)
917 return FALSE;
918
919 wxNode *node = m_dynamicEvents->First();
920 while (node)
921 {
922#if WXWIN_COMPATIBILITY_EVENT_TYPES
923 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
924#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
925 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
926#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
927
928 if ((entry->m_id == id) &&
929 ((entry->m_lastId == lastId) || (lastId == -1)) &&
930 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
931 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
932 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
933 {
934 if (entry->m_callbackUserData)
935 delete entry->m_callbackUserData;
936 m_dynamicEvents->DeleteNode( node );
937 delete entry;
938 return TRUE;
939 }
940 node = node->Next();
941 }
942 return FALSE;
943}
944
945bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
946{
947 wxCHECK_MSG( m_dynamicEvents, FALSE,
948 wxT("caller should check that we have dynamic events") );
949
950 int commandId = event.GetId();
951
952 wxNode *node = m_dynamicEvents->First();
953 while (node)
954 {
955#if WXWIN_COMPATIBILITY_EVENT_TYPES
956 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
957#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
958 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
959#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
960
961 if (entry->m_fn)
962 {
963 // Match, if event spec says any id will do (id == -1)
964 if ( (event.GetEventType() == entry->m_eventType) &&
965 (entry->m_id == -1 ||
966 (entry->m_lastId == -1 && commandId == entry->m_id) ||
967 (entry->m_lastId != -1 &&
968 (commandId >= entry->m_id && commandId <= entry->m_lastId))) )
969 {
970 event.Skip(FALSE);
971 event.m_callbackUserData = entry->m_callbackUserData;
972
973 (this->*((wxEventFunction) (entry->m_fn)))(event);
974
975 if (event.GetSkipped())
976 return FALSE;
977 else
978 return TRUE;
979 }
980 }
981 node = node->Next();
982 }
983 return FALSE;
984};
985
986void wxEvtHandler::DoSetClientObject( wxClientData *data )
987{
988 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
989 wxT("can't have both object and void client data") );
990
991 if ( m_clientObject )
992 delete m_clientObject;
993
994 m_clientObject = data;
995 m_clientDataType = wxClientData_Object;
996}
997
998wxClientData *wxEvtHandler::DoGetClientObject() const
999{
1000 // it's not an error to call GetClientObject() on a window which doesn't
1001 // have client data at all - NULL will be returned
1002 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1003 wxT("this window doesn't have object client data") );
1004
1005 return m_clientObject;
1006}
1007
1008void wxEvtHandler::DoSetClientData( void *data )
1009{
1010 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1011 wxT("can't have both object and void client data") );
1012
1013 m_clientData = data;
1014 m_clientDataType = wxClientData_Void;
1015}
1016
1017void *wxEvtHandler::DoGetClientData() const
1018{
1019 // it's not an error to call GetClientData() on a window which doesn't have
1020 // client data at all - NULL will be returned
1021 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1022 wxT("this window doesn't have void client data") );
1023
1024 return m_clientData;
1025}
1026
1027
1028#if WXWIN_COMPATIBILITY
1029bool wxEvtHandler::OnClose()
1030{
1031 if (GetNextHandler())
1032 return GetNextHandler()->OnClose();
1033 else
1034 return FALSE;
1035}
1036#endif // WXWIN_COMPATIBILITY
1037
1038#if wxUSE_GUI
1039
1040// Find a window with the focus, that is also a descendant of the given window.
1041// This is used to determine the window to initially send commands to.
1042wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1043{
1044 // Process events starting with the window with the focus, if any.
1045 wxWindow* focusWin = wxWindow::FindFocus();
1046 wxWindow* win = focusWin;
1047
1048 // Check if this is a descendant of this frame.
1049 // If not, win will be set to NULL.
1050 while (win)
1051 {
1052 if (win == ancestor)
1053 break;
1054 else
1055 win = win->GetParent();
1056 }
1057 if (win == (wxWindow*) NULL)
1058 focusWin = (wxWindow*) NULL;
1059
1060 return focusWin;
1061}
1062
1063#endif // wxUSE_GUI