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