1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/commandlinkbutton.cpp
3 // Purpose: wxCommandLinkButton
4 // Author: Rickard Westerlund
6 // Copyright: (c) 2010 wxWidgets team
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
21 #if wxUSE_COMMANDLINKBUTTON
25 #include "wx/dcclient.h"
28 #include "wx/commandlinkbutton.h"
29 #include "wx/msw/private.h"
30 #include "wx/msw/private/button.h"
31 #include "wx/msw/private/dc.h"
32 #include "wx/private/window.h"
35 #define BCM_SETNOTE 0x1609
38 #ifndef BS_COMMANDLINK
39 #define BS_COMMANDLINK 0xE
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
49 inline bool HasNativeCommandLinkButton()
51 return wxGetWinVersion() >= wxWinVersion_6
;
54 } // anonymous namespace
56 // ----------------------------------------------------------------------------
57 // Command link button
58 // ----------------------------------------------------------------------------
60 bool wxCommandLinkButton::Create(wxWindow
*parent
,
62 const wxString
& mainLabel
,
67 const wxValidator
& validator
,
70 if ( ! wxGenericCommandLinkButton::Create(parent
,
81 SetMainLabelAndNote(mainLabel
, note
);
88 wxCommandLinkButton::SetMainLabelAndNote(const wxString
& mainLabel
,
91 if ( HasNativeCommandLinkButton() )
93 wxButton::SetLabel(mainLabel
);
94 ::SendMessage(m_hWnd
, BCM_SETNOTE
, 0, wxMSW_CONV_LPARAM(note
));
96 // Preserve the user-specified label for GetLabel()
97 m_labelOrig
= mainLabel
;
99 m_labelOrig
<< '\n' << note
;
103 wxGenericCommandLinkButton::SetMainLabelAndNote(mainLabel
, note
);
107 WXDWORD
wxCommandLinkButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
109 WXDWORD ret
= wxButton::MSWGetStyle(style
, exstyle
);
110 if ( HasNativeCommandLinkButton() )
111 ret
|= BS_COMMANDLINK
;
116 bool wxCommandLinkButton::HasNativeBitmap() const
118 return HasNativeCommandLinkButton();
121 // ----------------------------------------------------------------------------
122 // size management including autosizing
123 // ----------------------------------------------------------------------------
125 // Margin measures can be found at
126 // http://expression.microsoft.com/en-us/ee662150.aspx
129 const int MAINLABEL_TOP_MARGIN
= 16; // Includes image top margin.
130 const int MAINLABEL_NOTE_LEFT_MARGIN
= 23;
131 const int NOTE_TOP_MARGIN
= 21;
132 const int NOTE_BOTTOM_MARGIN
= 1;
133 const int MAINLABEL_NOTE_MARGIN
= NOTE_TOP_MARGIN
- MAINLABEL_TOP_MARGIN
;
136 wxSize
wxCommandLinkButton::DoGetBestSize() const
140 // account for the text part if we have it or if we don't have any image at
141 // all (buttons initially created with empty label should still have a non
143 if ( ShowsLabel() || !m_imageData
)
146 if ( GetAuthNeeded() )
147 flags
|= wxMSWButton::Size_AuthNeeded
;
149 wxCommandLinkButton
*thisButton
=
150 const_cast<wxCommandLinkButton
*>(this);
151 wxClientDC
dc(thisButton
);
153 wxFont noteFont
= dc
.GetFont();
155 // 4/3 is the relationship between main label and note font sizes.
156 dc
.SetFont(noteFont
.Scaled(4.0f
/3.0f
));
157 size
= dc
.GetMultiLineTextExtent(GetLabelText(GetMainLabel()));
159 dc
.SetFont(noteFont
);
160 wxSize noteSize
= dc
.GetMultiLineTextExtent(GetLabelText(GetNote()));
162 if ( noteSize
.x
> size
.x
)
164 size
.y
+= noteSize
.y
;
166 size
= wxMSWButton::GetFittingSize(thisButton
,
170 // The height of a standard command link button is 25 and 35 DLUs for
171 // single lines and two lines respectively. Width is not accounted for.
172 int heightDef
= GetNote().AfterFirst('\n').empty() ? 25 : 35;
173 wxSize sizeDef
= thisButton
->ConvertDialogToPixels(wxSize(50,
176 if ( size
.y
< sizeDef
.y
)
182 AdjustForBitmapSize(size
);
186 // The default image size is 16x16.
192 size
.x
+= MAINLABEL_NOTE_LEFT_MARGIN
;
193 size
.y
+= MAINLABEL_TOP_MARGIN
+ NOTE_BOTTOM_MARGIN
;
194 if ( !GetNote().empty() )
195 size
.y
+= MAINLABEL_NOTE_MARGIN
;
202 #endif // wxUSE_COMMANDLINKBUTTON