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"
32 #include "wx/button.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
40 #include "wx/msw/private.h"
42 // ----------------------------------------------------------------------------
44 // ----------------------------------------------------------------------------
46 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
48 // this macro tries to adjust the default button height to a reasonable value
49 // using the char height as the base
50 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
52 // ============================================================================
54 // ============================================================================
56 // ----------------------------------------------------------------------------
57 // creation/destruction
58 // ----------------------------------------------------------------------------
60 bool wxButton::Create(wxWindow
*parent
,
62 const wxString
& label
,
66 const wxValidator
& validator
,
69 if ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
72 parent
->AddChild((wxButton
*)this);
74 m_backgroundColour
= parent
->GetBackgroundColour();
75 m_foregroundColour
= parent
->GetForegroundColour();
78 m_hWnd
= (WXHWND
)CreateWindowEx
80 MakeExtendedStyle(m_windowStyle
),
83 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
,
91 // Subclass again for purposes of dialog editing mode
94 SetFont(parent
->GetFont());
96 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
101 wxButton::~wxButton()
103 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
106 if ( panel
->GetDefaultItem() == this )
108 // don't leave the panel with invalid default item
109 panel
->SetDefaultItem(NULL
);
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
wxButton::GetDefaultSize()
143 static wxSize s_sizeBtn
;
145 if ( s_sizeBtn
.x
== 0 )
148 dc
.SetFont(wxSystemSettings::GetSystemFont(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
= NULL
;
173 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
176 btnOldDefault
= panel
->GetDefaultItem();
177 panel
->SetDefaultItem(this);
182 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
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 id
)
237 bool processed
= FALSE
;
240 case 1: // means that the message came from an accelerator
242 processed
= SendClickEvent();
249 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
251 // when we receive focus, we want to become the default button in our
253 if ( nMsg
== WM_SETFOCUS
)
257 // let the default processign take place too
260 // let the base class do all real processing
261 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
264 // ----------------------------------------------------------------------------
265 // owner-drawn buttons support
266 // ----------------------------------------------------------------------------
272 static void DrawButtonText(HDC hdc
,
274 const wxString
& text
,
277 COLORREF colOld
= SetTextColor(hdc
, col
);
278 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
280 DrawText(hdc
, text
, text
.length(), pRect
,
281 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
283 SetBkMode(hdc
, modeOld
);
284 SetTextColor(hdc
, colOld
);
287 static void DrawRect(HDC hdc
, const RECT
& r
)
289 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
290 LineTo(hdc
, r
.right
, r
.top
);
291 LineTo(hdc
, r
.right
, r
.bottom
);
292 LineTo(hdc
, r
.left
, r
.bottom
);
293 LineTo(hdc
, r
.left
, r
.top
);
296 void wxButton::MakeOwnerDrawn()
298 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
299 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
302 style
|= BS_OWNERDRAW
;
303 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
307 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
309 if ( !wxControl::SetBackgroundColour(colour
) )
322 bool wxButton::SetForegroundColour(const wxColour
&colour
)
324 if ( !wxControl::SetForegroundColour(colour
) )
338 The button frame looks like this normally:
343 W GB where W, G, B are white, grey and black pixels
348 When the button is selected, the button becomes like this (the total button
349 size doesn't change):
359 When the button is pushed (while selected) it is like:
370 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
371 bool selected
, bool pushed
)
374 CopyRect(&r
, &rectBtn
);
376 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
377 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
378 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
380 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
389 (void)SelectObject(hdc
, hpenGrey
);
390 InflateRect(&r
, -1, -1);
400 InflateRect(&r
, -1, -1);
403 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
404 LineTo(hdc
, r
.right
, r
.bottom
);
405 LineTo(hdc
, r
.right
, r
.top
- 1);
407 (void)SelectObject(hdc
, hpenWhite
);
408 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
409 LineTo(hdc
, r
.left
, r
.top
);
410 LineTo(hdc
, r
.right
, r
.top
);
412 (void)SelectObject(hdc
, hpenGrey
);
413 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
414 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
415 LineTo(hdc
, r
.right
- 1, r
.top
);
418 (void)SelectObject(hdc
, hpenOld
);
419 DeleteObject(hpenWhite
);
420 DeleteObject(hpenGrey
);
421 DeleteObject(hpenBlack
);
424 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
426 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
429 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
431 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
432 colFg
= wxColourToRGB(GetForegroundColour());
434 HDC hdc
= lpDIS
->hDC
;
435 UINT state
= lpDIS
->itemState
;
437 // first, draw the background
438 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
440 FillRect(hdc
, &rectBtn
, hbrushBackground
);
442 // draw the border for the current state
443 bool selected
= (state
& ODS_SELECTED
) != 0;
446 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
449 selected
= panel
->GetDefaultItem() == this;
452 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
454 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
456 // draw the focus rect if needed
457 if ( state
& ODS_FOCUS
)
460 CopyRect(&rectFocus
, &rectBtn
);
462 // I don't know where does this constant come from, but this is how
463 // Windows draws them
464 InflateRect(&rectFocus
, -4, -4);
466 DrawFocusRect(hdc
, &rectFocus
);
471 // the label is shifted by 1 pixel to create "pushed" effect
472 OffsetRect(&rectBtn
, 1, 1);
475 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
476 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
479 ::DeleteObject(hbrushBackground
);