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
250 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
252 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
255 wxSize sz
= GetClientSize();
257 bool isFocused
; // also selected
259 // For smaller size control (and for disabled background) use less spacing
263 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
266 isEnabled
= IsEnabled();
267 isFocused
= ShouldDrawFocus();
269 // Windows-style: for smaller size control (and for disabled background) use less spacing
273 focusSpacingX
= isEnabled
? 2 : 1;
274 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
293 // Drawing a list item
294 isEnabled
= true; // they are never disabled
295 isFocused
= flags
& wxCONTROL_SELECTED
? true : false;
301 // Set the background sub-rectangle for selection, disabled etc
302 wxRect
selRect(rect
);
303 selRect
.y
+= focusSpacingY
;
304 selRect
.height
-= (focusSpacingY
*2);
308 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
309 wcp
+= m_widthCustomPaint
;
311 selRect
.x
+= wcp
+ focusSpacingX
;
312 selRect
.width
-= wcp
+ (focusSpacingX
*2);
314 //wxUxThemeEngine* theme = (wxUxThemeEngine*) NULL;
316 // theme = wxUxThemeEngine::GetIfActive();
319 bool drawDottedEdge
= false;
323 // If popup is hidden and this control is focused,
324 // then draw the focus-indicator (selbgcolor background etc.).
328 // TODO: Proper theme color getting (JMS: I don't know which parts/colors to use,
329 // those below don't work)
332 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_SELECTED
,TMT_TEXTCOLOR
,&cref
);
333 dc
.SetTextForeground( wxRGBToColour(cref
) );
334 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_SELECTED
,TMT_FILLCOLOR
,&cref
);
335 bgCol
= wxRGBToColour(cref
);
340 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
341 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
342 if ( m_windowStyle
& wxCB_READONLY
)
343 drawDottedEdge
= true;
350 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
351 dc.SetTextForeground( wxRGBToColour(cref) );
352 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_FILLCOLOR,&cref);
353 bgCol = wxRGBToColour(cref);
357 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
358 bgCol
= GetBackgroundColour();
366 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_DISABLED,TMT_TEXTCOLOR,&cref);
367 dc.SetTextForeground( wxRGBToColour(cref) );
368 theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_DISABLED,TMT_EDGEFILLCOLOR,&cref);
369 bgCol = wxRGBToColour(cref);
373 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
374 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
380 dc
.DrawRectangle(selRect
);
381 if ( drawDottedEdge
)
382 wxMSWDrawFocusRect(dc
,selRect
);
384 // Don't clip exactly to the selection rectangle so we can draw
385 // to the non-selected area in front of it.
386 wxRect
clipRect(rect
.x
,rect
.y
,
387 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
388 dc
.SetClippingRegion(clipRect
);
391 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
393 // TODO: Convert drawing in this function to Windows API Code
395 wxSize sz
= GetClientSize();
396 wxAutoBufferedPaintDC
dc(this);
398 const wxRect
& rectb
= m_btnArea
;
399 wxRect rect
= m_tcArea
;
400 bool isEnabled
= IsEnabled();
401 wxColour bgCol
= GetBackgroundColour();
404 wxUxThemeEngine
* theme
= NULL
;
405 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
408 // area around both controls
409 wxRect
rect2(0,0,sz
.x
,sz
.y
);
410 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
416 // Use theme to draw border on XP
419 theme
= wxUxThemeEngine::GetIfActive();
422 // Select correct border colour
424 etsState
= ETS_DISABLED
;
426 etsState
= ETS_NORMAL
;
428 if ( m_widthCustomBorder
)
430 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,etsState
,TMT_BORDERCOLOR
,&cref
);
433 dc
.SetPen( wxRGBToColour(cref
) );
435 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
436 dc
.DrawRectangle(rect2
);
439 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,etsState
,TMT_TEXTCOLOR
,&cref
);
440 fgCol
= wxRGBToColour(cref
);
444 // draw regular background
445 fgCol
= GetForegroundColour();
448 rect2
.Deflate(m_widthCustomBorder
);
453 // clear main background
454 dc
.DrawRectangle(rect
);
456 // Button background with theme?
457 bool drawButBg
= true;
458 if ( hTheme
&& m_blankButtonBg
)
461 wxCopyRectToRECT(rectb
, r
);
463 // Draw parent background if needed (since button looks like its out of
464 // the combo, this is preferred).
465 theme
->DrawThemeParentBackground(GetHwndOf(this),
472 // Standard button rendering
473 DrawButton(dc
,rectb
,drawButBg
);
475 // paint required portion on the control
476 if ( (!m_text
|| m_widthCustomPaint
) )
478 wxASSERT( m_widthCustomPaint
>= 0 );
480 // this is intentionally here to allow drawed rectangle's
481 // right edge to be hidden
483 rect
.width
= m_widthCustomPaint
;
485 dc
.SetFont( GetFont() );
487 dc
.SetClippingRegion(rect
);
488 if ( m_popupInterface
)
489 m_popupInterface
->PaintComboControl(dc
,rect
);
491 wxComboPopup::DefaultPaintComboControl(this,dc
,rect
);
495 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
498 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
499 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
501 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
504 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
506 // if no textctrl and no special double-click, then the entire control acts
508 handlerFlags
|= wxCC_MF_ON_BUTTON
;
509 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
514 if ( isOnButtonArea
|| HasCapture() ||
515 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
517 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
519 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
522 else if ( m_btnState
)
524 // otherwise need to clear the hover status
526 RefreshRect(m_btnArea
);
531 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
532 // See header file for further information on this method.
533 HandleNormalMouseEvent(event
);
537 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
538 static wxUint32
GetUserPreferencesMask()
540 static wxUint32 userPreferencesMask
= 0;
541 static bool valueSet
= false;
544 return userPreferencesMask
;
546 wxRegKey
key(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
547 if( key
.Open(wxRegKey::Read
) )
550 if ( key
.QueryValue(wxT("UserPreferencesMask"), buf
) )
552 if ( buf
.GetDataLen() >= 4 )
554 wxByte
* p
= (wxByte
*) buf
.GetData();
555 userPreferencesMask
= p
[3] + (p
[2]<<8) + (p
[1]<<16) + (p
[0]<<24);
562 return userPreferencesMask
;
566 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
567 void wxComboCtrl::OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
569 bool stopTimer
= false;
571 wxWindow
* popup
= GetPopupControl()->GetControl();
573 // Popup was hidden before it was fully shown?
574 if ( IsPopupWindowState(Hidden
) )
580 wxLongLong t
= ::wxGetLocalTimeMillis();
581 const wxRect
& rect
= m_animRect
;
582 wxWindow
* win
= GetPopupWindow();
584 int pos
= (int) (t
-m_animStart
).GetLo();
585 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
587 int height
= rect
.height
;
588 //int h0 = rect.height;
589 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
590 int y
= (height
- h
);
594 if ( m_animFlags
& ShowAbove
)
596 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
600 popup
->Move( 0, -y
);
601 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
614 DoShowPopup( m_animRect
, m_animFlags
);
619 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
620 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
622 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
624 m_animStart
= ::wxGetLocalTimeMillis();
628 wxWindow
* win
= GetPopupWindow();
629 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
632 m_animTimer
.SetOwner( this, wxID_ANY
);
633 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
635 OnTimerEvent(*((wxTimerEvent
*)NULL
)); // Event is never used, so we can give NULL
644 wxCoord
wxComboCtrl::GetNativeTextIndent() const
646 if ( wxUxThemeEngine::GetIfActive() )
647 return NATIVE_TEXT_INDENT_XP
;
648 return NATIVE_TEXT_INDENT_CLASSIC
;
651 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
653 const bool isPopupShown
= IsPopupShown();
655 switch ( event
.GetKeyCode() )
658 // F4 toggles the popup in the native comboboxes, so emulate them
659 if ( !event
.AltDown() )
670 // On XP or with writable combo in Classic, arrows don't open the
671 // popup but Alt-arrow does
672 if ( event
.AltDown() ||
674 HasFlag(wxCB_READONLY
) &&
675 !wxUxThemeEngine::GetIfActive()
686 #endif // wxUSE_COMBOCTRL