added support for 2 extra mouse buttons (patch 1757630)
[wxWidgets.git] / src / common / event.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/event.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/list.h"
31 #include "wx/app.h"
32 #include "wx/utils.h"
33 #include "wx/stopwatch.h"
34 #include "wx/module.h"
35
36 #if wxUSE_GUI
37 #include "wx/window.h"
38 #include "wx/control.h"
39 #include "wx/dc.h"
40 #include "wx/textctrl.h"
41 #include "wx/validate.h"
42 #endif // wxUSE_GUI
43 #endif
44
45 // ----------------------------------------------------------------------------
46 // wxWin macros
47 // ----------------------------------------------------------------------------
48
49 #if wxUSE_BASE
50 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
51 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
52 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
53 #endif // wxUSE_BASE
54
55 #if wxUSE_GUI
56 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
57 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
58 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
59 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
60 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
61 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
62 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
63 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
64 IMPLEMENT_DYNAMIC_CLASS(wxNcPaintEvent, wxEvent)
65 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
66 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
67 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
69 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
70 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
78 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxDisplayChangedEvent, wxEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
88 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
89 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureChangedEvent, wxEvent)
90 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureLostEvent, wxEvent)
91 IMPLEMENT_DYNAMIC_CLASS(wxClipboardTextEvent, wxCommandEvent)
92 #endif // wxUSE_GUI
93
94 #if wxUSE_BASE
95
96 const wxEventTable *wxEvtHandler::GetEventTable() const
97 { return &wxEvtHandler::sm_eventTable; }
98
99 const wxEventTable wxEvtHandler::sm_eventTable =
100 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
101
102 wxEventHashTable &wxEvtHandler::GetEventHashTable() const
103 { return wxEvtHandler::sm_eventHashTable; }
104
105 wxEventHashTable wxEvtHandler::sm_eventHashTable(wxEvtHandler::sm_eventTable);
106
107 const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
108 { DECLARE_EVENT_TABLE_ENTRY(wxEVT_NULL, 0, 0, (wxObjectEventFunction)NULL, NULL) };
109
110
111 // wxUSE_MEMORY_TRACING considers memory freed from the static objects dtors
112 // leaked, so we need to manually clean up all event tables before checking for
113 // the memory leaks when using it, however this breaks re-initializing the
114 // library (i.e. repeated calls to wxInitialize/wxUninitialize) because the
115 // event tables won't be rebuilt the next time, so disable this by default
116 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
117
118 class wxEventTableEntryModule: public wxModule
119 {
120 public:
121 wxEventTableEntryModule() { }
122 virtual bool OnInit() { return true; }
123 virtual void OnExit() { wxEventHashTable::ClearAll(); }
124
125 DECLARE_DYNAMIC_CLASS(wxEventTableEntryModule)
126 };
127
128 IMPLEMENT_DYNAMIC_CLASS(wxEventTableEntryModule, wxModule)
129
130 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
131
132 // ----------------------------------------------------------------------------
133 // global variables
134 // ----------------------------------------------------------------------------
135
136 // To put pending event handlers
137 wxList *wxPendingEvents = (wxList *)NULL;
138
139 #if wxUSE_THREADS
140 // protects wxPendingEvents list
141 wxCriticalSection *wxPendingEventsLocker = (wxCriticalSection *)NULL;
142 #endif
143
144 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
145
146 // common event types are defined here, other event types are defined by the
147 // components which use them
148
149 const wxEventType wxEVT_FIRST = 10000;
150 const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
151
152 DEFINE_EVENT_TYPE(wxEVT_NULL)
153 DEFINE_EVENT_TYPE(wxEVT_IDLE)
154 DEFINE_EVENT_TYPE(wxEVT_SOCKET)
155 DEFINE_EVENT_TYPE(wxEVT_TIMER)
156
157 #endif // !WXWIN_COMPATIBILITY_EVENT_TYPES
158
159 #endif // wxUSE_BASE
160
161 #if wxUSE_GUI
162
163 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
164
165 DEFINE_EVENT_TYPE(wxEVT_COMMAND_BUTTON_CLICKED)
166 DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKBOX_CLICKED)
167 DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHOICE_SELECTED)
168 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_SELECTED)
169 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED)
170 DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED)
171 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MENU_SELECTED)
172 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SLIDER_UPDATED)
173 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBOX_SELECTED)
174 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBUTTON_SELECTED)
175 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SCROLLBAR_UPDATED)
176 DEFINE_EVENT_TYPE(wxEVT_COMMAND_VLBOX_SELECTED)
177 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COMBOBOX_SELECTED)
178 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_RCLICKED)
179 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_ENTER)
180 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRL_UPDATED)
181 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED)
182
183 // Mouse event types
184 DEFINE_EVENT_TYPE(wxEVT_LEFT_DOWN)
185 DEFINE_EVENT_TYPE(wxEVT_LEFT_UP)
186 DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DOWN)
187 DEFINE_EVENT_TYPE(wxEVT_MIDDLE_UP)
188 DEFINE_EVENT_TYPE(wxEVT_RIGHT_DOWN)
189 DEFINE_EVENT_TYPE(wxEVT_RIGHT_UP)
190 DEFINE_EVENT_TYPE(wxEVT_MOTION)
191 DEFINE_EVENT_TYPE(wxEVT_ENTER_WINDOW)
192 DEFINE_EVENT_TYPE(wxEVT_LEAVE_WINDOW)
193 DEFINE_EVENT_TYPE(wxEVT_LEFT_DCLICK)
194 DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DCLICK)
195 DEFINE_EVENT_TYPE(wxEVT_RIGHT_DCLICK)
196 DEFINE_EVENT_TYPE(wxEVT_SET_FOCUS)
197 DEFINE_EVENT_TYPE(wxEVT_KILL_FOCUS)
198 DEFINE_EVENT_TYPE(wxEVT_CHILD_FOCUS)
199 DEFINE_EVENT_TYPE(wxEVT_MOUSEWHEEL)
200 DEFINE_EVENT_TYPE(wxEVT_AUX1_DOWN)
201 DEFINE_EVENT_TYPE(wxEVT_AUX1_UP)
202 DEFINE_EVENT_TYPE(wxEVT_AUX1_DCLICK)
203 DEFINE_EVENT_TYPE(wxEVT_AUX2_DOWN)
204 DEFINE_EVENT_TYPE(wxEVT_AUX2_UP)
205 DEFINE_EVENT_TYPE(wxEVT_AUX2_DCLICK)
206
207 // Non-client mouse events
208 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DOWN)
209 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_UP)
210 DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DOWN)
211 DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_UP)
212 DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DOWN)
213 DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_UP)
214 DEFINE_EVENT_TYPE(wxEVT_NC_MOTION)
215 DEFINE_EVENT_TYPE(wxEVT_NC_ENTER_WINDOW)
216 DEFINE_EVENT_TYPE(wxEVT_NC_LEAVE_WINDOW)
217 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DCLICK)
218 DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DCLICK)
219 DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DCLICK)
220
221 // Character input event type
222 DEFINE_EVENT_TYPE(wxEVT_CHAR)
223 DEFINE_EVENT_TYPE(wxEVT_CHAR_HOOK)
224 DEFINE_EVENT_TYPE(wxEVT_NAVIGATION_KEY)
225 DEFINE_EVENT_TYPE(wxEVT_KEY_DOWN)
226 DEFINE_EVENT_TYPE(wxEVT_KEY_UP)
227 #if wxUSE_HOTKEY
228 DEFINE_EVENT_TYPE(wxEVT_HOTKEY)
229 #endif
230
231 // Set cursor event
232 DEFINE_EVENT_TYPE(wxEVT_SET_CURSOR)
233
234 // wxScrollbar and wxSlider event identifiers
235 DEFINE_EVENT_TYPE(wxEVT_SCROLL_TOP)
236 DEFINE_EVENT_TYPE(wxEVT_SCROLL_BOTTOM)
237 DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEUP)
238 DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEDOWN)
239 DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEUP)
240 DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEDOWN)
241 DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBTRACK)
242 DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBRELEASE)
243 DEFINE_EVENT_TYPE(wxEVT_SCROLL_CHANGED)
244
245 // Scroll events from wxWindow
246 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_TOP)
247 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_BOTTOM)
248 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEUP)
249 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEDOWN)
250 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEUP)
251 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEDOWN)
252 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBTRACK)
253 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBRELEASE)
254
255 // System events
256 DEFINE_EVENT_TYPE(wxEVT_SIZE)
257 DEFINE_EVENT_TYPE(wxEVT_SIZING)
258 DEFINE_EVENT_TYPE(wxEVT_MOVE)
259 DEFINE_EVENT_TYPE(wxEVT_MOVING)
260 DEFINE_EVENT_TYPE(wxEVT_CLOSE_WINDOW)
261 DEFINE_EVENT_TYPE(wxEVT_END_SESSION)
262 DEFINE_EVENT_TYPE(wxEVT_QUERY_END_SESSION)
263 DEFINE_EVENT_TYPE(wxEVT_HIBERNATE)
264 DEFINE_EVENT_TYPE(wxEVT_ACTIVATE_APP)
265 DEFINE_EVENT_TYPE(wxEVT_POWER)
266 DEFINE_EVENT_TYPE(wxEVT_ACTIVATE)
267 DEFINE_EVENT_TYPE(wxEVT_CREATE)
268 DEFINE_EVENT_TYPE(wxEVT_DESTROY)
269 DEFINE_EVENT_TYPE(wxEVT_SHOW)
270 DEFINE_EVENT_TYPE(wxEVT_ICONIZE)
271 DEFINE_EVENT_TYPE(wxEVT_MAXIMIZE)
272 DEFINE_EVENT_TYPE(wxEVT_MOUSE_CAPTURE_CHANGED)
273 DEFINE_EVENT_TYPE(wxEVT_MOUSE_CAPTURE_LOST)
274 DEFINE_EVENT_TYPE(wxEVT_PAINT)
275 DEFINE_EVENT_TYPE(wxEVT_ERASE_BACKGROUND)
276 DEFINE_EVENT_TYPE(wxEVT_NC_PAINT)
277 DEFINE_EVENT_TYPE(wxEVT_PAINT_ICON)
278 DEFINE_EVENT_TYPE(wxEVT_MENU_OPEN)
279 DEFINE_EVENT_TYPE(wxEVT_MENU_CLOSE)
280 DEFINE_EVENT_TYPE(wxEVT_MENU_HIGHLIGHT)
281 DEFINE_EVENT_TYPE(wxEVT_CONTEXT_MENU)
282 DEFINE_EVENT_TYPE(wxEVT_SYS_COLOUR_CHANGED)
283 DEFINE_EVENT_TYPE(wxEVT_DISPLAY_CHANGED)
284 DEFINE_EVENT_TYPE(wxEVT_SETTING_CHANGED)
285 DEFINE_EVENT_TYPE(wxEVT_QUERY_NEW_PALETTE)
286 DEFINE_EVENT_TYPE(wxEVT_PALETTE_CHANGED)
287 DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_DOWN)
288 DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_UP)
289 DEFINE_EVENT_TYPE(wxEVT_JOY_MOVE)
290 DEFINE_EVENT_TYPE(wxEVT_JOY_ZMOVE)
291 DEFINE_EVENT_TYPE(wxEVT_DROP_FILES)
292 DEFINE_EVENT_TYPE(wxEVT_DRAW_ITEM)
293 DEFINE_EVENT_TYPE(wxEVT_MEASURE_ITEM)
294 DEFINE_EVENT_TYPE(wxEVT_COMPARE_ITEM)
295 DEFINE_EVENT_TYPE(wxEVT_INIT_DIALOG)
296 DEFINE_EVENT_TYPE(wxEVT_UPDATE_UI)
297
298 // Clipboard events
299 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_COPY)
300 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_CUT)
301 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_PASTE)
302
303 // Generic command events
304 // Note: a click is a higher-level event than button down/up
305 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_CLICK)
306 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_DCLICK)
307 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_CLICK)
308 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_DCLICK)
309 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SET_FOCUS)
310 DEFINE_EVENT_TYPE(wxEVT_COMMAND_KILL_FOCUS)
311 DEFINE_EVENT_TYPE(wxEVT_COMMAND_ENTER)
312
313 // Help events
314 DEFINE_EVENT_TYPE(wxEVT_HELP)
315 DEFINE_EVENT_TYPE(wxEVT_DETAILED_HELP)
316
317 #endif // !WXWIN_COMPATIBILITY_EVENT_TYPES
318
319 #endif // wxUSE_GUI
320
321 #if wxUSE_BASE
322
323 wxIdleMode wxIdleEvent::sm_idleMode = wxIDLE_PROCESS_ALL;
324
325 // ============================================================================
326 // implementation
327 // ============================================================================
328
329 // ----------------------------------------------------------------------------
330 // event initialization
331 // ----------------------------------------------------------------------------
332
333 int wxNewEventType()
334 {
335 // MT-FIXME
336 static int s_lastUsedEventType = wxEVT_FIRST;
337
338 return s_lastUsedEventType++;
339 }
340
341 // ----------------------------------------------------------------------------
342 // wxEvent
343 // ----------------------------------------------------------------------------
344
345 /*
346 * General wxWidgets events, covering
347 * all interesting things that might happen (button clicking, resizing,
348 * setting text in widgets, etc.).
349 *
350 * For each completely new event type, derive a new event class.
351 *
352 */
353
354 wxEvent::wxEvent(int theId, wxEventType commandType )
355 {
356 m_eventType = commandType;
357 m_eventObject = (wxObject *) NULL;
358 m_timeStamp = 0;
359 m_id = theId;
360 m_skipped = false;
361 m_callbackUserData = (wxObject *) NULL;
362 m_isCommandEvent = false;
363 m_propagationLevel = wxEVENT_PROPAGATE_NONE;
364 }
365
366 wxEvent::wxEvent(const wxEvent &src)
367 : wxObject(src)
368 , m_eventObject(src.m_eventObject)
369 , m_eventType(src.m_eventType)
370 , m_timeStamp(src.m_timeStamp)
371 , m_id(src.m_id)
372 , m_callbackUserData(src.m_callbackUserData)
373 , m_propagationLevel(src.m_propagationLevel)
374 , m_skipped(src.m_skipped)
375 , m_isCommandEvent(src.m_isCommandEvent)
376 {
377 }
378
379 #endif // wxUSE_BASE
380
381 #if wxUSE_GUI
382
383 /*
384 * Command events
385 *
386 */
387
388 #ifdef __VISUALC__
389 // 'this' : used in base member initializer list (for m_commandString)
390 #pragma warning(disable:4355)
391 #endif
392
393 wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
394 : wxEvent(theId, commandType)
395 {
396 m_clientData = (char *) NULL;
397 m_clientObject = (wxClientData *) NULL;
398 m_extraLong = 0;
399 m_commandInt = 0;
400 m_isCommandEvent = true;
401
402 // the command events are propagated upwards by default
403 m_propagationLevel = wxEVENT_PROPAGATE_MAX;
404 }
405
406 #ifdef __VISUALC__
407 #pragma warning(default:4355)
408 #endif
409
410 wxString wxCommandEvent::GetString() const
411 {
412 if(m_eventType != wxEVT_COMMAND_TEXT_UPDATED || !m_eventObject)
413 return m_cmdString;
414 else
415 {
416 #if wxUSE_TEXTCTRL
417 wxTextCtrl *txt = wxDynamicCast(m_eventObject, wxTextCtrl);
418 if(txt)
419 return txt->GetValue();
420 else
421 #endif // wxUSE_TEXTCTRL
422 return m_cmdString;
423 }
424 }
425
426 /*
427 * UI update events
428 */
429
430 #if wxUSE_LONGLONG
431 wxLongLong wxUpdateUIEvent::sm_lastUpdate = 0;
432 #endif
433
434 long wxUpdateUIEvent::sm_updateInterval = 0;
435
436 wxUpdateUIMode wxUpdateUIEvent::sm_updateMode = wxUPDATE_UI_PROCESS_ALL;
437
438 // Can we update?
439 bool wxUpdateUIEvent::CanUpdate(wxWindowBase *win)
440 {
441 // Don't update if we've switched global updating off
442 // and this window doesn't support updates.
443 if (win &&
444 (GetMode() == wxUPDATE_UI_PROCESS_SPECIFIED &&
445 ((win->GetExtraStyle() & wxWS_EX_PROCESS_UI_UPDATES) == 0)))
446 return false;
447
448 if (sm_updateInterval == -1)
449 return false;
450
451 if (sm_updateInterval == 0)
452 return true;
453
454 #if wxUSE_STOPWATCH && wxUSE_LONGLONG
455 wxLongLong now = wxGetLocalTimeMillis();
456 if (now > (sm_lastUpdate + sm_updateInterval))
457 {
458 return true;
459 }
460
461 return false;
462 #else
463 // If we don't have wxStopWatch or wxLongLong, we
464 // should err on the safe side and update now anyway.
465 return true;
466 #endif
467 }
468
469 // Reset the update time to provide a delay until the next
470 // time we should update
471 void wxUpdateUIEvent::ResetUpdateTime()
472 {
473 #if wxUSE_STOPWATCH && wxUSE_LONGLONG
474 if (sm_updateInterval > 0)
475 {
476 wxLongLong now = wxGetLocalTimeMillis();
477 if (now > (sm_lastUpdate + sm_updateInterval))
478 {
479 sm_lastUpdate = now;
480 }
481 }
482 #endif
483 }
484
485 /*
486 * Scroll events
487 */
488
489 wxScrollEvent::wxScrollEvent(wxEventType commandType,
490 int id,
491 int pos,
492 int orient)
493 : wxCommandEvent(commandType, id)
494 {
495 m_extraLong = orient;
496 m_commandInt = pos;
497 }
498
499 /*
500 * ScrollWin events
501 */
502
503 wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
504 int pos,
505 int orient)
506 {
507 m_eventType = commandType;
508 m_extraLong = orient;
509 m_commandInt = pos;
510 }
511
512 /*
513 * Mouse events
514 *
515 */
516
517 wxMouseEvent::wxMouseEvent(wxEventType commandType)
518 {
519 m_eventType = commandType;
520 m_metaDown = false;
521 m_altDown = false;
522 m_controlDown = false;
523 m_shiftDown = false;
524 m_leftDown = false;
525 m_rightDown = false;
526 m_middleDown = false;
527 m_aux1Down = false;
528 m_aux2Down = false;
529 m_x = 0;
530 m_y = 0;
531 m_wheelRotation = 0;
532 m_wheelDelta = 0;
533 m_linesPerAction = 0;
534 m_wheelAxis = 0;
535 }
536
537 void wxMouseEvent::Assign(const wxMouseEvent& event)
538 {
539 m_eventType = event.m_eventType;
540
541 m_x = event.m_x;
542 m_y = event.m_y;
543
544 m_leftDown = event.m_leftDown;
545 m_middleDown = event.m_middleDown;
546 m_rightDown = event.m_rightDown;
547 m_aux1Down = event.m_aux1Down;
548 m_aux2Down = event.m_aux2Down;
549
550 m_controlDown = event.m_controlDown;
551 m_shiftDown = event.m_shiftDown;
552 m_altDown = event.m_altDown;
553 m_metaDown = event.m_metaDown;
554
555 m_wheelRotation = event.m_wheelRotation;
556 m_wheelDelta = event.m_wheelDelta;
557 m_linesPerAction = event.m_linesPerAction;
558 m_wheelAxis = event.m_wheelAxis;
559 }
560
561 // return true if was a button dclick event
562 bool wxMouseEvent::ButtonDClick(int but) const
563 {
564 switch (but)
565 {
566 default:
567 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
568 // fall through
569
570 case wxMOUSE_BTN_ANY:
571 return (LeftDClick() || MiddleDClick() || RightDClick() ||
572 Aux1DClick() || Aux2DClick());
573
574 case wxMOUSE_BTN_LEFT:
575 return LeftDClick();
576
577 case wxMOUSE_BTN_MIDDLE:
578 return MiddleDClick();
579
580 case wxMOUSE_BTN_RIGHT:
581 return RightDClick();
582
583 case wxMOUSE_BTN_AUX1:
584 return Aux1DClick();
585
586 case wxMOUSE_BTN_AUX2:
587 return Aux2DClick();
588 }
589 }
590
591 // return true if was a button down event
592 bool wxMouseEvent::ButtonDown(int but) const
593 {
594 switch (but)
595 {
596 default:
597 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
598 // fall through
599
600 case wxMOUSE_BTN_ANY:
601 return (LeftDown() || MiddleDown() || RightDown() ||
602 Aux1Down() || Aux2Down());
603
604 case wxMOUSE_BTN_LEFT:
605 return LeftDown();
606
607 case wxMOUSE_BTN_MIDDLE:
608 return MiddleDown();
609
610 case wxMOUSE_BTN_RIGHT:
611 return RightDown();
612
613 case wxMOUSE_BTN_AUX1:
614 return Aux1Down();
615
616 case wxMOUSE_BTN_AUX2:
617 return Aux2Down();
618 }
619 }
620
621 // return true if was a button up event
622 bool wxMouseEvent::ButtonUp(int but) const
623 {
624 switch (but)
625 {
626 default:
627 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
628 // fall through
629
630 case wxMOUSE_BTN_ANY:
631 return (LeftUp() || MiddleUp() || RightUp() ||
632 Aux1Up() || Aux2Up());
633
634 case wxMOUSE_BTN_LEFT:
635 return LeftUp();
636
637 case wxMOUSE_BTN_MIDDLE:
638 return MiddleUp();
639
640 case wxMOUSE_BTN_RIGHT:
641 return RightUp();
642
643 case wxMOUSE_BTN_AUX1:
644 return Aux1Up();
645
646 case wxMOUSE_BTN_AUX2:
647 return Aux2Up();
648 }
649 }
650
651 // return true if the given button is currently changing state
652 bool wxMouseEvent::Button(int but) const
653 {
654 switch (but)
655 {
656 default:
657 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
658 // fall through
659
660 case wxMOUSE_BTN_ANY:
661 return ButtonUp(wxMOUSE_BTN_ANY) ||
662 ButtonDown(wxMOUSE_BTN_ANY) ||
663 ButtonDClick(wxMOUSE_BTN_ANY);
664
665 case wxMOUSE_BTN_LEFT:
666 return LeftDown() || LeftUp() || LeftDClick();
667
668 case wxMOUSE_BTN_MIDDLE:
669 return MiddleDown() || MiddleUp() || MiddleDClick();
670
671 case wxMOUSE_BTN_RIGHT:
672 return RightDown() || RightUp() || RightDClick();
673
674 case wxMOUSE_BTN_AUX1:
675 return Aux1Down() || Aux1Up() || Aux1DClick();
676
677 case wxMOUSE_BTN_AUX2:
678 return Aux2Down() || Aux2Up() || Aux2DClick();
679 }
680 }
681
682 bool wxMouseEvent::ButtonIsDown(int but) const
683 {
684 switch (but)
685 {
686 default:
687 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
688 // fall through
689
690 case wxMOUSE_BTN_ANY:
691 return LeftIsDown() || MiddleIsDown() || RightIsDown() || Aux1Down() || Aux2Down();
692
693 case wxMOUSE_BTN_LEFT:
694 return LeftIsDown();
695
696 case wxMOUSE_BTN_MIDDLE:
697 return MiddleIsDown();
698
699 case wxMOUSE_BTN_RIGHT:
700 return RightIsDown();
701
702 case wxMOUSE_BTN_AUX1:
703 return Aux1IsDown();
704
705 case wxMOUSE_BTN_AUX2:
706 return Aux2IsDown();
707 }
708 }
709
710 int wxMouseEvent::GetButton() const
711 {
712 for ( int i = 1; i < wxMOUSE_BTN_MAX; i++ )
713 {
714 if ( Button(i) )
715 {
716 return i;
717 }
718 }
719
720 return wxMOUSE_BTN_NONE;
721 }
722
723 // Find the logical position of the event given the DC
724 wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
725 {
726 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
727 return pt;
728 }
729
730
731 /*
732 * Keyboard event
733 *
734 */
735
736 wxKeyEvent::wxKeyEvent(wxEventType type)
737 {
738 m_eventType = type;
739 m_shiftDown = false;
740 m_controlDown = false;
741 m_metaDown = false;
742 m_altDown = false;
743 m_keyCode = 0;
744 m_scanCode = 0;
745 #if wxUSE_UNICODE
746 m_uniChar = 0;
747 #endif
748 }
749
750 wxKeyEvent::wxKeyEvent(const wxKeyEvent& evt)
751 : wxEvent(evt)
752 {
753 m_x = evt.m_x;
754 m_y = evt.m_y;
755
756 m_keyCode = evt.m_keyCode;
757
758 m_controlDown = evt.m_controlDown;
759 m_shiftDown = evt.m_shiftDown;
760 m_altDown = evt.m_altDown;
761 m_metaDown = evt.m_metaDown;
762 m_scanCode = evt.m_scanCode;
763 m_rawCode = evt.m_rawCode;
764 m_rawFlags = evt.m_rawFlags;
765
766 #if wxUSE_UNICODE
767 m_uniChar = evt.m_uniChar;
768 #endif
769 }
770
771 #if WXWIN_COMPATIBILITY_2_6
772 long wxKeyEvent::KeyCode() const
773 {
774 return m_keyCode;
775 }
776 #endif // WXWIN_COMPATIBILITY_2_6
777
778 wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
779 {
780 SetEventType(wxEVT_CREATE);
781 SetEventObject(win);
782 }
783
784 wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
785 {
786 SetEventType(wxEVT_DESTROY);
787 SetEventObject(win);
788 }
789
790 wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
791 : wxCommandEvent(wxEVT_CHILD_FOCUS)
792 {
793 SetEventObject(win);
794 }
795
796 // ----------------------------------------------------------------------------
797 // wxHelpEvent
798 // ----------------------------------------------------------------------------
799
800 /* static */
801 wxHelpEvent::Origin wxHelpEvent::GuessOrigin(Origin origin)
802 {
803 if ( origin == Origin_Unknown )
804 {
805 // assume that the event comes from the help button if it's not from
806 // keyboard and that pressing F1 always results in the help event
807 origin = wxGetKeyState(WXK_F1) ? Origin_Keyboard : Origin_HelpButton;
808 }
809
810 return origin;
811 }
812
813 #endif // wxUSE_GUI
814
815
816 #if wxUSE_BASE
817
818 // ----------------------------------------------------------------------------
819 // wxEventHashTable
820 // ----------------------------------------------------------------------------
821
822 static const int EVENT_TYPE_TABLE_INIT_SIZE = 31; // Not too big not too small...
823
824 wxEventHashTable* wxEventHashTable::sm_first = NULL;
825
826 wxEventHashTable::wxEventHashTable(const wxEventTable &table)
827 : m_table(table),
828 m_rebuildHash(true)
829 {
830 AllocEventTypeTable(EVENT_TYPE_TABLE_INIT_SIZE);
831
832 m_next = sm_first;
833 if (m_next)
834 m_next->m_previous = this;
835 sm_first = this;
836 }
837
838 wxEventHashTable::~wxEventHashTable()
839 {
840 if (m_next)
841 m_next->m_previous = m_previous;
842 if (m_previous)
843 m_previous->m_next = m_next;
844 if (sm_first == this)
845 sm_first = m_next;
846
847 Clear();
848 }
849
850 void wxEventHashTable::Clear()
851 {
852 for ( size_t i = 0; i < m_size; i++ )
853 {
854 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
855 delete eTTnode;
856 }
857
858 delete[] m_eventTypeTable;
859 m_eventTypeTable = NULL;
860
861 m_size = 0;
862 }
863
864 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
865
866 // Clear all tables
867 void wxEventHashTable::ClearAll()
868 {
869 wxEventHashTable* table = sm_first;
870 while (table)
871 {
872 table->Clear();
873 table = table->m_next;
874 }
875 }
876
877 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
878
879 bool wxEventHashTable::HandleEvent(wxEvent &event, wxEvtHandler *self)
880 {
881 if (m_rebuildHash)
882 {
883 InitHashTable();
884 m_rebuildHash = false;
885 }
886
887 if (!m_eventTypeTable)
888 return false;
889
890 // Find all entries for the given event type.
891 wxEventType eventType = event.GetEventType();
892 const EventTypeTablePointer eTTnode = m_eventTypeTable[eventType % m_size];
893 if (eTTnode && eTTnode->eventType == eventType)
894 {
895 // Now start the search for an event handler
896 // that can handle an event with the given ID.
897 const wxEventTableEntryPointerArray&
898 eventEntryTable = eTTnode->eventEntryTable;
899
900 const size_t count = eventEntryTable.GetCount();
901 for (size_t n = 0; n < count; n++)
902 {
903 if ( wxEvtHandler::
904 ProcessEventIfMatches(*eventEntryTable[n], self, event) )
905 {
906 return true;
907 }
908 }
909 }
910
911 return false;
912 }
913
914 void wxEventHashTable::InitHashTable()
915 {
916 // Loop over the event tables and all its base tables.
917 const wxEventTable *table = &m_table;
918 while (table)
919 {
920 // Retrieve all valid event handler entries
921 const wxEventTableEntry *entry = table->entries;
922 while (entry->m_fn != 0)
923 {
924 // Add the event entry in the Hash.
925 AddEntry(*entry);
926
927 entry++;
928 }
929
930 table = table->baseTable;
931 }
932
933 // Lets free some memory.
934 size_t i;
935 for(i = 0; i < m_size; i++)
936 {
937 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
938 if (eTTnode)
939 {
940 eTTnode->eventEntryTable.Shrink();
941 }
942 }
943 }
944
945 void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
946 {
947 // This might happen 'accidentally' as the app is exiting
948 if (!m_eventTypeTable)
949 return;
950
951 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
952 EventTypeTablePointer eTTnode = *peTTnode;
953
954 if (eTTnode)
955 {
956 if (eTTnode->eventType != entry.m_eventType)
957 {
958 // Resize the table!
959 GrowEventTypeTable();
960 // Try again to add it.
961 AddEntry(entry);
962 return;
963 }
964 }
965 else
966 {
967 eTTnode = new EventTypeTable;
968 eTTnode->eventType = entry.m_eventType;
969 *peTTnode = eTTnode;
970 }
971
972 // Fill all hash entries between entry.m_id and entry.m_lastId...
973 eTTnode->eventEntryTable.Add(&entry);
974 }
975
976 void wxEventHashTable::AllocEventTypeTable(size_t size)
977 {
978 m_eventTypeTable = new EventTypeTablePointer[size];
979 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
980 m_size = size;
981 }
982
983 void wxEventHashTable::GrowEventTypeTable()
984 {
985 size_t oldSize = m_size;
986 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
987
988 // TODO: Search the most optimal grow sequence
989 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
990
991 for ( size_t i = 0; i < oldSize; /* */ )
992 {
993 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
994 if (eTToldNode)
995 {
996 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
997 EventTypeTablePointer eTTnode = *peTTnode;
998
999 // Check for collision, we don't want any.
1000 if (eTTnode)
1001 {
1002 GrowEventTypeTable();
1003 continue; // Don't increment the counter,
1004 // as we still need to add this element.
1005 }
1006 else
1007 {
1008 // Get the old value and put it in the new table.
1009 *peTTnode = oldEventTypeTable[i];
1010 }
1011 }
1012
1013 i++;
1014 }
1015
1016 delete[] oldEventTypeTable;
1017 }
1018
1019
1020 // ----------------------------------------------------------------------------
1021 // wxEvtHandler
1022 // ----------------------------------------------------------------------------
1023
1024 /*
1025 * Event handler
1026 */
1027
1028 wxEvtHandler::wxEvtHandler()
1029 {
1030 m_nextHandler = (wxEvtHandler *) NULL;
1031 m_previousHandler = (wxEvtHandler *) NULL;
1032 m_enabled = true;
1033 m_dynamicEvents = (wxList *) NULL;
1034 m_pendingEvents = (wxList *) NULL;
1035 #if wxUSE_THREADS
1036 # if !defined(__VISAGECPP__)
1037 m_eventsLocker = new wxCriticalSection;
1038 # endif
1039 #endif
1040
1041 // no client data (yet)
1042 m_clientData = NULL;
1043 m_clientDataType = wxClientData_None;
1044 }
1045
1046 wxEvtHandler::~wxEvtHandler()
1047 {
1048 // Takes itself out of the list of handlers
1049 if (m_previousHandler)
1050 m_previousHandler->m_nextHandler = m_nextHandler;
1051
1052 if (m_nextHandler)
1053 m_nextHandler->m_previousHandler = m_previousHandler;
1054
1055 if (m_dynamicEvents)
1056 {
1057 for ( wxList::iterator it = m_dynamicEvents->begin(),
1058 end = m_dynamicEvents->end();
1059 it != end;
1060 ++it )
1061 {
1062 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1063 wxEventTableEntry *entry = (wxEventTableEntry*)*it;
1064 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1065 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
1066 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1067
1068 if (entry->m_callbackUserData)
1069 delete entry->m_callbackUserData;
1070 delete entry;
1071 }
1072 delete m_dynamicEvents;
1073 };
1074
1075 if (m_pendingEvents)
1076 m_pendingEvents->DeleteContents(true);
1077 delete m_pendingEvents;
1078
1079 #if wxUSE_THREADS
1080 # if !defined(__VISAGECPP__)
1081 delete m_eventsLocker;
1082 # endif
1083
1084 // Remove us from wxPendingEvents if necessary.
1085 if(wxPendingEventsLocker)
1086 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1087 if ( wxPendingEvents )
1088 {
1089 // Delete all occurences of this from the list of pending events
1090 while (wxPendingEvents->DeleteObject(this)) { } // Do nothing
1091 }
1092 if(wxPendingEventsLocker)
1093 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1094 #endif
1095
1096 // we only delete object data, not untyped
1097 if ( m_clientDataType == wxClientData_Object )
1098 delete m_clientObject;
1099 }
1100
1101 #if wxUSE_THREADS
1102
1103 bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
1104 {
1105 // check that we are really in a child thread
1106 wxASSERT_MSG( !wxThread::IsMain(),
1107 wxT("use ProcessEvent() in main thread") );
1108
1109 AddPendingEvent(event);
1110
1111 return true;
1112 }
1113
1114 void wxEvtHandler::ClearEventLocker()
1115 {
1116 #if !defined(__VISAGECPP__)
1117 delete m_eventsLocker;
1118 m_eventsLocker = NULL;
1119 #endif
1120 }
1121
1122 #endif // wxUSE_THREADS
1123
1124 void wxEvtHandler::AddPendingEvent(const wxEvent& event)
1125 {
1126 // 1) Add event to list of pending events of this event handler
1127
1128 wxEvent *eventCopy = event.Clone();
1129
1130 // we must be able to copy the events here so the event class must
1131 // implement Clone() properly instead of just providing a NULL stab for it
1132 wxCHECK_RET( eventCopy,
1133 _T("events of this type aren't supposed to be posted") );
1134
1135 wxENTER_CRIT_SECT( Lock() );
1136
1137 if ( !m_pendingEvents )
1138 m_pendingEvents = new wxList;
1139
1140 m_pendingEvents->Append(eventCopy);
1141
1142 wxLEAVE_CRIT_SECT( Lock() );
1143
1144 // 2) Add this event handler to list of event handlers that
1145 // have pending events.
1146
1147 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1148
1149 if ( !wxPendingEvents )
1150 wxPendingEvents = new wxList;
1151 wxPendingEvents->Append(this);
1152
1153 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1154
1155 // 3) Inform the system that new pending events are somewhere,
1156 // and that these should be processed in idle time.
1157 wxWakeUpIdle();
1158 }
1159
1160 void wxEvtHandler::ProcessPendingEvents()
1161 {
1162 // this method is only called by wxApp if this handler does have
1163 // pending events
1164 wxCHECK_RET( m_pendingEvents,
1165 wxT("Please call wxApp::ProcessPendingEvents() instead") );
1166
1167 wxENTER_CRIT_SECT( Lock() );
1168
1169 // we leave the loop once we have processed all events that were present at
1170 // the start of ProcessPendingEvents because otherwise we could get into
1171 // infinite loop if the pending event handler execution resulted in another
1172 // event being posted
1173 size_t n = m_pendingEvents->size();
1174 for ( wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
1175 node;
1176 node = m_pendingEvents->GetFirst() )
1177 {
1178 wxEvent *event = (wxEvent *)node->GetData();
1179
1180 // It's importan we remove event from list before processing it.
1181 // Else a nested event loop, for example from a modal dialog, might
1182 // process the same event again.
1183
1184 m_pendingEvents->Erase(node);
1185
1186 wxLEAVE_CRIT_SECT( Lock() );
1187
1188 ProcessEvent(*event);
1189
1190 delete event;
1191
1192 wxENTER_CRIT_SECT( Lock() );
1193
1194 if ( --n == 0 )
1195 break;
1196 }
1197
1198 wxLEAVE_CRIT_SECT( Lock() );
1199 }
1200
1201 /*
1202 * Event table stuff
1203 */
1204 /* static */ bool
1205 wxEvtHandler::ProcessEventIfMatches(const wxEventTableEntryBase& entry,
1206 wxEvtHandler *handler,
1207 wxEvent& event)
1208 {
1209 int tableId1 = entry.m_id,
1210 tableId2 = entry.m_lastId;
1211
1212 // match only if the event type is the same and the id is either -1 in
1213 // the event table (meaning "any") or the event id matches the id
1214 // specified in the event table either exactly or by falling into
1215 // range between first and last
1216 if ((tableId1 == wxID_ANY) ||
1217 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1218 (tableId2 != wxID_ANY &&
1219 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1220 {
1221 event.Skip(false);
1222 event.m_callbackUserData = entry.m_callbackUserData;
1223
1224 #if wxUSE_EXCEPTIONS
1225 if ( wxTheApp )
1226 {
1227 // call the handler via wxApp method which allows the user to catch
1228 // any exceptions which may be thrown by any handler in the program
1229 // in one place
1230 wxTheApp->HandleEvent(handler, (wxEventFunction)entry.m_fn, event);
1231 }
1232 else
1233 #endif // wxUSE_EXCEPTIONS
1234 {
1235 // no need for an extra virtual function call
1236 (handler->*((wxEventFunction) (entry.m_fn)))(event);
1237 }
1238
1239 if (!event.GetSkipped())
1240 return true;
1241 }
1242
1243 return false;
1244 }
1245
1246 bool wxEvtHandler::TryParent(wxEvent& event)
1247 {
1248 if ( wxTheApp && (this != wxTheApp) )
1249 {
1250 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1251 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1252 // processed appropriately via SearchEventTable.
1253 if ( event.GetEventType() != wxEVT_IDLE )
1254 {
1255 if ( wxTheApp->ProcessEvent(event) )
1256 return true;
1257 }
1258 }
1259
1260 return false;
1261 }
1262
1263 bool wxEvtHandler::ProcessEvent(wxEvent& event)
1264 {
1265 // allow the application to hook into event processing
1266 if ( wxTheApp )
1267 {
1268 int rc = wxTheApp->FilterEvent(event);
1269 if ( rc != -1 )
1270 {
1271 wxASSERT_MSG( rc == 1 || rc == 0,
1272 _T("unexpected wxApp::FilterEvent return value") );
1273
1274 return rc != 0;
1275 }
1276 //else: proceed normally
1277 }
1278
1279 // An event handler can be enabled or disabled
1280 if ( GetEvtHandlerEnabled() )
1281 {
1282 // if we have a validator, it has higher priority than our own event
1283 // table
1284 if ( TryValidator(event) )
1285 return true;
1286
1287 // Handle per-instance dynamic event tables first
1288 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1289 return true;
1290
1291 // Then static per-class event tables
1292 if ( GetEventHashTable().HandleEvent(event, this) )
1293 return true;
1294 }
1295
1296 // Try going down the event handler chain
1297 if ( GetNextHandler() )
1298 {
1299 // notice that we shouldn't let the parent have the event even if the
1300 // next handler does not process it because it will have already passed
1301 // it to the parent in this case
1302 return GetNextHandler()->ProcessEvent(event);
1303 }
1304
1305 // Finally propagate the event upwards the window chain and/or to the
1306 // application object as necessary
1307 return TryParent(event);
1308 }
1309
1310
1311 bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1312 {
1313 const wxEventType eventType = event.GetEventType();
1314 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
1315 {
1316 const wxEventTableEntry& entry = table.entries[i];
1317 if ( eventType == entry.m_eventType )
1318 {
1319 if ( ProcessEventIfMatches(entry, this, event) )
1320 return true;
1321 }
1322 }
1323
1324 return false;
1325 }
1326
1327 void wxEvtHandler::Connect( int id, int lastId,
1328 int eventType,
1329 wxObjectEventFunction func,
1330 wxObject *userData,
1331 wxEvtHandler* eventSink )
1332 {
1333 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1334 wxEventTableEntry *entry = new wxEventTableEntry;
1335 entry->m_eventType = eventType;
1336 entry->m_id = id;
1337 entry->m_lastId = lastId;
1338 entry->m_fn = func;
1339 entry->m_callbackUserData = userData;
1340 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1341 wxDynamicEventTableEntry *entry =
1342 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData, eventSink);
1343 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1344
1345 if (!m_dynamicEvents)
1346 m_dynamicEvents = new wxList;
1347
1348 // Insert at the front of the list so most recent additions are found first
1349 m_dynamicEvents->Insert( (wxObject*) entry );
1350 }
1351
1352 bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
1353 wxObjectEventFunction func,
1354 wxObject *userData,
1355 wxEvtHandler* eventSink )
1356 {
1357 if (!m_dynamicEvents)
1358 return false;
1359
1360 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1361 while (node)
1362 {
1363 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1364 wxEventTableEntry *entry = (wxEventTableEntry*)node->GetData();
1365 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1366 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1367 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1368
1369 if ((entry->m_id == id) &&
1370 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
1371 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1372 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
1373 ((entry->m_eventSink == eventSink) || (eventSink == (wxEvtHandler*)NULL)) &&
1374 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
1375 {
1376 if (entry->m_callbackUserData)
1377 delete entry->m_callbackUserData;
1378 m_dynamicEvents->Erase( node );
1379 delete entry;
1380 return true;
1381 }
1382 node = node->GetNext();
1383 }
1384 return false;
1385 }
1386
1387 bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1388 {
1389 wxCHECK_MSG( m_dynamicEvents, false,
1390 wxT("caller should check that we have dynamic events") );
1391
1392 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1393 while (node)
1394 {
1395 #if WXWIN_COMPATIBILITY_EVENT_TYPES
1396 wxEventTableEntry *entry = (wxEventTableEntry*)node->GetData();
1397 #else // !WXWIN_COMPATIBILITY_EVENT_TYPES
1398 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1399 #endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
1400
1401 // get next node before (maybe) calling the event handler as it could
1402 // call Disconnect() invalidating the current node
1403 node = node->GetNext();
1404
1405 if ((event.GetEventType() == entry->m_eventType) && (entry->m_fn != 0))
1406 {
1407 wxEvtHandler *handler =
1408 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
1409 entry->m_eventSink ? entry->m_eventSink
1410 :
1411 #endif
1412 this;
1413
1414 if ( ProcessEventIfMatches(*entry, handler, event) )
1415 {
1416 return true;
1417 }
1418 }
1419 }
1420
1421 return false;
1422 }
1423
1424 void wxEvtHandler::DoSetClientObject( wxClientData *data )
1425 {
1426 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1427 wxT("can't have both object and void client data") );
1428
1429 if ( m_clientObject )
1430 delete m_clientObject;
1431
1432 m_clientObject = data;
1433 m_clientDataType = wxClientData_Object;
1434 }
1435
1436 wxClientData *wxEvtHandler::DoGetClientObject() const
1437 {
1438 // it's not an error to call GetClientObject() on a window which doesn't
1439 // have client data at all - NULL will be returned
1440 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1441 wxT("this window doesn't have object client data") );
1442
1443 return m_clientObject;
1444 }
1445
1446 void wxEvtHandler::DoSetClientData( void *data )
1447 {
1448 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1449 wxT("can't have both object and void client data") );
1450
1451 m_clientData = data;
1452 m_clientDataType = wxClientData_Void;
1453 }
1454
1455 void *wxEvtHandler::DoGetClientData() const
1456 {
1457 // it's not an error to call GetClientData() on a window which doesn't have
1458 // client data at all - NULL will be returned
1459 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1460 wxT("this window doesn't have void client data") );
1461
1462 return m_clientData;
1463 }
1464
1465 #endif // wxUSE_BASE
1466
1467 #if wxUSE_GUI
1468
1469 // Find a window with the focus, that is also a descendant of the given window.
1470 // This is used to determine the window to initially send commands to.
1471 wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1472 {
1473 // Process events starting with the window with the focus, if any.
1474 wxWindow* focusWin = wxWindow::FindFocus();
1475 wxWindow* win = focusWin;
1476
1477 // Check if this is a descendant of this frame.
1478 // If not, win will be set to NULL.
1479 while (win)
1480 {
1481 if (win == ancestor)
1482 break;
1483 else
1484 win = win->GetParent();
1485 }
1486 if (win == (wxWindow*) NULL)
1487 focusWin = (wxWindow*) NULL;
1488
1489 return focusWin;
1490 }
1491
1492 // ----------------------------------------------------------------------------
1493 // wxEventBlocker
1494 // ----------------------------------------------------------------------------
1495
1496 wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1497 {
1498 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1499
1500 m_window = win;
1501
1502 Block(type);
1503 m_window->PushEventHandler(this);
1504 }
1505
1506 wxEventBlocker::~wxEventBlocker()
1507 {
1508 wxEvtHandler *popped = m_window->PopEventHandler(false);
1509 wxCHECK_RET(popped == this,
1510 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1511 }
1512
1513 bool wxEventBlocker::ProcessEvent(wxEvent& event)
1514 {
1515 // should this event be blocked?
1516 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1517 {
1518 wxEventType t = (wxEventType)m_eventsToBlock[i];
1519 if ( t == wxEVT_ANY || t == event.GetEventType() )
1520 return true; // yes, it should: mark this event as processed
1521 }
1522
1523 return false;
1524 }
1525
1526 #endif // wxUSE_GUI