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 // Prepare background for double-buffering or better background theme
206 // support, whichever is possible.
207 SetDoubleBuffered(true);
208 if ( !IsDoubleBuffered() )
209 SetBackgroundStyle( wxBG_STYLE_PAINT
);
211 // Create textctrl, if necessary
212 CreateTextCtrl( wxNO_BORDER
);
214 // Add keyboard input handlers for main control and textctrl
215 InstallInputHandlers();
217 // SetInitialSize should be called last
218 SetInitialSize(size
);
223 wxComboCtrl::~wxComboCtrl()
227 void wxComboCtrl::OnResize()
230 // Recalculates button and textctrl areas
235 if ( wxUxThemeEngine::GetIfActive() )
237 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
242 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
245 // Technically Classic Windows style combo has more narrow button,
246 // but the native renderer doesn't paint it well like that.
248 CalculateAreas(btnWidth
);
250 // Position textctrl using standard routine
251 PositionTextCtrl(0, textCtrlYAdjust
);
254 // Draws non-XP GUI dotted line around the focus area
255 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
257 #if !defined(__WXWINCE__)
260 mswRect.left = rect.x;
261 mswRect.top = rect.y;
262 mswRect.right = rect.x + rect.width;
263 mswRect.bottom = rect.y + rect.height;
264 HDC hdc = (HDC) dc.GetHDC();
265 SetMapMode(hdc,MM_TEXT); // Just in case...
266 DrawFocusRect(hdc,&mswRect);
268 // FIXME: Use DrawFocusRect code above (currently it draws solid line
269 // for caption focus but works ok for other stuff).
270 // Also, this code below may not work in future wx versions, since
271 // it employs wxCAP_BUTT hack to have line of width 1.
272 dc
.SetLogicalFunction(wxINVERT
);
274 wxPen
pen(*wxBLACK
, 1, wxPENSTYLE_DOT
);
275 pen
.SetCap(wxCAP_BUTT
);
277 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
279 dc
.DrawRectangle(rect
);
281 dc
.SetLogicalFunction(wxCOPY
);
283 dc
.SetLogicalFunction(wxINVERT
);
285 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
286 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
288 dc
.DrawRectangle(rect
);
290 dc
.SetLogicalFunction(wxCOPY
);
294 // draw focus background on area in a way typical on platform
296 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
299 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
302 wxSize sz
= GetClientSize();
304 bool doDrawFocusRect
; // also selected
306 // For smaller size control (and for disabled background) use less spacing
310 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
313 isEnabled
= IsEnabled();
314 doDrawFocusRect
= ShouldDrawFocus();
317 // Windows-style: for smaller size control (and for disabled background) use less spacing
321 focusSpacingX
= isEnabled
? 2 : 1;
322 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
342 // Drawing a list item
343 isEnabled
= true; // they are never disabled
344 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
350 // Set the background sub-rectangle for selection, disabled etc
351 wxRect
selRect(rect
);
352 selRect
.y
+= focusSpacingY
;
353 selRect
.height
-= (focusSpacingY
*2);
357 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
358 wcp
+= m_widthCustomPaint
;
360 selRect
.x
+= wcp
+ focusSpacingX
;
361 selRect
.width
-= wcp
+ (focusSpacingX
*2);
363 //wxUxThemeEngine* theme = NULL;
365 // theme = wxUxThemeEngine::GetIfActive();
369 bool doDrawDottedEdge
= false;
370 bool doDrawSelRect
= true;
372 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
373 // (and other cases which are not that apparent).
377 // If popup is hidden and this control is focused,
378 // then draw the focus-indicator (selbgcolor background etc.).
379 if ( doDrawFocusRect
)
381 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
382 // it is not properly defined for combo boxes. Instead, they expect
383 // you to use DrawThemeText.
385 // Here is, however, sample code how to get theme colours:
388 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
389 // dc.SetTextForeground( wxRGBToColour(cref) );
390 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
392 // Vista style read-only combo
393 fgCol
= GetForegroundColour();
394 bgCol
= GetBackgroundColour();
395 doDrawSelRect
= false;
396 doDrawDottedEdge
= true;
400 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
401 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
406 fgCol
= GetForegroundColour();
407 bgCol
= GetBackgroundColour();
408 doDrawSelRect
= false;
413 fgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
);
414 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
417 dc
.SetTextForeground(fgCol
);
422 dc
.DrawRectangle(selRect
);
425 if ( doDrawDottedEdge
)
426 wxMSWDrawFocusRect(dc
, selRect
);
428 // Don't clip exactly to the selection rectangle so we can draw
429 // to the non-selected area in front of it.
430 wxRect
clipRect(rect
.x
,rect
.y
,
431 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
432 dc
.SetClippingRegion(clipRect
);
435 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
437 // TODO: Convert drawing in this function to Windows API Code
439 wxSize sz
= GetClientSize();
440 wxDC
* dcPtr
= wxAutoBufferedPaintDCFactory(this);
443 const wxRect
& rectButton
= m_btnArea
;
444 wxRect rectTextField
= m_tcArea
;
445 wxColour bgCol
= GetBackgroundColour();
448 const bool isEnabled
= IsEnabled();
450 wxMSWDCImpl
*impl
= (wxMSWDCImpl
*) dc
.GetImpl();
451 HDC hDc
= GetHdcOf(*impl
);
452 HWND hWnd
= GetHwndOf(this);
454 wxUxThemeEngine
* theme
= NULL
;
455 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
458 theme
= wxUxThemeEngine::GetIfActive();
459 #endif // wxUSE_UXTHEME
461 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
463 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
465 borderRect
= m_tcArea
;
466 borderRect
.Inflate(1);
469 int drawButFlags
= 0;
474 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
477 wxCopyRectToRECT(borderRect
, rFull
);
480 wxCopyRectToRECT(rectButton
, rButton
);
483 wxCopyRectToRECT(borderRect
, rBorder
);
485 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
486 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
489 // Get some states for themed drawing
494 butState
= CBXS_DISABLED
;
496 // Vista will display the drop-button as depressed always
497 // when the popup window is visilbe
498 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
499 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
501 butState
= CBXS_PRESSED
;
503 else if ( m_btnState
& wxCONTROL_CURRENT
)
509 butState
= CBXS_NORMAL
;
512 int comboBoxPart
= 0; // For XP, use the 'default' part
513 RECT
* rUseForBg
= &rBorder
;
515 bool drawFullButton
= false;
516 int bgState
= butState
;
517 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
519 if ( useVistaComboBox
)
521 // FIXME: Either SetBackgroundColour or GetBackgroundColour
522 // doesn't work under Vista, so here's a temporary
524 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
526 // Draw the entire control as a single button?
527 if ( !isNonStdButton
)
529 if ( HasFlag(wxCB_READONLY
) )
530 drawFullButton
= true;
533 if ( drawFullButton
)
535 comboBoxPart
= CP_READONLY
;
538 // It should be safe enough to update this flag here.
539 m_iFlags
|= wxCC_FULL_BUTTON
;
543 comboBoxPart
= CP_BORDER
;
544 m_iFlags
&= ~wxCC_FULL_BUTTON
;
547 bgState
= CBB_FOCUSED
;
549 bgState
= CBB_NORMAL
;
554 // Draw parent's background, if necessary
555 RECT
* rUseForTb
= NULL
;
557 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
559 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
560 rUseForTb
= &rButton
;
563 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
566 // Draw the control background (including the border)
567 if ( m_widthCustomBorder
> 0 )
569 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
573 // No border. We can't use theme, since it cannot be relied on
574 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
577 dc
.DrawRectangle(borderRect
);
581 // Draw the drop-button
582 if ( !isNonStdButton
)
584 drawButFlags
= Button_BitmapOnly
;
586 int butPart
= CP_DROPDOWNBUTTON
;
588 if ( useVistaComboBox
)
590 if ( drawFullButton
)
592 // We need to alter the button style slightly before
593 // drawing the actual button (but it was good above
594 // when background etc was done).
595 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
596 butState
= CBXS_NORMAL
;
599 if ( m_btnSide
== wxRIGHT
)
600 butPart
= CP_DROPDOWNBUTTONRIGHT
;
602 butPart
= CP_DROPDOWNBUTTONLEFT
;
605 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
607 else if ( useVistaComboBox
&&
608 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
610 // We'll do this, because DrawThemeParentBackground
611 // doesn't seem to be reliable on Vista.
612 drawButFlags
|= Button_PaintBackground
;
618 // Windows 2000 and earlier
619 drawButFlags
= Button_PaintBackground
;
623 dc
.DrawRectangle(borderRect
);
626 // Button rendering (may only do the bitmap on button, depending on the flags)
627 DrawButton( dc
, rectButton
, drawButFlags
);
629 // Paint required portion of the custom image on the control
630 if ( (!m_text
|| m_widthCustomPaint
) )
632 wxASSERT( m_widthCustomPaint
>= 0 );
634 // this is intentionally here to allow drawed rectangle's
635 // right edge to be hidden
637 rectTextField
.width
= m_widthCustomPaint
;
639 dc
.SetFont( GetFont() );
641 dc
.SetClippingRegion(rectTextField
);
642 if ( m_popupInterface
)
643 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
645 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
651 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
654 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
655 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
657 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
660 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
662 // if no textctrl and no special double-click, then the entire control acts
664 handlerFlags
|= wxCC_MF_ON_BUTTON
;
665 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
670 if ( isOnButtonArea
|| HasCapture() ||
671 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
673 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
675 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
678 else if ( m_btnState
)
680 // otherwise need to clear the hover status
682 RefreshRect(m_btnArea
);
687 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
688 // See header file for further information on this method.
689 HandleNormalMouseEvent(event
);
693 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
694 static wxUint32
GetUserPreferencesMask()
696 static wxUint32 userPreferencesMask
= 0;
697 static bool valueSet
= false;
700 return userPreferencesMask
;
702 wxRegKey
* pKey
= NULL
;
703 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
704 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
705 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
709 else if ( key2
.Exists() )
711 else if ( key3
.Exists() )
714 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
717 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
718 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
720 if ( buf
.GetDataLen() >= 4 )
722 wxUint32
* p
= (wxUint32
*) buf
.GetData();
723 userPreferencesMask
= *p
;
730 return userPreferencesMask
;
734 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
735 void wxComboCtrl::DoTimerEvent()
737 bool stopTimer
= false;
739 wxWindow
* win
= GetPopupWindow();
740 wxWindow
* popup
= GetPopupControl()->GetControl();
742 // Popup was hidden before it was fully shown?
743 if ( IsPopupWindowState(Hidden
) )
749 wxLongLong t
= ::wxGetLocalTimeMillis();
750 const wxRect
& rect
= m_animRect
;
752 int pos
= (int) (t
-m_animStart
).GetLo();
753 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
755 int height
= rect
.height
;
756 //int h0 = rect.height;
757 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
758 int y
= (height
- h
);
762 if ( m_animFlags
& ShowAbove
)
764 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
768 // Note that apparently Move() should be called after
769 // SetSize() to reduce (or even eliminate) animation garbage
770 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
771 popup
->Move( 0, -y
);
783 DoShowPopup( m_animRect
, m_animFlags
);
786 // Do a one final refresh to clean up the rare cases of animation
793 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
794 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
796 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
798 m_animStart
= ::wxGetLocalTimeMillis();
802 wxWindow
* win
= GetPopupWindow();
803 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
806 m_animTimer
.SetOwner( this, wxID_ANY
);
807 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
818 wxCoord
wxComboCtrl::GetNativeTextIndent() const
821 if ( wxUxThemeEngine::GetIfActive() )
822 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 case WXK_NUMPAD_DOWN
:
848 // Arrow keys (and mouse wheel) toggle the popup in the native
850 if ( event
.AltDown() )
858 #endif // wxUSE_COMBOCTRL