Various changes to make pop up menus work
[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 license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
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 && !defined(__WXMOTIF__)
32
33 #include "wx/popupwin.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/combobox.h" // wxComboControl
37 #include "wx/log.h"
38 #endif //WX_PRECOMP
39
40 #ifdef __WXUNIVERSAL__
41 #include "wx/univ/renderer.h"
42 #endif // __WXUNIVERSAL__
43
44 // there is no src/{msw,mgl}/popupwin.cpp to put this in, so we do it here - BTW we
45 // probably could do it for all ports here just as well
46 #if defined(__WXMSW__) || defined(__WXMGL__)
47 IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow)
48 #endif // __WXMSW__
49
50 IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow)
51
52 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
53 IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow)
54 #endif
55
56 // ----------------------------------------------------------------------------
57 // private classes
58 // ----------------------------------------------------------------------------
59
60 // event handlers which we use to intercept events which cause the popup to
61 // disappear
62 class wxPopupWindowHandler : public wxEvtHandler
63 {
64 public:
65 wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
66
67 protected:
68 // event handlers
69 void OnLeftDown(wxMouseEvent& event);
70
71 private:
72 wxPopupTransientWindow *m_popup;
73
74 DECLARE_EVENT_TABLE()
75 };
76
77 class wxPopupFocusHandler : public wxEvtHandler
78 {
79 public:
80 wxPopupFocusHandler(wxPopupTransientWindow *popup)
81 {
82 m_popup = popup;
83
84 #ifdef __WXGTK__
85 // ignore the next few OnKillFocus() calls
86 m_creationTime = time(NULL);
87 #endif // __WXGTK__
88 }
89
90 protected:
91 // event handlers
92 void OnKillFocus(wxFocusEvent& event);
93 void OnKeyDown(wxKeyEvent& event);
94
95 private:
96 wxPopupTransientWindow *m_popup;
97
98 // hack around wxGTK bug: we always get several kill focus events
99 // immediately after creation!
100 #ifdef __WXGTK__
101 time_t m_creationTime;
102 #endif // __WXGTK__
103
104 DECLARE_EVENT_TABLE()
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 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
117 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
118 END_EVENT_TABLE()
119
120 // ============================================================================
121 // implementation
122 // ============================================================================
123
124 // ----------------------------------------------------------------------------
125 // wxPopupWindowBase
126 // ----------------------------------------------------------------------------
127
128 wxPopupWindowBase::~wxPopupWindowBase()
129 {
130 // this destructor is required for Darwin
131 }
132
133 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
134 {
135 return TRUE;
136 }
137
138 void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
139 const wxSize& size)
140 {
141 wxSize sizeScreen = wxGetDisplaySize(),
142 sizeSelf = GetSize();
143
144 // is there enough space to put the popup below the window (where we put it
145 // by default)?
146 wxCoord y = ptOrigin.y + size.y;
147 if ( y + sizeSelf.y > sizeScreen.y )
148 {
149 // check if there is enough space above
150 if ( ptOrigin.y > sizeSelf.y )
151 {
152 // do position the control above the window
153 y -= size.y + sizeSelf.y;
154 }
155 //else: not enough space below nor above, leave below
156 }
157
158 // now check left/right too
159 wxCoord x = ptOrigin.x + size.x;
160 if ( x + sizeSelf.x > sizeScreen.x )
161 {
162 // check if there is enough space to the left
163 if ( ptOrigin.x > sizeSelf.x )
164 {
165 // do position the control to the left
166 x -= size.x + sizeSelf.x;
167 }
168 //else: not enough space there neither, leave in default position
169 }
170
171 Move(x, y, wxSIZE_NO_ADJUSTMENTS);
172 }
173
174 // ----------------------------------------------------------------------------
175 // wxPopupTransientWindow
176 // ----------------------------------------------------------------------------
177
178 void wxPopupTransientWindow::Init()
179 {
180 m_child =
181 m_focus = (wxWindow *)NULL;
182
183 m_handlerFocus = NULL;
184 m_handlerPopup = NULL;
185 }
186
187 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
188 {
189 Init();
190
191 (void)Create(parent, style);
192 }
193
194 wxPopupTransientWindow::~wxPopupTransientWindow()
195 {
196 PopHandlers();
197
198 delete m_handlerFocus;
199 delete m_handlerPopup;
200 }
201
202 void wxPopupTransientWindow::PopHandlers()
203 {
204 if ( m_child )
205 {
206 if ( !m_child->RemoveEventHandler(m_handlerPopup) )
207 {
208 // something is very wrong and someone else probably deleted our
209 // handler - so don't risk deleting it second time
210 m_handlerPopup = NULL;
211 }
212
213 m_child->ReleaseMouse();
214 m_child = NULL;
215 }
216
217 if ( m_focus )
218 {
219 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
220 {
221 // see above
222 m_handlerFocus = NULL;
223 }
224
225 m_focus = NULL;
226 }
227 }
228
229 void wxPopupTransientWindow::Popup(wxWindow *winFocus)
230 {
231 const wxWindowList& children = GetChildren();
232 if ( children.GetCount() )
233 {
234 m_child = children.GetFirst()->GetData();
235 }
236 else
237 {
238 m_child = this;
239 }
240
241 // we can't capture mouse before the window is shown in wxGTK, so do it
242 // first
243 Show();
244
245 delete m_handlerPopup;
246 m_handlerPopup = new wxPopupWindowHandler(this);
247
248 m_child->CaptureMouse();
249 m_child->PushEventHandler(m_handlerPopup);
250
251 m_focus = winFocus ? winFocus : this;
252 m_focus->SetFocus();
253
254 #ifdef __WXMSW__
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
258 m_focus = FindFocus();
259 if ( m_focus )
260 {
261 delete m_handlerFocus;
262 m_handlerFocus = new wxPopupFocusHandler(this);
263
264 m_focus->PushEventHandler(m_handlerFocus);
265 }
266 #endif // __WXMSW__
267 }
268
269 void wxPopupTransientWindow::Dismiss()
270 {
271 PopHandlers();
272
273 Hide();
274 }
275
276 void wxPopupTransientWindow::DismissAndNotify()
277 {
278 Dismiss();
279
280 OnDismiss();
281 }
282
283 void wxPopupTransientWindow::OnDismiss()
284 {
285 // nothing to do here - but it may be interesting for derived class
286 }
287
288 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
289 {
290 // no special processing here
291 return FALSE;
292 }
293
294 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
295
296 // ----------------------------------------------------------------------------
297 // wxPopupComboWindow
298 // ----------------------------------------------------------------------------
299
300 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
301 : wxPopupTransientWindow(parent)
302 {
303 m_combo = parent;
304 }
305
306 bool wxPopupComboWindow::Create(wxComboControl *parent)
307 {
308 m_combo = parent;
309
310 return wxPopupWindow::Create(parent);
311 }
312
313 void wxPopupComboWindow::PositionNearCombo()
314 {
315 // the origin point must be in screen coords
316 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
317
318 #if 0 //def __WXUNIVERSAL__
319 // account for the fact that (0, 0) is not the top left corner of the
320 // window: there is also the border
321 wxRect rectBorders = m_combo->GetRenderer()->
322 GetBorderDimensions(m_combo->GetBorder());
323 ptOrigin.x -= rectBorders.x;
324 ptOrigin.y -= rectBorders.y;
325 #endif // __WXUNIVERSAL__
326
327 // position below or above the combobox: the width is 0 to put it exactly
328 // below us, not to the left or to the right
329 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
330 }
331
332 void wxPopupComboWindow::OnDismiss()
333 {
334 m_combo->OnDismiss();
335 }
336
337 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
338
339 // ----------------------------------------------------------------------------
340 // wxPopupWindowHandler
341 // ----------------------------------------------------------------------------
342
343 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
344 {
345 // let the window have it first (we're the first event handler in the chain
346 // of handlers for this window)
347 if ( m_popup->ProcessLeftDown(event) )
348 {
349 return;
350 }
351
352 wxPoint pos = event.GetPosition();
353
354 // scrollbar on which the click occured
355 wxWindow *sbar = NULL;
356
357 wxWindow *win = (wxWindow *)event.GetEventObject();
358
359 switch ( win->HitTest(pos.x, pos.y) )
360 {
361 case wxHT_WINDOW_OUTSIDE:
362 // clicking outside a popup dismisses it
363 m_popup->DismissAndNotify();
364 break;
365
366 #ifdef __WXUNIVERSAL__
367 case wxHT_WINDOW_HORZ_SCROLLBAR:
368 sbar = win->GetScrollbar(wxHORIZONTAL);
369 break;
370
371 case wxHT_WINDOW_VERT_SCROLLBAR:
372 sbar = win->GetScrollbar(wxVERTICAL);
373 break;
374 #endif
375
376 default:
377 // forgot to update the switch after adding a new hit test code?
378 wxFAIL_MSG( _T("unexpected HitTest() return value") );
379 // fall through
380
381 case wxHT_WINDOW_CORNER:
382 // don't actually know if this one is good for anything, but let it
383 // pass just in case
384
385 case wxHT_WINDOW_INSIDE:
386 // let the normal processing take place
387 event.Skip();
388 break;
389 }
390
391 if ( sbar )
392 {
393 // translate the event coordinates to the scrollbar ones
394 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
395
396 // and give the event to it
397 wxMouseEvent event2 = event;
398 event2.m_x = pos.x;
399 event2.m_y = pos.y;
400
401 (void)sbar->GetEventHandler()->ProcessEvent(event2);
402 }
403 }
404
405 // ----------------------------------------------------------------------------
406 // wxPopupFocusHandler
407 // ----------------------------------------------------------------------------
408
409 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
410 {
411 #ifdef __WXGTK__
412 // ignore the next OnKillFocus() call
413 if ( time(NULL) < m_creationTime + 1 )
414 {
415 event.Skip();
416
417 return;
418 }
419 #endif // __WXGTK__
420
421 // when we lose focus we always disappear - unless it goes to the popup (in
422 // which case we don't really lose it)
423 wxWindow *win = event.GetWindow();
424 while ( win )
425 {
426 if ( win == m_popup )
427 return;
428 win = win->GetParent();
429 }
430
431 m_popup->DismissAndNotify();
432 }
433
434 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
435 {
436 // let the window have it first, it might process the keys
437 if ( !m_popup->ProcessEvent(event) )
438 {
439 // by default, dismiss the popup
440 m_popup->DismissAndNotify();
441 }
442 }
443
444 #endif // wxUSE_POPUPWIN
445