]> git.saurik.com Git - wxWidgets.git/blob - src/common/popupcmn.cpp
wxControl::SetLabel no longer strips out codes so do it here
[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 // 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" // wxComboControl
33 #include "wx/app.h" // wxPostEvent
34 #include "wx/log.h"
35 #include "wx/app.h"
36 #endif //WX_PRECOMP
37
38 #ifdef __WXUNIVERSAL__
39 #include "wx/univ/renderer.h"
40 #endif // __WXUNIVERSAL__
41
42 #ifdef __WXGTK__
43 #include <gtk/gtk.h>
44 #endif
45 #ifdef __WXX11__
46 #include "wx/x11/private.h"
47 #endif
48
49 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
50 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
51
52 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
54 #endif
55
56 // ----------------------------------------------------------------------------
57 // private classes
58 // ----------------------------------------------------------------------------
59
60 // event handlers which we use to intercept events which cause the popup to
61 // disappear
62 class wxPopupWindowHandler : public wxEvtHandler
63 {
64 public:
65 wxPopupWindowHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
66
67 protected:
68 // event handlers
69 void OnLeftDown(wxMouseEvent& event);
70
71 private:
72 wxPopupTransientWindow *m_popup;
73
74 DECLARE_EVENT_TABLE()
75 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
76 };
77
78 class wxPopupFocusHandler : public wxEvtHandler
79 {
80 public:
81 wxPopupFocusHandler(wxPopupTransientWindow *popup) : m_popup(popup) {}
82
83 protected:
84 void OnKillFocus(wxFocusEvent& event);
85 void OnKeyDown(wxKeyEvent& event);
86
87 private:
88 wxPopupTransientWindow *m_popup;
89
90 DECLARE_EVENT_TABLE()
91 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
92 };
93
94 // ----------------------------------------------------------------------------
95 // event tables
96 // ----------------------------------------------------------------------------
97
98 BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
99 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
100 END_EVENT_TABLE()
101
102 BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
103 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
104 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
105 END_EVENT_TABLE()
106
107 BEGIN_EVENT_TABLE(wxPopupTransientWindow, wxPopupWindow)
108 #ifdef __WXMSW__
109 EVT_IDLE(wxPopupTransientWindow::OnIdle)
110 #endif
111 END_EVENT_TABLE()
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // wxPopupWindowBase
119 // ----------------------------------------------------------------------------
120
121 wxPopupWindowBase::~wxPopupWindowBase()
122 {
123 // this destructor is required for Darwin
124 }
125
126 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
127 {
128 return true;
129 }
130
131 void 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
171 void wxPopupTransientWindow::Init()
172 {
173 m_child =
174 m_focus = (wxWindow *)NULL;
175
176 m_handlerFocus = NULL;
177 m_handlerPopup = NULL;
178 }
179
180 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
181 {
182 Init();
183
184 (void)Create(parent, style);
185 }
186
187 wxPopupTransientWindow::~wxPopupTransientWindow()
188 {
189 if (m_handlerPopup && m_handlerPopup->GetNextHandler())
190 PopHandlers();
191
192 wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler());
193 wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler());
194
195 delete m_handlerFocus;
196 delete m_handlerPopup;
197 }
198
199 void wxPopupTransientWindow::PopHandlers()
200 {
201 if ( m_child )
202 {
203 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
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 }
209 if (m_child->HasCapture())
210 {
211 m_child->ReleaseMouse();
212 }
213 m_child = NULL;
214 }
215
216 if ( m_focus )
217 {
218 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
219 {
220 // see above
221 m_handlerFocus = NULL;
222 }
223 }
224 m_focus = NULL;
225 }
226
227 void 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
239 Show();
240
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());
244
245 if (!m_handlerPopup)
246 m_handlerPopup = new wxPopupWindowHandler(this);
247
248 m_child->PushEventHandler(m_handlerPopup);
249
250 m_focus = winFocus ? winFocus : this;
251 m_focus->SetFocus();
252
253 #ifdef __WXMSW__
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
257 m_focus = FindFocus();
258 #elif defined(__WXGTK__)
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
264 if ( m_focus )
265 {
266 if (!m_handlerFocus)
267 m_handlerFocus = new wxPopupFocusHandler(this);
268
269 m_focus->PushEventHandler(m_handlerFocus);
270 }
271 }
272
273 bool wxPopupTransientWindow::Show( bool show )
274 {
275 #ifdef __WXGTK__
276 if (!show)
277 {
278 gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME );
279
280 gtk_grab_remove( m_widget );
281 }
282 #endif
283
284 #ifdef __WXX11__
285 if (!show)
286 {
287 XUngrabPointer( wxGlobalDisplay(), CurrentTime );
288 }
289 #endif
290
291 #ifdef __WXMSW__
292 if (!show && m_child && m_child->HasCapture())
293 {
294 m_child->ReleaseMouse();
295 }
296 #endif
297
298 bool ret = wxPopupWindow::Show( show );
299
300 #ifdef __WXGTK__
301 if (show)
302 {
303 gtk_grab_add( m_widget );
304
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 }
315 #endif
316
317 #ifdef __WXX11__
318 if (show)
319 {
320 Window xwindow = (Window) m_clientWindow;
321
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
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
341 return ret;
342 }
343
344 void wxPopupTransientWindow::Dismiss()
345 {
346 Hide();
347 PopHandlers();
348 }
349
350 void wxPopupTransientWindow::DismissAndNotify()
351 {
352 Dismiss();
353 OnDismiss();
354 }
355
356 void wxPopupTransientWindow::OnDismiss()
357 {
358 // nothing to do here - but it may be interesting for derived class
359 }
360
361 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
362 {
363 // no special processing here
364 return false;
365 }
366
367 #ifdef __WXMSW__
368 void wxPopupTransientWindow::OnIdle(wxIdleEvent& event)
369 {
370 event.Skip();
371
372 if (IsShown() && m_child)
373 {
374 wxPoint pos = ScreenToClient(wxGetMousePosition());
375 wxRect rect(GetSize());
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 }
390 }
391 }
392 }
393 #endif // __WXMSW__
394
395
396 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
397
398 // ----------------------------------------------------------------------------
399 // wxPopupComboWindow
400 // ----------------------------------------------------------------------------
401
402 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
403 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
404 END_EVENT_TABLE()
405
406 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
407 : wxPopupTransientWindow(parent)
408 {
409 m_combo = parent;
410 }
411
412 bool wxPopupComboWindow::Create(wxComboControl *parent)
413 {
414 m_combo = parent;
415
416 return wxPopupWindow::Create(parent);
417 }
418
419 void wxPopupComboWindow::PositionNearCombo()
420 {
421 // the origin point must be in screen coords
422 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0));
423
424 #if 0 //def __WXUNIVERSAL__
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
438 void wxPopupComboWindow::OnDismiss()
439 {
440 m_combo->OnDismiss();
441 }
442
443 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
444 {
445 m_combo->ProcessEvent(event);
446 }
447
448 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
449
450 // ----------------------------------------------------------------------------
451 // wxPopupWindowHandler
452 // ----------------------------------------------------------------------------
453
454 void 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 }
462
463 wxPoint pos = event.GetPosition();
464
465 // scrollbar on which the click occurred
466 wxWindow *sbar = NULL;
467
468 wxWindow *win = (wxWindow *)event.GetEventObject();
469
470 switch ( win->HitTest(pos.x, pos.y) )
471 {
472 case wxHT_WINDOW_OUTSIDE:
473 {
474 // do the coords translation now as after DismissAndNotify()
475 // m_popup may be destroyed
476 wxMouseEvent event2(event);
477
478 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
479
480 // clicking outside a popup dismisses it
481 m_popup->DismissAndNotify();
482
483 // dismissing a tooltip shouldn't waste a click, i.e. you
484 // should be able to dismiss it and press the button with the
485 // same click, so repost this event to the window beneath us
486 wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition());
487 if ( winUnder )
488 {
489 // translate the event coords to the ones of the window
490 // which is going to get the event
491 winUnder->ScreenToClient(&event2.m_x, &event2.m_y);
492
493 event2.SetEventObject(winUnder);
494 wxPostEvent(winUnder, event2);
495 }
496 }
497 break;
498
499 #ifdef __WXUNIVERSAL__
500 case wxHT_WINDOW_HORZ_SCROLLBAR:
501 sbar = win->GetScrollbar(wxHORIZONTAL);
502 break;
503
504 case wxHT_WINDOW_VERT_SCROLLBAR:
505 sbar = win->GetScrollbar(wxVERTICAL);
506 break;
507 #endif
508
509 default:
510 // forgot to update the switch after adding a new hit test code?
511 wxFAIL_MSG( _T("unexpected HitTest() return value") );
512 // fall through
513
514 case wxHT_WINDOW_CORNER:
515 // don't actually know if this one is good for anything, but let it
516 // pass just in case
517
518 case wxHT_WINDOW_INSIDE:
519 // let the normal processing take place
520 event.Skip();
521 break;
522 }
523
524 if ( sbar )
525 {
526 // translate the event coordinates to the scrollbar ones
527 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
528
529 // and give the event to it
530 wxMouseEvent event2 = event;
531 event2.m_x = pos.x;
532 event2.m_y = pos.y;
533
534 (void)sbar->GetEventHandler()->ProcessEvent(event2);
535 }
536 }
537
538 // ----------------------------------------------------------------------------
539 // wxPopupFocusHandler
540 // ----------------------------------------------------------------------------
541
542 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
543 {
544 // when we lose focus we always disappear - unless it goes to the popup (in
545 // which case we don't really lose it)
546 wxWindow *win = event.GetWindow();
547 while ( win )
548 {
549 if ( win == m_popup )
550 return;
551 win = win->GetParent();
552 }
553
554 m_popup->DismissAndNotify();
555 }
556
557 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
558 {
559 // let the window have it first, it might process the keys
560 if ( !m_popup->ProcessEvent(event) )
561 {
562 // by default, dismiss the popup
563 m_popup->DismissAndNotify();
564 }
565 }
566
567 #endif // wxUSE_POPUPWIN
568