]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
added a preImage of the selection in order to avoid unnecessary events being triggered
[wxWidgets.git] / src / common / event.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: 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 and Markus Holzem
e90c1d2a 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
8e193f38
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c801d85f 20#ifdef __GNUG__
0b746ba8 21 #pragma implementation "event.h"
c801d85f
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
0b746ba8 28 #pragma hdrstop
c801d85f
KB
29#endif
30
31#ifndef WX_PRECOMP
0b746ba8 32 #include "wx/defs.h"
0b746ba8 33 #include "wx/app.h"
e90c1d2a
VZ
34 #include "wx/list.h"
35
36 #if wxUSE_GUI
37 #include "wx/control.h"
38 #include "wx/utils.h"
39 #include "wx/dc.h"
40 #endif // wxUSE_GUI
c801d85f
KB
41#endif
42
43#include "wx/event.h"
e90c1d2a
VZ
44
45#if wxUSE_GUI
46 #include "wx/validate.h"
47#endif // wxUSE_GUI
c801d85f 48
8e193f38
VZ
49// ----------------------------------------------------------------------------
50// wxWin macros
51// ----------------------------------------------------------------------------
52
23f681ec
VZ
53IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
54IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
e90c1d2a 55
23f681ec 56#if wxUSE_GUI
6b55490a 57 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
23f681ec
VZ
58 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
59 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
60 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
61 IMPLEMENT_DYNAMIC_CLASS(wxScrollWinEvent, wxEvent)
62 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
63 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
64 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
65 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
1e6feb95 66 IMPLEMENT_DYNAMIC_CLASS(wxNcPaintEvent, wxEvent)
23f681ec
VZ
67 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
68 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
69 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
456bc6d9 70 IMPLEMENT_DYNAMIC_CLASS(wxChildFocusEvent, wxCommandEvent)
23f681ec
VZ
71 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
72 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
73 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
74 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
75 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
76 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
77 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
78 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
79 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
8e72b8b5 80 IMPLEMENT_DYNAMIC_CLASS(wxSetCursorEvent, wxEvent)
23f681ec
VZ
81 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
82 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
83 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
84 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
85 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
86 IMPLEMENT_DYNAMIC_CLASS(wxWindowCreateEvent, wxEvent)
87 IMPLEMENT_DYNAMIC_CLASS(wxWindowDestroyEvent, wxEvent)
b96340e6 88 IMPLEMENT_DYNAMIC_CLASS(wxHelpEvent, wxCommandEvent)
69231000 89 IMPLEMENT_DYNAMIC_CLASS(wxContextMenuEvent, wxCommandEvent)
23f681ec 90#endif // wxUSE_GUI
0b746ba8 91
23f681ec
VZ
92const wxEventTable *wxEvtHandler::GetEventTable() const
93 { return &wxEvtHandler::sm_eventTable; }
0b746ba8 94
23f681ec
VZ
95const wxEventTable wxEvtHandler::sm_eventTable =
96 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
0b746ba8 97
23f681ec 98const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
9aaf9bed 99 { DECLARE_EVENT_TABLE_ENTRY(wxEVT_NULL, 0, 0, (wxObjectEventFunction)NULL, NULL) };
c801d85f 100
8e193f38
VZ
101// ----------------------------------------------------------------------------
102// global variables
103// ----------------------------------------------------------------------------
104
105// To put pending event handlers
106wxList *wxPendingEvents = (wxList *)NULL;
107
7214297d 108#if wxUSE_THREADS
8e193f38
VZ
109 // protects wxPendingEvents list
110 wxCriticalSection *wxPendingEventsLocker = (wxCriticalSection *)NULL;
7214297d
GL
111#endif
112
cbee8f8d
VZ
113#if !WXWIN_COMPATIBILITY_EVENT_TYPES
114
2e4df4bf
VZ
115// common event types are defined here, other event types are defined by the
116// components which use them
117
461697c2 118DEFINE_EVENT_TYPE(wxEVT_NULL)
2e4df4bf
VZ
119DEFINE_EVENT_TYPE(wxEVT_COMMAND_BUTTON_CLICKED)
120DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKBOX_CLICKED)
121DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHOICE_SELECTED)
122DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_SELECTED)
123DEFINE_EVENT_TYPE(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED)
124DEFINE_EVENT_TYPE(wxEVT_COMMAND_CHECKLISTBOX_TOGGLED)
2e4df4bf
VZ
125DEFINE_EVENT_TYPE(wxEVT_COMMAND_MENU_SELECTED)
126DEFINE_EVENT_TYPE(wxEVT_COMMAND_SLIDER_UPDATED)
127DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBOX_SELECTED)
128DEFINE_EVENT_TYPE(wxEVT_COMMAND_RADIOBUTTON_SELECTED)
129DEFINE_EVENT_TYPE(wxEVT_COMMAND_SCROLLBAR_UPDATED)
130DEFINE_EVENT_TYPE(wxEVT_COMMAND_VLBOX_SELECTED)
131DEFINE_EVENT_TYPE(wxEVT_COMMAND_COMBOBOX_SELECTED)
132DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_RCLICKED)
133DEFINE_EVENT_TYPE(wxEVT_COMMAND_TOOL_ENTER)
134DEFINE_EVENT_TYPE(wxEVT_COMMAND_SPINCTRL_UPDATED)
0b5eceb6
VZ
135
136// Sockets and timers send events, too
2e4df4bf
VZ
137DEFINE_EVENT_TYPE(wxEVT_SOCKET)
138DEFINE_EVENT_TYPE(wxEVT_TIMER)
0b5eceb6
VZ
139
140// Mouse event types
2e4df4bf
VZ
141DEFINE_EVENT_TYPE(wxEVT_LEFT_DOWN)
142DEFINE_EVENT_TYPE(wxEVT_LEFT_UP)
143DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DOWN)
144DEFINE_EVENT_TYPE(wxEVT_MIDDLE_UP)
145DEFINE_EVENT_TYPE(wxEVT_RIGHT_DOWN)
146DEFINE_EVENT_TYPE(wxEVT_RIGHT_UP)
147DEFINE_EVENT_TYPE(wxEVT_MOTION)
148DEFINE_EVENT_TYPE(wxEVT_ENTER_WINDOW)
149DEFINE_EVENT_TYPE(wxEVT_LEAVE_WINDOW)
150DEFINE_EVENT_TYPE(wxEVT_LEFT_DCLICK)
151DEFINE_EVENT_TYPE(wxEVT_MIDDLE_DCLICK)
152DEFINE_EVENT_TYPE(wxEVT_RIGHT_DCLICK)
153DEFINE_EVENT_TYPE(wxEVT_SET_FOCUS)
154DEFINE_EVENT_TYPE(wxEVT_KILL_FOCUS)
456bc6d9 155DEFINE_EVENT_TYPE(wxEVT_CHILD_FOCUS)
d2c52078 156DEFINE_EVENT_TYPE(wxEVT_MOUSEWHEEL)
0b5eceb6
VZ
157
158// Non-client mouse events
2e4df4bf
VZ
159DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DOWN)
160DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_UP)
161DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DOWN)
162DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_UP)
163DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DOWN)
164DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_UP)
165DEFINE_EVENT_TYPE(wxEVT_NC_MOTION)
166DEFINE_EVENT_TYPE(wxEVT_NC_ENTER_WINDOW)
167DEFINE_EVENT_TYPE(wxEVT_NC_LEAVE_WINDOW)
168DEFINE_EVENT_TYPE(wxEVT_NC_LEFT_DCLICK)
169DEFINE_EVENT_TYPE(wxEVT_NC_MIDDLE_DCLICK)
170DEFINE_EVENT_TYPE(wxEVT_NC_RIGHT_DCLICK)
0b5eceb6
VZ
171
172// Character input event type
2e4df4bf
VZ
173DEFINE_EVENT_TYPE(wxEVT_CHAR)
174DEFINE_EVENT_TYPE(wxEVT_CHAR_HOOK)
175DEFINE_EVENT_TYPE(wxEVT_NAVIGATION_KEY)
176DEFINE_EVENT_TYPE(wxEVT_KEY_DOWN)
177DEFINE_EVENT_TYPE(wxEVT_KEY_UP)
0b5eceb6
VZ
178
179// Set cursor event
2e4df4bf 180DEFINE_EVENT_TYPE(wxEVT_SET_CURSOR)
0b5eceb6
VZ
181
182// wxScrollbar and wxSlider event identifiers
2e4df4bf
VZ
183DEFINE_EVENT_TYPE(wxEVT_SCROLL_TOP)
184DEFINE_EVENT_TYPE(wxEVT_SCROLL_BOTTOM)
185DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEUP)
186DEFINE_EVENT_TYPE(wxEVT_SCROLL_LINEDOWN)
187DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEUP)
188DEFINE_EVENT_TYPE(wxEVT_SCROLL_PAGEDOWN)
189DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBTRACK)
190DEFINE_EVENT_TYPE(wxEVT_SCROLL_THUMBRELEASE)
0b5eceb6
VZ
191
192// Scroll events from wxWindow
2e4df4bf
VZ
193DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_TOP)
194DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_BOTTOM)
195DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEUP)
196DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_LINEDOWN)
197DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEUP)
198DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_PAGEDOWN)
199DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBTRACK)
200DEFINE_EVENT_TYPE(wxEVT_SCROLLWIN_THUMBRELEASE)
0b5eceb6
VZ
201
202// System events
2e4df4bf
VZ
203DEFINE_EVENT_TYPE(wxEVT_SIZE)
204DEFINE_EVENT_TYPE(wxEVT_MOVE)
205DEFINE_EVENT_TYPE(wxEVT_CLOSE_WINDOW)
206DEFINE_EVENT_TYPE(wxEVT_END_SESSION)
207DEFINE_EVENT_TYPE(wxEVT_QUERY_END_SESSION)
208DEFINE_EVENT_TYPE(wxEVT_ACTIVATE_APP)
209DEFINE_EVENT_TYPE(wxEVT_POWER)
210DEFINE_EVENT_TYPE(wxEVT_ACTIVATE)
211DEFINE_EVENT_TYPE(wxEVT_CREATE)
212DEFINE_EVENT_TYPE(wxEVT_DESTROY)
213DEFINE_EVENT_TYPE(wxEVT_SHOW)
214DEFINE_EVENT_TYPE(wxEVT_ICONIZE)
215DEFINE_EVENT_TYPE(wxEVT_MAXIMIZE)
216DEFINE_EVENT_TYPE(wxEVT_MOUSE_CAPTURE_CHANGED)
217DEFINE_EVENT_TYPE(wxEVT_PAINT)
218DEFINE_EVENT_TYPE(wxEVT_ERASE_BACKGROUND)
219DEFINE_EVENT_TYPE(wxEVT_NC_PAINT)
220DEFINE_EVENT_TYPE(wxEVT_PAINT_ICON)
221DEFINE_EVENT_TYPE(wxEVT_MENU_CHAR)
222DEFINE_EVENT_TYPE(wxEVT_MENU_INIT)
223DEFINE_EVENT_TYPE(wxEVT_MENU_HIGHLIGHT)
224DEFINE_EVENT_TYPE(wxEVT_POPUP_MENU_INIT)
225DEFINE_EVENT_TYPE(wxEVT_CONTEXT_MENU)
226DEFINE_EVENT_TYPE(wxEVT_SYS_COLOUR_CHANGED)
227DEFINE_EVENT_TYPE(wxEVT_SETTING_CHANGED)
228DEFINE_EVENT_TYPE(wxEVT_QUERY_NEW_PALETTE)
229DEFINE_EVENT_TYPE(wxEVT_PALETTE_CHANGED)
230DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_DOWN)
231DEFINE_EVENT_TYPE(wxEVT_JOY_BUTTON_UP)
232DEFINE_EVENT_TYPE(wxEVT_JOY_MOVE)
233DEFINE_EVENT_TYPE(wxEVT_JOY_ZMOVE)
234DEFINE_EVENT_TYPE(wxEVT_DROP_FILES)
235DEFINE_EVENT_TYPE(wxEVT_DRAW_ITEM)
236DEFINE_EVENT_TYPE(wxEVT_MEASURE_ITEM)
237DEFINE_EVENT_TYPE(wxEVT_COMPARE_ITEM)
238DEFINE_EVENT_TYPE(wxEVT_INIT_DIALOG)
239DEFINE_EVENT_TYPE(wxEVT_IDLE)
240DEFINE_EVENT_TYPE(wxEVT_UPDATE_UI)
0b5eceb6
VZ
241
242// Generic command events
243// Note: a click is a higher-level event than button down/up
2e4df4bf
VZ
244DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_CLICK)
245DEFINE_EVENT_TYPE(wxEVT_COMMAND_LEFT_DCLICK)
246DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_CLICK)
247DEFINE_EVENT_TYPE(wxEVT_COMMAND_RIGHT_DCLICK)
248DEFINE_EVENT_TYPE(wxEVT_COMMAND_SET_FOCUS)
249DEFINE_EVENT_TYPE(wxEVT_COMMAND_KILL_FOCUS)
250DEFINE_EVENT_TYPE(wxEVT_COMMAND_ENTER)
0b5eceb6
VZ
251
252// Help events
2e4df4bf
VZ
253DEFINE_EVENT_TYPE(wxEVT_HELP)
254DEFINE_EVENT_TYPE(wxEVT_DETAILED_HELP)
6164f93c
VZ
255
256#endif // !WXWIN_COMPATIBILITY_EVENT_TYPES
257
258// ============================================================================
259// implementation
260// ============================================================================
0b5eceb6 261
0b5eceb6
VZ
262// ----------------------------------------------------------------------------
263// event initialization
264// ----------------------------------------------------------------------------
265
266int wxNewEventType()
267{
268 // MT-FIXME
269 static int s_lastUsedEventType = wxEVT_FIRST;
270
cbee8f8d
VZ
271#if WXWIN_COMPATIBILITY_2
272 // check that we don't overlap with the user-defined types: if it does
273 // happen, the best solution is probably to update the existing code to
274 // use wxNewEventType() instead of wxEVT_USER_FIRST
6164f93c
VZ
275 //
276 // due to the uncertainty
cbee8f8d
VZ
277 wxASSERT_MSG( s_lastUsedEventType < wxEVT_USER_FIRST - 1,
278 _T("possible event type conflict") );
279#endif // WXWIN_COMPATIBILITY_2
280
0b5eceb6
VZ
281 return s_lastUsedEventType++;
282}
283
8e193f38
VZ
284// ----------------------------------------------------------------------------
285// wxEvent
286// ----------------------------------------------------------------------------
287
c801d85f
KB
288/*
289 * General wxWindows events, covering
290 * all interesting things that might happen (button clicking, resizing,
291 * setting text in widgets, etc.).
292 *
293 * For each completely new event type, derive a new event class.
294 *
295 */
296
8e72b8b5 297wxEvent::wxEvent(int theId, wxEventType commandType )
c801d85f 298{
8e72b8b5 299 m_eventType = commandType;
0b746ba8 300 m_eventObject = (wxObject *) NULL;
0b746ba8
VZ
301 m_timeStamp = 0;
302 m_id = theId;
303 m_skipped = FALSE;
304 m_callbackUserData = (wxObject *) NULL;
193bf013 305 m_isCommandEvent = FALSE;
c801d85f
KB
306}
307
8e72b8b5 308wxEvent::wxEvent(const wxEvent &src)
a737331d 309{
8e72b8b5
RR
310 m_eventType = src.m_eventType;
311 m_eventObject = src.m_eventObject;
312 m_timeStamp = src.m_timeStamp;
313 m_id = src.m_id;
314 m_skipped = src.m_skipped;
315 m_callbackUserData = src.m_callbackUserData;
316 m_isCommandEvent = src.m_isCommandEvent;
a737331d
GL
317}
318
e90c1d2a
VZ
319#if wxUSE_GUI
320
c801d85f
KB
321/*
322 * Command events
323 *
324 */
325
7798a18e 326wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
8e72b8b5 327 : wxEvent( theId, commandType )
c801d85f 328{
0b746ba8
VZ
329 m_clientData = (char *) NULL;
330 m_clientObject = (wxClientData *) NULL;
331 m_extraLong = 0;
332 m_commandInt = 0;
04392d70 333 m_commandString = wxEmptyString;
193bf013 334 m_isCommandEvent = TRUE;
c801d85f
KB
335}
336
337/*
338 * Scroll events
339 */
340
0b746ba8
VZ
341wxScrollEvent::wxScrollEvent(wxEventType commandType,
342 int id,
343 int pos,
344 int orient)
345 : wxCommandEvent(commandType, id)
c801d85f 346{
0b746ba8
VZ
347 m_extraLong = orient;
348 m_commandInt = pos;
c801d85f
KB
349}
350
d1367c3d
RR
351/*
352 * ScrollWin events
353 */
354
355wxScrollWinEvent::wxScrollWinEvent(wxEventType commandType,
356 int pos,
357 int orient)
d1367c3d 358{
c5b42c87 359 m_eventType = commandType;
d1367c3d
RR
360 m_extraLong = orient;
361 m_commandInt = pos;
362}
363
c801d85f
KB
364/*
365 * Mouse events
366 *
367 */
368
7798a18e 369wxMouseEvent::wxMouseEvent(wxEventType commandType)
c801d85f 370{
0b746ba8
VZ
371 m_eventType = commandType;
372 m_metaDown = FALSE;
373 m_altDown = FALSE;
374 m_controlDown = FALSE;
375 m_shiftDown = FALSE;
376 m_leftDown = FALSE;
377 m_rightDown = FALSE;
378 m_middleDown = FALSE;
379 m_x = 0;
380 m_y = 0;
d2c52078
RD
381 m_wheelRotation = 0;
382 m_wheelDelta = 0;
383 m_linesPerAction = 0;
c801d85f
KB
384}
385
386// True if was a button dclick event (1 = left, 2 = middle, 3 = right)
387// or any button dclick event (but = -1)
388bool wxMouseEvent::ButtonDClick(int but) const
389{
0b746ba8
VZ
390 switch (but)
391 {
392 case -1:
393 return (LeftDClick() || MiddleDClick() || RightDClick());
394 case 1:
395 return LeftDClick();
396 case 2:
397 return MiddleDClick();
398 case 3:
399 return RightDClick();
400 default:
223d09f6 401 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDClick"));
0b746ba8
VZ
402 }
403
404 return FALSE;
c801d85f
KB
405}
406
407// True if was a button down event (1 = left, 2 = middle, 3 = right)
408// or any button down event (but = -1)
409bool wxMouseEvent::ButtonDown(int but) const
410{
0b746ba8
VZ
411 switch (but)
412 {
413 case -1:
414 return (LeftDown() || MiddleDown() || RightDown());
415 case 1:
416 return LeftDown();
417 case 2:
418 return MiddleDown();
419 case 3:
420 return RightDown();
421 default:
223d09f6 422 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonDown"));
0b746ba8
VZ
423 }
424
425 return FALSE;
c801d85f
KB
426}
427
428// True if was a button up event (1 = left, 2 = middle, 3 = right)
429// or any button up event (but = -1)
430bool wxMouseEvent::ButtonUp(int but) const
431{
1e6feb95
VZ
432 switch (but)
433 {
0b746ba8
VZ
434 case -1:
435 return (LeftUp() || MiddleUp() || RightUp());
436 case 1:
437 return LeftUp();
438 case 2:
439 return MiddleUp();
440 case 3:
441 return RightUp();
442 default:
223d09f6 443 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonUp"));
0b746ba8
VZ
444 }
445
446 return FALSE;
c801d85f
KB
447}
448
449// True if the given button is currently changing state
450bool wxMouseEvent::Button(int but) const
451{
1e6feb95
VZ
452 switch (but)
453 {
0b746ba8 454 case -1:
72cdf4c9 455 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1));
0b746ba8
VZ
456 case 1:
457 return (LeftDown() || LeftUp() || LeftDClick());
458 case 2:
459 return (MiddleDown() || MiddleUp() || MiddleDClick());
460 case 3:
461 return (RightDown() || RightUp() || RightDClick());
462 default:
223d09f6 463 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::Button"));
0b746ba8
VZ
464 }
465
466 return FALSE;
c801d85f
KB
467}
468
469bool wxMouseEvent::ButtonIsDown(int but) const
470{
1e6feb95
VZ
471 switch (but)
472 {
0b746ba8
VZ
473 case -1:
474 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
475 case 1:
476 return LeftIsDown();
477 case 2:
478 return MiddleIsDown();
479 case 3:
480 return RightIsDown();
481 default:
223d09f6 482 wxFAIL_MSG(wxT("invalid parameter in wxMouseEvent::ButtonIsDown"));
0b746ba8
VZ
483 }
484
485 return FALSE;
c801d85f
KB
486}
487
1e6feb95
VZ
488int wxMouseEvent::GetButton() const
489{
490 for ( int i = 1; i <= 3; i++ )
491 {
492 if ( Button(i) )
493 {
494 return i;
495 }
496 }
497
498 return -1;
499}
500
c801d85f
KB
501// Find the logical position of the event given the DC
502wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
503{
0757d27c
JS
504 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
505 return pt;
c801d85f
KB
506}
507
508
509/*
8e72b8b5 510 * Keyboard event
c801d85f
KB
511 *
512 */
513
7798a18e 514wxKeyEvent::wxKeyEvent(wxEventType type)
c801d85f 515{
0b746ba8
VZ
516 m_eventType = type;
517 m_shiftDown = FALSE;
518 m_controlDown = FALSE;
519 m_metaDown = FALSE;
520 m_altDown = FALSE;
521 m_keyCode = 0;
b0e813a0 522 m_scanCode = 0;
c801d85f
KB
523}
524
aadbdf11 525
42e69d6b 526wxWindowCreateEvent::wxWindowCreateEvent(wxWindow *win)
42e69d6b 527{
cfe17b74 528 SetEventType(wxEVT_CREATE);
42e69d6b
VZ
529 SetEventObject(win);
530}
531
532wxWindowDestroyEvent::wxWindowDestroyEvent(wxWindow *win)
42e69d6b 533{
cfe17b74 534 SetEventType(wxEVT_DESTROY);
42e69d6b
VZ
535 SetEventObject(win);
536}
537
456bc6d9
VZ
538wxChildFocusEvent::wxChildFocusEvent(wxWindow *win)
539 : wxCommandEvent(wxEVT_CHILD_FOCUS)
540{
541 SetEventObject(win);
542}
543
dd070522
VZ
544#endif // wxUSE_GUI
545
456bc6d9
VZ
546// ----------------------------------------------------------------------------
547// wxEvtHandler
548// ----------------------------------------------------------------------------
549
c801d85f
KB
550/*
551 * Event handler
552 */
553
0b746ba8 554wxEvtHandler::wxEvtHandler()
c801d85f 555{
0b746ba8
VZ
556 m_nextHandler = (wxEvtHandler *) NULL;
557 m_previousHandler = (wxEvtHandler *) NULL;
558 m_enabled = TRUE;
559 m_dynamicEvents = (wxList *) NULL;
193bf013 560 m_isWindow = FALSE;
8e193f38 561 m_pendingEvents = (wxList *) NULL;
7214297d 562#if wxUSE_THREADS
41404da7 563# if !defined(__VISAGECPP__)
8e193f38 564 m_eventsLocker = new wxCriticalSection;
41404da7 565# endif
7214297d 566#endif
b88c44e7
RD
567 // no client data (yet)
568 m_clientData = NULL;
569 m_clientDataType = wxClientData_None;
c801d85f
KB
570}
571
0b746ba8 572wxEvtHandler::~wxEvtHandler()
c801d85f 573{
0b746ba8
VZ
574 // Takes itself out of the list of handlers
575 if (m_previousHandler)
576 m_previousHandler->m_nextHandler = m_nextHandler;
577
578 if (m_nextHandler)
579 m_nextHandler->m_previousHandler = m_previousHandler;
580
581 if (m_dynamicEvents)
fe71f65c 582 {
0b746ba8
VZ
583 wxNode *node = m_dynamicEvents->First();
584 while (node)
585 {
2e4df4bf
VZ
586#if WXWIN_COMPATIBILITY_EVENT_TYPES
587 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
588#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
77d4384e 589 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
2e4df4bf
VZ
590#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
591
592 if (entry->m_callbackUserData)
593 delete entry->m_callbackUserData;
0b746ba8
VZ
594 delete entry;
595 node = node->Next();
596 }
597 delete m_dynamicEvents;
598 };
7214297d 599
8e193f38 600 delete m_pendingEvents;
7214297d 601
8e193f38 602#if wxUSE_THREADS
41404da7 603# if !defined(__VISAGECPP__)
7214297d 604 delete m_eventsLocker;
41404da7 605# endif
7214297d 606#endif
b88c44e7
RD
607
608 // we only delete object data, not untyped
609 if ( m_clientDataType == wxClientData_Object )
610 delete m_clientObject;
c801d85f
KB
611}
612
7214297d 613#if wxUSE_THREADS
aadbdf11 614
7214297d
GL
615bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
616{
7214297d 617 // check that we are really in a child thread
8e193f38
VZ
618 wxASSERT_MSG( !wxThread::IsMain(),
619 wxT("use ProcessEvent() in main thread") );
620
621 AddPendingEvent(event);
7214297d 622
8e193f38
VZ
623 return TRUE;
624}
625
626#endif // wxUSE_THREADS
627
628void wxEvtHandler::AddPendingEvent(wxEvent& event)
629{
16c1f79c
RR
630 // 1) Add event to list of pending events of this event handler
631
632#if defined(__VISAGECPP__)
633 wxENTER_CRIT_SECT( m_eventsLocker);
634#else
635 wxENTER_CRIT_SECT( *m_eventsLocker);
636#endif
637
8e193f38
VZ
638 if ( !m_pendingEvents )
639 m_pendingEvents = new wxList;
7214297d 640
8e193f38 641 wxEvent *event2 = (wxEvent *)event.Clone();
7214297d 642
8e193f38 643 m_pendingEvents->Append(event2);
7214297d 644
16c1f79c
RR
645#if defined(__VISAGECPP__)
646 wxLEAVE_CRIT_SECT( m_eventsLocker);
647#else
648 wxLEAVE_CRIT_SECT( *m_eventsLocker);
649#endif
650
651 // 2) Add this event handler to list of event handlers that
652 // have pending events.
cfe17b74 653
b568d04f 654 wxENTER_CRIT_SECT(*wxPendingEventsLocker);
72cdf4c9 655
8e193f38
VZ
656 if ( !wxPendingEvents )
657 wxPendingEvents = new wxList;
4d3a259a 658 wxPendingEvents->Append(this);
72cdf4c9 659
ce6d2511 660 wxLEAVE_CRIT_SECT(*wxPendingEventsLocker);
6164f93c 661
16c1f79c
RR
662 // 3) Inform the system that new pending events are somwehere,
663 // and that these should be processed in idle time.
bf9e3e73 664 wxWakeUpIdle();
7214297d
GL
665}
666
667void wxEvtHandler::ProcessPendingEvents()
668{
41404da7 669#if defined(__VISAGECPP__)
16c1f79c 670 wxENTER_CRIT_SECT( m_eventsLocker);
b568d04f 671#else
16c1f79c 672 wxENTER_CRIT_SECT( *m_eventsLocker);
41404da7 673#endif
8e193f38 674
7214297d 675 wxNode *node = m_pendingEvents->First();
8e193f38
VZ
676 while ( node )
677 {
16c1f79c
RR
678 wxEvent *event = (wxEvent *)node->Data();
679 delete node;
cfe17b74 680
16c1f79c 681 // In ProcessEvent, new events might get added and
6164f93c 682 // we can safely leave the crtical section here.
16c1f79c
RR
683#if defined(__VISAGECPP__)
684 wxLEAVE_CRIT_SECT( m_eventsLocker);
685#else
686 wxLEAVE_CRIT_SECT( *m_eventsLocker);
687#endif
8e193f38 688 ProcessEvent(*event);
9779893b 689 delete event;
16c1f79c
RR
690#if defined(__VISAGECPP__)
691 wxENTER_CRIT_SECT( m_eventsLocker);
692#else
693 wxENTER_CRIT_SECT( *m_eventsLocker);
694#endif
cfe17b74 695
8e193f38 696 node = m_pendingEvents->First();
7214297d 697 }
cfe17b74 698
16c1f79c
RR
699#if defined(__VISAGECPP__)
700 wxLEAVE_CRIT_SECT( m_eventsLocker);
701#else
702 wxLEAVE_CRIT_SECT( *m_eventsLocker);
703#endif
7214297d 704}
7214297d 705
c801d85f
KB
706/*
707 * Event table stuff
708 */
709
710bool wxEvtHandler::ProcessEvent(wxEvent& event)
711{
e90c1d2a 712#if wxUSE_GUI
1a77875b
JS
713
714 // We have to use the actual window or processing events from wxWindowNative
715 // destructor won't work (we don't see the wxWindow class)
716#ifdef __WXDEBUG__
8e193f38 717 // check that our flag corresponds to reality
1a77875b
JS
718 wxClassInfo* info = NULL;
719#ifdef __WXUNIVERSAL__
720# if defined(__WXMSW__)
721 info = CLASSINFO(wxWindowMSW);
722# elif defined(__WXGTK__)
723 info = CLASSINFO(wxWindowGTK);
724# elif defined(__WXMGL__)
725 info = CLASSINFO(wxWindowMGL);
19193a2c
KB
726# elif defined(__WXPM__)
727 info = CLASSINFO(wxWindowOS2);
1a77875b
JS
728# elif defined(__WXMAC__)
729 info = CLASSINFO(wxWindowMac);
730# elif defined(__WXMOTIF__)
731 info = CLASSINFO(wxWindowMotif);
732# endif
733#else
734 info = CLASSINFO(wxWindow);
735#endif
736
737 wxASSERT_MSG( m_isWindow == IsKindOf(info),
1e6feb95 738 _T("this should [not] be a window but it is [not]") );
1a77875b
JS
739#endif
740
e90c1d2a 741#endif // wxUSE_GUI
0b746ba8 742
8e193f38 743 // An event handler can be enabled or disabled
0b746ba8 744 if ( GetEvtHandlerEnabled() )
c801d85f 745 {
6164f93c 746
924ef850
RR
747#if 0
748/*
749 What is this? When using GUI threads, a non main
750 threads can send an event and process it itself.
751 This breaks GTK's GUI threads, so please explain.
752*/
753
8e193f38
VZ
754 // Check whether we are in a child thread.
755 if ( !wxThread::IsMain() )
7214297d 756 return ProcessThreadEvent(event);
924ef850 757#endif
0757d27c 758
8e193f38 759 // Handle per-instance dynamic event tables first
193bf013 760 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
0757d27c 761 return TRUE;
0b746ba8 762
8e193f38 763 // Then static per-class event tables
0b746ba8
VZ
764 const wxEventTable *table = GetEventTable();
765
e90c1d2a 766#if wxUSE_GUI && wxUSE_VALIDATORS
0b746ba8
VZ
767 // Try the associated validator first, if this is a window.
768 // Problem: if the event handler of the window has been replaced,
769 // this wxEvtHandler may no longer be a window.
770 // Therefore validators won't be processed if the handler
771 // has been replaced with SetEventHandler.
772 // THIS CAN BE CURED if PushEventHandler is used instead of
773 // SetEventHandler, and then processing will be passed down the
774 // chain of event handlers.
ce4169a4 775 if (m_isWindow)
0b746ba8
VZ
776 {
777 wxWindow *win = (wxWindow *)this;
778
779 // Can only use the validator of the window which
780 // is receiving the event
193bf013 781 if ( win == event.GetEventObject() )
0b746ba8 782 {
193bf013
VZ
783 wxValidator *validator = win->GetValidator();
784 if ( validator && validator->ProcessEvent(event) )
785 {
786 return TRUE;
787 }
0b746ba8
VZ
788 }
789 }
ce4169a4 790#endif
0b746ba8
VZ
791
792 // Search upwards through the inheritance hierarchy
ce4169a4 793 while (table)
0b746ba8 794 {
193bf013 795 if ( SearchEventTable((wxEventTable&)*table, event) )
0b746ba8
VZ
796 return TRUE;
797 table = table->baseTable;
798 }
c801d85f
KB
799 }
800
0b746ba8
VZ
801 // Try going down the event handler chain
802 if ( GetNextHandler() )
c801d85f 803 {
0b746ba8
VZ
804 if ( GetNextHandler()->ProcessEvent(event) )
805 return TRUE;
c801d85f 806 }
c801d85f 807
e90c1d2a 808#if wxUSE_GUI
3bdc265d
VZ
809 // Carry on up the parent-child hierarchy, but only if event is a command
810 // event: it wouldn't make sense for a parent to receive a child's size
811 // event, for example
193bf013 812 if ( m_isWindow && event.IsCommandEvent() )
0b746ba8
VZ
813 {
814 wxWindow *win = (wxWindow *)this;
3bdc265d 815
e4b713a2
VZ
816 // honour the requests to stop propagation at this window: this is
817 // used by the dialogs, for example, to prevent processing the events
818 // from the dialog controls in the parent frame which rarely, if ever,
819 // makes sense
820 if ( !(win->GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
3bdc265d
VZ
821 {
822 wxWindow *parent = win->GetParent();
e4b713a2 823 if ( parent && !parent->IsBeingDeleted() )
3bdc265d
VZ
824 return parent->GetEventHandler()->ProcessEvent(event);
825 }
0b746ba8 826 }
e90c1d2a 827#endif // wxUSE_GUI
0b746ba8
VZ
828
829 // Last try - application object.
193bf013 830 if ( wxTheApp && (this != wxTheApp) )
0b746ba8 831 {
193bf013
VZ
832 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
833 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
834 // processed appropriately via SearchEventTable.
835 if ( event.GetEventType() != wxEVT_IDLE )
836 {
837 if ( wxTheApp->ProcessEvent(event) )
838 return TRUE;
839 }
0b746ba8
VZ
840 }
841
c801d85f
KB
842 return FALSE;
843}
844
845bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
846{
68662769
VZ
847 wxEventType eventType = event.GetEventType();
848 int eventId = event.GetId();
0b746ba8 849
68662769
VZ
850 // BC++ doesn't like testing for m_fn without != 0
851 for ( int i = 0; table.entries[i].m_fn != 0; i++ )
c801d85f 852 {
6e58645e
VZ
853 // the line using reference exposes a bug in gcc: although it _seems_
854 // to work, it leads to weird crashes later on during program
855 // execution
856#ifdef __GNUG__
857 wxEventTableEntry entry = table.entries[i];
858#else
68662769 859 const wxEventTableEntry& entry = table.entries[i];
6e58645e 860#endif
68662769
VZ
861
862 // match only if the event type is the same and the id is either -1 in
863 // the event table (meaning "any") or the event id matches the id
864 // specified in the event table either exactly or by falling into
865 // range between first and last
866 if ( eventType == entry.m_eventType )
0b746ba8 867 {
68662769
VZ
868 int tableId1 = entry.m_id,
869 tableId2 = entry.m_lastId;
870
871 if ( (tableId1 == -1) ||
872 (tableId2 == -1 && eventId == tableId1) ||
873 (tableId2 != -1 &&
874 (eventId >= tableId1 && eventId <= tableId2)) )
875 {
876 event.Skip(FALSE);
877 event.m_callbackUserData = entry.m_callbackUserData;
0b746ba8 878
68662769 879 (this->*((wxEventFunction) (entry.m_fn)))(event);
0b746ba8 880
68662769
VZ
881 return !event.GetSkipped();
882 }
0b746ba8 883 }
c801d85f 884 }
68662769 885
0b746ba8 886 return FALSE;
c801d85f 887}
97d7bfb8 888
debe6624 889void wxEvtHandler::Connect( int id, int lastId,
77d4384e 890 int eventType,
0b746ba8
VZ
891 wxObjectEventFunction func,
892 wxObject *userData )
fe71f65c 893{
cbee8f8d 894#if WXWIN_COMPATIBILITY_EVENT_TYPES
2e4df4bf 895 wxEventTableEntry *entry = new wxEventTableEntry;
cbee8f8d
VZ
896 entry->m_eventType = eventType;
897 entry->m_id = id;
898 entry->m_lastId = lastId;
899 entry->m_fn = func;
900 entry->m_callbackUserData = userData;
901#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
0b5eceb6
VZ
902 wxDynamicEventTableEntry *entry =
903 new wxDynamicEventTableEntry(eventType, id, lastId, func, userData);
cbee8f8d 904#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
0b746ba8
VZ
905
906 if (!m_dynamicEvents)
907 m_dynamicEvents = new wxList;
908
909 m_dynamicEvents->Append( (wxObject*) entry );
fe71f65c 910}
97d7bfb8
RR
911
912bool wxEvtHandler::Disconnect( int id, int lastId, wxEventType eventType,
913 wxObjectEventFunction func,
914 wxObject *userData )
915{
916 if (!m_dynamicEvents)
917 return FALSE;
cfe17b74 918
97d7bfb8
RR
919 wxNode *node = m_dynamicEvents->First();
920 while (node)
921 {
2e4df4bf
VZ
922#if WXWIN_COMPATIBILITY_EVENT_TYPES
923 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
924#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
925 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
926#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
927
97d7bfb8
RR
928 if ((entry->m_id == id) &&
929 ((entry->m_lastId == lastId) || (lastId == -1)) &&
930 ((entry->m_eventType == eventType) || (eventType == wxEVT_NULL)) &&
931 ((entry->m_fn == func) || (func == (wxObjectEventFunction)NULL)) &&
932 ((entry->m_callbackUserData == userData) || (userData == (wxObject*)NULL)))
933 {
2e4df4bf
VZ
934 if (entry->m_callbackUserData)
935 delete entry->m_callbackUserData;
97d7bfb8
RR
936 m_dynamicEvents->DeleteNode( node );
937 delete entry;
938 return TRUE;
939 }
940 node = node->Next();
941 }
942 return FALSE;
943}
fe71f65c
RR
944
945bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
946{
193bf013 947 wxCHECK_MSG( m_dynamicEvents, FALSE,
223d09f6 948 wxT("caller should check that we have dynamic events") );
0b746ba8
VZ
949
950 int commandId = event.GetId();
951
952 wxNode *node = m_dynamicEvents->First();
953 while (node)
fe71f65c 954 {
2e4df4bf
VZ
955#if WXWIN_COMPATIBILITY_EVENT_TYPES
956 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
957#else // !WXWIN_COMPATIBILITY_EVENT_TYPES
958 wxDynamicEventTableEntry *entry = (wxDynamicEventTableEntry*)node->Data();
959#endif // WXWIN_COMPATIBILITY_EVENT_TYPES/!WXWIN_COMPATIBILITY_EVENT_TYPES
0b746ba8
VZ
960
961 if (entry->m_fn)
962 {
963 // Match, if event spec says any id will do (id == -1)
964 if ( (event.GetEventType() == entry->m_eventType) &&
965 (entry->m_id == -1 ||
966 (entry->m_lastId == -1 && commandId == entry->m_id) ||
967 (entry->m_lastId != -1 &&
968 (commandId >= entry->m_id && commandId <= entry->m_lastId))) )
969 {
970 event.Skip(FALSE);
971 event.m_callbackUserData = entry->m_callbackUserData;
972
973 (this->*((wxEventFunction) (entry->m_fn)))(event);
974
975 if (event.GetSkipped())
976 return FALSE;
977 else
978 return TRUE;
979 }
980 }
981 node = node->Next();
7b678698 982 }
0b746ba8 983 return FALSE;
fe71f65c
RR
984};
985
b88c44e7
RD
986void wxEvtHandler::DoSetClientObject( wxClientData *data )
987{
988 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
989 wxT("can't have both object and void client data") );
990
991 if ( m_clientObject )
992 delete m_clientObject;
993
994 m_clientObject = data;
995 m_clientDataType = wxClientData_Object;
996}
997
998wxClientData *wxEvtHandler::DoGetClientObject() const
999{
1000 // it's not an error to call GetClientObject() on a window which doesn't
1001 // have client data at all - NULL will be returned
1002 wxASSERT_MSG( m_clientDataType != wxClientData_Void,
1003 wxT("this window doesn't have object client data") );
1004
1005 return m_clientObject;
1006}
1007
1008void wxEvtHandler::DoSetClientData( void *data )
1009{
1010 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1011 wxT("can't have both object and void client data") );
1012
1013 m_clientData = data;
1014 m_clientDataType = wxClientData_Void;
1015}
1016
1017void *wxEvtHandler::DoGetClientData() const
1018{
1019 // it's not an error to call GetClientData() on a window which doesn't have
1020 // client data at all - NULL will be returned
1021 wxASSERT_MSG( m_clientDataType != wxClientData_Object,
1022 wxT("this window doesn't have void client data") );
1023
1024 return m_clientData;
1025}
1026
1027
e3065973 1028#if WXWIN_COMPATIBILITY
0b746ba8 1029bool wxEvtHandler::OnClose()
c801d85f 1030{
0b746ba8
VZ
1031 if (GetNextHandler())
1032 return GetNextHandler()->OnClose();
1033 else
1034 return FALSE;
c801d85f 1035}
193bf013 1036#endif // WXWIN_COMPATIBILITY
e3065973 1037
e90c1d2a
VZ
1038#if wxUSE_GUI
1039
e702ff0f
JS
1040// Find a window with the focus, that is also a descendant of the given window.
1041// This is used to determine the window to initially send commands to.
1042wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
1043{
1044 // Process events starting with the window with the focus, if any.
1045 wxWindow* focusWin = wxWindow::FindFocus();
1046 wxWindow* win = focusWin;
1047
1048 // Check if this is a descendant of this frame.
1049 // If not, win will be set to NULL.
1050 while (win)
1051 {
1052 if (win == ancestor)
1053 break;
1054 else
1055 win = win->GetParent();
1056 }
1057 if (win == (wxWindow*) NULL)
1058 focusWin = (wxWindow*) NULL;
1059
1060 return focusWin;
1061}
1062
e90c1d2a 1063#endif // wxUSE_GUI