]> git.saurik.com Git - wxWidgets.git/blob - src/common/combocmn.cpp
Add ribbon libraries sources to the monolithic build.
[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 {
1733 tc->SetFocus();
1734 }
1735 }
1736
1737 Refresh();
1738 }
1739
1740 void wxComboCtrlBase::OnIdleEvent( wxIdleEvent& WXUNUSED(event) )
1741 {
1742 if ( m_resetFocus )
1743 {
1744 m_resetFocus = false;
1745 wxWindow* tc = GetTextCtrl();
1746 if ( tc )
1747 tc->SetFocus();
1748 }
1749 }
1750
1751 void wxComboCtrlBase::OnSysColourChanged(wxSysColourChangedEvent& WXUNUSED(event))
1752 {
1753 OnThemeChange();
1754 // left margin may also have changed
1755 if ( !(m_iFlags & wxCC_IFLAG_LEFT_MARGIN_SET) )
1756 m_marginLeft = GetNativeTextIndent();
1757 RecalcAndRefresh();
1758 }
1759
1760 // ----------------------------------------------------------------------------
1761 // popup handling
1762 // ----------------------------------------------------------------------------
1763
1764 // Create popup window and the child control
1765 void wxComboCtrlBase::CreatePopup()
1766 {
1767 wxComboPopup* popupInterface = m_popupInterface;
1768 wxWindow* popup;
1769
1770 if ( !m_winPopup )
1771 {
1772 #ifdef wxComboPopupWindowBase2
1773 if ( m_iFlags & wxCC_IFLAG_USE_ALT_POPUP )
1774 {
1775 #if !USES_WXDIALOG
1776 m_winPopup = new wxComboPopupWindowBase2( this, wxNO_BORDER );
1777 #else
1778 m_winPopup = new wxComboPopupWindowBase2( this, wxID_ANY, wxEmptyString,
1779 wxPoint(-21,-21), wxSize(20, 20),
1780 wxNO_BORDER );
1781 #endif
1782 m_popupWinType = SECONDARY_POPUP_TYPE;
1783 }
1784 else
1785 #endif // wxComboPopupWindowBase2
1786 {
1787 m_winPopup = new wxComboPopupWindow( this, wxNO_BORDER );
1788 m_popupWinType = PRIMARY_POPUP_TYPE;
1789 }
1790 m_popupWinEvtHandler = new wxComboPopupWindowEvtHandler(this);
1791 m_winPopup->PushEventHandler(m_popupWinEvtHandler);
1792 }
1793
1794 popupInterface->Create(m_winPopup);
1795 m_popup = popup = popupInterface->GetControl();
1796
1797 m_popupExtraHandler = new wxComboPopupExtraEventHandler(this);
1798 popup->PushEventHandler( m_popupExtraHandler );
1799
1800 // This may be helpful on some platforms
1801 // (eg. it bypasses a wxGTK popupwindow bug where
1802 // window is not initially hidden when it should be)
1803 m_winPopup->Hide();
1804
1805 popupInterface->m_iFlags |= wxCP_IFLAG_CREATED;
1806 }
1807
1808 // Destroy popup window and the child control
1809 void wxComboCtrlBase::DestroyPopup()
1810 {
1811 HidePopup();
1812
1813 if ( m_popup )
1814 m_popup->RemoveEventHandler(m_popupExtraHandler);
1815
1816 delete m_popupExtraHandler;
1817
1818 delete m_popupInterface;
1819
1820 if ( m_winPopup )
1821 {
1822 m_winPopup->RemoveEventHandler(m_popupWinEvtHandler);
1823 delete m_popupWinEvtHandler;
1824 m_popupWinEvtHandler = NULL;
1825 m_winPopup->Destroy();
1826 }
1827
1828 m_popupExtraHandler = NULL;
1829 m_popupInterface = NULL;
1830 m_winPopup = NULL;
1831 m_popup = NULL;
1832 }
1833
1834 void wxComboCtrlBase::DoSetPopupControl(wxComboPopup* iface)
1835 {
1836 wxCHECK_RET( iface, wxT("no popup interface set for wxComboCtrl") );
1837
1838 DestroyPopup();
1839
1840 iface->InitBase(this);
1841 iface->Init();
1842
1843 m_popupInterface = iface;
1844
1845 if ( !iface->LazyCreate() )
1846 {
1847 CreatePopup();
1848 }
1849 else
1850 {
1851 m_popup = NULL;
1852 }
1853
1854 // This must be done after creation
1855 if ( m_valueString.length() )
1856 {
1857 iface->SetStringValue(m_valueString);
1858 //Refresh();
1859 }
1860 }
1861
1862 // Ensures there is atleast the default popup
1863 void wxComboCtrlBase::EnsurePopupControl()
1864 {
1865 if ( !m_popupInterface )
1866 SetPopupControl(NULL);
1867 }
1868
1869 void wxComboCtrlBase::OnButtonClick()
1870 {
1871 // Derived classes can override this method for totally custom
1872 // popup action
1873 if ( !IsPopupWindowState(Visible) )
1874 ShowPopup();
1875 else
1876 HidePopup();
1877 }
1878
1879 void wxComboCtrlBase::ShowPopup()
1880 {
1881 EnsurePopupControl();
1882 wxCHECK_RET( !IsPopupWindowState(Visible), wxT("popup window already shown") );
1883
1884 if ( IsPopupWindowState(Animating) )
1885 return;
1886
1887 SetFocus();
1888
1889 // Space above and below
1890 int screenHeight;
1891 wxPoint scrPos;
1892 int spaceAbove;
1893 int spaceBelow;
1894 int maxHeightPopup;
1895 wxSize ctrlSz = GetSize();
1896
1897 screenHeight = wxSystemSettings::GetMetric( wxSYS_SCREEN_Y );
1898 scrPos = GetParent()->ClientToScreen(GetPosition());
1899
1900 spaceAbove = scrPos.y;
1901 spaceBelow = screenHeight - spaceAbove - ctrlSz.y;
1902
1903 maxHeightPopup = spaceBelow;
1904 if ( spaceAbove > spaceBelow )
1905 maxHeightPopup = spaceAbove;
1906
1907 // Width
1908 int widthPopup = ctrlSz.x + m_extLeft + m_extRight;
1909
1910 if ( widthPopup < m_widthMinPopup )
1911 widthPopup = m_widthMinPopup;
1912
1913 wxWindow* winPopup = m_winPopup;
1914 wxWindow* popup;
1915
1916 // Need to disable tab traversal of parent
1917 //
1918 // NB: This is to fix a bug in wxMSW. In theory it could also be fixed
1919 // by, for instance, adding check to window.cpp:wxWindowMSW::MSWProcessMessage
1920 // that if transient popup is open, then tab traversal is to be ignored.
1921 // However, I think this code would still be needed for cases where
1922 // transient popup doesn't work yet (wxWinCE?).
1923 wxWindow* parent = GetParent();
1924 int parentFlags = parent->GetWindowStyle();
1925 if ( parentFlags & wxTAB_TRAVERSAL )
1926 {
1927 parent->SetWindowStyle( parentFlags & ~(wxTAB_TRAVERSAL) );
1928 m_iFlags |= wxCC_IFLAG_PARENT_TAB_TRAVERSAL;
1929 }
1930
1931 if ( !winPopup )
1932 {
1933 CreatePopup();
1934 winPopup = m_winPopup;
1935 popup = m_popup;
1936 }
1937 else
1938 {
1939 popup = m_popup;
1940 }
1941
1942 winPopup->Enable();
1943
1944 wxASSERT( !m_popup || m_popup == popup ); // Consistency check.
1945
1946 wxSize adjustedSize = m_popupInterface->GetAdjustedSize(widthPopup,
1947 m_heightPopup<=0?DEFAULT_POPUP_HEIGHT:m_heightPopup,
1948 maxHeightPopup);
1949
1950 popup->SetSize(adjustedSize);
1951 popup->Move(0,0);
1952 m_popupInterface->OnPopup();
1953
1954 //
1955 // Reposition and resize popup window
1956 //
1957
1958 wxSize szp = popup->GetSize();
1959
1960 int popupX;
1961 int popupY = scrPos.y + ctrlSz.y;
1962
1963 // Default anchor is wxLEFT
1964 int anchorSide = m_anchorSide;
1965 if ( !anchorSide )
1966 anchorSide = wxLEFT;
1967
1968 int rightX = scrPos.x + ctrlSz.x + m_extRight - szp.x;
1969 int leftX = scrPos.x - m_extLeft;
1970
1971 if ( wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft )
1972 leftX -= ctrlSz.x;
1973
1974 int screenWidth = wxSystemSettings::GetMetric( wxSYS_SCREEN_X );
1975
1976 // If there is not enough horizontal space, anchor on the other side.
1977 // If there is no space even then, place the popup at x 0.
1978 if ( anchorSide == wxRIGHT )
1979 {
1980 if ( rightX < 0 )
1981 {
1982 if ( (leftX+szp.x) < screenWidth )
1983 anchorSide = wxLEFT;
1984 else
1985 anchorSide = 0;
1986 }
1987 }
1988 else
1989 {
1990 if ( (leftX+szp.x) >= screenWidth )
1991 {
1992 if ( rightX >= 0 )
1993 anchorSide = wxRIGHT;
1994 else
1995 anchorSide = 0;
1996 }
1997 }
1998
1999 // Select x coordinate according to the anchor side
2000 if ( anchorSide == wxRIGHT )
2001 popupX = rightX;
2002 else if ( anchorSide == wxLEFT )
2003 popupX = leftX;
2004 else
2005 popupX = 0;
2006
2007 int showFlags = CanDeferShow;
2008
2009 if ( spaceBelow < szp.y )
2010 {
2011 popupY = scrPos.y - szp.y;
2012 showFlags |= ShowAbove;
2013 }
2014
2015 #if INSTALL_TOPLEV_HANDLER
2016 // Put top level window event handler into place
2017 if ( m_popupWinType == POPUPWIN_WXPOPUPWINDOW )
2018 {
2019 if ( !m_toplevEvtHandler )
2020 m_toplevEvtHandler = new wxComboFrameEventHandler(this);
2021
2022 wxWindow* toplev = ::wxGetTopLevelParent( this );
2023 wxASSERT( toplev );
2024 ((wxComboFrameEventHandler*)m_toplevEvtHandler)->OnPopup();
2025 toplev->PushEventHandler( m_toplevEvtHandler );
2026 }
2027 #endif
2028
2029 // Set string selection (must be this way instead of SetStringSelection)
2030 if ( m_text )
2031 {
2032 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2033 m_text->SelectAll();
2034
2035 m_popupInterface->SetStringValue( m_text->GetValue() );
2036 }
2037 else
2038 {
2039 // This is neede since focus/selection indication may change when popup is shown
2040 Refresh();
2041 }
2042
2043 // This must be after SetStringValue
2044 m_popupWinState = Animating;
2045
2046 wxRect popupWinRect( popupX, popupY, szp.x, szp.y );
2047
2048 m_popup = popup;
2049 if ( (m_iFlags & wxCC_IFLAG_DISABLE_POPUP_ANIM) ||
2050 AnimateShow( popupWinRect, showFlags ) )
2051 {
2052 DoShowPopup( popupWinRect, showFlags );
2053 }
2054 }
2055
2056 bool wxComboCtrlBase::AnimateShow( const wxRect& WXUNUSED(rect), int WXUNUSED(flags) )
2057 {
2058 return true;
2059 }
2060
2061 void wxComboCtrlBase::DoShowPopup( const wxRect& rect, int WXUNUSED(flags) )
2062 {
2063 wxWindow* winPopup = m_winPopup;
2064
2065 if ( IsPopupWindowState(Animating) )
2066 {
2067 // Make sure the popup window is shown in the right position.
2068 // Should not matter even if animation already did this.
2069
2070 // Some platforms (GTK) may like SetSize and Move to be separate
2071 // (though the bug was probably fixed).
2072 winPopup->SetSize( rect );
2073
2074 #if USES_WXPOPUPTRANSIENTWINDOW
2075 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2076 ((wxPopupTransientWindow*)winPopup)->Popup(m_popup);
2077 else
2078 #endif
2079 winPopup->Show();
2080
2081 m_popupWinState = Visible;
2082 }
2083 else if ( IsPopupWindowState(Hidden) )
2084 {
2085 // Animation was aborted
2086
2087 wxASSERT( !winPopup->IsShown() );
2088
2089 m_popupWinState = Hidden;
2090 }
2091
2092 Refresh();
2093 }
2094
2095 void wxComboCtrlBase::OnPopupDismiss()
2096 {
2097 // Just in case, avoid double dismiss
2098 if ( IsPopupWindowState(Hidden) )
2099 return;
2100
2101 // This must be set before focus - otherwise there will be recursive
2102 // OnPopupDismisses.
2103 m_popupWinState = Hidden;
2104
2105 //SetFocus();
2106 m_winPopup->Disable();
2107
2108 // Inform popup control itself
2109 m_popupInterface->OnDismiss();
2110
2111 if ( m_popupExtraHandler )
2112 ((wxComboPopupExtraEventHandler*)m_popupExtraHandler)->OnPopupDismiss();
2113
2114 #if INSTALL_TOPLEV_HANDLER
2115 // Remove top level window event handler
2116 if ( m_toplevEvtHandler )
2117 {
2118 wxWindow* toplev = ::wxGetTopLevelParent( this );
2119 if ( toplev )
2120 toplev->RemoveEventHandler( m_toplevEvtHandler );
2121 }
2122 #endif
2123
2124 m_timeCanAcceptClick = ::wxGetLocalTimeMillis();
2125
2126 if ( m_popupWinType == POPUPWIN_WXPOPUPTRANSIENTWINDOW )
2127 m_timeCanAcceptClick += 150;
2128
2129 // If cursor not on dropdown button, then clear its state
2130 // (technically not required by all ports, but do it for all just in case)
2131 if ( !m_btnArea.Contains(ScreenToClient(::wxGetMousePosition())) )
2132 m_btnState = 0;
2133
2134 // Return parent's tab traversal flag.
2135 // See ShowPopup for notes.
2136 if ( m_iFlags & wxCC_IFLAG_PARENT_TAB_TRAVERSAL )
2137 {
2138 wxWindow* parent = GetParent();
2139 parent->SetWindowStyle( parent->GetWindowStyle() | wxTAB_TRAVERSAL );
2140 m_iFlags &= ~(wxCC_IFLAG_PARENT_TAB_TRAVERSAL);
2141 }
2142
2143 // refresh control (necessary even if m_text)
2144 Refresh();
2145
2146 SetFocus();
2147 }
2148
2149 void wxComboCtrlBase::HidePopup()
2150 {
2151 // Should be able to call this without popup interface
2152 if ( IsPopupWindowState(Hidden) )
2153 return;
2154
2155 // transfer value and show it in textctrl, if any
2156 if ( !IsPopupWindowState(Animating) )
2157 SetValue( m_popupInterface->GetStringValue() );
2158
2159 m_winPopup->Hide();
2160
2161 OnPopupDismiss();
2162 }
2163
2164 // ----------------------------------------------------------------------------
2165 // customization methods
2166 // ----------------------------------------------------------------------------
2167
2168 void wxComboCtrlBase::SetButtonPosition( int width, int height,
2169 int side, int spacingX )
2170 {
2171 m_btnWid = width;
2172 m_btnHei = height;
2173 m_btnSide = side;
2174 m_btnSpacingX = spacingX;
2175
2176 RecalcAndRefresh();
2177 }
2178
2179 wxSize wxComboCtrlBase::GetButtonSize()
2180 {
2181 if ( m_btnSize.x > 0 )
2182 return m_btnSize;
2183
2184 wxSize retSize(m_btnWid,m_btnHei);
2185
2186 // Need to call CalculateAreas now if button size is
2187 // is not explicitly specified.
2188 if ( retSize.x <= 0 || retSize.y <= 0)
2189 {
2190 OnResize();
2191
2192 retSize = m_btnSize;
2193 }
2194
2195 return retSize;
2196 }
2197
2198 void wxComboCtrlBase::SetButtonBitmaps( const wxBitmap& bmpNormal,
2199 bool blankButtonBg,
2200 const wxBitmap& bmpPressed,
2201 const wxBitmap& bmpHover,
2202 const wxBitmap& bmpDisabled )
2203 {
2204 m_bmpNormal = bmpNormal;
2205 m_blankButtonBg = blankButtonBg;
2206
2207 if ( bmpPressed.Ok() )
2208 m_bmpPressed = bmpPressed;
2209 else
2210 m_bmpPressed = bmpNormal;
2211
2212 if ( bmpHover.Ok() )
2213 m_bmpHover = bmpHover;
2214 else
2215 m_bmpHover = bmpNormal;
2216
2217 if ( bmpDisabled.Ok() )
2218 m_bmpDisabled = bmpDisabled;
2219 else
2220 m_bmpDisabled = bmpNormal;
2221
2222 RecalcAndRefresh();
2223 }
2224
2225 void wxComboCtrlBase::SetCustomPaintWidth( int width )
2226 {
2227 if ( m_text )
2228 {
2229 // move textctrl accordingly
2230 wxRect r = m_text->GetRect();
2231 int inc = width - m_widthCustomPaint;
2232 r.x += inc;
2233 r.width -= inc;
2234 m_text->SetSize( r );
2235 }
2236
2237 m_widthCustomPaint = width;
2238
2239 RecalcAndRefresh();
2240 }
2241
2242 bool wxComboCtrlBase::DoSetMargins(const wxPoint& margins)
2243 {
2244 // For general sanity's sake, we ignore top margin. Instead
2245 // we will always try to center the text vertically.
2246 bool res = true;
2247
2248 if ( margins.x != -1 )
2249 {
2250 m_marginLeft = margins.x;
2251 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
2252 }
2253 else
2254 {
2255 m_marginLeft = GetNativeTextIndent();
2256 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
2257 }
2258
2259 if ( margins.y != -1 )
2260 {
2261 res = false;
2262 }
2263
2264 RecalcAndRefresh();
2265
2266 return res;
2267 }
2268
2269 wxPoint wxComboCtrlBase::DoGetMargins() const
2270 {
2271 return wxPoint(m_marginLeft, -1);
2272 }
2273
2274 #if WXWIN_COMPATIBILITY_2_6
2275 void wxComboCtrlBase::SetTextIndent( int indent )
2276 {
2277 if ( indent < 0 )
2278 {
2279 m_marginLeft = GetNativeTextIndent();
2280 m_iFlags &= ~(wxCC_IFLAG_LEFT_MARGIN_SET);
2281 }
2282 else
2283 {
2284 m_marginLeft = indent;
2285 m_iFlags |= wxCC_IFLAG_LEFT_MARGIN_SET;
2286 }
2287
2288 RecalcAndRefresh();
2289 }
2290
2291 wxCoord wxComboCtrlBase::GetTextIndent() const
2292 {
2293 return m_marginLeft;
2294 }
2295 #endif
2296
2297 wxCoord wxComboCtrlBase::GetNativeTextIndent() const
2298 {
2299 return DEFAULT_TEXT_INDENT;
2300 }
2301
2302 // ----------------------------------------------------------------------------
2303 // methods forwarded to wxTextCtrl
2304 // ----------------------------------------------------------------------------
2305
2306 wxString wxComboCtrlBase::GetValue() const
2307 {
2308 if ( m_text )
2309 return m_text->GetValue();
2310 return m_valueString;
2311 }
2312
2313 void wxComboCtrlBase::SetValueWithEvent(const wxString& value, bool withEvent)
2314 {
2315 if ( m_text )
2316 {
2317 if ( !withEvent )
2318 m_ignoreEvtText++;
2319
2320 m_text->SetValue(value);
2321
2322 if ( !(m_iFlags & wxCC_NO_TEXT_AUTO_SELECT) )
2323 m_text->SelectAll();
2324 }
2325
2326 // Since wxComboPopup may want to paint the combo as well, we need
2327 // to set the string value here (as well as sometimes in ShowPopup).
2328 if ( m_valueString != value )
2329 {
2330 m_valueString = value;
2331
2332 EnsurePopupControl();
2333
2334 if (m_popupInterface)
2335 m_popupInterface->SetStringValue(value);
2336 }
2337
2338 Refresh();
2339 }
2340
2341 void wxComboCtrlBase::SetValue(const wxString& value)
2342 {
2343 SetValueWithEvent(value, false);
2344 }
2345
2346 // In this SetValue variant wxComboPopup::SetStringValue is not called
2347 void wxComboCtrlBase::SetText(const wxString& value)
2348 {
2349 // Unlike in SetValue(), this must be called here or
2350 // the behaviour will no be consistent in readonlys.
2351 EnsurePopupControl();
2352
2353 m_valueString = value;
2354
2355 if ( m_text )
2356 {
2357 m_ignoreEvtText++;
2358 m_text->SetValue( value );
2359 }
2360
2361 Refresh();
2362 }
2363
2364 void wxComboCtrlBase::Copy()
2365 {
2366 if ( m_text )
2367 m_text->Copy();
2368 }
2369
2370 void wxComboCtrlBase::Cut()
2371 {
2372 if ( m_text )
2373 m_text->Cut();
2374 }
2375
2376 void wxComboCtrlBase::Paste()
2377 {
2378 if ( m_text )
2379 m_text->Paste();
2380 }
2381
2382 void wxComboCtrlBase::SetInsertionPoint(long pos)
2383 {
2384 if ( m_text )
2385 m_text->SetInsertionPoint(pos);
2386 }
2387
2388 void wxComboCtrlBase::SetInsertionPointEnd()
2389 {
2390 if ( m_text )
2391 m_text->SetInsertionPointEnd();
2392 }
2393
2394 long wxComboCtrlBase::GetInsertionPoint() const
2395 {
2396 if ( m_text )
2397 return m_text->GetInsertionPoint();
2398
2399 return 0;
2400 }
2401
2402 long wxComboCtrlBase::GetLastPosition() const
2403 {
2404 if ( m_text )
2405 return m_text->GetLastPosition();
2406
2407 return 0;
2408 }
2409
2410 void wxComboCtrlBase::Replace(long from, long to, const wxString& value)
2411 {
2412 if ( m_text )
2413 m_text->Replace(from, to, value);
2414 }
2415
2416 void wxComboCtrlBase::Remove(long from, long to)
2417 {
2418 if ( m_text )
2419 m_text->Remove(from, to);
2420 }
2421
2422 void wxComboCtrlBase::SetSelection(long from, long to)
2423 {
2424 if ( m_text )
2425 m_text->SetSelection(from, to);
2426 }
2427
2428 void wxComboCtrlBase::Undo()
2429 {
2430 if ( m_text )
2431 m_text->Undo();
2432 }
2433
2434 #endif // wxUSE_COMBOCTRL