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