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