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