]>
Commit | Line | Data |
---|---|---|
a340b80d | 1 | ///////////////////////////////////////////////////////////////////////////// |
b61f4f77 | 2 | // Name: src/common/combocmn.cpp |
a57d600f | 3 | // Purpose: wxComboCtrlBase |
a340b80d VZ |
4 | // Author: Jaakko Salli |
5 | // Modified by: | |
6 | // Created: Apr-30-2006 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2005 Jaakko Salli | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
a57d600f | 26 | #if wxUSE_COMBOCTRL |
a340b80d VZ |
27 | |
28 | #ifndef WX_PRECOMP | |
a340b80d VZ |
29 | #include "wx/log.h" |
30 | #include "wx/combobox.h" | |
31 | #include "wx/dcclient.h" | |
32 | #include "wx/settings.h" | |
33 | #include "wx/dialog.h" | |
c0badb70 | 34 | #include "wx/timer.h" |
a340b80d VZ |
35 | #endif |
36 | ||
37 | #include "wx/dcbuffer.h" | |
38 | #include "wx/tooltip.h" | |
a340b80d VZ |
39 | |
40 | #include "wx/combo.h" | |
41 | ||
42 | ||
43 | ||
44 | // constants | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | // Milliseconds to wait for two mouse-ups after focus inorder | |
48 | // to trigger a double-click. | |
49 | #define DOUBLE_CLICK_CONVERSION_TRESHOLD 500 | |
50 | ||
51 | #define DEFAULT_DROPBUTTON_WIDTH 19 | |
52 | ||
53 | #define BMP_BUTTON_MARGIN 4 | |
54 | ||
55 | #define DEFAULT_POPUP_HEIGHT 200 | |
56 | ||
57 | #define DEFAULT_TEXT_INDENT 3 | |
58 | ||
59 | #define COMBO_MARGIN 2 // spacing right of wxTextCtrl | |
60 | ||
61 | ||
62 | #if defined(__WXMSW__) | |
63 | ||
64 | #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
65 | ||
66 | //#undef wxUSE_POPUPWIN | |
67 | //#define wxUSE_POPUPWIN 0 | |
68 | ||
69 | #elif defined(__WXGTK__) | |
70 | ||
71 | #define USE_TRANSIENT_POPUP 1 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
72 | ||
73 | #elif defined(__WXMAC__) | |
74 | ||
75 | #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
76 | ||
77 | #else | |
78 | ||
79 | #define USE_TRANSIENT_POPUP 0 // Use wxPopupWindowTransient (preferred, if it works properly on platform) | |
80 | ||
81 | #endif | |
82 | ||
83 | ||
84 | // Popupwin is really only supported on wxMSW (not WINCE) and wxGTK, regardless | |
85 | // what the wxUSE_POPUPWIN says. | |
86 | // FIXME: Why isn't wxUSE_POPUPWIN reliable any longer? (it was in wxW2.6.2) | |
87 | #if (!defined(__WXMSW__) && !defined(__WXGTK__)) || defined(__WXWINCE__) | |
88 | #undef wxUSE_POPUPWIN | |
89 | #define wxUSE_POPUPWIN 0 | |
90 | #endif | |
91 | ||
92 | ||
93 | #if wxUSE_POPUPWIN | |
94 | #include "wx/popupwin.h" | |
95 | #else | |
96 | #undef USE_TRANSIENT_POPUP | |
97 | #define USE_TRANSIENT_POPUP 0 | |
98 | #endif | |
99 | ||
100 | ||
101 | #if USE_TRANSIENT_POPUP | |
102 | ||
103 | #define wxComboPopupWindowBase wxPopupTransientWindow | |
104 | #define INSTALL_TOPLEV_HANDLER 0 | |
105 | ||
106 | #elif wxUSE_POPUPWIN | |
107 | ||
108 | #define wxComboPopupWindowBase wxPopupWindow | |
109 | #define INSTALL_TOPLEV_HANDLER 1 | |
110 | ||
111 | #else | |
112 | ||
113 | #define wxComboPopupWindowBase wxDialog | |
114 | #define INSTALL_TOPLEV_HANDLER 0 // Doesn't need since can monitor active event | |
115 | ||
116 | #endif | |
117 | ||
118 | ||
119 | ||
120 | // | |
121 | // ** TODO ** | |
122 | // * wxComboPopupWindow for external use (ie. replace old wxUniv wxPopupComboWindow) | |
123 | // | |
124 | ||
125 | ||
126 | // ---------------------------------------------------------------------------- | |
127 | // wxComboFrameEventHandler takes care of hiding the popup when events happen | |
128 | // in its top level parent. | |
129 | // ---------------------------------------------------------------------------- | |
130 | ||
131 | #if INSTALL_TOPLEV_HANDLER | |
132 | ||
133 | // | |
134 | // This will no longer be necessary after wxTransientPopupWindow | |
135 | // works well on all platforms. | |
136 | // | |
137 | ||
138 | class wxComboFrameEventHandler : public wxEvtHandler | |
139 | { | |
140 | public: | |
a57d600f | 141 | wxComboFrameEventHandler( wxComboCtrlBase* pCb ); |
a340b80d VZ |
142 | ~wxComboFrameEventHandler(); |
143 | ||
144 | void OnPopup(); | |
145 | ||
146 | void OnIdle( wxIdleEvent& event ); | |
147 | void OnMouseEvent( wxMouseEvent& event ); | |
148 | void OnActivate( wxActivateEvent& event ); | |
149 | void OnResize( wxSizeEvent& event ); | |
150 | void OnMove( wxMoveEvent& event ); | |
151 | void OnMenuEvent( wxMenuEvent& event ); | |
152 | void OnClose( wxCloseEvent& event ); | |
153 | ||
154 | protected: | |
155 | wxWindow* m_focusStart; | |
a57d600f | 156 | wxComboCtrlBase* m_combo; |
a340b80d VZ |
157 | |
158 | private: | |
159 | DECLARE_EVENT_TABLE() | |
160 | }; | |
161 | ||
162 | BEGIN_EVENT_TABLE(wxComboFrameEventHandler, wxEvtHandler) | |
163 | EVT_IDLE(wxComboFrameEventHandler::OnIdle) | |
164 | EVT_LEFT_DOWN(wxComboFrameEventHandler::OnMouseEvent) | |
165 | EVT_RIGHT_DOWN(wxComboFrameEventHandler::OnMouseEvent) | |
166 | EVT_SIZE(wxComboFrameEventHandler::OnResize) | |
167 | EVT_MOVE(wxComboFrameEventHandler::OnMove) | |
168 | EVT_MENU_HIGHLIGHT(wxID_ANY,wxComboFrameEventHandler::OnMenuEvent) | |
169 | EVT_MENU_OPEN(wxComboFrameEventHandler::OnMenuEvent) | |
170 | EVT_ACTIVATE(wxComboFrameEventHandler::OnActivate) | |
171 | EVT_CLOSE(wxComboFrameEventHandler::OnClose) | |
172 | END_EVENT_TABLE() | |
173 | ||
a57d600f | 174 | wxComboFrameEventHandler::wxComboFrameEventHandler( wxComboCtrlBase* combo ) |
a340b80d VZ |
175 | : wxEvtHandler() |
176 | { | |
177 | m_combo = combo; | |
178 | } | |
179 | ||
180 | wxComboFrameEventHandler::~wxComboFrameEventHandler() | |
181 | { | |
182 | } | |
183 | ||
184 | void wxComboFrameEventHandler::OnPopup() | |
185 | { | |
186 | m_focusStart = ::wxWindow::FindFocus(); | |
187 | } | |
188 | ||
189 | void wxComboFrameEventHandler::OnIdle( wxIdleEvent& event ) | |
190 | { | |
191 | wxWindow* winFocused = ::wxWindow::FindFocus(); | |
192 | ||
193 | wxWindow* popup = m_combo->GetPopupControl(); | |
194 | wxWindow* winpopup = m_combo->GetPopupWindow(); | |
195 | ||
196 | if ( | |
197 | winFocused != m_focusStart && | |
198 | winFocused != popup && | |
199 | winFocused->GetParent() != popup && | |
200 | winFocused != winpopup && | |
201 | winFocused->GetParent() != winpopup && | |
202 | winFocused != m_combo && | |
203 | winFocused != m_combo->GetButton() // GTK (atleast) requires this | |
204 | ) | |
205 | { | |
206 | m_combo->HidePopup(); | |
207 | } | |
208 | ||
209 | event.Skip(); | |
210 | } | |
211 | ||
212 | void wxComboFrameEventHandler::OnMenuEvent( wxMenuEvent& event ) | |
213 | { | |
214 | m_combo->HidePopup(); | |
215 | event.Skip(); | |
216 | } | |
217 | ||
218 | void wxComboFrameEventHandler::OnMouseEvent( wxMouseEvent& event ) | |
219 | { | |
220 | m_combo->HidePopup(); | |
221 | event.Skip(); | |
222 | } | |
223 | ||
224 | void wxComboFrameEventHandler::OnClose( wxCloseEvent& event ) | |
225 | { | |
226 | m_combo->HidePopup(); | |
227 | event.Skip(); | |
228 | } | |
229 | ||
230 | void wxComboFrameEventHandler::OnActivate( wxActivateEvent& event ) | |
231 | { | |
232 | m_combo->HidePopup(); | |
233 | event.Skip(); | |
234 | } | |
235 | ||
236 | void wxComboFrameEventHandler::OnResize( wxSizeEvent& event ) | |
237 | { | |
238 | m_combo->HidePopup(); | |
239 | event.Skip(); | |
240 | } | |
241 | ||
242 | void wxComboFrameEventHandler::OnMove( wxMoveEvent& event ) | |
243 | { | |
244 | m_combo->HidePopup(); | |
245 | event.Skip(); | |
246 | } | |
247 | ||
248 | #endif // INSTALL_TOPLEV_HANDLER | |
249 | ||
250 | // ---------------------------------------------------------------------------- | |
251 | // wxComboPopupWindow is wxPopupWindow customized for | |
a57d600f | 252 | // wxComboCtrl. |
a340b80d VZ |
253 | // ---------------------------------------------------------------------------- |
254 | ||
255 | class wxComboPopupWindow : public wxComboPopupWindowBase | |
256 | { | |
257 | public: | |
258 | ||
a57d600f | 259 | wxComboPopupWindow( wxComboCtrlBase *parent, int style = wxBORDER_NONE ); |
a340b80d VZ |
260 | |
261 | #if USE_TRANSIENT_POPUP | |
262 | virtual bool ProcessLeftDown(wxMouseEvent& event); | |
263 | #endif | |
264 | ||
265 | void OnKeyEvent(wxKeyEvent& event); | |
266 | ||
267 | void OnMouseEvent( wxMouseEvent& event ); | |
268 | #if !wxUSE_POPUPWIN | |
269 | void OnActivate( wxActivateEvent& event ); | |
270 | #endif | |
271 | ||
272 | protected: | |
273 | ||
274 | #if USE_TRANSIENT_POPUP | |
275 | virtual void OnDismiss(); | |
276 | #endif | |
277 | ||
278 | private: | |
279 | DECLARE_EVENT_TABLE() | |
280 | }; | |
281 | ||
282 | ||
283 | BEGIN_EVENT_TABLE(wxComboPopupWindow, wxComboPopupWindowBase) | |
284 | EVT_MOUSE_EVENTS(wxComboPopupWindow::OnMouseEvent) | |
285 | #if !wxUSE_POPUPWIN | |
286 | EVT_ACTIVATE(wxComboPopupWindow::OnActivate) | |
287 | #endif | |
288 | EVT_KEY_DOWN(wxComboPopupWindow::OnKeyEvent) | |
289 | EVT_KEY_UP(wxComboPopupWindow::OnKeyEvent) | |
290 | END_EVENT_TABLE() | |
291 | ||
292 | ||
a57d600f | 293 | wxComboPopupWindow::wxComboPopupWindow( wxComboCtrlBase *parent, |
a340b80d VZ |
294 | int style ) |
295 | #if wxUSE_POPUPWIN | |
296 | : wxComboPopupWindowBase(parent,style) | |
297 | #else | |
298 | : wxComboPopupWindowBase(parent, | |
299 | wxID_ANY, | |
300 | wxEmptyString, | |
301 | wxPoint(-21,-21), | |
302 | wxSize(20,20), | |
303 | style) | |
304 | #endif | |
305 | { | |
306 | } | |
307 | ||
308 | void wxComboPopupWindow::OnKeyEvent( wxKeyEvent& event ) | |
309 | { | |
310 | // Relay keyboard event to the main child controls | |
311 | // (just skipping may just cause the popup to close) | |
312 | wxWindowList children = GetChildren(); | |
313 | wxWindowList::iterator node = children.begin(); | |
314 | wxWindow* child = (wxWindow*)*node; | |
315 | child->AddPendingEvent(event); | |
316 | } | |
317 | ||
318 | void wxComboPopupWindow::OnMouseEvent( wxMouseEvent& event ) | |
319 | { | |
320 | event.Skip(); | |
321 | } | |
322 | ||
323 | #if !wxUSE_POPUPWIN | |
324 | void wxComboPopupWindow::OnActivate( wxActivateEvent& event ) | |
325 | { | |
326 | if ( !event.GetActive() ) | |
327 | { | |
328 | // Tell combo control that we are dismissed. | |
a57d600f | 329 | wxComboCtrl* combo = (wxComboCtrl*) GetParent(); |
a340b80d | 330 | wxASSERT( combo ); |
a57d600f | 331 | wxASSERT( combo->IsKindOf(CLASSINFO(wxComboCtrl)) ); |
a340b80d VZ |
332 | |
333 | combo->HidePopup(); | |
334 | ||
335 | event.Skip(); | |
336 | } | |
337 | } | |
338 | #endif | |
339 | ||
340 | #if USE_TRANSIENT_POPUP | |
341 | bool wxComboPopupWindow::ProcessLeftDown(wxMouseEvent& event ) | |
342 | { | |
343 | return wxComboPopupWindowBase::ProcessLeftDown(event); | |
344 | } | |
345 | #endif | |
346 | ||
347 | #if USE_TRANSIENT_POPUP | |
348 | // First thing that happens when a transient popup closes is that this method gets called. | |
349 | void wxComboPopupWindow::OnDismiss() | |
350 | { | |
a57d600f VZ |
351 | wxComboCtrlBase* combo = (wxComboCtrlBase*) GetParent(); |
352 | wxASSERT_MSG( combo->IsKindOf(CLASSINFO(wxComboCtrlBase)), | |
353 | wxT("parent might not be wxComboCtrl, but check IMPLEMENT_DYNAMIC_CLASS(2) macro for correctness") ); | |
a340b80d VZ |
354 | |
355 | combo->OnPopupDismiss(); | |
356 | } | |
357 | #endif | |
358 | ||
359 | // ---------------------------------------------------------------------------- | |
360 | // wxComboPopup | |
361 | // | |
362 | // ---------------------------------------------------------------------------- | |
363 | ||
364 | wxComboPopup::~wxComboPopup() | |
365 | { | |
366 | } | |
367 | ||
368 | void wxComboPopup::OnPopup() | |
369 | { | |
370 | } | |
371 | ||
372 | void wxComboPopup::OnDismiss() | |
373 | { | |
374 | } | |
375 | ||
376 | wxSize wxComboPopup::GetAdjustedSize( int minWidth, | |
377 | int prefHeight, | |
378 | int WXUNUSED(maxHeight) ) | |
379 | { | |
380 | return wxSize(minWidth,prefHeight); | |
381 | } | |
382 | ||
a57d600f | 383 | void wxComboPopup::DefaultPaintComboControl( wxComboCtrlBase* combo, |
6d0ce565 | 384 | wxDC& dc, const wxRect& rect ) |
a340b80d | 385 | { |
6d0ce565 | 386 | if ( combo->GetWindowStyle() & wxCB_READONLY ) // ie. no textctrl |
a340b80d | 387 | { |
6d0ce565 | 388 | combo->DrawFocusBackground(dc,rect,0); |
a340b80d | 389 | |
6d0ce565 VZ |
390 | dc.DrawText( combo->GetValue(), |
391 | rect.x + combo->GetTextIndent(), | |
392 | (rect.height-dc.GetCharHeight())/2 + rect.y ); | |
a340b80d VZ |
393 | } |
394 | } | |
395 | ||
6d0ce565 VZ |
396 | void wxComboPopup::PaintComboControl( wxDC& dc, const wxRect& rect ) |
397 | { | |
398 | DefaultPaintComboControl(m_combo,dc,rect); | |
399 | } | |
400 | ||
a340b80d VZ |
401 | void wxComboPopup::OnComboKeyEvent( wxKeyEvent& event ) |
402 | { | |
403 | event.Skip(); | |
404 | } | |
405 | ||
406 | void wxComboPopup::OnComboDoubleClick() | |
407 | { | |
408 | } | |
409 | ||
410 | void wxComboPopup::SetStringValue( const wxString& WXUNUSED(value) ) | |
411 | { | |
412 | } | |
413 | ||
414 | bool wxComboPopup::LazyCreate() | |
415 | { | |
416 | return false; | |
417 | } | |
418 | ||
419 | void wxComboPopup::Dismiss() | |
420 | { | |
421 | m_combo->HidePopup(); | |
422 | } | |
423 | ||
424 | // ---------------------------------------------------------------------------- | |
425 | // input handling | |
426 | // ---------------------------------------------------------------------------- | |
427 | ||
428 | // | |
429 | // This is pushed to the event handler queue of either combo box | |
430 | // or its textctrl (latter if not readonly combo). | |
431 | // | |
432 | class wxComboBoxExtraInputHandler : public wxEvtHandler | |
433 | { | |
434 | public: | |
435 | ||
a57d600f | 436 | wxComboBoxExtraInputHandler( wxComboCtrlBase* combo ) |
a340b80d VZ |
437 | : wxEvtHandler() |
438 | { | |
439 | m_combo = combo; | |
440 | } | |
441 | ~wxComboBoxExtraInputHandler() { } | |
442 | void OnKey(wxKeyEvent& event); | |
443 | void OnFocus(wxFocusEvent& event); | |
444 | ||
445 | protected: | |
a57d600f | 446 | wxComboCtrlBase* m_combo; |
a340b80d VZ |
447 | |
448 | private: | |
449 | DECLARE_EVENT_TABLE() | |
450 | }; | |
451 | ||
452 | ||
453 | BEGIN_EVENT_TABLE(wxComboBoxExtraInputHandler, wxEvtHandler) | |
454 | EVT_KEY_DOWN(wxComboBoxExtraInputHandler::OnKey) | |
455 | EVT_SET_FOCUS(wxComboBoxExtraInputHandler::OnFocus) | |
456 | END_EVENT_TABLE() | |
457 | ||
458 | ||
459 | void wxComboBoxExtraInputHandler::OnKey(wxKeyEvent& event) | |
460 | { | |
461 | int keycode = event.GetKeyCode(); | |
462 | ||
463 | if ( keycode == WXK_TAB ) | |
464 | { | |
465 | wxNavigationKeyEvent evt; | |
466 | evt.SetFlags(wxNavigationKeyEvent::FromTab| | |
467 | (!event.ShiftDown()?wxNavigationKeyEvent::IsForward: | |
468 | wxNavigationKeyEvent::IsBackward)); | |
469 | evt.SetEventObject(m_combo); | |
470 | m_combo->GetParent()->GetEventHandler()->AddPendingEvent(evt); | |
471 | return; | |
472 | } | |
473 | ||
474 | if ( m_combo->IsPopupShown() ) | |
475 | { | |
476 | // pass it to the popped up control | |
6d0ce565 | 477 | m_combo->GetPopupControl()->GetControl()->AddPendingEvent(event); |
a340b80d VZ |
478 | } |
479 | else // no popup | |
480 | { | |
481 | int comboStyle = m_combo->GetWindowStyle(); | |
6d0ce565 | 482 | wxComboPopup* popupInterface = m_combo->GetPopupControl(); |
a340b80d VZ |
483 | |
484 | if ( !popupInterface ) | |
485 | { | |
486 | event.Skip(); | |
487 | return; | |
488 | } | |
489 | ||
490 | if ( (comboStyle & wxCB_READONLY) || | |
491 | ( keycode != WXK_RIGHT && keycode != WXK_LEFT ) | |
492 | ) | |
493 | { | |
494 | // Alternate keys: UP and DOWN show the popup instead of cycling | |
495 | if ( (comboStyle & wxCC_ALT_KEYS) ) | |
496 | { | |
497 | if ( keycode == WXK_UP || keycode == WXK_DOWN ) | |
498 | { | |
499 | m_combo->OnButtonClick(); | |
500 | return; | |
501 | } | |
6d0ce565 VZ |
502 | else |
503 | event.Skip(); | |
a340b80d VZ |
504 | } |
505 | else | |
506 | popupInterface->OnComboKeyEvent(event); | |
507 | } | |
508 | else | |
509 | event.Skip(); | |
510 | } | |
511 | } | |
512 | ||
a340b80d VZ |
513 | void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event) |
514 | { | |
515 | // FIXME: This code does run when control is clicked, | |
516 | // yet on Windows it doesn't select all the text. | |
517 | if ( !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) ) | |
518 | { | |
519 | if ( m_combo->GetTextCtrl() ) | |
520 | m_combo->GetTextCtrl()->SelectAll(); | |
521 | else | |
522 | m_combo->SetSelection(-1,-1); | |
523 | } | |
524 | ||
6d0ce565 VZ |
525 | if ( event.GetId() != m_combo->GetId() ) |
526 | { | |
527 | // Add textctrl set focus events as combo set focus events | |
528 | // NOTE: Simply changing the event and skipping didn't seem | |
529 | // to do the trick. | |
530 | wxFocusEvent evt2(wxEVT_SET_FOCUS,m_combo->GetId()); | |
531 | evt2.SetEventObject(m_combo); | |
532 | m_combo->GetEventHandler()->ProcessEvent(evt2); | |
533 | } | |
534 | else | |
535 | event.Skip(); | |
536 | ||
a340b80d VZ |
537 | event.Skip(); |
538 | } | |
539 | ||
540 | ||
541 | // | |
542 | // This is pushed to the event handler queue of the control in popup. | |
543 | // | |
544 | ||
545 | class wxComboPopupExtraEventHandler : public wxEvtHandler | |
546 | { | |
547 | public: | |
548 | ||
a57d600f | 549 | wxComboPopupExtraEventHandler( wxComboCtrlBase* combo ) |
a340b80d VZ |
550 | : wxEvtHandler() |
551 | { | |
552 | m_combo = combo; | |
553 | m_beenInside = false; | |
554 | } | |
555 | ~wxComboPopupExtraEventHandler() { } | |
556 | ||
557 | void OnMouseEvent( wxMouseEvent& event ); | |
558 | ||
a57d600f | 559 | // Called from wxComboCtrlBase::OnPopupDismiss |
a340b80d VZ |
560 | void OnPopupDismiss() |
561 | { | |
562 | m_beenInside = false; | |
563 | } | |
564 | ||
565 | protected: | |
a57d600f | 566 | wxComboCtrlBase* m_combo; |
a340b80d | 567 | |
6d0ce565 | 568 | bool m_beenInside; |
a340b80d VZ |
569 | |
570 | private: | |
571 | DECLARE_EVENT_TABLE() | |
572 | }; | |
573 | ||
574 | ||
575 | BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler) | |
576 | EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent) | |
577 | END_EVENT_TABLE() | |
578 | ||
579 | ||
580 | void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event ) | |
581 | { | |
582 | wxPoint pt = event.GetPosition(); | |
6d0ce565 | 583 | wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize(); |
a340b80d VZ |
584 | int evtType = event.GetEventType(); |
585 | bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y; | |
586 | ||
587 | if ( evtType == wxEVT_MOTION || | |
588 | evtType == wxEVT_LEFT_DOWN || | |
589 | evtType == wxEVT_RIGHT_DOWN ) | |
590 | { | |
591 | // Block motion and click events outside the popup | |
592 | if ( !isInside ) | |
593 | { | |
594 | event.Skip(false); | |
595 | return; | |
596 | } | |
597 | } | |
598 | else if ( evtType == wxEVT_LEFT_UP ) | |
599 | { | |
600 | // Don't let left-down events in if outside | |
601 | if ( evtType == wxEVT_LEFT_DOWN ) | |
602 | { | |
603 | if ( !isInside ) | |
604 | return; | |
605 | } | |
606 | ||
607 | if ( !m_beenInside ) | |
608 | { | |
609 | if ( isInside ) | |
610 | { | |
611 | m_beenInside = true; | |
612 | } | |
613 | else | |
614 | { | |
615 | // | |
616 | // Some mouse events to popup that happen outside it, before cursor | |
617 | // has been inside the popu, need to be ignored by it but relayed to | |
618 | // the dropbutton. | |
619 | // | |
620 | wxWindow* btn = m_combo->GetButton(); | |
621 | if ( btn ) | |
622 | btn->GetEventHandler()->AddPendingEvent(event); | |
623 | else | |
624 | m_combo->GetEventHandler()->AddPendingEvent(event); | |
625 | ||
626 | return; | |
627 | } | |
628 | ||
629 | event.Skip(); | |
630 | } | |
631 | } | |
632 | ||
633 | event.Skip(); | |
634 | } | |
635 | ||
636 | // ---------------------------------------------------------------------------- | |
a57d600f | 637 | // wxComboCtrlBase |
a340b80d VZ |
638 | // ---------------------------------------------------------------------------- |
639 | ||
640 | ||
a57d600f VZ |
641 | BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl) |
642 | EVT_TEXT(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent) | |
643 | EVT_SIZE(wxComboCtrlBase::OnSizeEvent) | |
644 | EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent) | |
645 | EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent) | |
646 | //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent) | |
647 | EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent) | |
648 | EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged) | |
a340b80d VZ |
649 | END_EVENT_TABLE() |
650 | ||
651 | ||
a57d600f | 652 | IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl) |
a340b80d VZ |
653 | |
654 | // Have global double buffer - should be enough for multiple combos | |
655 | static wxBitmap* gs_doubleBuffer = (wxBitmap*) NULL; | |
656 | ||
a57d600f | 657 | void wxComboCtrlBase::Init() |
a340b80d VZ |
658 | { |
659 | m_winPopup = (wxWindow *)NULL; | |
660 | m_popup = (wxWindow *)NULL; | |
661 | m_isPopupShown = false; | |
662 | m_btn = (wxWindow*) NULL; | |
663 | m_text = (wxTextCtrl*) NULL; | |
664 | m_popupInterface = (wxComboPopup*) NULL; | |
665 | ||
666 | m_extraEvtHandler = (wxEvtHandler*) NULL; | |
667 | m_popupExtraHandler = (wxEvtHandler*) NULL; | |
668 | m_textEvtHandler = (wxEvtHandler*) NULL; | |
669 | ||
670 | #if INSTALL_TOPLEV_HANDLER | |
671 | m_toplevEvtHandler = (wxEvtHandler*) NULL; | |
672 | #endif | |
673 | ||
674 | m_heightPopup = -1; | |
675 | m_widthMinPopup = -1; | |
676 | m_anchorSide = 0; | |
677 | m_widthCustomPaint = 0; | |
678 | m_widthCustomBorder = 0; | |
679 | ||
680 | m_btnState = 0; | |
681 | m_btnWidDefault = 0; | |
682 | m_blankButtonBg = false; | |
683 | m_btnWid = m_btnHei = 0; | |
684 | m_btnSide = wxRIGHT; | |
685 | m_btnSpacingX = 0; | |
686 | ||
687 | m_extLeft = 0; | |
688 | m_extRight = 0; | |
689 | m_absIndent = -1; | |
690 | m_iFlags = 0; | |
691 | m_downReceived = false; | |
692 | m_timeCanAcceptClick = 0; | |
693 | } | |
694 | ||
a57d600f | 695 | bool wxComboCtrlBase::Create(wxWindow *parent, |
b61f4f77 WS |
696 | wxWindowID id, |
697 | const wxString& value, | |
698 | const wxPoint& pos, | |
699 | const wxSize& size, | |
700 | long style, | |
701 | const wxValidator& validator, | |
702 | const wxString& name) | |
a340b80d VZ |
703 | { |
704 | if ( !wxControl::Create(parent, | |
705 | id, | |
706 | pos, | |
707 | size, | |
708 | style | wxWANTS_CHARS, | |
709 | validator, | |
710 | name) ) | |
711 | return false; | |
712 | ||
713 | m_valueString = value; | |
714 | ||
715 | // Get colours | |
716 | OnThemeChange(); | |
717 | m_absIndent = GetNativeTextIndent(); | |
718 | ||
719 | return true; | |
720 | } | |
721 | ||
a57d600f | 722 | void wxComboCtrlBase::InstallInputHandlers( bool alsoTextCtrl ) |
a340b80d VZ |
723 | { |
724 | if ( m_text && alsoTextCtrl ) | |
725 | { | |
726 | m_textEvtHandler = new wxComboBoxExtraInputHandler(this); | |
727 | m_text->PushEventHandler(m_textEvtHandler); | |
728 | } | |
729 | ||
730 | wxComboBoxExtraInputHandler* inputHandler = new wxComboBoxExtraInputHandler(this); | |
731 | PushEventHandler(inputHandler); | |
732 | m_extraEvtHandler = inputHandler; | |
733 | } | |
734 | ||
a57d600f | 735 | void wxComboCtrlBase::CreateTextCtrl( int extraStyle, const wxValidator& validator ) |
a340b80d VZ |
736 | { |
737 | if ( !(m_windowStyle & wxCB_READONLY) ) | |
738 | { | |
739 | m_text = new wxTextCtrl(this, | |
cf6f325c | 740 | wxID_ANY, |
a340b80d VZ |
741 | m_valueString, |
742 | wxDefaultPosition, | |
743 | wxDefaultSize, | |
744 | // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is | |
745 | // not used by the wxPropertyGrid and therefore the tab is | |
746 | // processed by looking at ancestors to see if they have | |
747 | // wxTAB_TRAVERSAL. The navigation event is then sent to | |
748 | // the wrong window. | |
749 | wxTE_PROCESS_TAB | | |
f2408396 | 750 | wxTE_PROCESS_ENTER | |
a340b80d VZ |
751 | extraStyle, |
752 | validator); | |
753 | ||
754 | // This is required for some platforms (GTK+ atleast) | |
755 | m_text->SetSizeHints(2,4); | |
756 | } | |
757 | } | |
758 | ||
a57d600f | 759 | void wxComboCtrlBase::OnThemeChange() |
a340b80d VZ |
760 | { |
761 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); | |
762 | } | |
763 | ||
a57d600f | 764 | wxComboCtrlBase::~wxComboCtrlBase() |
a340b80d VZ |
765 | { |
766 | if ( HasCapture() ) | |
767 | ReleaseMouse(); | |
768 | ||
769 | delete gs_doubleBuffer; | |
770 | gs_doubleBuffer = (wxBitmap*) NULL; | |
771 | ||
772 | #if INSTALL_TOPLEV_HANDLER | |
773 | delete ((wxComboFrameEventHandler*)m_toplevEvtHandler); | |
774 | m_toplevEvtHandler = (wxEvtHandler*) NULL; | |
775 | #endif | |
776 | ||
7ca4ac63 | 777 | DestroyPopup(); |
a340b80d VZ |
778 | |
779 | RemoveEventHandler(m_extraEvtHandler); | |
780 | ||
781 | if ( m_text ) | |
782 | m_text->RemoveEventHandler(m_textEvtHandler); | |
783 | ||
784 | delete m_textEvtHandler; | |
785 | delete m_extraEvtHandler; | |
786 | } | |
787 | ||
788 | ||
789 | // ---------------------------------------------------------------------------- | |
790 | // geometry stuff | |
791 | // ---------------------------------------------------------------------------- | |
792 | ||
793 | // Recalculates button and textctrl areas | |
a57d600f | 794 | void wxComboCtrlBase::CalculateAreas( int btnWidth ) |
a340b80d VZ |
795 | { |
796 | wxSize sz = GetClientSize(); | |
797 | int customBorder = m_widthCustomBorder; | |
a340b80d VZ |
798 | int btnBorder; // border for button only |
799 | ||
87419e97 VZ |
800 | // check if button should really be outside the border: we'll do it it if |
801 | // its platform default or bitmap+pushbutton background is used, but not if | |
802 | // there is vertical size adjustment or horizontal spacing. | |
803 | if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) || | |
804 | (m_bmpNormal.Ok() && m_blankButtonBg) ) && | |
805 | m_btnSpacingX == 0 && | |
806 | m_btnHei == 0 ) | |
a340b80d | 807 | { |
a340b80d VZ |
808 | m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE; |
809 | btnBorder = 0; | |
810 | } | |
811 | else | |
812 | { | |
a340b80d VZ |
813 | m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE); |
814 | btnBorder = customBorder; | |
815 | } | |
816 | ||
817 | // Defaul indentation | |
818 | if ( m_absIndent < 0 ) | |
819 | m_absIndent = GetNativeTextIndent(); | |
820 | ||
821 | int butWidth = btnWidth; | |
822 | ||
823 | if ( butWidth <= 0 ) | |
824 | butWidth = m_btnWidDefault; | |
825 | else | |
826 | m_btnWidDefault = butWidth; | |
827 | ||
828 | if ( butWidth <= 0 ) | |
829 | return; | |
830 | ||
831 | // Adjust button width | |
832 | if ( m_btnWid < 0 ) | |
833 | butWidth += m_btnWid; | |
834 | else if ( m_btnWid > 0 ) | |
835 | butWidth = m_btnWid; | |
836 | ||
837 | int butHeight = sz.y; | |
838 | ||
839 | butHeight -= btnBorder*2; | |
840 | ||
841 | // Adjust button height | |
842 | if ( m_btnHei < 0 ) | |
843 | butHeight += m_btnHei; | |
844 | else if ( m_btnHei > 0 ) | |
845 | butHeight = m_btnHei; | |
846 | ||
847 | // Use size of normal bitmap if... | |
848 | // It is larger | |
849 | // OR | |
850 | // button width is set to default and blank button bg is not drawn | |
851 | if ( m_bmpNormal.Ok() ) | |
852 | { | |
853 | int bmpReqWidth = m_bmpNormal.GetWidth(); | |
854 | int bmpReqHeight = m_bmpNormal.GetHeight(); | |
855 | ||
856 | // If drawing blank button background, we need to add some margin. | |
857 | if ( m_blankButtonBg ) | |
858 | { | |
859 | bmpReqWidth += BMP_BUTTON_MARGIN*2; | |
860 | bmpReqHeight += BMP_BUTTON_MARGIN*2; | |
861 | } | |
862 | ||
863 | if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) ) | |
864 | butWidth = bmpReqWidth; | |
865 | if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) ) | |
866 | butHeight = bmpReqHeight; | |
867 | ||
868 | // Need to fix height? | |
869 | if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 ) | |
870 | { | |
871 | int newY = butHeight+(customBorder*2); | |
872 | SetClientSize(-1,newY); | |
873 | sz.y = newY; | |
874 | } | |
875 | } | |
876 | ||
877 | int butAreaWid = butWidth + (m_btnSpacingX*2); | |
878 | ||
879 | m_btnSize.x = butWidth; | |
880 | m_btnSize.y = butHeight; | |
881 | ||
882 | m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder ); | |
883 | m_btnArea.y = btnBorder; | |
884 | m_btnArea.width = butAreaWid; | |
885 | m_btnArea.height = sz.y - (btnBorder*2); | |
886 | ||
887 | m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder; | |
888 | m_tcArea.y = customBorder; | |
889 | m_tcArea.width = sz.x - butAreaWid - (customBorder*2); | |
890 | m_tcArea.height = sz.y - (customBorder*2); | |
891 | ||
892 | /* | |
893 | if ( m_text ) | |
894 | { | |
895 | ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) + | |
896 | wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height)); | |
897 | } | |
898 | */ | |
899 | } | |
900 | ||
a57d600f | 901 | void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust ) |
a340b80d VZ |
902 | { |
903 | if ( !m_text ) | |
904 | return; | |
905 | ||
906 | wxSize sz = GetClientSize(); | |
907 | int customBorder = m_widthCustomBorder; | |
908 | ||
909 | if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER ) | |
910 | { | |
911 | // Centre textctrl | |
912 | int tcSizeY = m_text->GetBestSize().y; | |
913 | int diff = sz.y - tcSizeY; | |
914 | int y = textCtrlYAdjust + (diff/2); | |
915 | ||
916 | if ( y < customBorder ) | |
917 | y = customBorder; | |
918 | ||
919 | m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust, | |
920 | y, | |
921 | m_tcArea.width - COMBO_MARGIN - | |
922 | (textCtrlXAdjust + m_widthCustomPaint + m_absIndent), | |
923 | -1 ); | |
924 | ||
925 | // Make sure textctrl doesn't exceed the bottom custom border | |
926 | wxSize tsz = m_text->GetSize(); | |
927 | diff = (y + tsz.y) - (sz.y - customBorder); | |
928 | if ( diff >= 0 ) | |
929 | { | |
930 | tsz.y = tsz.y - diff - 1; | |
931 | m_text->SetSize(tsz); | |
932 | } | |
933 | } | |
934 | else | |
935 | { | |
936 | m_text->SetSize( m_tcArea.x, | |
937 | 0, | |
938 | sz.x - m_btnArea.x - m_widthCustomPaint - customBorder, | |
939 | sz.y ); | |
940 | } | |
941 | } | |
942 | ||
a57d600f | 943 | wxSize wxComboCtrlBase::DoGetBestSize() const |
a340b80d VZ |
944 | { |
945 | wxSize sizeText(150,0); | |
946 | ||
947 | if ( m_text ) | |
948 | sizeText = m_text->GetBestSize(); | |
949 | ||
950 | // TODO: Better method to calculate close-to-native control height. | |
951 | ||
952 | int fhei; | |
953 | if ( m_font.Ok() ) | |
954 | fhei = (m_font.GetPointSize()*2) + 5; | |
955 | else if ( wxNORMAL_FONT->Ok() ) | |
956 | fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5; | |
957 | else | |
958 | fhei = sizeText.y + 4; | |
959 | ||
960 | // Need to force height to accomodate bitmap? | |
961 | int btnSizeY = m_btnSize.y; | |
962 | if ( m_bmpNormal.Ok() && fhei < btnSizeY ) | |
963 | fhei = btnSizeY; | |
964 | ||
965 | // Control height doesn't depend on border | |
966 | /* | |
967 | // Add border | |
968 | int border = m_windowStyle & wxBORDER_MASK; | |
969 | if ( border == wxSIMPLE_BORDER ) | |
970 | fhei += 2; | |
971 | else if ( border == wxNO_BORDER ) | |
972 | fhei += (m_widthCustomBorder*2); | |
973 | else | |
974 | // Sunken etc. | |
975 | fhei += 4; | |
976 | */ | |
977 | ||
978 | // Final adjustments | |
979 | #ifdef __WXGTK__ | |
980 | fhei += 1; | |
981 | #endif | |
982 | ||
983 | wxSize ret(sizeText.x + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH, | |
984 | fhei); | |
985 | ||
986 | CacheBestSize(ret); | |
987 | return ret; | |
988 | } | |
989 | ||
a57d600f | 990 | void wxComboCtrlBase::DoMoveWindow(int x, int y, int width, int height) |
a340b80d VZ |
991 | { |
992 | // SetSize is called last in create, so it marks the end of creation | |
993 | m_iFlags |= wxCC_IFLAG_CREATED; | |
994 | ||
995 | wxControl::DoMoveWindow(x, y, width, height); | |
996 | } | |
997 | ||
a57d600f | 998 | void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event ) |
a340b80d VZ |
999 | { |
1000 | if ( !IsCreated() ) | |
1001 | return; | |
1002 | ||
a57d600f | 1003 | // defined by actual wxComboCtrls |
a340b80d VZ |
1004 | OnResize(); |
1005 | ||
1006 | event.Skip(); | |
1007 | } | |
1008 | ||
1009 | // ---------------------------------------------------------------------------- | |
1010 | // standard operations | |
1011 | // ---------------------------------------------------------------------------- | |
1012 | ||
a57d600f | 1013 | bool wxComboCtrlBase::Enable(bool enable) |
a340b80d VZ |
1014 | { |
1015 | if ( !wxControl::Enable(enable) ) | |
1016 | return false; | |
1017 | ||
1018 | if ( m_btn ) | |
1019 | m_btn->Enable(enable); | |
1020 | if ( m_text ) | |
1021 | m_text->Enable(enable); | |
1022 | ||
1023 | return true; | |
1024 | } | |
1025 | ||
a57d600f | 1026 | bool wxComboCtrlBase::Show(bool show) |
a340b80d VZ |
1027 | { |
1028 | if ( !wxControl::Show(show) ) | |
1029 | return false; | |
1030 | ||
1031 | if (m_btn) | |
1032 | m_btn->Show(show); | |
1033 | ||
1034 | if (m_text) | |
1035 | m_text->Show(show); | |
1036 | ||
1037 | return true; | |
1038 | } | |
1039 | ||
a57d600f | 1040 | bool wxComboCtrlBase::SetFont ( const wxFont& font ) |
a340b80d VZ |
1041 | { |
1042 | if ( !wxControl::SetFont(font) ) | |
1043 | return false; | |
1044 | ||
1045 | if (m_text) | |
1046 | m_text->SetFont(font); | |
1047 | ||
1048 | return true; | |
1049 | } | |
1050 | ||
1051 | #if wxUSE_TOOLTIPS | |
a57d600f | 1052 | void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip) |
a340b80d VZ |
1053 | { |
1054 | wxControl::DoSetToolTip(tooltip); | |
1055 | ||
1056 | // Set tool tip for button and text box | |
1057 | if ( tooltip ) | |
1058 | { | |
1059 | const wxString &tip = tooltip->GetTip(); | |
1060 | if ( m_text ) m_text->SetToolTip(tip); | |
1061 | if ( m_btn ) m_btn->SetToolTip(tip); | |
1062 | } | |
1063 | else | |
1064 | { | |
1065 | if ( m_text ) m_text->SetToolTip( (wxToolTip*) NULL ); | |
1066 | if ( m_btn ) m_btn->SetToolTip( (wxToolTip*) NULL ); | |
1067 | } | |
1068 | } | |
1069 | #endif // wxUSE_TOOLTIPS | |
1070 | ||
1071 | // ---------------------------------------------------------------------------- | |
1072 | // painting | |
1073 | // ---------------------------------------------------------------------------- | |
1074 | ||
1075 | // draw focus background on area in a way typical on platform | |
a57d600f | 1076 | void wxComboCtrlBase::DrawFocusBackground( wxDC& dc, const wxRect& rect, int flags ) |
a340b80d VZ |
1077 | { |
1078 | wxSize sz = GetClientSize(); | |
1079 | bool isEnabled; | |
1080 | bool isFocused; // also selected | |
1081 | ||
1082 | // For smaller size control (and for disabled background) use less spacing | |
1083 | int focusSpacingX; | |
1084 | int focusSpacingY; | |
1085 | ||
1086 | if ( !(flags & wxCONTROL_ISSUBMENU) ) | |
1087 | { | |
1088 | // Drawing control | |
1089 | isEnabled = IsEnabled(); | |
1090 | isFocused = ShouldDrawFocus(); | |
1091 | ||
1092 | // Windows-style: for smaller size control (and for disabled background) use less spacing | |
1093 | focusSpacingX = isEnabled ? 2 : 1; | |
1094 | focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1; | |
1095 | } | |
1096 | else | |
1097 | { | |
1098 | // Drawing a list item | |
1099 | isEnabled = true; // they are never disabled | |
1100 | isFocused = flags & wxCONTROL_SELECTED ? true : false; | |
1101 | ||
1102 | focusSpacingX = 0; | |
1103 | focusSpacingY = 0; | |
1104 | } | |
1105 | ||
1106 | // Set the background sub-rectangle for selection, disabled etc | |
1107 | wxRect selRect(rect); | |
1108 | selRect.y += focusSpacingY; | |
1109 | selRect.height -= (focusSpacingY*2); | |
1110 | selRect.x += m_widthCustomPaint + focusSpacingX; | |
1111 | selRect.width -= m_widthCustomPaint + (focusSpacingX*2); | |
1112 | ||
1113 | wxColour bgCol; | |
1114 | ||
1115 | if ( isEnabled ) | |
1116 | { | |
1117 | // If popup is hidden and this control is focused, | |
1118 | // then draw the focus-indicator (selbgcolor background etc.). | |
1119 | if ( isFocused ) | |
1120 | { | |
1121 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) ); | |
1122 | bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT); | |
1123 | } | |
1124 | else | |
1125 | { | |
1126 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) ); | |
1127 | bgCol = GetBackgroundColour(); | |
1128 | } | |
1129 | } | |
1130 | else | |
1131 | { | |
1132 | dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) ); | |
1133 | bgCol = GetBackgroundColour(); | |
1134 | } | |
1135 | ||
1136 | dc.SetBrush( bgCol ); | |
1137 | dc.SetPen( bgCol ); | |
1138 | dc.DrawRectangle( selRect ); | |
1139 | } | |
1140 | ||
a57d600f | 1141 | void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, bool paintBg ) |
a340b80d VZ |
1142 | { |
1143 | int drawState = m_btnState; | |
1144 | ||
1145 | #ifdef __WXGTK__ | |
1146 | if ( m_isPopupShown ) | |
1147 | drawState |= wxCONTROL_PRESSED; | |
1148 | #endif | |
1149 | ||
1150 | wxRect drawRect(rect.x+m_btnSpacingX, | |
1151 | rect.y+((rect.height-m_btnSize.y)/2), | |
1152 | m_btnSize.x, | |
1153 | m_btnSize.y); | |
1154 | ||
1155 | // Make sure area is not larger than the control | |
1156 | if ( drawRect.y < rect.y ) | |
1157 | drawRect.y = rect.y; | |
1158 | if ( drawRect.height > rect.height ) | |
1159 | drawRect.height = rect.height; | |
1160 | ||
1161 | bool enabled = IsEnabled(); | |
1162 | ||
1163 | if ( !enabled ) | |
1164 | drawState |= wxCONTROL_DISABLED; | |
1165 | ||
1166 | if ( !m_bmpNormal.Ok() ) | |
1167 | { | |
1168 | // Need to clear button background even if m_btn is present | |
a340b80d | 1169 | if ( paintBg ) |
b61f4f77 WS |
1170 | { |
1171 | wxColour bgCol; | |
1172 | ||
1173 | if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE ) | |
1174 | bgCol = GetParent()->GetBackgroundColour(); | |
1175 | else | |
1176 | bgCol = GetBackgroundColour(); | |
1177 | ||
1178 | dc.SetBrush(bgCol); | |
1179 | dc.SetPen(bgCol); | |
a340b80d | 1180 | dc.DrawRectangle(rect); |
b61f4f77 | 1181 | } |
a340b80d VZ |
1182 | |
1183 | // Draw standard button | |
1184 | wxRendererNative::Get().DrawComboBoxDropButton(this, | |
1185 | dc, | |
1186 | drawRect, | |
1187 | drawState); | |
1188 | } | |
1189 | else | |
1190 | { | |
1191 | // Draw bitmap | |
1192 | ||
1193 | wxBitmap* pBmp; | |
1194 | ||
1195 | if ( !enabled ) | |
1196 | pBmp = &m_bmpDisabled; | |
1197 | else if ( m_btnState & wxCONTROL_PRESSED ) | |
1198 | pBmp = &m_bmpPressed; | |
1199 | else if ( m_btnState & wxCONTROL_CURRENT ) | |
1200 | pBmp = &m_bmpHover; | |
1201 | else | |
1202 | pBmp = &m_bmpNormal; | |
1203 | ||
1204 | if ( m_blankButtonBg ) | |
1205 | { | |
1206 | // If using blank button background, we need to clear its background | |
1207 | // with button face colour instead of colour for rest of the control. | |
1208 | if ( paintBg ) | |
1209 | { | |
1210 | wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE); | |
1211 | //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); | |
1212 | dc.SetPen(bgCol); | |
1213 | dc.SetBrush(bgCol); | |
1214 | dc.DrawRectangle(rect); | |
1215 | } | |
1216 | ||
1217 | wxRendererNative::Get().DrawPushButton(this, | |
1218 | dc, | |
1219 | drawRect, | |
1220 | drawState); | |
1221 | ||
1222 | } | |
1223 | else | |
1224 | ||
1225 | { | |
1226 | // Need to clear button background even if m_btn is present | |
1227 | // (assume non-button background was cleared just before this call so brushes are good) | |
1228 | if ( paintBg ) | |
1229 | dc.DrawRectangle(rect); | |
1230 | } | |
1231 | ||
1232 | // Draw bitmap centered in drawRect | |
1233 | dc.DrawBitmap(*pBmp, | |
1234 | drawRect.x + (drawRect.width-pBmp->GetWidth())/2, | |
1235 | drawRect.y + (drawRect.height-pBmp->GetHeight())/2, | |
1236 | true); | |
1237 | } | |
1238 | } | |
1239 | ||
a57d600f | 1240 | void wxComboCtrlBase::RecalcAndRefresh() |
a340b80d VZ |
1241 | { |
1242 | if ( IsCreated() ) | |
1243 | { | |
1244 | wxSizeEvent evt(GetSize(),GetId()); | |
1245 | GetEventHandler()->ProcessEvent(evt); | |
1246 | Refresh(); | |
1247 | } | |
1248 | } | |
1249 | ||
a57d600f | 1250 | wxBitmap& wxComboCtrlBase::GetBufferBitmap( const wxSize& sz ) const |
a340b80d VZ |
1251 | { |
1252 | // If size is larger, recalculate double buffer bitmap | |
1253 | if ( !gs_doubleBuffer || | |
1254 | sz.x > gs_doubleBuffer->GetWidth() || | |
1255 | sz.y > gs_doubleBuffer->GetHeight() ) | |
1256 | { | |
1257 | delete gs_doubleBuffer; | |
1258 | gs_doubleBuffer = new wxBitmap(sz.x+25,sz.y); | |
1259 | } | |
1260 | return *gs_doubleBuffer; | |
1261 | } | |
1262 | ||
a340b80d VZ |
1263 | // ---------------------------------------------------------------------------- |
1264 | // miscellaneous event handlers | |
1265 | // ---------------------------------------------------------------------------- | |
1266 | ||
a57d600f | 1267 | void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event) |
a340b80d VZ |
1268 | { |
1269 | // Change event id and relay it forward | |
1270 | event.SetId(GetId()); | |
1271 | event.Skip(); | |
1272 | } | |
1273 | ||
1274 | // call if cursor is on button area or mouse is captured for the button | |
a57d600f | 1275 | bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event, |
a340b80d VZ |
1276 | int flags ) |
1277 | { | |
1278 | int type = event.GetEventType(); | |
1279 | ||
1280 | if ( type == wxEVT_MOTION ) | |
1281 | { | |
1282 | if ( flags & wxCC_MF_ON_BUTTON ) | |
1283 | { | |
1284 | if ( !(m_btnState & wxCONTROL_CURRENT) ) | |
1285 | { | |
1286 | // Mouse hover begins | |
1287 | m_btnState |= wxCONTROL_CURRENT; | |
1288 | if ( HasCapture() ) // Retain pressed state. | |
1289 | m_btnState |= wxCONTROL_PRESSED; | |
1290 | Refresh(); | |
1291 | } | |
1292 | } | |
1293 | else if ( (m_btnState & wxCONTROL_CURRENT) ) | |
1294 | { | |
1295 | // Mouse hover ends | |
1296 | m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED); | |
1297 | Refresh(); | |
1298 | } | |
1299 | } | |
1300 | else if ( type == wxEVT_LEFT_DOWN ) | |
1301 | { | |
1302 | // Only accept event if it wasn't right after popup dismiss | |
1303 | //if ( ::wxGetLocalTimeMillis() > m_timeCanClick ) | |
1304 | { | |
1305 | // Need to test this, because it might be outside. | |
1306 | if ( flags & wxCC_MF_ON_BUTTON ) | |
1307 | { | |
1308 | m_btnState |= wxCONTROL_PRESSED; | |
1309 | Refresh(); | |
1310 | ||
1311 | if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) ) | |
1312 | OnButtonClick(); | |
1313 | else | |
1314 | // If showing popup now, do not capture mouse or there will be interference | |
1315 | CaptureMouse(); | |
1316 | } | |
1317 | } | |
1318 | /*else | |
1319 | { | |
1320 | m_btnState = 0; | |
1321 | }*/ | |
1322 | } | |
1323 | else if ( type == wxEVT_LEFT_UP ) | |
1324 | { | |
1325 | ||
1326 | // Only accept event if mouse was left-press was previously accepted | |
1327 | if ( HasCapture() ) | |
1328 | ReleaseMouse(); | |
1329 | ||
1330 | if ( m_btnState & wxCONTROL_PRESSED ) | |
1331 | { | |
1332 | // If mouse was inside, fire the click event. | |
1333 | if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP ) | |
1334 | { | |
1335 | if ( flags & wxCC_MF_ON_BUTTON ) | |
1336 | OnButtonClick(); | |
1337 | } | |
1338 | ||
1339 | m_btnState &= ~(wxCONTROL_PRESSED); | |
1340 | Refresh(); | |
1341 | } | |
1342 | } | |
1343 | else if ( type == wxEVT_LEAVE_WINDOW ) | |
1344 | { | |
1345 | if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) ) | |
1346 | { | |
1347 | m_btnState &= ~(wxCONTROL_CURRENT); | |
1348 | ||
1349 | // Mouse hover ends | |
1350 | if ( !m_isPopupShown ) | |
1351 | { | |
1352 | m_btnState &= ~(wxCONTROL_PRESSED); | |
1353 | Refresh(); | |
1354 | } | |
1355 | } | |
1356 | } | |
1357 | else | |
1358 | return false; | |
1359 | ||
1360 | return true; | |
1361 | } | |
1362 | ||
1363 | // Conversion to double-clicks and some basic filtering | |
1364 | // returns true if event was consumed or filtered | |
a57d600f VZ |
1365 | //bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event, bool isOnButtonArea ) |
1366 | bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event, | |
a340b80d VZ |
1367 | int flags ) |
1368 | { | |
1369 | wxLongLong t = ::wxGetLocalTimeMillis(); | |
1370 | int evtType = event.GetEventType(); | |
1371 | ||
1372 | // | |
1373 | // Generate our own double-clicks | |
1374 | // (to allow on-focus dc-event on double-clicks instead of triple-clicks) | |
1375 | if ( (m_windowStyle & wxCC_SPECIAL_DCLICK) && | |
1376 | !m_isPopupShown && | |
1377 | //!(handlerFlags & wxCC_MF_ON_BUTTON) ) | |
1378 | !(flags & wxCC_MF_ON_BUTTON) ) | |
1379 | { | |
1380 | if ( evtType == wxEVT_LEFT_DOWN ) | |
1381 | { | |
1382 | // Set value to avoid up-events without corresponding downs | |
1383 | m_downReceived = true; | |
1384 | } | |
1385 | else if ( evtType == wxEVT_LEFT_DCLICK ) | |
1386 | { | |
1387 | // We'll make our own double-clicks | |
1388 | //evtType = 0; | |
1389 | event.SetEventType(0); | |
1390 | return true; | |
1391 | } | |
1392 | else if ( evtType == wxEVT_LEFT_UP ) | |
1393 | { | |
1394 | if ( m_downReceived || m_timeLastMouseUp == 1 ) | |
1395 | { | |
1396 | wxLongLong timeFromLastUp = (t-m_timeLastMouseUp); | |
1397 | ||
1398 | if ( timeFromLastUp < DOUBLE_CLICK_CONVERSION_TRESHOLD ) | |
1399 | { | |
1400 | //type = wxEVT_LEFT_DCLICK; | |
1401 | event.SetEventType(wxEVT_LEFT_DCLICK); | |
1402 | m_timeLastMouseUp = 1; | |
1403 | } | |
1404 | else | |
1405 | { | |
1406 | m_timeLastMouseUp = t; | |
1407 | } | |
1408 | ||
1409 | //m_downReceived = false; | |
1410 | } | |
1411 | } | |
1412 | } | |
1413 | ||
1414 | // Filter out clicks on button immediately after popup dismiss (Windows like behaviour) | |
1415 | if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick ) | |
1416 | { | |
1417 | event.SetEventType(0); | |
1418 | return true; | |
1419 | } | |
1420 | ||
1421 | return false; | |
1422 | } | |
1423 | ||
a57d600f | 1424 | void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event ) |
a340b80d VZ |
1425 | { |
1426 | int evtType = event.GetEventType(); | |
1427 | ||
1428 | if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) && | |
1429 | (m_windowStyle & wxCB_READONLY) ) | |
1430 | { | |
1431 | if ( m_isPopupShown ) | |
1432 | { | |
1433 | #if !wxUSE_POPUPWIN | |
1434 | // Normally do nothing - evt handler should close it for us | |
1435 | #elif !USE_TRANSIENT_POPUP | |
1436 | // Click here always hides the popup. | |
1437 | HidePopup(); | |
1438 | #endif | |
1439 | } | |
1440 | else | |
1441 | { | |
1442 | if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) ) | |
1443 | { | |
1444 | // In read-only mode, clicking the text is the | |
1445 | // same as clicking the button. | |
1446 | OnButtonClick(); | |
1447 | } | |
1448 | else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK ) | |
1449 | { | |
1450 | //if ( m_popupInterface->CycleValue() ) | |
1451 | // Refresh(); | |
1452 | if ( m_popupInterface ) | |
1453 | m_popupInterface->OnComboDoubleClick(); | |
1454 | } | |
1455 | } | |
1456 | } | |
1457 | else | |
1458 | if ( m_isPopupShown ) | |
1459 | { | |
1460 | // relay (some) mouse events to the popup | |
1461 | if ( evtType == wxEVT_MOUSEWHEEL ) | |
1462 | m_popup->AddPendingEvent(event); | |
1463 | } | |
1464 | else if ( evtType ) | |
1465 | event.Skip(); | |
1466 | } | |
1467 | ||
a57d600f | 1468 | void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& ) |
a340b80d VZ |
1469 | { |
1470 | // First click is the first part of double-click | |
1471 | // Some platforms don't generate down-less mouse up-event | |
1472 | // (Windows does, GTK+2 doesn't), so that's why we have | |
1473 | // to do this. | |
1474 | m_timeLastMouseUp = ::wxGetLocalTimeMillis(); | |
1475 | ||
1476 | if ( m_text ) | |
1477 | { | |
1478 | m_text->SetFocus(); | |
1479 | } | |
1480 | else | |
1481 | // no need to check for m_widthCustomPaint - that | |
1482 | // area never gets special handling when selected | |
1483 | // (in writable mode, that is) | |
1484 | Refresh(); | |
1485 | } | |
1486 | ||
a57d600f | 1487 | void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event)) |
a340b80d VZ |
1488 | { |
1489 | OnThemeChange(); | |
1490 | // indentation may also have changed | |
1491 | if ( !(m_iFlags & wxCC_IFLAG_INDENT_SET) ) | |
1492 | m_absIndent = GetNativeTextIndent(); | |
1493 | RecalcAndRefresh(); | |
1494 | } | |
1495 | ||
1496 | // ---------------------------------------------------------------------------- | |
1497 | // popup handling | |
1498 | // ---------------------------------------------------------------------------- | |
1499 | ||
1500 | // Create popup window and the child control | |
a57d600f | 1501 | void wxComboCtrlBase::CreatePopup() |
a340b80d VZ |
1502 | { |
1503 | wxComboPopup* popupInterface = m_popupInterface; | |
1504 | wxWindow* popup; | |
1505 | ||
1506 | if ( !m_winPopup ) | |
1507 | m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER ); | |
1508 | ||
1509 | popupInterface->Create(m_winPopup); | |
1510 | m_popup = popup = popupInterface->GetControl(); | |
1511 | ||
1512 | m_popupExtraHandler = new wxComboPopupExtraEventHandler(this); | |
1513 | popup->PushEventHandler( m_popupExtraHandler ); | |
1514 | ||
1515 | // This may be helpful on some platforms | |
1516 | // (eg. it bypasses a wxGTK popupwindow bug where | |
1517 | // window is not initially hidden when it should be) | |
1518 | m_winPopup->Hide(); | |
1519 | ||
1520 | popupInterface->m_iFlags |= wxCP_IFLAG_CREATED; | |
1521 | } | |
1522 | ||
7ca4ac63 WS |
1523 | // Destroy popup window and the child control |
1524 | void wxComboCtrlBase::DestroyPopup() | |
1525 | { | |
1526 | if ( m_popup ) | |
1527 | m_popup->RemoveEventHandler(m_popupExtraHandler); | |
1528 | ||
1529 | delete m_popupExtraHandler; | |
1530 | ||
1531 | HidePopup(); | |
1532 | ||
1533 | delete m_popupInterface; | |
1534 | ||
1535 | if ( m_winPopup ) | |
1536 | m_winPopup->Destroy(); | |
1537 | ||
1538 | m_popupInterface = (wxComboPopup*) NULL; | |
1539 | m_winPopup = (wxWindow*) NULL; | |
1540 | m_popup = (wxWindow*) NULL; | |
1541 | } | |
1542 | ||
a57d600f | 1543 | void wxComboCtrlBase::SetPopupControl( wxComboPopup* iface ) |
a340b80d | 1544 | { |
a57d600f | 1545 | wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") ); |
6d0ce565 | 1546 | |
7ca4ac63 | 1547 | DestroyPopup(); |
a340b80d | 1548 | |
6d0ce565 VZ |
1549 | iface->InitBase(this); |
1550 | iface->Init(); | |
1551 | ||
a340b80d VZ |
1552 | m_popupInterface = iface; |
1553 | ||
7ca4ac63 | 1554 | if ( !iface->LazyCreate() ) |
a340b80d VZ |
1555 | { |
1556 | CreatePopup(); | |
1557 | } | |
1558 | else | |
1559 | { | |
1560 | m_popup = (wxWindow*) NULL; | |
1561 | } | |
1562 | ||
6d0ce565 VZ |
1563 | // This must be done after creation |
1564 | if ( m_valueString.length() ) | |
1565 | { | |
a340b80d | 1566 | iface->SetStringValue(m_valueString); |
6d0ce565 VZ |
1567 | //Refresh(); |
1568 | } | |
1569 | } | |
a340b80d | 1570 | |
6d0ce565 | 1571 | // Ensures there is atleast the default popup |
a57d600f | 1572 | void wxComboCtrlBase::EnsurePopupControl() |
6d0ce565 VZ |
1573 | { |
1574 | if ( !m_popupInterface ) | |
1575 | SetPopupControl(NULL); | |
a340b80d VZ |
1576 | } |
1577 | ||
a57d600f | 1578 | void wxComboCtrlBase::OnButtonClick() |
a340b80d VZ |
1579 | { |
1580 | // Derived classes can override this method for totally custom | |
1581 | // popup action | |
1582 | ShowPopup(); | |
1583 | } | |
1584 | ||
a57d600f | 1585 | void wxComboCtrlBase::ShowPopup() |
a340b80d | 1586 | { |
6d0ce565 | 1587 | EnsurePopupControl(); |
a340b80d VZ |
1588 | wxCHECK_RET( !IsPopupShown(), wxT("popup window already shown") ); |
1589 | ||
1590 | SetFocus(); | |
1591 | ||
1592 | // Space above and below | |
1593 | int screenHeight; | |
1594 | wxPoint scrPos; | |
1595 | int spaceAbove; | |
1596 | int spaceBelow; | |
1597 | int maxHeightPopup; | |
1598 | wxSize ctrlSz = GetSize(); | |
1599 | ||
1600 | screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y ); | |
1601 | scrPos = GetParent()->ClientToScreen(GetPosition()); | |
1602 | ||
1603 | spaceAbove = scrPos.y; | |
1604 | spaceBelow = screenHeight - spaceAbove - ctrlSz.y; | |
1605 | ||
1606 | maxHeightPopup = spaceBelow; | |
1607 | if ( spaceAbove > spaceBelow ) | |
1608 | maxHeightPopup = spaceAbove; | |
1609 | ||
1610 | // Width | |
1611 | int widthPopup = ctrlSz.x + m_extLeft + m_extRight; | |
1612 | ||
1613 | if ( widthPopup < m_widthMinPopup ) | |
1614 | widthPopup = m_widthMinPopup; | |
1615 | ||
1616 | wxWindow* winPopup = m_winPopup; | |
1617 | wxWindow* popup; | |
1618 | ||
1619 | // Need to disable tab traversal of parent | |
1620 | // | |
1621 | // NB: This is to fix a bug in wxMSW. In theory it could also be fixed | |
1622 | // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage | |
1623 | // that if transient popup is open, then tab traversal is to be ignored. | |
1624 | // However, I think this code would still be needed for cases where | |
1625 | // transient popup doesn't work yet (wxWinCE?). | |
1626 | wxWindow* parent = GetParent(); | |
1627 | int parentFlags = parent->GetWindowStyle(); | |
1628 | if ( parentFlags & wxTAB_TRAVERSAL ) | |
1629 | { | |
1630 | parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) ); | |
1631 | m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL; | |
1632 | } | |
1633 | ||
1634 | if ( !winPopup ) | |
1635 | { | |
1636 | CreatePopup(); | |
1637 | winPopup = m_winPopup; | |
1638 | popup = m_popup; | |
1639 | } | |
1640 | else | |
1641 | { | |
1642 | popup = m_popup; | |
1643 | } | |
1644 | ||
1645 | wxASSERT( !m_popup || m_popup == popup ); // Consistency check. | |
1646 | ||
1647 | wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup, | |
1648 | m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup, | |
1649 | maxHeightPopup); | |
1650 | ||
1651 | popup->SetSize(adjustedSize); | |
1652 | popup->Move(0,0); | |
1653 | m_popupInterface->OnPopup(); | |
1654 | ||
1655 | // | |
1656 | // Reposition and resize popup window | |
1657 | // | |
1658 | ||
1659 | wxSize szp = popup->GetSize(); | |
1660 | ||
1661 | int popupX; | |
1662 | int popupY = scrPos.y + ctrlSz.y; | |
1663 | ||
b7540dc1 | 1664 | // Default anchor is wxLEFT |
a340b80d VZ |
1665 | int anchorSide = m_anchorSide; |
1666 | if ( !anchorSide ) | |
b7540dc1 | 1667 | anchorSide = wxLEFT; |
a340b80d | 1668 | |
b7540dc1 WS |
1669 | int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x; |
1670 | int leftX = scrPos.x - m_extLeft; | |
1671 | int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ); | |
1672 | ||
1673 | // If there is not enough horizontal space, anchor on the other side. | |
1674 | // If there is no space even then, place the popup at x 0. | |
1675 | if ( anchorSide == wxRIGHT ) | |
1676 | { | |
1677 | if ( rightX < 0 ) | |
1678 | { | |
1679 | if ( (leftX+szp.x) < screenWidth ) | |
1680 | anchorSide = wxLEFT; | |
1681 | else | |
1682 | anchorSide = 0; | |
1683 | } | |
1684 | } | |
1685 | else | |
1686 | { | |
1687 | if ( (leftX+szp.x) >= screenWidth ) | |
1688 | { | |
1689 | if ( rightX >= 0 ) | |
1690 | anchorSide = wxRIGHT; | |
1691 | else | |
1692 | anchorSide = 0; | |
1693 | } | |
1694 | } | |
1695 | ||
1696 | // Select x coordinate according to the anchor side | |
a340b80d | 1697 | if ( anchorSide == wxRIGHT ) |
b7540dc1 WS |
1698 | popupX = rightX; |
1699 | else if ( anchorSide == wxLEFT ) | |
1700 | popupX = leftX; | |
a340b80d | 1701 | else |
b7540dc1 | 1702 | popupX = 0; |
a340b80d VZ |
1703 | |
1704 | if ( spaceBelow < szp.y ) | |
1705 | { | |
1706 | popupY = scrPos.y - szp.y; | |
1707 | } | |
1708 | ||
1709 | // Move to position | |
1710 | //wxLogDebug(wxT("popup scheduled position1: %i,%i"),ptp.x,ptp.y); | |
1711 | //wxLogDebug(wxT("popup position1: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y); | |
1712 | ||
1713 | // Some platforms (GTK) may need these two to be separate | |
1714 | winPopup->SetSize( szp.x, szp.y ); | |
1715 | winPopup->Move( popupX, popupY ); | |
1716 | ||
1717 | //wxLogDebug(wxT("popup position2: %i,%i"),winPopup->GetPosition().x,winPopup->GetPosition().y); | |
1718 | ||
1719 | m_popup = popup; | |
1720 | ||
1721 | // Set string selection (must be this way instead of SetStringSelection) | |
1722 | if ( m_text ) | |
1723 | { | |
1724 | if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) ) | |
1725 | m_text->SelectAll(); | |
1726 | ||
1727 | m_popupInterface->SetStringValue( m_text->GetValue() ); | |
1728 | } | |
1729 | else | |
1730 | { | |
1731 | // This is neede since focus/selection indication may change when popup is shown | |
a340b80d VZ |
1732 | Refresh(); |
1733 | } | |
1734 | ||
1735 | // This must be after SetStringValue | |
1736 | m_isPopupShown = true; | |
1737 | ||
1738 | // Show it | |
1739 | #if USE_TRANSIENT_POPUP | |
1740 | ((wxPopupTransientWindow*)winPopup)->Popup(popup); | |
1741 | #else | |
1742 | winPopup->Show(); | |
1743 | #endif | |
1744 | ||
1745 | #if INSTALL_TOPLEV_HANDLER | |
1746 | // Put top level window event handler into place | |
1747 | if ( !m_toplevEvtHandler ) | |
1748 | m_toplevEvtHandler = new wxComboFrameEventHandler(this); | |
1749 | ||
1750 | wxWindow* toplev = ::wxGetTopLevelParent( this ); | |
1751 | wxASSERT( toplev ); | |
1752 | ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup(); | |
1753 | toplev->PushEventHandler( m_toplevEvtHandler ); | |
1754 | #endif | |
1755 | ||
1756 | } | |
1757 | ||
a57d600f | 1758 | void wxComboCtrlBase::OnPopupDismiss() |
a340b80d VZ |
1759 | { |
1760 | // Just in case, avoid double dismiss | |
1761 | if ( !m_isPopupShown ) | |
1762 | return; | |
1763 | ||
1764 | // *Must* set this before focus etc. | |
1765 | m_isPopupShown = false; | |
1766 | ||
1767 | // Inform popup control itself | |
1768 | m_popupInterface->OnDismiss(); | |
1769 | ||
1770 | if ( m_popupExtraHandler ) | |
1771 | ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss(); | |
1772 | ||
1773 | #if INSTALL_TOPLEV_HANDLER | |
1774 | // Remove top level window event handler | |
1775 | if ( m_toplevEvtHandler ) | |
1776 | { | |
1777 | wxWindow* toplev = ::wxGetTopLevelParent( this ); | |
1778 | if ( toplev ) | |
1779 | toplev->RemoveEventHandler( m_toplevEvtHandler ); | |
1780 | } | |
1781 | #endif | |
1782 | ||
1783 | m_timeCanAcceptClick = ::wxGetLocalTimeMillis() + 150; | |
1784 | ||
1785 | // If cursor not on dropdown button, then clear its state | |
1786 | // (technically not required by all ports, but do it for all just in case) | |
1787 | if ( !m_btnArea.Inside(ScreenToClient(::wxGetMousePosition())) ) | |
1788 | m_btnState = 0; | |
1789 | ||
1790 | // Return parent's tab traversal flag. | |
1791 | // See ShowPopup for notes. | |
1792 | if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL ) | |
1793 | { | |
1794 | wxWindow* parent = GetParent(); | |
1795 | parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL ); | |
1796 | m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL); | |
1797 | } | |
1798 | ||
1799 | // refresh control (necessary even if m_text) | |
1800 | Refresh(); | |
1801 | ||
1802 | #if !wxUSE_POPUPWIN | |
1803 | SetFocus(); | |
1804 | #endif | |
1805 | ||
1806 | } | |
1807 | ||
a57d600f | 1808 | void wxComboCtrlBase::HidePopup() |
a340b80d VZ |
1809 | { |
1810 | // Should be able to call this without popup interface | |
1811 | //wxCHECK_RET( m_popupInterface, _T("no popup interface") ); | |
1812 | if ( !m_isPopupShown ) | |
1813 | return; | |
1814 | ||
1815 | // transfer value and show it in textctrl, if any | |
1816 | SetValue( m_popupInterface->GetStringValue() ); | |
1817 | ||
1818 | #if USE_TRANSIENT_POPUP | |
1819 | ((wxPopupTransientWindow*)m_winPopup)->Dismiss(); | |
1820 | #else | |
1821 | m_winPopup->Hide(); | |
1822 | #endif | |
1823 | ||
1824 | OnPopupDismiss(); | |
1825 | } | |
1826 | ||
1827 | // ---------------------------------------------------------------------------- | |
1828 | // customization methods | |
1829 | // ---------------------------------------------------------------------------- | |
1830 | ||
a57d600f | 1831 | void wxComboCtrlBase::SetButtonPosition( int width, int height, |
a340b80d VZ |
1832 | int side, int spacingX ) |
1833 | { | |
1834 | m_btnWid = width; | |
1835 | m_btnHei = height; | |
1836 | m_btnSide = side; | |
1837 | m_btnSpacingX = spacingX; | |
1838 | ||
1839 | RecalcAndRefresh(); | |
1840 | } | |
1841 | ||
a57d600f | 1842 | void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal, |
a340b80d VZ |
1843 | bool blankButtonBg, |
1844 | const wxBitmap& bmpPressed, | |
1845 | const wxBitmap& bmpHover, | |
1846 | const wxBitmap& bmpDisabled ) | |
1847 | { | |
1848 | m_bmpNormal = bmpNormal; | |
1849 | m_blankButtonBg = blankButtonBg; | |
1850 | ||
1851 | if ( bmpPressed.Ok() ) | |
1852 | m_bmpPressed = bmpPressed; | |
1853 | else | |
1854 | m_bmpPressed = bmpNormal; | |
1855 | ||
1856 | if ( bmpHover.Ok() ) | |
1857 | m_bmpHover = bmpHover; | |
1858 | else | |
1859 | m_bmpHover = bmpNormal; | |
1860 | ||
1861 | if ( bmpDisabled.Ok() ) | |
1862 | m_bmpDisabled = bmpDisabled; | |
1863 | else | |
1864 | m_bmpDisabled = bmpNormal; | |
1865 | ||
1866 | RecalcAndRefresh(); | |
1867 | } | |
1868 | ||
a57d600f | 1869 | void wxComboCtrlBase::SetCustomPaintWidth( int width ) |
a340b80d VZ |
1870 | { |
1871 | if ( m_text ) | |
1872 | { | |
1873 | // move textctrl accordingly | |
1874 | wxRect r = m_text->GetRect(); | |
1875 | int inc = width - m_widthCustomPaint; | |
1876 | r.x += inc; | |
1877 | r.width -= inc; | |
1878 | m_text->SetSize( r ); | |
1879 | } | |
1880 | ||
1881 | m_widthCustomPaint = width; | |
1882 | ||
1883 | RecalcAndRefresh(); | |
1884 | } | |
1885 | ||
a57d600f | 1886 | void wxComboCtrlBase::SetTextIndent( int indent ) |
a340b80d VZ |
1887 | { |
1888 | if ( indent < 0 ) | |
1889 | { | |
1890 | m_absIndent = GetNativeTextIndent(); | |
1891 | m_iFlags &= ~(wxCC_IFLAG_INDENT_SET); | |
1892 | } | |
1893 | else | |
1894 | { | |
1895 | m_absIndent = indent; | |
1896 | m_iFlags |= wxCC_IFLAG_INDENT_SET; | |
1897 | } | |
1898 | ||
1899 | RecalcAndRefresh(); | |
1900 | } | |
1901 | ||
a57d600f | 1902 | wxCoord wxComboCtrlBase::GetNativeTextIndent() const |
a340b80d VZ |
1903 | { |
1904 | return DEFAULT_TEXT_INDENT; | |
1905 | } | |
1906 | ||
1907 | // ---------------------------------------------------------------------------- | |
1908 | // methods forwarded to wxTextCtrl | |
1909 | // ---------------------------------------------------------------------------- | |
1910 | ||
a57d600f | 1911 | wxString wxComboCtrlBase::GetValue() const |
a340b80d VZ |
1912 | { |
1913 | if ( m_text ) | |
1914 | return m_text->GetValue(); | |
1915 | return m_valueString; | |
1916 | } | |
1917 | ||
a57d600f | 1918 | void wxComboCtrlBase::SetValue(const wxString& value) |
a340b80d VZ |
1919 | { |
1920 | if ( m_text ) | |
1921 | { | |
1922 | m_text->SetValue(value); | |
1923 | if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) ) | |
1924 | m_text->SelectAll(); | |
1925 | } | |
1926 | ||
6d0ce565 VZ |
1927 | m_valueString = value; |
1928 | ||
1929 | Refresh(); | |
1930 | ||
a340b80d VZ |
1931 | // Since wxComboPopup may want to paint the combo as well, we need |
1932 | // to set the string value here (as well as sometimes in ShowPopup). | |
1933 | if ( m_valueString != value && m_popupInterface ) | |
1934 | { | |
1935 | m_popupInterface->SetStringValue(value); | |
1936 | } | |
6d0ce565 VZ |
1937 | } |
1938 | ||
1939 | // In this SetValue variant wxComboPopup::SetStringValue is not called | |
a57d600f | 1940 | void wxComboCtrlBase::SetText(const wxString& value) |
6d0ce565 VZ |
1941 | { |
1942 | // Unlike in SetValue(), this must be called here or | |
1943 | // the behaviour will no be consistent in readonlys. | |
1944 | EnsurePopupControl(); | |
a340b80d VZ |
1945 | |
1946 | m_valueString = value; | |
1947 | ||
1948 | Refresh(); | |
1949 | } | |
1950 | ||
a57d600f | 1951 | void wxComboCtrlBase::Copy() |
a340b80d VZ |
1952 | { |
1953 | if ( m_text ) | |
1954 | m_text->Copy(); | |
1955 | } | |
1956 | ||
a57d600f | 1957 | void wxComboCtrlBase::Cut() |
a340b80d VZ |
1958 | { |
1959 | if ( m_text ) | |
1960 | m_text->Cut(); | |
1961 | } | |
1962 | ||
a57d600f | 1963 | void wxComboCtrlBase::Paste() |
a340b80d VZ |
1964 | { |
1965 | if ( m_text ) | |
1966 | m_text->Paste(); | |
1967 | } | |
1968 | ||
a57d600f | 1969 | void wxComboCtrlBase::SetInsertionPoint(long pos) |
a340b80d VZ |
1970 | { |
1971 | if ( m_text ) | |
1972 | m_text->SetInsertionPoint(pos); | |
1973 | } | |
1974 | ||
a57d600f | 1975 | void wxComboCtrlBase::SetInsertionPointEnd() |
a340b80d VZ |
1976 | { |
1977 | if ( m_text ) | |
1978 | m_text->SetInsertionPointEnd(); | |
1979 | } | |
1980 | ||
a57d600f | 1981 | long wxComboCtrlBase::GetInsertionPoint() const |
a340b80d VZ |
1982 | { |
1983 | if ( m_text ) | |
1984 | return m_text->GetInsertionPoint(); | |
1985 | ||
1986 | return 0; | |
1987 | } | |
1988 | ||
a57d600f | 1989 | long wxComboCtrlBase::GetLastPosition() const |
a340b80d VZ |
1990 | { |
1991 | if ( m_text ) | |
1992 | return m_text->GetLastPosition(); | |
1993 | ||
1994 | return 0; | |
1995 | } | |
1996 | ||
a57d600f | 1997 | void wxComboCtrlBase::Replace(long from, long to, const wxString& value) |
a340b80d VZ |
1998 | { |
1999 | if ( m_text ) | |
2000 | m_text->Replace(from, to, value); | |
2001 | } | |
2002 | ||
a57d600f | 2003 | void wxComboCtrlBase::Remove(long from, long to) |
a340b80d VZ |
2004 | { |
2005 | if ( m_text ) | |
2006 | m_text->Remove(from, to); | |
2007 | } | |
2008 | ||
a57d600f | 2009 | void wxComboCtrlBase::SetSelection(long from, long to) |
a340b80d VZ |
2010 | { |
2011 | if ( m_text ) | |
2012 | m_text->SetSelection(from, to); | |
2013 | } | |
2014 | ||
a57d600f | 2015 | void wxComboCtrlBase::Undo() |
a340b80d VZ |
2016 | { |
2017 | if ( m_text ) | |
2018 | m_text->Undo(); | |
2019 | } | |
2020 | ||
a57d600f | 2021 | #endif // wxUSE_COMBOCTRL |