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