]> git.saurik.com Git - wxWidgets.git/blob - src/common/popupcmn.cpp
minor fix that was part of helpbest.patch
[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 #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
49 class wxPopupWindowHandler : public wxEvtHandler
50 {
51 public:
52 wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
53
54 protected:
55 // event handlers
56 void OnLeftDown(wxMouseEvent& event);
57
58 private:
59 wxPopupTransientWindow *m_popup;
60
61 DECLARE_EVENT_TABLE()
62 };
63
64 class wxPopupFocusHandler : public wxEvtHandler
65 {
66 public:
67 wxPopupFocusHandler(wxPopupTransientWindow *popup) { m_popup = popup; }
68
69 protected:
70 // event handlers
71 void OnKillFocus(wxFocusEvent& event);
72
73 private:
74 wxPopupTransientWindow *m_popup;
75
76 DECLARE_EVENT_TABLE()
77 };
78
79 // ----------------------------------------------------------------------------
80 // event tables
81 // ----------------------------------------------------------------------------
82
83 BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler)
84 EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown)
85 END_EVENT_TABLE()
86
87 BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler)
88 EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus)
89 END_EVENT_TABLE()
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxPopupWindowBase
97 // ----------------------------------------------------------------------------
98
99 bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags))
100 {
101 return TRUE;
102 }
103
104 void 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
144 void wxPopupTransientWindow::Init()
145 {
146 m_child =
147 m_focus = (wxWindow *)NULL;
148 }
149
150 wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent)
151 {
152 Init();
153
154 (void)Create(parent);
155 }
156
157 wxPopupTransientWindow::~wxPopupTransientWindow()
158 {
159 PopHandlers();
160 }
161
162 void 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
178 void 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
190 // we can't capture mouse before the window is shown in wxGTL
191 #ifdef __WXGTK__
192 Show();
193 #endif
194
195 m_child->CaptureMouse();
196 m_child->PushEventHandler(new wxPopupWindowHandler(this));
197
198 #ifndef __WXGTK__
199 Show();
200 #endif
201
202 m_focus = winFocus ? winFocus : this;
203 m_focus->PushEventHandler(new wxPopupFocusHandler(this));
204 m_focus->SetFocus();
205 }
206
207 void wxPopupTransientWindow::Dismiss()
208 {
209 PopHandlers();
210
211 Hide();
212 }
213
214 void wxPopupTransientWindow::DismissAndNotify()
215 {
216 Dismiss();
217
218 OnDismiss();
219 }
220
221 void wxPopupTransientWindow::OnDismiss()
222 {
223 // nothing to do here - but it may be interesting for derived class
224 }
225
226 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
227 {
228 // no special processing here
229 return FALSE;
230 }
231
232 #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
233
234 // ----------------------------------------------------------------------------
235 // wxPopupComboWindow
236 // ----------------------------------------------------------------------------
237
238 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
239 : wxPopupTransientWindow(parent)
240 {
241 m_combo = parent;
242 }
243
244 bool wxPopupComboWindow::Create(wxComboControl *parent)
245 {
246 m_combo = parent;
247
248 return wxPopupWindow::Create(parent);
249 }
250
251 void wxPopupComboWindow::PositionNearCombo()
252 {
253 // the origin point must be in screen coords
254 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
255
256 #if 0 //def __WXUNIVERSAL__
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
270 void wxPopupComboWindow::OnDismiss()
271 {
272 m_combo->OnDismiss();
273 }
274
275 #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__)
276
277 // ----------------------------------------------------------------------------
278 // wxPopupWindowHandler
279 // ----------------------------------------------------------------------------
280
281 void 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
303 #ifdef __WXUNIVERSAL__
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;
311 #endif
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
346 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
347 {
348 // when we lose focus we always disappear
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();
354 }
355
356 #endif // wxUSE_POPUPWIN