]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
fixed the width of the (week day as number) field, should be 1, not 2
[wxWidgets.git] / src / common / popupcmn.cpp
CommitLineData
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>
9// License: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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 52IMPLEMENT_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
64class wxPopupWindowHandler : public wxEvtHandler
65{
66public:
67 wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
68
69protected:
70 // event handlers
71 void OnLeftDown(wxMouseEvent& event);
72
73private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
77};
78
79class wxPopupFocusHandler : public wxEvtHandler
80{
81public:
516cdd54
VZ
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 }
c02ee97f
VZ
91
92protected:
93 // event handlers
94 void OnKillFocus(wxFocusEvent& event);
d7cff34d 95 void OnKeyDown(wxKeyEvent& event);
c02ee97f
VZ
96
97private:
98 wxPopupTransientWindow *m_popup;
99
516cdd54
VZ
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
c02ee97f
VZ
106 DECLARE_EVENT_TABLE()
107};
108
109// ----------------------------------------------------------------------------
110// event tables
111// ----------------------------------------------------------------------------
112
113BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
114 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
115END_EVENT_TABLE()
116
117BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
118 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
d7cff34d 119 EVT_KEY_DOWN(wxPopupFocusHandler::OnKeyDown)
c02ee97f
VZ
120END_EVENT_TABLE()
121
122// ============================================================================
123// implementation
124// ============================================================================
125
126// ----------------------------------------------------------------------------
127// wxPopupWindowBase
128// ----------------------------------------------------------------------------
129
799ea011
GD
130wxPopupWindowBase::~wxPopupWindowBase()
131{
132 // this destructor is required for Darwin
133}
134
c02ee97f
VZ
135bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
136{
137 return TRUE;
138}
139
140void 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
180void wxPopupTransientWindow::Init()
181{
182 m_child =
183 m_focus = (wxWindow *)NULL;
d7cff34d
VZ
184
185 m_handlerFocus = NULL;
186 m_handlerPopup = NULL;
c02ee97f
VZ
187}
188
758bce95 189wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style)
c02ee97f
VZ
190{
191 Init();
192
758bce95 193 (void)Create(parent, style);
c02ee97f
VZ
194}
195
196wxPopupTransientWindow::~wxPopupTransientWindow()
197{
198 PopHandlers();
d7cff34d
VZ
199
200 delete m_handlerFocus;
201 delete m_handlerPopup;
c02ee97f
VZ
202}
203
204void wxPopupTransientWindow::PopHandlers()
205{
206 if ( m_child )
207 {
d7cff34d
VZ
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
c02ee97f
VZ
215 m_child->ReleaseMouse();
216 m_child = NULL;
217 }
218
219 if ( m_focus )
220 {
9b8270da 221#ifndef __WXX11__
d7cff34d
VZ
222 if ( !m_focus->RemoveEventHandler(m_handlerFocus) )
223 {
224 // see above
225 m_handlerFocus = NULL;
226 }
9b8270da 227#endif
c02ee97f
VZ
228 m_focus = NULL;
229 }
230}
231
232void 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
516cdd54
VZ
244 // we can't capture mouse before the window is shown in wxGTK, so do it
245 // first
e4606ed9 246 Show();
e4606ed9 247
d7cff34d
VZ
248 delete m_handlerPopup;
249 m_handlerPopup = new wxPopupWindowHandler(this);
250
c02ee97f 251 m_child->CaptureMouse();
d7cff34d 252 m_child->PushEventHandler(m_handlerPopup);
c02ee97f 253
c02ee97f 254 m_focus = winFocus ? winFocus : this;
c02ee97f 255 m_focus->SetFocus();
60178eb4 256
9b8270da
RR
257#ifndef __WXX11__
258
516cdd54 259#ifdef __WXMSW__
d7cff34d
VZ
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
516cdd54 263 m_focus = FindFocus();
60178eb4 264 if ( m_focus )
771c7b4a 265#endif // __WXMSW__
60178eb4 266 {
d7cff34d
VZ
267 delete m_handlerFocus;
268 m_handlerFocus = new wxPopupFocusHandler(this);
269
270 m_focus->PushEventHandler(m_handlerFocus);
60178eb4 271 }
326c7ea8
VZ
272
273#endif // !__WXX11__
c02ee97f
VZ
274}
275
276void wxPopupTransientWindow::Dismiss()
277{
278 PopHandlers();
279
280 Hide();
281}
282
283void wxPopupTransientWindow::DismissAndNotify()
284{
285 Dismiss();
286
287 OnDismiss();
288}
289
290void wxPopupTransientWindow::OnDismiss()
291{
292 // nothing to do here - but it may be interesting for derived class
293}
294
295bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
296{
297 // no special processing here
298 return FALSE;
299}
300
a8132cd4 301#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
302
303// ----------------------------------------------------------------------------
304// wxPopupComboWindow
305// ----------------------------------------------------------------------------
306
e0c83227
VZ
307BEGIN_EVENT_TABLE(wxPopupComboWindow, wxPopupTransientWindow)
308 EVT_KEY_DOWN(wxPopupComboWindow::OnKeyDown)
309END_EVENT_TABLE()
310
c02ee97f
VZ
311wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
312 : wxPopupTransientWindow(parent)
313{
314 m_combo = parent;
315}
316
317bool wxPopupComboWindow::Create(wxComboControl *parent)
318{
319 m_combo = parent;
320
321 return wxPopupWindow::Create(parent);
322}
323
324void wxPopupComboWindow::PositionNearCombo()
325{
326 // the origin point must be in screen coords
327 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
328
89e7a223 329#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
330 // account for the fact that (0, 0) is not the top left corner of the
331 // window: there is also the border
332 wxRect rectBorders = m_combo->GetRenderer()->
333 GetBorderDimensions(m_combo->GetBorder());
334 ptOrigin.x -= rectBorders.x;
335 ptOrigin.y -= rectBorders.y;
336#endif // __WXUNIVERSAL__
337
338 // position below or above the combobox: the width is 0 to put it exactly
339 // below us, not to the left or to the right
340 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
341}
342
343void wxPopupComboWindow::OnDismiss()
344{
345 m_combo->OnDismiss();
346}
347
e0c83227
VZ
348void wxPopupComboWindow::OnKeyDown(wxKeyEvent& event)
349{
350 m_combo->ProcessEvent(event);
351}
352
a8132cd4 353#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
354
355// ----------------------------------------------------------------------------
356// wxPopupWindowHandler
357// ----------------------------------------------------------------------------
358
359void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
360{
361 // let the window have it first (we're the first event handler in the chain
362 // of handlers for this window)
363 if ( m_popup->ProcessLeftDown(event) )
364 {
365 return;
366 }
326c7ea8 367
c02ee97f
VZ
368 wxPoint pos = event.GetPosition();
369
370 // scrollbar on which the click occured
371 wxWindow *sbar = NULL;
372
373 wxWindow *win = (wxWindow *)event.GetEventObject();
326c7ea8 374
c02ee97f
VZ
375 switch ( win->HitTest(pos.x, pos.y) )
376 {
377 case wxHT_WINDOW_OUTSIDE:
326c7ea8
VZ
378 {
379 // do the coords translation now as after DismissAndNotify()
380 // m_popup may be destroyed
381 wxMouseEvent event2(event);
382
383 m_popup->ClientToScreen(&event2.m_x, &event2.m_y);
384
385 // clicking outside a popup dismisses it
386 m_popup->DismissAndNotify();
387
388 // dismissing a tooltip shouldn't waste a click, i.e. you
389 // should be able to dismiss it and press the button with the
390 // same click, so repost this event to the window beneath us
391 wxWindow *win = wxFindWindowAtPoint(event2.GetPosition());
392 if ( win )
393 {
394 // translate the event coords to the ones of the window
395 // which is going to get the event
396 win->ScreenToClient(&event2.m_x, &event2.m_y);
397
398 event2.SetEventObject(win);
399 wxPostEvent(win, event2);
400 }
401 }
c02ee97f
VZ
402 break;
403
a8132cd4 404#ifdef __WXUNIVERSAL__
c02ee97f
VZ
405 case wxHT_WINDOW_HORZ_SCROLLBAR:
406 sbar = win->GetScrollbar(wxHORIZONTAL);
407 break;
408
409 case wxHT_WINDOW_VERT_SCROLLBAR:
410 sbar = win->GetScrollbar(wxVERTICAL);
411 break;
a8132cd4 412#endif
c02ee97f
VZ
413
414 default:
415 // forgot to update the switch after adding a new hit test code?
416 wxFAIL_MSG( _T("unexpected HitTest() return value") );
417 // fall through
418
419 case wxHT_WINDOW_CORNER:
420 // don't actually know if this one is good for anything, but let it
421 // pass just in case
422
423 case wxHT_WINDOW_INSIDE:
424 // let the normal processing take place
425 event.Skip();
426 break;
427 }
428
429 if ( sbar )
430 {
431 // translate the event coordinates to the scrollbar ones
432 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
433
434 // and give the event to it
435 wxMouseEvent event2 = event;
436 event2.m_x = pos.x;
437 event2.m_y = pos.y;
438
439 (void)sbar->GetEventHandler()->ProcessEvent(event2);
440 }
441}
442
443// ----------------------------------------------------------------------------
444// wxPopupFocusHandler
445// ----------------------------------------------------------------------------
446
447void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
448{
516cdd54
VZ
449#ifdef __WXGTK__
450 // ignore the next OnKillFocus() call
451 if ( time(NULL) < m_creationTime + 1 )
452 {
453 event.Skip();
454
455 return;
456 }
457#endif // __WXGTK__
458
60178eb4
VZ
459 // when we lose focus we always disappear - unless it goes to the popup (in
460 // which case we don't really lose it)
36a56c65
VS
461 wxWindow *win = event.GetWindow();
462 while ( win )
463 {
464 if ( win == m_popup )
465 return;
466 win = win->GetParent();
467 }
326c7ea8 468
36a56c65 469 m_popup->DismissAndNotify();
60178eb4 470}
54800df8 471
d7cff34d 472void wxPopupFocusHandler::OnKeyDown(wxKeyEvent& event)
60178eb4
VZ
473{
474 // let the window have it first, it might process the keys
475 if ( !m_popup->ProcessEvent(event) )
476 {
477 // by default, dismiss the popup
54800df8 478 m_popup->DismissAndNotify();
60178eb4 479 }
c02ee97f
VZ
480}
481
482#endif // wxUSE_POPUPWIN
eb729cd3 483