]> git.saurik.com Git - wxWidgets.git/blob - src/common/popupcmn.cpp
avoid conflict between wxBookCtrlBase::DoSetSelection() and the derived classes;...
[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 // License: 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 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/renderer.h"
39 #endif // __WXUNIVERSAL__
40
41 #ifdef __WXGTK__
42 #include <gtk/gtk.h>
43 #endif
44 #ifdef __WXX11__
45 #include "wx/x11/private.h"
46 #endif
47
48 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
50
51 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
53 #endif
54
55 // ----------------------------------------------------------------------------
56 // private classes
57 // ----------------------------------------------------------------------------
58
59 // event handlers which we use to intercept events which cause the popup to
60 // disappear
61 class wxPopupWindowHandler : public wxEvtHandler
62 {
63 public:
64 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
65
66 protected:
67 // event handlers
68 void OnLeftDown(wxMouseEvent& event);
69
70 private:
71 wxPopupTransientWindow *m_popup;
72
73 DECLARE_EVENT_TABLE()
74 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
75 };
76
77 class wxPopupFocusHandler : public wxEvtHandler
78 {
79 public:
80 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
81
82 protected:
83 void OnKillFocus(wxFocusEvent& event);
84 void OnKeyDown(wxKeyEvent& event);
85
86 private:
87 wxPopupTransientWindow *m_popup;
88
89 DECLARE_EVENT_TABLE()
90 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
91 };
92
93 // ----------------------------------------------------------------------------
94 // event tables
95 // ----------------------------------------------------------------------------
96
97 BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
98 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
99 END_EVENT_TABLE()
100
101 BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
102 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
103 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
104 END_EVENT_TABLE()
105
106 BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
107 #ifdef __WXMSW__
108 EVT_IDLE(wxPopupTransientWindow::OnIdle)
109 #endif
110 END_EVENT_TABLE()
111
112 // ============================================================================
113 // implementation
114 // ============================================================================
115
116 // ----------------------------------------------------------------------------
117 // wxPopupWindowBase
118 // ----------------------------------------------------------------------------
119
120 wxPopupWindowBase::~wxPopupWindowBase()
121 {
122 // this destructor is required for Darwin
123 }
124
125 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
126 {
127 return true;
128 }
129
130 void 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
170 void wxPopupTransientWindow::Init()
171 {
172 m_child =
173 m_focus = (wxWindow *)NULL;
174
175 m_handlerFocus = NULL;
176 m_handlerPopup = NULL;
177 }
178
179 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
180 {
181 Init();
182
183 (void)Create(parent, style);
184 }
185
186 wxPopupTransientWindow::~wxPopupTransientWindow()
187 {
188 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
189 PopHandlers();
190
191 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
192 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
193
194 delete m_handlerFocus;
195 delete m_handlerPopup;
196 }
197
198 void wxPopupTransientWindow::PopHandlers()
199 {
200 if ( m_child )
201 {
202 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
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 }
208 if (m_child->HasCapture())
209 {
210 m_child->ReleaseMouse();
211 }
212 m_child = NULL;
213 }
214
215 if ( m_focus )
216 {
217 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
218 {
219 // see above
220 m_handlerFocus = NULL;
221 }
222 }
223 m_focus = NULL;
224 }
225
226 void 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
238 Show();
239
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());
243
244 if (!m_handlerPopup)
245 m_handlerPopup = new wxPopupWindowHandler(this);
246
247 m_child->PushEventHandler(m_handlerPopup);
248
249 m_focus = winFocus ? winFocus : this;
250 m_focus->SetFocus();
251
252 #ifdef __WXMSW__
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
256 m_focus = FindFocus();
257 #elif defined(__WXGTK__)
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
263 if ( m_focus )
264 {
265 if (!m_handlerFocus)
266 m_handlerFocus = new wxPopupFocusHandler(this);
267
268 m_focus->PushEventHandler(m_handlerFocus);
269 }
270 }
271
272 bool wxPopupTransientWindow::Show( bool show )
273 {
274 #ifdef __WXGTK__
275 if (!show)
276 {
277 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
278
279 gtk_grab_remove( m_widget );
280 }
281 #endif
282
283 #ifdef __WXX11__
284 if (!show)
285 {
286 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
287 }
288 #endif
289
290 #ifdef __WXMSW__
291 if (!show && m_child && m_child->HasCapture())
292 {
293 m_child->ReleaseMouse();
294 }
295 #endif
296
297 bool ret = wxPopupWindow::Show( show );
298
299 #ifdef __WXGTK__
300 if (show)
301 {
302 gtk_grab_add( m_widget );
303
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 }
314 #endif
315
316 #ifdef __WXX11__
317 if (show)
318 {
319 Window xwindow = (Window) m_clientWindow;
320
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
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
340 return ret;
341 }
342
343 void wxPopupTransientWindow::Dismiss()
344 {
345 Hide();
346 PopHandlers();
347 }
348
349 void wxPopupTransientWindow::DismissAndNotify()
350 {
351 Dismiss();
352 OnDismiss();
353 }
354
355 void wxPopupTransientWindow::OnDismiss()
356 {
357 // nothing to do here - but it may be interesting for derived class
358 }
359
360 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
361 {
362 // no special processing here
363 return false;
364 }
365
366 #ifdef __WXMSW__
367 void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
368 {
369 event.Skip();
370
371 if (IsShown() && m_child)
372 {
373 wxPoint pos = ScreenToClient(wxGetMousePosition());
374 wxRect rect(GetSize());
375
376 if ( rect.Contains(pos) )
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 }
389 }
390 }
391 }
392 #endif // __WXMSW__
393
394
395 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
396
397 // ----------------------------------------------------------------------------
398 // wxPopupComboWindow
399 // ----------------------------------------------------------------------------
400
401 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
402 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
403 END_EVENT_TABLE()
404
405 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
406 : wxPopupTransientWindow(parent)
407 {
408 m_combo = parent;
409 }
410
411 bool wxPopupComboWindow::Create(wxComboCtrl *parent)
412 {
413 m_combo = parent;
414
415 return wxPopupWindow::Create(parent);
416 }
417
418 void wxPopupComboWindow::PositionNearCombo()
419 {
420 // the origin point must be in screen coords
421 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
422
423 #if 0 //def __WXUNIVERSAL__
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
437 void wxPopupComboWindow::OnDismiss()
438 {
439 m_combo->OnPopupDismiss();
440 }
441
442 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
443 {
444 m_combo->ProcessEvent(event);
445 }
446
447 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
448
449 // ----------------------------------------------------------------------------
450 // wxPopupWindowHandler
451 // ----------------------------------------------------------------------------
452
453 void 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 }
461
462 wxPoint pos = event.GetPosition();
463
464 // in non-Univ ports the system manages scrollbars for us
465 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
466 // scrollbar on which the click occurred
467 wxWindow *sbar = NULL;
468 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
469
470 wxWindow *win = (wxWindow *)event.GetEventObject();
471
472 switch ( win->HitTest(pos.x, pos.y) )
473 {
474 case wxHT_WINDOW_OUTSIDE:
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
488 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
489 if ( winUnder )
490 {
491 // translate the event coords to the ones of the window
492 // which is going to get the event
493 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
494
495 event2.SetEventObject(winUnder);
496 wxPostEvent(winUnder, event2);
497 }
498 }
499 break;
500
501 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
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;
509 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
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
526 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
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 }
539 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
540 }
541
542 // ----------------------------------------------------------------------------
543 // wxPopupFocusHandler
544 // ----------------------------------------------------------------------------
545
546 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
547 {
548 // when we lose focus we always disappear - unless it goes to the popup (in
549 // which case we don't really lose it)
550 wxWindow *win = event.GetWindow();
551 while ( win )
552 {
553 if ( win == m_popup )
554 return;
555 win = win->GetParent();
556 }
557
558 m_popup->DismissAndNotify();
559 }
560
561 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
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
567 m_popup->DismissAndNotify();
568 }
569 }
570
571 #endif // wxUSE_POPUPWIN