]> git.saurik.com Git - wxWidgets.git/blob - src/common/popupcmn.cpp
Changed the pragmas to allow debugging under Linux
[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 "popupwin.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
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 m_child->CaptureMouse();
191 m_child->PushEventHandler(new wxPopupWindowHandler(this));
192
193 Show();
194
195 m_focus = winFocus ? winFocus : this;
196 m_focus->PushEventHandler(new wxPopupFocusHandler(this));
197 m_focus->SetFocus();
198 }
199
200 void wxPopupTransientWindow::Dismiss()
201 {
202 PopHandlers();
203
204 Hide();
205 }
206
207 void wxPopupTransientWindow::DismissAndNotify()
208 {
209 Dismiss();
210
211 OnDismiss();
212 }
213
214 void wxPopupTransientWindow::OnDismiss()
215 {
216 // nothing to do here - but it may be interesting for derived class
217 }
218
219 bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event))
220 {
221 // no special processing here
222 return FALSE;
223 }
224
225 #if wxUSE_COMBOBOX
226
227 // ----------------------------------------------------------------------------
228 // wxPopupComboWindow
229 // ----------------------------------------------------------------------------
230
231 wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent)
232 : wxPopupTransientWindow(parent)
233 {
234 m_combo = parent;
235 }
236
237 bool wxPopupComboWindow::Create(wxComboControl *parent)
238 {
239 m_combo = parent;
240
241 return wxPopupWindow::Create(parent);
242 }
243
244 void wxPopupComboWindow::PositionNearCombo()
245 {
246 // the origin point must be in screen coords
247 wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0));
248
249 #ifdef __WXUNIVERSAL__
250 // account for the fact that (0, 0) is not the top left corner of the
251 // window: there is also the border
252 wxRect rectBorders = m_combo->GetRenderer()->
253 GetBorderDimensions(m_combo->GetBorder());
254 ptOrigin.x -= rectBorders.x;
255 ptOrigin.y -= rectBorders.y;
256 #endif // __WXUNIVERSAL__
257
258 // position below or above the combobox: the width is 0 to put it exactly
259 // below us, not to the left or to the right
260 Position(ptOrigin, wxSize(0, m_combo->GetSize().y));
261 }
262
263 void wxPopupComboWindow::OnDismiss()
264 {
265 m_combo->OnDismiss();
266 }
267
268 #endif // wxUSE_COMBOBOX
269
270 // ----------------------------------------------------------------------------
271 // wxPopupWindowHandler
272 // ----------------------------------------------------------------------------
273
274 void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event)
275 {
276 // let the window have it first (we're the first event handler in the chain
277 // of handlers for this window)
278 if ( m_popup->ProcessLeftDown(event) )
279 {
280 return;
281 }
282
283 wxPoint pos = event.GetPosition();
284
285 // scrollbar on which the click occured
286 wxWindow *sbar = NULL;
287
288 wxWindow *win = (wxWindow *)event.GetEventObject();
289 switch ( win->HitTest(pos.x, pos.y) )
290 {
291 case wxHT_WINDOW_OUTSIDE:
292 // clicking outside a popup dismisses it
293 m_popup->DismissAndNotify();
294 break;
295
296 case wxHT_WINDOW_HORZ_SCROLLBAR:
297 sbar = win->GetScrollbar(wxHORIZONTAL);
298 break;
299
300 case wxHT_WINDOW_VERT_SCROLLBAR:
301 sbar = win->GetScrollbar(wxVERTICAL);
302 break;
303
304 default:
305 // forgot to update the switch after adding a new hit test code?
306 wxFAIL_MSG( _T("unexpected HitTest() return value") );
307 // fall through
308
309 case wxHT_WINDOW_CORNER:
310 // don't actually know if this one is good for anything, but let it
311 // pass just in case
312
313 case wxHT_WINDOW_INSIDE:
314 // let the normal processing take place
315 event.Skip();
316 break;
317 }
318
319 if ( sbar )
320 {
321 // translate the event coordinates to the scrollbar ones
322 pos = sbar->ScreenToClient(win->ClientToScreen(pos));
323
324 // and give the event to it
325 wxMouseEvent event2 = event;
326 event2.m_x = pos.x;
327 event2.m_y = pos.y;
328
329 (void)sbar->GetEventHandler()->ProcessEvent(event2);
330 }
331 }
332
333 // ----------------------------------------------------------------------------
334 // wxPopupFocusHandler
335 // ----------------------------------------------------------------------------
336
337 void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event)
338 {
339 // when we lose focus we always disappear
340 m_popup->DismissAndNotify();
341 }
342
343 #endif // wxUSE_POPUPWIN