]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
avoid conflict between wxBookCtrlBase::DoSetSelection() and the derived classes;...
[wxWidgets.git] / src / common / popupcmn.cpp
CommitLineData
c02ee97f 1///////////////////////////////////////////////////////////////////////////////
670f9935 2// Name: src/common/popupcmn.cpp
c02ee97f
VZ
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>
65571936 9// License: wxWindows licence
c02ee97f
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
c02ee97f
VZ
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
833a51f6 27#if wxUSE_POPUPWIN
c02ee97f
VZ
28
29#include "wx/popupwin.h"
30
31#ifndef WX_PRECOMP
a57d600f 32 #include "wx/combobox.h" // wxComboCtrl
6a0fab3a 33 #include "wx/app.h" // wxPostEvent
ee351013 34 #include "wx/log.h"
c02ee97f
VZ
35#endif //WX_PRECOMP
36
37#ifdef __WXUNIVERSAL__
38 #include "wx/univ/renderer.h"
39#endif // __WXUNIVERSAL__
40
537a0880
RR
41#ifdef __WXGTK__
42 #include <gtk/gtk.h>
43#endif
c51c4752
RR
44#ifdef __WXX11__
45#include "wx/x11/private.h"
46#endif
eb729cd3 47
537a0880 48IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
6c3422e9 49IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
761df41e
VZ
50
51#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
53#endif
6c3422e9 54
c02ee97f
VZ
55// ----------------------------------------------------------------------------
56// private classes
57// ----------------------------------------------------------------------------
58
59// event handlers which we use to intercept events which cause the popup to
60// disappear
61class wxPopupWindowHandler : public wxEvtHandler
62{
63public:
276612f7 64 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
65
66protected:
67 // event handlers
68 void OnLeftDown(wxMouseEvent& event);
69
70private:
71 wxPopupTransientWindow *m_popup;
72
73 DECLARE_EVENT_TABLE()
22f3361e 74 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
c02ee97f
VZ
75};
76
77class wxPopupFocusHandler : public wxEvtHandler
78{
79public:
276612f7 80 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
81
82protected:
c02ee97f 83 void OnKillFocus(wxFocusEvent& event);
d7cff34d 84 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
85
86private:
87 wxPopupTransientWindow *m_popup;
88
89 DECLARE_EVENT_TABLE()
22f3361e 90 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
c02ee97f
VZ
91};
92
93// ----------------------------------------------------------------------------
94// event tables
95// ----------------------------------------------------------------------------
96
97BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
98 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
99END_EVENT_TABLE()
100
101BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
102 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
d7cff34d 103 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
104END_EVENT_TABLE()
105
414f2513
RD
106BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
107#ifdef __WXMSW__
108 EVT_IDLE(wxPopupTransientWindow::OnIdle)
109#endif
110END_EVENT_TABLE()
111
c02ee97f
VZ
112// ============================================================================
113// implementation
114// ============================================================================
115
116// ----------------------------------------------------------------------------
117// wxPopupWindowBase
118// ----------------------------------------------------------------------------
119
799ea011
GD
120wxPopupWindowBase::~wxPopupWindowBase()
121{
122 // this destructor is required for Darwin
123}
124
c02ee97f
VZ
125bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
126{
7e548f6b 127 return true;
c02ee97f
VZ
128}
129
130void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
131 const wxSize& size)
132{
133 wxSize sizeScreen = wxGetDisplaySize(),
134 sizeSelf = GetSize();
135
136 // is there enough space to put the popup below the window (where we put it
137 // by default)?
138 wxCoord y = ptOrigin.y + size.y;
139 if ( y + sizeSelf.y > sizeScreen.y )
140 {
141 // check if there is enough space above
142 if ( ptOrigin.y > sizeSelf.y )
143 {
144 // do position the control above the window
145 y -= size.y + sizeSelf.y;
146 }
147 //else: not enough space below nor above, leave below
148 }
149
150 // now check left/right too
151 wxCoord x = ptOrigin.x + size.x;
152 if ( x + sizeSelf.x > sizeScreen.x )
153 {
154 // check if there is enough space to the left
155 if ( ptOrigin.x > sizeSelf.x )
156 {
157 // do position the control to the left
158 x -= size.x + sizeSelf.x;
159 }
160 //else: not enough space there neither, leave in default position
161 }
162
163 Move(x, y, wxSIZE_NO_ADJUSTMENTS);
164}
165
166// ----------------------------------------------------------------------------
167// wxPopupTransientWindow
168// ----------------------------------------------------------------------------
169
170void wxPopupTransientWindow::Init()
171{
172 m_child =
173 m_focus = (wxWindow *)NULL;
d7cff34d
VZ
174
175 m_handlerFocus = NULL;
176 m_handlerPopup = NULL;
c02ee97f
VZ
177}
178
758bce95 179wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
180{
181 Init();
182
758bce95 183 (void)Create(parent, style);
c02ee97f
VZ
184}
185
186wxPopupTransientWindow::~wxPopupTransientWindow()
187{
0bf16170
MW
188 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
189 PopHandlers();
17a1ebd1 190
0bf16170
MW
191 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
192 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
193
194 delete m_handlerFocus;
195 delete m_handlerPopup;
c02ee97f
VZ
196}
197
198void wxPopupTransientWindow::PopHandlers()
199{
200 if ( m_child )
201 {
0bf16170 202 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
d7cff34d
VZ
203 {
204 // something is very wrong and someone else probably deleted our
205 // handler - so don't risk deleting it second time
206 m_handlerPopup = NULL;
207 }
414f2513 208 if (m_child->HasCapture())
17a1ebd1 209 {
414f2513
RD
210 m_child->ReleaseMouse();
211 }
c02ee97f
VZ
212 m_child = NULL;
213 }
214
215 if ( m_focus )
216 {
0bf16170 217 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
d7cff34d
VZ
218 {
219 // see above
220 m_handlerFocus = NULL;
221 }
c02ee97f 222 }
537a0880 223 m_focus = NULL;
c02ee97f
VZ
224}
225
226void wxPopupTransientWindow::Popup(wxWindow *winFocus)
227{
228 const wxWindowList& children = GetChildren();
229 if ( children.GetCount() )
230 {
231 m_child = children.GetFirst()->GetData();
232 }
233 else
234 {
235 m_child = this;
236 }
237
e4606ed9 238 Show();
e4606ed9 239
0bf16170
MW
240 // There is is a problem if these are still in use
241 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
242 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
276612f7 243
0bf16170
MW
244 if (!m_handlerPopup)
245 m_handlerPopup = new wxPopupWindowHandler(this);
d7cff34d 246
d7cff34d 247 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 248
c02ee97f 249 m_focus = winFocus ? winFocus : this;
c02ee97f 250 m_focus->SetFocus();
60178eb4 251
516cdd54 252#ifdef __WXMSW__
d7cff34d
VZ
253 // MSW doesn't allow to set focus to the popup window, but we need to
254 // subclass the window which has the focus, and not winFocus passed in or
255 // otherwise everything else breaks down
516cdd54 256 m_focus = FindFocus();
8c624a14 257#elif defined(__WXGTK__)
0bf16170
MW
258 // GTK+ catches the activate events from the popup
259 // window, not the focus events from the child window
260 m_focus = this;
261#endif
262
60178eb4
VZ
263 if ( m_focus )
264 {
0bf16170
MW
265 if (!m_handlerFocus)
266 m_handlerFocus = new wxPopupFocusHandler(this);
d7cff34d
VZ
267
268 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 269 }
537a0880
RR
270}
271
272bool wxPopupTransientWindow::Show( bool show )
273{
274#ifdef __WXGTK__
275 if (!show)
f4bb632c
RR
276 {
277 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
2997ca30 278
537a0880 279 gtk_grab_remove( m_widget );
f4bb632c 280 }
537a0880
RR
281#endif
282
c51c4752
RR
283#ifdef __WXX11__
284 if (!show)
285 {
286 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
287 }
288#endif
289
414f2513
RD
290#ifdef __WXMSW__
291 if (!show && m_child && m_child->HasCapture())
17a1ebd1 292 {
414f2513
RD
293 m_child->ReleaseMouse();
294 }
295#endif
17a1ebd1 296
537a0880 297 bool ret = wxPopupWindow::Show( show );
2997ca30 298
537a0880
RR
299#ifdef __WXGTK__
300 if (show)
f4bb632c 301 {
537a0880 302 gtk_grab_add( m_widget );
2997ca30 303
f4bb632c
RR
304 gdk_pointer_grab( m_widget->window, TRUE,
305 (GdkEventMask)
306 (GDK_BUTTON_PRESS_MASK |
307 GDK_BUTTON_RELEASE_MASK |
308 GDK_POINTER_MOTION_HINT_MASK |
309 GDK_POINTER_MOTION_MASK),
310 (GdkWindow *) NULL,
311 (GdkCursor *) NULL,
312 (guint32)GDK_CURRENT_TIME );
313 }
537a0880
RR
314#endif
315
c51c4752
RR
316#ifdef __WXX11__
317 if (show)
318 {
319 Window xwindow = (Window) m_clientWindow;
2997ca30 320
c51c4752
RR
321 /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow,
322 True,
323 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask,
324 GrabModeAsync,
325 GrabModeAsync,
326 None,
327 None,
328 CurrentTime );
329 }
330#endif
414f2513
RD
331
332#ifdef __WXMSW__
333 if (show && m_child)
334 {
335 // Assume that the mouse is outside the popup to begin with
336 m_child->CaptureMouse();
337 }
338#endif
339
537a0880 340 return ret;
c02ee97f
VZ
341}
342
343void wxPopupTransientWindow::Dismiss()
344{
c02ee97f 345 Hide();
414f2513 346 PopHandlers();
c02ee97f
VZ
347}
348
349void wxPopupTransientWindow::DismissAndNotify()
350{
351 Dismiss();
c02ee97f
VZ
352 OnDismiss();
353}
354
355void wxPopupTransientWindow::OnDismiss()
356{
357 // nothing to do here - but it may be interesting for derived class
358}
359
360bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
361{
362 // no special processing here
7e548f6b 363 return false;
c02ee97f
VZ
364}
365
414f2513
RD
366#ifdef __WXMSW__
367void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
368{
369 event.Skip();
370
371 if (IsShown() && m_child)
372 {
373 wxPoint pos = ScreenToClient(wxGetMousePosition());
80797568 374 wxRect rect(GetSize());
414f2513 375
22a35096 376 if ( rect.Contains(pos) )
414f2513
RD
377 {
378 if ( m_child->HasCapture() )
379 {
380 m_child->ReleaseMouse();
381 }
382 }
383 else
384 {
385 if ( !m_child->HasCapture() )
386 {
387 m_child->CaptureMouse();
388 }
17a1ebd1 389 }
414f2513
RD
390 }
391}
17a1ebd1 392#endif // __WXMSW__
414f2513
RD
393
394
a8132cd4 395#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
396
397// ----------------------------------------------------------------------------
398// wxPopupComboWindow
399// ----------------------------------------------------------------------------
400
e0c83227
VZ
401BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
402 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
403END_EVENT_TABLE()
404
a57d600f 405wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
c02ee97f
VZ
406 : wxPopupTransientWindow(parent)
407{
408 m_combo = parent;
409}
410
a57d600f 411bool wxPopupComboWindow::Create(wxComboCtrl *parent)
c02ee97f
VZ
412{
413 m_combo = parent;
414
415 return wxPopupWindow::Create(parent);
416}
417
418void wxPopupComboWindow::PositionNearCombo()
419{
420 // the origin point must be in screen coords
c47addef 421 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
c02ee97f 422
89e7a223 423#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
424 // account for the fact that (0, 0) is not the top left corner of the
425 // window: there is also the border
426 wxRect rectBorders = m_combo->GetRenderer()->
427 GetBorderDimensions(m_combo->GetBorder());
428 ptOrigin.x -= rectBorders.x;
429 ptOrigin.y -= rectBorders.y;
430#endif // __WXUNIVERSAL__
431
432 // position below or above the combobox: the width is 0 to put it exactly
433 // below us, not to the left or to the right
434 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
435}
436
437void wxPopupComboWindow::OnDismiss()
438{
a340b80d 439 m_combo->OnPopupDismiss();
c02ee97f
VZ
440}
441
e0c83227
VZ
442void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
443{
444 m_combo->ProcessEvent(event);
445}
446
a8132cd4 447#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
448
449// ----------------------------------------------------------------------------
450// wxPopupWindowHandler
451// ----------------------------------------------------------------------------
452
453void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
454{
455 // let the window have it first (we're the first event handler in the chain
456 // of handlers for this window)
457 if ( m_popup->ProcessLeftDown(event) )
458 {
459 return;
460 }
326c7ea8 461
c02ee97f
VZ
462 wxPoint pos = event.GetPosition();
463
af5c81b3 464 // in non-Univ ports the system manages scrollbars for us
9a6384ca 465#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
3103e8a9 466 // scrollbar on which the click occurred
c02ee97f 467 wxWindow *sbar = NULL;
9a6384ca 468#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
469
470 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 471
c02ee97f
VZ
472 switch ( win->HitTest(pos.x, pos.y) )
473 {
474 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
475 {
476 // do the coords translation now as after DismissAndNotify()
477 // m_popup may be destroyed
478 wxMouseEvent event2(event);
479
480 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
481
482 // clicking outside a popup dismisses it
483 m_popup->DismissAndNotify();
484
485 // dismissing a tooltip shouldn't waste a click, i.e. you
486 // should be able to dismiss it and press the button with the
487 // same click, so repost this event to the window beneath us
17a1ebd1
VZ
488 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
489 if ( winUnder )
326c7ea8
VZ
490 {
491 // translate the event coords to the ones of the window
492 // which is going to get the event
17a1ebd1 493 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
326c7ea8 494
17a1ebd1
VZ
495 event2.SetEventObject(winUnder);
496 wxPostEvent(winUnder, event2);
326c7ea8
VZ
497 }
498 }
c02ee97f
VZ
499 break;
500
9a6384ca 501#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
502 case wxHT_WINDOW_HORZ_SCROLLBAR:
503 sbar = win->GetScrollbar(wxHORIZONTAL);
504 break;
505
506 case wxHT_WINDOW_VERT_SCROLLBAR:
507 sbar = win->GetScrollbar(wxVERTICAL);
508 break;
9a6384ca 509#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
510
511 default:
512 // forgot to update the switch after adding a new hit test code?
513 wxFAIL_MSG( _T("unexpected HitTest() return value") );
514 // fall through
515
516 case wxHT_WINDOW_CORNER:
517 // don't actually know if this one is good for anything, but let it
518 // pass just in case
519
520 case wxHT_WINDOW_INSIDE:
521 // let the normal processing take place
522 event.Skip();
523 break;
524 }
525
9a6384ca 526#if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
c02ee97f
VZ
527 if ( sbar )
528 {
529 // translate the event coordinates to the scrollbar ones
530 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
531
532 // and give the event to it
533 wxMouseEvent event2 = event;
534 event2.m_x = pos.x;
535 event2.m_y = pos.y;
536
537 (void)sbar->GetEventHandler()->ProcessEvent(event2);
538 }
9a6384ca 539#endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
c02ee97f
VZ
540}
541
542// ----------------------------------------------------------------------------
543// wxPopupFocusHandler
544// ----------------------------------------------------------------------------
545
546void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
547{
60178eb4
VZ
548 // when we lose focus we always disappear - unless it goes to the popup (in
549 // which case we don't really lose it)
36a56c65
VS
550 wxWindow *win = event.GetWindow();
551 while ( win )
552 {
553 if ( win == m_popup )
554 return;
555 win = win->GetParent();
556 }
326c7ea8 557
36a56c65 558 m_popup->DismissAndNotify();
60178eb4 559}
54800df8 560
d7cff34d 561void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4
VZ
562{
563 // let the window have it first, it might process the keys
564 if ( !m_popup->ProcessEvent(event) )
565 {
566 // by default, dismiss the popup
54800df8 567 m_popup->DismissAndNotify();
60178eb4 568 }
c02ee97f
VZ
569}
570
571#endif // wxUSE_POPUPWIN