]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
applied the patch from Dimitri to prevent crashes with (some) interlaced GIFs
[wxWidgets.git] / src / common / popupcmn.cpp
CommitLineData
c02ee97f
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: common/popupcmn.cpp
3// Purpose: implementation of wxPopupTransientWindow
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 06.01.01
7// RCS-ID: $Id$
8// Copyright: (c) 2001 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// License: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
6522713c 21 #pragma implementation "popupwinbase.h"
c02ee97f
VZ
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
45f22d48 31#if wxUSE_POPUPWIN && !defined(__WXMOTIF__)
c02ee97f
VZ
32
33#include "wx/popupwin.h"
34
35#ifndef WX_PRECOMP
36 #include "wx/combobox.h" // wxComboControl
6a0fab3a 37 #include "wx/app.h" // wxPostEvent
ee351013 38 #include "wx/log.h"
3390759e 39 #include "wx/app.h"
c02ee97f
VZ
40#endif //WX_PRECOMP
41
42#ifdef __WXUNIVERSAL__
43 #include "wx/univ/renderer.h"
44#endif // __WXUNIVERSAL__
45
159960c0 46// there is no src/{msw,mgl}/popupwin.cpp to put this in, so we do it here - BTW we
eb729cd3 47// probably could do it for all ports here just as well
159960c0 48#if defined(__WXMSW__) || defined(__WXMGL__)
eb729cd3
VZ
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
50#endif // __WXMSW__
51
6c3422e9 52IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
761df41e
VZ
53
54#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
55 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
56#endif
6c3422e9 57
c02ee97f
VZ
58// ----------------------------------------------------------------------------
59// private classes
60// ----------------------------------------------------------------------------
61
62// event handlers which we use to intercept events which cause the popup to
63// disappear
64class wxPopupWindowHandler : public wxEvtHandler
65{
66public:
67 wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
68
69protected:
70 // event handlers
71 void OnLeftDown(wxMouseEvent& event);
72
73private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
77};
78
79class wxPopupFocusHandler : public wxEvtHandler
80{
81public:
516cdd54
VZ
82 wxPopupFocusHandler(wxPopupTransientWindow *popup)
83 {
84 m_popup = popup;
85
86#ifdef __WXGTK__
87 // ignore the next few OnKillFocus() calls
88 m_creationTime = time(NULL);
89#endif // __WXGTK__
90 }
c02ee97f
VZ
91
92protected:
93 // event handlers
94 void OnKillFocus(wxFocusEvent& event);
d7cff34d 95 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
96
97private:
98 wxPopupTransientWindow *m_popup;
99
516cdd54
VZ
100 // hack around wxGTK bug: we always get several kill focus events
101 // immediately after creation!
102#ifdef __WXGTK__
103 time_t m_creationTime;
104#endif // __WXGTK__
105
c02ee97f
VZ
106 DECLARE_EVENT_TABLE()
107};
108
109// ----------------------------------------------------------------------------
110// event tables
111// ----------------------------------------------------------------------------
112
113BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
114 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
115END_EVENT_TABLE()
116
117BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
118 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
d7cff34d 119 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
120END_EVENT_TABLE()
121
122// ============================================================================
123// implementation
124// ============================================================================
125
126// ----------------------------------------------------------------------------
127// wxPopupWindowBase
128// ----------------------------------------------------------------------------
129
799ea011
GD
130wxPopupWindowBase::~wxPopupWindowBase()
131{
132 // this destructor is required for Darwin
133}
134
c02ee97f
VZ
135bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
136{
137 return TRUE;
138}
139
140void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
141 const wxSize& size)
142{
143 wxSize sizeScreen = wxGetDisplaySize(),
144 sizeSelf = GetSize();
145
146 // is there enough space to put the popup below the window (where we put it
147 // by default)?
148 wxCoord y = ptOrigin.y + size.y;
149 if ( y + sizeSelf.y > sizeScreen.y )
150 {
151 // check if there is enough space above
152 if ( ptOrigin.y > sizeSelf.y )
153 {
154 // do position the control above the window
155 y -= size.y + sizeSelf.y;
156 }
157 //else: not enough space below nor above, leave below
158 }
159
160 // now check left/right too
161 wxCoord x = ptOrigin.x + size.x;
162 if ( x + sizeSelf.x > sizeScreen.x )
163 {
164 // check if there is enough space to the left
165 if ( ptOrigin.x > sizeSelf.x )
166 {
167 // do position the control to the left
168 x -= size.x + sizeSelf.x;
169 }
170 //else: not enough space there neither, leave in default position
171 }
172
173 Move(x, y, wxSIZE_NO_ADJUSTMENTS);
174}
175
176// ----------------------------------------------------------------------------
177// wxPopupTransientWindow
178// ----------------------------------------------------------------------------
179
180void wxPopupTransientWindow::Init()
181{
182 m_child =
183 m_focus = (wxWindow *)NULL;
d7cff34d
VZ
184
185 m_handlerFocus = NULL;
186 m_handlerPopup = NULL;
c02ee97f
VZ
187}
188
758bce95 189wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
190{
191 Init();
192
758bce95 193 (void)Create(parent, style);
c02ee97f
VZ
194}
195
196wxPopupTransientWindow::~wxPopupTransientWindow()
197{
198 PopHandlers();
d7cff34d
VZ
199
200 delete m_handlerFocus;
201 delete m_handlerPopup;
c02ee97f
VZ
202}
203
204void wxPopupTransientWindow::PopHandlers()
205{
206 if ( m_child )
207 {
d7cff34d
VZ
208 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
209 {
210 // something is very wrong and someone else probably deleted our
211 // handler - so don't risk deleting it second time
212 m_handlerPopup = NULL;
213 }
214
c02ee97f
VZ
215 m_child->ReleaseMouse();
216 m_child = NULL;
217 }
218
219 if ( m_focus )
220 {
9b8270da 221#ifndef __WXX11__
d7cff34d
VZ
222 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
223 {
224 // see above
225 m_handlerFocus = NULL;
226 }
9b8270da 227#endif
c02ee97f
VZ
228 m_focus = NULL;
229 }
230}
231
232void wxPopupTransientWindow::Popup(wxWindow *winFocus)
233{
234 const wxWindowList& children = GetChildren();
235 if ( children.GetCount() )
236 {
237 m_child = children.GetFirst()->GetData();
238 }
239 else
240 {
241 m_child = this;
242 }
243
516cdd54
VZ
244 // we can't capture mouse before the window is shown in wxGTK, so do it
245 // first
e4606ed9 246 Show();
e4606ed9 247
d7cff34d
VZ
248 delete m_handlerPopup;
249 m_handlerPopup = new wxPopupWindowHandler(this);
250
c02ee97f 251 m_child->CaptureMouse();
d7cff34d 252 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 253
c02ee97f 254 m_focus = winFocus ? winFocus : this;
c02ee97f 255 m_focus->SetFocus();
60178eb4 256
9b8270da
RR
257#ifndef __WXX11__
258
516cdd54 259#ifdef __WXMSW__
d7cff34d
VZ
260 // MSW doesn't allow to set focus to the popup window, but we need to
261 // subclass the window which has the focus, and not winFocus passed in or
262 // otherwise everything else breaks down
516cdd54 263 m_focus = FindFocus();
60178eb4 264 if ( m_focus )
771c7b4a 265#endif // __WXMSW__
60178eb4 266 {
d7cff34d
VZ
267 delete m_handlerFocus;
268 m_handlerFocus = new wxPopupFocusHandler(this);
269
270 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 271 }
326c7ea8
VZ
272
273#endif // !__WXX11__
c02ee97f
VZ
274}
275
276void wxPopupTransientWindow::Dismiss()
277{
278 PopHandlers();
279
280 Hide();
281}
282
283void wxPopupTransientWindow::DismissAndNotify()
284{
285 Dismiss();
286
287 OnDismiss();
288}
289
290void wxPopupTransientWindow::OnDismiss()
291{
292 // nothing to do here - but it may be interesting for derived class
293}
294
295bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
296{
297 // no special processing here
298 return FALSE;
299}
300
a8132cd4 301#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
302
303// ----------------------------------------------------------------------------
304// wxPopupComboWindow
305// ----------------------------------------------------------------------------
306
307wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
308 : wxPopupTransientWindow(parent)
309{
310 m_combo = parent;
311}
312
313bool wxPopupComboWindow::Create(wxComboControl *parent)
314{
315 m_combo = parent;
316
317 return wxPopupWindow::Create(parent);
318}
319
320void wxPopupComboWindow::PositionNearCombo()
321{
322 // the origin point must be in screen coords
323 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
324
89e7a223 325#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
326 // account for the fact that (0, 0) is not the top left corner of the
327 // window: there is also the border
328 wxRect rectBorders = m_combo->GetRenderer()->
329 GetBorderDimensions(m_combo->GetBorder());
330 ptOrigin.x -= rectBorders.x;
331 ptOrigin.y -= rectBorders.y;
332#endif // __WXUNIVERSAL__
333
334 // position below or above the combobox: the width is 0 to put it exactly
335 // below us, not to the left or to the right
336 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
337}
338
339void wxPopupComboWindow::OnDismiss()
340{
341 m_combo->OnDismiss();
342}
343
a8132cd4 344#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
345
346// ----------------------------------------------------------------------------
347// wxPopupWindowHandler
348// ----------------------------------------------------------------------------
349
350void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
351{
352 // let the window have it first (we're the first event handler in the chain
353 // of handlers for this window)
354 if ( m_popup->ProcessLeftDown(event) )
355 {
356 return;
357 }
326c7ea8 358
c02ee97f
VZ
359 wxPoint pos = event.GetPosition();
360
361 // scrollbar on which the click occured
362 wxWindow *sbar = NULL;
363
364 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 365
c02ee97f
VZ
366 switch ( win->HitTest(pos.x, pos.y) )
367 {
368 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
369 {
370 // do the coords translation now as after DismissAndNotify()
371 // m_popup may be destroyed
372 wxMouseEvent event2(event);
373
374 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
375
376 // clicking outside a popup dismisses it
377 m_popup->DismissAndNotify();
378
379 // dismissing a tooltip shouldn't waste a click, i.e. you
380 // should be able to dismiss it and press the button with the
381 // same click, so repost this event to the window beneath us
382 wxWindow *win = wxFindWindowAtPoint(event2.GetPosition());
383 if ( win )
384 {
385 // translate the event coords to the ones of the window
386 // which is going to get the event
387 win->ScreenToClient(&event2.m_x, &event2.m_y);
388
389 event2.SetEventObject(win);
390 wxPostEvent(win, event2);
391 }
392 }
c02ee97f
VZ
393 break;
394
a8132cd4 395#ifdef __WXUNIVERSAL__
c02ee97f
VZ
396 case wxHT_WINDOW_HORZ_SCROLLBAR:
397 sbar = win->GetScrollbar(wxHORIZONTAL);
398 break;
399
400 case wxHT_WINDOW_VERT_SCROLLBAR:
401 sbar = win->GetScrollbar(wxVERTICAL);
402 break;
a8132cd4 403#endif
c02ee97f
VZ
404
405 default:
406 // forgot to update the switch after adding a new hit test code?
407 wxFAIL_MSG( _T("unexpected HitTest() return value") );
408 // fall through
409
410 case wxHT_WINDOW_CORNER:
411 // don't actually know if this one is good for anything, but let it
412 // pass just in case
413
414 case wxHT_WINDOW_INSIDE:
415 // let the normal processing take place
416 event.Skip();
417 break;
418 }
419
420 if ( sbar )
421 {
422 // translate the event coordinates to the scrollbar ones
423 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
424
425 // and give the event to it
426 wxMouseEvent event2 = event;
427 event2.m_x = pos.x;
428 event2.m_y = pos.y;
429
430 (void)sbar->GetEventHandler()->ProcessEvent(event2);
431 }
432}
433
434// ----------------------------------------------------------------------------
435// wxPopupFocusHandler
436// ----------------------------------------------------------------------------
437
438void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
439{
516cdd54
VZ
440#ifdef __WXGTK__
441 // ignore the next OnKillFocus() call
442 if ( time(NULL) < m_creationTime + 1 )
443 {
444 event.Skip();
445
446 return;
447 }
448#endif // __WXGTK__
449
60178eb4
VZ
450 // when we lose focus we always disappear - unless it goes to the popup (in
451 // which case we don't really lose it)
36a56c65
VS
452 wxWindow *win = event.GetWindow();
453 while ( win )
454 {
455 if ( win == m_popup )
456 return;
457 win = win->GetParent();
458 }
326c7ea8 459
36a56c65 460 m_popup->DismissAndNotify();
60178eb4 461}
54800df8 462
d7cff34d 463void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4
VZ
464{
465 // let the window have it first, it might process the keys
466 if ( !m_popup->ProcessEvent(event) )
467 {
468 // by default, dismiss the popup
54800df8 469 m_popup->DismissAndNotify();
60178eb4 470 }
c02ee97f
VZ
471}
472
473#endif // wxUSE_POPUPWIN
eb729cd3 474