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 TEXTCTRLXADJUST_XP 1
121 #define TEXTCTRLYADJUST_XP 3
122 #define TEXTCTRLXADJUST_CLASSIC 1
123 #define TEXTCTRLYADJUST_CLASSIC 3
125 #define COMBOBOX_ANIMATION_RESOLUTION 10
127 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
129 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<2)
132 // ============================================================================
134 // ============================================================================
137 BEGIN_EVENT_TABLE(wxComboCtrl
, wxComboCtrlBase
)
138 EVT_PAINT(wxComboCtrl::OnPaintEvent
)
139 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent
)
140 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
141 EVT_TIMER(wxID_ANY
, wxComboCtrl::OnTimerEvent
)
146 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxComboCtrlBase
)
148 void wxComboCtrl::Init()
152 bool wxComboCtrl::Create(wxWindow
*parent
,
154 const wxString
& value
,
158 const wxValidator
& validator
,
159 const wxString
& name
)
163 long border
= style
& wxBORDER_MASK
;
166 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
174 // For XP, have 1-width custom border, for older version use sunken
175 border
= wxBORDER_NONE
;
176 m_widthCustomBorder
= 1;
180 border
= wxBORDER_SUNKEN
;
182 style
= (style
& ~(wxBORDER_MASK
)) | border
;
185 // create main window
186 if ( !wxComboCtrlBase::Create(parent
,
191 style
| wxFULL_REPAINT_ON_RESIZE
,
199 if ( ::wxGetWinVersion() >= wxWinVersion_Vista
)
200 m_iFlags
|= wxCC_BUTTON_STAYS_DOWN
|wxCC_BUTTON_COVERS_BORDER
;
204 if ( style
& wxCC_STD_BUTTON
)
205 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
207 // Create textctrl, if necessary
208 CreateTextCtrl( wxNO_BORDER
, validator
);
210 // Add keyboard input handlers for main control and textctrl
211 InstallInputHandlers();
213 // Prepare background for double-buffering
214 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
216 // SetInitialSize should be called last
217 SetInitialSize(size
);
222 wxComboCtrl::~wxComboCtrl()
226 void wxComboCtrl::OnThemeChange()
228 // there doesn't seem to be any way to get the text colour using themes
229 // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX
230 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
233 wxUxThemeEngine
* const theme
= wxUxThemeEngine::GetIfActive();
236 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
237 wxUxThemeHandle
hTheme(this, L
"EDIT");
239 HRESULT hr
= theme
->GetThemeColor
249 SetBackgroundColour(wxRGBToColour(col
));
251 // skip the call below
255 wxLogApiError(_T("GetThemeColor(EDIT, ETS_NORMAL, TMT_FILLCOLOR)"), hr
);
259 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
262 void wxComboCtrl::OnResize()
265 // Recalculates button and textctrl areas
271 if ( wxUxThemeEngine::GetIfActive() )
273 textCtrlXAdjust
= TEXTCTRLXADJUST_XP
;
274 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
279 textCtrlXAdjust
= TEXTCTRLXADJUST_CLASSIC
;
280 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
283 // Technically Classic Windows style combo has more narrow button,
284 // but the native renderer doesn't paint it well like that.
286 CalculateAreas(btnWidth
);
288 // Position textctrl using standard routine
289 PositionTextCtrl(textCtrlXAdjust
,textCtrlYAdjust
);
292 // Draws non-XP GUI dotted line around the focus area
293 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
295 #if !defined(__WXWINCE__)
298 mswRect.left = rect.x;
299 mswRect.top = rect.y;
300 mswRect.right = rect.x + rect.width;
301 mswRect.bottom = rect.y + rect.height;
302 HDC hdc = (HDC) dc.GetHDC();
303 SetMapMode(hdc,MM_TEXT); // Just in case...
304 DrawFocusRect(hdc,&mswRect);
306 // FIXME: Use DrawFocusRect code above (currently it draws solid line
307 // for caption focus but works ok for other stuff).
308 // Also, this code below may not work in future wx versions, since
309 // it employs wxCAP_BUTT hack to have line of width 1.
310 dc
.SetLogicalFunction(wxINVERT
);
312 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
313 pen
.SetCap(wxCAP_BUTT
);
315 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
317 dc
.DrawRectangle(rect
);
319 dc
.SetLogicalFunction(wxCOPY
);
321 dc
.SetLogicalFunction(wxINVERT
);
323 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
324 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
326 dc
.DrawRectangle(rect
);
328 dc
.SetLogicalFunction(wxCOPY
);
332 // draw focus background on area in a way typical on platform
334 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
337 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
340 wxSize sz
= GetClientSize();
342 bool doDrawFocusRect
; // also selected
344 // For smaller size control (and for disabled background) use less spacing
348 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
351 isEnabled
= IsEnabled();
352 doDrawFocusRect
= ShouldDrawFocus();
355 // Windows-style: for smaller size control (and for disabled background) use less spacing
359 focusSpacingX
= isEnabled
? 2 : 1;
360 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
380 // Drawing a list item
381 isEnabled
= true; // they are never disabled
382 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
388 // Set the background sub-rectangle for selection, disabled etc
389 wxRect
selRect(rect
);
390 selRect
.y
+= focusSpacingY
;
391 selRect
.height
-= (focusSpacingY
*2);
395 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
396 wcp
+= m_widthCustomPaint
;
398 selRect
.x
+= wcp
+ focusSpacingX
;
399 selRect
.width
-= wcp
+ (focusSpacingX
*2);
401 //wxUxThemeEngine* theme = NULL;
403 // theme = wxUxThemeEngine::GetIfActive();
406 bool doDrawDottedEdge
= false;
407 bool doDrawSelRect
= true;
409 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
410 // (and other cases which are not that apparent).
414 // If popup is hidden and this control is focused,
415 // then draw the focus-indicator (selbgcolor background etc.).
416 if ( doDrawFocusRect
)
418 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
419 // it is not properly defined for combo boxes. Instead, they expect
420 // you to use DrawThemeText.
422 // Here is, however, sample code how to get theme colours:
425 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
426 // dc.SetTextForeground( wxRGBToColour(cref) );
427 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
429 // Vista style read-only combo
430 doDrawSelRect
= false;
431 doDrawDottedEdge
= true;
435 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
436 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
441 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
442 bgCol
= GetBackgroundColour();
443 doDrawSelRect
= false;
448 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
449 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
456 dc
.DrawRectangle(selRect
);
459 if ( doDrawDottedEdge
)
460 wxMSWDrawFocusRect(dc
, selRect
);
462 // Don't clip exactly to the selection rectangle so we can draw
463 // to the non-selected area in front of it.
464 wxRect
clipRect(rect
.x
,rect
.y
,
465 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
466 dc
.SetClippingRegion(clipRect
);
469 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
471 // TODO: Convert drawing in this function to Windows API Code
473 wxSize sz
= GetClientSize();
474 wxAutoBufferedPaintDC
dc(this);
476 const wxRect
& rectButton
= m_btnArea
;
477 wxRect rectTextField
= m_tcArea
;
478 wxColour bgCol
= GetBackgroundColour();
481 const bool isEnabled
= IsEnabled();
483 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
484 HDC hDc
= GetHdcOf(*impl
);
485 HWND hWnd
= GetHwndOf(this);
487 wxUxThemeEngine
* theme
= NULL
;
488 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
491 theme
= wxUxThemeEngine::GetIfActive();
492 #endif // wxUSE_UXTHEME
494 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
496 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
498 borderRect
= m_tcArea
;
499 borderRect
.Inflate(1);
502 int drawButFlags
= 0;
507 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
510 wxCopyRectToRECT(borderRect
, rFull
);
513 wxCopyRectToRECT(rectButton
, rButton
);
516 wxCopyRectToRECT(borderRect
, rBorder
);
518 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
519 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
522 // Get some states for themed drawing
527 butState
= CBXS_DISABLED
;
529 // Vista will display the drop-button as depressed always
530 // when the popup window is visilbe
531 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
532 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
534 butState
= CBXS_PRESSED
;
536 else if ( m_btnState
& wxCONTROL_CURRENT
)
542 butState
= CBXS_NORMAL
;
545 int comboBoxPart
= 0; // For XP, use the 'default' part
546 RECT
* rUseForBg
= &rBorder
;
548 bool drawFullButton
= false;
549 int bgState
= butState
;
550 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
552 if ( useVistaComboBox
)
554 // FIXME: Either SetBackgroundColour or GetBackgroundColour
555 // doesn't work under Vista, so here's a temporary
557 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
559 // Draw the entire control as a single button?
560 if ( !isNonStdButton
)
562 if ( HasFlag(wxCB_READONLY
) )
563 drawFullButton
= true;
566 if ( drawFullButton
)
568 comboBoxPart
= CP_READONLY
;
571 // It should be safe enough to update this flag here.
572 m_iFlags
|= wxCC_FULL_BUTTON
;
576 comboBoxPart
= CP_BORDER
;
577 m_iFlags
&= ~wxCC_FULL_BUTTON
;
580 bgState
= CBB_FOCUSED
;
582 bgState
= CBB_NORMAL
;
587 // Draw parent's background, if necessary
588 RECT
* rUseForTb
= NULL
;
590 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
592 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
593 rUseForTb
= &rButton
;
596 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
599 // Draw the control background (including the border)
600 if ( m_widthCustomBorder
> 0 )
602 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
606 // No border. We can't use theme, since it cannot be relied on
607 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
610 dc
.DrawRectangle(borderRect
);
614 // Draw the drop-button
615 if ( !isNonStdButton
)
617 drawButFlags
= Button_BitmapOnly
;
619 int butPart
= CP_DROPDOWNBUTTON
;
621 if ( useVistaComboBox
)
623 if ( drawFullButton
)
625 // We need to alter the button style slightly before
626 // drawing the actual button (but it was good above
627 // when background etc was done).
628 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
629 butState
= CBXS_NORMAL
;
632 if ( m_btnSide
== wxRIGHT
)
633 butPart
= CP_DROPDOWNBUTTONRIGHT
;
635 butPart
= CP_DROPDOWNBUTTONLEFT
;
638 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
640 else if ( useVistaComboBox
&&
641 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
643 // We'll do this, because DrawThemeParentBackground
644 // doesn't seem to be reliable on Vista.
645 drawButFlags
|= Button_PaintBackground
;
651 // Windows 2000 and earlier
652 drawButFlags
= Button_PaintBackground
;
656 dc
.DrawRectangle(borderRect
);
659 // Button rendering (may only do the bitmap on button, depending on the flags)
660 DrawButton( dc
, rectButton
, drawButFlags
);
662 // Paint required portion of the custom image on the control
663 if ( (!m_text
|| m_widthCustomPaint
) )
665 wxASSERT( m_widthCustomPaint
>= 0 );
667 // this is intentionally here to allow drawed rectangle's
668 // right edge to be hidden
670 rectTextField
.width
= m_widthCustomPaint
;
672 dc
.SetFont( GetFont() );
674 dc
.SetClippingRegion(rectTextField
);
675 if ( m_popupInterface
)
676 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
678 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
682 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
685 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
686 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
688 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
691 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
693 // if no textctrl and no special double-click, then the entire control acts
695 handlerFlags
|= wxCC_MF_ON_BUTTON
;
696 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
701 if ( isOnButtonArea
|| HasCapture() ||
702 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
704 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
706 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
709 else if ( m_btnState
)
711 // otherwise need to clear the hover status
713 RefreshRect(m_btnArea
);
718 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
719 // See header file for further information on this method.
720 HandleNormalMouseEvent(event
);
724 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
725 static wxUint32
GetUserPreferencesMask()
727 static wxUint32 userPreferencesMask
= 0;
728 static bool valueSet
= false;
731 return userPreferencesMask
;
733 wxRegKey
* pKey
= NULL
;
734 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
735 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
736 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
740 else if ( key2
.Exists() )
742 else if ( key3
.Exists() )
745 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
748 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
749 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
751 if ( buf
.GetDataLen() >= 4 )
753 wxUint32
* p
= (wxUint32
*) buf
.GetData();
754 userPreferencesMask
= *p
;
761 return userPreferencesMask
;
765 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
766 void wxComboCtrl::DoTimerEvent()
768 bool stopTimer
= false;
770 wxWindow
* popup
= GetPopupControl()->GetControl();
772 // Popup was hidden before it was fully shown?
773 if ( IsPopupWindowState(Hidden
) )
779 wxLongLong t
= ::wxGetLocalTimeMillis();
780 const wxRect
& rect
= m_animRect
;
781 wxWindow
* win
= GetPopupWindow();
783 int pos
= (int) (t
-m_animStart
).GetLo();
784 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
786 int height
= rect
.height
;
787 //int h0 = rect.height;
788 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
789 int y
= (height
- h
);
793 if ( m_animFlags
& ShowAbove
)
795 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
799 popup
->Move( 0, -y
);
800 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
813 DoShowPopup( m_animRect
, m_animFlags
);
818 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
819 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
821 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
823 m_animStart
= ::wxGetLocalTimeMillis();
827 wxWindow
* win
= GetPopupWindow();
828 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
831 m_animTimer
.SetOwner( this, wxID_ANY
);
832 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
843 wxCoord
wxComboCtrl::GetNativeTextIndent() const
846 if ( wxUxThemeEngine::GetIfActive() )
847 return NATIVE_TEXT_INDENT_XP
;
849 return NATIVE_TEXT_INDENT_CLASSIC
;
852 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
854 const bool isPopupShown
= IsPopupShown();
856 switch ( event
.GetKeyCode() )
859 // F4 toggles the popup in the native comboboxes, so emulate them
860 if ( !event
.AltDown() )
871 // On XP or with writable combo in Classic, arrows don't open the
872 // popup but Alt-arrow does
873 if ( event
.AltDown() ||
875 HasFlag(wxCB_READONLY
)
877 && !wxUxThemeEngine::GetIfActive()
889 #endif // wxUSE_COMBOCTRL