Tried to restore wxPopupTransientWindow functionality
[wxWidgets.git] / src / common / popupcmn.cpp
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>
9 // License: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "popupwinbase.h"
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
31 #if wxUSE_POPUPWIN
32
33 #include "wx/popupwin.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/combobox.h" // wxComboControl
37 #include "wx/app.h" // wxPostEvent
38 #include "wx/log.h"
39 #include "wx/app.h"
40 #endif //WX_PRECOMP
41
42 #ifdef __WXUNIVERSAL__
43 #include "wx/univ/renderer.h"
44 #endif // __WXUNIVERSAL__
45
46 #ifdef __WXGTK__
47 #include <gtk/gtk.h>
48 #endif
49
50 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
51 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
52
53 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
54 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
55 #endif
56
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()
76 DECLARE_NO_COPY_CLASS(wxPopupWindowHandler)
77 };
78
79 class wxPopupFocusHandler : public wxEvtHandler
80 {
81 public:
82 wxPopupFocusHandler(wxPopupTransientWindow *popup)
83 {
84 m_popup = popup;
85 }
86
87 protected:
88 // event handlers
89 #ifdef __WXMSW__
90 // Under MSW, we catch the kill focus event
91 void OnKillFocus(wxFocusEvent& event);
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
98 void OnKeyDown(wxKeyEvent& event);
99
100 private:
101 wxPopupTransientWindow *m_popup;
102
103 DECLARE_EVENT_TABLE()
104 DECLARE_NO_COPY_CLASS(wxPopupFocusHandler)
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)
116 #ifdef __WXMSW__
117 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
118 #else
119 EVT_ACTIVATE(wxPopupFocusHandler::OnActivate)
120 #endif
121 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
122 END_EVENT_TABLE()
123
124 // ============================================================================
125 // implementation
126 // ============================================================================
127
128 // ----------------------------------------------------------------------------
129 // wxPopupWindowBase
130 // ----------------------------------------------------------------------------
131
132 wxPopupWindowBase::~wxPopupWindowBase()
133 {
134 // this destructor is required for Darwin
135 }
136
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;
186
187 m_handlerFocus = NULL;
188 m_handlerPopup = NULL;
189 }
190
191 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
192 {
193 Init();
194
195 (void)Create(parent, style);
196 }
197
198 wxPopupTransientWindow::~wxPopupTransientWindow()
199 {
200 PopHandlers();
201
202 delete m_handlerFocus;
203 delete m_handlerPopup;
204 }
205
206 void wxPopupTransientWindow::PopHandlers()
207 {
208 if ( m_child )
209 {
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
217 m_child->ReleaseMouse();
218 m_child = NULL;
219 }
220
221 #ifdef __WXMSW__
222 if ( m_focus )
223 {
224 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
225 {
226 // see above
227 m_handlerFocus = NULL;
228 }
229 }
230 #else
231 if ( !RemoveEventHandler(m_handlerFocus) )
232 {
233 // see above
234 m_handlerFocus = NULL;
235 }
236 #endif
237 m_focus = NULL;
238 }
239
240 void wxPopupTransientWindow::Popup(wxWindow *winFocus)
241 {
242 const wxWindowList& children = GetChildren();
243 if ( children.GetCount() )
244 {
245 m_child = children.GetFirst()->GetData();
246 }
247 else
248 {
249 m_child = this;
250 }
251
252 Show();
253
254 delete m_handlerPopup;
255 m_handlerPopup = new wxPopupWindowHandler(this);
256
257 m_child->CaptureMouse();
258 m_child->PushEventHandler(m_handlerPopup);
259
260 m_focus = winFocus ? winFocus : this;
261 m_focus->SetFocus();
262
263 #ifdef __WXMSW__
264 // MSW doesn't allow to set focus to the popup window, but we need to
265 // subclass the window which has the focus, and not winFocus passed in or
266 // otherwise everything else breaks down
267 m_focus = FindFocus();
268 if ( m_focus )
269 {
270 delete m_handlerFocus;
271 m_handlerFocus = new wxPopupFocusHandler(this);
272
273 m_focus->PushEventHandler(m_handlerFocus);
274 }
275 #else
276 // GTK+ catches the activate events from the popup
277 // window, not the focus events from the child window
278 delete m_handlerFocus;
279 m_handlerFocus = new wxPopupFocusHandler(this);
280 PushEventHandler(m_handlerFocus);
281 #endif // __WXMSW__
282
283 }
284
285 bool wxPopupTransientWindow::Show( bool show )
286 {
287 #ifdef __WXGTK__
288 if (!show)
289 gtk_grab_remove( m_widget );
290 #endif
291
292 bool ret = wxPopupWindow::Show( show );
293
294 #ifdef __WXGTK__
295 if (show)
296 gtk_grab_add( m_widget );
297 #endif
298
299 return ret;
300 }
301
302 void wxPopupTransientWindow::Dismiss()
303 {
304 PopHandlers();
305
306 Hide();
307 }
308
309 void wxPopupTransientWindow::DismissAndNotify()
310 {
311 Dismiss();
312
313 OnDismiss();
314 }
315
316 void wxPopupTransientWindow::OnDismiss()
317 {
318 // nothing to do here - but it may be interesting for derived class
319 }
320
321 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
322 {
323 // no special processing here
324 return false;
325 }
326
327 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
328
329 // ----------------------------------------------------------------------------
330 // wxPopupComboWindow
331 // ----------------------------------------------------------------------------
332
333 BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
334 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
335 END_EVENT_TABLE()
336
337 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
338 : wxPopupTransientWindow(parent)
339 {
340 m_combo = parent;
341 }
342
343 bool wxPopupComboWindow::Create(wxComboControl *parent)
344 {
345 m_combo = parent;
346
347 return wxPopupWindow::Create(parent);
348 }
349
350 void wxPopupComboWindow::PositionNearCombo()
351 {
352 // the origin point must be in screen coords
353 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
354
355 #if 0 //def __WXUNIVERSAL__
356 // account for the fact that (0, 0) is not the top left corner of the
357 // window: there is also the border
358 wxRect rectBorders = m_combo->GetRenderer()->
359 GetBorderDimensions(m_combo->GetBorder());
360 ptOrigin.x -= rectBorders.x;
361 ptOrigin.y -= rectBorders.y;
362 #endif // __WXUNIVERSAL__
363
364 // position below or above the combobox: the width is 0 to put it exactly
365 // below us, not to the left or to the right
366 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
367 }
368
369 void wxPopupComboWindow::OnDismiss()
370 {
371 m_combo->OnDismiss();
372 }
373
374 void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
375 {
376 m_combo->ProcessEvent(event);
377 }
378
379 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
380
381 // ----------------------------------------------------------------------------
382 // wxPopupWindowHandler
383 // ----------------------------------------------------------------------------
384
385 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
386 {
387 // let the window have it first (we're the first event handler in the chain
388 // of handlers for this window)
389 if ( m_popup->ProcessLeftDown(event) )
390 {
391 return;
392 }
393
394 wxPoint pos = event.GetPosition();
395
396 // scrollbar on which the click occured
397 wxWindow *sbar = NULL;
398
399 wxWindow *win = (wxWindow *)event.GetEventObject();
400
401 switch ( win->HitTest(pos.x, pos.y) )
402 {
403 case wxHT_WINDOW_OUTSIDE:
404 {
405 // do the coords translation now as after DismissAndNotify()
406 // m_popup may be destroyed
407 wxMouseEvent event2(event);
408
409 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
410
411 // clicking outside a popup dismisses it
412 m_popup->DismissAndNotify();
413
414 // dismissing a tooltip shouldn't waste a click, i.e. you
415 // should be able to dismiss it and press the button with the
416 // same click, so repost this event to the window beneath us
417 wxWindow *win = wxFindWindowAtPoint(event2.GetPosition());
418 if ( win )
419 {
420 // translate the event coords to the ones of the window
421 // which is going to get the event
422 win->ScreenToClient(&event2.m_x, &event2.m_y);
423
424 event2.SetEventObject(win);
425 wxPostEvent(win, event2);
426 }
427 }
428 break;
429
430 #ifdef __WXUNIVERSAL__
431 case wxHT_WINDOW_HORZ_SCROLLBAR:
432 sbar = win->GetScrollbar(wxHORIZONTAL);
433 break;
434
435 case wxHT_WINDOW_VERT_SCROLLBAR:
436 sbar = win->GetScrollbar(wxVERTICAL);
437 break;
438 #endif
439
440 default:
441 // forgot to update the switch after adding a new hit test code?
442 wxFAIL_MSG( _T("unexpected HitTest() return value") );
443 // fall through
444
445 case wxHT_WINDOW_CORNER:
446 // don't actually know if this one is good for anything, but let it
447 // pass just in case
448
449 case wxHT_WINDOW_INSIDE:
450 // let the normal processing take place
451 event.Skip();
452 break;
453 }
454
455 if ( sbar )
456 {
457 // translate the event coordinates to the scrollbar ones
458 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
459
460 // and give the event to it
461 wxMouseEvent event2 = event;
462 event2.m_x = pos.x;
463 event2.m_y = pos.y;
464
465 (void)sbar->GetEventHandler()->ProcessEvent(event2);
466 }
467 }
468
469 // ----------------------------------------------------------------------------
470 // wxPopupFocusHandler
471 // ----------------------------------------------------------------------------
472
473 #ifdef __WXMSW__
474 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
475 {
476 // when we lose focus we always disappear - unless it goes to the popup (in
477 // which case we don't really lose it)
478 wxWindow *win = event.GetWindow();
479 while ( win )
480 {
481 if ( win == m_popup )
482 return;
483 win = win->GetParent();
484 }
485
486 m_popup->DismissAndNotify();
487 }
488 #else
489 void wxPopupFocusHandler::OnActivate(wxActivateEvent &event)
490 {
491 if (event.GetActive())
492 m_popup->DismissAndNotify();
493 }
494 #endif
495
496 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
497 {
498 // let the window have it first, it might process the keys
499 if ( !m_popup->ProcessEvent(event) )
500 {
501 // by default, dismiss the popup
502 m_popup->DismissAndNotify();
503 }
504 }
505
506 #endif // wxUSE_POPUPWIN
507