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 COMBOBOX_ANIMATION_RESOLUTION 10
122 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
124 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<2)
127 // ============================================================================
129 // ============================================================================
132 BEGIN_EVENT_TABLE(wxComboCtrl
, wxComboCtrlBase
)
133 EVT_PAINT(wxComboCtrl::OnPaintEvent
)
134 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent
)
135 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
136 EVT_TIMER(wxID_ANY
, wxComboCtrl::OnTimerEvent
)
141 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxComboCtrlBase
)
143 void wxComboCtrl::Init()
147 bool wxComboCtrl::Create(wxWindow
*parent
,
149 const wxString
& value
,
153 const wxValidator
& validator
,
154 const wxString
& name
)
158 long border
= style
& wxBORDER_MASK
;
161 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
169 // For XP, have 1-width custom border, for older version use sunken
170 border
= wxBORDER_NONE
;
171 m_widthCustomBorder
= 1;
175 border
= wxBORDER_SUNKEN
;
177 style
= (style
& ~(wxBORDER_MASK
)) | border
;
180 // create main window
181 if ( !wxComboCtrlBase::Create(parent
,
186 style
| wxFULL_REPAINT_ON_RESIZE
,
194 if ( ::wxGetWinVersion() >= wxWinVersion_Vista
)
195 m_iFlags
|= wxCC_BUTTON_STAYS_DOWN
|wxCC_BUTTON_COVERS_BORDER
;
199 if ( style
& wxCC_STD_BUTTON
)
200 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
202 // Prepare background for double-buffering or better background theme
203 // support, whichever is possible.
204 SetDoubleBuffered(true);
205 if ( !IsDoubleBuffered() )
206 SetBackgroundStyle( wxBG_STYLE_PAINT
);
208 // Create textctrl, if necessary
209 CreateTextCtrl( wxNO_BORDER
);
211 // Add keyboard input handlers for main control and textctrl
212 InstallInputHandlers();
214 // SetInitialSize should be called last
215 SetInitialSize(size
);
220 wxComboCtrl::~wxComboCtrl()
224 void wxComboCtrl::OnResize()
227 // Recalculates button and textctrl areas
229 // Technically Classic Windows style combo has more narrow button,
230 // but the native renderer doesn't paint it well like that.
232 CalculateAreas(btnWidth
);
234 // Position textctrl using standard routine
238 // Draws non-XP GUI dotted line around the focus area
239 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
241 #if !defined(__WXWINCE__)
244 mswRect.left = rect.x;
245 mswRect.top = rect.y;
246 mswRect.right = rect.x + rect.width;
247 mswRect.bottom = rect.y + rect.height;
248 HDC hdc = (HDC) dc.GetHDC();
249 SetMapMode(hdc,MM_TEXT); // Just in case...
250 DrawFocusRect(hdc,&mswRect);
252 // FIXME: Use DrawFocusRect code above (currently it draws solid line
253 // for caption focus but works ok for other stuff).
254 // Also, this code below may not work in future wx versions, since
255 // it employs wxCAP_BUTT hack to have line of width 1.
256 dc
.SetLogicalFunction(wxINVERT
);
258 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
259 pen
.SetCap(wxCAP_BUTT
);
261 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
263 dc
.DrawRectangle(rect
);
265 dc
.SetLogicalFunction(wxCOPY
);
267 dc
.SetLogicalFunction(wxINVERT
);
269 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
270 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
272 dc
.DrawRectangle(rect
);
274 dc
.SetLogicalFunction(wxCOPY
);
278 // draw focus background on area in a way typical on platform
280 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
283 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
286 wxSize sz
= GetClientSize();
288 bool doDrawFocusRect
; // also selected
290 // For smaller size control (and for disabled background) use less spacing
294 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
297 isEnabled
= IsThisEnabled();
298 doDrawFocusRect
= ShouldDrawFocus();
301 // Windows-style: for smaller size control (and for disabled background) use less spacing
305 focusSpacingX
= isEnabled
? 2 : 1;
306 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
326 // Drawing a list item
327 isEnabled
= true; // they are never disabled
328 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
334 // Set the background sub-rectangle for selection, disabled etc
335 wxRect
selRect(rect
);
336 selRect
.y
+= focusSpacingY
;
337 selRect
.height
-= (focusSpacingY
*2);
341 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
342 wcp
+= m_widthCustomPaint
;
344 selRect
.x
+= wcp
+ focusSpacingX
;
345 selRect
.width
-= wcp
+ (focusSpacingX
*2);
347 //wxUxThemeEngine* theme = NULL;
349 // theme = wxUxThemeEngine::GetIfActive();
353 bool doDrawDottedEdge
= false;
354 bool doDrawSelRect
= true;
356 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
357 // (and other cases which are not that apparent).
361 // If popup is hidden and this control is focused,
362 // then draw the focus-indicator (selbgcolor background etc.).
363 if ( doDrawFocusRect
)
365 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
366 // it is not properly defined for combo boxes. Instead, they expect
367 // you to use DrawThemeText.
369 // Here is, however, sample code how to get theme colours:
372 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
373 // dc.SetTextForeground( wxRGBToColour(cref) );
374 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
376 // Vista style read-only combo
377 fgCol
= GetForegroundColour();
378 bgCol
= GetBackgroundColour();
379 doDrawSelRect
= false;
380 doDrawDottedEdge
= true;
384 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
385 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
390 fgCol
= GetForegroundColour();
391 bgCol
= GetBackgroundColour();
392 doDrawSelRect
= false;
397 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
398 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
401 dc
.SetTextForeground(fgCol
);
406 dc
.DrawRectangle(selRect
);
409 if ( doDrawDottedEdge
)
410 wxMSWDrawFocusRect(dc
, selRect
);
412 // Don't clip exactly to the selection rectangle so we can draw
413 // to the non-selected area in front of it.
414 wxRect
clipRect(rect
.x
,rect
.y
,
415 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
416 dc
.SetClippingRegion(clipRect
);
419 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
421 // TODO: Convert drawing in this function to Windows API Code
423 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
425 wxSize sz
= GetClientSize();
426 wxDC
* dcPtr
= wxAutoBufferedPaintDCFactory(this);
429 const wxRect
& rectButton
= m_btnArea
;
430 wxRect rectTextField
= m_tcArea
;
431 wxColour bgCol
= GetBackgroundColour();
433 // FIXME: Either SetBackgroundColour or GetBackgroundColour
434 // doesn't work under Vista, so here's a temporary
436 // In the theme-less rendering code below, this fixes incorrect
437 // background on read-only comboboxes (they are gray, but should be
439 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
442 const bool isEnabled
= IsThisEnabled();
444 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
445 HDC hDc
= GetHdcOf(*impl
);
446 HWND hWnd
= GetHwndOf(this);
448 wxUxThemeEngine
* theme
= NULL
;
449 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
452 theme
= wxUxThemeEngine::GetIfActive();
453 #endif // wxUSE_UXTHEME
455 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
457 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
459 borderRect
= m_tcArea
;
460 borderRect
.Inflate(1);
463 int drawButFlags
= 0;
469 wxCopyRectToRECT(borderRect
, rFull
);
472 wxCopyRectToRECT(rectButton
, rButton
);
475 wxCopyRectToRECT(borderRect
, rBorder
);
477 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
478 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
481 // Get some states for themed drawing
486 butState
= CBXS_DISABLED
;
488 // Vista will display the drop-button as depressed always
489 // when the popup window is visilbe
490 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
491 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
493 butState
= CBXS_PRESSED
;
495 else if ( m_btnState
& wxCONTROL_CURRENT
)
501 butState
= CBXS_NORMAL
;
504 int comboBoxPart
= 0; // For XP, use the 'default' part
505 RECT
* rUseForBg
= &rBorder
;
507 bool drawFullButton
= false;
508 int bgState
= butState
;
509 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
511 if ( useVistaComboBox
)
513 // Draw the entire control as a single button?
514 if ( !isNonStdButton
)
516 if ( HasFlag(wxCB_READONLY
) )
517 drawFullButton
= true;
520 if ( drawFullButton
)
522 comboBoxPart
= CP_READONLY
;
525 // It should be safe enough to update this flag here.
526 m_iFlags
|= wxCC_FULL_BUTTON
;
530 comboBoxPart
= CP_BORDER
;
531 m_iFlags
&= ~wxCC_FULL_BUTTON
;
534 bgState
= CBB_FOCUSED
;
536 bgState
= CBB_NORMAL
;
541 // Draw parent's background, if necessary
542 RECT
* rUseForTb
= NULL
;
544 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
546 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
547 rUseForTb
= &rButton
;
550 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
553 // Draw the control background (including the border)
554 if ( m_widthCustomBorder
> 0 )
556 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
560 // No border. We can't use theme, since it cannot be relied on
561 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
564 dc
.DrawRectangle(borderRect
);
568 // Draw the drop-button
569 if ( !isNonStdButton
)
571 drawButFlags
= Button_BitmapOnly
;
573 int butPart
= CP_DROPDOWNBUTTON
;
575 if ( useVistaComboBox
)
577 if ( drawFullButton
)
579 // We need to alter the button style slightly before
580 // drawing the actual button (but it was good above
581 // when background etc was done).
582 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
583 butState
= CBXS_NORMAL
;
586 if ( m_btnSide
== wxRIGHT
)
587 butPart
= CP_DROPDOWNBUTTONRIGHT
;
589 butPart
= CP_DROPDOWNBUTTONLEFT
;
592 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
594 else if ( useVistaComboBox
&&
595 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
597 // We'll do this, because DrawThemeParentBackground
598 // doesn't seem to be reliable on Vista.
599 drawButFlags
|= Button_PaintBackground
;
605 // Windows 2000 and earlier
606 drawButFlags
= Button_PaintBackground
;
610 dc
.DrawRectangle(borderRect
);
613 // Button rendering (may only do the bitmap on button, depending on the flags)
614 DrawButton( dc
, rectButton
, drawButFlags
);
616 // Paint required portion of the custom image on the control
617 if ( (!m_text
|| m_widthCustomPaint
) )
619 wxASSERT( m_widthCustomPaint
>= 0 );
621 // this is intentionally here to allow drawed rectangle's
622 // right edge to be hidden
624 rectTextField
.width
= m_widthCustomPaint
;
626 dc
.SetFont( GetFont() );
628 dc
.SetClippingRegion(rectTextField
);
629 if ( m_popupInterface
)
630 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
632 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
638 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
641 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
642 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
644 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
647 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
649 // if no textctrl and no special double-click, then the entire control acts
651 handlerFlags
|= wxCC_MF_ON_BUTTON
;
652 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
657 if ( isOnButtonArea
|| HasCapture() ||
658 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
660 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
662 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
665 else if ( m_btnState
)
667 // otherwise need to clear the hover status
669 RefreshRect(m_btnArea
);
674 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
675 // See header file for further information on this method.
676 HandleNormalMouseEvent(event
);
680 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
681 static wxUint32
GetUserPreferencesMask()
683 static wxUint32 userPreferencesMask
= 0;
684 static bool valueSet
= false;
687 return userPreferencesMask
;
689 wxRegKey
* pKey
= NULL
;
690 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
691 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
692 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
696 else if ( key2
.Exists() )
698 else if ( key3
.Exists() )
701 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
704 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
705 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
707 if ( buf
.GetDataLen() >= 4 )
709 wxUint32
* p
= (wxUint32
*) buf
.GetData();
710 userPreferencesMask
= *p
;
717 return userPreferencesMask
;
721 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
722 void wxComboCtrl::DoTimerEvent()
724 bool stopTimer
= false;
726 wxWindow
* win
= GetPopupWindow();
727 wxWindow
* popup
= GetPopupControl()->GetControl();
729 // Popup was hidden before it was fully shown?
730 if ( IsPopupWindowState(Hidden
) )
736 wxLongLong t
= ::wxGetLocalTimeMillis();
737 const wxRect
& rect
= m_animRect
;
739 int pos
= (int) (t
-m_animStart
).GetLo();
740 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
742 int height
= rect
.height
;
743 //int h0 = rect.height;
744 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
745 int y
= (height
- h
);
749 if ( m_animFlags
& ShowAbove
)
751 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
755 // Note that apparently Move() should be called after
756 // SetSize() to reduce (or even eliminate) animation garbage
757 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
758 popup
->Move( 0, -y
);
770 DoShowPopup( m_animRect
, m_animFlags
);
773 // Do a one final refresh to clean up the rare cases of animation
780 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
781 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
783 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
785 m_animStart
= ::wxGetLocalTimeMillis();
789 wxWindow
* win
= GetPopupWindow();
790 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
793 m_animTimer
.SetOwner( this, wxID_ANY
);
794 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
805 wxCoord
wxComboCtrl::GetNativeTextIndent() const
808 if ( wxUxThemeEngine::GetIfActive() )
809 return NATIVE_TEXT_INDENT_XP
;
811 return NATIVE_TEXT_INDENT_CLASSIC
;
814 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
816 const bool isPopupShown
= IsPopupShown();
818 switch ( event
.GetKeyCode() )
821 // F4 toggles the popup in the native comboboxes, so emulate them
822 if ( !event
.AltDown() )
833 case WXK_NUMPAD_DOWN
:
835 // Arrow keys (and mouse wheel) toggle the popup in the native
837 if ( event
.AltDown() )
845 #endif // wxUSE_COMBOCTRL