]> git.saurik.com Git - wxWidgets.git/blame - src/common/popupcmn.cpp
We use native paths now in wxWindows. On the Mac, too.
[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
37#endif //WX_PRECOMP
38
39#ifdef __WXUNIVERSAL__
40 #include "wx/univ/renderer.h"
41#endif // __WXUNIVERSAL__
42
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// event handlers which we use to intercept events which cause the popup to
48// disappear
49class wxPopupWindowHandler : public wxEvtHandler
50{
51public:
52 wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
53
54protected:
55 // event handlers
56 void OnLeftDown(wxMouseEvent& event);
57
58private:
59 wxPopupTransientWindow *m_popup;
60
61 DECLARE_EVENT_TABLE()
62};
63
64class wxPopupFocusHandler : public wxEvtHandler
65{
66public:
67 wxPopupFocusHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
68
69protected:
70 // event handlers
71 void OnKillFocus(wxFocusEvent& event);
72
73private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
77};
78
79// ----------------------------------------------------------------------------
80// event tables
81// ----------------------------------------------------------------------------
82
83BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
84 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
85END_EVENT_TABLE()
86
87BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
88 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
89END_EVENT_TABLE()
90
91// ============================================================================
92// implementation
93// ============================================================================
94
95// ----------------------------------------------------------------------------
96// wxPopupWindowBase
97// ----------------------------------------------------------------------------
98
99bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
100{
101 return TRUE;
102}
103
104void wxPopupWindowBase::Position(const wxPoint& ptOrigin,
105 const wxSize& size)
106{
107 wxSize sizeScreen = wxGetDisplaySize(),
108 sizeSelf = GetSize();
109
110 // is there enough space to put the popup below the window (where we put it
111 // by default)?
112 wxCoord y = ptOrigin.y + size.y;
113 if ( y + sizeSelf.y > sizeScreen.y )
114 {
115 // check if there is enough space above
116 if ( ptOrigin.y > sizeSelf.y )
117 {
118 // do position the control above the window
119 y -= size.y + sizeSelf.y;
120 }
121 //else: not enough space below nor above, leave below
122 }
123
124 // now check left/right too
125 wxCoord x = ptOrigin.x + size.x;
126 if ( x + sizeSelf.x > sizeScreen.x )
127 {
128 // check if there is enough space to the left
129 if ( ptOrigin.x > sizeSelf.x )
130 {
131 // do position the control to the left
132 x -= size.x + sizeSelf.x;
133 }
134 //else: not enough space there neither, leave in default position
135 }
136
137 Move(x, y, wxSIZE_NO_ADJUSTMENTS);
138}
139
140// ----------------------------------------------------------------------------
141// wxPopupTransientWindow
142// ----------------------------------------------------------------------------
143
144void wxPopupTransientWindow::Init()
145{
146 m_child =
147 m_focus = (wxWindow *)NULL;
148}
149
150wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent)
151{
152 Init();
153
154 (void)Create(parent);
155}
156
157wxPopupTransientWindow::~wxPopupTransientWindow()
158{
159 PopHandlers();
160}
161
162void wxPopupTransientWindow::PopHandlers()
163{
164 if ( m_child )
165 {
166 m_child->PopEventHandler(TRUE /* delete it */);
167 m_child->ReleaseMouse();
168 m_child = NULL;
169 }
170
171 if ( m_focus )
172 {
173 m_focus->PopEventHandler(TRUE /* delete it */);
174 m_focus = NULL;
175 }
176}
177
178void wxPopupTransientWindow::Popup(wxWindow *winFocus)
179{
180 const wxWindowList& children = GetChildren();
181 if ( children.GetCount() )
182 {
183 m_child = children.GetFirst()->GetData();
184 }
185 else
186 {
187 m_child = this;
188 }
189
e4606ed9
VZ
190 // we can't capture mouse before the window is shown in wxGTL
191#ifdef __WXGTK__
192 Show();
193#endif
194
c02ee97f
VZ
195 m_child->CaptureMouse();
196 m_child->PushEventHandler(new wxPopupWindowHandler(this));
197
e4606ed9 198#ifndef __WXGTK__
c02ee97f 199 Show();
e4606ed9 200#endif
c02ee97f
VZ
201
202 m_focus = winFocus ? winFocus : this;
203 m_focus->PushEventHandler(new wxPopupFocusHandler(this));
204 m_focus->SetFocus();
205}
206
207void wxPopupTransientWindow::Dismiss()
208{
209 PopHandlers();
210
211 Hide();
212}
213
214void wxPopupTransientWindow::DismissAndNotify()
215{
216 Dismiss();
217
218 OnDismiss();
219}
220
221void wxPopupTransientWindow::OnDismiss()
222{
223 // nothing to do here - but it may be interesting for derived class
224}
225
226bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
227{
228 // no special processing here
229 return FALSE;
230}
231
a8132cd4 232#if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
233
234// ----------------------------------------------------------------------------
235// wxPopupComboWindow
236// ----------------------------------------------------------------------------
237
238wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
239 : wxPopupTransientWindow(parent)
240{
241 m_combo = parent;
242}
243
244bool wxPopupComboWindow::Create(wxComboControl *parent)
245{
246 m_combo = parent;
247
248 return wxPopupWindow::Create(parent);
249}
250
251void wxPopupComboWindow::PositionNearCombo()
252{
253 // the origin point must be in screen coords
254 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
255
89e7a223 256#if 0 //def __WXUNIVERSAL__
c02ee97f
VZ
257 // account for the fact that (0, 0) is not the top left corner of the
258 // window: there is also the border
259 wxRect rectBorders = m_combo->GetRenderer()->
260 GetBorderDimensions(m_combo->GetBorder());
261 ptOrigin.x -= rectBorders.x;
262 ptOrigin.y -= rectBorders.y;
263#endif // __WXUNIVERSAL__
264
265 // position below or above the combobox: the width is 0 to put it exactly
266 // below us, not to the left or to the right
267 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
268}
269
270void wxPopupComboWindow::OnDismiss()
271{
272 m_combo->OnDismiss();
273}
274
a8132cd4 275#endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
c02ee97f
VZ
276
277// ----------------------------------------------------------------------------
278// wxPopupWindowHandler
279// ----------------------------------------------------------------------------
280
281void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
282{
283 // let the window have it first (we're the first event handler in the chain
284 // of handlers for this window)
285 if ( m_popup->ProcessLeftDown(event) )
286 {
287 return;
288 }
289
290 wxPoint pos = event.GetPosition();
291
292 // scrollbar on which the click occured
293 wxWindow *sbar = NULL;
294
295 wxWindow *win = (wxWindow *)event.GetEventObject();
296 switch ( win->HitTest(pos.x, pos.y) )
297 {
298 case wxHT_WINDOW_OUTSIDE:
299 // clicking outside a popup dismisses it
300 m_popup->DismissAndNotify();
301 break;
302
a8132cd4 303#ifdef __WXUNIVERSAL__
c02ee97f
VZ
304 case wxHT_WINDOW_HORZ_SCROLLBAR:
305 sbar = win->GetScrollbar(wxHORIZONTAL);
306 break;
307
308 case wxHT_WINDOW_VERT_SCROLLBAR:
309 sbar = win->GetScrollbar(wxVERTICAL);
310 break;
a8132cd4 311#endif
c02ee97f
VZ
312
313 default:
314 // forgot to update the switch after adding a new hit test code?
315 wxFAIL_MSG( _T("unexpected HitTest() return value") );
316 // fall through
317
318 case wxHT_WINDOW_CORNER:
319 // don't actually know if this one is good for anything, but let it
320 // pass just in case
321
322 case wxHT_WINDOW_INSIDE:
323 // let the normal processing take place
324 event.Skip();
325 break;
326 }
327
328 if ( sbar )
329 {
330 // translate the event coordinates to the scrollbar ones
331 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
332
333 // and give the event to it
334 wxMouseEvent event2 = event;
335 event2.m_x = pos.x;
336 event2.m_y = pos.y;
337
338 (void)sbar->GetEventHandler()->ProcessEvent(event2);
339 }
340}
341
342// ----------------------------------------------------------------------------
343// wxPopupFocusHandler
344// ----------------------------------------------------------------------------
345
346void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
347{
348 // when we lose focus we always disappear
54800df8
JS
349
350 // But if m_popup was about to get the focus,
351 // don't disappear.
352 if (event.GetWindow() != m_popup)
353 m_popup->DismissAndNotify();
c02ee97f
VZ
354}
355
356#endif // wxUSE_POPUPWIN