]>
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 | ||
24b3cc2c | 46 | // there is no src/mgl/popupwin.cpp to put this in, so we do it here - BTW we |
eb729cd3 | 47 | // probably could do it for all ports here just as well |
e830103c | 48 | #if defined(__WXMGL__) |
eb729cd3 VZ |
49 | IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow) |
50 | #endif // __WXMSW__ | |
51 | ||
6c3422e9 | 52 | IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow) |
761df41e VZ |
53 | |
54 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) | |
55 | IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow) | |
56 | #endif | |
6c3422e9 | 57 | |
c02ee97f VZ |
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() | |
22f3361e | 77 | DECLARE_NO_COPY_CLASS(wxPopupWindowHandler) |
c02ee97f VZ |
78 | }; |
79 | ||
80 | class wxPopupFocusHandler : public wxEvtHandler | |
81 | { | |
82 | public: | |
516cdd54 VZ |
83 | wxPopupFocusHandler(wxPopupTransientWindow *popup) |
84 | { | |
85 | m_popup = popup; | |
86 | ||
87 | #ifdef __WXGTK__ | |
88 | // ignore the next few OnKillFocus() calls | |
89 | m_creationTime = time(NULL); | |
90 | #endif // __WXGTK__ | |
91 | } | |
c02ee97f VZ |
92 | |
93 | protected: | |
94 | // event handlers | |
95 | void OnKillFocus(wxFocusEvent& event); | |
d7cff34d | 96 | void OnKeyDown(wxKeyEvent& event); |
c02ee97f VZ |
97 | |
98 | private: | |
99 | wxPopupTransientWindow *m_popup; | |
100 | ||
516cdd54 VZ |
101 | // hack around wxGTK bug: we always get several kill focus events |
102 | // immediately after creation! | |
103 | #ifdef __WXGTK__ | |
104 | time_t m_creationTime; | |
105 | #endif // __WXGTK__ | |
106 | ||
c02ee97f | 107 | DECLARE_EVENT_TABLE() |
22f3361e | 108 | DECLARE_NO_COPY_CLASS(wxPopupFocusHandler) |
c02ee97f VZ |
109 | }; |
110 | ||
111 | // ---------------------------------------------------------------------------- | |
112 | // event tables | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler) | |
116 | EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown) | |
117 | END_EVENT_TABLE() | |
118 | ||
119 | BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler) | |
120 | EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus) | |
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(); | |
65f8780c | 201 | #ifndef __WXX11__ |
d7cff34d | 202 | delete m_handlerFocus; |
7e548f6b | 203 | #endif |
d7cff34d | 204 | delete m_handlerPopup; |
c02ee97f VZ |
205 | } |
206 | ||
207 | void wxPopupTransientWindow::PopHandlers() | |
208 | { | |
209 | if ( m_child ) | |
210 | { | |
d7cff34d VZ |
211 | if ( !m_child->RemoveEventHandler(m_handlerPopup) ) |
212 | { | |
213 | // something is very wrong and someone else probably deleted our | |
214 | // handler - so don't risk deleting it second time | |
215 | m_handlerPopup = NULL; | |
216 | } | |
217 | ||
c02ee97f VZ |
218 | m_child->ReleaseMouse(); |
219 | m_child = NULL; | |
220 | } | |
221 | ||
222 | if ( m_focus ) | |
223 | { | |
9b8270da | 224 | #ifndef __WXX11__ |
d7cff34d VZ |
225 | if ( !m_focus->RemoveEventHandler(m_handlerFocus) ) |
226 | { | |
227 | // see above | |
228 | m_handlerFocus = NULL; | |
229 | } | |
9b8270da | 230 | #endif |
c02ee97f VZ |
231 | m_focus = NULL; |
232 | } | |
233 | } | |
234 | ||
235 | void wxPopupTransientWindow::Popup(wxWindow *winFocus) | |
236 | { | |
237 | const wxWindowList& children = GetChildren(); | |
238 | if ( children.GetCount() ) | |
239 | { | |
240 | m_child = children.GetFirst()->GetData(); | |
241 | } | |
242 | else | |
243 | { | |
244 | m_child = this; | |
245 | } | |
246 | ||
516cdd54 VZ |
247 | // we can't capture mouse before the window is shown in wxGTK, so do it |
248 | // first | |
e4606ed9 | 249 | Show(); |
e4606ed9 | 250 | |
d7cff34d VZ |
251 | delete m_handlerPopup; |
252 | m_handlerPopup = new wxPopupWindowHandler(this); | |
253 | ||
c02ee97f | 254 | m_child->CaptureMouse(); |
d7cff34d | 255 | m_child->PushEventHandler(m_handlerPopup); |
c02ee97f | 256 | |
c02ee97f | 257 | m_focus = winFocus ? winFocus : this; |
c02ee97f | 258 | m_focus->SetFocus(); |
60178eb4 | 259 | |
9b8270da RR |
260 | #ifndef __WXX11__ |
261 | ||
516cdd54 | 262 | #ifdef __WXMSW__ |
d7cff34d VZ |
263 | // MSW doesn't allow to set focus to the popup window, but we need to |
264 | // subclass the window which has the focus, and not winFocus passed in or | |
265 | // otherwise everything else breaks down | |
516cdd54 | 266 | m_focus = FindFocus(); |
60178eb4 | 267 | if ( m_focus ) |
771c7b4a | 268 | #endif // __WXMSW__ |
60178eb4 | 269 | { |
d7cff34d VZ |
270 | delete m_handlerFocus; |
271 | m_handlerFocus = new wxPopupFocusHandler(this); | |
272 | ||
273 | m_focus->PushEventHandler(m_handlerFocus); | |
60178eb4 | 274 | } |
326c7ea8 VZ |
275 | |
276 | #endif // !__WXX11__ | |
c02ee97f VZ |
277 | } |
278 | ||
279 | void wxPopupTransientWindow::Dismiss() | |
280 | { | |
281 | PopHandlers(); | |
282 | ||
283 | Hide(); | |
284 | } | |
285 | ||
286 | void wxPopupTransientWindow::DismissAndNotify() | |
287 | { | |
288 | Dismiss(); | |
289 | ||
290 | OnDismiss(); | |
291 | } | |
292 | ||
293 | void wxPopupTransientWindow::OnDismiss() | |
294 | { | |
295 | // nothing to do here - but it may be interesting for derived class | |
296 | } | |
297 | ||
298 | bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event)) | |
299 | { | |
300 | // no special processing here | |
7e548f6b | 301 | return false; |
c02ee97f VZ |
302 | } |
303 | ||
a8132cd4 | 304 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
305 | |
306 | // ---------------------------------------------------------------------------- | |
307 | // wxPopupComboWindow | |
308 | // ---------------------------------------------------------------------------- | |
309 | ||
e0c83227 VZ |
310 | BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow) |
311 | EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown) | |
312 | END_EVENT_TABLE() | |
313 | ||
c02ee97f VZ |
314 | wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent) |
315 | : wxPopupTransientWindow(parent) | |
316 | { | |
317 | m_combo = parent; | |
318 | } | |
319 | ||
320 | bool wxPopupComboWindow::Create(wxComboControl *parent) | |
321 | { | |
322 | m_combo = parent; | |
323 | ||
324 | return wxPopupWindow::Create(parent); | |
325 | } | |
326 | ||
327 | void wxPopupComboWindow::PositionNearCombo() | |
328 | { | |
329 | // the origin point must be in screen coords | |
330 | wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0)); | |
331 | ||
89e7a223 | 332 | #if 0 //def __WXUNIVERSAL__ |
c02ee97f VZ |
333 | // account for the fact that (0, 0) is not the top left corner of the |
334 | // window: there is also the border | |
335 | wxRect rectBorders = m_combo->GetRenderer()-> | |
336 | GetBorderDimensions(m_combo->GetBorder()); | |
337 | ptOrigin.x -= rectBorders.x; | |
338 | ptOrigin.y -= rectBorders.y; | |
339 | #endif // __WXUNIVERSAL__ | |
340 | ||
341 | // position below or above the combobox: the width is 0 to put it exactly | |
342 | // below us, not to the left or to the right | |
343 | Position(ptOrigin, wxSize(0, m_combo->GetSize().y)); | |
344 | } | |
345 | ||
346 | void wxPopupComboWindow::OnDismiss() | |
347 | { | |
348 | m_combo->OnDismiss(); | |
349 | } | |
350 | ||
e0c83227 VZ |
351 | void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event) |
352 | { | |
353 | m_combo->ProcessEvent(event); | |
354 | } | |
355 | ||
a8132cd4 | 356 | #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
357 | |
358 | // ---------------------------------------------------------------------------- | |
359 | // wxPopupWindowHandler | |
360 | // ---------------------------------------------------------------------------- | |
361 | ||
362 | void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event) | |
363 | { | |
364 | // let the window have it first (we're the first event handler in the chain | |
365 | // of handlers for this window) | |
366 | if ( m_popup->ProcessLeftDown(event) ) | |
367 | { | |
368 | return; | |
369 | } | |
326c7ea8 | 370 | |
c02ee97f VZ |
371 | wxPoint pos = event.GetPosition(); |
372 | ||
373 | // scrollbar on which the click occured | |
374 | wxWindow *sbar = NULL; | |
375 | ||
376 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
326c7ea8 | 377 | |
c02ee97f VZ |
378 | switch ( win->HitTest(pos.x, pos.y) ) |
379 | { | |
380 | case wxHT_WINDOW_OUTSIDE: | |
326c7ea8 VZ |
381 | { |
382 | // do the coords translation now as after DismissAndNotify() | |
383 | // m_popup may be destroyed | |
384 | wxMouseEvent event2(event); | |
385 | ||
386 | m_popup->ClientToScreen(&event2.m_x, &event2.m_y); | |
387 | ||
388 | // clicking outside a popup dismisses it | |
389 | m_popup->DismissAndNotify(); | |
390 | ||
391 | // dismissing a tooltip shouldn't waste a click, i.e. you | |
392 | // should be able to dismiss it and press the button with the | |
393 | // same click, so repost this event to the window beneath us | |
394 | wxWindow *win = wxFindWindowAtPoint(event2.GetPosition()); | |
395 | if ( win ) | |
396 | { | |
397 | // translate the event coords to the ones of the window | |
398 | // which is going to get the event | |
399 | win->ScreenToClient(&event2.m_x, &event2.m_y); | |
400 | ||
401 | event2.SetEventObject(win); | |
402 | wxPostEvent(win, event2); | |
403 | } | |
404 | } | |
c02ee97f VZ |
405 | break; |
406 | ||
a8132cd4 | 407 | #ifdef __WXUNIVERSAL__ |
c02ee97f VZ |
408 | case wxHT_WINDOW_HORZ_SCROLLBAR: |
409 | sbar = win->GetScrollbar(wxHORIZONTAL); | |
410 | break; | |
411 | ||
412 | case wxHT_WINDOW_VERT_SCROLLBAR: | |
413 | sbar = win->GetScrollbar(wxVERTICAL); | |
414 | break; | |
a8132cd4 | 415 | #endif |
c02ee97f VZ |
416 | |
417 | default: | |
418 | // forgot to update the switch after adding a new hit test code? | |
419 | wxFAIL_MSG( _T("unexpected HitTest() return value") ); | |
420 | // fall through | |
421 | ||
422 | case wxHT_WINDOW_CORNER: | |
423 | // don't actually know if this one is good for anything, but let it | |
424 | // pass just in case | |
425 | ||
426 | case wxHT_WINDOW_INSIDE: | |
427 | // let the normal processing take place | |
428 | event.Skip(); | |
429 | break; | |
430 | } | |
431 | ||
432 | if ( sbar ) | |
433 | { | |
434 | // translate the event coordinates to the scrollbar ones | |
435 | pos = sbar->ScreenToClient(win->ClientToScreen(pos)); | |
436 | ||
437 | // and give the event to it | |
438 | wxMouseEvent event2 = event; | |
439 | event2.m_x = pos.x; | |
440 | event2.m_y = pos.y; | |
441 | ||
442 | (void)sbar->GetEventHandler()->ProcessEvent(event2); | |
443 | } | |
444 | } | |
445 | ||
446 | // ---------------------------------------------------------------------------- | |
447 | // wxPopupFocusHandler | |
448 | // ---------------------------------------------------------------------------- | |
449 | ||
450 | void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event) | |
451 | { | |
516cdd54 VZ |
452 | #ifdef __WXGTK__ |
453 | // ignore the next OnKillFocus() call | |
454 | if ( time(NULL) < m_creationTime + 1 ) | |
455 | { | |
456 | event.Skip(); | |
457 | ||
458 | return; | |
459 | } | |
460 | #endif // __WXGTK__ | |
461 | ||
60178eb4 VZ |
462 | // when we lose focus we always disappear - unless it goes to the popup (in |
463 | // which case we don't really lose it) | |
36a56c65 VS |
464 | wxWindow *win = event.GetWindow(); |
465 | while ( win ) | |
466 | { | |
467 | if ( win == m_popup ) | |
468 | return; | |
469 | win = win->GetParent(); | |
470 | } | |
326c7ea8 | 471 | |
36a56c65 | 472 | m_popup->DismissAndNotify(); |
60178eb4 | 473 | } |
54800df8 | 474 | |
d7cff34d | 475 | void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event) |
60178eb4 VZ |
476 | { |
477 | // let the window have it first, it might process the keys | |
478 | if ( !m_popup->ProcessEvent(event) ) | |
479 | { | |
480 | // by default, dismiss the popup | |
54800df8 | 481 | m_popup->DismissAndNotify(); |
60178eb4 | 482 | } |
c02ee97f VZ |
483 | } |
484 | ||
485 | #endif // wxUSE_POPUPWIN | |
eb729cd3 | 486 |