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