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