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::OnResize()
227 // Recalculates button and textctrl areas
232 if ( wxUxThemeEngine::GetIfActive() )
234 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
239 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
242 // Technically Classic Windows style combo has more narrow button,
243 // but the native renderer doesn't paint it well like that.
245 CalculateAreas(btnWidth
);
247 // Position textctrl using standard routine
248 PositionTextCtrl(0, textCtrlYAdjust
);
251 // Draws non-XP GUI dotted line around the focus area
252 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
254 #if !defined(__WXWINCE__)
257 mswRect.left = rect.x;
258 mswRect.top = rect.y;
259 mswRect.right = rect.x + rect.width;
260 mswRect.bottom = rect.y + rect.height;
261 HDC hdc = (HDC) dc.GetHDC();
262 SetMapMode(hdc,MM_TEXT); // Just in case...
263 DrawFocusRect(hdc,&mswRect);
265 // FIXME: Use DrawFocusRect code above (currently it draws solid line
266 // for caption focus but works ok for other stuff).
267 // Also, this code below may not work in future wx versions, since
268 // it employs wxCAP_BUTT hack to have line of width 1.
269 dc
.SetLogicalFunction(wxINVERT
);
271 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
272 pen
.SetCap(wxCAP_BUTT
);
274 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
276 dc
.DrawRectangle(rect
);
278 dc
.SetLogicalFunction(wxCOPY
);
280 dc
.SetLogicalFunction(wxINVERT
);
282 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
283 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
285 dc
.DrawRectangle(rect
);
287 dc
.SetLogicalFunction(wxCOPY
);
291 // draw focus background on area in a way typical on platform
293 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
296 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
299 wxSize sz
= GetClientSize();
301 bool doDrawFocusRect
; // also selected
303 // For smaller size control (and for disabled background) use less spacing
307 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
310 isEnabled
= IsEnabled();
311 doDrawFocusRect
= ShouldDrawFocus();
314 // Windows-style: for smaller size control (and for disabled background) use less spacing
318 focusSpacingX
= isEnabled
? 2 : 1;
319 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
339 // Drawing a list item
340 isEnabled
= true; // they are never disabled
341 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
347 // Set the background sub-rectangle for selection, disabled etc
348 wxRect
selRect(rect
);
349 selRect
.y
+= focusSpacingY
;
350 selRect
.height
-= (focusSpacingY
*2);
354 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
355 wcp
+= m_widthCustomPaint
;
357 selRect
.x
+= wcp
+ focusSpacingX
;
358 selRect
.width
-= wcp
+ (focusSpacingX
*2);
360 //wxUxThemeEngine* theme = NULL;
362 // theme = wxUxThemeEngine::GetIfActive();
366 bool doDrawDottedEdge
= false;
367 bool doDrawSelRect
= true;
369 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
370 // (and other cases which are not that apparent).
374 // If popup is hidden and this control is focused,
375 // then draw the focus-indicator (selbgcolor background etc.).
376 if ( doDrawFocusRect
)
378 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
379 // it is not properly defined for combo boxes. Instead, they expect
380 // you to use DrawThemeText.
382 // Here is, however, sample code how to get theme colours:
385 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
386 // dc.SetTextForeground( wxRGBToColour(cref) );
387 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
389 // Vista style read-only combo
390 fgCol
= GetForegroundColour();
391 bgCol
= GetBackgroundColour();
392 doDrawSelRect
= false;
393 doDrawDottedEdge
= true;
397 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
398 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
403 fgCol
= GetForegroundColour();
404 bgCol
= GetBackgroundColour();
405 doDrawSelRect
= false;
410 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
411 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
414 dc
.SetTextForeground(fgCol
);
419 dc
.DrawRectangle(selRect
);
422 if ( doDrawDottedEdge
)
423 wxMSWDrawFocusRect(dc
, selRect
);
425 // Don't clip exactly to the selection rectangle so we can draw
426 // to the non-selected area in front of it.
427 wxRect
clipRect(rect
.x
,rect
.y
,
428 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
429 dc
.SetClippingRegion(clipRect
);
432 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
434 // TODO: Convert drawing in this function to Windows API Code
436 wxSize sz
= GetClientSize();
437 wxAutoBufferedPaintDC
dc(this);
439 const wxRect
& rectButton
= m_btnArea
;
440 wxRect rectTextField
= m_tcArea
;
441 wxColour bgCol
= GetBackgroundColour();
444 const bool isEnabled
= IsEnabled();
446 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
447 HDC hDc
= GetHdcOf(*impl
);
448 HWND hWnd
= GetHwndOf(this);
450 wxUxThemeEngine
* theme
= NULL
;
451 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
454 theme
= wxUxThemeEngine::GetIfActive();
455 #endif // wxUSE_UXTHEME
457 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
459 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
461 borderRect
= m_tcArea
;
462 borderRect
.Inflate(1);
465 int drawButFlags
= 0;
470 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
473 wxCopyRectToRECT(borderRect
, rFull
);
476 wxCopyRectToRECT(rectButton
, rButton
);
479 wxCopyRectToRECT(borderRect
, rBorder
);
481 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
482 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
485 // Get some states for themed drawing
490 butState
= CBXS_DISABLED
;
492 // Vista will display the drop-button as depressed always
493 // when the popup window is visilbe
494 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
495 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
497 butState
= CBXS_PRESSED
;
499 else if ( m_btnState
& wxCONTROL_CURRENT
)
505 butState
= CBXS_NORMAL
;
508 int comboBoxPart
= 0; // For XP, use the 'default' part
509 RECT
* rUseForBg
= &rBorder
;
511 bool drawFullButton
= false;
512 int bgState
= butState
;
513 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
515 if ( useVistaComboBox
)
517 // FIXME: Either SetBackgroundColour or GetBackgroundColour
518 // doesn't work under Vista, so here's a temporary
520 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
522 // Draw the entire control as a single button?
523 if ( !isNonStdButton
)
525 if ( HasFlag(wxCB_READONLY
) )
526 drawFullButton
= true;
529 if ( drawFullButton
)
531 comboBoxPart
= CP_READONLY
;
534 // It should be safe enough to update this flag here.
535 m_iFlags
|= wxCC_FULL_BUTTON
;
539 comboBoxPart
= CP_BORDER
;
540 m_iFlags
&= ~wxCC_FULL_BUTTON
;
543 bgState
= CBB_FOCUSED
;
545 bgState
= CBB_NORMAL
;
550 // Draw parent's background, if necessary
551 RECT
* rUseForTb
= NULL
;
553 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
555 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
556 rUseForTb
= &rButton
;
559 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
562 // Draw the control background (including the border)
563 if ( m_widthCustomBorder
> 0 )
565 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
569 // No border. We can't use theme, since it cannot be relied on
570 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
573 dc
.DrawRectangle(borderRect
);
577 // Draw the drop-button
578 if ( !isNonStdButton
)
580 drawButFlags
= Button_BitmapOnly
;
582 int butPart
= CP_DROPDOWNBUTTON
;
584 if ( useVistaComboBox
)
586 if ( drawFullButton
)
588 // We need to alter the button style slightly before
589 // drawing the actual button (but it was good above
590 // when background etc was done).
591 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
592 butState
= CBXS_NORMAL
;
595 if ( m_btnSide
== wxRIGHT
)
596 butPart
= CP_DROPDOWNBUTTONRIGHT
;
598 butPart
= CP_DROPDOWNBUTTONLEFT
;
601 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
603 else if ( useVistaComboBox
&&
604 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
606 // We'll do this, because DrawThemeParentBackground
607 // doesn't seem to be reliable on Vista.
608 drawButFlags
|= Button_PaintBackground
;
614 // Windows 2000 and earlier
615 drawButFlags
= Button_PaintBackground
;
619 dc
.DrawRectangle(borderRect
);
622 // Button rendering (may only do the bitmap on button, depending on the flags)
623 DrawButton( dc
, rectButton
, drawButFlags
);
625 // Paint required portion of the custom image on the control
626 if ( (!m_text
|| m_widthCustomPaint
) )
628 wxASSERT( m_widthCustomPaint
>= 0 );
630 // this is intentionally here to allow drawed rectangle's
631 // right edge to be hidden
633 rectTextField
.width
= m_widthCustomPaint
;
635 dc
.SetFont( GetFont() );
637 dc
.SetClippingRegion(rectTextField
);
638 if ( m_popupInterface
)
639 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
641 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
645 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
648 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
649 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
651 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
654 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
656 // if no textctrl and no special double-click, then the entire control acts
658 handlerFlags
|= wxCC_MF_ON_BUTTON
;
659 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
664 if ( isOnButtonArea
|| HasCapture() ||
665 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
667 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
669 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
672 else if ( m_btnState
)
674 // otherwise need to clear the hover status
676 RefreshRect(m_btnArea
);
681 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
682 // See header file for further information on this method.
683 HandleNormalMouseEvent(event
);
687 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
688 static wxUint32
GetUserPreferencesMask()
690 static wxUint32 userPreferencesMask
= 0;
691 static bool valueSet
= false;
694 return userPreferencesMask
;
696 wxRegKey
* pKey
= NULL
;
697 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
698 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
699 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
703 else if ( key2
.Exists() )
705 else if ( key3
.Exists() )
708 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
711 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
712 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
714 if ( buf
.GetDataLen() >= 4 )
716 wxUint32
* p
= (wxUint32
*) buf
.GetData();
717 userPreferencesMask
= *p
;
724 return userPreferencesMask
;
728 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
729 void wxComboCtrl::DoTimerEvent()
731 bool stopTimer
= false;
733 wxWindow
* win
= GetPopupWindow();
734 wxWindow
* popup
= GetPopupControl()->GetControl();
736 // Popup was hidden before it was fully shown?
737 if ( IsPopupWindowState(Hidden
) )
743 wxLongLong t
= ::wxGetLocalTimeMillis();
744 const wxRect
& rect
= m_animRect
;
746 int pos
= (int) (t
-m_animStart
).GetLo();
747 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
749 int height
= rect
.height
;
750 //int h0 = rect.height;
751 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
752 int y
= (height
- h
);
756 if ( m_animFlags
& ShowAbove
)
758 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
762 // Note that apparently Move() should be called after
763 // SetSize() to reduce (or even eliminate) animation garbage
764 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
765 popup
->Move( 0, -y
);
777 DoShowPopup( m_animRect
, m_animFlags
);
780 // Do a one final refresh to clean up the rare cases of animation
787 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
788 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
790 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
792 m_animStart
= ::wxGetLocalTimeMillis();
796 wxWindow
* win
= GetPopupWindow();
797 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
800 m_animTimer
.SetOwner( this, wxID_ANY
);
801 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
812 wxCoord
wxComboCtrl::GetNativeTextIndent() const
815 if ( wxUxThemeEngine::GetIfActive() )
816 return NATIVE_TEXT_INDENT_XP
;
818 return NATIVE_TEXT_INDENT_CLASSIC
;
821 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
823 const bool isPopupShown
= IsPopupShown();
825 switch ( event
.GetKeyCode() )
828 // F4 toggles the popup in the native comboboxes, so emulate them
829 if ( !event
.AltDown() )
840 case WXK_NUMPAD_DOWN
:
842 // Arrow keys (and mouse wheel) toggle the popup in the native
844 if ( event
.AltDown() )
852 #endif // wxUSE_COMBOCTRL