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