]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/recguard.h" | |
38 | ||
39 | #ifdef __WXUNIVERSAL__ | |
40 | #include "wx/univ/renderer.h" | |
41 | #include "wx/scrolbar.h" | |
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 | #if defined( __WXMSW__ ) || defined( __WXMAC__) | |
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; | |
155 | ||
156 | if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft ) | |
157 | { | |
158 | // shift the window to the left instead of the right. | |
159 | x -= size.x; | |
160 | x -= sizeSelf.x; // also shift it by window width. | |
161 | } | |
162 | else | |
163 | x += size.x; | |
164 | ||
165 | ||
166 | if ( x + sizeSelf.x > sizeScreen.x ) | |
167 | { | |
168 | // check if there is enough space to the left | |
169 | if ( ptOrigin.x > sizeSelf.x ) | |
170 | { | |
171 | // do position the control to the left | |
172 | x -= size.x + sizeSelf.x; | |
173 | } | |
174 | //else: not enough space there neither, leave in default position | |
175 | } | |
176 | ||
177 | Move(x, y, wxSIZE_NO_ADJUSTMENTS); | |
178 | } | |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // wxPopupTransientWindow | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | void wxPopupTransientWindow::Init() | |
185 | { | |
186 | m_child = | |
187 | m_focus = (wxWindow *)NULL; | |
188 | ||
189 | m_handlerFocus = NULL; | |
190 | m_handlerPopup = NULL; | |
191 | } | |
192 | ||
193 | wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style) | |
194 | { | |
195 | Init(); | |
196 | ||
197 | (void)Create(parent, style); | |
198 | } | |
199 | ||
200 | wxPopupTransientWindow::~wxPopupTransientWindow() | |
201 | { | |
202 | if (m_handlerPopup && m_handlerPopup->GetNextHandler()) | |
203 | PopHandlers(); | |
204 | ||
205 | wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler()); | |
206 | wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler()); | |
207 | ||
208 | delete m_handlerFocus; | |
209 | delete m_handlerPopup; | |
210 | } | |
211 | ||
212 | void wxPopupTransientWindow::PopHandlers() | |
213 | { | |
214 | if ( m_child ) | |
215 | { | |
216 | if ( !m_child->RemoveEventHandler(m_handlerPopup) ) | |
217 | { | |
218 | // something is very wrong and someone else probably deleted our | |
219 | // handler - so don't risk deleting it second time | |
220 | m_handlerPopup = NULL; | |
221 | } | |
222 | if (m_child->HasCapture()) | |
223 | { | |
224 | m_child->ReleaseMouse(); | |
225 | } | |
226 | m_child = NULL; | |
227 | } | |
228 | ||
229 | if ( m_focus ) | |
230 | { | |
231 | if ( !m_focus->RemoveEventHandler(m_handlerFocus) ) | |
232 | { | |
233 | // see above | |
234 | m_handlerFocus = NULL; | |
235 | } | |
236 | } | |
237 | m_focus = NULL; | |
238 | } | |
239 | ||
240 | void wxPopupTransientWindow::Popup(wxWindow *winFocus) | |
241 | { | |
242 | const wxWindowList& children = GetChildren(); | |
243 | if ( children.GetCount() ) | |
244 | { | |
245 | m_child = children.GetFirst()->GetData(); | |
246 | } | |
247 | else | |
248 | { | |
249 | m_child = this; | |
250 | } | |
251 | ||
252 | Show(); | |
253 | ||
254 | // There is is a problem if these are still in use | |
255 | wxASSERT(!m_handlerFocus || !m_handlerFocus->GetNextHandler()); | |
256 | wxASSERT(!m_handlerPopup || !m_handlerPopup->GetNextHandler()); | |
257 | ||
258 | if (!m_handlerPopup) | |
259 | m_handlerPopup = new wxPopupWindowHandler(this); | |
260 | ||
261 | m_child->PushEventHandler(m_handlerPopup); | |
262 | ||
263 | #if defined(__WXMSW__) | |
264 | // Focusing on child of popup window does not work on MSW unless WS_POPUP | |
265 | // style is set. We do not even want to try to set the focus, as it may | |
266 | // provoke errors on some Windows versions (Vista and later). | |
267 | if ( ::GetWindowLong(GetHwnd(), GWL_STYLE) & WS_POPUP ) | |
268 | #endif | |
269 | { | |
270 | m_focus = winFocus ? winFocus : this; | |
271 | m_focus->SetFocus(); | |
272 | } | |
273 | ||
274 | #if defined( __WXMSW__ ) || defined( __WXMAC__) | |
275 | // MSW doesn't allow to set focus to the popup window, but we need to | |
276 | // subclass the window which has the focus, and not winFocus passed in or | |
277 | // otherwise everything else breaks down | |
278 | m_focus = FindFocus(); | |
279 | #elif defined(__WXGTK__) | |
280 | // GTK+ catches the activate events from the popup | |
281 | // window, not the focus events from the child window | |
282 | m_focus = this; | |
283 | #endif | |
284 | ||
285 | if ( m_focus ) | |
286 | { | |
287 | if (!m_handlerFocus) | |
288 | m_handlerFocus = new wxPopupFocusHandler(this); | |
289 | ||
290 | m_focus->PushEventHandler(m_handlerFocus); | |
291 | } | |
292 | } | |
293 | ||
294 | bool wxPopupTransientWindow::Show( bool show ) | |
295 | { | |
296 | #ifdef __WXGTK__ | |
297 | if (!show) | |
298 | { | |
299 | gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME ); | |
300 | ||
301 | gtk_grab_remove( m_widget ); | |
302 | } | |
303 | #endif | |
304 | ||
305 | #ifdef __WXX11__ | |
306 | if (!show) | |
307 | { | |
308 | XUngrabPointer( wxGlobalDisplay(), CurrentTime ); | |
309 | } | |
310 | #endif | |
311 | ||
312 | #if defined( __WXMSW__ ) || defined( __WXMAC__) | |
313 | if (!show && m_child && m_child->HasCapture()) | |
314 | { | |
315 | m_child->ReleaseMouse(); | |
316 | } | |
317 | #endif | |
318 | ||
319 | bool ret = wxPopupWindow::Show( show ); | |
320 | ||
321 | #ifdef __WXGTK__ | |
322 | if (show) | |
323 | { | |
324 | gtk_grab_add( m_widget ); | |
325 | ||
326 | gdk_pointer_grab( m_widget->window, TRUE, | |
327 | (GdkEventMask) | |
328 | (GDK_BUTTON_PRESS_MASK | | |
329 | GDK_BUTTON_RELEASE_MASK | | |
330 | GDK_POINTER_MOTION_HINT_MASK | | |
331 | GDK_POINTER_MOTION_MASK), | |
332 | (GdkWindow *) NULL, | |
333 | (GdkCursor *) NULL, | |
334 | (guint32)GDK_CURRENT_TIME ); | |
335 | } | |
336 | #endif | |
337 | ||
338 | #ifdef __WXX11__ | |
339 | if (show) | |
340 | { | |
341 | Window xwindow = (Window) m_clientWindow; | |
342 | ||
343 | /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow, | |
344 | True, | |
345 | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, | |
346 | GrabModeAsync, | |
347 | GrabModeAsync, | |
348 | None, | |
349 | None, | |
350 | CurrentTime ); | |
351 | } | |
352 | #endif | |
353 | ||
354 | #if defined( __WXMSW__ ) || defined( __WXMAC__) | |
355 | if (show && m_child) | |
356 | { | |
357 | // Assume that the mouse is outside the popup to begin with | |
358 | m_child->CaptureMouse(); | |
359 | } | |
360 | #endif | |
361 | ||
362 | return ret; | |
363 | } | |
364 | ||
365 | void wxPopupTransientWindow::Dismiss() | |
366 | { | |
367 | Hide(); | |
368 | PopHandlers(); | |
369 | } | |
370 | ||
371 | void wxPopupTransientWindow::DismissAndNotify() | |
372 | { | |
373 | Dismiss(); | |
374 | OnDismiss(); | |
375 | } | |
376 | ||
377 | void wxPopupTransientWindow::OnDismiss() | |
378 | { | |
379 | // nothing to do here - but it may be interesting for derived class | |
380 | } | |
381 | ||
382 | bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event)) | |
383 | { | |
384 | // no special processing here | |
385 | return false; | |
386 | } | |
387 | ||
388 | #if defined( __WXMSW__ ) || defined( __WXMAC__) | |
389 | void wxPopupTransientWindow::OnIdle(wxIdleEvent& event) | |
390 | { | |
391 | event.Skip(); | |
392 | ||
393 | if (IsShown() && m_child) | |
394 | { | |
395 | wxPoint pos = ScreenToClient(wxGetMousePosition()); | |
396 | wxRect rect(GetSize()); | |
397 | ||
398 | if ( rect.Contains(pos) ) | |
399 | { | |
400 | if ( m_child->HasCapture() ) | |
401 | { | |
402 | m_child->ReleaseMouse(); | |
403 | } | |
404 | } | |
405 | else | |
406 | { | |
407 | if ( !m_child->HasCapture() ) | |
408 | { | |
409 | m_child->CaptureMouse(); | |
410 | } | |
411 | } | |
412 | } | |
413 | } | |
414 | #endif // __WXMSW__ | |
415 | ||
416 | ||
417 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) | |
418 | ||
419 | // ---------------------------------------------------------------------------- | |
420 | // wxPopupComboWindow | |
421 | // ---------------------------------------------------------------------------- | |
422 | ||
423 | BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow) | |
424 | EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown) | |
425 | END_EVENT_TABLE() | |
426 | ||
427 | wxPopupComboWindow::wxPopupComboWindow(wxComboCtrl *parent) | |
428 | : wxPopupTransientWindow(parent) | |
429 | { | |
430 | m_combo = parent; | |
431 | } | |
432 | ||
433 | bool wxPopupComboWindow::Create(wxComboCtrl *parent) | |
434 | { | |
435 | m_combo = parent; | |
436 | ||
437 | return wxPopupWindow::Create(parent); | |
438 | } | |
439 | ||
440 | void wxPopupComboWindow::PositionNearCombo() | |
441 | { | |
442 | // the origin point must be in screen coords | |
443 | wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0)); | |
444 | ||
445 | #if 0 //def __WXUNIVERSAL__ | |
446 | // account for the fact that (0, 0) is not the top left corner of the | |
447 | // window: there is also the border | |
448 | wxRect rectBorders = m_combo->GetRenderer()-> | |
449 | GetBorderDimensions(m_combo->GetBorder()); | |
450 | ptOrigin.x -= rectBorders.x; | |
451 | ptOrigin.y -= rectBorders.y; | |
452 | #endif // __WXUNIVERSAL__ | |
453 | ||
454 | // position below or above the combobox: the width is 0 to put it exactly | |
455 | // below us, not to the left or to the right | |
456 | Position(ptOrigin, wxSize(0, m_combo->GetSize().y)); | |
457 | } | |
458 | ||
459 | void wxPopupComboWindow::OnDismiss() | |
460 | { | |
461 | m_combo->OnPopupDismiss(); | |
462 | } | |
463 | ||
464 | void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event) | |
465 | { | |
466 | m_combo->ProcessEvent(event); | |
467 | } | |
468 | ||
469 | #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) | |
470 | ||
471 | // ---------------------------------------------------------------------------- | |
472 | // wxPopupWindowHandler | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event) | |
476 | { | |
477 | // let the window have it first (we're the first event handler in the chain | |
478 | // of handlers for this window) | |
479 | if ( m_popup->ProcessLeftDown(event) ) | |
480 | { | |
481 | return; | |
482 | } | |
483 | ||
484 | wxPoint pos = event.GetPosition(); | |
485 | ||
486 | // in non-Univ ports the system manages scrollbars for us | |
487 | #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR | |
488 | // scrollbar on which the click occurred | |
489 | wxWindow *sbar = NULL; | |
490 | #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR | |
491 | ||
492 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
493 | ||
494 | switch ( win->HitTest(pos.x, pos.y) ) | |
495 | { | |
496 | case wxHT_WINDOW_OUTSIDE: | |
497 | { | |
498 | // do the coords translation now as after DismissAndNotify() | |
499 | // m_popup may be destroyed | |
500 | wxMouseEvent event2(event); | |
501 | ||
502 | m_popup->ClientToScreen(&event2.m_x, &event2.m_y); | |
503 | ||
504 | // clicking outside a popup dismisses it | |
505 | m_popup->DismissAndNotify(); | |
506 | ||
507 | // dismissing a tooltip shouldn't waste a click, i.e. you | |
508 | // should be able to dismiss it and press the button with the | |
509 | // same click, so repost this event to the window beneath us | |
510 | wxWindow *winUnder = wxFindWindowAtPoint(event2.GetPosition()); | |
511 | if ( winUnder ) | |
512 | { | |
513 | // translate the event coords to the ones of the window | |
514 | // which is going to get the event | |
515 | winUnder->ScreenToClient(&event2.m_x, &event2.m_y); | |
516 | ||
517 | event2.SetEventObject(winUnder); | |
518 | wxPostEvent(winUnder->GetEventHandler(), event2); | |
519 | } | |
520 | } | |
521 | break; | |
522 | ||
523 | #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR | |
524 | case wxHT_WINDOW_HORZ_SCROLLBAR: | |
525 | sbar = win->GetScrollbar(wxHORIZONTAL); | |
526 | break; | |
527 | ||
528 | case wxHT_WINDOW_VERT_SCROLLBAR: | |
529 | sbar = win->GetScrollbar(wxVERTICAL); | |
530 | break; | |
531 | #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR | |
532 | ||
533 | default: | |
534 | // forgot to update the switch after adding a new hit test code? | |
535 | wxFAIL_MSG( _T("unexpected HitTest() return value") ); | |
536 | // fall through | |
537 | ||
538 | case wxHT_WINDOW_CORNER: | |
539 | // don't actually know if this one is good for anything, but let it | |
540 | // pass just in case | |
541 | ||
542 | case wxHT_WINDOW_INSIDE: | |
543 | // let the normal processing take place | |
544 | event.Skip(); | |
545 | break; | |
546 | } | |
547 | ||
548 | #if defined(__WXUNIVERSAL__) && wxUSE_SCROLLBAR | |
549 | if ( sbar ) | |
550 | { | |
551 | // translate the event coordinates to the scrollbar ones | |
552 | pos = sbar->ScreenToClient(win->ClientToScreen(pos)); | |
553 | ||
554 | // and give the event to it | |
555 | wxMouseEvent event2 = event; | |
556 | event2.m_x = pos.x; | |
557 | event2.m_y = pos.y; | |
558 | ||
559 | (void)sbar->GetEventHandler()->ProcessEvent(event2); | |
560 | } | |
561 | #endif // __WXUNIVERSAL__ && wxUSE_SCROLLBAR | |
562 | } | |
563 | ||
564 | // ---------------------------------------------------------------------------- | |
565 | // wxPopupFocusHandler | |
566 | // ---------------------------------------------------------------------------- | |
567 | ||
568 | void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event) | |
569 | { | |
570 | // when we lose focus we always disappear - unless it goes to the popup (in | |
571 | // which case we don't really lose it) | |
572 | wxWindow *win = event.GetWindow(); | |
573 | while ( win ) | |
574 | { | |
575 | if ( win == m_popup ) | |
576 | return; | |
577 | win = win->GetParent(); | |
578 | } | |
579 | ||
580 | m_popup->DismissAndNotify(); | |
581 | } | |
582 | ||
583 | void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event) | |
584 | { | |
585 | // we can be associated with the popup itself in which case we should avoid | |
586 | // infinite recursion | |
587 | static int s_inside; | |
588 | wxRecursionGuard guard(s_inside); | |
589 | if ( guard.IsInside() ) | |
590 | { | |
591 | event.Skip(); | |
592 | return; | |
593 | } | |
594 | ||
595 | // let the window have it first, it might process the keys | |
596 | if ( !m_popup->GetEventHandler()->ProcessEvent(event) ) | |
597 | { | |
598 | // by default, dismiss the popup | |
599 | m_popup->DismissAndNotify(); | |
600 | } | |
601 | } | |
602 | ||
603 | #endif // wxUSE_POPUPWIN |