]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
Commented out call that breaks metal style setting before Create
[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
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
32 #include "wx/combobox.h" // wxComboControl
6a0fab3a 33 #include "wx/app.h" // wxPostEvent
ee351013 34 #include "wx/log.h"
3390759e 35 #include "wx/app.h"
c02ee97f
VZ
36#endif //WX_PRECOMP
37
38#ifdef __WXUNIVERSAL__
39 #include "wx/univ/renderer.h"
40#endif // __WXUNIVERSAL__
41
537a0880
RR
42#ifdef __WXGTK__
43 #include <gtk/gtk.h>
44#endif
c51c4752
RR
45#ifdef __WXX11__
46#include "wx/x11/private.h"
47#endif
eb729cd3 48
537a0880 49IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
6c3422e9 50IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
761df41e
VZ
51
52#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
54#endif
6c3422e9 55
c02ee97f
VZ
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:
276612f7 65 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
66
67protected:
68 // event handlers
69 void OnLeftDown(wxMouseEvent& event);
70
71private:
72 wxPopupTransientWindow *m_popup;
73
74 DECLARE_EVENT_TABLE()
22f3361e 75 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
c02ee97f
VZ
76};
77
78class wxPopupFocusHandler : public wxEvtHandler
79{
80public:
276612f7 81 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
c02ee97f
VZ
82
83protected:
c02ee97f 84 void OnKillFocus(wxFocusEvent& event);
d7cff34d 85 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
86
87private:
88 wxPopupTransientWindow *m_popup;
89
90 DECLARE_EVENT_TABLE()
22f3361e 91 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
c02ee97f
VZ
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)
d7cff34d 104 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
105END_EVENT_TABLE()
106
414f2513
RD
107BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
108#ifdef __WXMSW__
109 EVT_IDLE(wxPopupTransientWindow::OnIdle)
110#endif
111END_EVENT_TABLE()
112
c02ee97f
VZ
113// ============================================================================
114// implementation
115// ============================================================================
116
117// ----------------------------------------------------------------------------
118// wxPopupWindowBase
119// ----------------------------------------------------------------------------
120
799ea011
GD
121wxPopupWindowBase::~wxPopupWindowBase()
122{
123 // this destructor is required for Darwin
124}
125
c02ee97f
VZ
126bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
127{
7e548f6b 128 return true;
c02ee97f
VZ
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;
d7cff34d
VZ
175
176 m_handlerFocus = NULL;
177 m_handlerPopup = NULL;
c02ee97f
VZ
178}
179
758bce95 180wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
181{
182 Init();
183
758bce95 184 (void)Create(parent, style);
c02ee97f
VZ
185}
186
187wxPopupTransientWindow::~wxPopupTransientWindow()
188{
0bf16170
MW
189 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
190 PopHandlers();
17a1ebd1 191
0bf16170
MW
192 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
193 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
194
195 delete m_handlerFocus;
196 delete m_handlerPopup;
c02ee97f
VZ
197}
198
199void wxPopupTransientWindow::PopHandlers()
200{
201 if ( m_child )
202 {
0bf16170 203 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
d7cff34d
VZ
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 }
414f2513 209 if (m_child->HasCapture())
17a1ebd1 210 {
414f2513
RD
211 m_child->ReleaseMouse();
212 }
c02ee97f
VZ
213 m_child = NULL;
214 }
215
216 if ( m_focus )
217 {
0bf16170 218 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
d7cff34d
VZ
219 {
220 // see above
221 m_handlerFocus = NULL;
222 }
c02ee97f 223 }
537a0880 224 m_focus = NULL;
c02ee97f
VZ
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
e4606ed9 239 Show();
e4606ed9 240
0bf16170
MW
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());
276612f7 244
0bf16170
MW
245 if (!m_handlerPopup)
246 m_handlerPopup = new wxPopupWindowHandler(this);
d7cff34d 247
d7cff34d 248 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 249
c02ee97f 250 m_focus = winFocus ? winFocus : this;
c02ee97f 251 m_focus->SetFocus();
60178eb4 252
516cdd54 253#ifdef __WXMSW__
d7cff34d
VZ
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
516cdd54 257 m_focus = FindFocus();
8c624a14 258#elif defined(__WXGTK__)
0bf16170
MW
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
60178eb4
VZ
264 if ( m_focus )
265 {
0bf16170
MW
266 if (!m_handlerFocus)
267 m_handlerFocus = new wxPopupFocusHandler(this);
d7cff34d
VZ
268
269 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 270 }
537a0880
RR
271}
272
273bool wxPopupTransientWindow::Show( bool show )
274{
275#ifdef __WXGTK__
276 if (!show)
f4bb632c
RR
277 {
278 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
2997ca30 279
537a0880 280 gtk_grab_remove( m_widget );
f4bb632c 281 }
537a0880
RR
282#endif
283
c51c4752
RR
284#ifdef __WXX11__
285 if (!show)
286 {
287 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
288 }
289#endif
290
414f2513
RD
291#ifdef __WXMSW__
292 if (!show && m_child && m_child->HasCapture())
17a1ebd1 293 {
414f2513
RD
294 m_child->ReleaseMouse();
295 }
296#endif
17a1ebd1 297
537a0880 298 bool ret = wxPopupWindow::Show( show );
2997ca30 299
537a0880
RR
300#ifdef __WXGTK__
301 if (show)
f4bb632c 302 {
537a0880 303 gtk_grab_add( m_widget );
2997ca30 304
f4bb632c
RR
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 }
537a0880
RR
315#endif
316
c51c4752
RR
317#ifdef __WXX11__
318 if (show)
319 {
320 Window xwindow = (Window) m_clientWindow;
2997ca30 321
c51c4752
RR
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
414f2513
RD
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
537a0880 341 return ret;
c02ee97f
VZ
342}
343
344void wxPopupTransientWindow::Dismiss()
345{
c02ee97f 346 Hide();
414f2513 347 PopHandlers();
c02ee97f
VZ
348}
349
350void wxPopupTransientWindow::DismissAndNotify()
351{
352 Dismiss();
c02ee97f
VZ
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
7e548f6b 364 return false;
c02ee97f
VZ
365}
366
414f2513
RD
367#ifdef __WXMSW__
368void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
369{
370 event.Skip();
371
372 if (IsShown() && m_child)
373 {
374 wxPoint pos = ScreenToClient(wxGetMousePosition());
80797568 375 wxRect rect(GetSize());
414f2513
RD
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 }
17a1ebd1 390 }
414f2513
RD
391 }
392}
17a1ebd1 393#endif // __WXMSW__
414f2513
RD
394
395
a8132cd4 396#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
397
398// ----------------------------------------------------------------------------
399// wxPopupComboWindow
400// ----------------------------------------------------------------------------
401
e0c83227
VZ
402BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
403 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
404END_EVENT_TABLE()
405
c02ee97f
VZ
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
c47addef 422 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
c02ee97f 423
89e7a223 424#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
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
e0c83227
VZ
443void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
444{
445 m_combo->ProcessEvent(event);
446}
447
a8132cd4 448#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
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 }
326c7ea8 462
c02ee97f
VZ
463 wxPoint pos = event.GetPosition();
464
af5c81b3
VZ
465 // in non-Univ ports the system manages scrollbars for us
466#ifdef __WXUNIVERSAL__
3103e8a9 467 // scrollbar on which the click occurred
c02ee97f 468 wxWindow *sbar = NULL;
af5c81b3 469#endif // __WXUNIVERSAL__
c02ee97f
VZ
470
471 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 472
c02ee97f
VZ
473 switch ( win->HitTest(pos.x, pos.y) )
474 {
475 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
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
17a1ebd1
VZ
489 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
490 if ( winUnder )
326c7ea8
VZ
491 {
492 // translate the event coords to the ones of the window
493 // which is going to get the event
17a1ebd1 494 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
326c7ea8 495
17a1ebd1
VZ
496 event2.SetEventObject(winUnder);
497 wxPostEvent(winUnder, event2);
326c7ea8
VZ
498 }
499 }
c02ee97f
VZ
500 break;
501
a8132cd4 502#ifdef __WXUNIVERSAL__
c02ee97f
VZ
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;
af5c81b3 510#endif // __WXUNIVERSAL__
c02ee97f
VZ
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
af5c81b3 527#ifdef __WXUNIVERSAL__
c02ee97f
VZ
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 }
af5c81b3 540#endif // __WXUNIVERSAL__
c02ee97f
VZ
541}
542
543// ----------------------------------------------------------------------------
544// wxPopupFocusHandler
545// ----------------------------------------------------------------------------
546
547void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
548{
60178eb4
VZ
549 // when we lose focus we always disappear - unless it goes to the popup (in
550 // which case we don't really lose it)
36a56c65
VS
551 wxWindow *win = event.GetWindow();
552 while ( win )
553 {
554 if ( win == m_popup )
555 return;
556 win = win->GetParent();
557 }
326c7ea8 558
36a56c65 559 m_popup->DismissAndNotify();
60178eb4 560}
54800df8 561
d7cff34d 562void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4
VZ
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
54800df8 568 m_popup->DismissAndNotify();
60178eb4 569 }
c02ee97f
VZ
570}
571
572#endif // wxUSE_POPUPWIN
eb729cd3 573