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 else if ( nMsg
== WM_MOVE
)
275 // let the base class do all real processing
276 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
279 // ----------------------------------------------------------------------------
280 // owner-drawn buttons support
281 // ----------------------------------------------------------------------------
287 static void DrawButtonText(HDC hdc
,
289 const wxString
& text
,
292 COLORREF colOld
= SetTextColor(hdc
, col
);
293 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
295 DrawText(hdc
, text
, text
.length(), pRect
,
296 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
298 SetBkMode(hdc
, modeOld
);
299 SetTextColor(hdc
, colOld
);
302 static void DrawRect(HDC hdc
, const RECT
& r
)
304 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
305 LineTo(hdc
, r
.right
, r
.top
);
306 LineTo(hdc
, r
.right
, r
.bottom
);
307 LineTo(hdc
, r
.left
, r
.bottom
);
308 LineTo(hdc
, r
.left
, r
.top
);
311 void wxButton::MakeOwnerDrawn()
313 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
314 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
317 style
|= BS_OWNERDRAW
;
318 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
322 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
324 if ( !wxControl::SetBackgroundColour(colour
) )
337 bool wxButton::SetForegroundColour(const wxColour
&colour
)
339 if ( !wxControl::SetForegroundColour(colour
) )
353 The button frame looks like this normally:
356 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
357 WH GB H = light grey (LIGHT)
358 WH GB G = dark grey (SHADOW)
359 WH GB B = black (DKSHADOW)
364 When the button is selected, the button becomes like this (the total button
365 size doesn't change):
376 When the button is pushed (while selected) it is like:
388 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
389 bool selected
, bool pushed
)
392 CopyRect(&r
, &rectBtn
);
394 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
395 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
396 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
397 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
399 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
408 (void)SelectObject(hdc
, hpenGrey
);
409 InflateRect(&r
, -1, -1);
419 InflateRect(&r
, -1, -1);
422 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
423 LineTo(hdc
, r
.right
, r
.bottom
);
424 LineTo(hdc
, r
.right
, r
.top
- 1);
426 (void)SelectObject(hdc
, hpenWhite
);
427 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
428 LineTo(hdc
, r
.left
, r
.top
);
429 LineTo(hdc
, r
.right
, r
.top
);
431 (void)SelectObject(hdc
, hpenLightGr
);
432 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
433 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
434 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
436 (void)SelectObject(hdc
, hpenGrey
);
437 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
438 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
439 LineTo(hdc
, r
.right
- 1, r
.top
);
442 (void)SelectObject(hdc
, hpenOld
);
443 DeleteObject(hpenWhite
);
444 DeleteObject(hpenLightGr
);
445 DeleteObject(hpenGrey
);
446 DeleteObject(hpenBlack
);
449 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
451 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
454 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
456 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
457 colFg
= wxColourToRGB(GetForegroundColour());
459 HDC hdc
= lpDIS
->hDC
;
460 UINT state
= lpDIS
->itemState
;
462 // first, draw the background
463 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
465 FillRect(hdc
, &rectBtn
, hbrushBackground
);
467 // draw the border for the current state
468 bool selected
= (state
& ODS_SELECTED
) != 0;
471 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
474 selected
= panel
->GetDefaultItem() == this;
477 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
479 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
481 // draw the focus rect if needed
482 if ( state
& ODS_FOCUS
)
485 CopyRect(&rectFocus
, &rectBtn
);
487 // I don't know where does this constant come from, but this is how
488 // Windows draws them
489 InflateRect(&rectFocus
, -4, -4);
491 DrawFocusRect(hdc
, &rectFocus
);
496 // the label is shifted by 1 pixel to create "pushed" effect
497 OffsetRect(&rectBtn
, 1, 1);
500 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
501 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
504 ::DeleteObject(hbrushBackground
);
511 #endif // wxUSE_BUTTON