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