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