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::OnThemeChange()
226 // there doesn't seem to be any way to get the text colour using themes
227 // API: TMT_TEXTCOLOR doesn't work neither for EDIT nor COMBOBOX
230 wxColour fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
);
231 SetForegroundColour(fgCol
);
235 // NB: use EDIT, not COMBOBOX (the latter works in XP but not Vista)
236 wxColour bgCol
= MSWGetThemeColour(L
"EDIT",
239 ThemeColourBackground
,
240 wxSYS_COLOUR_WINDOW
);
244 SetBackgroundColour(bgCol
);
249 void wxComboCtrl::OnResize()
252 // Recalculates button and textctrl areas
257 if ( wxUxThemeEngine::GetIfActive() )
259 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
264 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
267 // Technically Classic Windows style combo has more narrow button,
268 // but the native renderer doesn't paint it well like that.
270 CalculateAreas(btnWidth
);
272 // Position textctrl using standard routine
273 PositionTextCtrl(0, textCtrlYAdjust
);
276 // Draws non-XP GUI dotted line around the focus area
277 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
279 #if !defined(__WXWINCE__)
282 mswRect.left = rect.x;
283 mswRect.top = rect.y;
284 mswRect.right = rect.x + rect.width;
285 mswRect.bottom = rect.y + rect.height;
286 HDC hdc = (HDC) dc.GetHDC();
287 SetMapMode(hdc,MM_TEXT); // Just in case...
288 DrawFocusRect(hdc,&mswRect);
290 // FIXME: Use DrawFocusRect code above (currently it draws solid line
291 // for caption focus but works ok for other stuff).
292 // Also, this code below may not work in future wx versions, since
293 // it employs wxCAP_BUTT hack to have line of width 1.
294 dc
.SetLogicalFunction(wxINVERT
);
296 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
297 pen
.SetCap(wxCAP_BUTT
);
299 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
301 dc
.DrawRectangle(rect
);
303 dc
.SetLogicalFunction(wxCOPY
);
305 dc
.SetLogicalFunction(wxINVERT
);
307 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
308 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
310 dc
.DrawRectangle(rect
);
312 dc
.SetLogicalFunction(wxCOPY
);
316 // draw focus background on area in a way typical on platform
318 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
321 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
324 wxSize sz
= GetClientSize();
326 bool doDrawFocusRect
; // also selected
328 // For smaller size control (and for disabled background) use less spacing
332 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
335 isEnabled
= IsEnabled();
336 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;
364 // Drawing a list item
365 isEnabled
= true; // they are never disabled
366 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
372 // Set the background sub-rectangle for selection, disabled etc
373 wxRect
selRect(rect
);
374 selRect
.y
+= focusSpacingY
;
375 selRect
.height
-= (focusSpacingY
*2);
379 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
380 wcp
+= m_widthCustomPaint
;
382 selRect
.x
+= wcp
+ focusSpacingX
;
383 selRect
.width
-= wcp
+ (focusSpacingX
*2);
385 //wxUxThemeEngine* theme = NULL;
387 // theme = wxUxThemeEngine::GetIfActive();
391 bool doDrawDottedEdge
= false;
392 bool doDrawSelRect
= true;
394 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
395 // (and other cases which are not that apparent).
399 // If popup is hidden and this control is focused,
400 // then draw the focus-indicator (selbgcolor background etc.).
401 if ( doDrawFocusRect
)
403 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
404 // it is not properly defined for combo boxes. Instead, they expect
405 // you to use DrawThemeText.
407 // Here is, however, sample code how to get theme colours:
410 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
411 // dc.SetTextForeground( wxRGBToColour(cref) );
412 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
414 // Vista style read-only combo
415 fgCol
= GetForegroundColour();
416 bgCol
= GetBackgroundColour();
417 doDrawSelRect
= false;
418 doDrawDottedEdge
= true;
422 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
423 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
428 fgCol
= GetForegroundColour();
429 bgCol
= GetBackgroundColour();
430 doDrawSelRect
= false;
435 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
436 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
439 dc
.SetTextForeground(fgCol
);
444 dc
.DrawRectangle(selRect
);
447 if ( doDrawDottedEdge
)
448 wxMSWDrawFocusRect(dc
, selRect
);
450 // Don't clip exactly to the selection rectangle so we can draw
451 // to the non-selected area in front of it.
452 wxRect
clipRect(rect
.x
,rect
.y
,
453 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
454 dc
.SetClippingRegion(clipRect
);
457 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
459 // TODO: Convert drawing in this function to Windows API Code
461 wxSize sz
= GetClientSize();
462 wxAutoBufferedPaintDC
dc(this);
464 const wxRect
& rectButton
= m_btnArea
;
465 wxRect rectTextField
= m_tcArea
;
466 wxColour bgCol
= GetBackgroundColour();
469 const bool isEnabled
= IsEnabled();
471 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
472 HDC hDc
= GetHdcOf(*impl
);
473 HWND hWnd
= GetHwndOf(this);
475 wxUxThemeEngine
* theme
= NULL
;
476 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
479 theme
= wxUxThemeEngine::GetIfActive();
480 #endif // wxUSE_UXTHEME
482 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
484 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
486 borderRect
= m_tcArea
;
487 borderRect
.Inflate(1);
490 int drawButFlags
= 0;
495 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
498 wxCopyRectToRECT(borderRect
, rFull
);
501 wxCopyRectToRECT(rectButton
, rButton
);
504 wxCopyRectToRECT(borderRect
, rBorder
);
506 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
507 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
510 // Get some states for themed drawing
515 butState
= CBXS_DISABLED
;
517 // Vista will display the drop-button as depressed always
518 // when the popup window is visilbe
519 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
520 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
522 butState
= CBXS_PRESSED
;
524 else if ( m_btnState
& wxCONTROL_CURRENT
)
530 butState
= CBXS_NORMAL
;
533 int comboBoxPart
= 0; // For XP, use the 'default' part
534 RECT
* rUseForBg
= &rBorder
;
536 bool drawFullButton
= false;
537 int bgState
= butState
;
538 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
540 if ( useVistaComboBox
)
542 // FIXME: Either SetBackgroundColour or GetBackgroundColour
543 // doesn't work under Vista, so here's a temporary
545 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
547 // Draw the entire control as a single button?
548 if ( !isNonStdButton
)
550 if ( HasFlag(wxCB_READONLY
) )
551 drawFullButton
= true;
554 if ( drawFullButton
)
556 comboBoxPart
= CP_READONLY
;
559 // It should be safe enough to update this flag here.
560 m_iFlags
|= wxCC_FULL_BUTTON
;
564 comboBoxPart
= CP_BORDER
;
565 m_iFlags
&= ~wxCC_FULL_BUTTON
;
568 bgState
= CBB_FOCUSED
;
570 bgState
= CBB_NORMAL
;
575 // Draw parent's background, if necessary
576 RECT
* rUseForTb
= NULL
;
578 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
580 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
581 rUseForTb
= &rButton
;
584 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
587 // Draw the control background (including the border)
588 if ( m_widthCustomBorder
> 0 )
590 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
594 // No border. We can't use theme, since it cannot be relied on
595 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
598 dc
.DrawRectangle(borderRect
);
602 // Draw the drop-button
603 if ( !isNonStdButton
)
605 drawButFlags
= Button_BitmapOnly
;
607 int butPart
= CP_DROPDOWNBUTTON
;
609 if ( useVistaComboBox
)
611 if ( drawFullButton
)
613 // We need to alter the button style slightly before
614 // drawing the actual button (but it was good above
615 // when background etc was done).
616 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
617 butState
= CBXS_NORMAL
;
620 if ( m_btnSide
== wxRIGHT
)
621 butPart
= CP_DROPDOWNBUTTONRIGHT
;
623 butPart
= CP_DROPDOWNBUTTONLEFT
;
626 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
628 else if ( useVistaComboBox
&&
629 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
631 // We'll do this, because DrawThemeParentBackground
632 // doesn't seem to be reliable on Vista.
633 drawButFlags
|= Button_PaintBackground
;
639 // Windows 2000 and earlier
640 drawButFlags
= Button_PaintBackground
;
644 dc
.DrawRectangle(borderRect
);
647 // Button rendering (may only do the bitmap on button, depending on the flags)
648 DrawButton( dc
, rectButton
, drawButFlags
);
650 // Paint required portion of the custom image on the control
651 if ( (!m_text
|| m_widthCustomPaint
) )
653 wxASSERT( m_widthCustomPaint
>= 0 );
655 // this is intentionally here to allow drawed rectangle's
656 // right edge to be hidden
658 rectTextField
.width
= m_widthCustomPaint
;
660 dc
.SetFont( GetFont() );
662 dc
.SetClippingRegion(rectTextField
);
663 if ( m_popupInterface
)
664 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
666 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
670 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
673 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
674 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
676 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
679 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
681 // if no textctrl and no special double-click, then the entire control acts
683 handlerFlags
|= wxCC_MF_ON_BUTTON
;
684 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
689 if ( isOnButtonArea
|| HasCapture() ||
690 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
692 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
694 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
697 else if ( m_btnState
)
699 // otherwise need to clear the hover status
701 RefreshRect(m_btnArea
);
706 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
707 // See header file for further information on this method.
708 HandleNormalMouseEvent(event
);
712 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
713 static wxUint32
GetUserPreferencesMask()
715 static wxUint32 userPreferencesMask
= 0;
716 static bool valueSet
= false;
719 return userPreferencesMask
;
721 wxRegKey
* pKey
= NULL
;
722 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
723 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
724 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
728 else if ( key2
.Exists() )
730 else if ( key3
.Exists() )
733 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
736 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
737 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
739 if ( buf
.GetDataLen() >= 4 )
741 wxUint32
* p
= (wxUint32
*) buf
.GetData();
742 userPreferencesMask
= *p
;
749 return userPreferencesMask
;
753 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
754 void wxComboCtrl::DoTimerEvent()
756 bool stopTimer
= false;
758 wxWindow
* popup
= GetPopupControl()->GetControl();
760 // Popup was hidden before it was fully shown?
761 if ( IsPopupWindowState(Hidden
) )
767 wxLongLong t
= ::wxGetLocalTimeMillis();
768 const wxRect
& rect
= m_animRect
;
769 wxWindow
* win
= GetPopupWindow();
771 int pos
= (int) (t
-m_animStart
).GetLo();
772 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
774 int height
= rect
.height
;
775 //int h0 = rect.height;
776 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
777 int y
= (height
- h
);
781 if ( m_animFlags
& ShowAbove
)
783 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
787 popup
->Move( 0, -y
);
788 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
801 DoShowPopup( m_animRect
, m_animFlags
);
806 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
807 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
809 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
811 m_animStart
= ::wxGetLocalTimeMillis();
815 wxWindow
* win
= GetPopupWindow();
816 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
819 m_animTimer
.SetOwner( this, wxID_ANY
);
820 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
831 wxCoord
wxComboCtrl::GetNativeTextIndent() const
834 if ( wxUxThemeEngine::GetIfActive() )
835 return NATIVE_TEXT_INDENT_XP
;
837 return NATIVE_TEXT_INDENT_CLASSIC
;
840 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
842 const bool isPopupShown
= IsPopupShown();
844 switch ( event
.GetKeyCode() )
847 // F4 toggles the popup in the native comboboxes, so emulate them
848 if ( !event
.AltDown() )
859 // On XP or with writable combo in Classic, arrows don't open the
860 // popup but Alt-arrow does
861 if ( event
.AltDown() ||
863 HasFlag(wxCB_READONLY
)
865 && !wxUxThemeEngine::GetIfActive()
877 #endif // wxUSE_COMBOCTRL