]> git.saurik.com Git - wxWidgets.git/blame - src/common/event.cpp
combobox buglet when inserting item
[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
0b746ba8 9// Licence: wxWindows licence
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
0b746ba8 13 #pragma implementation "event.h"
c801d85f
KB
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
0b746ba8 20 #pragma hdrstop
c801d85f
KB
21#endif
22
23#ifndef WX_PRECOMP
0b746ba8
VZ
24 #include "wx/defs.h"
25 #include "wx/control.h"
26 #include "wx/utils.h"
27 #include "wx/app.h"
28 #include "wx/dc.h"
c801d85f
KB
29#endif
30
31#include "wx/event.h"
32#include "wx/validate.h"
33
34#if !USE_SHARED_LIBRARY
0b746ba8
VZ
35 IMPLEMENT_DYNAMIC_CLASS(wxEvtHandler, wxObject)
36 IMPLEMENT_ABSTRACT_CLASS(wxEvent, wxObject)
37 IMPLEMENT_DYNAMIC_CLASS(wxCommandEvent, wxEvent)
38 IMPLEMENT_DYNAMIC_CLASS(wxNotifyEvent, wxCommandEvent)
39 IMPLEMENT_DYNAMIC_CLASS(wxScrollEvent, wxCommandEvent)
40 IMPLEMENT_DYNAMIC_CLASS(wxMouseEvent, wxEvent)
41 IMPLEMENT_DYNAMIC_CLASS(wxKeyEvent, wxEvent)
42 IMPLEMENT_DYNAMIC_CLASS(wxSizeEvent, wxEvent)
43 IMPLEMENT_DYNAMIC_CLASS(wxPaintEvent, wxEvent)
44 IMPLEMENT_DYNAMIC_CLASS(wxEraseEvent, wxEvent)
45 IMPLEMENT_DYNAMIC_CLASS(wxMoveEvent, wxEvent)
46 IMPLEMENT_DYNAMIC_CLASS(wxFocusEvent, wxEvent)
47 IMPLEMENT_DYNAMIC_CLASS(wxCloseEvent, wxEvent)
48 IMPLEMENT_DYNAMIC_CLASS(wxShowEvent, wxEvent)
49 IMPLEMENT_DYNAMIC_CLASS(wxMaximizeEvent, wxEvent)
50 IMPLEMENT_DYNAMIC_CLASS(wxIconizeEvent, wxEvent)
51 IMPLEMENT_DYNAMIC_CLASS(wxMenuEvent, wxEvent)
52 IMPLEMENT_DYNAMIC_CLASS(wxJoystickEvent, wxEvent)
53 IMPLEMENT_DYNAMIC_CLASS(wxDropFilesEvent, wxEvent)
54 IMPLEMENT_DYNAMIC_CLASS(wxActivateEvent, wxEvent)
55 IMPLEMENT_DYNAMIC_CLASS(wxInitDialogEvent, wxEvent)
56 IMPLEMENT_DYNAMIC_CLASS(wxSysColourChangedEvent, wxEvent)
57 IMPLEMENT_DYNAMIC_CLASS(wxIdleEvent, wxEvent)
58 IMPLEMENT_DYNAMIC_CLASS(wxUpdateUIEvent, wxCommandEvent)
59 IMPLEMENT_DYNAMIC_CLASS(wxNavigationKeyEvent, wxCommandEvent)
60 IMPLEMENT_DYNAMIC_CLASS(wxPaletteChangedEvent, wxEvent)
61 IMPLEMENT_DYNAMIC_CLASS(wxQueryNewPaletteEvent, wxEvent)
62
63 const wxEventTable *wxEvtHandler::GetEventTable() const
64 { return &wxEvtHandler::sm_eventTable; }
65
66 const wxEventTable wxEvtHandler::sm_eventTable =
67 { (const wxEventTable *)NULL, &wxEvtHandler::sm_eventTableEntries[0] };
68
69 const wxEventTableEntry wxEvtHandler::sm_eventTableEntries[] =
70 { { 0, 0, 0,
71 #ifdef __SGI_CC__
72 // stupid SGI compiler --- offer aug 98
73 0L }
74 #else // !SGI CC
75 NULL }
76 #endif // SGI/!SGI
77 };
78
79#endif // !USE_SHARED_LIBRARY
c801d85f 80
7214297d
GL
81#if wxUSE_THREADS
82/* To put pending event handlers */
4d3a259a
GL
83extern wxList *wxPendingEvents;
84extern wxCriticalSection *wxPendingEventsLocker;
7214297d
GL
85#endif
86
c801d85f
KB
87/*
88 * General wxWindows events, covering
89 * all interesting things that might happen (button clicking, resizing,
90 * setting text in widgets, etc.).
91 *
92 * For each completely new event type, derive a new event class.
93 *
94 */
95
96wxEvent::wxEvent(int theId)
97{
0b746ba8
VZ
98 m_eventType = wxEVT_NULL;
99 m_eventObject = (wxObject *) NULL;
100 m_eventHandle = (char *) NULL;
101 m_timeStamp = 0;
102 m_id = theId;
103 m_skipped = FALSE;
104 m_callbackUserData = (wxObject *) NULL;
193bf013 105 m_isCommandEvent = FALSE;
c801d85f
KB
106}
107
a737331d
GL
108wxObject *wxEvent::Clone() const
109{
110 wxEvent *event = (wxEvent *)wxObject::Clone();
111
112 event->m_eventType = m_eventType;
113 event->m_eventObject = m_eventObject;
114 event->m_eventHandle = m_eventHandle;
115 event->m_timeStamp = m_timeStamp;
116 event->m_id = m_id;
117 event->m_skipped = m_skipped;
118 event->m_callbackUserData = m_callbackUserData;
119 event->m_isCommandEvent = m_isCommandEvent;
120
121 return event;
122}
123
c801d85f
KB
124/*
125 * Command events
126 *
127 */
128
7798a18e 129wxCommandEvent::wxCommandEvent(wxEventType commandType, int theId)
c801d85f 130{
0b746ba8
VZ
131 m_eventType = commandType;
132 m_clientData = (char *) NULL;
133 m_clientObject = (wxClientData *) NULL;
134 m_extraLong = 0;
135 m_commandInt = 0;
136 m_id = theId;
9f06bcb3 137 m_commandString = (wxChar *) NULL;
193bf013 138 m_isCommandEvent = TRUE;
c801d85f
KB
139}
140
141/*
142 * Scroll events
143 */
144
0b746ba8
VZ
145wxScrollEvent::wxScrollEvent(wxEventType commandType,
146 int id,
147 int pos,
148 int orient)
149 : wxCommandEvent(commandType, id)
c801d85f 150{
0b746ba8
VZ
151 m_extraLong = orient;
152 m_commandInt = pos;
c801d85f
KB
153}
154
155
156/*
157 * Mouse events
158 *
159 */
160
7798a18e 161wxMouseEvent::wxMouseEvent(wxEventType commandType)
c801d85f 162{
0b746ba8
VZ
163 m_eventType = commandType;
164 m_metaDown = FALSE;
165 m_altDown = FALSE;
166 m_controlDown = FALSE;
167 m_shiftDown = FALSE;
168 m_leftDown = FALSE;
169 m_rightDown = FALSE;
170 m_middleDown = FALSE;
171 m_x = 0;
172 m_y = 0;
c801d85f
KB
173}
174
175// True if was a button dclick event (1 = left, 2 = middle, 3 = right)
176// or any button dclick event (but = -1)
177bool wxMouseEvent::ButtonDClick(int but) const
178{
0b746ba8
VZ
179 switch (but)
180 {
181 case -1:
182 return (LeftDClick() || MiddleDClick() || RightDClick());
183 case 1:
184 return LeftDClick();
185 case 2:
186 return MiddleDClick();
187 case 3:
188 return RightDClick();
189 default:
50920146 190 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDClick"));
0b746ba8
VZ
191 }
192
193 return FALSE;
c801d85f
KB
194}
195
196// True if was a button down event (1 = left, 2 = middle, 3 = right)
197// or any button down event (but = -1)
198bool wxMouseEvent::ButtonDown(int but) const
199{
0b746ba8
VZ
200 switch (but)
201 {
202 case -1:
203 return (LeftDown() || MiddleDown() || RightDown());
204 case 1:
205 return LeftDown();
206 case 2:
207 return MiddleDown();
208 case 3:
209 return RightDown();
210 default:
50920146 211 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonDown"));
0b746ba8
VZ
212 }
213
214 return FALSE;
c801d85f
KB
215}
216
217// True if was a button up event (1 = left, 2 = middle, 3 = right)
218// or any button up event (but = -1)
219bool wxMouseEvent::ButtonUp(int but) const
220{
0b746ba8
VZ
221 switch (but) {
222 case -1:
223 return (LeftUp() || MiddleUp() || RightUp());
224 case 1:
225 return LeftUp();
226 case 2:
227 return MiddleUp();
228 case 3:
229 return RightUp();
230 default:
50920146 231 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonUp"));
0b746ba8
VZ
232 }
233
234 return FALSE;
c801d85f
KB
235}
236
237// True if the given button is currently changing state
238bool wxMouseEvent::Button(int but) const
239{
0b746ba8
VZ
240 switch (but) {
241 case -1:
242 return (ButtonUp(-1) || ButtonDown(-1) || ButtonDClick(-1)) ;
243 case 1:
244 return (LeftDown() || LeftUp() || LeftDClick());
245 case 2:
246 return (MiddleDown() || MiddleUp() || MiddleDClick());
247 case 3:
248 return (RightDown() || RightUp() || RightDClick());
249 default:
50920146 250 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::Button"));
0b746ba8
VZ
251 }
252
253 return FALSE;
c801d85f
KB
254}
255
256bool wxMouseEvent::ButtonIsDown(int but) const
257{
0b746ba8
VZ
258 switch (but) {
259 case -1:
260 return (LeftIsDown() || MiddleIsDown() || RightIsDown());
261 case 1:
262 return LeftIsDown();
263 case 2:
264 return MiddleIsDown();
265 case 3:
266 return RightIsDown();
267 default:
50920146 268 wxFAIL_MSG(_T("invalid parameter in wxMouseEvent::ButtonIsDown"));
0b746ba8
VZ
269 }
270
271 return FALSE;
c801d85f
KB
272}
273
274// Find the logical position of the event given the DC
275wxPoint wxMouseEvent::GetLogicalPosition(const wxDC& dc) const
276{
0757d27c
JS
277 wxPoint pt(dc.DeviceToLogicalX(m_x), dc.DeviceToLogicalY(m_y));
278 return pt;
c801d85f
KB
279}
280
281
282/*
283 * Keyboard events
284 *
285 */
286
7798a18e 287wxKeyEvent::wxKeyEvent(wxEventType type)
c801d85f 288{
0b746ba8
VZ
289 m_eventType = type;
290 m_shiftDown = FALSE;
291 m_controlDown = FALSE;
292 m_metaDown = FALSE;
293 m_altDown = FALSE;
294 m_keyCode = 0;
c801d85f
KB
295}
296
297/*
298 * Event handler
299 */
300
0b746ba8 301wxEvtHandler::wxEvtHandler()
c801d85f 302{
0b746ba8
VZ
303 m_nextHandler = (wxEvtHandler *) NULL;
304 m_previousHandler = (wxEvtHandler *) NULL;
305 m_enabled = TRUE;
306 m_dynamicEvents = (wxList *) NULL;
193bf013 307 m_isWindow = FALSE;
7214297d
GL
308#if wxUSE_THREADS
309 m_eventsLocker = new wxCriticalSection();
310#endif
311 m_pendingEvents = (wxList *) NULL;
c801d85f
KB
312}
313
0b746ba8 314wxEvtHandler::~wxEvtHandler()
c801d85f 315{
0b746ba8
VZ
316 // Takes itself out of the list of handlers
317 if (m_previousHandler)
318 m_previousHandler->m_nextHandler = m_nextHandler;
319
320 if (m_nextHandler)
321 m_nextHandler->m_previousHandler = m_previousHandler;
322
323 if (m_dynamicEvents)
fe71f65c 324 {
0b746ba8
VZ
325 wxNode *node = m_dynamicEvents->First();
326 while (node)
327 {
328 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
329 if (entry->m_callbackUserData) delete entry->m_callbackUserData;
330 delete entry;
331 node = node->Next();
332 }
333 delete m_dynamicEvents;
334 };
7214297d 335
a737331d 336#if wxUSE_THREADS
7214297d
GL
337 if (m_pendingEvents)
338 delete m_pendingEvents;
339
7214297d
GL
340 delete m_eventsLocker;
341#endif
c801d85f
KB
342}
343
7214297d
GL
344#if wxUSE_THREADS
345bool wxEvtHandler::ProcessThreadEvent(wxEvent& event)
346{
347 wxEvent *event_main;
348 wxCriticalSectionLocker locker(*m_eventsLocker);
349
350 // check that we are really in a child thread
351 wxASSERT( !wxThread::IsMain() );
352
353 if (m_pendingEvents == NULL)
354 m_pendingEvents = new wxList();
355
a737331d 356 event_main = (wxEvent *)event.Clone();
7214297d
GL
357
358 m_pendingEvents->Append(event_main);
359
4d3a259a
GL
360 wxPendingEventsLocker->Enter();
361 wxPendingEvents->Append(this);
362 wxPendingEventsLocker->Leave();
7214297d
GL
363
364 return TRUE;
365}
366
367void wxEvtHandler::ProcessPendingEvents()
368{
369 wxCriticalSectionLocker locker(*m_eventsLocker);
370 wxNode *node = m_pendingEvents->First();
371 wxEvent *event;
372
373 while (node != NULL) {
374 event = (wxEvent *)node->Data();
375 ProcessEvent(*event);
376 delete node;
377 node = m_pendingEvents->First();
378 }
379}
380#endif
381
c801d85f
KB
382/*
383 * Event table stuff
384 */
385
386bool wxEvtHandler::ProcessEvent(wxEvent& event)
387{
193bf013
VZ
388 // check that our flag corresponds to reality
389 wxASSERT( m_isWindow == IsKindOf(CLASSINFO(wxWindow)) );
0b746ba8
VZ
390
391 // An event handler can be enabled or disabled
392 if ( GetEvtHandlerEnabled() )
c801d85f 393 {
7214297d
GL
394#if wxUSE_THREADS
395 // Check whether we are in a child thread.
396 if (!wxThread::IsMain())
397 return ProcessThreadEvent(event);
398#endif
0b746ba8 399 // Handle per-instance dynamic event tables first
0757d27c 400
193bf013 401 if ( m_dynamicEvents && SearchDynamicEventTable(event) )
0757d27c 402 return TRUE;
0b746ba8
VZ
403
404 // Then static per-class event tables
405
406 const wxEventTable *table = GetEventTable();
407
408 // Try the associated validator first, if this is a window.
409 // Problem: if the event handler of the window has been replaced,
410 // this wxEvtHandler may no longer be a window.
411 // Therefore validators won't be processed if the handler
412 // has been replaced with SetEventHandler.
413 // THIS CAN BE CURED if PushEventHandler is used instead of
414 // SetEventHandler, and then processing will be passed down the
415 // chain of event handlers.
193bf013 416 if ( m_isWindow )
0b746ba8
VZ
417 {
418 wxWindow *win = (wxWindow *)this;
419
420 // Can only use the validator of the window which
421 // is receiving the event
193bf013 422 if ( win == event.GetEventObject() )
0b746ba8 423 {
193bf013
VZ
424 wxValidator *validator = win->GetValidator();
425 if ( validator && validator->ProcessEvent(event) )
426 {
427 return TRUE;
428 }
0b746ba8
VZ
429 }
430 }
431
432 // Search upwards through the inheritance hierarchy
193bf013 433 while ( table )
0b746ba8 434 {
193bf013 435 if ( SearchEventTable((wxEventTable&)*table, event) )
0b746ba8
VZ
436 return TRUE;
437 table = table->baseTable;
438 }
c801d85f
KB
439 }
440
0b746ba8
VZ
441 // Try going down the event handler chain
442 if ( GetNextHandler() )
c801d85f 443 {
0b746ba8
VZ
444 if ( GetNextHandler()->ProcessEvent(event) )
445 return TRUE;
c801d85f 446 }
c801d85f 447
0b746ba8
VZ
448 // Carry on up the parent-child hierarchy,
449 // but only if event is a command event: it wouldn't
450 // make sense for a parent to receive a child's size event, for example
193bf013 451 if ( m_isWindow && event.IsCommandEvent() )
0b746ba8
VZ
452 {
453 wxWindow *win = (wxWindow *)this;
454 wxWindow *parent = win->GetParent();
455 if (parent && !parent->IsBeingDeleted())
193bf013 456 return parent->GetEventHandler()->ProcessEvent(event);
0b746ba8
VZ
457 }
458
459 // Last try - application object.
193bf013 460 if ( wxTheApp && (this != wxTheApp) )
0b746ba8 461 {
193bf013
VZ
462 // Special case: don't pass wxEVT_IDLE to wxApp, since it'll always
463 // swallow it. wxEVT_IDLE is sent explicitly to wxApp so it will be
464 // processed appropriately via SearchEventTable.
465 if ( event.GetEventType() != wxEVT_IDLE )
466 {
467 if ( wxTheApp->ProcessEvent(event) )
468 return TRUE;
469 }
0b746ba8
VZ
470 }
471
c801d85f
KB
472 return FALSE;
473}
474
475bool wxEvtHandler::SearchEventTable(wxEventTable& table, wxEvent& event)
476{
0b746ba8
VZ
477 int i = 0;
478 int commandId = event.GetId();
479
480 // BC++ doesn't like while (table.entries[i].m_fn)
481
2432b92d
JS
482#ifdef __SC__
483 while (table.entries[i].m_fn != 0)
484#else
0b746ba8 485 while (table.entries[i].m_fn != 0L)
2432b92d 486#endif
c801d85f 487 {
0b746ba8
VZ
488 if ((event.GetEventType() == table.entries[i].m_eventType) &&
489 (table.entries[i].m_id == -1 || // Match, if event spec says any id will do (id == -1)
490 (table.entries[i].m_lastId == -1 && commandId == table.entries[i].m_id) ||
491 (table.entries[i].m_lastId != -1 &&
492 (commandId >= table.entries[i].m_id && commandId <= table.entries[i].m_lastId))))
493 {
494 event.Skip(FALSE);
495 event.m_callbackUserData = table.entries[i].m_callbackUserData;
496
497 (this->*((wxEventFunction) (table.entries[i].m_fn)))(event);
498
499 if ( event.GetSkipped() )
500 return FALSE;
501 else
502 return TRUE;
503 }
504 i++;
c801d85f 505 }
0b746ba8 506 return FALSE;
c801d85f 507}
debe6624 508void wxEvtHandler::Connect( int id, int lastId,
d4a23fee 509 wxEventType eventType,
0b746ba8
VZ
510 wxObjectEventFunction func,
511 wxObject *userData )
fe71f65c 512{
0b746ba8
VZ
513 wxEventTableEntry *entry = new wxEventTableEntry;
514 entry->m_id = id;
515 entry->m_lastId = lastId;
516 entry->m_eventType = eventType;
517 entry->m_fn = func;
518 entry->m_callbackUserData = userData;
519
520 if (!m_dynamicEvents)
521 m_dynamicEvents = new wxList;
522
523 m_dynamicEvents->Append( (wxObject*) entry );
fe71f65c
RR
524}
525
526bool wxEvtHandler::SearchDynamicEventTable( wxEvent& event )
527{
193bf013 528 wxCHECK_MSG( m_dynamicEvents, FALSE,
50920146 529 _T("caller should check that we have dynamic events") );
0b746ba8
VZ
530
531 int commandId = event.GetId();
532
533 wxNode *node = m_dynamicEvents->First();
534 while (node)
fe71f65c 535 {
0b746ba8
VZ
536 wxEventTableEntry *entry = (wxEventTableEntry*)node->Data();
537
538 if (entry->m_fn)
539 {
540 // Match, if event spec says any id will do (id == -1)
541 if ( (event.GetEventType() == entry->m_eventType) &&
542 (entry->m_id == -1 ||
543 (entry->m_lastId == -1 && commandId == entry->m_id) ||
544 (entry->m_lastId != -1 &&
545 (commandId >= entry->m_id && commandId <= entry->m_lastId))) )
546 {
547 event.Skip(FALSE);
548 event.m_callbackUserData = entry->m_callbackUserData;
549
550 (this->*((wxEventFunction) (entry->m_fn)))(event);
551
552 if (event.GetSkipped())
553 return FALSE;
554 else
555 return TRUE;
556 }
557 }
558 node = node->Next();
7b678698 559 }
0b746ba8 560 return FALSE;
fe71f65c
RR
561};
562
e3065973 563#if WXWIN_COMPATIBILITY
0b746ba8 564bool wxEvtHandler::OnClose()
c801d85f 565{
0b746ba8
VZ
566 if (GetNextHandler())
567 return GetNextHandler()->OnClose();
568 else
569 return FALSE;
c801d85f 570}
193bf013 571#endif // WXWIN_COMPATIBILITY
e3065973 572
e702ff0f
JS
573// Find a window with the focus, that is also a descendant of the given window.
574// This is used to determine the window to initially send commands to.
575wxWindow* wxFindFocusDescendant(wxWindow* ancestor)
576{
577 // Process events starting with the window with the focus, if any.
578 wxWindow* focusWin = wxWindow::FindFocus();
579 wxWindow* win = focusWin;
580
581 // Check if this is a descendant of this frame.
582 // If not, win will be set to NULL.
583 while (win)
584 {
585 if (win == ancestor)
586 break;
587 else
588 win = win->GetParent();
589 }
590 if (win == (wxWindow*) NULL)
591 focusWin = (wxWindow*) NULL;
592
593 return focusWin;
594}
595