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