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