]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/event.cpp
added wxFont::IsFixedWidth(), documented it and implemented for wxGTK/Motif
[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 wxEvent *eventCopy = event.Clone();
633
634 // we must be able to copy the events here so the event class must
635 // implement Clone() properly instead of just providing a NULL stab for it
636 wxCHECK_RET( eventCopy,
637 _T("events of this type aren't supposed to be posted") );
638
639#if defined(__VISAGECPP__)
640 wxENTER_CRIT_SECT( m_eventsLocker);
641#else
642 wxENTER_CRIT_SECT( *m_eventsLocker);
643#endif
644
645 if ( !m_pendingEvents )
646 m_pendingEvents = new wxList;
647
648 m_pendingEvents->Append(eventCopy);
649
650#if defined(__VISAGECPP__)
651 wxLEAVE_CRIT_SECT( m_eventsLocker);
652#else
653 wxLEAVE_CRIT_SECT( *m_eventsLocker);
654#endif
655
656 // 2) Add this event handler to list of event handlers that
657 // have pending events.
658
659 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
660
661 if ( !wxPendingEvents )
662 wxPendingEvents = new wxList;
663 wxPendingEvents->Append(this);
664
665 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
666
667 // 3) Inform the system that new pending events are somwehere,
668 // and that these should be processed in idle time.
669 wxWakeUpIdle();
670}
671
672void wxEvtHandler::ProcessPendingEvents()
673{
674#if defined(__VISAGECPP__)
675 wxENTER_CRIT_SECT( m_eventsLocker);
676#else
677 wxENTER_CRIT_SECT( *m_eventsLocker);
678#endif
679
680 wxNode *node = m_pendingEvents->First();
681 while ( node )
682 {
683 wxEvent *event = (wxEvent *)node->Data();
684 delete node;
685
686 // In ProcessEvent, new events might get added and
687 // we can safely leave the crtical section here.
688#if defined(__VISAGECPP__)
689 wxLEAVE_CRIT_SECT( m_eventsLocker);
690#else
691 wxLEAVE_CRIT_SECT( *m_eventsLocker);
692#endif
693 ProcessEvent(*event);
694 delete event;
695#if defined(__VISAGECPP__)
696 wxENTER_CRIT_SECT( m_eventsLocker);
697#else
698 wxENTER_CRIT_SECT( *m_eventsLocker);
699#endif
700
701 node = m_pendingEvents->First();
702 }
703
704#if defined(__VISAGECPP__)
705 wxLEAVE_CRIT_SECT( m_eventsLocker);
706#else
707 wxLEAVE_CRIT_SECT( *m_eventsLocker);
708#endif
709}
710
711/*
712 * Event table stuff
713 */
714
715bool wxEvtHandler::ProcessEvent(wxEvent& event)
716{
717#if wxUSE_GUI
718
719 // We have to use the actual window or processing events from
720 // wxWindowNative destructor won't work (we don't see the wxWindow class)
721#ifdef __WXDEBUG__
722 // check that our flag corresponds to reality
723 wxClassInfo* info = NULL;
724#ifdef __WXUNIVERSAL__
725# if defined(__WXMSW__)
726 info = CLASSINFO(wxWindowMSW);
727# elif defined(__WXGTK__)
728 info = CLASSINFO(wxWindowGTK);
729# elif defined(__WXMGL__)
730 info = CLASSINFO(wxWindowMGL);
731# elif defined(__WXPM__)
732 info = CLASSINFO(wxWindowOS2);
733# elif defined(__WXMAC__)
734 info = CLASSINFO(wxWindowMac);
735# elif defined(__WXMOTIF__)
736 info = CLASSINFO(wxWindowMotif);
737# endif
738#else
739 info = CLASSINFO(wxWindow);
740#endif
741
742 wxASSERT_MSG( m_isWindow == IsKindOf(info),
743 wxString(GetClassInfo()->GetClassName()) + _T(" should [not] be a window but it is [not]") );
744#endif
745
746#endif // wxUSE_GUI
747
748 // An event handler can be enabled or disabled
749 if ( GetEvtHandlerEnabled() )
750 {
751
752#if 0
753/*
754 What is this? When using GUI threads, a non main
755 threads can send an event and process it itself.
756 This breaks GTK's GUI threads, so please explain.
757*/
758
759 // Check whether we are in a child thread.
760 if ( !wxThread::IsMain() )
761 return ProcessThreadEvent(event);
762#endif
763
764 // Handle per-instance dynamic event tables first
765 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
766 return TRUE;
767
768 // Then static per-class event tables
769 const wxEventTable *table = GetEventTable();
770
771#if wxUSE_GUI && wxUSE_VALIDATORS
772 // Try the associated validator first, if this is a window.
773 // Problem: if the event handler of the window has been replaced,
774 // this wxEvtHandler may no longer be a window.
775 // Therefore validators won't be processed if the handler
776 // has been replaced with SetEventHandler.
777 // THIS CAN BE CURED if PushEventHandler is used instead of
778 // SetEventHandler, and then processing will be passed down the
779 // chain of event handlers.
780 if (m_isWindow)
781 {
782 wxWindow *win = (wxWindow *)this;
783
784 // Can only use the validator of the window which
785 // is receiving the event
786 if ( win == event.GetEventObject() )
787 {
788 wxValidator *validator = win->GetValidator();
789 if ( validator && validator->ProcessEvent(event) )
790 {
791 return TRUE;
792 }
793 }
794 }
795#endif
796
797 // Search upwards through the inheritance hierarchy
798 while (table)
799 {
800 if ( SearchEventTable((wxEventTable&)*table, event) )
801 return TRUE;
802 table = table->baseTable;
803 }
804 }
805
806 // Try going down the event handler chain
807 if ( GetNextHandler() )
808 {
809 if ( GetNextHandler()->ProcessEvent(event) )
810 return TRUE;
811 }
812
813#if wxUSE_GUI
814 // Carry on up the parent-child hierarchy, but only if event is a command
815 // event: it wouldn't make sense for a parent to receive a child's size
816 // event, for example
817 if ( m_isWindow && event.IsCommandEvent() )
818 {
819 wxWindow *win = (wxWindow *)this;
820
821 // honour the requests to stop propagation at this window: this is
822 // used by the dialogs, for example, to prevent processing the events
823 // from the dialog controls in the parent frame which rarely, if ever,
824 // makes sense
825 if ( !(win->GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
826 {
827 wxWindow *parent = win->GetParent();
828 if ( parent && !parent->IsBeingDeleted() )
829 return parent->GetEventHandler()->ProcessEvent(event);
830 }
831 }
832#endif // wxUSE_GUI
833
834 // Last try - application object.
835 if ( wxTheApp && (this != wxTheApp) )
836 {
837 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
838 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
839 // processed appropriately via SearchEventTable.
840 if ( event.GetEventType() != wxEVT_IDLE )
841 {
842 if ( wxTheApp->ProcessEvent(event) )
843 return TRUE;
844 }
845 }
846
847 return FALSE;
848}
849
850bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
851{
852 wxEventType eventType = event.GetEventType();
853 int eventId = event.GetId();
854
855 // BC++ doesn't like testing for m_fn without != 0
856 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
857 {
858 // the line using reference exposes a bug in gcc: although it _seems_
859 // to work, it leads to weird crashes later on during program
860 // execution
861#ifdef __GNUG__
862 wxEventTableEntry entry = table.entries[i];
863#else
864 const wxEventTableEntry& entry = table.entries[i];
865#endif
866
867 // match only if the event type is the same and the id is either -1 in
868 // the event table (meaning "any") or the event id matches the id
869 // specified in the event table either exactly or by falling into
870 // range between first and last
871 if ( eventType == entry.m_eventType )
872 {
873 int tableId1 = entry.m_id,
874 tableId2 = entry.m_lastId;
875
876 if ( (tableId1 == -1) ||
877 (tableId2 == -1 && eventId == tableId1) ||
878 (tableId2 != -1 &&
879 (eventId >= tableId1 && eventId <= tableId2)) )
880 {
881 event.Skip(FALSE);
882 event.m_callbackUserData = entry.m_callbackUserData;
883
884 (this->*((wxEventFunction) (entry.m_fn)))(event);
885
886 return !event.GetSkipped();
887 }
888 }
889 }
890
891 return FALSE;
892}
893
894void wxEvtHandler::Connect( int id, int lastId,
895 int eventType,
896 wxObjectEventFunction func,
897 wxObject *userData )
898{
899#if WXWIN_COMPATIBILITY_EVENT_TYPES
900 wxEventTableEntry *entry = new wxEventTableEntry;
901 entry->m_eventType = eventType;
902 entry->m_id = id;
903 entry->m_lastId = lastId;
904 entry->m_fn = func;
905 entry->m_callbackUserData = userData;
906#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
907 wxDynamicEventTableEntry *entry =
908 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData);
909#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
910
911 if (!m_dynamicEvents)
912 m_dynamicEvents = new wxList;
913
914 m_dynamicEvents->Append( (wxObject*) entry );
915}
916
917bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
918 wxObjectEventFunction func,
919 wxObject *userData )
920{
921 if (!m_dynamicEvents)
922 return FALSE;
923
924 wxNode *node = m_dynamicEvents->First();
925 while (node)
926 {
927#if WXWIN_COMPATIBILITY_EVENT_TYPES
928 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
929#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
930 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
931#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
932
933 if ((entry->m_id == id) &&
934 ((entry->m_lastId == lastId) || (lastId == -1)) &&
935 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
936 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
937 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
938 {
939 if (entry->m_callbackUserData)
940 delete entry->m_callbackUserData;
941 m_dynamicEvents->DeleteNode( node );
942 delete entry;
943 return TRUE;
944 }
945 node = node->Next();
946 }
947 return FALSE;
948}
949
950bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
951{
952 wxCHECK_MSG( m_dynamicEvents, FALSE,
953 wxT("caller should check that we have dynamic events") );
954
955 int commandId = event.GetId();
956
957 wxNode *node = m_dynamicEvents->First();
958 while (node)
959 {
960#if WXWIN_COMPATIBILITY_EVENT_TYPES
961 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
962#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
963 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
964#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
965
966 if (entry->m_fn)
967 {
968 // Match, if event spec says any id will do (id == -1)
969 if ( (event.GetEventType() == entry->m_eventType) &&
970 (entry->m_id == -1 ||
971 (entry->m_lastId == -1 && commandId == entry->m_id) ||
972 (entry->m_lastId != -1 &&
973 (commandId >= entry->m_id && commandId <= entry->m_lastId))) )
974 {
975 event.Skip(FALSE);
976 event.m_callbackUserData = entry->m_callbackUserData;
977
978 (this->*((wxEventFunction) (entry->m_fn)))(event);
979
980 if (event.GetSkipped())
981 return FALSE;
982 else
983 return TRUE;
984 }
985 }
986 node = node->Next();
987 }
988 return FALSE;
989};
990
991void wxEvtHandler::DoSetClientObject( wxClientData *data )
992{
993 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
994 wxT("can't have both object and void client data") );
995
996 if ( m_clientObject )
997 delete m_clientObject;
998
999 m_clientObject = data;
1000 m_clientDataType = wxClientData_Object;
1001}
1002
1003wxClientData *wxEvtHandler::DoGetClientObject() const
1004{
1005 // it's not an error to call GetClientObject() on a window which doesn't
1006 // have client data at all - NULL will be returned
1007 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1008 wxT("this window doesn't have object client data") );
1009
1010 return m_clientObject;
1011}
1012
1013void wxEvtHandler::DoSetClientData( void *data )
1014{
1015 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1016 wxT("can't have both object and void client data") );
1017
1018 m_clientData = data;
1019 m_clientDataType = wxClientData_Void;
1020}
1021
1022void *wxEvtHandler::DoGetClientData() const
1023{
1024 // it's not an error to call GetClientData() on a window which doesn't have
1025 // client data at all - NULL will be returned
1026 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1027 wxT("this window doesn't have void client data") );
1028
1029 return m_clientData;
1030}
1031
1032
1033#if WXWIN_COMPATIBILITY
1034bool wxEvtHandler::OnClose()
1035{
1036 if (GetNextHandler())
1037 return GetNextHandler()->OnClose();
1038 else
1039 return FALSE;
1040}
1041#endif // WXWIN_COMPATIBILITY
1042
1043#if wxUSE_GUI
1044
1045// Find a window with the focus, that is also a descendant of the given window.
1046// This is used to determine the window to initially send commands to.
1047wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1048{
1049 // Process events starting with the window with the focus, if any.
1050 wxWindow* focusWin = wxWindow::FindFocus();
1051 wxWindow* win = focusWin;
1052
1053 // Check if this is a descendant of this frame.
1054 // If not, win will be set to NULL.
1055 while (win)
1056 {
1057 if (win == ancestor)
1058 break;
1059 else
1060 win = win->GetParent();
1061 }
1062 if (win == (wxWindow*) NULL)
1063 focusWin = (wxWindow*) NULL;
1064
1065 return focusWin;
1066}
1067
1068#endif // wxUSE_GUI