1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/commandlinkbutton.cpp
3 // Purpose: wxCommandLinkButton
4 // Author: Rickard Westerlund
7 // Copyright: (c) 2010 wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #if wxUSE_COMMANDLINKBUTTON
26 #include "wx/dcclient.h"
29 #include "wx/commandlinkbutton.h"
30 #include "wx/msw/private.h"
31 #include "wx/msw/private/button.h"
32 #include "wx/msw/private/dc.h"
33 #include "wx/private/window.h"
36 #define BCM_SETNOTE 0x1609
39 #ifndef BS_COMMANDLINK
40 #define BS_COMMANDLINK 0xE
43 // ----------------------------------------------------------------------------
45 // ----------------------------------------------------------------------------
50 inline bool HasNativeCommandLinkButton()
52 return wxGetWinVersion() >= wxWinVersion_6
;
55 } // anonymous namespace
57 // ----------------------------------------------------------------------------
58 // Command link button
59 // ----------------------------------------------------------------------------
61 bool wxCommandLinkButton::Create(wxWindow
*parent
,
63 const wxString
& mainLabel
,
68 const wxValidator
& validator
,
71 if ( ! wxGenericCommandLinkButton::Create(parent
,
82 SetMainLabelAndNote(mainLabel
, note
);
89 wxCommandLinkButton::SetMainLabelAndNote(const wxString
& mainLabel
,
92 if ( HasNativeCommandLinkButton() )
94 wxButton::SetLabel(mainLabel
);
95 ::SendMessage(m_hWnd
, BCM_SETNOTE
, 0, (LPARAM
) note
.wx_str());
97 // Preserve the user-specified label for GetLabel()
98 m_labelOrig
= mainLabel
;
100 m_labelOrig
<< '\n' << note
;
104 wxGenericCommandLinkButton::SetMainLabelAndNote(mainLabel
, note
);
108 WXDWORD
wxCommandLinkButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
110 WXDWORD ret
= wxButton::MSWGetStyle(style
, exstyle
);
111 if ( HasNativeCommandLinkButton() )
112 ret
|= BS_COMMANDLINK
;
117 bool wxCommandLinkButton::HasNativeBitmap() const
119 return HasNativeCommandLinkButton();
122 // ----------------------------------------------------------------------------
123 // size management including autosizing
124 // ----------------------------------------------------------------------------
126 // Margin measures can be found at
127 // http://expression.microsoft.com/en-us/ee662150.aspx
130 const int MAINLABEL_TOP_MARGIN
= 16; // Includes image top margin.
131 const int MAINLABEL_NOTE_LEFT_MARGIN
= 23;
132 const int NOTE_TOP_MARGIN
= 21;
133 const int NOTE_BOTTOM_MARGIN
= 1;
134 const int MAINLABEL_NOTE_MARGIN
= NOTE_TOP_MARGIN
- MAINLABEL_TOP_MARGIN
;
137 wxSize
wxCommandLinkButton::DoGetBestSize() const
141 // account for the text part if we have it or if we don't have any image at
142 // all (buttons initially created with empty label should still have a non
144 if ( ShowsLabel() || !m_imageData
)
147 if ( GetAuthNeeded() )
148 flags
|= wxMSWButton::Size_AuthNeeded
;
150 wxCommandLinkButton
*thisButton
=
151 const_cast<wxCommandLinkButton
*>(this);
152 wxClientDC
dc(thisButton
);
154 wxFont noteFont
= dc
.GetFont();
156 // 4/3 is the relationship between main label and note font sizes.
157 dc
.SetFont(noteFont
.Scaled(4.0f
/3.0f
));
158 size
= dc
.GetMultiLineTextExtent(GetLabelText(GetMainLabel()));
160 dc
.SetFont(noteFont
);
161 wxSize noteSize
= dc
.GetMultiLineTextExtent(GetLabelText(GetNote()));
163 if ( noteSize
.x
> size
.x
)
165 size
.y
+= noteSize
.y
;
167 size
= wxMSWButton::GetFittingSize(thisButton
,
171 // The height of a standard command link button is 25 and 35 DLUs for
172 // single lines and two lines respectively. Width is not accounted for.
173 int heightDef
= GetNote().AfterFirst('\n').empty() ? 25 : 35;
174 wxSize sizeDef
= thisButton
->ConvertDialogToPixels(wxSize(50,
177 if ( size
.y
< sizeDef
.y
)
183 AdjustForBitmapSize(size
);
187 // The default image size is 16x16.
193 size
.x
+= MAINLABEL_NOTE_LEFT_MARGIN
;
194 size
.y
+= MAINLABEL_TOP_MARGIN
+ NOTE_BOTTOM_MARGIN
;
195 if ( !GetNote().empty() )
196 size
.y
+= MAINLABEL_NOTE_MARGIN
;
203 #endif // wxUSE_COMMANDLINKBUTTON