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