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