Also redirect wxEVT_KEY_UP and wxEVT_CHAR from the embedded wxTextCtrl
[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 wxObject* prevObj = event.GetEventObject();
588
589 event.SetId(combo->GetId());
590 event.SetEventObject(combo);
591 combo->GetEventHandler()->ProcessEvent(event);
592
593 event.SetId(((wxWindow*)prevObj)->GetId());
594 event.SetEventObject(prevObj);
595 }
596
597 void wxComboBoxExtraInputHandler::OnFocus(wxFocusEvent& event)
598 {
599 // FIXME: This code does run when control is clicked,
600 // yet on Windows it doesn't select all the text.
601 if ( event.GetEventType() == wxEVT_SET_FOCUS &&
602 !(m_combo->GetInternalFlags() & wxCC_NO_TEXT_AUTO_SELECT) )
603 {
604 if ( m_combo->GetTextCtrl() )
605 m_combo->GetTextCtrl()->SelectAll();
606 else
607 m_combo->SetSelection(-1,-1);
608 }
609
610 // Send focus indication to parent.
611 // NB: This is needed for cases where the textctrl gets focus
612 // instead of its parent. While this may trigger multiple
613 // wxEVT_SET_FOCUSes (since m_text->SetFocus is called
614 // from combo's focus event handler), they should be quite
615 // harmless.
616 wxFocusEvent evt2(event.GetEventType(),m_combo->GetId());
617 evt2.SetEventObject(m_combo);
618 m_combo->GetEventHandler()->ProcessEvent(evt2);
619
620 event.Skip();
621 }
622
623
624 //
625 // This is pushed to the event handler queue of the control in popup.
626 //
627
628 class wxComboPopupExtraEventHandler : public wxEvtHandler
629 {
630 public:
631
632 wxComboPopupExtraEventHandler( wxComboCtrlBase* combo )
633 : wxEvtHandler()
634 {
635 m_combo = combo;
636 m_beenInside = false;
637 }
638 virtual ~wxComboPopupExtraEventHandler() { }
639
640 void OnMouseEvent( wxMouseEvent& event );
641
642 // Called from wxComboCtrlBase::OnPopupDismiss
643 void OnPopupDismiss()
644 {
645 m_beenInside = false;
646 }
647
648 protected:
649 wxComboCtrlBase* m_combo;
650
651 bool m_beenInside;
652
653 private:
654 DECLARE_EVENT_TABLE()
655 };
656
657
658 BEGIN_EVENT_TABLE(wxComboPopupExtraEventHandler, wxEvtHandler)
659 EVT_MOUSE_EVENTS(wxComboPopupExtraEventHandler::OnMouseEvent)
660 END_EVENT_TABLE()
661
662
663 void wxComboPopupExtraEventHandler::OnMouseEvent( wxMouseEvent& event )
664 {
665 wxPoint pt = event.GetPosition();
666 wxSize sz = m_combo->GetPopupControl()->GetControl()->GetClientSize();
667 int evtType = event.GetEventType();
668 bool isInside = pt.x >= 0 && pt.y >= 0 && pt.x < sz.x && pt.y < sz.y;
669 bool relayToButton = false;
670
671 event.Skip();
672
673 if ( evtType == wxEVT_MOTION ||
674 evtType == wxEVT_LEFT_DOWN ||
675 evtType == wxEVT_RIGHT_DOWN )
676 {
677 // Block motion and click events outside the popup
678 if ( !isInside || !m_combo->IsPopupShown() )
679 {
680 event.Skip(false);
681 }
682 }
683 else if ( evtType == wxEVT_LEFT_UP )
684 {
685 if ( !m_combo->IsPopupShown() )
686 {
687 event.Skip(false);
688 relayToButton = true;
689 }
690 else if ( !m_beenInside )
691 {
692 if ( isInside )
693 {
694 m_beenInside = true;
695 }
696 else
697 {
698 relayToButton = true;
699 }
700 }
701 }
702
703 if ( relayToButton )
704 {
705 //
706 // Some mouse events to popup that happen outside it, before cursor
707 // has been inside the popup, need to be ignored by it but relayed to
708 // the dropbutton.
709 //
710 wxWindow* eventSink = m_combo;
711 wxWindow* btn = m_combo->GetButton();
712 if ( btn )
713 eventSink = btn;
714
715 eventSink->GetEventHandler()->ProcessEvent(event);
716 }
717 }
718
719 // ----------------------------------------------------------------------------
720 // wxComboCtrlTextCtrl
721 // ----------------------------------------------------------------------------
722
723 class wxComboCtrlTextCtrl : public wxTextCtrl
724 {
725 public:
726 wxComboCtrlTextCtrl() : wxTextCtrl() { }
727 virtual ~wxComboCtrlTextCtrl() { }
728
729 virtual wxWindow *GetMainWindowOfCompositeControl()
730 {
731 wxComboCtrl* combo = (wxComboCtrl*) GetParent();
732
733 // Returning this instead of just 'parent' lets FindFocus work
734 // correctly even when parent control is a child of a composite
735 // generic control (as is case with wxGenericDatePickerCtrl).
736 return combo->GetMainWindowOfCompositeControl();
737 }
738 };
739
740 // ----------------------------------------------------------------------------
741 // wxComboCtrlBase
742 // ----------------------------------------------------------------------------
743
744
745 BEGIN_EVENT_TABLE(wxComboCtrlBase, wxControl)
746 EVT_TEXT(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
747 EVT_SIZE(wxComboCtrlBase::OnSizeEvent)
748 EVT_SET_FOCUS(wxComboCtrlBase::OnFocusEvent)
749 EVT_KILL_FOCUS(wxComboCtrlBase::OnFocusEvent)
750 EVT_IDLE(wxComboCtrlBase::OnIdleEvent)
751 //EVT_BUTTON(wxID_ANY,wxComboCtrlBase::OnButtonClickEvent)
752 EVT_KEY_DOWN(wxComboCtrlBase::OnKeyEvent)
753 EVT_TEXT_ENTER(wxID_ANY,wxComboCtrlBase::OnTextCtrlEvent)
754 EVT_SYS_COLOUR_CHANGED(wxComboCtrlBase::OnSysColourChanged)
755 END_EVENT_TABLE()
756
757
758 IMPLEMENT_ABSTRACT_CLASS(wxComboCtrlBase, wxControl)
759
760 void wxComboCtrlBase::Init()
761 {
762 m_winPopup = NULL;
763 m_popup = NULL;
764 m_popupWinState = Hidden;
765 m_btn = NULL;
766 m_text = NULL;
767 m_popupInterface = NULL;
768
769 m_popupExtraHandler = NULL;
770 m_textEvtHandler = NULL;
771
772 #if INSTALL_TOPLEV_HANDLER
773 m_toplevEvtHandler = NULL;
774 #endif
775
776 m_mainCtrlWnd = this;
777
778 m_heightPopup = -1;
779 m_widthMinPopup = -1;
780 m_anchorSide = 0;
781 m_widthCustomPaint = 0;
782 m_widthCustomBorder = 0;
783
784 m_btnState = 0;
785 m_btnWidDefault = 0;
786 m_blankButtonBg = false;
787 m_ignoreEvtText = 0;
788 m_popupWinType = POPUPWIN_NONE;
789 m_btnWid = m_btnHei = -1;
790 m_btnSide = wxRIGHT;
791 m_btnSpacingX = 0;
792
793 m_extLeft = 0;
794 m_extRight = 0;
795 m_absIndent = -1;
796 m_iFlags = 0;
797 m_timeCanAcceptClick = 0;
798
799 m_resetFocus = false;
800 }
801
802 bool wxComboCtrlBase::Create(wxWindow *parent,
803 wxWindowID id,
804 const wxString& value,
805 const wxPoint& pos,
806 const wxSize& size,
807 long style,
808 const wxValidator& validator,
809 const wxString& name)
810 {
811 if ( !wxControl::Create(parent,
812 id,
813 pos,
814 size,
815 style | wxWANTS_CHARS,
816 validator,
817 name) )
818 return false;
819
820 m_valueString = value;
821
822 // Get colours
823 OnThemeChange();
824 m_absIndent = GetNativeTextIndent();
825
826 m_iFlags |= wxCC_IFLAG_CREATED;
827
828 // If x and y indicate valid size, wxSizeEvent won't be
829 // emitted automatically, so we need to add artifical one.
830 if ( size.x > 0 && size.y > 0 )
831 {
832 wxSizeEvent evt(size,GetId());
833 GetEventHandler()->AddPendingEvent(evt);
834 }
835
836 return true;
837 }
838
839 void wxComboCtrlBase::InstallInputHandlers()
840 {
841 if ( m_text )
842 {
843 m_textEvtHandler = new wxComboBoxExtraInputHandler(this);
844 m_text->PushEventHandler(m_textEvtHandler);
845 }
846 }
847
848 void
849 wxComboCtrlBase::CreateTextCtrl(int style, const wxValidator& validator)
850 {
851 if ( !(m_windowStyle & wxCB_READONLY) )
852 {
853 if ( m_text )
854 m_text->Destroy();
855
856 // wxTE_PROCESS_TAB is needed because on Windows, wxTAB_TRAVERSAL is
857 // not used by the wxPropertyGrid and therefore the tab is processed by
858 // looking at ancestors to see if they have wxTAB_TRAVERSAL. The
859 // navigation event is then sent to the wrong window.
860 style |= wxTE_PROCESS_TAB;
861
862 if ( HasFlag(wxTE_PROCESS_ENTER) )
863 style |= wxTE_PROCESS_ENTER;
864
865 // Ignore EVT_TEXT generated by the constructor (but only
866 // if the event redirector already exists)
867 // NB: This must be " = 1" instead of "++";
868 if ( m_textEvtHandler )
869 m_ignoreEvtText = 1;
870 else
871 m_ignoreEvtText = 0;
872
873 m_text = new wxComboCtrlTextCtrl();
874 m_text->Create(this, wxID_ANY, m_valueString,
875 wxDefaultPosition, wxSize(10,-1),
876 style, validator);
877 }
878 }
879
880 void wxComboCtrlBase::OnThemeChange()
881 {
882 // Leave the default bg on the Mac so the area used by the focus ring will
883 // be the correct colour and themed brush. Instead we'll use
884 // wxSYS_COLOUR_WINDOW in the EVT_PAINT handler as needed.
885 #ifndef __WXMAC__
886 SetOwnBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
887 #endif
888 }
889
890 wxComboCtrlBase::~wxComboCtrlBase()
891 {
892 if ( HasCapture() )
893 ReleaseMouse();
894
895 #if INSTALL_TOPLEV_HANDLER
896 delete ((wxComboFrameEventHandler*)m_toplevEvtHandler);
897 m_toplevEvtHandler = NULL;
898 #endif
899
900 DestroyPopup();
901
902 if ( m_text )
903 m_text->RemoveEventHandler(m_textEvtHandler);
904
905 delete m_textEvtHandler;
906 }
907
908
909 // ----------------------------------------------------------------------------
910 // geometry stuff
911 // ----------------------------------------------------------------------------
912
913 // Recalculates button and textctrl areas
914 void wxComboCtrlBase::CalculateAreas( int btnWidth )
915 {
916 wxSize sz = GetClientSize();
917 int customBorder = m_widthCustomBorder;
918 int btnBorder; // border for button only
919
920 // check if button should really be outside the border: we'll do it it if
921 // its platform default or bitmap+pushbutton background is used, but not if
922 // there is vertical size adjustment or horizontal spacing.
923 if ( ( (m_iFlags & wxCC_BUTTON_OUTSIDE_BORDER) ||
924 (m_bmpNormal.Ok() && m_blankButtonBg) ) &&
925 m_btnSpacingX == 0 &&
926 m_btnHei <= 0 )
927 {
928 m_iFlags |= wxCC_IFLAG_BUTTON_OUTSIDE;
929 btnBorder = 0;
930 }
931 else if ( (m_iFlags & wxCC_BUTTON_COVERS_BORDER) &&
932 m_btnSpacingX == 0 && !m_bmpNormal.Ok() )
933 {
934 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
935 btnBorder = 0;
936 }
937 else
938 {
939 m_iFlags &= ~(wxCC_IFLAG_BUTTON_OUTSIDE);
940 btnBorder = customBorder;
941 }
942
943 // Defaul indentation
944 if ( m_absIndent < 0 )
945 m_absIndent = GetNativeTextIndent();
946
947 int butWidth = btnWidth;
948
949 if ( butWidth <= 0 )
950 butWidth = m_btnWidDefault;
951 else
952 m_btnWidDefault = butWidth;
953
954 if ( butWidth <= 0 )
955 return;
956
957 int butHeight = sz.y - btnBorder*2;
958
959 // Adjust button width
960 if ( m_btnWid > 0 )
961 butWidth = m_btnWid;
962 else
963 {
964 // Adjust button width to match aspect ratio
965 // (but only if control is smaller than best size).
966 int bestHeight = GetBestSize().y;
967 int height = GetSize().y;
968
969 if ( height < bestHeight )
970 {
971 // Make very small buttons square, as it makes
972 // them accommodate arrow image better and still
973 // looks decent.
974 if ( height > 18 )
975 butWidth = (height*butWidth)/bestHeight;
976 else
977 butWidth = butHeight;
978 }
979 }
980
981 // Adjust button height
982 if ( m_btnHei > 0 )
983 butHeight = m_btnHei;
984
985 // Use size of normal bitmap if...
986 // It is larger
987 // OR
988 // button width is set to default and blank button bg is not drawn
989 if ( m_bmpNormal.Ok() )
990 {
991 int bmpReqWidth = m_bmpNormal.GetWidth();
992 int bmpReqHeight = m_bmpNormal.GetHeight();
993
994 // If drawing blank button background, we need to add some margin.
995 if ( m_blankButtonBg )
996 {
997 bmpReqWidth += BMP_BUTTON_MARGIN*2;
998 bmpReqHeight += BMP_BUTTON_MARGIN*2;
999 }
1000
1001 if ( butWidth < bmpReqWidth || ( m_btnWid == 0 && !m_blankButtonBg ) )
1002 butWidth = bmpReqWidth;
1003 if ( butHeight < bmpReqHeight || ( m_btnHei == 0 && !m_blankButtonBg ) )
1004 butHeight = bmpReqHeight;
1005
1006 // Need to fix height?
1007 if ( (sz.y-(customBorder*2)) < butHeight && btnWidth == 0 )
1008 {
1009 int newY = butHeight+(customBorder*2);
1010 SetClientSize(wxDefaultCoord,newY);
1011 if ( m_bmpNormal.Ok() || m_btnArea.width != butWidth || m_btnArea.height != butHeight )
1012 m_iFlags |= wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
1013 else
1014 m_iFlags &= ~wxCC_IFLAG_HAS_NONSTANDARD_BUTTON;
1015
1016 sz.y = newY;
1017 }
1018 }
1019
1020 int butAreaWid = butWidth + (m_btnSpacingX*2);
1021
1022 m_btnSize.x = butWidth;
1023 m_btnSize.y = butHeight;
1024
1025 m_btnArea.x = ( m_btnSide==wxRIGHT ? sz.x - butAreaWid - btnBorder : btnBorder );
1026 m_btnArea.y = btnBorder + FOCUS_RING;
1027 m_btnArea.width = butAreaWid;
1028 m_btnArea.height = sz.y - ((btnBorder+FOCUS_RING)*2);
1029
1030 m_tcArea.x = ( m_btnSide==wxRIGHT ? 0 : butAreaWid ) + customBorder + FOCUS_RING;
1031 m_tcArea.y = customBorder + FOCUS_RING;
1032 m_tcArea.width = sz.x - butAreaWid - (customBorder*2) - (FOCUS_RING*2);
1033 m_tcArea.height = sz.y - ((customBorder+FOCUS_RING)*2);
1034
1035 /*
1036 if ( m_text )
1037 {
1038 ::wxMessageBox(wxString::Format(wxT("ButtonArea (%i,%i,%i,%i)\n"),m_btnArea.x,m_btnArea.y,m_btnArea.width,m_btnArea.height) +
1039 wxString::Format(wxT("TextCtrlArea (%i,%i,%i,%i)"),m_tcArea.x,m_tcArea.y,m_tcArea.width,m_tcArea.height));
1040 }
1041 */
1042 }
1043
1044 void wxComboCtrlBase::PositionTextCtrl( int textCtrlXAdjust, int textCtrlYAdjust )
1045 {
1046 if ( !m_text )
1047 return;
1048
1049 #if !TEXTCTRL_TEXT_CENTERED
1050
1051 wxSize sz = GetClientSize();
1052
1053 int customBorder = m_widthCustomBorder;
1054 if ( (m_text->GetWindowStyleFlag() & wxBORDER_MASK) == wxNO_BORDER )
1055 {
1056 // Centre textctrl
1057 int tcSizeY = m_text->GetBestSize().y;
1058 int diff = sz.y - tcSizeY;
1059 int y = textCtrlYAdjust + (diff/2);
1060
1061 if ( y < customBorder )
1062 y = customBorder;
1063
1064 m_text->SetSize( m_tcArea.x + m_widthCustomPaint + m_absIndent + textCtrlXAdjust,
1065 y,
1066 m_tcArea.width - COMBO_MARGIN -
1067 (textCtrlXAdjust + m_widthCustomPaint + m_absIndent),
1068 -1 );
1069
1070 // Make sure textctrl doesn't exceed the bottom custom border
1071 wxSize tsz = m_text->GetSize();
1072 diff = (y + tsz.y) - (sz.y - customBorder);
1073 if ( diff >= 0 )
1074 {
1075 tsz.y = tsz.y - diff - 1;
1076 m_text->SetSize(tsz);
1077 }
1078 }
1079 else
1080 #else // TEXTCTRL_TEXT_CENTERED
1081 wxUnusedVar(textCtrlXAdjust);
1082 wxUnusedVar(textCtrlYAdjust);
1083 #endif // !TEXTCTRL_TEXT_CENTERED/TEXTCTRL_TEXT_CENTERED
1084 {
1085 // If it has border, have textctrl will the entire text field.
1086 m_text->SetSize( m_tcArea.x + m_widthCustomPaint,
1087 m_tcArea.y,
1088 m_tcArea.width - m_widthCustomPaint,
1089 m_tcArea.height );
1090 }
1091 }
1092
1093 wxSize wxComboCtrlBase::DoGetBestSize() const
1094 {
1095 wxSize sizeText(150,0);
1096
1097 if ( m_text )
1098 sizeText = m_text->GetBestSize();
1099
1100 // TODO: Better method to calculate close-to-native control height.
1101
1102 int fhei;
1103 if ( m_font.Ok() )
1104 fhei = (m_font.GetPointSize()*2) + 5;
1105 else if ( wxNORMAL_FONT->Ok() )
1106 fhei = (wxNORMAL_FONT->GetPointSize()*2) + 5;
1107 else
1108 fhei = sizeText.y + 4;
1109
1110 // Need to force height to accomodate bitmap?
1111 int btnSizeY = m_btnSize.y;
1112 if ( m_bmpNormal.Ok() && fhei < btnSizeY )
1113 fhei = btnSizeY;
1114
1115 // Control height doesn't depend on border
1116 /*
1117 // Add border
1118 int border = m_windowStyle & wxBORDER_MASK;
1119 if ( border == wxSIMPLE_BORDER )
1120 fhei += 2;
1121 else if ( border == wxNO_BORDER )
1122 fhei += (m_widthCustomBorder*2);
1123 else
1124 // Sunken etc.
1125 fhei += 4;
1126 */
1127
1128 // Final adjustments
1129 #ifdef __WXGTK__
1130 fhei += 1;
1131 #endif
1132
1133 #ifdef __WXMAC__
1134 // these are the numbers from the HIG:
1135 switch ( m_windowVariant )
1136 {
1137 case wxWINDOW_VARIANT_NORMAL:
1138 default :
1139 fhei = 22;
1140 break;
1141 case wxWINDOW_VARIANT_SMALL:
1142 fhei = 19;
1143 break;
1144 case wxWINDOW_VARIANT_MINI:
1145 fhei = 15;
1146 break;
1147 }
1148 #endif
1149
1150 fhei += 2 * FOCUS_RING;
1151 int width = sizeText.x + FOCUS_RING + COMBO_MARGIN + DEFAULT_DROPBUTTON_WIDTH;
1152
1153 wxSize ret(width, fhei);
1154 CacheBestSize(ret);
1155 return ret;
1156 }
1157
1158 void wxComboCtrlBase::OnSizeEvent( wxSizeEvent& event )
1159 {
1160 if ( !IsCreated() )
1161 return;
1162
1163 // defined by actual wxComboCtrls
1164 OnResize();
1165
1166 event.Skip();
1167 }
1168
1169 // ----------------------------------------------------------------------------
1170 // standard operations
1171 // ----------------------------------------------------------------------------
1172
1173 bool wxComboCtrlBase::Enable(bool enable)
1174 {
1175 if ( !wxControl::Enable(enable) )
1176 return false;
1177
1178 if ( m_btn )
1179 m_btn->Enable(enable);
1180 if ( m_text )
1181 m_text->Enable(enable);
1182
1183 Refresh();
1184
1185 return true;
1186 }
1187
1188 bool wxComboCtrlBase::Show(bool show)
1189 {
1190 if ( !wxControl::Show(show) )
1191 return false;
1192
1193 if (m_btn)
1194 m_btn->Show(show);
1195
1196 if (m_text)
1197 m_text->Show(show);
1198
1199 return true;
1200 }
1201
1202 bool wxComboCtrlBase::SetFont ( const wxFont& font )
1203 {
1204 if ( !wxControl::SetFont(font) )
1205 return false;
1206
1207 if (m_text)
1208 m_text->SetFont(font);
1209
1210 return true;
1211 }
1212
1213 #if wxUSE_TOOLTIPS
1214 void wxComboCtrlBase::DoSetToolTip(wxToolTip *tooltip)
1215 {
1216 wxControl::DoSetToolTip(tooltip);
1217
1218 // Set tool tip for button and text box
1219 if ( tooltip )
1220 {
1221 const wxString &tip = tooltip->GetTip();
1222 if ( m_text ) m_text->SetToolTip(tip);
1223 if ( m_btn ) m_btn->SetToolTip(tip);
1224 }
1225 else
1226 {
1227 if ( m_text ) m_text->SetToolTip( NULL );
1228 if ( m_btn ) m_btn->SetToolTip( NULL );
1229 }
1230 }
1231 #endif // wxUSE_TOOLTIPS
1232
1233 #if wxUSE_VALIDATORS
1234 void wxComboCtrlBase::SetValidator(const wxValidator& validator)
1235 {
1236 wxTextCtrl* textCtrl = GetTextCtrl();
1237
1238 if ( textCtrl )
1239 textCtrl->SetValidator( validator );
1240 else
1241 wxControl::SetValidator( validator );
1242 }
1243
1244 wxValidator* wxComboCtrlBase::GetValidator()
1245 {
1246 wxTextCtrl* textCtrl = GetTextCtrl();
1247
1248 return textCtrl ? textCtrl->GetValidator() : wxControl::GetValidator();
1249 }
1250 #endif // wxUSE_VALIDATORS
1251
1252 // ----------------------------------------------------------------------------
1253 // painting
1254 // ----------------------------------------------------------------------------
1255
1256 #if (!defined(__WXMSW__)) || defined(__WXUNIVERSAL__)
1257 // prepare combo box background on area in a way typical on platform
1258 void wxComboCtrlBase::PrepareBackground( wxDC& dc, const wxRect& rect, int flags ) const
1259 {
1260 wxSize sz = GetClientSize();
1261 bool isEnabled;
1262 bool doDrawFocusRect; // also selected
1263
1264 // For smaller size control (and for disabled background) use less spacing
1265 int focusSpacingX;
1266 int focusSpacingY;
1267
1268 if ( !(flags & wxCONTROL_ISSUBMENU) )
1269 {
1270 // Drawing control
1271 isEnabled = IsEnabled();
1272 doDrawFocusRect = ShouldDrawFocus() && !(m_iFlags & wxCC_FULL_BUTTON);
1273
1274 // Windows-style: for smaller size control (and for disabled background) use less spacing
1275 focusSpacingX = isEnabled ? 2 : 1;
1276 focusSpacingY = sz.y > (GetCharHeight()+2) && isEnabled ? 2 : 1;
1277 }
1278 else
1279 {
1280 // Drawing a list item
1281 isEnabled = true; // they are never disabled
1282 doDrawFocusRect = (flags & wxCONTROL_SELECTED) != 0;
1283
1284 focusSpacingX = 0;
1285 focusSpacingY = 0;
1286 }
1287
1288 // Set the background sub-rectangle for selection, disabled etc
1289 wxRect selRect(rect);
1290 selRect.y += focusSpacingY;
1291 selRect.height -= (focusSpacingY*2);
1292
1293 int wcp = 0;
1294
1295 if ( !(flags & wxCONTROL_ISSUBMENU) )
1296 wcp += m_widthCustomPaint;
1297
1298 selRect.x += wcp + focusSpacingX;
1299 selRect.width -= wcp + (focusSpacingX*2);
1300
1301 wxColour bgCol;
1302 bool doDrawSelRect = true;
1303
1304 if ( isEnabled )
1305 {
1306 // If popup is hidden and this control is focused,
1307 // then draw the focus-indicator (selbgcolor background etc.).
1308 if ( doDrawFocusRect )
1309 {
1310 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT) );
1311 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT);
1312 }
1313 else
1314 {
1315 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT) );
1316 #ifndef __WXMAC__ // see note in OnThemeChange
1317 doDrawSelRect = false;
1318 bgCol = GetBackgroundColour();
1319 #else
1320 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1321 #endif
1322 }
1323 }
1324 else
1325 {
1326 dc.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT) );
1327 #ifndef __WXMAC__ // see note in OnThemeChange
1328 bgCol = GetBackgroundColour();
1329 #else
1330 bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1331 #endif
1332 }
1333
1334 dc.SetBrush( bgCol );
1335 if ( doDrawSelRect )
1336 {
1337 dc.SetPen( bgCol );
1338 dc.DrawRectangle( selRect );
1339 }
1340
1341 // Don't clip exactly to the selection rectangle so we can draw
1342 // to the non-selected area in front of it.
1343 wxRect clipRect(rect.x,rect.y,
1344 (selRect.x+selRect.width)-rect.x,rect.height);
1345 dc.SetClippingRegion(clipRect);
1346 }
1347 #else
1348 // Save the library size a bit for platforms that re-implement this.
1349 void wxComboCtrlBase::PrepareBackground( wxDC&, const wxRect&, int ) const
1350 {
1351 }
1352 #endif
1353
1354 void wxComboCtrlBase::DrawButton( wxDC& dc, const wxRect& rect, int flags )
1355 {
1356 int drawState = m_btnState;
1357
1358 if ( (m_iFlags & wxCC_BUTTON_STAYS_DOWN) &&
1359 GetPopupWindowState() >= Animating )
1360 drawState |= wxCONTROL_PRESSED;
1361
1362 wxRect drawRect(rect.x+m_btnSpacingX,
1363 rect.y+((rect.height-m_btnSize.y)/2),
1364 m_btnSize.x,
1365 m_btnSize.y);
1366
1367 // Make sure area is not larger than the control
1368 if ( drawRect.y < rect.y )
1369 drawRect.y = rect.y;
1370 if ( drawRect.height > rect.height )
1371 drawRect.height = rect.height;
1372
1373 bool enabled = IsEnabled();
1374
1375 if ( !enabled )
1376 drawState |= wxCONTROL_DISABLED;
1377
1378 if ( !m_bmpNormal.Ok() )
1379 {
1380 if ( flags & Button_BitmapOnly )
1381 return;
1382
1383 // Need to clear button background even if m_btn is present
1384 if ( flags & Button_PaintBackground )
1385 {
1386 wxColour bgCol;
1387
1388 if ( m_iFlags & wxCC_IFLAG_BUTTON_OUTSIDE )
1389 bgCol = GetParent()->GetBackgroundColour();
1390 else
1391 bgCol = GetBackgroundColour();
1392
1393 dc.SetBrush(bgCol);
1394 dc.SetPen(bgCol);
1395 dc.DrawRectangle(rect);
1396 }
1397
1398 // Draw standard button
1399 wxRendererNative::Get().DrawComboBoxDropButton(this,
1400 dc,
1401 drawRect,
1402 drawState);
1403 }
1404 else
1405 {
1406 // Draw bitmap
1407
1408 wxBitmap* pBmp;
1409
1410 if ( !enabled )
1411 pBmp = &m_bmpDisabled;
1412 else if ( m_btnState & wxCONTROL_PRESSED )
1413 pBmp = &m_bmpPressed;
1414 else if ( m_btnState & wxCONTROL_CURRENT )
1415 pBmp = &m_bmpHover;
1416 else
1417 pBmp = &m_bmpNormal;
1418
1419 if ( m_blankButtonBg )
1420 {
1421 // If using blank button background, we need to clear its background
1422 // with button face colour instead of colour for rest of the control.
1423 if ( flags & Button_PaintBackground )
1424 {
1425 wxColour bgCol = GetParent()->GetBackgroundColour(); //wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
1426 //wxColour bgCol = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW);
1427 dc.SetPen(bgCol);
1428 dc.SetBrush(bgCol);
1429 dc.DrawRectangle(rect);
1430 }
1431
1432 if ( !(flags & Button_BitmapOnly) )
1433 {
1434 wxRendererNative::Get().DrawPushButton(this,
1435 dc,
1436 drawRect,
1437 drawState);
1438 }
1439 }
1440 else
1441
1442 {
1443 // Need to clear button background even if m_btn is present
1444 // (assume non-button background was cleared just before this call so brushes are good)
1445 if ( flags & Button_PaintBackground )
1446 dc.DrawRectangle(rect);
1447 }
1448
1449 // Draw bitmap centered in drawRect
1450 dc.DrawBitmap(*pBmp,
1451 drawRect.x + (drawRect.width-pBmp->GetWidth())/2,
1452 drawRect.y + (drawRect.height-pBmp->GetHeight())/2,
1453 true);
1454 }
1455 }
1456
1457 void wxComboCtrlBase::RecalcAndRefresh()
1458 {
1459 if ( IsCreated() )
1460 {
1461 wxSizeEvent evt(GetSize(),GetId());
1462 GetEventHandler()->ProcessEvent(evt);
1463 Refresh();
1464 }
1465 }
1466
1467 // ----------------------------------------------------------------------------
1468 // miscellaneous event handlers
1469 // ----------------------------------------------------------------------------
1470
1471 void wxComboCtrlBase::OnTextCtrlEvent(wxCommandEvent& event)
1472 {
1473 if ( event.GetEventType() == wxEVT_COMMAND_TEXT_UPDATED )
1474 {
1475 if ( m_ignoreEvtText > 0 )
1476 {
1477 m_ignoreEvtText--;
1478 return;
1479 }
1480 }
1481
1482 // Change event id, object and string before relaying it forward
1483 event.SetId(GetId());
1484 wxString s = event.GetString();
1485 event.SetEventObject(this);
1486 event.SetString(s);
1487 event.Skip();
1488 }
1489
1490 // call if cursor is on button area or mouse is captured for the button
1491 bool wxComboCtrlBase::HandleButtonMouseEvent( wxMouseEvent& event,
1492 int flags )
1493 {
1494 int type = event.GetEventType();
1495
1496 if ( type == wxEVT_MOTION )
1497 {
1498 if ( (flags & wxCC_MF_ON_BUTTON) &&
1499 IsPopupWindowState(Hidden) )
1500 {
1501 if ( !(m_btnState & wxCONTROL_CURRENT) )
1502 {
1503 // Mouse hover begins
1504 m_btnState |= wxCONTROL_CURRENT;
1505 if ( HasCapture() ) // Retain pressed state.
1506 m_btnState |= wxCONTROL_PRESSED;
1507 Refresh();
1508 }
1509 }
1510 else if ( (m_btnState & wxCONTROL_CURRENT) )
1511 {
1512 // Mouse hover ends
1513 m_btnState &= ~(wxCONTROL_CURRENT|wxCONTROL_PRESSED);
1514 Refresh();
1515 }
1516 }
1517 else if ( type == wxEVT_LEFT_DOWN || type == wxEVT_LEFT_DCLICK )
1518 {
1519 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1520 {
1521 m_btnState |= wxCONTROL_PRESSED;
1522 Refresh();
1523
1524 if ( !(m_iFlags & wxCC_POPUP_ON_MOUSE_UP) )
1525 OnButtonClick();
1526 else
1527 // If showing popup now, do not capture mouse or there will be interference
1528 CaptureMouse();
1529 }
1530 }
1531 else if ( type == wxEVT_LEFT_UP )
1532 {
1533
1534 // Only accept event if mouse was left-press was previously accepted
1535 if ( HasCapture() )
1536 ReleaseMouse();
1537
1538 if ( m_btnState & wxCONTROL_PRESSED )
1539 {
1540 // If mouse was inside, fire the click event.
1541 if ( m_iFlags & wxCC_POPUP_ON_MOUSE_UP )
1542 {
1543 if ( flags & (wxCC_MF_ON_CLICK_AREA|wxCC_MF_ON_BUTTON) )
1544 OnButtonClick();
1545 }
1546
1547 m_btnState &= ~(wxCONTROL_PRESSED);
1548 Refresh();
1549 }
1550 }
1551 else if ( type == wxEVT_LEAVE_WINDOW )
1552 {
1553 if ( m_btnState & (wxCONTROL_CURRENT|wxCONTROL_PRESSED) )
1554 {
1555 m_btnState &= ~(wxCONTROL_CURRENT);
1556
1557 // Mouse hover ends
1558 if ( IsPopupWindowState(Hidden) )
1559 {
1560 m_btnState &= ~(wxCONTROL_PRESSED);
1561 Refresh();
1562 }
1563 }
1564 }
1565 else
1566 return false;
1567
1568 // Never have 'hot' state when popup is being shown
1569 // (this is mostly needed because of the animation).
1570 if ( !IsPopupWindowState(Hidden) )
1571 m_btnState &= ~wxCONTROL_CURRENT;
1572
1573 return true;
1574 }
1575
1576 // returns true if event was consumed or filtered
1577 bool wxComboCtrlBase::PreprocessMouseEvent( wxMouseEvent& event,
1578 int WXUNUSED(flags) )
1579 {
1580 wxLongLong t = ::wxGetLocalTimeMillis();
1581 int evtType = event.GetEventType();
1582
1583 #if USES_WXPOPUPWINDOW || USES_WXDIALOG
1584 if ( m_popupWinType != POPUPWIN_WXPOPUPTRANSIENTWINDOW )
1585 {
1586 if ( IsPopupWindowState(Visible) &&
1587 ( evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_RIGHT_DOWN ) )
1588 {
1589 HidePopup();
1590 return true;
1591 }
1592 }
1593 #endif
1594
1595 // Filter out clicks on button immediately after popup dismiss
1596 if ( evtType == wxEVT_LEFT_DOWN && t < m_timeCanAcceptClick )
1597 {
1598 event.SetEventType(0);
1599 return true;
1600 }
1601
1602 return false;
1603 }
1604
1605 void wxComboCtrlBase::HandleNormalMouseEvent( wxMouseEvent& event )
1606 {
1607 int evtType = event.GetEventType();
1608
1609 if ( (evtType == wxEVT_LEFT_DOWN || evtType == wxEVT_LEFT_DCLICK) &&
1610 (m_windowStyle & wxCB_READONLY) )
1611 {
1612 if ( GetPopupWindowState() >= Animating )
1613 {
1614 #if USES_WXPOPUPWINDOW
1615 // Click here always hides the popup.
1616 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
1617 HidePopup();
1618 #endif
1619 }
1620 else
1621 {
1622 if ( !(m_windowStyle & wxCC_SPECIAL_DCLICK) )
1623 {
1624 // In read-only mode, clicking the text is the
1625 // same as clicking the button.
1626 OnButtonClick();
1627 }
1628 else if ( /*evtType == wxEVT_LEFT_UP || */evtType == wxEVT_LEFT_DCLICK )
1629 {
1630 //if ( m_popupInterface->CycleValue() )
1631 // Refresh();
1632 if ( m_popupInterface )
1633 m_popupInterface->OnComboDoubleClick();
1634 }
1635 }
1636 }
1637 else
1638 if ( IsPopupShown() )
1639 {
1640 // relay (some) mouse events to the popup
1641 if ( evtType == wxEVT_MOUSEWHEEL )
1642 m_popup->GetEventHandler()->AddPendingEvent(event);
1643 }
1644 else if ( evtType )
1645 event.Skip();
1646 }
1647
1648 void wxComboCtrlBase::OnKeyEvent(wxKeyEvent& event)
1649 {
1650 if ( IsPopupShown() )
1651 {
1652 // pass it to the popped up control
1653 GetPopupControl()->GetControl()->GetEventHandler()->AddPendingEvent(event);
1654 }
1655 else // no popup
1656 {
1657 if ( HandleAsNavigationKey(event) )
1658 return;
1659
1660 if ( IsKeyPopupToggle(event) )
1661 {
1662 OnButtonClick();
1663 return;
1664 }
1665
1666 int comboStyle = GetWindowStyle();
1667 wxComboPopup* popupInterface = GetPopupControl();
1668
1669 if ( !popupInterface )
1670 {
1671 event.Skip();
1672 return;
1673 }
1674
1675 int keycode = event.GetKeyCode();
1676
1677 if ( (comboStyle & wxCB_READONLY) ||
1678 (keycode != WXK_RIGHT && keycode != WXK_LEFT) )
1679 {
1680 popupInterface->OnComboKeyEvent(event);
1681 }
1682 else
1683 event.Skip();
1684 }
1685 }
1686
1687 void wxComboCtrlBase::OnFocusEvent( wxFocusEvent& event )
1688 {
1689 if ( event.GetEventType() == wxEVT_SET_FOCUS )
1690 {
1691 wxWindow* tc = GetTextCtrl();
1692 if ( tc && tc != DoFindFocus() )
1693 #ifdef __WXMAC__
1694 m_resetFocus = true;
1695 #else
1696 {
1697 tc->SetFocus();
1698 }
1699 #endif
1700 }
1701
1702 Refresh();
1703 }
1704
1705 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent& WXUNUSED(event) )
1706 {
1707 if ( m_resetFocus )
1708 {
1709 m_resetFocus = false;
1710 wxWindow* tc = GetTextCtrl();
1711 if ( tc )
1712 tc->SetFocus();
1713 }
1714 }
1715
1716 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
1717 {
1718 OnThemeChange();
1719 // indentation may also have changed
1720 if ( !(m_iFlags & wxCC_IFLAG_INDENT_SET) )
1721 m_absIndent = GetNativeTextIndent();
1722 RecalcAndRefresh();
1723 }
1724
1725 // ----------------------------------------------------------------------------
1726 // popup handling
1727 // ----------------------------------------------------------------------------
1728
1729 // Create popup window and the child control
1730 void wxComboCtrlBase::CreatePopup()
1731 {
1732 wxComboPopup* popupInterface = m_popupInterface;
1733 wxWindow* popup;
1734
1735 if ( !m_winPopup )
1736 {
1737 #ifdef wxComboPopupWindowBase2
1738 if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP )
1739 {
1740 #if !USES_WXDIALOG
1741 m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER );
1742 #else
1743 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString,
1744 wxPoint(-21,-21), wxSize(20, 20),
1745 wxNO_BORDER );
1746 #endif
1747 m_popupWinType = SECONDARY_POPUP_TYPE;
1748 }
1749 else
1750 #endif // wxComboPopupWindowBase2
1751 {
1752 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
1753 m_popupWinType = PRIMARY_POPUP_TYPE;
1754 }
1755 m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this);
1756 m_winPopup->PushEventHandler(m_popupWinEvtHandler);
1757 }
1758
1759 popupInterface->Create(m_winPopup);
1760 m_popup = popup = popupInterface->GetControl();
1761
1762 m_popupExtraHandler = new wxComboPopupExtraEventHandler(this);
1763 popup->PushEventHandler( m_popupExtraHandler );
1764
1765 // This may be helpful on some platforms
1766 // (eg. it bypasses a wxGTK popupwindow bug where
1767 // window is not initially hidden when it should be)
1768 m_winPopup->Hide();
1769
1770 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
1771 }
1772
1773 // Destroy popup window and the child control
1774 void wxComboCtrlBase::DestroyPopup()
1775 {
1776 HidePopup();
1777
1778 if ( m_popup )
1779 m_popup->RemoveEventHandler(m_popupExtraHandler);
1780
1781 delete m_popupExtraHandler;
1782
1783 delete m_popupInterface;
1784
1785 if ( m_winPopup )
1786 {
1787 m_winPopup->RemoveEventHandler(m_popupWinEvtHandler);
1788 delete m_popupWinEvtHandler;
1789 m_popupWinEvtHandler = NULL;
1790 m_winPopup->Destroy();
1791 }
1792
1793 m_popupExtraHandler = NULL;
1794 m_popupInterface = NULL;
1795 m_winPopup = NULL;
1796 m_popup = NULL;
1797 }
1798
1799 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
1800 {
1801 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
1802
1803 DestroyPopup();
1804
1805 iface->InitBase(this);
1806 iface->Init();
1807
1808 m_popupInterface = iface;
1809
1810 if ( !iface->LazyCreate() )
1811 {
1812 CreatePopup();
1813 }
1814 else
1815 {
1816 m_popup = NULL;
1817 }
1818
1819 // This must be done after creation
1820 if ( m_valueString.length() )
1821 {
1822 iface->SetStringValue(m_valueString);
1823 //Refresh();
1824 }
1825 }
1826
1827 // Ensures there is atleast the default popup
1828 void wxComboCtrlBase::EnsurePopupControl()
1829 {
1830 if ( !m_popupInterface )
1831 SetPopupControl(NULL);
1832 }
1833
1834 void wxComboCtrlBase::OnButtonClick()
1835 {
1836 // Derived classes can override this method for totally custom
1837 // popup action
1838 if ( !IsPopupWindowState(Visible) )
1839 ShowPopup();
1840 else
1841 HidePopup();
1842 }
1843
1844 void wxComboCtrlBase::ShowPopup()
1845 {
1846 EnsurePopupControl();
1847 wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") );
1848
1849 if ( IsPopupWindowState(Animating) )
1850 return;
1851
1852 SetFocus();
1853
1854 // Space above and below
1855 int screenHeight;
1856 wxPoint scrPos;
1857 int spaceAbove;
1858 int spaceBelow;
1859 int maxHeightPopup;
1860 wxSize ctrlSz = GetSize();
1861
1862 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
1863 scrPos = GetParent()->ClientToScreen(GetPosition());
1864
1865 spaceAbove = scrPos.y;
1866 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
1867
1868 maxHeightPopup = spaceBelow;
1869 if ( spaceAbove > spaceBelow )
1870 maxHeightPopup = spaceAbove;
1871
1872 // Width
1873 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
1874
1875 if ( widthPopup < m_widthMinPopup )
1876 widthPopup = m_widthMinPopup;
1877
1878 wxWindow* winPopup = m_winPopup;
1879 wxWindow* popup;
1880
1881 // Need to disable tab traversal of parent
1882 //
1883 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1884 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1885 // that if transient popup is open, then tab traversal is to be ignored.
1886 // However, I think this code would still be needed for cases where
1887 // transient popup doesn't work yet (wxWinCE?).
1888 wxWindow* parent = GetParent();
1889 int parentFlags = parent->GetWindowStyle();
1890 if ( parentFlags & wxTAB_TRAVERSAL )
1891 {
1892 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
1893 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
1894 }
1895
1896 if ( !winPopup )
1897 {
1898 CreatePopup();
1899 winPopup = m_winPopup;
1900 popup = m_popup;
1901 }
1902 else
1903 {
1904 popup = m_popup;
1905 }
1906
1907 winPopup->Enable();
1908
1909 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
1910
1911 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
1912 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
1913 maxHeightPopup);
1914
1915 popup->SetSize(adjustedSize);
1916 popup->Move(0,0);
1917 m_popupInterface->OnPopup();
1918
1919 //
1920 // Reposition and resize popup window
1921 //
1922
1923 wxSize szp = popup->GetSize();
1924
1925 int popupX;
1926 int popupY = scrPos.y + ctrlSz.y;
1927
1928 // Default anchor is wxLEFT
1929 int anchorSide = m_anchorSide;
1930 if ( !anchorSide )
1931 anchorSide = wxLEFT;
1932
1933 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
1934 int leftX = scrPos.x - m_extLeft;
1935
1936 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
1937 leftX -= ctrlSz.x;
1938
1939 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
1940
1941 // If there is not enough horizontal space, anchor on the other side.
1942 // If there is no space even then, place the popup at x 0.
1943 if ( anchorSide == wxRIGHT )
1944 {
1945 if ( rightX < 0 )
1946 {
1947 if ( (leftX+szp.x) < screenWidth )
1948 anchorSide = wxLEFT;
1949 else
1950 anchorSide = 0;
1951 }
1952 }
1953 else
1954 {
1955 if ( (leftX+szp.x) >= screenWidth )
1956 {
1957 if ( rightX >= 0 )
1958 anchorSide = wxRIGHT;
1959 else
1960 anchorSide = 0;
1961 }
1962 }
1963
1964 // Select x coordinate according to the anchor side
1965 if ( anchorSide == wxRIGHT )
1966 popupX = rightX;
1967 else if ( anchorSide == wxLEFT )
1968 popupX = leftX;
1969 else
1970 popupX = 0;
1971
1972 int showFlags = CanDeferShow;
1973
1974 if ( spaceBelow < szp.y )
1975 {
1976 popupY = scrPos.y - szp.y;
1977 showFlags |= ShowAbove;
1978 }
1979
1980 #if INSTALL_TOPLEV_HANDLER
1981 // Put top level window event handler into place
1982 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
1983 {
1984 if ( !m_toplevEvtHandler )
1985 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
1986
1987 wxWindow* toplev = ::wxGetTopLevelParent( this );
1988 wxASSERT( toplev );
1989 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
1990 toplev->PushEventHandler( m_toplevEvtHandler );
1991 }
1992 #endif
1993
1994 // Set string selection (must be this way instead of SetStringSelection)
1995 if ( m_text )
1996 {
1997 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
1998 m_text->SelectAll();
1999
2000 m_popupInterface->SetStringValue( m_text->GetValue() );
2001 }
2002 else
2003 {
2004 // This is neede since focus/selection indication may change when popup is shown
2005 Refresh();
2006 }
2007
2008 // This must be after SetStringValue
2009 m_popupWinState = Animating;
2010
2011 wxRect popupWinRect( popupX, popupY, szp.x, szp.y );
2012
2013 m_popup = popup;
2014 if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) ||
2015 AnimateShow( popupWinRect, showFlags ) )
2016 {
2017 DoShowPopup( popupWinRect, showFlags );
2018 }
2019 }
2020
2021 bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) )
2022 {
2023 return true;
2024 }
2025
2026 void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) )
2027 {
2028 wxWindow* winPopup = m_winPopup;
2029
2030 if ( IsPopupWindowState(Animating) )
2031 {
2032 // Make sure the popup window is shown in the right position.
2033 // Should not matter even if animation already did this.
2034
2035 // Some platforms (GTK) may like SetSize and Move to be separate
2036 // (though the bug was probably fixed).
2037 winPopup->SetSize( rect );
2038
2039 #if USES_WXPOPUPTRANSIENTWINDOW
2040 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2041 ((wxPopupTransientWindow*)winPopup)->Popup(m_popup);
2042 else
2043 #endif
2044 winPopup->Show();
2045
2046 m_popupWinState = Visible;
2047 }
2048 else if ( IsPopupWindowState(Hidden) )
2049 {
2050 // Animation was aborted
2051
2052 wxASSERT( !winPopup->IsShown() );
2053
2054 m_popupWinState = Hidden;
2055 }
2056
2057 Refresh();
2058 }
2059
2060 void wxComboCtrlBase::OnPopupDismiss()
2061 {
2062 // Just in case, avoid double dismiss
2063 if ( IsPopupWindowState(Hidden) )
2064 return;
2065
2066 // This must be set before focus - otherwise there will be recursive
2067 // OnPopupDismisses.
2068 m_popupWinState = Hidden;
2069
2070 //SetFocus();
2071 m_winPopup->Disable();
2072
2073 // Inform popup control itself
2074 m_popupInterface->OnDismiss();
2075
2076 if ( m_popupExtraHandler )
2077 ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss();
2078
2079 #if INSTALL_TOPLEV_HANDLER
2080 // Remove top level window event handler
2081 if ( m_toplevEvtHandler )
2082 {
2083 wxWindow* toplev = ::wxGetTopLevelParent( this );
2084 if ( toplev )
2085 toplev->RemoveEventHandler( m_toplevEvtHandler );
2086 }
2087 #endif
2088
2089 m_timeCanAcceptClick = ::wxGetLocalTimeMillis();
2090
2091 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2092 m_timeCanAcceptClick += 150;
2093
2094 // If cursor not on dropdown button, then clear its state
2095 // (technically not required by all ports, but do it for all just in case)
2096 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
2097 m_btnState = 0;
2098
2099 // Return parent's tab traversal flag.
2100 // See ShowPopup for notes.
2101 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
2102 {
2103 wxWindow* parent = GetParent();
2104 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
2105 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
2106 }
2107
2108 // refresh control (necessary even if m_text)
2109 Refresh();
2110
2111 SetFocus();
2112 }
2113
2114 void wxComboCtrlBase::HidePopup()
2115 {
2116 // Should be able to call this without popup interface
2117 if ( IsPopupWindowState(Hidden) )
2118 return;
2119
2120 // transfer value and show it in textctrl, if any
2121 if ( !IsPopupWindowState(Animating) )
2122 SetValue( m_popupInterface->GetStringValue() );
2123
2124 m_winPopup->Hide();
2125
2126 OnPopupDismiss();
2127 }
2128
2129 // ----------------------------------------------------------------------------
2130 // customization methods
2131 // ----------------------------------------------------------------------------
2132
2133 void wxComboCtrlBase::SetButtonPosition( int width, int height,
2134 int side, int spacingX )
2135 {
2136 m_btnWid = width;
2137 m_btnHei = height;
2138 m_btnSide = side;
2139 m_btnSpacingX = spacingX;
2140
2141 RecalcAndRefresh();
2142 }
2143
2144 wxSize wxComboCtrlBase::GetButtonSize()
2145 {
2146 if ( m_btnSize.x > 0 )
2147 return m_btnSize;
2148
2149 wxSize retSize(m_btnWid,m_btnHei);
2150
2151 // Need to call CalculateAreas now if button size is
2152 // is not explicitly specified.
2153 if ( retSize.x <= 0 || retSize.y <= 0)
2154 {
2155 OnResize();
2156
2157 retSize = m_btnSize;
2158 }
2159
2160 return retSize;
2161 }
2162
2163 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
2164 bool blankButtonBg,
2165 const wxBitmap& bmpPressed,
2166 const wxBitmap& bmpHover,
2167 const wxBitmap& bmpDisabled )
2168 {
2169 m_bmpNormal = bmpNormal;
2170 m_blankButtonBg = blankButtonBg;
2171
2172 if ( bmpPressed.Ok() )
2173 m_bmpPressed = bmpPressed;
2174 else
2175 m_bmpPressed = bmpNormal;
2176
2177 if ( bmpHover.Ok() )
2178 m_bmpHover = bmpHover;
2179 else
2180 m_bmpHover = bmpNormal;
2181
2182 if ( bmpDisabled.Ok() )
2183 m_bmpDisabled = bmpDisabled;
2184 else
2185 m_bmpDisabled = bmpNormal;
2186
2187 RecalcAndRefresh();
2188 }
2189
2190 void wxComboCtrlBase::SetCustomPaintWidth( int width )
2191 {
2192 if ( m_text )
2193 {
2194 // move textctrl accordingly
2195 wxRect r = m_text->GetRect();
2196 int inc = width - m_widthCustomPaint;
2197 r.x += inc;
2198 r.width -= inc;
2199 m_text->SetSize( r );
2200 }
2201
2202 m_widthCustomPaint = width;
2203
2204 RecalcAndRefresh();
2205 }
2206
2207 void wxComboCtrlBase::SetTextIndent( int indent )
2208 {
2209 if ( indent < 0 )
2210 {
2211 m_absIndent = GetNativeTextIndent();
2212 m_iFlags &= ~(wxCC_IFLAG_INDENT_SET);
2213 }
2214 else
2215 {
2216 m_absIndent = indent;
2217 m_iFlags |= wxCC_IFLAG_INDENT_SET;
2218 }
2219
2220 RecalcAndRefresh();
2221 }
2222
2223 wxCoord wxComboCtrlBase::GetNativeTextIndent() const
2224 {
2225 return DEFAULT_TEXT_INDENT;
2226 }
2227
2228 // ----------------------------------------------------------------------------
2229 // methods forwarded to wxTextCtrl
2230 // ----------------------------------------------------------------------------
2231
2232 wxString wxComboCtrlBase::GetValue() const
2233 {
2234 if ( m_text )
2235 return m_text->GetValue();
2236 return m_valueString;
2237 }
2238
2239 void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent)
2240 {
2241 if ( m_text )
2242 {
2243 if ( !withEvent )
2244 m_ignoreEvtText++;
2245
2246 m_text->SetValue(value);
2247
2248 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2249 m_text->SelectAll();
2250 }
2251
2252 // Since wxComboPopup may want to paint the combo as well, we need
2253 // to set the string value here (as well as sometimes in ShowPopup).
2254 if ( m_valueString != value )
2255 {
2256 m_valueString = value;
2257
2258 EnsurePopupControl();
2259
2260 if (m_popupInterface)
2261 m_popupInterface->SetStringValue(value);
2262 }
2263
2264 Refresh();
2265 }
2266
2267 void wxComboCtrlBase::SetValue(const wxString& value)
2268 {
2269 SetValueWithEvent(value, false);
2270 }
2271
2272 // In this SetValue variant wxComboPopup::SetStringValue is not called
2273 void wxComboCtrlBase::SetText(const wxString& value)
2274 {
2275 // Unlike in SetValue(), this must be called here or
2276 // the behaviour will no be consistent in readonlys.
2277 EnsurePopupControl();
2278
2279 m_valueString = value;
2280
2281 if ( m_text )
2282 {
2283 m_ignoreEvtText++;
2284 m_text->SetValue( value );
2285 }
2286
2287 Refresh();
2288 }
2289
2290 void wxComboCtrlBase::Copy()
2291 {
2292 if ( m_text )
2293 m_text->Copy();
2294 }
2295
2296 void wxComboCtrlBase::Cut()
2297 {
2298 if ( m_text )
2299 m_text->Cut();
2300 }
2301
2302 void wxComboCtrlBase::Paste()
2303 {
2304 if ( m_text )
2305 m_text->Paste();
2306 }
2307
2308 void wxComboCtrlBase::SetInsertionPoint(long pos)
2309 {
2310 if ( m_text )
2311 m_text->SetInsertionPoint(pos);
2312 }
2313
2314 void wxComboCtrlBase::SetInsertionPointEnd()
2315 {
2316 if ( m_text )
2317 m_text->SetInsertionPointEnd();
2318 }
2319
2320 long wxComboCtrlBase::GetInsertionPoint() const
2321 {
2322 if ( m_text )
2323 return m_text->GetInsertionPoint();
2324
2325 return 0;
2326 }
2327
2328 long wxComboCtrlBase::GetLastPosition() const
2329 {
2330 if ( m_text )
2331 return m_text->GetLastPosition();
2332
2333 return 0;
2334 }
2335
2336 void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2337 {
2338 if ( m_text )
2339 m_text->Replace(from, to, value);
2340 }
2341
2342 void wxComboCtrlBase::Remove(long from, long to)
2343 {
2344 if ( m_text )
2345 m_text->Remove(from, to);
2346 }
2347
2348 void wxComboCtrlBase::SetSelection(long from, long to)
2349 {
2350 if ( m_text )
2351 m_text->SetSelection(from, to);
2352 }
2353
2354 void wxComboCtrlBase::Undo()
2355 {
2356 if ( m_text )
2357 m_text->Undo();
2358 }
2359
2360 #endif // wxUSE_COMBOCTRL