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"
45 // Change to #if 1 to include tmschema.h for easier testing of theme
51 //----------------------------------
55 #define ETS_SELECTED 3
56 #define ETS_DISABLED 4
58 #define ETS_READONLY 6
60 #define TMT_FILLCOLOR 3802
61 #define TMT_TEXTCOLOR 3803
62 #define TMT_BORDERCOLOR 3801
63 #define TMT_EDGEFILLCOLOR 3808
64 #define TMT_BGTYPE 4001
66 #define BT_IMAGEFILE 0
67 #define BT_BORDERFILL 1
69 #define CP_DROPDOWNBUTTON 1
70 #define CP_BACKGROUND 2 // This and above are Vista and later only
71 #define CP_TRANSPARENTBACKGROUND 3
74 #define CP_DROPDOWNBUTTONRIGHT 6
75 #define CP_DROPDOWNBUTTONLEFT 7
76 #define CP_CUEBANNER 8
80 #define CBXS_PRESSED 3
81 #define CBXS_DISABLED 4
83 #define CBXSR_NORMAL 1
85 #define CBXSR_PRESSED 3
86 #define CBXSR_DISABLED 4
88 #define CBXSL_NORMAL 1
90 #define CBXSL_PRESSED 3
91 #define CBXSL_DISABLED 4
93 #define CBTBS_NORMAL 1
95 #define CBTBS_DISABLED 3
96 #define CBTBS_FOCUSED 4
100 #define CBB_FOCUSED 3
101 #define CBB_DISABLED 4
103 #define CBRO_NORMAL 1
105 #define CBRO_PRESSED 3
106 #define CBRO_DISABLED 4
108 #define CBCB_NORMAL 1
110 #define CBCB_PRESSED 3
111 #define CBCB_DISABLED 4
116 #define NATIVE_TEXT_INDENT_XP 4
117 #define NATIVE_TEXT_INDENT_CLASSIC 2
119 #define TEXTCTRLXADJUST_XP 1
120 #define TEXTCTRLYADJUST_XP 3
121 #define TEXTCTRLXADJUST_CLASSIC 1
122 #define TEXTCTRLYADJUST_CLASSIC 3
124 #define COMBOBOX_ANIMATION_RESOLUTION 10
126 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
128 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<2)
131 // ============================================================================
133 // ============================================================================
136 BEGIN_EVENT_TABLE(wxComboCtrl
, wxComboCtrlBase
)
137 EVT_PAINT(wxComboCtrl::OnPaintEvent
)
138 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent
)
139 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
140 EVT_TIMER(wxID_ANY
, wxComboCtrl::OnTimerEvent
)
145 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxComboCtrlBase
)
147 void wxComboCtrl::Init()
151 bool wxComboCtrl::Create(wxWindow
*parent
,
153 const wxString
& value
,
157 const wxValidator
& validator
,
158 const wxString
& name
)
162 long border
= style
& wxBORDER_MASK
;
165 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
173 // For XP, have 1-width custom border, for older version use sunken
174 border
= wxBORDER_NONE
;
175 m_widthCustomBorder
= 1;
179 border
= wxBORDER_SUNKEN
;
181 style
= (style
& ~(wxBORDER_MASK
)) | border
;
184 // create main window
185 if ( !wxComboCtrlBase::Create(parent
,
190 style
| wxFULL_REPAINT_ON_RESIZE
,
198 if ( ::wxGetWinVersion() >= wxWinVersion_Vista
)
199 m_iFlags
|= wxCC_BUTTON_STAYS_DOWN
|wxCC_BUTTON_COVERS_BORDER
;
203 if ( style
& wxCC_STD_BUTTON
)
204 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
206 // Create textctrl, if necessary
207 CreateTextCtrl( wxNO_BORDER
, validator
);
209 // Add keyboard input handlers for main control and textctrl
210 InstallInputHandlers();
212 // Prepare background for double-buffering
213 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
215 // SetInitialSize should be called last
216 SetInitialSize(size
);
221 wxComboCtrl::~wxComboCtrl()
225 void wxComboCtrl::OnThemeChange()
227 // there doesn't seem to be any way to get the text colour using themes
228 // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX
229 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
232 wxUxThemeEngine
* const theme
= wxUxThemeEngine::GetIfActive();
235 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
236 wxUxThemeHandle
hTheme(this, L
"EDIT");
238 HRESULT hr
= theme
->GetThemeColor
248 SetBackgroundColour(wxRGBToColour(col
));
250 // skip the call below
254 wxLogApiError(_T("GetThemeColor(EDIT, ETS_NORMAL, TMT_FILLCOLOR)"), hr
);
258 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
261 void wxComboCtrl::OnResize()
264 // Recalculates button and textctrl areas
270 if ( wxUxThemeEngine::GetIfActive() )
272 textCtrlXAdjust
= TEXTCTRLXADJUST_XP
;
273 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
278 textCtrlXAdjust
= TEXTCTRLXADJUST_CLASSIC
;
279 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
282 // Technically Classic Windows style combo has more narrow button,
283 // but the native renderer doesn't paint it well like that.
285 CalculateAreas(btnWidth
);
287 // Position textctrl using standard routine
288 PositionTextCtrl(textCtrlXAdjust
,textCtrlYAdjust
);
291 // Draws non-XP GUI dotted line around the focus area
292 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
294 #if !defined(__WXWINCE__)
297 mswRect.left = rect.x;
298 mswRect.top = rect.y;
299 mswRect.right = rect.x + rect.width;
300 mswRect.bottom = rect.y + rect.height;
301 HDC hdc = (HDC) dc.GetHDC();
302 SetMapMode(hdc,MM_TEXT); // Just in case...
303 DrawFocusRect(hdc,&mswRect);
305 // FIXME: Use DrawFocusRect code above (currently it draws solid line
306 // for caption focus but works ok for other stuff).
307 // Also, this code below may not work in future wx versions, since
308 // it employs wxCAP_BUTT hack to have line of width 1.
309 dc
.SetLogicalFunction(wxINVERT
);
311 wxPen
pen(*wxBLACK
,1,wxDOT
);
312 pen
.SetCap(wxCAP_BUTT
);
314 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
316 dc
.DrawRectangle(rect
);
318 dc
.SetLogicalFunction(wxCOPY
);
320 dc
.SetLogicalFunction(wxINVERT
);
322 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
323 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
325 dc
.DrawRectangle(rect
);
327 dc
.SetLogicalFunction(wxCOPY
);
331 // draw focus background on area in a way typical on platform
333 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
336 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
339 wxSize sz
= GetClientSize();
341 bool doDrawFocusRect
; // also selected
343 // For smaller size control (and for disabled background) use less spacing
347 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
350 isEnabled
= IsEnabled();
351 doDrawFocusRect
= ShouldDrawFocus();
354 // Windows-style: for smaller size control (and for disabled background) use less spacing
358 focusSpacingX
= isEnabled
? 2 : 1;
359 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
379 // Drawing a list item
380 isEnabled
= true; // they are never disabled
381 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
387 // Set the background sub-rectangle for selection, disabled etc
388 wxRect
selRect(rect
);
389 selRect
.y
+= focusSpacingY
;
390 selRect
.height
-= (focusSpacingY
*2);
394 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
395 wcp
+= m_widthCustomPaint
;
397 selRect
.x
+= wcp
+ focusSpacingX
;
398 selRect
.width
-= wcp
+ (focusSpacingX
*2);
400 //wxUxThemeEngine* theme = (wxUxThemeEngine*) NULL;
402 // theme = wxUxThemeEngine::GetIfActive();
405 bool doDrawDottedEdge
= false;
406 bool doDrawSelRect
= true;
408 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
409 // (and other cases which are not that apparent).
413 // If popup is hidden and this control is focused,
414 // then draw the focus-indicator (selbgcolor background etc.).
415 if ( doDrawFocusRect
)
417 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
418 // it is not properly defined for combo boxes. Instead, they expect
419 // you to use DrawThemeText.
421 // Here is, however, sample code how to get theme colours:
424 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
425 // dc.SetTextForeground( wxRGBToColour(cref) );
426 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
428 // Vista style read-only combo
429 doDrawSelRect
= false;
430 doDrawDottedEdge
= true;
434 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
435 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
440 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
441 bgCol
= GetBackgroundColour();
442 doDrawSelRect
= false;
447 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
448 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
455 dc
.DrawRectangle(selRect
);
458 if ( doDrawDottedEdge
)
459 wxMSWDrawFocusRect(dc
, selRect
);
461 // Don't clip exactly to the selection rectangle so we can draw
462 // to the non-selected area in front of it.
463 wxRect
clipRect(rect
.x
,rect
.y
,
464 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
465 dc
.SetClippingRegion(clipRect
);
468 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
470 // TODO: Convert drawing in this function to Windows API Code
472 wxSize sz
= GetClientSize();
473 wxAutoBufferedPaintDC
dc(this);
475 const wxRect
& rectButton
= m_btnArea
;
476 wxRect rectTextField
= m_tcArea
;
477 wxColour bgCol
= GetBackgroundColour();
480 const bool isEnabled
= IsEnabled();
482 HDC hDc
= GetHdcOf(dc
);
483 HWND hWnd
= GetHwndOf(this);
485 wxUxThemeEngine
* theme
= NULL
;
486 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
489 theme
= wxUxThemeEngine::GetIfActive();
490 #endif // wxUSE_UXTHEME
492 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
494 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
496 borderRect
= m_tcArea
;
497 borderRect
.Inflate(1);
500 int drawButFlags
= 0;
505 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
508 wxCopyRectToRECT(borderRect
, rFull
);
511 wxCopyRectToRECT(rectButton
, rButton
);
514 wxCopyRectToRECT(borderRect
, rBorder
);
516 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
517 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
520 // Get some states for themed drawing
525 butState
= CBXS_DISABLED
;
527 // Vista will display the drop-button as depressed always
528 // when the popup window is visilbe
529 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
530 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
532 butState
= CBXS_PRESSED
;
534 else if ( m_btnState
& wxCONTROL_CURRENT
)
540 butState
= CBXS_NORMAL
;
543 int comboBoxPart
= 0; // For XP, use the 'default' part
544 RECT
* rUseForBg
= &rBorder
;
546 bool drawFullButton
= false;
547 int bgState
= butState
;
548 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
550 if ( useVistaComboBox
)
552 // FIXME: Either SetBackgroundColour or GetBackgroundColour
553 // doesn't work under Vista, so here's a temporary
555 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
557 // Draw the entire control as a single button?
558 if ( !isNonStdButton
)
560 if ( HasFlag(wxCB_READONLY
) )
561 drawFullButton
= true;
564 if ( drawFullButton
)
566 comboBoxPart
= CP_READONLY
;
569 // It should be safe enough to update this flag here.
570 m_iFlags
|= wxCC_FULL_BUTTON
;
574 comboBoxPart
= CP_BORDER
;
575 m_iFlags
&= ~wxCC_FULL_BUTTON
;
578 bgState
= CBB_FOCUSED
;
580 bgState
= CBB_NORMAL
;
585 // Draw parent's background, if necessary
586 RECT
* rUseForTb
= NULL
;
588 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
590 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
591 rUseForTb
= &rButton
;
594 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
597 // Draw the control background (including the border)
598 if ( m_widthCustomBorder
> 0 )
600 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
604 // No border. We can't use theme, since it cannot be relied on
605 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
608 dc
.DrawRectangle(borderRect
);
612 // Draw the drop-button
613 if ( !isNonStdButton
)
615 drawButFlags
= Button_BitmapOnly
;
617 int butPart
= CP_DROPDOWNBUTTON
;
619 if ( useVistaComboBox
)
621 if ( drawFullButton
)
623 // We need to alter the button style slightly before
624 // drawing the actual button (but it was good above
625 // when background etc was done).
626 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
627 butState
= CBXS_NORMAL
;
630 if ( m_btnSide
== wxRIGHT
)
631 butPart
= CP_DROPDOWNBUTTONRIGHT
;
633 butPart
= CP_DROPDOWNBUTTONLEFT
;
636 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
638 else if ( useVistaComboBox
&&
639 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
641 // We'll do this, because DrawThemeParentBackground
642 // doesn't seem to be reliable on Vista.
643 drawButFlags
|= Button_PaintBackground
;
649 // Windows 2000 and earlier
650 drawButFlags
= Button_PaintBackground
;
654 dc
.DrawRectangle(borderRect
);
657 // Button rendering (may only do the bitmap on button, depending on the flags)
658 DrawButton( dc
, rectButton
, drawButFlags
);
660 // Paint required portion of the custom image on the control
661 if ( (!m_text
|| m_widthCustomPaint
) )
663 wxASSERT( m_widthCustomPaint
>= 0 );
665 // this is intentionally here to allow drawed rectangle's
666 // right edge to be hidden
668 rectTextField
.width
= m_widthCustomPaint
;
670 dc
.SetFont( GetFont() );
672 dc
.SetClippingRegion(rectTextField
);
673 if ( m_popupInterface
)
674 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
676 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
680 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
683 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
684 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
686 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
689 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
691 // if no textctrl and no special double-click, then the entire control acts
693 handlerFlags
|= wxCC_MF_ON_BUTTON
;
694 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
699 if ( isOnButtonArea
|| HasCapture() ||
700 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
702 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
704 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
707 else if ( m_btnState
)
709 // otherwise need to clear the hover status
711 RefreshRect(m_btnArea
);
716 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
717 // See header file for further information on this method.
718 HandleNormalMouseEvent(event
);
722 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
723 static wxUint32
GetUserPreferencesMask()
725 static wxUint32 userPreferencesMask
= 0;
726 static bool valueSet
= false;
729 return userPreferencesMask
;
731 wxRegKey
* pKey
= NULL
;
732 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
733 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
734 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
738 else if ( key2
.Exists() )
740 else if ( key3
.Exists() )
743 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
746 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
747 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
749 if ( buf
.GetDataLen() >= 4 )
751 wxUint32
* p
= (wxUint32
*) buf
.GetData();
752 userPreferencesMask
= *p
;
759 return userPreferencesMask
;
763 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
764 void wxComboCtrl::OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
766 bool stopTimer
= false;
768 wxWindow
* popup
= GetPopupControl()->GetControl();
770 // Popup was hidden before it was fully shown?
771 if ( IsPopupWindowState(Hidden
) )
777 wxLongLong t
= ::wxGetLocalTimeMillis();
778 const wxRect
& rect
= m_animRect
;
779 wxWindow
* win
= GetPopupWindow();
781 int pos
= (int) (t
-m_animStart
).GetLo();
782 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
784 int height
= rect
.height
;
785 //int h0 = rect.height;
786 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
787 int y
= (height
- h
);
791 if ( m_animFlags
& ShowAbove
)
793 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
797 popup
->Move( 0, -y
);
798 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
811 DoShowPopup( m_animRect
, m_animFlags
);
816 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
817 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
819 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
821 m_animStart
= ::wxGetLocalTimeMillis();
825 wxWindow
* win
= GetPopupWindow();
826 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
829 m_animTimer
.SetOwner( this, wxID_ANY
);
830 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
832 OnTimerEvent(*((wxTimerEvent
*)NULL
)); // Event is never used, so we can give NULL
841 wxCoord
wxComboCtrl::GetNativeTextIndent() const
844 if ( wxUxThemeEngine::GetIfActive() )
845 return NATIVE_TEXT_INDENT_XP
;
847 return NATIVE_TEXT_INDENT_CLASSIC
;
850 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
852 const bool isPopupShown
= IsPopupShown();
854 switch ( event
.GetKeyCode() )
857 // F4 toggles the popup in the native comboboxes, so emulate them
858 if ( !event
.AltDown() )
869 // On XP or with writable combo in Classic, arrows don't open the
870 // popup but Alt-arrow does
871 if ( event
.AltDown() ||
873 HasFlag(wxCB_READONLY
)
875 && !wxUxThemeEngine::GetIfActive()
887 #endif // wxUSE_COMBOCTRL