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