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