]>
Commit | Line | Data |
---|---|---|
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 | 53 | IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow) |
6c3422e9 | 54 | IMPLEMENT_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 | |
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() | |
22f3361e | 79 | DECLARE_NO_COPY_CLASS(wxPopupWindowHandler) |
c02ee97f VZ |
80 | }; |
81 | ||
82 | class wxPopupFocusHandler : public wxEvtHandler | |
83 | { | |
84 | public: | |
516cdd54 VZ |
85 | wxPopupFocusHandler(wxPopupTransientWindow *popup) |
86 | { | |
87 | m_popup = popup; | |
516cdd54 | 88 | } |
c02ee97f VZ |
89 | |
90 | protected: | |
c02ee97f | 91 | void OnKillFocus(wxFocusEvent& event); |
d7cff34d | 92 | void OnKeyDown(wxKeyEvent& event); |
c02ee97f VZ |
93 | |
94 | private: | |
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 | ||
105 | BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler) | |
106 | EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown) | |
107 | END_EVENT_TABLE() | |
108 | ||
109 | BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler) | |
110 | EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus) | |
d7cff34d | 111 | EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown) |
c02ee97f VZ |
112 | END_EVENT_TABLE() |
113 | ||
114 | // ============================================================================ | |
115 | // implementation | |
116 | // ============================================================================ | |
117 | ||
118 | // ---------------------------------------------------------------------------- | |
119 | // wxPopupWindowBase | |
120 | // ---------------------------------------------------------------------------- | |
121 | ||
799ea011 GD |
122 | wxPopupWindowBase::~wxPopupWindowBase() |
123 | { | |
124 | // this destructor is required for Darwin | |
125 | } | |
126 | ||
c02ee97f VZ |
127 | bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags)) |
128 | { | |
7e548f6b | 129 | return true; |
c02ee97f VZ |
130 | } |
131 | ||
132 | void 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 | ||
172 | void 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 | 181 | wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style) |
c02ee97f VZ |
182 | { |
183 | Init(); | |
184 | ||
758bce95 | 185 | (void)Create(parent, style); |
c02ee97f VZ |
186 | } |
187 | ||
188 | wxPopupTransientWindow::~wxPopupTransientWindow() | |
189 | { | |
190 | PopHandlers(); | |
191 | } | |
192 | ||
193 | void 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 | ||
c02ee97f VZ |
204 | m_child = NULL; |
205 | } | |
206 | ||
537a0880 | 207 | #ifdef __WXMSW__ |
c02ee97f VZ |
208 | if ( m_focus ) |
209 | { | |
f6758939 | 210 | if ( m_handlerFocus && !m_focus->RemoveEventHandler(m_handlerFocus) ) |
d7cff34d VZ |
211 | { |
212 | // see above | |
213 | m_handlerFocus = NULL; | |
214 | } | |
c02ee97f | 215 | } |
537a0880 | 216 | #else |
f6758939 | 217 | if ( m_handlerFocus && !RemoveEventHandler(m_handlerFocus) ) |
537a0880 RR |
218 | { |
219 | // see above | |
220 | m_handlerFocus = NULL; | |
221 | } | |
222 | #endif | |
f6758939 RR |
223 | |
224 | // delete the handlers, they'll be created as necessary in Popup() | |
225 | delete m_handlerPopup; | |
226 | m_handlerPopup = NULL; | |
227 | delete m_handlerFocus; | |
228 | m_handlerFocus = NULL; | |
229 | ||
537a0880 | 230 | m_focus = NULL; |
c02ee97f VZ |
231 | } |
232 | ||
233 | void wxPopupTransientWindow::Popup(wxWindow *winFocus) | |
234 | { | |
235 | const wxWindowList& children = GetChildren(); | |
236 | if ( children.GetCount() ) | |
237 | { | |
238 | m_child = children.GetFirst()->GetData(); | |
239 | } | |
240 | else | |
241 | { | |
242 | m_child = this; | |
243 | } | |
244 | ||
e4606ed9 | 245 | Show(); |
e4606ed9 | 246 | |
d7cff34d VZ |
247 | delete m_handlerPopup; |
248 | m_handlerPopup = new wxPopupWindowHandler(this); | |
249 | ||
d7cff34d | 250 | m_child->PushEventHandler(m_handlerPopup); |
c02ee97f | 251 | |
c02ee97f | 252 | m_focus = winFocus ? winFocus : this; |
c02ee97f | 253 | m_focus->SetFocus(); |
60178eb4 | 254 | |
516cdd54 | 255 | #ifdef __WXMSW__ |
d7cff34d VZ |
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 | |
516cdd54 | 259 | m_focus = FindFocus(); |
60178eb4 VZ |
260 | if ( m_focus ) |
261 | { | |
d7cff34d VZ |
262 | delete m_handlerFocus; |
263 | m_handlerFocus = new wxPopupFocusHandler(this); | |
264 | ||
265 | m_focus->PushEventHandler(m_handlerFocus); | |
60178eb4 | 266 | } |
537a0880 RR |
267 | #else |
268 | // GTK+ catches the activate events from the popup | |
269 | // window, not the focus events from the child window | |
270 | delete m_handlerFocus; | |
271 | m_handlerFocus = new wxPopupFocusHandler(this); | |
272 | PushEventHandler(m_handlerFocus); | |
273 | #endif // __WXMSW__ | |
326c7ea8 | 274 | |
537a0880 RR |
275 | } |
276 | ||
277 | bool wxPopupTransientWindow::Show( bool show ) | |
278 | { | |
279 | #ifdef __WXGTK__ | |
280 | if (!show) | |
f4bb632c RR |
281 | { |
282 | gdk_pointer_ungrab( (guint32)GDK_CURRENT_TIME ); | |
2997ca30 | 283 | |
537a0880 | 284 | gtk_grab_remove( m_widget ); |
f4bb632c | 285 | } |
537a0880 RR |
286 | #endif |
287 | ||
c51c4752 RR |
288 | #ifdef __WXX11__ |
289 | if (!show) | |
290 | { | |
291 | XUngrabPointer( wxGlobalDisplay(), CurrentTime ); | |
292 | } | |
293 | #endif | |
294 | ||
537a0880 | 295 | bool ret = wxPopupWindow::Show( show ); |
2997ca30 | 296 | |
537a0880 RR |
297 | #ifdef __WXGTK__ |
298 | if (show) | |
f4bb632c | 299 | { |
537a0880 | 300 | gtk_grab_add( m_widget ); |
2997ca30 | 301 | |
f4bb632c RR |
302 | gdk_pointer_grab( m_widget->window, TRUE, |
303 | (GdkEventMask) | |
304 | (GDK_BUTTON_PRESS_MASK | | |
305 | GDK_BUTTON_RELEASE_MASK | | |
306 | GDK_POINTER_MOTION_HINT_MASK | | |
307 | GDK_POINTER_MOTION_MASK), | |
308 | (GdkWindow *) NULL, | |
309 | (GdkCursor *) NULL, | |
310 | (guint32)GDK_CURRENT_TIME ); | |
311 | } | |
537a0880 RR |
312 | #endif |
313 | ||
c51c4752 RR |
314 | #ifdef __WXX11__ |
315 | if (show) | |
316 | { | |
317 | Window xwindow = (Window) m_clientWindow; | |
2997ca30 | 318 | |
c51c4752 RR |
319 | /* int res =*/ XGrabPointer(wxGlobalDisplay(), xwindow, |
320 | True, | |
321 | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask, | |
322 | GrabModeAsync, | |
323 | GrabModeAsync, | |
324 | None, | |
325 | None, | |
326 | CurrentTime ); | |
327 | } | |
328 | #endif | |
537a0880 | 329 | return ret; |
c02ee97f VZ |
330 | } |
331 | ||
332 | void wxPopupTransientWindow::Dismiss() | |
333 | { | |
334 | PopHandlers(); | |
335 | ||
336 | Hide(); | |
337 | } | |
338 | ||
339 | void wxPopupTransientWindow::DismissAndNotify() | |
340 | { | |
341 | Dismiss(); | |
342 | ||
343 | OnDismiss(); | |
344 | } | |
345 | ||
346 | void wxPopupTransientWindow::OnDismiss() | |
347 | { | |
348 | // nothing to do here - but it may be interesting for derived class | |
349 | } | |
350 | ||
351 | bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event)) | |
352 | { | |
353 | // no special processing here | |
7e548f6b | 354 | return false; |
c02ee97f VZ |
355 | } |
356 | ||
a8132cd4 | 357 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
358 | |
359 | // ---------------------------------------------------------------------------- | |
360 | // wxPopupComboWindow | |
361 | // ---------------------------------------------------------------------------- | |
362 | ||
e0c83227 VZ |
363 | BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow) |
364 | EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown) | |
365 | END_EVENT_TABLE() | |
366 | ||
c02ee97f VZ |
367 | wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent) |
368 | : wxPopupTransientWindow(parent) | |
369 | { | |
370 | m_combo = parent; | |
371 | } | |
372 | ||
373 | bool wxPopupComboWindow::Create(wxComboControl *parent) | |
374 | { | |
375 | m_combo = parent; | |
376 | ||
377 | return wxPopupWindow::Create(parent); | |
378 | } | |
379 | ||
380 | void wxPopupComboWindow::PositionNearCombo() | |
381 | { | |
382 | // the origin point must be in screen coords | |
c47addef | 383 | wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0,0)); |
c02ee97f | 384 | |
89e7a223 | 385 | #if 0 //def __WXUNIVERSAL__ |
c02ee97f VZ |
386 | // account for the fact that (0, 0) is not the top left corner of the |
387 | // window: there is also the border | |
388 | wxRect rectBorders = m_combo->GetRenderer()-> | |
389 | GetBorderDimensions(m_combo->GetBorder()); | |
390 | ptOrigin.x -= rectBorders.x; | |
391 | ptOrigin.y -= rectBorders.y; | |
392 | #endif // __WXUNIVERSAL__ | |
393 | ||
394 | // position below or above the combobox: the width is 0 to put it exactly | |
395 | // below us, not to the left or to the right | |
396 | Position(ptOrigin, wxSize(0, m_combo->GetSize().y)); | |
397 | } | |
398 | ||
399 | void wxPopupComboWindow::OnDismiss() | |
400 | { | |
401 | m_combo->OnDismiss(); | |
402 | } | |
403 | ||
e0c83227 VZ |
404 | void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event) |
405 | { | |
406 | m_combo->ProcessEvent(event); | |
407 | } | |
408 | ||
a8132cd4 | 409 | #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
410 | |
411 | // ---------------------------------------------------------------------------- | |
412 | // wxPopupWindowHandler | |
413 | // ---------------------------------------------------------------------------- | |
414 | ||
415 | void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event) | |
416 | { | |
417 | // let the window have it first (we're the first event handler in the chain | |
418 | // of handlers for this window) | |
419 | if ( m_popup->ProcessLeftDown(event) ) | |
420 | { | |
421 | return; | |
422 | } | |
326c7ea8 | 423 | |
c02ee97f VZ |
424 | wxPoint pos = event.GetPosition(); |
425 | ||
426 | // scrollbar on which the click occured | |
427 | wxWindow *sbar = NULL; | |
428 | ||
429 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
326c7ea8 | 430 | |
c02ee97f VZ |
431 | switch ( win->HitTest(pos.x, pos.y) ) |
432 | { | |
433 | case wxHT_WINDOW_OUTSIDE: | |
326c7ea8 VZ |
434 | { |
435 | // do the coords translation now as after DismissAndNotify() | |
436 | // m_popup may be destroyed | |
437 | wxMouseEvent event2(event); | |
438 | ||
439 | m_popup->ClientToScreen(&event2.m_x, &event2.m_y); | |
440 | ||
441 | // clicking outside a popup dismisses it | |
442 | m_popup->DismissAndNotify(); | |
443 | ||
444 | // dismissing a tooltip shouldn't waste a click, i.e. you | |
445 | // should be able to dismiss it and press the button with the | |
446 | // same click, so repost this event to the window beneath us | |
447 | wxWindow *win = wxFindWindowAtPoint(event2.GetPosition()); | |
448 | if ( win ) | |
449 | { | |
450 | // translate the event coords to the ones of the window | |
451 | // which is going to get the event | |
452 | win->ScreenToClient(&event2.m_x, &event2.m_y); | |
453 | ||
454 | event2.SetEventObject(win); | |
455 | wxPostEvent(win, event2); | |
456 | } | |
457 | } | |
c02ee97f VZ |
458 | break; |
459 | ||
a8132cd4 | 460 | #ifdef __WXUNIVERSAL__ |
c02ee97f VZ |
461 | case wxHT_WINDOW_HORZ_SCROLLBAR: |
462 | sbar = win->GetScrollbar(wxHORIZONTAL); | |
463 | break; | |
464 | ||
465 | case wxHT_WINDOW_VERT_SCROLLBAR: | |
466 | sbar = win->GetScrollbar(wxVERTICAL); | |
467 | break; | |
a8132cd4 | 468 | #endif |
c02ee97f VZ |
469 | |
470 | default: | |
471 | // forgot to update the switch after adding a new hit test code? | |
472 | wxFAIL_MSG( _T("unexpected HitTest() return value") ); | |
473 | // fall through | |
474 | ||
475 | case wxHT_WINDOW_CORNER: | |
476 | // don't actually know if this one is good for anything, but let it | |
477 | // pass just in case | |
478 | ||
479 | case wxHT_WINDOW_INSIDE: | |
480 | // let the normal processing take place | |
481 | event.Skip(); | |
482 | break; | |
483 | } | |
484 | ||
485 | if ( sbar ) | |
486 | { | |
487 | // translate the event coordinates to the scrollbar ones | |
488 | pos = sbar->ScreenToClient(win->ClientToScreen(pos)); | |
489 | ||
490 | // and give the event to it | |
491 | wxMouseEvent event2 = event; | |
492 | event2.m_x = pos.x; | |
493 | event2.m_y = pos.y; | |
494 | ||
495 | (void)sbar->GetEventHandler()->ProcessEvent(event2); | |
496 | } | |
497 | } | |
498 | ||
499 | // ---------------------------------------------------------------------------- | |
500 | // wxPopupFocusHandler | |
501 | // ---------------------------------------------------------------------------- | |
502 | ||
503 | void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event) | |
504 | { | |
60178eb4 VZ |
505 | // when we lose focus we always disappear - unless it goes to the popup (in |
506 | // which case we don't really lose it) | |
36a56c65 VS |
507 | wxWindow *win = event.GetWindow(); |
508 | while ( win ) | |
509 | { | |
510 | if ( win == m_popup ) | |
511 | return; | |
512 | win = win->GetParent(); | |
513 | } | |
326c7ea8 | 514 | |
36a56c65 | 515 | m_popup->DismissAndNotify(); |
60178eb4 | 516 | } |
54800df8 | 517 | |
d7cff34d | 518 | void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event) |
60178eb4 VZ |
519 | { |
520 | // let the window have it first, it might process the keys | |
521 | if ( !m_popup->ProcessEvent(event) ) | |
522 | { | |
523 | // by default, dismiss the popup | |
54800df8 | 524 | m_popup->DismissAndNotify(); |
60178eb4 | 525 | } |
c02ee97f VZ |
526 | } |
527 | ||
528 | #endif // wxUSE_POPUPWIN | |
eb729cd3 | 529 |