Compile fix for Universal
[wxWidgets.git] / src / common / popupcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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" // wxComboCtrl
33 #include "wx/app.h" // wxPostEvent
34 #include "wx/log.h"
35 #endif //WX_PRECOMP
36
37 #ifdef __WXUNIVERSAL__
38 #include "wx/univ/renderer.h"
39 #if wxUSE_SCROLLBAR
40 #include "wx/scrolbar.h"
41 #endif
42 #endif // __WXUNIVERSAL__
43
44 #ifdef __WXGTK__
45 #include <gtk/gtk.h>
46 #endif
47 #ifdef __WXX11__
48 #include "wx/x11/private.h"
49 #endif
50
51 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
52 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
53
54 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
55 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
56 #endif
57
58 // ----------------------------------------------------------------------------
59 // private classes
60 // ----------------------------------------------------------------------------
61
62 // event handlers which we use to intercept events which cause the popup to
63 // disappear
64 class wxPopupWindowHandler : public wxEvtHandler
65 {
66 public:
67 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
68
69 protected:
70 // event handlers
71 void OnLeftDown(wxMouseEvent& event);
72
73 private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
77 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
78 };
79
80 class wxPopupFocusHandler : public wxEvtHandler
81 {
82 public:
83 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
84
85 protected:
86 void OnKillFocus(wxFocusEvent& event);
87 void OnKeyDown(wxKeyEvent& event);
88
89 private:
90 wxPopupTransientWindow *m_popup;
91
92 DECLARE_EVENT_TABLE()
93 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
94 };
95
96 // ----------------------------------------------------------------------------
97 // event tables
98 // ----------------------------------------------------------------------------
99
100 BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
101 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
102 END_EVENT_TABLE()
103
104 BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
105 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
106 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
107 END_EVENT_TABLE()
108
109 BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
110 #ifdef __WXMSW__
111 EVT_IDLE(wxPopupTransientWindow::OnIdle)
112 #endif
113 END_EVENT_TABLE()
114
115 // ============================================================================
116 // implementation
117 // ============================================================================
118
119 // ----------------------------------------------------------------------------
120 // wxPopupWindowBase
121 // ----------------------------------------------------------------------------
122
123 wxPopupWindowBase::~wxPopupWindowBase()
124 {
125 // this destructor is required for Darwin
126 }
127
128 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
129 {
130 return true;
131 }
132
133 void 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
173 void wxPopupTransientWindow::Init()
174 {
175 m_child =
176 m_focus = (wxWindow *)NULL;
177
178 m_handlerFocus = NULL;
179 m_handlerPopup = NULL;
180 }
181
182 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
183 {
184 Init();
185
186 (void)Create(parent, style);
187 }
188
189 wxPopupTransientWindow::~wxPopupTransientWindow()
190 {
191 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
192 PopHandlers();
193
194 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
195 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
196
197 delete m_handlerFocus;
198 delete m_handlerPopup;
199 }
200
201 void wxPopupTransientWindow::PopHandlers()
202 {
203 if ( m_child )
204 {
205 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
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 }
211 if (m_child->HasCapture())
212 {
213 m_child->ReleaseMouse();
214 }
215 m_child = NULL;
216 }
217
218 if ( m_focus )
219 {
220 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
221 {
222 // see above
223 m_handlerFocus = NULL;
224 }
225 }
226 m_focus = NULL;
227 }
228
229 void 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
241 Show();
242
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());
246
247 if (!m_handlerPopup)
248 m_handlerPopup = new wxPopupWindowHandler(this);
249
250 m_child->PushEventHandler(m_handlerPopup);
251
252 m_focus = winFocus ? winFocus : this;
253 m_focus->SetFocus();
254
255 #ifdef __WXMSW__
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
259 m_focus = FindFocus();
260 #elif defined(__WXGTK__)
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
266 if ( m_focus )
267 {
268 if (!m_handlerFocus)
269 m_handlerFocus = new wxPopupFocusHandler(this);
270
271 m_focus->PushEventHandler(m_handlerFocus);
272 }
273 }
274
275 bool wxPopupTransientWindow::Show( bool show )
276 {
277 #ifdef __WXGTK__
278 if (!show)
279 {
280 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
281
282 gtk_grab_remove( m_widget );
283 }
284 #endif
285
286 #ifdef __WXX11__
287 if (!show)
288 {
289 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
290 }
291 #endif
292
293 #ifdef __WXMSW__
294 if (!show && m_child && m_child->HasCapture())
295 {
296 m_child->ReleaseMouse();
297 }
298 #endif
299
300 bool ret = wxPopupWindow::Show( show );
301
302 #ifdef __WXGTK__
303 if (show)
304 {
305 gtk_grab_add( m_widget );
306
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 }
317 #endif
318
319 #ifdef __WXX11__
320 if (show)
321 {
322 Window xwindow = (Window) m_clientWindow;
323
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
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
343 return ret;
344 }
345
346 void wxPopupTransientWindow::Dismiss()
347 {
348 Hide();
349 PopHandlers();
350 }
351
352 void wxPopupTransientWindow::DismissAndNotify()
353 {
354 Dismiss();
355 OnDismiss();
356 }
357
358 void wxPopupTransientWindow::OnDismiss()
359 {
360 // nothing to do here - but it may be interesting for derived class
361 }
362
363 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
364 {
365 // no special processing here
366 return false;
367 }
368
369 #ifdef __WXMSW__
370 void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
371 {
372 event.Skip();
373
374 if (IsShown() && m_child)
375 {
376 wxPoint pos = ScreenToClient(wxGetMousePosition());
377 wxRect rect(GetSize());
378
379 if ( rect.Contains(pos) )
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 }
392 }
393 }
394 }
395 #endif // __WXMSW__
396
397
398 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
399
400 // ----------------------------------------------------------------------------
401 // wxPopupComboWindow
402 // ----------------------------------------------------------------------------
403
404 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
405 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
406 END_EVENT_TABLE()
407
408 wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent)
409 : wxPopupTransientWindow(parent)
410 {
411 m_combo = parent;
412 }
413
414 bool wxPopupComboWindow::Create(wxComboCtrl *parent)
415 {
416 m_combo = parent;
417
418 return wxPopupWindow::Create(parent);
419 }
420
421 void wxPopupComboWindow::PositionNearCombo()
422 {
423 // the origin point must be in screen coords
424 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
425
426 #if 0 //def __WXUNIVERSAL__
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
440 void wxPopupComboWindow::OnDismiss()
441 {
442 m_combo->OnPopupDismiss();
443 }
444
445 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
446 {
447 m_combo->ProcessEvent(event);
448 }
449
450 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
451
452 // ----------------------------------------------------------------------------
453 // wxPopupWindowHandler
454 // ----------------------------------------------------------------------------
455
456 void 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 }
464
465 wxPoint pos = event.GetPosition();
466
467 // in non-Univ ports the system manages scrollbars for us
468 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
469 // scrollbar on which the click occurred
470 wxWindow *sbar = NULL;
471 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
472
473 wxWindow *win = (wxWindow *)event.GetEventObject();
474
475 switch ( win->HitTest(pos.x, pos.y) )
476 {
477 case wxHT_WINDOW_OUTSIDE:
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
491 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
492 if ( winUnder )
493 {
494 // translate the event coords to the ones of the window
495 // which is going to get the event
496 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
497
498 event2.SetEventObject(winUnder);
499 wxPostEvent(winUnder, event2);
500 }
501 }
502 break;
503
504 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
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;
512 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
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
529 #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR
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 }
542 #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR
543 }
544
545 // ----------------------------------------------------------------------------
546 // wxPopupFocusHandler
547 // ----------------------------------------------------------------------------
548
549 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
550 {
551 // when we lose focus we always disappear - unless it goes to the popup (in
552 // which case we don't really lose it)
553 wxWindow *win = event.GetWindow();
554 while ( win )
555 {
556 if ( win == m_popup )
557 return;
558 win = win->GetParent();
559 }
560
561 m_popup->DismissAndNotify();
562 }
563
564 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
565 {
566 // let the window have it first, it might process the keys
567 if ( !m_popup->GetEventHandler()->ProcessEvent(event) )
568 {
569 // by default, dismiss the popup
570 m_popup->DismissAndNotify();
571 }
572 }
573
574 #endif // wxUSE_POPUPWIN