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