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