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