]>
Commit | Line | Data |
---|---|---|
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 | ||
159960c0 | 43 | // there is no src/{msw,mgl}/popupwin.cpp to put this in, so we do it here - BTW we |
eb729cd3 | 44 | // probably could do it for all ports here just as well |
159960c0 | 45 | #if defined(__WXMSW__) || defined(__WXMGL__) |
eb729cd3 VZ |
46 | IMPLEMENT_DYNAMIC_CLASS(wxPopupWindow, wxWindow) |
47 | #endif // __WXMSW__ | |
48 | ||
6c3422e9 | 49 | IMPLEMENT_DYNAMIC_CLASS(wxPopupTransientWindow, wxPopupWindow) |
761df41e VZ |
50 | |
51 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) | |
52 | IMPLEMENT_DYNAMIC_CLASS(wxPopupComboWindow, wxPopupTransientWindow) | |
53 | #endif | |
6c3422e9 | 54 | |
c02ee97f VZ |
55 | // ---------------------------------------------------------------------------- |
56 | // private classes | |
57 | // ---------------------------------------------------------------------------- | |
58 | ||
59 | // event handlers which we use to intercept events which cause the popup to | |
60 | // disappear | |
61 | class wxPopupWindowHandler : public wxEvtHandler | |
62 | { | |
63 | public: | |
64 | wxPopupWindowHandler(wxPopupTransientWindow *popup) { m_popup = popup; } | |
65 | ||
66 | protected: | |
67 | // event handlers | |
68 | void OnLeftDown(wxMouseEvent& event); | |
69 | ||
70 | private: | |
71 | wxPopupTransientWindow *m_popup; | |
72 | ||
73 | DECLARE_EVENT_TABLE() | |
74 | }; | |
75 | ||
76 | class wxPopupFocusHandler : public wxEvtHandler | |
77 | { | |
78 | public: | |
516cdd54 VZ |
79 | wxPopupFocusHandler(wxPopupTransientWindow *popup) |
80 | { | |
81 | m_popup = popup; | |
82 | ||
83 | #ifdef __WXGTK__ | |
84 | // ignore the next few OnKillFocus() calls | |
85 | m_creationTime = time(NULL); | |
86 | #endif // __WXGTK__ | |
87 | } | |
c02ee97f VZ |
88 | |
89 | protected: | |
90 | // event handlers | |
91 | void OnKillFocus(wxFocusEvent& event); | |
60178eb4 | 92 | void OnKeyUp(wxKeyEvent& event); |
c02ee97f VZ |
93 | |
94 | private: | |
95 | wxPopupTransientWindow *m_popup; | |
96 | ||
516cdd54 VZ |
97 | // hack around wxGTK bug: we always get several kill focus events |
98 | // immediately after creation! | |
99 | #ifdef __WXGTK__ | |
100 | time_t m_creationTime; | |
101 | #endif // __WXGTK__ | |
102 | ||
c02ee97f VZ |
103 | DECLARE_EVENT_TABLE() |
104 | }; | |
105 | ||
106 | // ---------------------------------------------------------------------------- | |
107 | // event tables | |
108 | // ---------------------------------------------------------------------------- | |
109 | ||
110 | BEGIN_EVENT_TABLE(wxPopupWindowHandler, wxEvtHandler) | |
111 | EVT_LEFT_DOWN(wxPopupWindowHandler::OnLeftDown) | |
112 | END_EVENT_TABLE() | |
113 | ||
114 | BEGIN_EVENT_TABLE(wxPopupFocusHandler, wxEvtHandler) | |
115 | EVT_KILL_FOCUS(wxPopupFocusHandler::OnKillFocus) | |
60178eb4 | 116 | EVT_KEY_UP(wxPopupFocusHandler::OnKeyUp) |
c02ee97f VZ |
117 | END_EVENT_TABLE() |
118 | ||
119 | // ============================================================================ | |
120 | // implementation | |
121 | // ============================================================================ | |
122 | ||
123 | // ---------------------------------------------------------------------------- | |
124 | // wxPopupWindowBase | |
125 | // ---------------------------------------------------------------------------- | |
126 | ||
799ea011 GD |
127 | wxPopupWindowBase::~wxPopupWindowBase() |
128 | { | |
129 | // this destructor is required for Darwin | |
130 | } | |
131 | ||
c02ee97f VZ |
132 | bool wxPopupWindowBase::Create(wxWindow* WXUNUSED(parent), int WXUNUSED(flags)) |
133 | { | |
134 | return TRUE; | |
135 | } | |
136 | ||
137 | void wxPopupWindowBase::Position(const wxPoint& ptOrigin, | |
138 | const wxSize& size) | |
139 | { | |
140 | wxSize sizeScreen = wxGetDisplaySize(), | |
141 | sizeSelf = GetSize(); | |
142 | ||
143 | // is there enough space to put the popup below the window (where we put it | |
144 | // by default)? | |
145 | wxCoord y = ptOrigin.y + size.y; | |
146 | if ( y + sizeSelf.y > sizeScreen.y ) | |
147 | { | |
148 | // check if there is enough space above | |
149 | if ( ptOrigin.y > sizeSelf.y ) | |
150 | { | |
151 | // do position the control above the window | |
152 | y -= size.y + sizeSelf.y; | |
153 | } | |
154 | //else: not enough space below nor above, leave below | |
155 | } | |
156 | ||
157 | // now check left/right too | |
158 | wxCoord x = ptOrigin.x + size.x; | |
159 | if ( x + sizeSelf.x > sizeScreen.x ) | |
160 | { | |
161 | // check if there is enough space to the left | |
162 | if ( ptOrigin.x > sizeSelf.x ) | |
163 | { | |
164 | // do position the control to the left | |
165 | x -= size.x + sizeSelf.x; | |
166 | } | |
167 | //else: not enough space there neither, leave in default position | |
168 | } | |
169 | ||
170 | Move(x, y, wxSIZE_NO_ADJUSTMENTS); | |
171 | } | |
172 | ||
173 | // ---------------------------------------------------------------------------- | |
174 | // wxPopupTransientWindow | |
175 | // ---------------------------------------------------------------------------- | |
176 | ||
177 | void wxPopupTransientWindow::Init() | |
178 | { | |
179 | m_child = | |
180 | m_focus = (wxWindow *)NULL; | |
181 | } | |
182 | ||
758bce95 | 183 | wxPopupTransientWindow::wxPopupTransientWindow(wxWindow *parent, int style) |
c02ee97f VZ |
184 | { |
185 | Init(); | |
186 | ||
758bce95 | 187 | (void)Create(parent, style); |
c02ee97f VZ |
188 | } |
189 | ||
190 | wxPopupTransientWindow::~wxPopupTransientWindow() | |
191 | { | |
192 | PopHandlers(); | |
193 | } | |
194 | ||
195 | void wxPopupTransientWindow::PopHandlers() | |
196 | { | |
197 | if ( m_child ) | |
198 | { | |
199 | m_child->PopEventHandler(TRUE /* delete it */); | |
200 | m_child->ReleaseMouse(); | |
201 | m_child = NULL; | |
202 | } | |
203 | ||
204 | if ( m_focus ) | |
205 | { | |
206 | m_focus->PopEventHandler(TRUE /* delete it */); | |
207 | m_focus = NULL; | |
208 | } | |
209 | } | |
210 | ||
211 | void wxPopupTransientWindow::Popup(wxWindow *winFocus) | |
212 | { | |
213 | const wxWindowList& children = GetChildren(); | |
214 | if ( children.GetCount() ) | |
215 | { | |
216 | m_child = children.GetFirst()->GetData(); | |
217 | } | |
218 | else | |
219 | { | |
220 | m_child = this; | |
221 | } | |
222 | ||
516cdd54 VZ |
223 | // we can't capture mouse before the window is shown in wxGTK, so do it |
224 | // first | |
e4606ed9 | 225 | Show(); |
e4606ed9 | 226 | |
c02ee97f VZ |
227 | m_child->CaptureMouse(); |
228 | m_child->PushEventHandler(new wxPopupWindowHandler(this)); | |
229 | ||
c02ee97f | 230 | m_focus = winFocus ? winFocus : this; |
c02ee97f | 231 | m_focus->SetFocus(); |
60178eb4 | 232 | |
516cdd54 | 233 | #ifdef __WXMSW__ |
60178eb4 VZ |
234 | // FIXME: I don't know why does this happen but sometimes SetFocus() simply |
235 | // refuses to work under MSW - no error happens but the focus is not | |
236 | // given to the window, i.e. the assert below is triggered | |
237 | // | |
238 | // Try work around this as we can... | |
60178eb4 | 239 | |
516cdd54 VZ |
240 | //wxASSERT_MSG( FindFocus() == m_focus, _T("setting focus failed") ); |
241 | m_focus = FindFocus(); | |
60178eb4 | 242 | if ( m_focus ) |
516cdd54 | 243 | #endif // __WXMSW__ |
60178eb4 VZ |
244 | { |
245 | m_focus->PushEventHandler(new wxPopupFocusHandler(this)); | |
246 | } | |
c02ee97f VZ |
247 | } |
248 | ||
249 | void wxPopupTransientWindow::Dismiss() | |
250 | { | |
251 | PopHandlers(); | |
252 | ||
253 | Hide(); | |
254 | } | |
255 | ||
256 | void wxPopupTransientWindow::DismissAndNotify() | |
257 | { | |
258 | Dismiss(); | |
259 | ||
260 | OnDismiss(); | |
261 | } | |
262 | ||
263 | void wxPopupTransientWindow::OnDismiss() | |
264 | { | |
265 | // nothing to do here - but it may be interesting for derived class | |
266 | } | |
267 | ||
268 | bool wxPopupTransientWindow::ProcessLeftDown(wxMouseEvent& WXUNUSED(event)) | |
269 | { | |
270 | // no special processing here | |
271 | return FALSE; | |
272 | } | |
273 | ||
a8132cd4 | 274 | #if wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
275 | |
276 | // ---------------------------------------------------------------------------- | |
277 | // wxPopupComboWindow | |
278 | // ---------------------------------------------------------------------------- | |
279 | ||
280 | wxPopupComboWindow::wxPopupComboWindow(wxComboControl *parent) | |
281 | : wxPopupTransientWindow(parent) | |
282 | { | |
283 | m_combo = parent; | |
284 | } | |
285 | ||
286 | bool wxPopupComboWindow::Create(wxComboControl *parent) | |
287 | { | |
288 | m_combo = parent; | |
289 | ||
290 | return wxPopupWindow::Create(parent); | |
291 | } | |
292 | ||
293 | void wxPopupComboWindow::PositionNearCombo() | |
294 | { | |
295 | // the origin point must be in screen coords | |
296 | wxPoint ptOrigin = m_combo->ClientToScreen(wxPoint(0, 0)); | |
297 | ||
89e7a223 | 298 | #if 0 //def __WXUNIVERSAL__ |
c02ee97f VZ |
299 | // account for the fact that (0, 0) is not the top left corner of the |
300 | // window: there is also the border | |
301 | wxRect rectBorders = m_combo->GetRenderer()-> | |
302 | GetBorderDimensions(m_combo->GetBorder()); | |
303 | ptOrigin.x -= rectBorders.x; | |
304 | ptOrigin.y -= rectBorders.y; | |
305 | #endif // __WXUNIVERSAL__ | |
306 | ||
307 | // position below or above the combobox: the width is 0 to put it exactly | |
308 | // below us, not to the left or to the right | |
309 | Position(ptOrigin, wxSize(0, m_combo->GetSize().y)); | |
310 | } | |
311 | ||
312 | void wxPopupComboWindow::OnDismiss() | |
313 | { | |
314 | m_combo->OnDismiss(); | |
315 | } | |
316 | ||
a8132cd4 | 317 | #endif // wxUSE_COMBOBOX && defined(__WXUNIVERSAL__) |
c02ee97f VZ |
318 | |
319 | // ---------------------------------------------------------------------------- | |
320 | // wxPopupWindowHandler | |
321 | // ---------------------------------------------------------------------------- | |
322 | ||
323 | void wxPopupWindowHandler::OnLeftDown(wxMouseEvent& event) | |
324 | { | |
325 | // let the window have it first (we're the first event handler in the chain | |
326 | // of handlers for this window) | |
327 | if ( m_popup->ProcessLeftDown(event) ) | |
328 | { | |
329 | return; | |
330 | } | |
331 | ||
332 | wxPoint pos = event.GetPosition(); | |
333 | ||
334 | // scrollbar on which the click occured | |
335 | wxWindow *sbar = NULL; | |
336 | ||
337 | wxWindow *win = (wxWindow *)event.GetEventObject(); | |
338 | switch ( win->HitTest(pos.x, pos.y) ) | |
339 | { | |
340 | case wxHT_WINDOW_OUTSIDE: | |
341 | // clicking outside a popup dismisses it | |
342 | m_popup->DismissAndNotify(); | |
343 | break; | |
344 | ||
a8132cd4 | 345 | #ifdef __WXUNIVERSAL__ |
c02ee97f VZ |
346 | case wxHT_WINDOW_HORZ_SCROLLBAR: |
347 | sbar = win->GetScrollbar(wxHORIZONTAL); | |
348 | break; | |
349 | ||
350 | case wxHT_WINDOW_VERT_SCROLLBAR: | |
351 | sbar = win->GetScrollbar(wxVERTICAL); | |
352 | break; | |
a8132cd4 | 353 | #endif |
c02ee97f VZ |
354 | |
355 | default: | |
356 | // forgot to update the switch after adding a new hit test code? | |
357 | wxFAIL_MSG( _T("unexpected HitTest() return value") ); | |
358 | // fall through | |
359 | ||
360 | case wxHT_WINDOW_CORNER: | |
361 | // don't actually know if this one is good for anything, but let it | |
362 | // pass just in case | |
363 | ||
364 | case wxHT_WINDOW_INSIDE: | |
365 | // let the normal processing take place | |
366 | event.Skip(); | |
367 | break; | |
368 | } | |
369 | ||
370 | if ( sbar ) | |
371 | { | |
372 | // translate the event coordinates to the scrollbar ones | |
373 | pos = sbar->ScreenToClient(win->ClientToScreen(pos)); | |
374 | ||
375 | // and give the event to it | |
376 | wxMouseEvent event2 = event; | |
377 | event2.m_x = pos.x; | |
378 | event2.m_y = pos.y; | |
379 | ||
380 | (void)sbar->GetEventHandler()->ProcessEvent(event2); | |
381 | } | |
382 | } | |
383 | ||
384 | // ---------------------------------------------------------------------------- | |
385 | // wxPopupFocusHandler | |
386 | // ---------------------------------------------------------------------------- | |
387 | ||
388 | void wxPopupFocusHandler::OnKillFocus(wxFocusEvent& event) | |
389 | { | |
516cdd54 VZ |
390 | #ifdef __WXGTK__ |
391 | // ignore the next OnKillFocus() call | |
392 | if ( time(NULL) < m_creationTime + 1 ) | |
393 | { | |
394 | event.Skip(); | |
395 | ||
396 | return; | |
397 | } | |
398 | #endif // __WXGTK__ | |
399 | ||
60178eb4 VZ |
400 | // when we lose focus we always disappear - unless it goes to the popup (in |
401 | // which case we don't really lose it) | |
402 | if ( event.GetWindow() != m_popup ) | |
403 | m_popup->DismissAndNotify(); | |
404 | } | |
54800df8 | 405 | |
60178eb4 VZ |
406 | void wxPopupFocusHandler::OnKeyUp(wxKeyEvent& event) |
407 | { | |
408 | // let the window have it first, it might process the keys | |
409 | if ( !m_popup->ProcessEvent(event) ) | |
410 | { | |
411 | // by default, dismiss the popup | |
54800df8 | 412 | m_popup->DismissAndNotify(); |
60178eb4 | 413 | } |
c02ee97f VZ |
414 | } |
415 | ||
416 | #endif // wxUSE_POPUPWIN | |
eb729cd3 | 417 |