1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/combo.cpp
3 // Purpose: wxMSW wxComboCtrl
4 // Author: Jaakko Salli
6 // Created: Apr-30-2006
8 // Copyright: (c) 2005 Jaakko Salli
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #include "wx/wxprec.h"
30 #include "wx/combobox.h"
31 #include "wx/dcclient.h"
32 #include "wx/settings.h"
33 #include "wx/dialog.h"
34 #include "wx/stopwatch.h"
37 #include "wx/dcbuffer.h"
40 #include "wx/msw/registry.h"
42 #include "wx/msw/uxtheme.h"
44 #include "wx/msw/dc.h"
46 // Change to #if 1 to include tmschema.h for easier testing of theme
52 //----------------------------------
56 #define ETS_SELECTED 3
57 #define ETS_DISABLED 4
59 #define ETS_READONLY 6
61 #define TMT_FILLCOLOR 3802
62 #define TMT_TEXTCOLOR 3803
63 #define TMT_BORDERCOLOR 3801
64 #define TMT_EDGEFILLCOLOR 3808
65 #define TMT_BGTYPE 4001
67 #define BT_IMAGEFILE 0
68 #define BT_BORDERFILL 1
70 #define CP_DROPDOWNBUTTON 1
71 #define CP_BACKGROUND 2 // This and above are Vista and later only
72 #define CP_TRANSPARENTBACKGROUND 3
75 #define CP_DROPDOWNBUTTONRIGHT 6
76 #define CP_DROPDOWNBUTTONLEFT 7
77 #define CP_CUEBANNER 8
81 #define CBXS_PRESSED 3
82 #define CBXS_DISABLED 4
84 #define CBXSR_NORMAL 1
86 #define CBXSR_PRESSED 3
87 #define CBXSR_DISABLED 4
89 #define CBXSL_NORMAL 1
91 #define CBXSL_PRESSED 3
92 #define CBXSL_DISABLED 4
94 #define CBTBS_NORMAL 1
96 #define CBTBS_DISABLED 3
97 #define CBTBS_FOCUSED 4
101 #define CBB_FOCUSED 3
102 #define CBB_DISABLED 4
104 #define CBRO_NORMAL 1
106 #define CBRO_PRESSED 3
107 #define CBRO_DISABLED 4
109 #define CBCB_NORMAL 1
111 #define CBCB_PRESSED 3
112 #define CBCB_DISABLED 4
117 #define NATIVE_TEXT_INDENT_XP 4
118 #define NATIVE_TEXT_INDENT_CLASSIC 2
120 #define TEXTCTRLYADJUST_XP 3
121 #define TEXTCTRLYADJUST_CLASSIC 3
123 #define COMBOBOX_ANIMATION_RESOLUTION 10
125 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
127 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<2)
130 // ============================================================================
132 // ============================================================================
135 BEGIN_EVENT_TABLE(wxComboCtrl
, wxComboCtrlBase
)
136 EVT_PAINT(wxComboCtrl::OnPaintEvent
)
137 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent
)
138 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
139 EVT_TIMER(wxID_ANY
, wxComboCtrl::OnTimerEvent
)
144 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxComboCtrlBase
)
146 void wxComboCtrl::Init()
150 bool wxComboCtrl::Create(wxWindow
*parent
,
152 const wxString
& value
,
156 const wxValidator
& validator
,
157 const wxString
& name
)
161 long border
= style
& wxBORDER_MASK
;
164 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
172 // For XP, have 1-width custom border, for older version use sunken
173 border
= wxBORDER_NONE
;
174 m_widthCustomBorder
= 1;
178 border
= wxBORDER_SUNKEN
;
180 style
= (style
& ~(wxBORDER_MASK
)) | border
;
183 // create main window
184 if ( !wxComboCtrlBase::Create(parent
,
189 style
| wxFULL_REPAINT_ON_RESIZE
,
197 if ( ::wxGetWinVersion() >= wxWinVersion_Vista
)
198 m_iFlags
|= wxCC_BUTTON_STAYS_DOWN
|wxCC_BUTTON_COVERS_BORDER
;
202 if ( style
& wxCC_STD_BUTTON
)
203 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
205 // Create textctrl, if necessary
206 CreateTextCtrl( wxNO_BORDER
, validator
);
208 // Add keyboard input handlers for main control and textctrl
209 InstallInputHandlers();
211 // Prepare background for double-buffering
212 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
214 // SetInitialSize should be called last
215 SetInitialSize(size
);
220 wxComboCtrl::~wxComboCtrl()
224 void wxComboCtrl::OnThemeChange()
226 // there doesn't seem to be any way to get the text colour using themes
227 // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX
230 wxColour fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
231 SetForegroundColour(fgCol
);
235 wxColour bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
238 wxUxThemeEngine
* const theme
= wxUxThemeEngine::GetIfActive();
241 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
242 wxUxThemeHandle
hTheme(this, L
"EDIT");
244 HRESULT hr
= theme
->GetThemeColor
254 bgCol
= wxRGBToColour(col
);
258 wxLogApiError("GetThemeColor(EDIT, ETS_NORMAL, TMT_FILLCOLOR)",
266 SetBackgroundColour(bgCol
);
271 void wxComboCtrl::OnResize()
274 // Recalculates button and textctrl areas
279 if ( wxUxThemeEngine::GetIfActive() )
281 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
286 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
289 // Technically Classic Windows style combo has more narrow button,
290 // but the native renderer doesn't paint it well like that.
292 CalculateAreas(btnWidth
);
294 // Position textctrl using standard routine
295 PositionTextCtrl(0, textCtrlYAdjust
);
298 // Draws non-XP GUI dotted line around the focus area
299 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
301 #if !defined(__WXWINCE__)
304 mswRect.left = rect.x;
305 mswRect.top = rect.y;
306 mswRect.right = rect.x + rect.width;
307 mswRect.bottom = rect.y + rect.height;
308 HDC hdc = (HDC) dc.GetHDC();
309 SetMapMode(hdc,MM_TEXT); // Just in case...
310 DrawFocusRect(hdc,&mswRect);
312 // FIXME: Use DrawFocusRect code above (currently it draws solid line
313 // for caption focus but works ok for other stuff).
314 // Also, this code below may not work in future wx versions, since
315 // it employs wxCAP_BUTT hack to have line of width 1.
316 dc
.SetLogicalFunction(wxINVERT
);
318 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
319 pen
.SetCap(wxCAP_BUTT
);
321 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
323 dc
.DrawRectangle(rect
);
325 dc
.SetLogicalFunction(wxCOPY
);
327 dc
.SetLogicalFunction(wxINVERT
);
329 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
330 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
332 dc
.DrawRectangle(rect
);
334 dc
.SetLogicalFunction(wxCOPY
);
338 // draw focus background on area in a way typical on platform
340 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
343 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
346 wxSize sz
= GetClientSize();
348 bool doDrawFocusRect
; // also selected
350 // For smaller size control (and for disabled background) use less spacing
354 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
357 isEnabled
= IsEnabled();
358 doDrawFocusRect
= ShouldDrawFocus();
361 // Windows-style: for smaller size control (and for disabled background) use less spacing
365 focusSpacingX
= isEnabled
? 2 : 1;
366 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
386 // Drawing a list item
387 isEnabled
= true; // they are never disabled
388 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
394 // Set the background sub-rectangle for selection, disabled etc
395 wxRect
selRect(rect
);
396 selRect
.y
+= focusSpacingY
;
397 selRect
.height
-= (focusSpacingY
*2);
401 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
402 wcp
+= m_widthCustomPaint
;
404 selRect
.x
+= wcp
+ focusSpacingX
;
405 selRect
.width
-= wcp
+ (focusSpacingX
*2);
407 //wxUxThemeEngine* theme = NULL;
409 // theme = wxUxThemeEngine::GetIfActive();
413 bool doDrawDottedEdge
= false;
414 bool doDrawSelRect
= true;
416 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
417 // (and other cases which are not that apparent).
421 // If popup is hidden and this control is focused,
422 // then draw the focus-indicator (selbgcolor background etc.).
423 if ( doDrawFocusRect
)
425 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
426 // it is not properly defined for combo boxes. Instead, they expect
427 // you to use DrawThemeText.
429 // Here is, however, sample code how to get theme colours:
432 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
433 // dc.SetTextForeground( wxRGBToColour(cref) );
434 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
436 // Vista style read-only combo
437 fgCol
= GetForegroundColour();
438 bgCol
= GetBackgroundColour();
439 doDrawSelRect
= false;
440 doDrawDottedEdge
= true;
444 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
445 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
450 fgCol
= GetForegroundColour();
451 bgCol
= GetBackgroundColour();
452 doDrawSelRect
= false;
457 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
458 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
461 dc
.SetTextForeground(fgCol
);
466 dc
.DrawRectangle(selRect
);
469 if ( doDrawDottedEdge
)
470 wxMSWDrawFocusRect(dc
, selRect
);
472 // Don't clip exactly to the selection rectangle so we can draw
473 // to the non-selected area in front of it.
474 wxRect
clipRect(rect
.x
,rect
.y
,
475 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
476 dc
.SetClippingRegion(clipRect
);
479 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
481 // TODO: Convert drawing in this function to Windows API Code
483 wxSize sz
= GetClientSize();
484 wxAutoBufferedPaintDC
dc(this);
486 const wxRect
& rectButton
= m_btnArea
;
487 wxRect rectTextField
= m_tcArea
;
488 wxColour bgCol
= GetBackgroundColour();
491 const bool isEnabled
= IsEnabled();
493 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
494 HDC hDc
= GetHdcOf(*impl
);
495 HWND hWnd
= GetHwndOf(this);
497 wxUxThemeEngine
* theme
= NULL
;
498 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
501 theme
= wxUxThemeEngine::GetIfActive();
502 #endif // wxUSE_UXTHEME
504 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
506 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
508 borderRect
= m_tcArea
;
509 borderRect
.Inflate(1);
512 int drawButFlags
= 0;
517 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
520 wxCopyRectToRECT(borderRect
, rFull
);
523 wxCopyRectToRECT(rectButton
, rButton
);
526 wxCopyRectToRECT(borderRect
, rBorder
);
528 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
529 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
532 // Get some states for themed drawing
537 butState
= CBXS_DISABLED
;
539 // Vista will display the drop-button as depressed always
540 // when the popup window is visilbe
541 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
542 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
544 butState
= CBXS_PRESSED
;
546 else if ( m_btnState
& wxCONTROL_CURRENT
)
552 butState
= CBXS_NORMAL
;
555 int comboBoxPart
= 0; // For XP, use the 'default' part
556 RECT
* rUseForBg
= &rBorder
;
558 bool drawFullButton
= false;
559 int bgState
= butState
;
560 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
562 if ( useVistaComboBox
)
564 // FIXME: Either SetBackgroundColour or GetBackgroundColour
565 // doesn't work under Vista, so here's a temporary
567 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
569 // Draw the entire control as a single button?
570 if ( !isNonStdButton
)
572 if ( HasFlag(wxCB_READONLY
) )
573 drawFullButton
= true;
576 if ( drawFullButton
)
578 comboBoxPart
= CP_READONLY
;
581 // It should be safe enough to update this flag here.
582 m_iFlags
|= wxCC_FULL_BUTTON
;
586 comboBoxPart
= CP_BORDER
;
587 m_iFlags
&= ~wxCC_FULL_BUTTON
;
590 bgState
= CBB_FOCUSED
;
592 bgState
= CBB_NORMAL
;
597 // Draw parent's background, if necessary
598 RECT
* rUseForTb
= NULL
;
600 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
602 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
603 rUseForTb
= &rButton
;
606 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
609 // Draw the control background (including the border)
610 if ( m_widthCustomBorder
> 0 )
612 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
616 // No border. We can't use theme, since it cannot be relied on
617 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
620 dc
.DrawRectangle(borderRect
);
624 // Draw the drop-button
625 if ( !isNonStdButton
)
627 drawButFlags
= Button_BitmapOnly
;
629 int butPart
= CP_DROPDOWNBUTTON
;
631 if ( useVistaComboBox
)
633 if ( drawFullButton
)
635 // We need to alter the button style slightly before
636 // drawing the actual button (but it was good above
637 // when background etc was done).
638 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
639 butState
= CBXS_NORMAL
;
642 if ( m_btnSide
== wxRIGHT
)
643 butPart
= CP_DROPDOWNBUTTONRIGHT
;
645 butPart
= CP_DROPDOWNBUTTONLEFT
;
648 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
650 else if ( useVistaComboBox
&&
651 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
653 // We'll do this, because DrawThemeParentBackground
654 // doesn't seem to be reliable on Vista.
655 drawButFlags
|= Button_PaintBackground
;
661 // Windows 2000 and earlier
662 drawButFlags
= Button_PaintBackground
;
666 dc
.DrawRectangle(borderRect
);
669 // Button rendering (may only do the bitmap on button, depending on the flags)
670 DrawButton( dc
, rectButton
, drawButFlags
);
672 // Paint required portion of the custom image on the control
673 if ( (!m_text
|| m_widthCustomPaint
) )
675 wxASSERT( m_widthCustomPaint
>= 0 );
677 // this is intentionally here to allow drawed rectangle's
678 // right edge to be hidden
680 rectTextField
.width
= m_widthCustomPaint
;
682 dc
.SetFont( GetFont() );
684 dc
.SetClippingRegion(rectTextField
);
685 if ( m_popupInterface
)
686 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
688 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
692 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
695 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
696 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
698 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
701 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
703 // if no textctrl and no special double-click, then the entire control acts
705 handlerFlags
|= wxCC_MF_ON_BUTTON
;
706 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
711 if ( isOnButtonArea
|| HasCapture() ||
712 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
714 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
716 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
719 else if ( m_btnState
)
721 // otherwise need to clear the hover status
723 RefreshRect(m_btnArea
);
728 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
729 // See header file for further information on this method.
730 HandleNormalMouseEvent(event
);
734 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
735 static wxUint32
GetUserPreferencesMask()
737 static wxUint32 userPreferencesMask
= 0;
738 static bool valueSet
= false;
741 return userPreferencesMask
;
743 wxRegKey
* pKey
= NULL
;
744 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
745 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
746 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
750 else if ( key2
.Exists() )
752 else if ( key3
.Exists() )
755 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
758 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
759 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
761 if ( buf
.GetDataLen() >= 4 )
763 wxUint32
* p
= (wxUint32
*) buf
.GetData();
764 userPreferencesMask
= *p
;
771 return userPreferencesMask
;
775 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
776 void wxComboCtrl::DoTimerEvent()
778 bool stopTimer
= false;
780 wxWindow
* popup
= GetPopupControl()->GetControl();
782 // Popup was hidden before it was fully shown?
783 if ( IsPopupWindowState(Hidden
) )
789 wxLongLong t
= ::wxGetLocalTimeMillis();
790 const wxRect
& rect
= m_animRect
;
791 wxWindow
* win
= GetPopupWindow();
793 int pos
= (int) (t
-m_animStart
).GetLo();
794 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
796 int height
= rect
.height
;
797 //int h0 = rect.height;
798 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
799 int y
= (height
- h
);
803 if ( m_animFlags
& ShowAbove
)
805 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
809 popup
->Move( 0, -y
);
810 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
823 DoShowPopup( m_animRect
, m_animFlags
);
828 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
829 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
831 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
833 m_animStart
= ::wxGetLocalTimeMillis();
837 wxWindow
* win
= GetPopupWindow();
838 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
841 m_animTimer
.SetOwner( this, wxID_ANY
);
842 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
853 wxCoord
wxComboCtrl::GetNativeTextIndent() const
856 if ( wxUxThemeEngine::GetIfActive() )
857 return NATIVE_TEXT_INDENT_XP
;
859 return NATIVE_TEXT_INDENT_CLASSIC
;
862 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
864 const bool isPopupShown
= IsPopupShown();
866 switch ( event
.GetKeyCode() )
869 // F4 toggles the popup in the native comboboxes, so emulate them
870 if ( !event
.AltDown() )
881 // On XP or with writable combo in Classic, arrows don't open the
882 // popup but Alt-arrow does
883 if ( event
.AltDown() ||
885 HasFlag(wxCB_READONLY
)
887 && !wxUxThemeEngine::GetIfActive()
899 #endif // wxUSE_COMBOCTRL