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 ( !CreateBase(parent
, id
, pos
, size
, style
, validator
, name
) )
74 parent
->AddChild((wxButton
*)this);
76 m_backgroundColour
= parent
->GetBackgroundColour();
77 m_foregroundColour
= parent
->GetForegroundColour();
79 long msStyle
= WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
/* | WS_CLIPSIBLINGS */ ;
81 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
82 msStyle
|= WS_CLIPSIBLINGS
;
85 if(m_windowStyle
& wxBU_LEFT
)
87 if(m_windowStyle
& wxBU_RIGHT
)
89 if(m_windowStyle
& wxBU_TOP
)
91 if(m_windowStyle
& wxBU_BOTTOM
)
95 m_hWnd
= (WXHWND
)CreateWindowEx
97 MakeExtendedStyle(m_windowStyle
),
112 msg
.Printf(wxT("CreateWindowEx failed"));
114 msg
.Printf(wxT("CreateWindowEx failed with error number %ld"), (long) GetLastError());
119 // Subclass again for purposes of dialog editing mode
122 SetFont(parent
->GetFont());
124 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
129 wxButton::~wxButton()
131 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
134 if ( panel
->GetDefaultItem() == this )
136 // don't leave the panel with invalid default item
137 panel
->SetDefaultItem(NULL
);
142 // ----------------------------------------------------------------------------
143 // size management including autosizing
144 // ----------------------------------------------------------------------------
146 wxSize
wxButton::DoGetBestSize() const
148 wxString label
= wxGetWindowText(GetHWND());
150 GetTextExtent(label
, &wBtn
, NULL
);
153 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
155 // add a margin - the button is wider than just its label
158 // the button height is proportional to the height of the font used
159 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
161 wxSize sz
= GetDefaultSize();
162 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
163 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
169 wxSize
wxButtonBase::GetDefaultSize()
171 static wxSize s_sizeBtn
;
173 if ( s_sizeBtn
.x
== 0 )
176 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
178 // the size of a standard button in the dialog units is 50x14,
179 // translate this to pixels
180 // NB1: the multipliers come from the Windows convention
181 // NB2: the extra +1/+2 were needed to get the size be the same as the
182 // size of the buttons in the standard dialog - I don't know how
183 // this happens, but on my system this size is 75x23 in pixels and
184 // 23*8 isn't even divisible by 14... Would be nice to understand
185 // why these constants are needed though!
186 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
187 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
193 // ----------------------------------------------------------------------------
194 // set this button as the default one in its panel
195 // ----------------------------------------------------------------------------
197 void wxButton::SetDefault()
199 wxWindow
*parent
= GetParent();
200 wxButton
*btnOldDefault
= NULL
;
201 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
204 btnOldDefault
= panel
->GetDefaultItem();
205 panel
->SetDefaultItem(this);
210 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
215 // remove the BS_DEFPUSHBUTTON style from the other button
216 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
218 // don't do it with the owner drawn buttons because it will reset
219 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
220 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
222 style
&= ~BS_DEFPUSHBUTTON
;
223 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
227 // redraw the button - it will notice itself that it's not the
228 // default one any longer
229 btnOldDefault
->Refresh();
233 // set this button as the default
234 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
235 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
237 style
|= BS_DEFPUSHBUTTON
;
238 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 bool wxButton::SendClickEvent()
248 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
249 event
.SetEventObject(this);
251 return ProcessCommand(event
);
254 void wxButton::Command(wxCommandEvent
& event
)
256 ProcessCommand(event
);
259 // ----------------------------------------------------------------------------
260 // event/message handlers
261 // ----------------------------------------------------------------------------
263 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
265 bool processed
= FALSE
;
268 case 1: // message came from an accelerator
269 case BN_CLICKED
: // normal buttons send this
270 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
271 processed
= SendClickEvent();
278 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
280 // when we receive focus, we want to become the default button in our
282 if ( nMsg
== WM_SETFOCUS
)
286 // let the default processign take place too
288 else if ( nMsg
== WM_LBUTTONDBLCLK
)
290 // emulate a click event to force an owner-drawn button to change its
291 // appearance - without this, it won't do it
292 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
294 // and conitnue with processing the message normally as well
297 // let the base class do all real processing
298 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
301 // ----------------------------------------------------------------------------
302 // owner-drawn buttons support
303 // ----------------------------------------------------------------------------
309 static void DrawButtonText(HDC hdc
,
311 const wxString
& text
,
314 COLORREF colOld
= SetTextColor(hdc
, col
);
315 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
317 DrawText(hdc
, text
, text
.length(), pRect
,
318 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
320 SetBkMode(hdc
, modeOld
);
321 SetTextColor(hdc
, colOld
);
324 static void DrawRect(HDC hdc
, const RECT
& r
)
326 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
327 LineTo(hdc
, r
.right
, r
.top
);
328 LineTo(hdc
, r
.right
, r
.bottom
);
329 LineTo(hdc
, r
.left
, r
.bottom
);
330 LineTo(hdc
, r
.left
, r
.top
);
333 void wxButton::MakeOwnerDrawn()
335 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
336 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
339 style
|= BS_OWNERDRAW
;
340 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
344 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
346 if ( !wxControl::SetBackgroundColour(colour
) )
359 bool wxButton::SetForegroundColour(const wxColour
&colour
)
361 if ( !wxControl::SetForegroundColour(colour
) )
375 The button frame looks like this normally:
378 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
379 WH GB H = light grey (LIGHT)
380 WH GB G = dark grey (SHADOW)
381 WH GB B = black (DKSHADOW)
386 When the button is selected, the button becomes like this (the total button
387 size doesn't change):
398 When the button is pushed (while selected) it is like:
410 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
411 bool selected
, bool pushed
)
414 CopyRect(&r
, &rectBtn
);
416 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
417 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
418 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
419 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
421 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
430 (void)SelectObject(hdc
, hpenGrey
);
431 InflateRect(&r
, -1, -1);
441 InflateRect(&r
, -1, -1);
444 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
445 LineTo(hdc
, r
.right
, r
.bottom
);
446 LineTo(hdc
, r
.right
, r
.top
- 1);
448 (void)SelectObject(hdc
, hpenWhite
);
449 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
450 LineTo(hdc
, r
.left
, r
.top
);
451 LineTo(hdc
, r
.right
, r
.top
);
453 (void)SelectObject(hdc
, hpenLightGr
);
454 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
455 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
456 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
458 (void)SelectObject(hdc
, hpenGrey
);
459 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
460 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
461 LineTo(hdc
, r
.right
- 1, r
.top
);
464 (void)SelectObject(hdc
, hpenOld
);
465 DeleteObject(hpenWhite
);
466 DeleteObject(hpenLightGr
);
467 DeleteObject(hpenGrey
);
468 DeleteObject(hpenBlack
);
471 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
473 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
476 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
478 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
479 colFg
= wxColourToRGB(GetForegroundColour());
481 HDC hdc
= lpDIS
->hDC
;
482 UINT state
= lpDIS
->itemState
;
484 // first, draw the background
485 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
487 FillRect(hdc
, &rectBtn
, hbrushBackground
);
489 // draw the border for the current state
490 bool selected
= (state
& ODS_SELECTED
) != 0;
493 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
496 selected
= panel
->GetDefaultItem() == this;
499 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
501 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
503 // draw the focus rect if needed
504 if ( state
& ODS_FOCUS
)
507 CopyRect(&rectFocus
, &rectBtn
);
509 // I don't know where does this constant come from, but this is how
510 // Windows draws them
511 InflateRect(&rectFocus
, -4, -4);
513 DrawFocusRect(hdc
, &rectFocus
);
518 // the label is shifted by 1 pixel to create "pushed" effect
519 OffsetRect(&rectBtn
, 1, 1);
522 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
523 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
526 ::DeleteObject(hbrushBackground
);
533 #endif // wxUSE_BUTTON