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"
41 #include "wx/msw/uxtheme.h"
43 // Change to #if 1 to include tmschema.h for easier testing of theme
49 //----------------------------------
53 #define ETS_SELECTED 3
54 #define ETS_DISABLED 4
56 #define ETS_READONLY 6
58 #define TMT_FILLCOLOR 3802
59 #define TMT_TEXTCOLOR 3803
60 #define TMT_BORDERCOLOR 3801
61 #define TMT_EDGEFILLCOLOR 3808
62 #define TMT_BGTYPE 4001
64 #define BT_IMAGEFILE 0
65 #define BT_BORDERFILL 1
67 #define CP_DROPDOWNBUTTON 1
68 #define CP_BACKGROUND 2 // This and above are Vista and later only
69 #define CP_TRANSPARENTBACKGROUND 3
72 #define CP_DROPDOWNBUTTONRIGHT 6
73 #define CP_DROPDOWNBUTTONLEFT 7
74 #define CP_CUEBANNER 8
78 #define CBXS_PRESSED 3
79 #define CBXS_DISABLED 4
81 #define CBXSR_NORMAL 1
83 #define CBXSR_PRESSED 3
84 #define CBXSR_DISABLED 4
86 #define CBXSL_NORMAL 1
88 #define CBXSL_PRESSED 3
89 #define CBXSL_DISABLED 4
91 #define CBTBS_NORMAL 1
93 #define CBTBS_DISABLED 3
94 #define CBTBS_FOCUSED 4
99 #define CBB_DISABLED 4
101 #define CBRO_NORMAL 1
103 #define CBRO_PRESSED 3
104 #define CBRO_DISABLED 4
106 #define CBCB_NORMAL 1
108 #define CBCB_PRESSED 3
109 #define CBCB_DISABLED 4
114 #define NATIVE_TEXT_INDENT_XP 4
115 #define NATIVE_TEXT_INDENT_CLASSIC 2
117 #define TEXTCTRLXADJUST_XP 1
118 #define TEXTCTRLYADJUST_XP 3
119 #define TEXTCTRLXADJUST_CLASSIC 1
120 #define TEXTCTRLYADJUST_CLASSIC 3
122 #define COMBOBOX_ANIMATION_RESOLUTION 10
124 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
126 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<2)
129 // ============================================================================
131 // ============================================================================
134 BEGIN_EVENT_TABLE(wxComboCtrl
, wxComboCtrlBase
)
135 EVT_PAINT(wxComboCtrl::OnPaintEvent
)
136 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent
)
137 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
138 EVT_TIMER(wxID_ANY
, wxComboCtrl::OnTimerEvent
)
143 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxComboCtrlBase
)
145 void wxComboCtrl::Init()
149 bool wxComboCtrl::Create(wxWindow
*parent
,
151 const wxString
& value
,
155 const wxValidator
& validator
,
156 const wxString
& name
)
160 long border
= style
& wxBORDER_MASK
;
162 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
168 // For XP, have 1-width custom border, for older version use sunken
169 border
= wxBORDER_NONE
;
170 m_widthCustomBorder
= 1;
173 border
= wxBORDER_SUNKEN
;
175 style
= (style
& ~(wxBORDER_MASK
)) | border
;
178 // create main window
179 if ( !wxComboCtrlBase::Create(parent
,
184 style
| wxFULL_REPAINT_ON_RESIZE
,
191 if ( ::wxGetWinVersion() >= wxWinVersion_Vista
)
192 m_iFlags
|= wxCC_BUTTON_STAYS_DOWN
|wxCC_BUTTON_COVERS_BORDER
;
195 if ( style
& wxCC_STD_BUTTON
)
196 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
198 // Create textctrl, if necessary
199 CreateTextCtrl( wxNO_BORDER
, validator
);
201 // Add keyboard input handlers for main control and textctrl
202 InstallInputHandlers();
204 // Prepare background for double-buffering
205 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
207 // SetInitialSize should be called last
208 SetInitialSize(size
);
213 wxComboCtrl::~wxComboCtrl()
217 void wxComboCtrl::OnThemeChange()
219 // there doesn't seem to be any way to get the text colour using themes
220 // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX
221 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
223 wxUxThemeEngine
* const theme
= wxUxThemeEngine::GetIfActive();
226 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
227 wxUxThemeHandle
hTheme(this, L
"EDIT");
229 HRESULT hr
= theme
->GetThemeColor
239 SetBackgroundColour(wxRGBToColour(col
));
241 // skip the call below
245 wxLogApiError(_T("GetThemeColor(EDIT, ETS_NORMAL, TMT_FILLCOLOR)"), hr
);
248 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
251 void wxComboCtrl::OnResize()
254 // Recalculates button and textctrl areas
259 if ( wxUxThemeEngine::GetIfActive() )
261 textCtrlXAdjust
= TEXTCTRLXADJUST_XP
;
262 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
266 textCtrlXAdjust
= TEXTCTRLXADJUST_CLASSIC
;
267 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
270 // Technically Classic Windows style combo has more narrow button,
271 // but the native renderer doesn't paint it well like that.
273 CalculateAreas(btnWidth
);
275 // Position textctrl using standard routine
276 PositionTextCtrl(textCtrlXAdjust
,textCtrlYAdjust
);
279 // Draws non-XP GUI dotted line around the focus area
280 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
282 #if !defined(__WXWINCE__)
285 mswRect.left = rect.x;
286 mswRect.top = rect.y;
287 mswRect.right = rect.x + rect.width;
288 mswRect.bottom = rect.y + rect.height;
289 HDC hdc = (HDC) dc.GetHDC();
290 SetMapMode(hdc,MM_TEXT); // Just in case...
291 DrawFocusRect(hdc,&mswRect);
293 // FIXME: Use DrawFocusRect code above (currently it draws solid line
294 // for caption focus but works ok for other stuff).
295 // Also, this code below may not work in future wx versions, since
296 // it employs wxCAP_BUTT hack to have line of width 1.
297 dc
.SetLogicalFunction(wxINVERT
);
299 wxPen
pen(*wxBLACK
,1,wxDOT
);
300 pen
.SetCap(wxCAP_BUTT
);
302 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
304 dc
.DrawRectangle(rect
);
306 dc
.SetLogicalFunction(wxCOPY
);
308 dc
.SetLogicalFunction(wxINVERT
);
310 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
311 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
313 dc
.DrawRectangle(rect
);
315 dc
.SetLogicalFunction(wxCOPY
);
319 // draw focus background on area in a way typical on platform
321 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
323 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
325 wxSize sz
= GetClientSize();
327 bool doDrawFocusRect
; // also selected
329 // For smaller size control (and for disabled background) use less spacing
333 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
336 isEnabled
= IsEnabled();
337 doDrawFocusRect
= ShouldDrawFocus();
339 // Windows-style: for smaller size control (and for disabled background) use less spacing
343 focusSpacingX
= isEnabled
? 2 : 1;
344 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
363 // Drawing a list item
364 isEnabled
= true; // they are never disabled
365 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
371 // Set the background sub-rectangle for selection, disabled etc
372 wxRect
selRect(rect
);
373 selRect
.y
+= focusSpacingY
;
374 selRect
.height
-= (focusSpacingY
*2);
378 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
379 wcp
+= m_widthCustomPaint
;
381 selRect
.x
+= wcp
+ focusSpacingX
;
382 selRect
.width
-= wcp
+ (focusSpacingX
*2);
384 //wxUxThemeEngine* theme = (wxUxThemeEngine*) NULL;
386 // theme = wxUxThemeEngine::GetIfActive();
389 bool doDrawDottedEdge
= false;
390 bool doDrawSelRect
= true;
392 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
393 // (and other cases which are not that apparent).
397 // If popup is hidden and this control is focused,
398 // then draw the focus-indicator (selbgcolor background etc.).
399 if ( doDrawFocusRect
)
401 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
402 // it is not properly defined for combo boxes. Instead, they expect
403 // you to use DrawThemeText.
405 // Here is, however, sample code how to get theme colours:
408 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
409 // dc.SetTextForeground( wxRGBToColour(cref) );
410 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
412 // Vista style read-only combo
413 doDrawSelRect
= false;
414 doDrawDottedEdge
= true;
418 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
419 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
424 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
425 bgCol
= GetBackgroundColour();
426 doDrawSelRect
= false;
431 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
432 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
439 dc
.DrawRectangle(selRect
);
442 if ( doDrawDottedEdge
)
443 wxMSWDrawFocusRect(dc
, selRect
);
445 // Don't clip exactly to the selection rectangle so we can draw
446 // to the non-selected area in front of it.
447 wxRect
clipRect(rect
.x
,rect
.y
,
448 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
449 dc
.SetClippingRegion(clipRect
);
452 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
454 // TODO: Convert drawing in this function to Windows API Code
456 wxSize sz
= GetClientSize();
457 wxAutoBufferedPaintDC
dc(this);
459 const wxRect
& rectButton
= m_btnArea
;
460 wxRect rectTextField
= m_tcArea
;
461 const bool isEnabled
= IsEnabled();
462 wxColour bgCol
= GetBackgroundColour();
464 HDC hDc
= GetHdcOf(dc
);
465 HWND hWnd
= GetHwndOf(this);
467 wxUxThemeEngine
* theme
= NULL
;
468 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
471 theme
= wxUxThemeEngine::GetIfActive();
473 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
475 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
477 borderRect
= m_tcArea
;
478 borderRect
.Inflate(1);
481 int drawButFlags
= 0;
485 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
488 wxCopyRectToRECT(borderRect
, rFull
);
491 wxCopyRectToRECT(rectButton
, rButton
);
494 wxCopyRectToRECT(borderRect
, rBorder
);
496 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
497 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
500 // Get some states for themed drawing
505 butState
= CBXS_DISABLED
;
507 // Vista will display the drop-button as depressed always
508 // when the popup window is visilbe
509 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
510 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
512 butState
= CBXS_PRESSED
;
514 else if ( m_btnState
& wxCONTROL_CURRENT
)
520 butState
= CBXS_NORMAL
;
523 int comboBoxPart
= 0; // For XP, use the 'default' part
524 RECT
* rUseForBg
= &rBorder
;
526 bool drawFullButton
= false;
527 int bgState
= butState
;
528 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
530 if ( useVistaComboBox
)
532 // FIXME: Either SetBackgroundColour or GetBackgroundColour
533 // doesn't work under Vista, so here's a temporary
535 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
537 // Draw the entire control as a single button?
538 if ( !isNonStdButton
)
540 if ( HasFlag(wxCB_READONLY
) )
541 drawFullButton
= true;
544 if ( drawFullButton
)
546 comboBoxPart
= CP_READONLY
;
549 // It should be safe enough to update this flag here.
550 m_iFlags
|= wxCC_FULL_BUTTON
;
554 comboBoxPart
= CP_BORDER
;
555 m_iFlags
&= ~wxCC_FULL_BUTTON
;
558 bgState
= CBB_FOCUSED
;
560 bgState
= CBB_NORMAL
;
565 // Draw parent's background, if necessary
566 RECT
* rUseForTb
= NULL
;
568 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
570 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
571 rUseForTb
= &rButton
;
574 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
577 // Draw the control background (including the border)
578 if ( m_widthCustomBorder
> 0 )
580 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
584 // No border. We can't use theme, since it cannot be relied on
585 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
588 dc
.DrawRectangle(borderRect
);
592 // Draw the drop-button
593 if ( !isNonStdButton
)
595 drawButFlags
= Button_BitmapOnly
;
597 int butPart
= CP_DROPDOWNBUTTON
;
599 if ( useVistaComboBox
)
601 if ( drawFullButton
)
603 // We need to alter the button style slightly before
604 // drawing the actual button (but it was good above
605 // when background etc was done).
606 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
607 butState
= CBXS_NORMAL
;
610 if ( m_btnSide
== wxRIGHT
)
611 butPart
= CP_DROPDOWNBUTTONRIGHT
;
613 butPart
= CP_DROPDOWNBUTTONLEFT
;
616 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
618 else if ( useVistaComboBox
&&
619 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
621 // We'll do this, because DrawThemeParentBackground
622 // doesn't seem to be reliable on Vista.
623 drawButFlags
|= Button_PaintBackground
;
628 // Windows 2000 and earlier
629 drawButFlags
= Button_PaintBackground
;
633 dc
.DrawRectangle(borderRect
);
636 // Button rendering (may only do the bitmap on button, depending on the flags)
637 DrawButton( dc
, rectButton
, drawButFlags
);
639 // Paint required portion of the custom image on the control
640 if ( (!m_text
|| m_widthCustomPaint
) )
642 wxASSERT( m_widthCustomPaint
>= 0 );
644 // this is intentionally here to allow drawed rectangle's
645 // right edge to be hidden
647 rectTextField
.width
= m_widthCustomPaint
;
649 dc
.SetFont( GetFont() );
651 dc
.SetClippingRegion(rectTextField
);
652 if ( m_popupInterface
)
653 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
655 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
659 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
662 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
663 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
665 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
668 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
670 // if no textctrl and no special double-click, then the entire control acts
672 handlerFlags
|= wxCC_MF_ON_BUTTON
;
673 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
678 if ( isOnButtonArea
|| HasCapture() ||
679 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
681 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
683 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
686 else if ( m_btnState
)
688 // otherwise need to clear the hover status
690 RefreshRect(m_btnArea
);
695 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
696 // See header file for further information on this method.
697 HandleNormalMouseEvent(event
);
701 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
702 static wxUint32
GetUserPreferencesMask()
704 static wxUint32 userPreferencesMask
= 0;
705 static bool valueSet
= false;
708 return userPreferencesMask
;
710 wxRegKey
* pKey
= NULL
;
711 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
712 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
713 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
717 else if ( key2
.Exists() )
719 else if ( key3
.Exists() )
722 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
725 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
726 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
728 if ( buf
.GetDataLen() >= 4 )
730 wxUint32
* p
= (wxUint32
*) buf
.GetData();
731 userPreferencesMask
= *p
;
738 return userPreferencesMask
;
742 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
743 void wxComboCtrl::OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
745 bool stopTimer
= false;
747 wxWindow
* popup
= GetPopupControl()->GetControl();
749 // Popup was hidden before it was fully shown?
750 if ( IsPopupWindowState(Hidden
) )
756 wxLongLong t
= ::wxGetLocalTimeMillis();
757 const wxRect
& rect
= m_animRect
;
758 wxWindow
* win
= GetPopupWindow();
760 int pos
= (int) (t
-m_animStart
).GetLo();
761 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
763 int height
= rect
.height
;
764 //int h0 = rect.height;
765 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
766 int y
= (height
- h
);
770 if ( m_animFlags
& ShowAbove
)
772 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
776 popup
->Move( 0, -y
);
777 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
790 DoShowPopup( m_animRect
, m_animFlags
);
795 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
796 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
798 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
800 m_animStart
= ::wxGetLocalTimeMillis();
804 wxWindow
* win
= GetPopupWindow();
805 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
808 m_animTimer
.SetOwner( this, wxID_ANY
);
809 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
811 OnTimerEvent(*((wxTimerEvent
*)NULL
)); // Event is never used, so we can give NULL
820 wxCoord
wxComboCtrl::GetNativeTextIndent() const
822 if ( wxUxThemeEngine::GetIfActive() )
823 return NATIVE_TEXT_INDENT_XP
;
824 return NATIVE_TEXT_INDENT_CLASSIC
;
827 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
829 const bool isPopupShown
= IsPopupShown();
831 switch ( event
.GetKeyCode() )
834 // F4 toggles the popup in the native comboboxes, so emulate them
835 if ( !event
.AltDown() )
846 // On XP or with writable combo in Classic, arrows don't open the
847 // popup but Alt-arrow does
848 if ( event
.AltDown() ||
850 HasFlag(wxCB_READONLY
) &&
851 !wxUxThemeEngine::GetIfActive()
862 #endif // wxUSE_COMBOCTRL