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