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