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