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