fix more Borland release build warnings about unused variable/code without effect
[wxWidgets.git] / src / common / event.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/event.cpp
3 // Purpose: Event classes
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #include "wx/event.h"
28 #include "wx/evtloop.h"
29
30 #ifndef WX_PRECOMP
31 #include "wx/list.h"
32 #include "wx/app.h"
33 #include "wx/utils.h"
34 #include "wx/stopwatch.h"
35 #include "wx/module.h"
36
37 #if wxUSE_GUI
38 #include "wx/window.h"
39 #include "wx/control.h"
40 #include "wx/dc.h"
41 #include "wx/textctrl.h"
42 #include "wx/validate.h"
43 #endif // wxUSE_GUI
44 #endif
45
46 #include "wx/thread.h"
47
48 #if wxUSE_BASE
49 #include "wx/ptr_scpd.h"
50
51 wxDECLARE_SCOPED_PTR(wxEvent, wxEventPtr)
52 wxDEFINE_SCOPED_PTR(wxEvent, wxEventPtr)
53 #endif // wxUSE_BASE
54
55 // ----------------------------------------------------------------------------
56 // wxWin macros
57 // ----------------------------------------------------------------------------
58
59 #if wxUSE_BASE
60 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
61 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
62 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
63 #endif // wxUSE_BASE
64
65 #if wxUSE_GUI
66 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
67 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
69 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
70 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
71 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxNcPaintEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
78 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
80 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
81 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
88 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
89 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
90 IMPLEMENT_DYNAMIC_CLASS(wxDisplayChangedEvent, wxEvent)
91 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
92 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxEvent)
93 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
94 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
95 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
96 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
97 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
98 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
99 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureChangedEvent, wxEvent)
100 IMPLEMENT_DYNAMIC_CLASS(wxMouseCaptureLostEvent, wxEvent)
101 IMPLEMENT_DYNAMIC_CLASS(wxClipboardTextEvent, wxCommandEvent)
102 #endif // wxUSE_GUI
103
104 #if wxUSE_BASE
105
106 const wxEventTable *wxEvtHandler::GetEventTable() const
107 { return &wxEvtHandler::sm_eventTable; }
108
109 const wxEventTable wxEvtHandler::sm_eventTable =
110 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
111
112 wxEventHashTable &wxEvtHandler::GetEventHashTable() const
113 { return wxEvtHandler::sm_eventHashTable; }
114
115 wxEventHashTable wxEvtHandler::sm_eventHashTable(wxEvtHandler::sm_eventTable);
116
117 const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
118 { DECLARE_EVENT_TABLE_ENTRY(wxEVT_NULL, 0, 0, (wxObjectEventFunction)NULL, NULL) };
119
120
121 // wxUSE_MEMORY_TRACING considers memory freed from the static objects dtors
122 // leaked, so we need to manually clean up all event tables before checking for
123 // the memory leaks when using it, however this breaks re-initializing the
124 // library (i.e. repeated calls to wxInitialize/wxUninitialize) because the
125 // event tables won't be rebuilt the next time, so disable this by default
126 #if defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING
127
128 class wxEventTableEntryModule: public wxModule
129 {
130 public:
131 wxEventTableEntryModule() { }
132 virtual bool OnInit() { return true; }
133 virtual void OnExit() { wxEventHashTable::ClearAll(); }
134
135 DECLARE_DYNAMIC_CLASS(wxEventTableEntryModule)
136 };
137
138 IMPLEMENT_DYNAMIC_CLASS(wxEventTableEntryModule, wxModule)
139
140 #endif // __WXDEBUG__ && wxUSE_MEMORY_TRACING
141
142 // ----------------------------------------------------------------------------
143 // global variables
144 // ----------------------------------------------------------------------------
145
146 // List containing event handlers with pending events (each handler can occur
147 // at most once here)
148 wxList *wxPendingEvents = NULL;
149
150 #if wxUSE_THREADS
151 // protects wxPendingEvents list
152 wxCriticalSection *wxPendingEventsLocker = NULL;
153 #endif
154
155 // common event types are defined here, other event types are defined by the
156 // components which use them
157
158 const wxEventType wxEVT_FIRST = 10000;
159 const wxEventType wxEVT_USER_FIRST = wxEVT_FIRST + 2000;
160
161 DEFINE_EVENT_TYPE(wxEVT_NULL)
162 DEFINE_EVENT_TYPE(wxEVT_IDLE)
163 DEFINE_EVENT_TYPE(wxEVT_SOCKET)
164 DEFINE_EVENT_TYPE(wxEVT_TIMER)
165
166 #endif // wxUSE_BASE
167
168 #if wxUSE_GUI
169
170 DEFINE_EVENT_TYPE(wxEVT_COMMAND_BUTTON_CLICKED)
171 DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKBOX_CLICKED)
172 DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHOICE_SELECTED)
173 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_SELECTED)
174 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED)
175 DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED)
176 DEFINE_EVENT_TYPE(wxEVT_COMMAND_MENU_SELECTED)
177 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SLIDER_UPDATED)
178 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBOX_SELECTED)
179 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBUTTON_SELECTED)
180 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SCROLLBAR_UPDATED)
181 DEFINE_EVENT_TYPE(wxEVT_COMMAND_VLBOX_SELECTED)
182 DEFINE_EVENT_TYPE(wxEVT_COMMAND_COMBOBOX_SELECTED)
183 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_RCLICKED)
184 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_ENTER)
185 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRL_UPDATED)
186 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRLDOUBLE_UPDATED)
187 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_DROPDOWN_CLICKED)
188
189 // Mouse event types
190 DEFINE_EVENT_TYPE(wxEVT_LEFT_DOWN)
191 DEFINE_EVENT_TYPE(wxEVT_LEFT_UP)
192 DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DOWN)
193 DEFINE_EVENT_TYPE(wxEVT_MIDDLE_UP)
194 DEFINE_EVENT_TYPE(wxEVT_RIGHT_DOWN)
195 DEFINE_EVENT_TYPE(wxEVT_RIGHT_UP)
196 DEFINE_EVENT_TYPE(wxEVT_MOTION)
197 DEFINE_EVENT_TYPE(wxEVT_ENTER_WINDOW)
198 DEFINE_EVENT_TYPE(wxEVT_LEAVE_WINDOW)
199 DEFINE_EVENT_TYPE(wxEVT_LEFT_DCLICK)
200 DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DCLICK)
201 DEFINE_EVENT_TYPE(wxEVT_RIGHT_DCLICK)
202 DEFINE_EVENT_TYPE(wxEVT_SET_FOCUS)
203 DEFINE_EVENT_TYPE(wxEVT_KILL_FOCUS)
204 DEFINE_EVENT_TYPE(wxEVT_CHILD_FOCUS)
205 DEFINE_EVENT_TYPE(wxEVT_MOUSEWHEEL)
206 DEFINE_EVENT_TYPE(wxEVT_AUX1_DOWN)
207 DEFINE_EVENT_TYPE(wxEVT_AUX1_UP)
208 DEFINE_EVENT_TYPE(wxEVT_AUX1_DCLICK)
209 DEFINE_EVENT_TYPE(wxEVT_AUX2_DOWN)
210 DEFINE_EVENT_TYPE(wxEVT_AUX2_UP)
211 DEFINE_EVENT_TYPE(wxEVT_AUX2_DCLICK)
212
213 // Non-client mouse events
214 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DOWN)
215 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_UP)
216 DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DOWN)
217 DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_UP)
218 DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DOWN)
219 DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_UP)
220 DEFINE_EVENT_TYPE(wxEVT_NC_MOTION)
221 DEFINE_EVENT_TYPE(wxEVT_NC_ENTER_WINDOW)
222 DEFINE_EVENT_TYPE(wxEVT_NC_LEAVE_WINDOW)
223 DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DCLICK)
224 DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DCLICK)
225 DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DCLICK)
226
227 // Character input event type
228 DEFINE_EVENT_TYPE(wxEVT_CHAR)
229 DEFINE_EVENT_TYPE(wxEVT_CHAR_HOOK)
230 DEFINE_EVENT_TYPE(wxEVT_NAVIGATION_KEY)
231 DEFINE_EVENT_TYPE(wxEVT_KEY_DOWN)
232 DEFINE_EVENT_TYPE(wxEVT_KEY_UP)
233 #if wxUSE_HOTKEY
234 DEFINE_EVENT_TYPE(wxEVT_HOTKEY)
235 #endif
236
237 // Set cursor event
238 DEFINE_EVENT_TYPE(wxEVT_SET_CURSOR)
239
240 // wxScrollbar and wxSlider event identifiers
241 DEFINE_EVENT_TYPE(wxEVT_SCROLL_TOP)
242 DEFINE_EVENT_TYPE(wxEVT_SCROLL_BOTTOM)
243 DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEUP)
244 DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEDOWN)
245 DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEUP)
246 DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEDOWN)
247 DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBTRACK)
248 DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBRELEASE)
249 DEFINE_EVENT_TYPE(wxEVT_SCROLL_CHANGED)
250
251 // Scroll events from wxWindow
252 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_TOP)
253 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_BOTTOM)
254 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEUP)
255 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEDOWN)
256 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEUP)
257 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEDOWN)
258 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBTRACK)
259 DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBRELEASE)
260
261 // System events
262 DEFINE_EVENT_TYPE(wxEVT_SIZE)
263 DEFINE_EVENT_TYPE(wxEVT_SIZING)
264 DEFINE_EVENT_TYPE(wxEVT_MOVE)
265 DEFINE_EVENT_TYPE(wxEVT_MOVING)
266 DEFINE_EVENT_TYPE(wxEVT_MOVE_START)
267 DEFINE_EVENT_TYPE(wxEVT_MOVE_END)
268 DEFINE_EVENT_TYPE(wxEVT_CLOSE_WINDOW)
269 DEFINE_EVENT_TYPE(wxEVT_END_SESSION)
270 DEFINE_EVENT_TYPE(wxEVT_QUERY_END_SESSION)
271 DEFINE_EVENT_TYPE(wxEVT_HIBERNATE)
272 DEFINE_EVENT_TYPE(wxEVT_ACTIVATE_APP)
273 DEFINE_EVENT_TYPE(wxEVT_ACTIVATE)
274 DEFINE_EVENT_TYPE(wxEVT_CREATE)
275 DEFINE_EVENT_TYPE(wxEVT_DESTROY)
276 DEFINE_EVENT_TYPE(wxEVT_SHOW)
277 DEFINE_EVENT_TYPE(wxEVT_ICONIZE)
278 DEFINE_EVENT_TYPE(wxEVT_MAXIMIZE)
279 DEFINE_EVENT_TYPE(wxEVT_MOUSE_CAPTURE_CHANGED)
280 DEFINE_EVENT_TYPE(wxEVT_MOUSE_CAPTURE_LOST)
281 DEFINE_EVENT_TYPE(wxEVT_PAINT)
282 DEFINE_EVENT_TYPE(wxEVT_ERASE_BACKGROUND)
283 DEFINE_EVENT_TYPE(wxEVT_NC_PAINT)
284 DEFINE_EVENT_TYPE(wxEVT_PAINT_ICON)
285 DEFINE_EVENT_TYPE(wxEVT_MENU_OPEN)
286 DEFINE_EVENT_TYPE(wxEVT_MENU_CLOSE)
287 DEFINE_EVENT_TYPE(wxEVT_MENU_HIGHLIGHT)
288 DEFINE_EVENT_TYPE(wxEVT_CONTEXT_MENU)
289 DEFINE_EVENT_TYPE(wxEVT_SYS_COLOUR_CHANGED)
290 DEFINE_EVENT_TYPE(wxEVT_DISPLAY_CHANGED)
291 DEFINE_EVENT_TYPE(wxEVT_SETTING_CHANGED)
292 DEFINE_EVENT_TYPE(wxEVT_QUERY_NEW_PALETTE)
293 DEFINE_EVENT_TYPE(wxEVT_PALETTE_CHANGED)
294 DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_DOWN)
295 DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_UP)
296 DEFINE_EVENT_TYPE(wxEVT_JOY_MOVE)
297 DEFINE_EVENT_TYPE(wxEVT_JOY_ZMOVE)
298 DEFINE_EVENT_TYPE(wxEVT_DROP_FILES)
299 DEFINE_EVENT_TYPE(wxEVT_DRAW_ITEM)
300 DEFINE_EVENT_TYPE(wxEVT_MEASURE_ITEM)
301 DEFINE_EVENT_TYPE(wxEVT_COMPARE_ITEM)
302 DEFINE_EVENT_TYPE(wxEVT_INIT_DIALOG)
303 DEFINE_EVENT_TYPE(wxEVT_UPDATE_UI)
304
305 // Clipboard events
306 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_COPY)
307 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_CUT)
308 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_PASTE)
309
310 // Generic command events
311 // Note: a click is a higher-level event than button down/up
312 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_CLICK)
313 DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_DCLICK)
314 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_CLICK)
315 DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_DCLICK)
316 DEFINE_EVENT_TYPE(wxEVT_COMMAND_SET_FOCUS)
317 DEFINE_EVENT_TYPE(wxEVT_COMMAND_KILL_FOCUS)
318 DEFINE_EVENT_TYPE(wxEVT_COMMAND_ENTER)
319
320 // Help events
321 DEFINE_EVENT_TYPE(wxEVT_HELP)
322 DEFINE_EVENT_TYPE(wxEVT_DETAILED_HELP)
323
324 #endif // wxUSE_GUI
325
326 #if wxUSE_BASE
327
328 wxIdleMode wxIdleEvent::sm_idleMode = wxIDLE_PROCESS_ALL;
329
330 // ============================================================================
331 // implementation
332 // ============================================================================
333
334 // ----------------------------------------------------------------------------
335 // event initialization
336 // ----------------------------------------------------------------------------
337
338 int wxNewEventType()
339 {
340 // MT-FIXME
341 static int s_lastUsedEventType = wxEVT_FIRST;
342
343 return s_lastUsedEventType++;
344 }
345
346 // ----------------------------------------------------------------------------
347 // wxEvent
348 // ----------------------------------------------------------------------------
349
350 /*
351 * General wxWidgets events, covering
352 * all interesting things that might happen (button clicking, resizing,
353 * setting text in widgets, etc.).
354 *
355 * For each completely new event type, derive a new event class.
356 *
357 */
358
359 wxEvent::wxEvent(int theId, wxEventType commandType )
360 {
361 m_eventType = commandType;
362 m_eventObject = (wxObject *) NULL;
363 m_timeStamp = 0;
364 m_id = theId;
365 m_skipped = false;
366 m_callbackUserData = (wxObject *) NULL;
367 m_isCommandEvent = false;
368 m_propagationLevel = wxEVENT_PROPAGATE_NONE;
369 }
370
371 wxEvent::wxEvent(const wxEvent& src)
372 : wxObject(src)
373 , m_eventObject(src.m_eventObject)
374 , m_eventType(src.m_eventType)
375 , m_timeStamp(src.m_timeStamp)
376 , m_id(src.m_id)
377 , m_callbackUserData(src.m_callbackUserData)
378 , m_propagationLevel(src.m_propagationLevel)
379 , m_skipped(src.m_skipped)
380 , m_isCommandEvent(src.m_isCommandEvent)
381 {
382 }
383
384 wxEvent& wxEvent::operator=(const wxEvent& src)
385 {
386 wxObject::operator=(src);
387
388 m_eventObject = src.m_eventObject;
389 m_eventType = src.m_eventType;
390 m_timeStamp = src.m_timeStamp;
391 m_id = src.m_id;
392 m_callbackUserData = src.m_callbackUserData;
393 m_propagationLevel = src.m_propagationLevel;
394 m_skipped = src.m_skipped;
395 m_isCommandEvent = src.m_isCommandEvent;
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 = (char *) NULL;
418 m_clientObject = (wxClientData *) 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 if ( wxEvtHandler::
913 ProcessEventIfMatches(*eventEntryTable[n], self, event) )
914 {
915 return true;
916 }
917 }
918 }
919
920 return false;
921 }
922
923 void wxEventHashTable::InitHashTable()
924 {
925 // Loop over the event tables and all its base tables.
926 const wxEventTable *table = &m_table;
927 while (table)
928 {
929 // Retrieve all valid event handler entries
930 const wxEventTableEntry *entry = table->entries;
931 while (entry->m_fn != 0)
932 {
933 // Add the event entry in the Hash.
934 AddEntry(*entry);
935
936 entry++;
937 }
938
939 table = table->baseTable;
940 }
941
942 // Lets free some memory.
943 size_t i;
944 for(i = 0; i < m_size; i++)
945 {
946 EventTypeTablePointer eTTnode = m_eventTypeTable[i];
947 if (eTTnode)
948 {
949 eTTnode->eventEntryTable.Shrink();
950 }
951 }
952 }
953
954 void wxEventHashTable::AddEntry(const wxEventTableEntry &entry)
955 {
956 // This might happen 'accidentally' as the app is exiting
957 if (!m_eventTypeTable)
958 return;
959
960 EventTypeTablePointer *peTTnode = &m_eventTypeTable[entry.m_eventType % m_size];
961 EventTypeTablePointer eTTnode = *peTTnode;
962
963 if (eTTnode)
964 {
965 if (eTTnode->eventType != entry.m_eventType)
966 {
967 // Resize the table!
968 GrowEventTypeTable();
969 // Try again to add it.
970 AddEntry(entry);
971 return;
972 }
973 }
974 else
975 {
976 eTTnode = new EventTypeTable;
977 eTTnode->eventType = entry.m_eventType;
978 *peTTnode = eTTnode;
979 }
980
981 // Fill all hash entries between entry.m_id and entry.m_lastId...
982 eTTnode->eventEntryTable.Add(&entry);
983 }
984
985 void wxEventHashTable::AllocEventTypeTable(size_t size)
986 {
987 m_eventTypeTable = new EventTypeTablePointer[size];
988 memset((void *)m_eventTypeTable, 0, sizeof(EventTypeTablePointer)*size);
989 m_size = size;
990 }
991
992 void wxEventHashTable::GrowEventTypeTable()
993 {
994 size_t oldSize = m_size;
995 EventTypeTablePointer *oldEventTypeTable = m_eventTypeTable;
996
997 // TODO: Search the most optimal grow sequence
998 AllocEventTypeTable(/* GetNextPrime(oldSize) */oldSize*2+1);
999
1000 for ( size_t i = 0; i < oldSize; /* */ )
1001 {
1002 EventTypeTablePointer eTToldNode = oldEventTypeTable[i];
1003 if (eTToldNode)
1004 {
1005 EventTypeTablePointer *peTTnode = &m_eventTypeTable[eTToldNode->eventType % m_size];
1006 EventTypeTablePointer eTTnode = *peTTnode;
1007
1008 // Check for collision, we don't want any.
1009 if (eTTnode)
1010 {
1011 GrowEventTypeTable();
1012 continue; // Don't increment the counter,
1013 // as we still need to add this element.
1014 }
1015 else
1016 {
1017 // Get the old value and put it in the new table.
1018 *peTTnode = oldEventTypeTable[i];
1019 }
1020 }
1021
1022 i++;
1023 }
1024
1025 delete[] oldEventTypeTable;
1026 }
1027
1028 // ----------------------------------------------------------------------------
1029 // wxEvtHandler
1030 // ----------------------------------------------------------------------------
1031
1032 /*
1033 * Event handler
1034 */
1035
1036 wxEvtHandler::wxEvtHandler()
1037 {
1038 m_nextHandler = (wxEvtHandler *) NULL;
1039 m_previousHandler = (wxEvtHandler *) NULL;
1040 m_enabled = true;
1041 m_dynamicEvents = (wxList *) NULL;
1042 m_pendingEvents = (wxList *) NULL;
1043
1044 // no client data (yet)
1045 m_clientData = NULL;
1046 m_clientDataType = wxClientData_None;
1047 }
1048
1049 wxEvtHandler::~wxEvtHandler()
1050 {
1051 // Takes itself out of the list of handlers
1052 if (m_previousHandler)
1053 m_previousHandler->m_nextHandler = m_nextHandler;
1054
1055 if (m_nextHandler)
1056 m_nextHandler->m_previousHandler = m_previousHandler;
1057
1058 if (m_dynamicEvents)
1059 {
1060 for ( wxList::iterator it = m_dynamicEvents->begin(),
1061 end = m_dynamicEvents->end();
1062 it != end;
1063 ++it )
1064 {
1065 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)*it;
1066
1067 // Remove ourselves from sink destructor notifications
1068 // (this has usually been been done, in wxTrackable destructor)
1069 wxEvtHandler *eventSink = entry->m_eventSink;
1070 if ( eventSink )
1071 {
1072 wxEventConnectionRef * const
1073 evtConnRef = FindRefInTrackerList(eventSink);
1074 if ( evtConnRef )
1075 {
1076 eventSink->RemoveNode(evtConnRef);
1077 delete evtConnRef;
1078 }
1079 }
1080
1081 if (entry->m_callbackUserData)
1082 delete entry->m_callbackUserData;
1083 delete entry;
1084 }
1085 delete m_dynamicEvents;
1086 };
1087
1088 if (m_pendingEvents)
1089 m_pendingEvents->DeleteContents(true);
1090 delete m_pendingEvents;
1091
1092 // Remove us from wxPendingEvents if necessary.
1093 if ( wxPendingEvents )
1094 {
1095 #if wxUSE_THREADS
1096 if (wxPendingEventsLocker)
1097 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1098 #endif
1099
1100 if ( wxPendingEvents->DeleteObject(this) )
1101 {
1102 // check that we were present only once in the list
1103 wxASSERT_MSG( !wxPendingEvents->Find(this),
1104 "Handler occurs twice in wxPendingEvents list" );
1105 }
1106 //else: we weren't in this list at all, it's ok
1107
1108 #if wxUSE_THREADS
1109 if (wxPendingEventsLocker)
1110 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1111 #endif
1112 }
1113
1114 // we only delete object data, not untyped
1115 if ( m_clientDataType == wxClientData_Object )
1116 delete m_clientObject;
1117 }
1118
1119 #if wxUSE_THREADS
1120
1121 bool wxEvtHandler::ProcessThreadEvent(const wxEvent& event)
1122 {
1123 // check that we are really in a child thread
1124 wxASSERT_MSG( !wxThread::IsMain(),
1125 wxT("use ProcessEvent() in main thread") );
1126
1127 AddPendingEvent(event);
1128
1129 return true;
1130 }
1131
1132 #endif // wxUSE_THREADS
1133
1134 void wxEvtHandler::QueueEvent(wxEvent *event)
1135 {
1136 wxCHECK_RET( event, "NULL event can't be posted" );
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 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1147
1148 // 2) Add this event handler to list of event handlers that
1149 // have pending events.
1150
1151 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1152
1153 if ( !wxPendingEvents )
1154 wxPendingEvents = new wxList;
1155 if ( !wxPendingEvents->Find(this) )
1156 wxPendingEvents->Append(this);
1157
1158 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1159
1160 // 3) Inform the system that new pending events are somewhere,
1161 // and that these should be processed in idle time.
1162 wxWakeUpIdle();
1163 }
1164
1165 void wxEvtHandler::ProcessPendingEvents()
1166 {
1167 wxENTER_CRIT_SECT( m_pendingEventsLock );
1168
1169 // this method is only called by wxApp if this handler does have
1170 // pending events
1171 wxCHECK_RET( m_pendingEvents && !m_pendingEvents->IsEmpty(),
1172 "should have pending events if called" );
1173
1174 wxList::compatibility_iterator node = m_pendingEvents->GetFirst();
1175 wxEventPtr event(wx_static_cast(wxEvent *, node->GetData()));
1176
1177 // it's important we remove event from list before processing it, else a
1178 // nested event loop, for example from a modal dialog, might process the
1179 // same event again.
1180 m_pendingEvents->Erase(node);
1181
1182 // if there are no more pending events left, we don't need to stay in this
1183 // list
1184 if ( m_pendingEvents->IsEmpty() )
1185 {
1186 #if wxUSE_THREADS
1187 if (wxPendingEventsLocker)
1188 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
1189 #endif
1190 wxPendingEvents->DeleteObject(this);
1191 #if wxUSE_THREADS
1192 if (wxPendingEventsLocker)
1193 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
1194 #endif
1195 }
1196
1197 wxLEAVE_CRIT_SECT( m_pendingEventsLock );
1198
1199 ProcessEvent(*event);
1200
1201 // careful: this object could have been deleted by the event handler
1202 // executed by the above ProcessEvent() call, so we can't access any fields
1203 // of this object any more
1204 }
1205
1206 /*
1207 * Event table stuff
1208 */
1209 /* static */ bool
1210 wxEvtHandler::ProcessEventIfMatches(const wxEventTableEntryBase& entry,
1211 wxEvtHandler *handler,
1212 wxEvent& event)
1213 {
1214 int tableId1 = entry.m_id,
1215 tableId2 = entry.m_lastId;
1216
1217 // match only if the event type is the same and the id is either -1 in
1218 // the event table (meaning "any") or the event id matches the id
1219 // specified in the event table either exactly or by falling into
1220 // range between first and last
1221 if ((tableId1 == wxID_ANY) ||
1222 (tableId2 == wxID_ANY && tableId1 == event.GetId()) ||
1223 (tableId2 != wxID_ANY &&
1224 (event.GetId() >= tableId1 && event.GetId() <= tableId2)))
1225 {
1226 event.Skip(false);
1227 event.m_callbackUserData = entry.m_callbackUserData;
1228
1229 #if wxUSE_EXCEPTIONS
1230 if ( wxTheApp )
1231 {
1232 // call the handler via wxApp method which allows the user to catch
1233 // any exceptions which may be thrown by any handler in the program
1234 // in one place
1235 wxTheApp->HandleEvent(handler, (wxEventFunction)entry.m_fn, event);
1236 }
1237 else
1238 #endif // wxUSE_EXCEPTIONS
1239 {
1240 // no need for an extra virtual function call
1241 (handler->*((wxEventFunction) (entry.m_fn)))(event);
1242 }
1243
1244 if (!event.GetSkipped())
1245 return true;
1246 }
1247
1248 return false;
1249 }
1250
1251 bool wxEvtHandler::TryParent(wxEvent& event)
1252 {
1253 if ( GetNextHandler() )
1254 {
1255 // the next handler will pass it to wxTheApp if it doesn't process it,
1256 // so return from here to avoid doing it again
1257 return GetNextHandler()->TryParent(event);
1258 }
1259
1260 if ( wxTheApp && (this != wxTheApp) )
1261 {
1262 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
1263 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
1264 // processed appropriately via SearchEventTable.
1265 if ( event.GetEventType() != wxEVT_IDLE )
1266 {
1267 if ( wxTheApp->ProcessEvent(event) )
1268 return true;
1269 }
1270 }
1271
1272 return false;
1273 }
1274
1275 bool wxEvtHandler::ProcessEvent(wxEvent& event)
1276 {
1277 // allow the application to hook into event processing
1278 if ( wxTheApp )
1279 {
1280 int rc = wxTheApp->FilterEvent(event);
1281 if ( rc != -1 )
1282 {
1283 wxASSERT_MSG( rc == 1 || rc == 0,
1284 _T("unexpected wxApp::FilterEvent return value") );
1285
1286 return rc != 0;
1287 }
1288 //else: proceed normally
1289 }
1290
1291 if ( ProcessEventHere(event) )
1292 return true;
1293
1294 // propagate the event upwards the window chain and/or to the application
1295 // object if it wasn't processed at this level
1296 return TryParent(event);
1297 }
1298
1299 bool wxEvtHandler::ProcessEventHere(wxEvent& event)
1300 {
1301 // An event handler can be enabled or disabled
1302 if ( GetEvtHandlerEnabled() )
1303 {
1304 // if we have a validator, it has higher priority than our own event
1305 // table
1306 if ( TryValidator(event) )
1307 return true;
1308
1309 // Handle per-instance dynamic event tables first
1310 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
1311 return true;
1312
1313 // Then static per-class event tables
1314 if ( GetEventHashTable().HandleEvent(event, this) )
1315 return true;
1316 }
1317
1318 // Try going down the event handler chain
1319 if ( GetNextHandler() && GetNextHandler()->ProcessEventHere(event) )
1320 return true;
1321
1322 // We don't have a handler for this event.
1323 return false;
1324 }
1325
1326 bool wxEvtHandler::SafelyProcessEvent(wxEvent& event)
1327 {
1328 #if wxUSE_EXCEPTIONS
1329 try
1330 {
1331 #endif
1332 return ProcessEvent(event);
1333 #if wxUSE_EXCEPTIONS
1334 }
1335 catch ( ... )
1336 {
1337 wxEventLoopBase *loop = wxEventLoopBase::GetActive();
1338 try
1339 {
1340 if ( !wxTheApp || !wxTheApp->OnExceptionInMainLoop() )
1341 {
1342 if ( loop )
1343 loop->Exit();
1344 }
1345 //else: continue running current event loop
1346
1347 return false;
1348 }
1349 catch ( ... )
1350 {
1351 // OnExceptionInMainLoop() threw, possibly rethrowing the same
1352 // exception again: very good, but we still need Exit() to
1353 // be called
1354 if ( loop )
1355 loop->Exit();
1356 throw;
1357 }
1358 }
1359 #endif // wxUSE_EXCEPTIONS
1360 }
1361
1362
1363 bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
1364 {
1365 const wxEventType eventType = event.GetEventType();
1366 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
1367 {
1368 const wxEventTableEntry& entry = table.entries[i];
1369 if ( eventType == entry.m_eventType )
1370 {
1371 if ( ProcessEventIfMatches(entry, this, event) )
1372 return true;
1373 }
1374 }
1375
1376 return false;
1377 }
1378
1379 void wxEvtHandler::Connect( int id, int lastId,
1380 int eventType,
1381 wxObjectEventFunction func,
1382 wxObject *userData,
1383 wxEvtHandler* eventSink )
1384 {
1385 wxDynamicEventTableEntry *entry =
1386 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData, eventSink);
1387
1388 if (!m_dynamicEvents)
1389 m_dynamicEvents = new wxList;
1390
1391 // Insert at the front of the list so most recent additions are found first
1392 m_dynamicEvents->Insert( (wxObject*) entry );
1393
1394 // Make sure we get to know when a sink is destroyed
1395 if ( eventSink && eventSink != this )
1396 {
1397 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1398 if ( evtConnRef )
1399 evtConnRef->IncRef( );
1400 else
1401 new wxEventConnectionRef(this, eventSink);
1402 }
1403 }
1404
1405 bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
1406 wxObjectEventFunction func,
1407 wxObject *userData,
1408 wxEvtHandler* eventSink )
1409 {
1410 if (!m_dynamicEvents)
1411 return false;
1412
1413 // Remove connection from tracker node (wxEventConnectionRef)
1414 if ( eventSink && eventSink != this )
1415 {
1416 wxEventConnectionRef *evtConnRef = FindRefInTrackerList(eventSink);
1417 if ( evtConnRef )
1418 evtConnRef->DecRef();
1419 }
1420
1421 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1422 while (node)
1423 {
1424 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1425
1426 if ((entry->m_id == id) &&
1427 ((entry->m_lastId == lastId) || (lastId == wxID_ANY)) &&
1428 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
1429 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
1430 ((entry->m_eventSink == eventSink) || (eventSink == (wxEvtHandler*)NULL)) &&
1431 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
1432 {
1433 if (entry->m_callbackUserData)
1434 delete entry->m_callbackUserData;
1435 m_dynamicEvents->Erase( node );
1436 delete entry;
1437 return true;
1438 }
1439 node = node->GetNext();
1440 }
1441 return false;
1442 }
1443
1444 bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
1445 {
1446 wxCHECK_MSG( m_dynamicEvents, false,
1447 wxT("caller should check that we have dynamic events") );
1448
1449 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst();
1450 while (node)
1451 {
1452 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1453
1454 // get next node before (maybe) calling the event handler as it could
1455 // call Disconnect() invalidating the current node
1456 node = node->GetNext();
1457
1458 if ((event.GetEventType() == entry->m_eventType) && (entry->m_fn != 0))
1459 {
1460 wxEvtHandler *handler = entry->m_eventSink ? entry->m_eventSink
1461 : this;
1462 if ( ProcessEventIfMatches(*entry, handler, event) )
1463 return true;
1464 }
1465 }
1466
1467 return false;
1468 }
1469
1470 void wxEvtHandler::DoSetClientObject( wxClientData *data )
1471 {
1472 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1473 wxT("can't have both object and void client data") );
1474
1475 if ( m_clientObject )
1476 delete m_clientObject;
1477
1478 m_clientObject = data;
1479 m_clientDataType = wxClientData_Object;
1480 }
1481
1482 wxClientData *wxEvtHandler::DoGetClientObject() const
1483 {
1484 // it's not an error to call GetClientObject() on a window which doesn't
1485 // have client data at all - NULL will be returned
1486 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1487 wxT("this window doesn't have object client data") );
1488
1489 return m_clientObject;
1490 }
1491
1492 void wxEvtHandler::DoSetClientData( void *data )
1493 {
1494 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1495 wxT("can't have both object and void client data") );
1496
1497 m_clientData = data;
1498 m_clientDataType = wxClientData_Void;
1499 }
1500
1501 void *wxEvtHandler::DoGetClientData() const
1502 {
1503 // it's not an error to call GetClientData() on a window which doesn't have
1504 // client data at all - NULL will be returned
1505 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1506 wxT("this window doesn't have void client data") );
1507
1508 return m_clientData;
1509 }
1510
1511 // A helper to find an wxEventConnectionRef object
1512 wxEventConnectionRef *
1513 wxEvtHandler::FindRefInTrackerList(wxEvtHandler *eventSink)
1514 {
1515 for ( wxTrackerNode *node = eventSink->GetFirst(); node; node = node->m_nxt )
1516 {
1517 // we only want wxEventConnectionRef nodes here
1518 wxEventConnectionRef *evtConnRef = node->ToEventConnection();
1519 if ( evtConnRef && evtConnRef->m_src == this )
1520 {
1521 wxASSERT( evtConnRef->m_sink==eventSink );
1522 return evtConnRef;
1523 }
1524 }
1525
1526 return NULL;
1527 }
1528
1529 void wxEvtHandler::OnSinkDestroyed( wxEvtHandler *sink )
1530 {
1531 wxASSERT(m_dynamicEvents);
1532
1533 // remove all connections with this sink
1534 wxList::compatibility_iterator node = m_dynamicEvents->GetFirst(), node_nxt;
1535 while (node)
1536 {
1537 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->GetData();
1538 node_nxt = node->GetNext();
1539
1540 if ( entry->m_eventSink==sink )
1541 {
1542 if (entry->m_callbackUserData)
1543 delete entry->m_callbackUserData;
1544 m_dynamicEvents->Erase( node );
1545 delete entry;
1546 }
1547 node = node_nxt;
1548 }
1549 }
1550
1551 #endif // wxUSE_BASE
1552
1553 #if wxUSE_GUI
1554
1555 // Find a window with the focus, that is also a descendant of the given window.
1556 // This is used to determine the window to initially send commands to.
1557 wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1558 {
1559 // Process events starting with the window with the focus, if any.
1560 wxWindow* focusWin = wxWindow::FindFocus();
1561 wxWindow* win = focusWin;
1562
1563 // Check if this is a descendant of this frame.
1564 // If not, win will be set to NULL.
1565 while (win)
1566 {
1567 if (win == ancestor)
1568 break;
1569 else
1570 win = win->GetParent();
1571 }
1572 if (win == (wxWindow*) NULL)
1573 focusWin = (wxWindow*) NULL;
1574
1575 return focusWin;
1576 }
1577
1578 // ----------------------------------------------------------------------------
1579 // wxEventBlocker
1580 // ----------------------------------------------------------------------------
1581
1582 wxEventBlocker::wxEventBlocker(wxWindow *win, wxEventType type)
1583 {
1584 wxCHECK_RET(win, wxT("Null window given to wxEventBlocker"));
1585
1586 m_window = win;
1587
1588 Block(type);
1589 m_window->PushEventHandler(this);
1590 }
1591
1592 wxEventBlocker::~wxEventBlocker()
1593 {
1594 wxEvtHandler *popped = m_window->PopEventHandler(false);
1595 wxCHECK_RET(popped == this,
1596 wxT("Don't push other event handlers into a window managed by wxEventBlocker!"));
1597 }
1598
1599 bool wxEventBlocker::ProcessEvent(wxEvent& event)
1600 {
1601 // should this event be blocked?
1602 for ( size_t i = 0; i < m_eventsToBlock.size(); i++ )
1603 {
1604 wxEventType t = (wxEventType)m_eventsToBlock[i];
1605 if ( t == wxEVT_ANY || t == event.GetEventType() )
1606 return true; // yes, it should: mark this event as processed
1607 }
1608
1609 return false;
1610 }
1611
1612 #endif // wxUSE_GUI