Next try on pop up menus. (#ifdef x11).
[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 #ifndef __WXX11__
220 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
221 {
222 // see above
223 m_handlerFocus = NULL;
224 }
225 #endif
226 m_focus = NULL;
227 }
228 }
229
230 void wxPopupTransientWindow::Popup(wxWindow *winFocus)
231 {
232 const wxWindowList& children = GetChildren();
233 if ( children.GetCount() )
234 {
235 m_child = children.GetFirst()->GetData();
236 }
237 else
238 {
239 m_child = this;
240 }
241
242 // we can't capture mouse before the window is shown in wxGTK, so do it
243 // first
244 Show();
245
246 delete m_handlerPopup;
247 m_handlerPopup = new wxPopupWindowHandler(this);
248
249 m_child->CaptureMouse();
250 m_child->PushEventHandler(m_handlerPopup);
251
252 m_focus = winFocus ? winFocus : this;
253 m_focus->SetFocus();
254
255 #ifndef __WXX11__
256
257 #ifdef __WXMSW__
258 // MSW doesn't allow to set focus to the popup window, but we need to
259 // subclass the window which has the focus, and not winFocus passed in or
260 // otherwise everything else breaks down
261 m_focus = FindFocus();
262 if ( m_focus )
263 #endif // __WXMSW__
264 {
265 delete m_handlerFocus;
266 m_handlerFocus = new wxPopupFocusHandler(this);
267
268 m_focus->PushEventHandler(m_handlerFocus);
269 }
270
271 #endif
272 }
273
274 void wxPopupTransientWindow::Dismiss()
275 {
276 PopHandlers();
277
278 Hide();
279 }
280
281 void wxPopupTransientWindow::DismissAndNotify()
282 {
283 Dismiss();
284
285 OnDismiss();
286 }
287
288 void wxPopupTransientWindow::OnDismiss()
289 {
290 // nothing to do here - but it may be interesting for derived class
291 }
292
293 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
294 {
295 // no special processing here
296 return FALSE;
297 }
298
299 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
300
301 // ----------------------------------------------------------------------------
302 // wxPopupComboWindow
303 // ----------------------------------------------------------------------------
304
305 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
306 : wxPopupTransientWindow(parent)
307 {
308 m_combo = parent;
309 }
310
311 bool wxPopupComboWindow::Create(wxComboControl *parent)
312 {
313 m_combo = parent;
314
315 return wxPopupWindow::Create(parent);
316 }
317
318 void wxPopupComboWindow::PositionNearCombo()
319 {
320 // the origin point must be in screen coords
321 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
322
323 #if 0 //def __WXUNIVERSAL__
324 // account for the fact that (0, 0) is not the top left corner of the
325 // window: there is also the border
326 wxRect rectBorders = m_combo->GetRenderer()->
327 GetBorderDimensions(m_combo->GetBorder());
328 ptOrigin.x -= rectBorders.x;
329 ptOrigin.y -= rectBorders.y;
330 #endif // __WXUNIVERSAL__
331
332 // position below or above the combobox: the width is 0 to put it exactly
333 // below us, not to the left or to the right
334 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
335 }
336
337 void wxPopupComboWindow::OnDismiss()
338 {
339 m_combo->OnDismiss();
340 }
341
342 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
343
344 // ----------------------------------------------------------------------------
345 // wxPopupWindowHandler
346 // ----------------------------------------------------------------------------
347
348 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
349 {
350 // let the window have it first (we're the first event handler in the chain
351 // of handlers for this window)
352 if ( m_popup->ProcessLeftDown(event) )
353 {
354 return;
355 }
356
357 wxPoint pos = event.GetPosition();
358
359 // scrollbar on which the click occured
360 wxWindow *sbar = NULL;
361
362 wxWindow *win = (wxWindow *)event.GetEventObject();
363
364 switch ( win->HitTest(pos.x, pos.y) )
365 {
366 case wxHT_WINDOW_OUTSIDE:
367 // clicking outside a popup dismisses it
368 m_popup->DismissAndNotify();
369 break;
370
371 #ifdef __WXUNIVERSAL__
372 case wxHT_WINDOW_HORZ_SCROLLBAR:
373 sbar = win->GetScrollbar(wxHORIZONTAL);
374 break;
375
376 case wxHT_WINDOW_VERT_SCROLLBAR:
377 sbar = win->GetScrollbar(wxVERTICAL);
378 break;
379 #endif
380
381 default:
382 // forgot to update the switch after adding a new hit test code?
383 wxFAIL_MSG( _T("unexpected HitTest() return value") );
384 // fall through
385
386 case wxHT_WINDOW_CORNER:
387 // don't actually know if this one is good for anything, but let it
388 // pass just in case
389
390 case wxHT_WINDOW_INSIDE:
391 // let the normal processing take place
392 event.Skip();
393 break;
394 }
395
396 if ( sbar )
397 {
398 // translate the event coordinates to the scrollbar ones
399 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
400
401 // and give the event to it
402 wxMouseEvent event2 = event;
403 event2.m_x = pos.x;
404 event2.m_y = pos.y;
405
406 (void)sbar->GetEventHandler()->ProcessEvent(event2);
407 }
408 }
409
410 // ----------------------------------------------------------------------------
411 // wxPopupFocusHandler
412 // ----------------------------------------------------------------------------
413
414 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
415 {
416 #ifdef __WXGTK__
417 // ignore the next OnKillFocus() call
418 if ( time(NULL) < m_creationTime + 1 )
419 {
420 event.Skip();
421
422 return;
423 }
424 #endif // __WXGTK__
425
426 // when we lose focus we always disappear - unless it goes to the popup (in
427 // which case we don't really lose it)
428 wxWindow *win = event.GetWindow();
429 while ( win )
430 {
431 if ( win == m_popup )
432 return;
433 win = win->GetParent();
434 }
435
436 m_popup->DismissAndNotify();
437 }
438
439 void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
440 {
441 // let the window have it first, it might process the keys
442 if ( !m_popup->ProcessEvent(event) )
443 {
444 // by default, dismiss the popup
445 m_popup->DismissAndNotify();
446 }
447 }
448
449 #endif // wxUSE_POPUPWIN
450