1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/button.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "button.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/button.h"
37 #include "wx/bmpbuttn.h"
38 #include "wx/settings.h"
39 #include "wx/dcscreen.h"
42 #include "wx/msw/private.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
50 // this macro tries to adjust the default button height to a reasonable value
51 // using the char height as the base
52 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
54 // ============================================================================
56 // ============================================================================
58 // ----------------------------------------------------------------------------
59 // creation/destruction
60 // ----------------------------------------------------------------------------
62 bool wxButton::Create(wxWindow
*parent
,
64 const wxString
& label
,
68 const wxValidator
& validator
,
71 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
74 return MSWCreateControl(_T("BUTTON"), label
, pos
, size
, style
);
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 WXDWORD
wxButton::MSWGetStyle(long style
, WXDWORD
*exstyle
) const
87 // buttons never have an external border, they draw their own one
88 WXDWORD msStyle
= wxControl::MSWGetStyle
90 (style
& ~wxBORDER_MASK
) | wxBORDER_NONE
, exstyle
93 // we must use WS_CLIPSIBLINGS with the buttons or they would draw over
94 // each other in any resizeable dialog which has more than one button in
96 msStyle
|= WS_CLIPSIBLINGS
;
99 // don't use "else if" here: weird as it is, but you may combine wxBU_LEFT
100 // and wxBU_RIGHT to get BS_CENTER!
101 if ( style
& wxBU_LEFT
)
103 if ( style
& wxBU_RIGHT
)
105 if ( style
& wxBU_TOP
)
107 if ( style
& wxBU_BOTTOM
)
108 msStyle
|= BS_BOTTOM
;
114 // ----------------------------------------------------------------------------
115 // size management including autosizing
116 // ----------------------------------------------------------------------------
118 wxSize
wxButton::DoGetBestSize() const
120 wxString label
= wxGetWindowText(GetHWND());
122 GetTextExtent(label
, &wBtn
, NULL
);
125 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
127 // add a margin - the button is wider than just its label
130 // the button height is proportional to the height of the font used
131 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
133 wxSize sz
= GetDefaultSize();
134 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
135 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
141 wxSize
wxButtonBase::GetDefaultSize()
143 static wxSize s_sizeBtn
;
145 if ( s_sizeBtn
.x
== 0 )
148 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
150 // the size of a standard button in the dialog units is 50x14,
151 // translate this to pixels
152 // NB1: the multipliers come from the Windows convention
153 // NB2: the extra +1/+2 were needed to get the size be the same as the
154 // size of the buttons in the standard dialog - I don't know how
155 // this happens, but on my system this size is 75x23 in pixels and
156 // 23*8 isn't even divisible by 14... Would be nice to understand
157 // why these constants are needed though!
158 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
159 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
165 // ----------------------------------------------------------------------------
166 // set this button as the default one in its panel
167 // ----------------------------------------------------------------------------
169 void wxButton::SetDefault()
171 wxWindow
*parent
= GetParent();
172 wxButton
*btnOldDefault
;
175 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
176 btnOldDefault
= wxDynamicCast(winOldDefault
, wxButton
);
178 ::SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
180 else // is a button without parent really normal?
182 btnOldDefault
= NULL
;
185 if ( btnOldDefault
&& btnOldDefault
!= this )
187 // remove the BS_DEFPUSHBUTTON style from the other button
188 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
190 // don't do it with the owner drawn buttons because it will reset
191 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
192 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
194 style
&= ~BS_DEFPUSHBUTTON
;
195 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
199 // redraw the button - it will notice itself that it's not the
200 // default one any longer
201 btnOldDefault
->Refresh();
205 // set this button as the default
206 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
207 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
209 style
|= BS_DEFPUSHBUTTON
;
210 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxButton::SendClickEvent()
220 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
221 event
.SetEventObject(this);
223 return ProcessCommand(event
);
226 void wxButton::Command(wxCommandEvent
& event
)
228 ProcessCommand(event
);
231 // ----------------------------------------------------------------------------
232 // event/message handlers
233 // ----------------------------------------------------------------------------
235 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
237 bool processed
= FALSE
;
240 case 1: // message came from an accelerator
241 case BN_CLICKED
: // normal buttons send this
242 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
243 processed
= SendClickEvent();
250 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
252 // when we receive focus, we want to become the default button in our
254 if ( nMsg
== WM_SETFOCUS
)
258 // let the default processign take place too
260 else if ( nMsg
== WM_LBUTTONDBLCLK
)
262 // emulate a click event to force an owner-drawn button to change its
263 // appearance - without this, it won't do it
264 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
266 // and conitnue with processing the message normally as well
269 // let the base class do all real processing
270 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
273 // ----------------------------------------------------------------------------
274 // owner-drawn buttons support
275 // ----------------------------------------------------------------------------
281 static void DrawButtonText(HDC hdc
,
283 const wxString
& text
,
286 COLORREF colOld
= SetTextColor(hdc
, col
);
287 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
289 DrawText(hdc
, text
, text
.length(), pRect
,
290 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
292 SetBkMode(hdc
, modeOld
);
293 SetTextColor(hdc
, colOld
);
296 static void DrawRect(HDC hdc
, const RECT
& r
)
298 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
299 LineTo(hdc
, r
.right
, r
.top
);
300 LineTo(hdc
, r
.right
, r
.bottom
);
301 LineTo(hdc
, r
.left
, r
.bottom
);
302 LineTo(hdc
, r
.left
, r
.top
);
305 void wxButton::MakeOwnerDrawn()
307 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
308 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
311 style
|= BS_OWNERDRAW
;
312 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
316 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
318 if ( !wxControl::SetBackgroundColour(colour
) )
331 bool wxButton::SetForegroundColour(const wxColour
&colour
)
333 if ( !wxControl::SetForegroundColour(colour
) )
347 The button frame looks like this normally:
350 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
351 WH GB H = light grey (LIGHT)
352 WH GB G = dark grey (SHADOW)
353 WH GB B = black (DKSHADOW)
358 When the button is selected, the button becomes like this (the total button
359 size doesn't change):
370 When the button is pushed (while selected) it is like:
382 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
383 bool selected
, bool pushed
)
386 CopyRect(&r
, &rectBtn
);
388 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
389 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
390 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
391 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
393 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
402 (void)SelectObject(hdc
, hpenGrey
);
403 InflateRect(&r
, -1, -1);
413 InflateRect(&r
, -1, -1);
416 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
417 LineTo(hdc
, r
.right
, r
.bottom
);
418 LineTo(hdc
, r
.right
, r
.top
- 1);
420 (void)SelectObject(hdc
, hpenWhite
);
421 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
422 LineTo(hdc
, r
.left
, r
.top
);
423 LineTo(hdc
, r
.right
, r
.top
);
425 (void)SelectObject(hdc
, hpenLightGr
);
426 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
427 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
428 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
430 (void)SelectObject(hdc
, hpenGrey
);
431 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
432 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
433 LineTo(hdc
, r
.right
- 1, r
.top
);
436 (void)SelectObject(hdc
, hpenOld
);
437 DeleteObject(hpenWhite
);
438 DeleteObject(hpenLightGr
);
439 DeleteObject(hpenGrey
);
440 DeleteObject(hpenBlack
);
443 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
445 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
448 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
450 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
451 colFg
= wxColourToRGB(GetForegroundColour());
453 HDC hdc
= lpDIS
->hDC
;
454 UINT state
= lpDIS
->itemState
;
456 // first, draw the background
457 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
459 FillRect(hdc
, &rectBtn
, hbrushBackground
);
461 // draw the border for the current state
462 bool selected
= (state
& ODS_SELECTED
) != 0;
465 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
468 selected
= panel
->GetDefaultItem() == this;
471 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
473 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
475 // draw the focus rect if needed
476 if ( state
& ODS_FOCUS
)
479 CopyRect(&rectFocus
, &rectBtn
);
481 // I don't know where does this constant come from, but this is how
482 // Windows draws them
483 InflateRect(&rectFocus
, -4, -4);
485 DrawFocusRect(hdc
, &rectFocus
);
490 // the label is shifted by 1 pixel to create "pushed" effect
491 OffsetRect(&rectBtn
, 1, 1);
494 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
495 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
498 ::DeleteObject(hbrushBackground
);
505 #endif // wxUSE_BUTTON