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
48 //----------------------------------
52 #define ETS_SELECTED 3
53 #define ETS_DISABLED 4
55 #define ETS_READONLY 6
57 #define TMT_FILLCOLOR 3802
58 #define TMT_TEXTCOLOR 3803
59 #define TMT_BORDERCOLOR 3801
60 #define TMT_EDGEFILLCOLOR 3808
61 //----------------------------------
65 #define NATIVE_TEXT_INDENT_XP 4
66 #define NATIVE_TEXT_INDENT_CLASSIC 2
68 #define TEXTCTRLXADJUST_XP 1
69 #define TEXTCTRLYADJUST_XP 3
70 #define TEXTCTRLXADJUST_CLASSIC 1
71 #define TEXTCTRLYADJUST_CLASSIC 2
73 #define COMBOBOX_ANIMATION_RESOLUTION 10
75 #define COMBOBOX_ANIMATION_DURATION 200 // In milliseconds
77 #define wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM (1<<26)
80 // ============================================================================
82 // ============================================================================
85 BEGIN_EVENT_TABLE(wxComboCtrl
, wxComboCtrlBase
)
86 EVT_PAINT(wxComboCtrl::OnPaintEvent
)
87 EVT_MOUSE_EVENTS(wxComboCtrl::OnMouseEvent
)
88 EVT_TIMER(wxID_ANY
, wxComboCtrl::OnTimerEvent
)
92 IMPLEMENT_DYNAMIC_CLASS(wxComboCtrl
, wxComboCtrlBase
)
94 void wxComboCtrl::Init()
98 bool wxComboCtrl::Create(wxWindow
*parent
,
100 const wxString
& value
,
104 const wxValidator
& validator
,
105 const wxString
& name
)
109 long border
= style
& wxBORDER_MASK
;
111 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
115 // For XP, have 1-width custom border, for older version use sunken
118 border
= wxBORDER_NONE
;
119 m_widthCustomBorder
= 1;
122 border
= wxBORDER_SUNKEN
;
124 style
= (style
& ~(wxBORDER_MASK
)) | border
;
127 // create main window
128 if ( !wxComboCtrlBase::Create(parent
,
133 style
| wxFULL_REPAINT_ON_RESIZE
,
138 if ( style
& wxCC_STD_BUTTON
)
139 m_iFlags
|= wxCC_POPUP_ON_MOUSE_UP
;
141 // Create textctrl, if necessary
142 CreateTextCtrl( wxNO_BORDER
, validator
);
144 // Add keyboard input handlers for main control and textctrl
145 InstallInputHandlers();
147 // Prepare background for double-buffering
148 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
150 // SetBestSize should be called last
156 wxComboCtrl::~wxComboCtrl()
160 void wxComboCtrl::OnThemeChange()
162 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
165 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
168 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_NORMAL
,TMT_FILLCOLOR
,&col
);
169 SetBackgroundColour(wxRGBToColour(col
));
170 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_NORMAL
,TMT_TEXTCOLOR
,&col
);
171 SetForegroundColour(wxRGBToColour(col
));
175 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
176 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
180 void wxComboCtrl::OnResize()
183 // Recalculates button and textctrl areas
188 if ( wxUxThemeEngine::GetIfActive() )
190 textCtrlXAdjust
= TEXTCTRLXADJUST_XP
;
191 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
195 textCtrlXAdjust
= TEXTCTRLXADJUST_CLASSIC
;
196 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
199 // Technically Classic Windows style combo has more narrow button,
200 // but the native renderer doesn't paint it well like that.
202 CalculateAreas(btnWidth
);
204 // Position textctrl using standard routine
205 PositionTextCtrl(textCtrlXAdjust
,textCtrlYAdjust
);
208 // Draws non-XP GUI dotted line around the focus area
209 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
211 #if !defined(__WXWINCE__)
214 mswRect.left = rect.x;
215 mswRect.top = rect.y;
216 mswRect.right = rect.x + rect.width;
217 mswRect.bottom = rect.y + rect.height;
218 HDC hdc = (HDC) dc.GetHDC();
219 SetMapMode(hdc,MM_TEXT); // Just in case...
220 DrawFocusRect(hdc,&mswRect);
222 // FIXME: Use DrawFocusRect code above (currently it draws solid line
223 // for caption focus but works ok for other stuff).
224 // Also, this code below may not work in future wx versions, since
225 // it employs wxCAP_BUTT hack to have line of width 1.
226 dc
.SetLogicalFunction(wxINVERT
);
228 wxPen
pen(*wxBLACK
,1,wxDOT
);
229 pen
.SetCap(wxCAP_BUTT
);
231 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
233 dc
.DrawRectangle(rect
);
235 dc
.SetLogicalFunction(wxCOPY
);
237 dc
.SetLogicalFunction(wxINVERT
);
239 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
240 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
242 dc
.DrawRectangle(rect
);
244 dc
.SetLogicalFunction(wxCOPY
);
248 // draw focus background on area in a way typical on platform
249 void wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
251 wxUxThemeEngine
* theme
= (wxUxThemeEngine
*) NULL
;
253 // Constructor only calls GetHWND() const, so it should be safe
254 // to cast "this" to const.
255 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
258 wxSize sz
= GetClientSize();
260 bool isFocused
; // also selected
262 // For smaller size control (and for disabled background) use less spacing
266 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
269 isEnabled
= IsEnabled();
270 isFocused
= ShouldDrawFocus();
272 // Windows-style: for smaller size control (and for disabled background) use less spacing
276 focusSpacingX
= isEnabled
? 2 : 1;
277 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
296 // Drawing a list item
297 isEnabled
= true; // they are never disabled
298 isFocused
= flags
& wxCONTROL_SELECTED
? true : false;
304 // Set the background sub-rectangle for selection, disabled etc
305 wxRect
selRect(rect
);
306 selRect
.y
+= focusSpacingY
;
307 selRect
.height
-= (focusSpacingY
*2);
311 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
312 wcp
+= m_widthCustomPaint
;
314 selRect
.x
+= wcp
+ focusSpacingX
;
315 selRect
.width
-= wcp
+ (focusSpacingX
*2);
318 theme
= wxUxThemeEngine::GetIfActive();
321 bool drawDottedEdge
= false;
325 // If popup is hidden and this control is focused,
326 // then draw the focus-indicator (selbgcolor background etc.).
330 // TODO: Proper theme color getting (JMS: I don't know which parts/colors to use,
331 // those below don't work)
334 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_SELECTED
,TMT_TEXTCOLOR
,&cref
);
335 dc
.SetTextForeground( wxRGBToColour(cref
) );
336 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_SELECTED
,TMT_FILLCOLOR
,&cref
);
337 bgCol
= wxRGBToColour(cref
);
342 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
343 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
344 if ( m_windowStyle
& wxCB_READONLY
)
345 drawDottedEdge
= true;
352 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
353 dc.SetTextForeground( wxRGBToColour(cref) );
354 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_FILLCOLOR,&cref);
355 bgCol = wxRGBToColour(cref);
359 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
360 bgCol
= GetBackgroundColour();
368 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_DISABLED,TMT_TEXTCOLOR,&cref);
369 dc.SetTextForeground( wxRGBToColour(cref) );
370 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_DISABLED,TMT_EDGEFILLCOLOR,&cref);
371 bgCol = wxRGBToColour(cref);
375 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
376 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
382 dc
.DrawRectangle(selRect
);
383 if ( drawDottedEdge
)
384 wxMSWDrawFocusRect(dc
,selRect
);
386 // Don't clip exactly to the selection rectangle so we can draw
387 // to the non-selected area in front of it.
388 wxRect
clipRect(rect
.x
,rect
.y
,
389 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
390 dc
.SetClippingRegion(clipRect
);
393 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
395 // TODO: Convert drawing in this function to Windows API Code
397 wxSize sz
= GetClientSize();
398 wxAutoBufferedPaintDC
dc(this);
400 const wxRect
& rectb
= m_btnArea
;
401 wxRect rect
= m_tcArea
;
402 bool isEnabled
= IsEnabled();
403 wxColour bgCol
= GetBackgroundColour();
406 wxUxThemeEngine
* theme
= NULL
;
407 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
410 // area around both controls
411 wxRect
rect2(0,0,sz
.x
,sz
.y
);
412 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
418 // Use theme to draw border on XP
421 theme
= wxUxThemeEngine::GetIfActive();
424 // Select correct border colour
426 etsState
= ETS_DISABLED
;
428 etsState
= ETS_NORMAL
;
430 if ( m_widthCustomBorder
)
432 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,etsState
,TMT_BORDERCOLOR
,&cref
);
435 dc
.SetPen( wxRGBToColour(cref
) );
437 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
438 dc
.DrawRectangle(rect2
);
441 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,etsState
,TMT_TEXTCOLOR
,&cref
);
442 fgCol
= wxRGBToColour(cref
);
446 // draw regular background
447 fgCol
= GetForegroundColour();
450 rect2
.Deflate(m_widthCustomBorder
);
455 // clear main background
456 dc
.DrawRectangle(rect
);
458 // Button background with theme?
459 bool drawButBg
= true;
460 if ( hTheme
&& m_blankButtonBg
)
463 wxCopyRectToRECT(rectb
, r
);
465 // Draw parent background if needed (since button looks like its out of
466 // the combo, this is preferred).
467 theme
->DrawThemeParentBackground(GetHwndOf(this),
474 // Standard button rendering
475 DrawButton(dc
,rectb
,drawButBg
);
477 // paint required portion on the control
478 if ( (!m_text
|| m_widthCustomPaint
) )
480 wxASSERT( m_widthCustomPaint
>= 0 );
482 // this is intentionally here to allow drawed rectangle's
483 // right edge to be hidden
485 rect
.width
= m_widthCustomPaint
;
487 dc
.SetFont( GetFont() );
489 dc
.SetClippingRegion(rect
);
490 if ( m_popupInterface
)
491 m_popupInterface
->PaintComboControl(dc
,rect
);
493 wxComboPopup::DefaultPaintComboControl(this,dc
,rect
);
497 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
500 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
501 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
503 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
506 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
508 // if no textctrl and no special double-click, then the entire control acts
510 handlerFlags
|= wxCC_MF_ON_BUTTON
;
511 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
516 if ( isOnButtonArea
|| HasCapture() ||
517 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
519 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
521 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
524 else if ( m_btnState
)
526 // otherwise need to clear the hover status
528 RefreshRect(m_btnArea
);
533 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
534 // See header file for further information on this method.
535 HandleNormalMouseEvent(event
);
539 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
540 static wxUint32
GetUserPreferencesMask()
542 static wxUint32 userPreferencesMask
= 0;
543 static bool valueSet
= false;
546 return userPreferencesMask
;
548 wxRegKey
key(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
549 if( key
.Open(wxRegKey::Read
) )
552 if ( key
.QueryValue(wxT("UserPreferencesMask"), buf
) )
554 if ( buf
.GetDataLen() >= 4 )
556 wxByte
* p
= (wxByte
*) buf
.GetData();
557 userPreferencesMask
= p
[3] + (p
[2]<<8) + (p
[1]<<16) + (p
[0]<<24);
564 return userPreferencesMask
;
568 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
569 void wxComboCtrl::OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
571 bool stopTimer
= false;
573 wxWindow
* popup
= GetPopupControl()->GetControl();
575 // Popup was hidden before it was fully shown?
576 if ( IsPopupWindowState(Hidden
) )
582 wxLongLong t
= ::wxGetLocalTimeMillis();
583 const wxRect
& rect
= m_animRect
;
584 wxWindow
* win
= GetPopupWindow();
586 int pos
= (int) (t
-m_animStart
).GetLo();
587 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
589 int height
= rect
.height
;
590 //int h0 = rect.height;
591 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
592 int y
= (height
- h
);
596 if ( m_animFlags
& ShowAbove
)
598 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
602 popup
->Move( 0, -y
);
603 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
616 DoShowPopup( m_animRect
, m_animFlags
);
621 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
622 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
624 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
626 m_animStart
= ::wxGetLocalTimeMillis();
630 wxWindow
* win
= GetPopupWindow();
631 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
634 m_animTimer
.SetOwner( this, wxID_ANY
);
635 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
637 OnTimerEvent(*((wxTimerEvent
*)NULL
)); // Event is never used, so we can give NULL
646 wxCoord
wxComboCtrl::GetNativeTextIndent() const
648 if ( wxUxThemeEngine::GetIfActive() )
649 return NATIVE_TEXT_INDENT_XP
;
650 return NATIVE_TEXT_INDENT_CLASSIC
;
653 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
655 const bool isPopupShown
= IsPopupShown();
657 switch ( event
.GetKeyCode() )
660 // F4 toggles the popup in the native comboboxes, so emulate them
661 if ( !event
.AltDown() )
672 // On XP or with writable combo in Classic, arrows don't open the
673 // popup but Alt-arrow does
674 if ( event
.AltDown() ||
676 HasFlag(wxCB_READONLY
) &&
677 !wxUxThemeEngine::GetIfActive()
688 #endif // wxUSE_COMBOCTRL