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 wxSize sz
= GetClientSize();
424 wxDC
* dcPtr
= wxAutoBufferedPaintDCFactory(this);
427 const wxRect
& rectButton
= m_btnArea
;
428 wxRect rectTextField
= m_tcArea
;
430 // FIXME: Either SetBackgroundColour or GetBackgroundColour
431 // doesn't work under Vista, so here's a temporary
433 // In the theme-less rendering code below, this fixes incorrect
434 // background on read-only comboboxes (they are gray, but should be
436 wxColour bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
439 const bool isEnabled
= IsThisEnabled();
441 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
442 HDC hDc
= GetHdcOf(*impl
);
443 HWND hWnd
= GetHwndOf(this);
445 wxUxThemeEngine
* theme
= NULL
;
446 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
449 theme
= wxUxThemeEngine::GetIfActive();
450 #endif // wxUSE_UXTHEME
452 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
454 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
456 borderRect
= m_tcArea
;
457 borderRect
.Inflate(1);
460 int drawButFlags
= 0;
465 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
468 wxCopyRectToRECT(borderRect
, rFull
);
471 wxCopyRectToRECT(rectButton
, rButton
);
474 wxCopyRectToRECT(borderRect
, rBorder
);
476 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
477 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
480 // Get some states for themed drawing
485 butState
= CBXS_DISABLED
;
487 // Vista will display the drop-button as depressed always
488 // when the popup window is visilbe
489 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
490 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
492 butState
= CBXS_PRESSED
;
494 else if ( m_btnState
& wxCONTROL_CURRENT
)
500 butState
= CBXS_NORMAL
;
503 int comboBoxPart
= 0; // For XP, use the 'default' part
504 RECT
* rUseForBg
= &rBorder
;
506 bool drawFullButton
= false;
507 int bgState
= butState
;
508 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
510 if ( useVistaComboBox
)
512 // Draw the entire control as a single button?
513 if ( !isNonStdButton
)
515 if ( HasFlag(wxCB_READONLY
) )
516 drawFullButton
= true;
519 if ( drawFullButton
)
521 comboBoxPart
= CP_READONLY
;
524 // It should be safe enough to update this flag here.
525 m_iFlags
|= wxCC_FULL_BUTTON
;
529 comboBoxPart
= CP_BORDER
;
530 m_iFlags
&= ~wxCC_FULL_BUTTON
;
533 bgState
= CBB_FOCUSED
;
535 bgState
= CBB_NORMAL
;
540 // Draw parent's background, if necessary
541 RECT
* rUseForTb
= NULL
;
543 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
545 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
546 rUseForTb
= &rButton
;
549 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
552 // Draw the control background (including the border)
553 if ( m_widthCustomBorder
> 0 )
555 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
559 // No border. We can't use theme, since it cannot be relied on
560 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
563 dc
.DrawRectangle(borderRect
);
567 // Draw the drop-button
568 if ( !isNonStdButton
)
570 drawButFlags
= Button_BitmapOnly
;
572 int butPart
= CP_DROPDOWNBUTTON
;
574 if ( useVistaComboBox
)
576 if ( drawFullButton
)
578 // We need to alter the button style slightly before
579 // drawing the actual button (but it was good above
580 // when background etc was done).
581 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
582 butState
= CBXS_NORMAL
;
585 if ( m_btnSide
== wxRIGHT
)
586 butPart
= CP_DROPDOWNBUTTONRIGHT
;
588 butPart
= CP_DROPDOWNBUTTONLEFT
;
591 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
593 else if ( useVistaComboBox
&&
594 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
596 // We'll do this, because DrawThemeParentBackground
597 // doesn't seem to be reliable on Vista.
598 drawButFlags
|= Button_PaintBackground
;
604 // Windows 2000 and earlier
605 drawButFlags
= Button_PaintBackground
;
609 dc
.DrawRectangle(borderRect
);
612 // Button rendering (may only do the bitmap on button, depending on the flags)
613 DrawButton( dc
, rectButton
, drawButFlags
);
615 // Paint required portion of the custom image on the control
616 if ( (!m_text
|| m_widthCustomPaint
) )
618 wxASSERT( m_widthCustomPaint
>= 0 );
620 // this is intentionally here to allow drawed rectangle's
621 // right edge to be hidden
623 rectTextField
.width
= m_widthCustomPaint
;
625 dc
.SetFont( GetFont() );
627 dc
.SetClippingRegion(rectTextField
);
628 if ( m_popupInterface
)
629 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
631 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
637 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
640 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
641 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
643 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
646 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
648 // if no textctrl and no special double-click, then the entire control acts
650 handlerFlags
|= wxCC_MF_ON_BUTTON
;
651 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
656 if ( isOnButtonArea
|| HasCapture() ||
657 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
659 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
661 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
664 else if ( m_btnState
)
666 // otherwise need to clear the hover status
668 RefreshRect(m_btnArea
);
673 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
674 // See header file for further information on this method.
675 HandleNormalMouseEvent(event
);
679 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
680 static wxUint32
GetUserPreferencesMask()
682 static wxUint32 userPreferencesMask
= 0;
683 static bool valueSet
= false;
686 return userPreferencesMask
;
688 wxRegKey
* pKey
= NULL
;
689 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
690 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
691 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
695 else if ( key2
.Exists() )
697 else if ( key3
.Exists() )
700 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
703 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
704 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
706 if ( buf
.GetDataLen() >= 4 )
708 wxUint32
* p
= (wxUint32
*) buf
.GetData();
709 userPreferencesMask
= *p
;
716 return userPreferencesMask
;
720 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
721 void wxComboCtrl::DoTimerEvent()
723 bool stopTimer
= false;
725 wxWindow
* win
= GetPopupWindow();
726 wxWindow
* popup
= GetPopupControl()->GetControl();
728 // Popup was hidden before it was fully shown?
729 if ( IsPopupWindowState(Hidden
) )
735 wxLongLong t
= ::wxGetLocalTimeMillis();
736 const wxRect
& rect
= m_animRect
;
738 int pos
= (int) (t
-m_animStart
).GetLo();
739 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
741 int height
= rect
.height
;
742 //int h0 = rect.height;
743 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
744 int y
= (height
- h
);
748 if ( m_animFlags
& ShowAbove
)
750 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
754 // Note that apparently Move() should be called after
755 // SetSize() to reduce (or even eliminate) animation garbage
756 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
757 popup
->Move( 0, -y
);
769 DoShowPopup( m_animRect
, m_animFlags
);
772 // Do a one final refresh to clean up the rare cases of animation
779 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
780 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
782 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
784 m_animStart
= ::wxGetLocalTimeMillis();
788 wxWindow
* win
= GetPopupWindow();
789 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
792 m_animTimer
.SetOwner( this, wxID_ANY
);
793 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
804 wxCoord
wxComboCtrl::GetNativeTextIndent() const
807 if ( wxUxThemeEngine::GetIfActive() )
808 return NATIVE_TEXT_INDENT_XP
;
810 return NATIVE_TEXT_INDENT_CLASSIC
;
813 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
815 const bool isPopupShown
= IsPopupShown();
817 switch ( event
.GetKeyCode() )
820 // F4 toggles the popup in the native comboboxes, so emulate them
821 if ( !event
.AltDown() )
832 case WXK_NUMPAD_DOWN
:
834 // Arrow keys (and mouse wheel) toggle the popup in the native
836 if ( event
.AltDown() )
844 #endif // wxUSE_COMBOCTRL