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