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();
77 long msStyle
= WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
/* | WS_CLIPSIBLINGS */ ;
80 if(m_windowStyle
& wxBU_LEFT
)
82 if(m_windowStyle
& wxBU_RIGHT
)
84 if(m_windowStyle
& wxBU_TOP
)
86 if(m_windowStyle
& wxBU_BOTTOM
)
90 m_hWnd
= (WXHWND
)CreateWindowEx
92 MakeExtendedStyle(m_windowStyle
),
107 msg
.Printf(wxT("CreateWindowEx failed"));
109 msg
.Printf(wxT("CreateWindowEx failed with error number %ld"), (long) GetLastError());
114 // Subclass again for purposes of dialog editing mode
117 SetFont(parent
->GetFont());
119 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
124 wxButton::~wxButton()
126 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
129 if ( panel
->GetDefaultItem() == this )
131 // don't leave the panel with invalid default item
132 panel
->SetDefaultItem(NULL
);
137 // ----------------------------------------------------------------------------
138 // size management including autosizing
139 // ----------------------------------------------------------------------------
141 wxSize
wxButton::DoGetBestSize() const
143 wxString label
= wxGetWindowText(GetHWND());
145 GetTextExtent(label
, &wBtn
, NULL
);
148 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
150 // add a margin - the button is wider than just its label
153 // the button height is proportional to the height of the font used
154 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
156 wxSize sz
= GetDefaultSize();
157 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
158 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
164 wxSize
wxButton::GetDefaultSize()
166 static wxSize s_sizeBtn
;
168 if ( s_sizeBtn
.x
== 0 )
171 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
173 // the size of a standard button in the dialog units is 50x14,
174 // translate this to pixels
175 // NB1: the multipliers come from the Windows convention
176 // NB2: the extra +1/+2 were needed to get the size be the same as the
177 // size of the buttons in the standard dialog - I don't know how
178 // this happens, but on my system this size is 75x23 in pixels and
179 // 23*8 isn't even divisible by 14... Would be nice to understand
180 // why these constants are needed though!
181 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
182 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
188 // ----------------------------------------------------------------------------
189 // set this button as the default one in its panel
190 // ----------------------------------------------------------------------------
192 void wxButton::SetDefault()
194 wxWindow
*parent
= GetParent();
195 wxButton
*btnOldDefault
= NULL
;
196 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
199 btnOldDefault
= panel
->GetDefaultItem();
200 panel
->SetDefaultItem(this);
205 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
210 // remove the BS_DEFPUSHBUTTON style from the other button
211 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
213 // don't do it with the owner drawn buttons because it will reset
214 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
215 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
217 style
&= ~BS_DEFPUSHBUTTON
;
218 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
222 // redraw the button - it will notice itself that it's not the
223 // default one any longer
224 btnOldDefault
->Refresh();
228 // set this button as the default
229 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
230 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
232 style
|= BS_DEFPUSHBUTTON
;
233 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
237 // ----------------------------------------------------------------------------
239 // ----------------------------------------------------------------------------
241 bool wxButton::SendClickEvent()
243 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
244 event
.SetEventObject(this);
246 return ProcessCommand(event
);
249 void wxButton::Command(wxCommandEvent
& event
)
251 ProcessCommand(event
);
254 // ----------------------------------------------------------------------------
255 // event/message handlers
256 // ----------------------------------------------------------------------------
258 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
260 bool processed
= FALSE
;
263 case 1: // message came from an accelerator
264 case BN_CLICKED
: // normal buttons send this
265 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
266 processed
= SendClickEvent();
273 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
275 // when we receive focus, we want to become the default button in our
277 if ( nMsg
== WM_SETFOCUS
)
281 // let the default processign take place too
283 else if ( nMsg
== WM_LBUTTONDBLCLK
)
285 // emulate a click event to force an owner-drawn button to change its
286 // appearance - without this, it won't do it
287 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
289 // and conitnue with processing the message normally as well
292 // let the base class do all real processing
293 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
296 // ----------------------------------------------------------------------------
297 // owner-drawn buttons support
298 // ----------------------------------------------------------------------------
304 static void DrawButtonText(HDC hdc
,
306 const wxString
& text
,
309 COLORREF colOld
= SetTextColor(hdc
, col
);
310 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
312 DrawText(hdc
, text
, text
.length(), pRect
,
313 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
315 SetBkMode(hdc
, modeOld
);
316 SetTextColor(hdc
, colOld
);
319 static void DrawRect(HDC hdc
, const RECT
& r
)
321 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
322 LineTo(hdc
, r
.right
, r
.top
);
323 LineTo(hdc
, r
.right
, r
.bottom
);
324 LineTo(hdc
, r
.left
, r
.bottom
);
325 LineTo(hdc
, r
.left
, r
.top
);
328 void wxButton::MakeOwnerDrawn()
330 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
331 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
334 style
|= BS_OWNERDRAW
;
335 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
339 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
341 if ( !wxControl::SetBackgroundColour(colour
) )
354 bool wxButton::SetForegroundColour(const wxColour
&colour
)
356 if ( !wxControl::SetForegroundColour(colour
) )
370 The button frame looks like this normally:
373 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
374 WH GB H = light grey (LIGHT)
375 WH GB G = dark grey (SHADOW)
376 WH GB B = black (DKSHADOW)
381 When the button is selected, the button becomes like this (the total button
382 size doesn't change):
393 When the button is pushed (while selected) it is like:
405 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
406 bool selected
, bool pushed
)
409 CopyRect(&r
, &rectBtn
);
411 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
412 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
413 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
414 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
416 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
425 (void)SelectObject(hdc
, hpenGrey
);
426 InflateRect(&r
, -1, -1);
436 InflateRect(&r
, -1, -1);
439 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
440 LineTo(hdc
, r
.right
, r
.bottom
);
441 LineTo(hdc
, r
.right
, r
.top
- 1);
443 (void)SelectObject(hdc
, hpenWhite
);
444 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
445 LineTo(hdc
, r
.left
, r
.top
);
446 LineTo(hdc
, r
.right
, r
.top
);
448 (void)SelectObject(hdc
, hpenLightGr
);
449 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
450 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
451 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
453 (void)SelectObject(hdc
, hpenGrey
);
454 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
455 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
456 LineTo(hdc
, r
.right
- 1, r
.top
);
459 (void)SelectObject(hdc
, hpenOld
);
460 DeleteObject(hpenWhite
);
461 DeleteObject(hpenLightGr
);
462 DeleteObject(hpenGrey
);
463 DeleteObject(hpenBlack
);
466 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
468 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
471 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
473 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
474 colFg
= wxColourToRGB(GetForegroundColour());
476 HDC hdc
= lpDIS
->hDC
;
477 UINT state
= lpDIS
->itemState
;
479 // first, draw the background
480 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
482 FillRect(hdc
, &rectBtn
, hbrushBackground
);
484 // draw the border for the current state
485 bool selected
= (state
& ODS_SELECTED
) != 0;
488 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
491 selected
= panel
->GetDefaultItem() == this;
494 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
496 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
498 // draw the focus rect if needed
499 if ( state
& ODS_FOCUS
)
502 CopyRect(&rectFocus
, &rectBtn
);
504 // I don't know where does this constant come from, but this is how
505 // Windows draws them
506 InflateRect(&rectFocus
, -4, -4);
508 DrawFocusRect(hdc
, &rectFocus
);
513 // the label is shifted by 1 pixel to create "pushed" effect
514 OffsetRect(&rectBtn
, 1, 1);
517 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
518 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
521 ::DeleteObject(hbrushBackground
);