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