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 wxUxThemeEngine
* theme
= wxUxThemeEngine::GetIfActive();
222 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
225 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_NORMAL
,TMT_FILLCOLOR
,&col
);
226 SetBackgroundColour(wxRGBToColour(col
));
227 theme
->GetThemeColor(hTheme
,EP_EDITTEXT
,ETS_NORMAL
,TMT_TEXTCOLOR
,&col
);
228 SetForegroundColour(wxRGBToColour(col
));
232 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
233 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
237 void wxComboCtrl::OnResize()
240 // Recalculates button and textctrl areas
245 if ( wxUxThemeEngine::GetIfActive() )
247 textCtrlXAdjust
= TEXTCTRLXADJUST_XP
;
248 textCtrlYAdjust
= TEXTCTRLYADJUST_XP
;
252 textCtrlXAdjust
= TEXTCTRLXADJUST_CLASSIC
;
253 textCtrlYAdjust
= TEXTCTRLYADJUST_CLASSIC
;
256 // Technically Classic Windows style combo has more narrow button,
257 // but the native renderer doesn't paint it well like that.
259 CalculateAreas(btnWidth
);
261 // Position textctrl using standard routine
262 PositionTextCtrl(textCtrlXAdjust
,textCtrlYAdjust
);
265 // Draws non-XP GUI dotted line around the focus area
266 static void wxMSWDrawFocusRect( wxDC
& dc
, const wxRect
& rect
)
268 #if !defined(__WXWINCE__)
271 mswRect.left = rect.x;
272 mswRect.top = rect.y;
273 mswRect.right = rect.x + rect.width;
274 mswRect.bottom = rect.y + rect.height;
275 HDC hdc = (HDC) dc.GetHDC();
276 SetMapMode(hdc,MM_TEXT); // Just in case...
277 DrawFocusRect(hdc,&mswRect);
279 // FIXME: Use DrawFocusRect code above (currently it draws solid line
280 // for caption focus but works ok for other stuff).
281 // Also, this code below may not work in future wx versions, since
282 // it employs wxCAP_BUTT hack to have line of width 1.
283 dc
.SetLogicalFunction(wxINVERT
);
285 wxPen
pen(*wxBLACK
,1,wxDOT
);
286 pen
.SetCap(wxCAP_BUTT
);
288 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
290 dc
.DrawRectangle(rect
);
292 dc
.SetLogicalFunction(wxCOPY
);
294 dc
.SetLogicalFunction(wxINVERT
);
296 dc
.SetPen(wxPen(*wxBLACK
,1,wxDOT
));
297 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
299 dc
.DrawRectangle(rect
);
301 dc
.SetLogicalFunction(wxCOPY
);
305 // draw focus background on area in a way typical on platform
307 wxComboCtrl::PrepareBackground( wxDC
& dc
, const wxRect
& rect
, int flags
) const
309 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
311 wxSize sz
= GetClientSize();
313 bool doDrawFocusRect
; // also selected
315 // For smaller size control (and for disabled background) use less spacing
319 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
322 isEnabled
= IsEnabled();
323 doDrawFocusRect
= ShouldDrawFocus();
325 // Windows-style: for smaller size control (and for disabled background) use less spacing
329 focusSpacingX
= isEnabled
? 2 : 1;
330 focusSpacingY
= sz
.y
> (GetCharHeight()+2) && isEnabled
? 2 : 1;
349 // Drawing a list item
350 isEnabled
= true; // they are never disabled
351 doDrawFocusRect
= flags
& wxCONTROL_SELECTED
? true : false;
357 // Set the background sub-rectangle for selection, disabled etc
358 wxRect
selRect(rect
);
359 selRect
.y
+= focusSpacingY
;
360 selRect
.height
-= (focusSpacingY
*2);
364 if ( !(flags
& wxCONTROL_ISSUBMENU
) )
365 wcp
+= m_widthCustomPaint
;
367 selRect
.x
+= wcp
+ focusSpacingX
;
368 selRect
.width
-= wcp
+ (focusSpacingX
*2);
370 //wxUxThemeEngine* theme = (wxUxThemeEngine*) NULL;
372 // theme = wxUxThemeEngine::GetIfActive();
375 bool doDrawDottedEdge
= false;
376 bool doDrawSelRect
= true;
378 // TODO: doDrawDottedEdge = true when focus has arrived to control via tab.
379 // (and other cases which are not that apparent).
383 // If popup is hidden and this control is focused,
384 // then draw the focus-indicator (selbgcolor background etc.).
385 if ( doDrawFocusRect
)
387 // NB: We can't really use XP visual styles to get TMT_TEXTCOLOR since
388 // it is not properly defined for combo boxes. Instead, they expect
389 // you to use DrawThemeText.
391 // Here is, however, sample code how to get theme colours:
394 // theme->GetThemeColor(hTheme,EP_EDITTEXT,ETS_NORMAL,TMT_TEXTCOLOR,&cref);
395 // dc.SetTextForeground( wxRGBToColour(cref) );
396 if ( (m_iFlags
& wxCC_FULL_BUTTON
) && !(flags
& wxCONTROL_ISSUBMENU
) )
398 // Vista style read-only combo
399 doDrawSelRect
= false;
400 doDrawDottedEdge
= true;
404 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) );
405 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHT
);
410 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
) );
411 bgCol
= GetBackgroundColour();
412 doDrawSelRect
= false;
417 dc
.SetTextForeground( wxSystemSettings::GetColour(wxSYS_COLOUR_GRAYTEXT
) );
418 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE
);
425 dc
.DrawRectangle(selRect
);
428 if ( doDrawDottedEdge
)
429 wxMSWDrawFocusRect(dc
, selRect
);
431 // Don't clip exactly to the selection rectangle so we can draw
432 // to the non-selected area in front of it.
433 wxRect
clipRect(rect
.x
,rect
.y
,
434 (selRect
.x
+selRect
.width
)-rect
.x
-1,rect
.height
);
435 dc
.SetClippingRegion(clipRect
);
438 void wxComboCtrl::OnPaintEvent( wxPaintEvent
& WXUNUSED(event
) )
440 // TODO: Convert drawing in this function to Windows API Code
442 wxSize sz
= GetClientSize();
443 wxAutoBufferedPaintDC
dc(this);
445 const wxRect
& rectButton
= m_btnArea
;
446 wxRect rectTextField
= m_tcArea
;
447 const bool isEnabled
= IsEnabled();
448 wxColour bgCol
= GetBackgroundColour();
450 HDC hDc
= GetHdcOf(dc
);
451 HWND hWnd
= GetHwndOf(this);
453 wxUxThemeEngine
* theme
= NULL
;
454 wxUxThemeHandle
hTheme(this, L
"COMBOBOX");
457 theme
= wxUxThemeEngine::GetIfActive();
459 wxRect
borderRect(0,0,sz
.x
,sz
.y
);
461 if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
463 borderRect
= m_tcArea
;
464 borderRect
.Inflate(1);
467 int drawButFlags
= 0;
471 const bool useVistaComboBox
= ::wxGetWinVersion() >= wxWinVersion_Vista
;
474 wxCopyRectToRECT(borderRect
, rFull
);
477 wxCopyRectToRECT(rectButton
, rButton
);
480 wxCopyRectToRECT(borderRect
, rBorder
);
482 bool isNonStdButton
= (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) ||
483 (m_iFlags
& wxCC_IFLAG_HAS_NONSTANDARD_BUTTON
);
486 // Get some states for themed drawing
491 butState
= CBXS_DISABLED
;
493 // Vista will display the drop-button as depressed always
494 // when the popup window is visilbe
495 else if ( (m_btnState
& wxCONTROL_PRESSED
) ||
496 (useVistaComboBox
&& !IsPopupWindowState(Hidden
)) )
498 butState
= CBXS_PRESSED
;
500 else if ( m_btnState
& wxCONTROL_CURRENT
)
506 butState
= CBXS_NORMAL
;
509 int comboBoxPart
= 0; // For XP, use the 'default' part
510 RECT
* rUseForBg
= &rBorder
;
512 bool drawFullButton
= false;
513 int bgState
= butState
;
514 const bool isFocused
= (FindFocus() == GetMainWindowOfCompositeControl()) ? true : false;
516 if ( useVistaComboBox
)
518 // FIXME: Either SetBackgroundColour or GetBackgroundColour
519 // doesn't work under Vista, so here's a temporary
521 bgCol
= wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
);
523 // Draw the entire control as a single button?
524 if ( !isNonStdButton
)
526 if ( HasFlag(wxCB_READONLY
) )
527 drawFullButton
= true;
530 if ( drawFullButton
)
532 comboBoxPart
= CP_READONLY
;
535 // It should be safe enough to update this flag here.
536 m_iFlags
|= wxCC_FULL_BUTTON
;
540 comboBoxPart
= CP_BORDER
;
541 m_iFlags
&= ~wxCC_FULL_BUTTON
;
544 bgState
= CBB_FOCUSED
;
546 bgState
= CBB_NORMAL
;
551 // Draw parent's background, if necessary
552 RECT
* rUseForTb
= NULL
;
554 if ( theme
->IsThemeBackgroundPartiallyTransparent( hTheme
, comboBoxPart
, bgState
) )
556 else if ( m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
)
557 rUseForTb
= &rButton
;
560 theme
->DrawThemeParentBackground( hWnd
, hDc
, rUseForTb
);
563 // Draw the control background (including the border)
564 if ( m_widthCustomBorder
> 0 )
566 theme
->DrawThemeBackground( hTheme
, hDc
, comboBoxPart
, bgState
, rUseForBg
, NULL
);
570 // No border. We can't use theme, since it cannot be relied on
571 // to deliver borderless drawing, even with DrawThemeBackgroundEx.
574 dc
.DrawRectangle(borderRect
);
578 // Draw the drop-button
579 if ( !isNonStdButton
)
581 drawButFlags
= Button_BitmapOnly
;
583 int butPart
= CP_DROPDOWNBUTTON
;
585 if ( useVistaComboBox
)
587 if ( drawFullButton
)
589 // We need to alter the button style slightly before
590 // drawing the actual button (but it was good above
591 // when background etc was done).
592 if ( butState
== CBXS_HOT
|| butState
== CBXS_PRESSED
)
593 butState
= CBXS_NORMAL
;
596 if ( m_btnSide
== wxRIGHT
)
597 butPart
= CP_DROPDOWNBUTTONRIGHT
;
599 butPart
= CP_DROPDOWNBUTTONLEFT
;
602 theme
->DrawThemeBackground( hTheme
, hDc
, butPart
, butState
, &rButton
, NULL
);
604 else if ( useVistaComboBox
&&
605 (m_iFlags
& wxCC_IFLAG_BUTTON_OUTSIDE
) )
607 // We'll do this, because DrawThemeParentBackground
608 // doesn't seem to be reliable on Vista.
609 drawButFlags
|= Button_PaintBackground
;
614 // Windows 2000 and earlier
615 drawButFlags
= Button_PaintBackground
;
619 dc
.DrawRectangle(borderRect
);
622 // Button rendering (may only do the bitmap on button, depending on the flags)
623 DrawButton( dc
, rectButton
, drawButFlags
);
625 // Paint required portion of the custom image on the control
626 if ( (!m_text
|| m_widthCustomPaint
) )
628 wxASSERT( m_widthCustomPaint
>= 0 );
630 // this is intentionally here to allow drawed rectangle's
631 // right edge to be hidden
633 rectTextField
.width
= m_widthCustomPaint
;
635 dc
.SetFont( GetFont() );
637 dc
.SetClippingRegion(rectTextField
);
638 if ( m_popupInterface
)
639 m_popupInterface
->PaintComboControl(dc
,rectTextField
);
641 wxComboPopup::DefaultPaintComboControl(this,dc
,rectTextField
);
645 void wxComboCtrl::OnMouseEvent( wxMouseEvent
& event
)
648 bool isOnButtonArea
= m_btnArea
.Contains(mx
,event
.m_y
);
649 int handlerFlags
= isOnButtonArea
? wxCC_MF_ON_BUTTON
: 0;
651 if ( PreprocessMouseEvent(event
,isOnButtonArea
) )
654 if ( (m_windowStyle
& (wxCC_SPECIAL_DCLICK
|wxCB_READONLY
)) == wxCB_READONLY
)
656 // if no textctrl and no special double-click, then the entire control acts
658 handlerFlags
|= wxCC_MF_ON_BUTTON
;
659 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
664 if ( isOnButtonArea
|| HasCapture() ||
665 (m_widthCustomPaint
&& mx
< (m_tcArea
.x
+m_widthCustomPaint
)) )
667 handlerFlags
|= wxCC_MF_ON_CLICK_AREA
;
669 if ( HandleButtonMouseEvent(event
,handlerFlags
) )
672 else if ( m_btnState
)
674 // otherwise need to clear the hover status
676 RefreshRect(m_btnArea
);
681 // This will handle left_down and left_dclick events outside button in a Windows-like manner.
682 // See header file for further information on this method.
683 HandleNormalMouseEvent(event
);
687 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
688 static wxUint32
GetUserPreferencesMask()
690 static wxUint32 userPreferencesMask
= 0;
691 static bool valueSet
= false;
694 return userPreferencesMask
;
696 wxRegKey
* pKey
= NULL
;
697 wxRegKey
key1(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Control Panel"));
698 wxRegKey
key2(wxRegKey::HKCU
, wxT("Software\\Policies\\Microsoft\\Windows\\Control Panel"));
699 wxRegKey
key3(wxRegKey::HKCU
, wxT("Control Panel\\Desktop"));
703 else if ( key2
.Exists() )
705 else if ( key3
.Exists() )
708 if ( pKey
&& pKey
->Open(wxRegKey::Read
) )
711 if ( pKey
->HasValue(wxT("UserPreferencesMask")) &&
712 pKey
->QueryValue(wxT("UserPreferencesMask"), buf
) )
714 if ( buf
.GetDataLen() >= 4 )
716 wxUint32
* p
= (wxUint32
*) buf
.GetData();
717 userPreferencesMask
= *p
;
724 return userPreferencesMask
;
728 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
729 void wxComboCtrl::OnTimerEvent( wxTimerEvent
& WXUNUSED(event
) )
731 bool stopTimer
= false;
733 wxWindow
* popup
= GetPopupControl()->GetControl();
735 // Popup was hidden before it was fully shown?
736 if ( IsPopupWindowState(Hidden
) )
742 wxLongLong t
= ::wxGetLocalTimeMillis();
743 const wxRect
& rect
= m_animRect
;
744 wxWindow
* win
= GetPopupWindow();
746 int pos
= (int) (t
-m_animStart
).GetLo();
747 if ( pos
< COMBOBOX_ANIMATION_DURATION
)
749 int height
= rect
.height
;
750 //int h0 = rect.height;
751 int h
= (((pos
*256)/COMBOBOX_ANIMATION_DURATION
)*height
)/256;
752 int y
= (height
- h
);
756 if ( m_animFlags
& ShowAbove
)
758 win
->SetSize( rect
.x
, rect
.y
+ height
- h
, rect
.width
, h
);
762 popup
->Move( 0, -y
);
763 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, h
);
776 DoShowPopup( m_animRect
, m_animFlags
);
781 #if wxUSE_COMBOCTRL_POPUP_ANIMATION
782 bool wxComboCtrl::AnimateShow( const wxRect
& rect
, int flags
)
784 if ( GetUserPreferencesMask() & wxMSW_DESKTOP_USERPREFERENCESMASK_COMBOBOXANIM
)
786 m_animStart
= ::wxGetLocalTimeMillis();
790 wxWindow
* win
= GetPopupWindow();
791 win
->SetSize( rect
.x
, rect
.y
, rect
.width
, 0 );
794 m_animTimer
.SetOwner( this, wxID_ANY
);
795 m_animTimer
.Start( COMBOBOX_ANIMATION_RESOLUTION
, wxTIMER_CONTINUOUS
);
797 OnTimerEvent(*((wxTimerEvent
*)NULL
)); // Event is never used, so we can give NULL
806 wxCoord
wxComboCtrl::GetNativeTextIndent() const
808 if ( wxUxThemeEngine::GetIfActive() )
809 return NATIVE_TEXT_INDENT_XP
;
810 return NATIVE_TEXT_INDENT_CLASSIC
;
813 bool wxComboCtrl::IsKeyPopupToggle(const wxKeyEvent
& event
) const
815 const bool isPopupShown
= IsPopupShown();
817 switch ( event
.GetKeyCode() )
820 // F4 toggles the popup in the native comboboxes, so emulate them
821 if ( !event
.AltDown() )
832 // On XP or with writable combo in Classic, arrows don't open the
833 // popup but Alt-arrow does
834 if ( event
.AltDown() ||
836 HasFlag(wxCB_READONLY
) &&
837 !wxUxThemeEngine::GetIfActive()
848 #endif // wxUSE_COMBOCTRL