]> git.saurik.com Git - wxWidgets.git/blob - src/common/popupcmn.cpp
Add wxDataViewCtrl::GTKPathToItem() function and use it.
[wxWidgets.git] / src / common / popupcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_POPUPWIN
28
29 #include "wx/popupwin.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/combobox.h" // wxComboCtrl
33 #include "wx/app.h" // wxPostEvent
34 #include "wx/log.h"
35 #endif //WX_PRECOMP
36
37 #include "wx/display.h"
38 #include "wx/recguard.h"
39
40 #ifdef __WXUNIVERSAL__
41 #include "wx/univ/renderer.h"
42 #include "wx/scrolbar.h"
43 #endif // __WXUNIVERSAL__
44
45 #ifdef __WXGTK__
46 #include <gtk/gtk.h>
47 #elif defined(__WXMSW__)
48 #include "wx/msw/private.h"
49 #elif defined(__WXX11__)
50 #include "wx/x11/private.h"
51 #endif
52
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
54 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
55
56 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
57 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
58 #endif
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // event handlers which we use to intercept events which cause the popup to
65 // disappear
66 class wxPopupWindowHandler : public wxEvtHandler
67 {
68 public:
69 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
70
71 protected:
72 // event handlers
73 void OnLeftDown(wxMouseEvent& event);
74
75 private:
76 wxPopupTransientWindow *m_popup;
77
78 DECLARE_EVENT_TABLE()
79 wxDECLARE_NO_COPY_CLASS(wxPopupWindowHandler);
80 };
81
82 class wxPopupFocusHandler : public wxEvtHandler
83 {
84 public:
85 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
86
87 protected:
88 void OnKillFocus(wxFocusEvent& event);
89 void OnChar(wxKeyEvent& event);
90
91 private:
92 wxPopupTransientWindow *m_popup;
93
94 DECLARE_EVENT_TABLE()
95 wxDECLARE_NO_COPY_CLASS(wxPopupFocusHandler);
96 };
97
98 // ----------------------------------------------------------------------------
99 // event tables
100 // ----------------------------------------------------------------------------
101
102 BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
103 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
104 END_EVENT_TABLE()
105
106 BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
107 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
108 EVT_CHAR(wxPopupFocusHandler::OnChar)
109 END_EVENT_TABLE()
110
111 BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
112 #if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
113 EVT_IDLE(wxPopupTransientWindow::OnIdle)
114 #endif
115 END_EVENT_TABLE()
116
117 // ============================================================================
118 // implementation
119 // ============================================================================
120
121 // ----------------------------------------------------------------------------
122 // wxPopupWindowBase
123 // ----------------------------------------------------------------------------
124
125 wxPopupWindowBase::~wxPopupWindowBase()
126 {
127 // this destructor is required for Darwin
128 }
129
130 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
131 {
132 return true;
133 }
134
135 void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
136 const wxSize& size)
137 {
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();
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;
162 if ( y + sizeSelf.y > posScreen.y + sizeScreen.y )
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
174 wxCoord x = ptOrigin.x;
175
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
185
186 if ( x + sizeSelf.x > posScreen.x + sizeScreen.x )
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
204 void wxPopupTransientWindow::Init()
205 {
206 m_child =
207 m_focus = NULL;
208
209 m_handlerFocus = NULL;
210 m_handlerPopup = NULL;
211 }
212
213 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
214 {
215 Init();
216
217 (void)Create(parent, style);
218 }
219
220 wxPopupTransientWindow::~wxPopupTransientWindow()
221 {
222 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
223 PopHandlers();
224
225 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
226 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
227
228 delete m_handlerFocus;
229 delete m_handlerPopup;
230 }
231
232 void wxPopupTransientWindow::PopHandlers()
233 {
234 if ( m_child )
235 {
236 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
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 }
242 if (m_child->HasCapture())
243 {
244 m_child->ReleaseMouse();
245 }
246 m_child = NULL;
247 }
248
249 if ( m_focus )
250 {
251 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
252 {
253 // see above
254 m_handlerFocus = NULL;
255 }
256 }
257 m_focus = NULL;
258 }
259
260 void 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
272 Show();
273
274 // There is a problem if these are still in use
275 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
276 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
277
278 if (!m_handlerPopup)
279 m_handlerPopup = new wxPopupWindowHandler(this);
280
281 m_child->PushEventHandler(m_handlerPopup);
282
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 }
293
294 #if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_CARBON)
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
298 m_focus = FindFocus();
299 #elif defined(__WXGTK__)
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
305 if ( m_focus )
306 {
307 if (!m_handlerFocus)
308 m_handlerFocus = new wxPopupFocusHandler(this);
309
310 m_focus->PushEventHandler(m_handlerFocus);
311 }
312 }
313
314 bool wxPopupTransientWindow::Show( bool show )
315 {
316 #ifdef __WXGTK__
317 if (!show)
318 {
319 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
320
321 gtk_grab_remove( m_widget );
322 }
323 #endif
324
325 #ifdef __WXX11__
326 if (!show)
327 {
328 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
329 }
330 #endif
331
332 #if defined( __WXMSW__ ) || defined( __WXMAC__)
333 if (!show && m_child && m_child->HasCapture())
334 {
335 m_child->ReleaseMouse();
336 }
337 #endif
338
339 bool ret = wxPopupWindow::Show( show );
340
341 #ifdef __WXGTK__
342 if (show)
343 {
344 gtk_grab_add( m_widget );
345
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),
352 NULL,
353 NULL,
354 (guint32)GDK_CURRENT_TIME );
355 }
356 #endif
357
358 #ifdef __WXX11__
359 if (show)
360 {
361 Window xwindow = (Window) m_clientWindow;
362
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
373
374 #if defined( __WXMSW__ ) || defined( __WXMAC__)
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
382 return ret;
383 }
384
385 void wxPopupTransientWindow::Dismiss()
386 {
387 Hide();
388 PopHandlers();
389 }
390
391 void wxPopupTransientWindow::DismissAndNotify()
392 {
393 Dismiss();
394 OnDismiss();
395 }
396
397 void wxPopupTransientWindow::OnDismiss()
398 {
399 // nothing to do here - but it may be interesting for derived class
400 }
401
402 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
403 {
404 // no special processing here
405 return false;
406 }
407
408 #if defined( __WXMSW__ ) || ( defined( __WXMAC__ ) && wxOSX_USE_CARBON )
409 void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
410 {
411 event.Skip();
412
413 if (IsShown() && m_child)
414 {
415 wxPoint pos = ScreenToClient(wxGetMousePosition());
416 wxRect rect(GetSize());
417
418 if ( rect.Contains(pos) )
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 }
431 }
432 }
433 }
434 #endif // __WXMSW__
435
436
437 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
438
439 // ----------------------------------------------------------------------------
440 // wxPopupComboWindow
441 // ----------------------------------------------------------------------------
442
443 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
444 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
445 END_EVENT_TABLE()
446
447 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
448 : wxPopupTransientWindow(parent)
449 {
450 m_combo = parent;
451 }
452
453 bool wxPopupComboWindow::Create(wxComboCtrl *parent)
454 {
455 m_combo = parent;
456
457 return wxPopupWindow::Create(parent);
458 }
459
460 void wxPopupComboWindow::PositionNearCombo()
461 {
462 // the origin point must be in screen coords
463 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
464
465 #if 0 //def __WXUNIVERSAL__
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
479 void wxPopupComboWindow::OnDismiss()
480 {
481 m_combo->OnPopupDismiss(true);
482 }
483
484 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
485 {
486 m_combo->ProcessWindowEvent(event);
487 }
488
489 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
490
491 // ----------------------------------------------------------------------------
492 // wxPopupWindowHandler
493 // ----------------------------------------------------------------------------
494
495 void 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 }
503
504 wxPoint pos = event.GetPosition();
505
506 // in non-Univ ports the system manages scrollbars for us
507 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
508 // scrollbar on which the click occurred
509 wxWindow *sbar = NULL;
510 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
511
512 wxWindow *win = (wxWindow *)event.GetEventObject();
513
514 switch ( win->HitTest(pos.x, pos.y) )
515 {
516 case wxHT_WINDOW_OUTSIDE:
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
530 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
531 if ( winUnder )
532 {
533 // translate the event coords to the ones of the window
534 // which is going to get the event
535 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
536
537 event2.SetEventObject(winUnder);
538 wxPostEvent(winUnder->GetEventHandler(), event2);
539 }
540 }
541 break;
542
543 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
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;
551 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
552
553 default:
554 // forgot to update the switch after adding a new hit test code?
555 wxFAIL_MSG( wxT("unexpected HitTest() return value") );
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
568 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
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 }
581 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
582 }
583
584 // ----------------------------------------------------------------------------
585 // wxPopupFocusHandler
586 // ----------------------------------------------------------------------------
587
588 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
589 {
590 // when we lose focus we always disappear - unless it goes to the popup (in
591 // which case we don't really lose it)
592 wxWindow *win = event.GetWindow();
593 while ( win )
594 {
595 if ( win == m_popup )
596 return;
597 win = win->GetParent();
598 }
599
600 m_popup->DismissAndNotify();
601 }
602
603 void wxPopupFocusHandler::OnChar(wxKeyEvent& event)
604 {
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
615 // let the window have it first, it might process the keys
616 if ( !m_popup->GetEventHandler()->ProcessEvent(event) )
617 {
618 // by default, dismiss the popup
619 m_popup->DismissAndNotify();
620 }
621 }
622
623 #endif // wxUSE_POPUPWIN