]>
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 | |
eb729cd3 | 49 | |
537a0880 | 50 | IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow) |
6c3422e9 | 51 | IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow) |
761df41e VZ |
52 | |
53 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) | |
54 | IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow) | |
55 | #endif | |
6c3422e9 | 56 | |
c02ee97f VZ |
57 | // ---------------------------------------------------------------------------- |
58 | // private classes | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // event handlers which we use to intercept events which cause the popup to | |
62 | // disappear | |
63 | class wxPopupWindowHandler : public wxEvtHandler | |
64 | { | |
65 | public: | |
66 | wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; } | |
67 | ||
68 | protected: | |
69 | // event handlers | |
70 | void OnLeftDown(wxMouseEvent& event); | |
71 | ||
72 | private: | |
73 | wxPopupTransientWindow *m_popup; | |
74 | ||
75 | DECLARE_EVENT_TABLE() | |
22f3361e | 76 | DECLARE_NO_COPY_CLASS(wxPopupWindowHandler) |
c02ee97f VZ |
77 | }; |
78 | ||
79 | class wxPopupFocusHandler : public wxEvtHandler | |
80 | { | |
81 | public: | |
516cdd54 VZ |
82 | wxPopupFocusHandler(wxPopupTransientWindow *popup) |
83 | { | |
84 | m_popup = popup; | |
516cdd54 | 85 | } |
c02ee97f VZ |
86 | |
87 | protected: | |
88 | // event handlers | |
537a0880 RR |
89 | #ifdef __WXMSW__ |
90 | // Under MSW, we catch the kill focus event | |
c02ee97f | 91 | void OnKillFocus(wxFocusEvent& event); |
537a0880 RR |
92 | #else |
93 | // Under GTK+, event a transient popup window | |
94 | // is a toplevel window so we need to catch | |
95 | // deactivate events | |
96 | void OnActivate(wxActivateEvent &event); | |
97 | #endif | |
d7cff34d | 98 | void OnKeyDown(wxKeyEvent& event); |
c02ee97f VZ |
99 | |
100 | private: | |
101 | wxPopupTransientWindow *m_popup; | |
102 | ||
103 | DECLARE_EVENT_TABLE() | |
22f3361e | 104 | DECLARE_NO_COPY_CLASS(wxPopupFocusHandler) |
c02ee97f VZ |
105 | }; |
106 | ||
107 | // ---------------------------------------------------------------------------- | |
108 | // event tables | |
109 | // ---------------------------------------------------------------------------- | |
110 | ||
111 | BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler) | |
112 | EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown) | |
113 | END_EVENT_TABLE() | |
114 | ||
115 | BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler) | |
537a0880 | 116 | #ifdef __WXMSW__ |
c02ee97f | 117 | EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus) |
537a0880 RR |
118 | #else |
119 | EVT_ACTIVATE(wxPopupFocusHandler::OnActivate) | |
120 | #endif | |
d7cff34d | 121 | EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown) |
c02ee97f VZ |
122 | END_EVENT_TABLE() |
123 | ||
124 | // ============================================================================ | |
125 | // implementation | |
126 | // ============================================================================ | |
127 | ||
128 | // ---------------------------------------------------------------------------- | |
129 | // wxPopupWindowBase | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
799ea011 GD |
132 | wxPopupWindowBase::~wxPopupWindowBase() |
133 | { | |
134 | // this destructor is required for Darwin | |
135 | } | |
136 | ||
c02ee97f VZ |
137 | bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags)) |
138 | { | |
7e548f6b | 139 | return true; |
c02ee97f VZ |
140 | } |
141 | ||
142 | void wxPopupWindowBase::Position(const wxPoint& ptOrigin, | |
143 | const wxSize& size) | |
144 | { | |
145 | wxSize sizeScreen = wxGetDisplaySize(), | |
146 | sizeSelf = GetSize(); | |
147 | ||
148 | // is there enough space to put the popup below the window (where we put it | |
149 | // by default)? | |
150 | wxCoord y = ptOrigin.y + size.y; | |
151 | if ( y + sizeSelf.y > sizeScreen.y ) | |
152 | { | |
153 | // check if there is enough space above | |
154 | if ( ptOrigin.y > sizeSelf.y ) | |
155 | { | |
156 | // do position the control above the window | |
157 | y -= size.y + sizeSelf.y; | |
158 | } | |
159 | //else: not enough space below nor above, leave below | |
160 | } | |
161 | ||
162 | // now check left/right too | |
163 | wxCoord x = ptOrigin.x + size.x; | |
164 | if ( x + sizeSelf.x > sizeScreen.x ) | |
165 | { | |
166 | // check if there is enough space to the left | |
167 | if ( ptOrigin.x > sizeSelf.x ) | |
168 | { | |
169 | // do position the control to the left | |
170 | x -= size.x + sizeSelf.x; | |
171 | } | |
172 | //else: not enough space there neither, leave in default position | |
173 | } | |
174 | ||
175 | Move(x, y, wxSIZE_NO_ADJUSTMENTS); | |
176 | } | |
177 | ||
178 | // ---------------------------------------------------------------------------- | |
179 | // wxPopupTransientWindow | |
180 | // ---------------------------------------------------------------------------- | |
181 | ||
182 | void wxPopupTransientWindow::Init() | |
183 | { | |
184 | m_child = | |
185 | m_focus = (wxWindow *)NULL; | |
d7cff34d VZ |
186 | |
187 | m_handlerFocus = NULL; | |
188 | m_handlerPopup = NULL; | |
c02ee97f VZ |
189 | } |
190 | ||
758bce95 | 191 | wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style) |
c02ee97f VZ |
192 | { |
193 | Init(); | |
194 | ||
758bce95 | 195 | (void)Create(parent, style); |
c02ee97f VZ |
196 | } |
197 | ||
198 | wxPopupTransientWindow::~wxPopupTransientWindow() | |
199 | { | |
200 | PopHandlers(); | |
201 | } | |
202 | ||
203 | void wxPopupTransientWindow::PopHandlers() | |
204 | { | |
205 | if ( m_child ) | |
206 | { | |
f6758939 | 207 | if ( m_handlerPopup && !m_child->RemoveEventHandler(m_handlerPopup) ) |
d7cff34d VZ |
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 | ||
c02ee97f VZ |
214 | m_child->ReleaseMouse(); |
215 | m_child = NULL; | |
216 | } | |
217 | ||
537a0880 | 218 | #ifdef __WXMSW__ |
c02ee97f VZ |
219 | if ( m_focus ) |
220 | { | |
f6758939 | 221 | if ( m_handlerFocus && !m_focus->RemoveEventHandler(m_handlerFocus) ) |
d7cff34d VZ |
222 | { |
223 | // see above | |
224 | m_handlerFocus = NULL; | |
225 | } | |
c02ee97f | 226 | } |
537a0880 | 227 | #else |
f6758939 | 228 | if ( m_handlerFocus && !RemoveEventHandler(m_handlerFocus) ) |
537a0880 RR |
229 | { |
230 | // see above | |
231 | m_handlerFocus = NULL; | |
232 | } | |
233 | #endif | |
f6758939 RR |
234 | |
235 | // delete the handlers, they'll be created as necessary in Popup() | |
236 | delete m_handlerPopup; | |
237 | m_handlerPopup = NULL; | |
238 | delete m_handlerFocus; | |
239 | m_handlerFocus = NULL; | |
240 | ||
537a0880 | 241 | m_focus = NULL; |
c02ee97f VZ |
242 | } |
243 | ||
244 | void wxPopupTransientWindow::Popup(wxWindow *winFocus) | |
245 | { | |
246 | const wxWindowList& children = GetChildren(); | |
247 | if ( children.GetCount() ) | |
248 | { | |
249 | m_child = children.GetFirst()->GetData(); | |
250 | } | |
251 | else | |
252 | { | |
253 | m_child = this; | |
254 | } | |
255 | ||
e4606ed9 | 256 | Show(); |
e4606ed9 | 257 | |
d7cff34d VZ |
258 | delete m_handlerPopup; |
259 | m_handlerPopup = new wxPopupWindowHandler(this); | |
260 | ||
c02ee97f | 261 | m_child->CaptureMouse(); |
d7cff34d | 262 | m_child->PushEventHandler(m_handlerPopup); |
c02ee97f | 263 | |
c02ee97f | 264 | m_focus = winFocus ? winFocus : this; |
c02ee97f | 265 | m_focus->SetFocus(); |
60178eb4 | 266 | |
516cdd54 | 267 | #ifdef __WXMSW__ |
d7cff34d VZ |
268 | // MSW doesn't allow to set focus to the popup window, but we need to |
269 | // subclass the window which has the focus, and not winFocus passed in or | |
270 | // otherwise everything else breaks down | |
516cdd54 | 271 | m_focus = FindFocus(); |
60178eb4 VZ |
272 | if ( m_focus ) |
273 | { | |
d7cff34d VZ |
274 | delete m_handlerFocus; |
275 | m_handlerFocus = new wxPopupFocusHandler(this); | |
276 | ||
277 | m_focus->PushEventHandler(m_handlerFocus); | |
60178eb4 | 278 | } |
537a0880 RR |
279 | #else |
280 | // GTK+ catches the activate events from the popup | |
281 | // window, not the focus events from the child window | |
282 | delete m_handlerFocus; | |
283 | m_handlerFocus = new wxPopupFocusHandler(this); | |
284 | PushEventHandler(m_handlerFocus); | |
285 | #endif // __WXMSW__ | |
326c7ea8 | 286 | |
537a0880 RR |
287 | } |
288 | ||
289 | bool wxPopupTransientWindow::Show( bool show ) | |
290 | { | |
291 | #ifdef __WXGTK__ | |
292 | if (!show) | |
293 | gtk_grab_remove( m_widget ); | |
294 | #endif | |
295 | ||
296 | bool ret = wxPopupWindow::Show( show ); | |
297 | ||
298 | #ifdef __WXGTK__ | |
299 | if (show) | |
300 | gtk_grab_add( m_widget ); | |
301 | #endif | |
302 | ||
303 | return ret; | |
c02ee97f VZ |
304 | } |
305 | ||
306 | void wxPopupTransientWindow::Dismiss() | |
307 | { | |
308 | PopHandlers(); | |
309 | ||
310 | Hide(); | |
311 | } | |
312 | ||
313 | void wxPopupTransientWindow::DismissAndNotify() | |
314 | { | |
315 | Dismiss(); | |
316 | ||
317 | OnDismiss(); | |
318 | } | |
319 | ||
320 | void wxPopupTransientWindow::OnDismiss() | |
321 | { | |
322 | // nothing to do here - but it may be interesting for derived class | |
323 | } | |
324 | ||
325 | bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event)) | |
326 | { | |
327 | // no special processing here | |
7e548f6b | 328 | return false; |
c02ee97f VZ |
329 | } |
330 | ||
a8132cd4 | 331 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
332 | |
333 | // ---------------------------------------------------------------------------- | |
334 | // wxPopupComboWindow | |
335 | // ---------------------------------------------------------------------------- | |
336 | ||
e0c83227 VZ |
337 | BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow) |
338 | EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown) | |
339 | END_EVENT_TABLE() | |
340 | ||
c02ee97f VZ |
341 | wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent) |
342 | : wxPopupTransientWindow(parent) | |
343 | { | |
344 | m_combo = parent; | |
345 | } | |
346 | ||
347 | bool wxPopupComboWindow::Create(wxComboControl *parent) | |
348 | { | |
349 | m_combo = parent; | |
350 | ||
351 | return wxPopupWindow::Create(parent); | |
352 | } | |
353 | ||
354 | void wxPopupComboWindow::PositionNearCombo() | |
355 | { | |
356 | // the origin point must be in screen coords | |
357 | wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0)); | |
358 | ||
89e7a223 | 359 | #if 0 //def __WXUNIVERSAL__ |
c02ee97f VZ |
360 | // account for the fact that (0, 0) is not the top left corner of the |
361 | // window: there is also the border | |
362 | wxRect rectBorders = m_combo->GetRenderer()-> | |
363 | GetBorderDimensions(m_combo->GetBorder()); | |
364 | ptOrigin.x -= rectBorders.x; | |
365 | ptOrigin.y -= rectBorders.y; | |
366 | #endif // __WXUNIVERSAL__ | |
367 | ||
368 | // position below or above the combobox: the width is 0 to put it exactly | |
369 | // below us, not to the left or to the right | |
370 | Position(ptOrigin, wxSize(0, m_combo->GetSize().y)); | |
371 | } | |
372 | ||
373 | void wxPopupComboWindow::OnDismiss() | |
374 | { | |
375 | m_combo->OnDismiss(); | |
376 | } | |
377 | ||
e0c83227 VZ |
378 | void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event) |
379 | { | |
380 | m_combo->ProcessEvent(event); | |
381 | } | |
382 | ||
a8132cd4 | 383 | #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
384 | |
385 | // ---------------------------------------------------------------------------- | |
386 | // wxPopupWindowHandler | |
387 | // ---------------------------------------------------------------------------- | |
388 | ||
389 | void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event) | |
390 | { | |
391 | // let the window have it first (we're the first event handler in the chain | |
392 | // of handlers for this window) | |
393 | if ( m_popup->ProcessLeftDown(event) ) | |
394 | { | |
395 | return; | |
396 | } | |
326c7ea8 | 397 | |
c02ee97f VZ |
398 | wxPoint pos = event.GetPosition(); |
399 | ||
400 | // scrollbar on which the click occured | |
401 | wxWindow *sbar = NULL; | |
402 | ||
403 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
326c7ea8 | 404 | |
c02ee97f VZ |
405 | switch ( win->HitTest(pos.x, pos.y) ) |
406 | { | |
407 | case wxHT_WINDOW_OUTSIDE: | |
326c7ea8 VZ |
408 | { |
409 | // do the coords translation now as after DismissAndNotify() | |
410 | // m_popup may be destroyed | |
411 | wxMouseEvent event2(event); | |
412 | ||
413 | m_popup->ClientToScreen(&event2.m_x, &event2.m_y); | |
414 | ||
415 | // clicking outside a popup dismisses it | |
416 | m_popup->DismissAndNotify(); | |
417 | ||
418 | // dismissing a tooltip shouldn't waste a click, i.e. you | |
419 | // should be able to dismiss it and press the button with the | |
420 | // same click, so repost this event to the window beneath us | |
421 | wxWindow *win = wxFindWindowAtPoint(event2.GetPosition()); | |
422 | if ( win ) | |
423 | { | |
424 | // translate the event coords to the ones of the window | |
425 | // which is going to get the event | |
426 | win->ScreenToClient(&event2.m_x, &event2.m_y); | |
427 | ||
428 | event2.SetEventObject(win); | |
429 | wxPostEvent(win, event2); | |
430 | } | |
431 | } | |
c02ee97f VZ |
432 | break; |
433 | ||
a8132cd4 | 434 | #ifdef __WXUNIVERSAL__ |
c02ee97f VZ |
435 | case wxHT_WINDOW_HORZ_SCROLLBAR: |
436 | sbar = win->GetScrollbar(wxHORIZONTAL); | |
437 | break; | |
438 | ||
439 | case wxHT_WINDOW_VERT_SCROLLBAR: | |
440 | sbar = win->GetScrollbar(wxVERTICAL); | |
441 | break; | |
a8132cd4 | 442 | #endif |
c02ee97f VZ |
443 | |
444 | default: | |
445 | // forgot to update the switch after adding a new hit test code? | |
446 | wxFAIL_MSG( _T("unexpected HitTest() return value") ); | |
447 | // fall through | |
448 | ||
449 | case wxHT_WINDOW_CORNER: | |
450 | // don't actually know if this one is good for anything, but let it | |
451 | // pass just in case | |
452 | ||
453 | case wxHT_WINDOW_INSIDE: | |
454 | // let the normal processing take place | |
455 | event.Skip(); | |
456 | break; | |
457 | } | |
458 | ||
459 | if ( sbar ) | |
460 | { | |
461 | // translate the event coordinates to the scrollbar ones | |
462 | pos = sbar->ScreenToClient(win->ClientToScreen(pos)); | |
463 | ||
464 | // and give the event to it | |
465 | wxMouseEvent event2 = event; | |
466 | event2.m_x = pos.x; | |
467 | event2.m_y = pos.y; | |
468 | ||
469 | (void)sbar->GetEventHandler()->ProcessEvent(event2); | |
470 | } | |
471 | } | |
472 | ||
473 | // ---------------------------------------------------------------------------- | |
474 | // wxPopupFocusHandler | |
475 | // ---------------------------------------------------------------------------- | |
476 | ||
537a0880 | 477 | #ifdef __WXMSW__ |
c02ee97f VZ |
478 | void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event) |
479 | { | |
60178eb4 VZ |
480 | // when we lose focus we always disappear - unless it goes to the popup (in |
481 | // which case we don't really lose it) | |
36a56c65 VS |
482 | wxWindow *win = event.GetWindow(); |
483 | while ( win ) | |
484 | { | |
485 | if ( win == m_popup ) | |
486 | return; | |
487 | win = win->GetParent(); | |
488 | } | |
326c7ea8 | 489 | |
36a56c65 | 490 | m_popup->DismissAndNotify(); |
60178eb4 | 491 | } |
537a0880 RR |
492 | #else |
493 | void wxPopupFocusHandler::OnActivate(wxActivateEvent &event) | |
494 | { | |
495 | if (event.GetActive()) | |
496 | m_popup->DismissAndNotify(); | |
497 | } | |
498 | #endif | |
54800df8 | 499 | |
d7cff34d | 500 | void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event) |
60178eb4 VZ |
501 | { |
502 | // let the window have it first, it might process the keys | |
503 | if ( !m_popup->ProcessEvent(event) ) | |
504 | { | |
505 | // by default, dismiss the popup | |
54800df8 | 506 | m_popup->DismissAndNotify(); |
60178eb4 | 507 | } |
c02ee97f VZ |
508 | } |
509 | ||
510 | #endif // wxUSE_POPUPWIN | |
eb729cd3 | 511 |