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
228 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
231 wxUxThemeEngine
* const theme
= wxUxThemeEngine::GetIfActive();
234 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
235 wxUxThemeHandle
hTheme(this, L
"EDIT");
237 HRESULT hr
= theme
->GetThemeColor
247 SetBackgroundColour(wxRGBToColour(col
));
249 // skip the call below
253 wxLogApiError(wxT("GetThemeColor(EDIT, ETS_NORMAL, TMT_FILLCOLOR)"), hr
);
257 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
260 void wxComboCtrl::OnResize()
263 // Recalculates button and textctrl areas
268 if ( wxUxThemeEngine::GetIfActive() )
270 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
275 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
278 // Technically Classic Windows style combo has more narrow button,
279 // but the native renderer doesn't paint it well like that.
281 CalculateAreas(btnWidth
);
283 // Position textctrl using standard routine
284 PositionTextCtrl(0, textCtrlYAdjust
);
287 // Draws non-XP GUI dotted line around the focus area
288 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
290 #if !defined(__WXWINCE__)
293 mswRect.left = rect.x;
294 mswRect.top = rect.y;
295 mswRect.right = rect.x + rect.width;
296 mswRect.bottom = rect.y + rect.height;
297 HDC hdc = (HDC) dc.GetHDC();
298 SetMapMode(hdc,MM_TEXT); // Just in case...
299 DrawFocusRect(hdc,&mswRect);
301 // FIXME: Use DrawFocusRect code above (currently it draws solid line
302 // for caption focus but works ok for other stuff).
303 // Also, this code below may not work in future wx versions, since
304 // it employs wxCAP_BUTT hack to have line of width 1.
305 dc
.SetLogicalFunction(wxINVERT
);
307 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
308 pen
.SetCap(wxCAP_BUTT
);
310 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
312 dc
.DrawRectangle(rect
);
314 dc
.SetLogicalFunction(wxCOPY
);
316 dc
.SetLogicalFunction(wxINVERT
);
318 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
319 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
321 dc
.DrawRectangle(rect
);
323 dc
.SetLogicalFunction(wxCOPY
);
327 // draw focus background on area in a way typical on platform
329 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
332 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
335 wxSize sz
= GetClientSize();
337 bool doDrawFocusRect
; // also selected
339 // For smaller size control (and for disabled background) use less spacing
343 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
346 isEnabled
= IsEnabled();
347 doDrawFocusRect
= ShouldDrawFocus();
350 // Windows-style: for smaller size control (and for disabled background) use less spacing
354 focusSpacingX
= isEnabled
? 2 : 1;
355 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
375 // Drawing a list item
376 isEnabled
= true; // they are never disabled
377 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
383 // Set the background sub-rectangle for selection, disabled etc
384 wxRect
selRect(rect
);
385 selRect
.y
+= focusSpacingY
;
386 selRect
.height
-= (focusSpacingY
*2);
390 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
391 wcp
+= m_widthCustomPaint
;
393 selRect
.x
+= wcp
+ focusSpacingX
;
394 selRect
.width
-= wcp
+ (focusSpacingX
*2);
396 //wxUxThemeEngine* theme = NULL;
398 // theme = wxUxThemeEngine::GetIfActive();
401 bool doDrawDottedEdge
= false;
402 bool doDrawSelRect
= true;
404 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
405 // (and other cases which are not that apparent).
409 // If popup is hidden and this control is focused,
410 // then draw the focus-indicator (selbgcolor background etc.).
411 if ( doDrawFocusRect
)
413 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
414 // it is not properly defined for combo boxes. Instead, they expect
415 // you to use DrawThemeText.
417 // Here is, however, sample code how to get theme colours:
420 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
421 // dc.SetTextForeground( wxRGBToColour(cref) );
422 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
424 // Vista style read-only combo
425 doDrawSelRect
= false;
426 doDrawDottedEdge
= true;
430 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
431 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
436 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
437 bgCol
= GetBackgroundColour();
438 doDrawSelRect
= false;
443 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
444 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
451 dc
.DrawRectangle(selRect
);
454 if ( doDrawDottedEdge
)
455 wxMSWDrawFocusRect(dc
, selRect
);
457 // Don't clip exactly to the selection rectangle so we can draw
458 // to the non-selected area in front of it.
459 wxRect
clipRect(rect
.x
,rect
.y
,
460 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
461 dc
.SetClippingRegion(clipRect
);
464 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
466 // TODO: Convert drawing in this function to Windows API Code
468 wxSize sz
= GetClientSize();
469 wxAutoBufferedPaintDC
dc(this);
471 const wxRect
& rectButton
= m_btnArea
;
472 wxRect rectTextField
= m_tcArea
;
473 wxColour bgCol
= GetBackgroundColour();
476 const bool isEnabled
= IsEnabled();
478 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
479 HDC hDc
= GetHdcOf(*impl
);
480 HWND hWnd
= GetHwndOf(this);
482 wxUxThemeEngine
* theme
= NULL
;
483 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
486 theme
= wxUxThemeEngine::GetIfActive();
487 #endif // wxUSE_UXTHEME
489 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
491 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
493 borderRect
= m_tcArea
;
494 borderRect
.Inflate(1);
497 int drawButFlags
= 0;
502 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
505 wxCopyRectToRECT(borderRect
, rFull
);
508 wxCopyRectToRECT(rectButton
, rButton
);
511 wxCopyRectToRECT(borderRect
, rBorder
);
513 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
514 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
517 // Get some states for themed drawing
522 butState
= CBXS_DISABLED
;
524 // Vista will display the drop-button as depressed always
525 // when the popup window is visilbe
526 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
527 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
529 butState
= CBXS_PRESSED
;
531 else if ( m_btnState
& wxCONTROL_CURRENT
)
537 butState
= CBXS_NORMAL
;
540 int comboBoxPart
= 0; // For XP, use the 'default' part
541 RECT
* rUseForBg
= &rBorder
;
543 bool drawFullButton
= false;
544 int bgState
= butState
;
545 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
547 if ( useVistaComboBox
)
549 // FIXME: Either SetBackgroundColour or GetBackgroundColour
550 // doesn't work under Vista, so here's a temporary
552 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
554 // Draw the entire control as a single button?
555 if ( !isNonStdButton
)
557 if ( HasFlag(wxCB_READONLY
) )
558 drawFullButton
= true;
561 if ( drawFullButton
)
563 comboBoxPart
= CP_READONLY
;
566 // It should be safe enough to update this flag here.
567 m_iFlags
|= wxCC_FULL_BUTTON
;
571 comboBoxPart
= CP_BORDER
;
572 m_iFlags
&= ~wxCC_FULL_BUTTON
;
575 bgState
= CBB_FOCUSED
;
577 bgState
= CBB_NORMAL
;
582 // Draw parent's background, if necessary
583 RECT
* rUseForTb
= NULL
;
585 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
587 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
588 rUseForTb
= &rButton
;
591 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
594 // Draw the control background (including the border)
595 if ( m_widthCustomBorder
> 0 )
597 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
601 // No border. We can't use theme, since it cannot be relied on
602 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
605 dc
.DrawRectangle(borderRect
);
609 // Draw the drop-button
610 if ( !isNonStdButton
)
612 drawButFlags
= Button_BitmapOnly
;
614 int butPart
= CP_DROPDOWNBUTTON
;
616 if ( useVistaComboBox
)
618 if ( drawFullButton
)
620 // We need to alter the button style slightly before
621 // drawing the actual button (but it was good above
622 // when background etc was done).
623 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
624 butState
= CBXS_NORMAL
;
627 if ( m_btnSide
== wxRIGHT
)
628 butPart
= CP_DROPDOWNBUTTONRIGHT
;
630 butPart
= CP_DROPDOWNBUTTONLEFT
;
633 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
635 else if ( useVistaComboBox
&&
636 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
638 // We'll do this, because DrawThemeParentBackground
639 // doesn't seem to be reliable on Vista.
640 drawButFlags
|= Button_PaintBackground
;
646 // Windows 2000 and earlier
647 drawButFlags
= Button_PaintBackground
;
651 dc
.DrawRectangle(borderRect
);
654 // Button rendering (may only do the bitmap on button, depending on the flags)
655 DrawButton( dc
, rectButton
, drawButFlags
);
657 // Paint required portion of the custom image on the control
658 if ( (!m_text
|| m_widthCustomPaint
) )
660 wxASSERT( m_widthCustomPaint
>= 0 );
662 // this is intentionally here to allow drawed rectangle's
663 // right edge to be hidden
665 rectTextField
.width
= m_widthCustomPaint
;
667 dc
.SetFont( GetFont() );
669 dc
.SetClippingRegion(rectTextField
);
670 if ( m_popupInterface
)
671 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
673 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
677 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
680 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
681 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
683 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
686 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
688 // if no textctrl and no special double-click, then the entire control acts
690 handlerFlags
|= wxCC_MF_ON_BUTTON
;
691 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
696 if ( isOnButtonArea
|| HasCapture() ||
697 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
699 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
701 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
704 else if ( m_btnState
)
706 // otherwise need to clear the hover status
708 RefreshRect(m_btnArea
);
713 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
714 // See header file for further information on this method.
715 HandleNormalMouseEvent(event
);
719 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
720 static wxUint32
GetUserPreferencesMask()
722 static wxUint32 userPreferencesMask
= 0;
723 static bool valueSet
= false;
726 return userPreferencesMask
;
728 wxRegKey
* pKey
= NULL
;
729 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
730 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
731 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
735 else if ( key2
.Exists() )
737 else if ( key3
.Exists() )
740 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
743 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
744 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
746 if ( buf
.GetDataLen() >= 4 )
748 wxUint32
* p
= (wxUint32
*) buf
.GetData();
749 userPreferencesMask
= *p
;
756 return userPreferencesMask
;
760 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
761 void wxComboCtrl::DoTimerEvent()
763 bool stopTimer
= false;
765 wxWindow
* popup
= GetPopupControl()->GetControl();
767 // Popup was hidden before it was fully shown?
768 if ( IsPopupWindowState(Hidden
) )
774 wxLongLong t
= ::wxGetLocalTimeMillis();
775 const wxRect
& rect
= m_animRect
;
776 wxWindow
* win
= GetPopupWindow();
778 int pos
= (int) (t
-m_animStart
).GetLo();
779 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
781 int height
= rect
.height
;
782 //int h0 = rect.height;
783 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
784 int y
= (height
- h
);
788 if ( m_animFlags
& ShowAbove
)
790 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
794 popup
->Move( 0, -y
);
795 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
808 DoShowPopup( m_animRect
, m_animFlags
);
813 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
814 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
816 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
818 m_animStart
= ::wxGetLocalTimeMillis();
822 wxWindow
* win
= GetPopupWindow();
823 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
826 m_animTimer
.SetOwner( this, wxID_ANY
);
827 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
838 wxCoord
wxComboCtrl::GetNativeTextIndent() const
841 if ( wxUxThemeEngine::GetIfActive() )
842 return NATIVE_TEXT_INDENT_XP
;
844 return NATIVE_TEXT_INDENT_CLASSIC
;
847 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
849 const bool isPopupShown
= IsPopupShown();
851 switch ( event
.GetKeyCode() )
854 // F4 toggles the popup in the native comboboxes, so emulate them
855 if ( !event
.AltDown() )
866 // On XP or with writable combo in Classic, arrows don't open the
867 // popup but Alt-arrow does
868 if ( event
.AltDown() ||
870 HasFlag(wxCB_READONLY
)
872 && !wxUxThemeEngine::GetIfActive()
884 #endif // wxUSE_COMBOCTRL