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