]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
Warning and build fixes focused with Borland and Watcom.
[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:
69 wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
70
71protected:
72 // event handlers
73 void OnLeftDown(wxMouseEvent& event);
74
75private:
76 wxPopupTransientWindow *m_popup;
77
78 DECLARE_EVENT_TABLE()
22f3361e 79 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
c02ee97f
VZ
80};
81
82class wxPopupFocusHandler : public wxEvtHandler
83{
84public:
516cdd54
VZ
85 wxPopupFocusHandler(wxPopupTransientWindow *popup)
86 {
87 m_popup = popup;
516cdd54 88 }
c02ee97f
VZ
89
90protected:
c02ee97f 91 void OnKillFocus(wxFocusEvent& event);
d7cff34d 92 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
93
94private:
95 wxPopupTransientWindow *m_popup;
96
97 DECLARE_EVENT_TABLE()
22f3361e 98 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
c02ee97f
VZ
99};
100
101// ----------------------------------------------------------------------------
102// event tables
103// ----------------------------------------------------------------------------
104
105BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
106 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
107END_EVENT_TABLE()
108
109BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
110 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
d7cff34d 111 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
112END_EVENT_TABLE()
113
114// ============================================================================
115// implementation
116// ============================================================================
117
118// ----------------------------------------------------------------------------
119// wxPopupWindowBase
120// ----------------------------------------------------------------------------
121
799ea011
GD
122wxPopupWindowBase::~wxPopupWindowBase()
123{
124 // this destructor is required for Darwin
125}
126
c02ee97f
VZ
127bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
128{
7e548f6b 129 return true;
c02ee97f
VZ
130}
131
132void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
133 const wxSize& size)
134{
135 wxSize sizeScreen = wxGetDisplaySize(),
136 sizeSelf = GetSize();
137
138 // is there enough space to put the popup below the window (where we put it
139 // by default)?
140 wxCoord y = ptOrigin.y + size.y;
141 if ( y + sizeSelf.y > sizeScreen.y )
142 {
143 // check if there is enough space above
144 if ( ptOrigin.y > sizeSelf.y )
145 {
146 // do position the control above the window
147 y -= size.y + sizeSelf.y;
148 }
149 //else: not enough space below nor above, leave below
150 }
151
152 // now check left/right too
153 wxCoord x = ptOrigin.x + size.x;
154 if ( x + sizeSelf.x > sizeScreen.x )
155 {
156 // check if there is enough space to the left
157 if ( ptOrigin.x > sizeSelf.x )
158 {
159 // do position the control to the left
160 x -= size.x + sizeSelf.x;
161 }
162 //else: not enough space there neither, leave in default position
163 }
164
165 Move(x, y, wxSIZE_NO_ADJUSTMENTS);
166}
167
168// ----------------------------------------------------------------------------
169// wxPopupTransientWindow
170// ----------------------------------------------------------------------------
171
172void wxPopupTransientWindow::Init()
173{
174 m_child =
175 m_focus = (wxWindow *)NULL;
d7cff34d
VZ
176
177 m_handlerFocus = NULL;
178 m_handlerPopup = NULL;
c02ee97f
VZ
179}
180
758bce95 181wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
182{
183 Init();
184
758bce95 185 (void)Create(parent, style);
c02ee97f
VZ
186}
187
188wxPopupTransientWindow::~wxPopupTransientWindow()
189{
190 PopHandlers();
191}
192
193void wxPopupTransientWindow::PopHandlers()
194{
195 if ( m_child )
196 {
f6758939 197 if ( m_handlerPopup && !m_child->RemoveEventHandler(m_handlerPopup) )
d7cff34d
VZ
198 {
199 // something is very wrong and someone else probably deleted our
200 // handler - so don't risk deleting it second time
201 m_handlerPopup = NULL;
202 }
203
f4bb632c 204 // m_child->ReleaseMouse();
c02ee97f
VZ
205 m_child = NULL;
206 }
207
537a0880 208#ifdef __WXMSW__
c02ee97f
VZ
209 if ( m_focus )
210 {
f6758939 211 if ( m_handlerFocus && !m_focus->RemoveEventHandler(m_handlerFocus) )
d7cff34d
VZ
212 {
213 // see above
214 m_handlerFocus = NULL;
215 }
c02ee97f 216 }
537a0880 217#else
f6758939 218 if ( m_handlerFocus && !RemoveEventHandler(m_handlerFocus) )
537a0880
RR
219 {
220 // see above
221 m_handlerFocus = NULL;
222 }
223#endif
f6758939
RR
224
225 // delete the handlers, they'll be created as necessary in Popup()
226 delete m_handlerPopup;
227 m_handlerPopup = NULL;
228 delete m_handlerFocus;
229 m_handlerFocus = NULL;
230
537a0880 231 m_focus = NULL;
c02ee97f
VZ
232}
233
234void wxPopupTransientWindow::Popup(wxWindow *winFocus)
235{
236 const wxWindowList& children = GetChildren();
237 if ( children.GetCount() )
238 {
239 m_child = children.GetFirst()->GetData();
240 }
241 else
242 {
243 m_child = this;
244 }
245
e4606ed9 246 Show();
e4606ed9 247
d7cff34d
VZ
248 delete m_handlerPopup;
249 m_handlerPopup = new wxPopupWindowHandler(this);
250
f7204798 251 // m_child->CaptureMouse();
d7cff34d 252 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 253
c02ee97f 254 m_focus = winFocus ? winFocus : this;
c02ee97f 255 m_focus->SetFocus();
60178eb4 256
516cdd54 257#ifdef __WXMSW__
d7cff34d
VZ
258 // MSW doesn't allow to set focus to the popup window, but we need to
259 // subclass the window which has the focus, and not winFocus passed in or
260 // otherwise everything else breaks down
516cdd54 261 m_focus = FindFocus();
60178eb4
VZ
262 if ( m_focus )
263 {
d7cff34d
VZ
264 delete m_handlerFocus;
265 m_handlerFocus = new wxPopupFocusHandler(this);
266
267 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 268 }
537a0880
RR
269#else
270 // GTK+ catches the activate events from the popup
271 // window, not the focus events from the child window
272 delete m_handlerFocus;
273 m_handlerFocus = new wxPopupFocusHandler(this);
274 PushEventHandler(m_handlerFocus);
275#endif // __WXMSW__
326c7ea8 276
537a0880
RR
277}
278
279bool wxPopupTransientWindow::Show( bool show )
280{
281#ifdef __WXGTK__
282 if (!show)
f4bb632c
RR
283 {
284 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
2997ca30 285
537a0880 286 gtk_grab_remove( m_widget );
f4bb632c 287 }
537a0880
RR
288#endif
289
c51c4752
RR
290#ifdef __WXX11__
291 if (!show)
292 {
293 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
294 }
295#endif
296
537a0880 297 bool ret = wxPopupWindow::Show( show );
2997ca30 298
537a0880
RR
299#ifdef __WXGTK__
300 if (show)
f4bb632c 301 {
537a0880 302 gtk_grab_add( m_widget );
2997ca30 303
f4bb632c
RR
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 }
537a0880
RR
314#endif
315
c51c4752
RR
316#ifdef __WXX11__
317 if (show)
318 {
319 Window xwindow = (Window) m_clientWindow;
2997ca30 320
c51c4752
RR
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
537a0880 331 return ret;
c02ee97f
VZ
332}
333
334void wxPopupTransientWindow::Dismiss()
335{
336 PopHandlers();
337
338 Hide();
339}
340
341void wxPopupTransientWindow::DismissAndNotify()
342{
343 Dismiss();
344
345 OnDismiss();
346}
347
348void wxPopupTransientWindow::OnDismiss()
349{
350 // nothing to do here - but it may be interesting for derived class
351}
352
353bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
354{
355 // no special processing here
7e548f6b 356 return false;
c02ee97f
VZ
357}
358
a8132cd4 359#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
360
361// ----------------------------------------------------------------------------
362// wxPopupComboWindow
363// ----------------------------------------------------------------------------
364
e0c83227
VZ
365BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
366 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
367END_EVENT_TABLE()
368
c02ee97f
VZ
369wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
370 : wxPopupTransientWindow(parent)
371{
372 m_combo = parent;
373}
374
375bool wxPopupComboWindow::Create(wxComboControl *parent)
376{
377 m_combo = parent;
378
379 return wxPopupWindow::Create(parent);
380}
381
382void wxPopupComboWindow::PositionNearCombo()
383{
384 // the origin point must be in screen coords
c47addef 385 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
c02ee97f 386
89e7a223 387#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
388 // account for the fact that (0, 0) is not the top left corner of the
389 // window: there is also the border
390 wxRect rectBorders = m_combo->GetRenderer()->
391 GetBorderDimensions(m_combo->GetBorder());
392 ptOrigin.x -= rectBorders.x;
393 ptOrigin.y -= rectBorders.y;
394#endif // __WXUNIVERSAL__
395
396 // position below or above the combobox: the width is 0 to put it exactly
397 // below us, not to the left or to the right
398 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
399}
400
401void wxPopupComboWindow::OnDismiss()
402{
403 m_combo->OnDismiss();
404}
405
e0c83227
VZ
406void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
407{
408 m_combo->ProcessEvent(event);
409}
410
a8132cd4 411#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
412
413// ----------------------------------------------------------------------------
414// wxPopupWindowHandler
415// ----------------------------------------------------------------------------
416
417void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
418{
419 // let the window have it first (we're the first event handler in the chain
420 // of handlers for this window)
421 if ( m_popup->ProcessLeftDown(event) )
422 {
423 return;
424 }
326c7ea8 425
c02ee97f
VZ
426 wxPoint pos = event.GetPosition();
427
428 // scrollbar on which the click occured
429 wxWindow *sbar = NULL;
430
431 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 432
c02ee97f
VZ
433 switch ( win->HitTest(pos.x, pos.y) )
434 {
435 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
436 {
437 // do the coords translation now as after DismissAndNotify()
438 // m_popup may be destroyed
439 wxMouseEvent event2(event);
440
441 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
442
443 // clicking outside a popup dismisses it
444 m_popup->DismissAndNotify();
445
446 // dismissing a tooltip shouldn't waste a click, i.e. you
447 // should be able to dismiss it and press the button with the
448 // same click, so repost this event to the window beneath us
449 wxWindow *win = wxFindWindowAtPoint(event2.GetPosition());
450 if ( win )
451 {
452 // translate the event coords to the ones of the window
453 // which is going to get the event
454 win->ScreenToClient(&event2.m_x, &event2.m_y);
455
456 event2.SetEventObject(win);
457 wxPostEvent(win, event2);
458 }
459 }
c02ee97f
VZ
460 break;
461
a8132cd4 462#ifdef __WXUNIVERSAL__
c02ee97f
VZ
463 case wxHT_WINDOW_HORZ_SCROLLBAR:
464 sbar = win->GetScrollbar(wxHORIZONTAL);
465 break;
466
467 case wxHT_WINDOW_VERT_SCROLLBAR:
468 sbar = win->GetScrollbar(wxVERTICAL);
469 break;
a8132cd4 470#endif
c02ee97f
VZ
471
472 default:
473 // forgot to update the switch after adding a new hit test code?
474 wxFAIL_MSG( _T("unexpected HitTest() return value") );
475 // fall through
476
477 case wxHT_WINDOW_CORNER:
478 // don't actually know if this one is good for anything, but let it
479 // pass just in case
480
481 case wxHT_WINDOW_INSIDE:
482 // let the normal processing take place
483 event.Skip();
484 break;
485 }
486
487 if ( sbar )
488 {
489 // translate the event coordinates to the scrollbar ones
490 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
491
492 // and give the event to it
493 wxMouseEvent event2 = event;
494 event2.m_x = pos.x;
495 event2.m_y = pos.y;
496
497 (void)sbar->GetEventHandler()->ProcessEvent(event2);
498 }
499}
500
501// ----------------------------------------------------------------------------
502// wxPopupFocusHandler
503// ----------------------------------------------------------------------------
504
505void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
506{
60178eb4
VZ
507 // when we lose focus we always disappear - unless it goes to the popup (in
508 // which case we don't really lose it)
36a56c65
VS
509 wxWindow *win = event.GetWindow();
510 while ( win )
511 {
512 if ( win == m_popup )
513 return;
514 win = win->GetParent();
515 }
326c7ea8 516
36a56c65 517 m_popup->DismissAndNotify();
60178eb4 518}
54800df8 519
d7cff34d 520void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4
VZ
521{
522 // let the window have it first, it might process the keys
523 if ( !m_popup->ProcessEvent(event) )
524 {
525 // by default, dismiss the popup
54800df8 526 m_popup->DismissAndNotify();
60178eb4 527 }
c02ee97f
VZ
528}
529
530#endif // wxUSE_POPUPWIN
eb729cd3 531