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()
133 // ----------------------------------------------------------------------------
134 // size management including autosizing
135 // ----------------------------------------------------------------------------
137 wxSize
wxButton::DoGetBestSize() const
139 wxString label
= wxGetWindowText(GetHWND());
141 GetTextExtent(label
, &wBtn
, NULL
);
144 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
146 // add a margin - the button is wider than just its label
149 // the button height is proportional to the height of the font used
150 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
152 wxSize sz
= GetDefaultSize();
153 if (wBtn
> sz
.x
) sz
.x
= wBtn
;
154 if (hBtn
> sz
.y
) sz
.y
= hBtn
;
160 wxSize
wxButtonBase::GetDefaultSize()
162 static wxSize s_sizeBtn
;
164 if ( s_sizeBtn
.x
== 0 )
167 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
169 // the size of a standard button in the dialog units is 50x14,
170 // translate this to pixels
171 // NB1: the multipliers come from the Windows convention
172 // NB2: the extra +1/+2 were needed to get the size be the same as the
173 // size of the buttons in the standard dialog - I don't know how
174 // this happens, but on my system this size is 75x23 in pixels and
175 // 23*8 isn't even divisible by 14... Would be nice to understand
176 // why these constants are needed though!
177 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
178 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
184 // ----------------------------------------------------------------------------
185 // set this button as the default one in its panel
186 // ----------------------------------------------------------------------------
188 void wxButton::SetDefault()
190 wxWindow
*parent
= GetParent();
191 wxButton
*btnOldDefault
;
194 wxWindow
*winOldDefault
= parent
->SetDefaultItem(this);
195 btnOldDefault
= wxDynamicCast(winOldDefault
, wxButton
);
197 ::SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
199 else // is a button without parent really normal?
201 btnOldDefault
= NULL
;
204 if ( btnOldDefault
&& btnOldDefault
!= this )
206 // remove the BS_DEFPUSHBUTTON style from the other button
207 long style
= GetWindowLong(GetHwndOf(btnOldDefault
), GWL_STYLE
);
209 // don't do it with the owner drawn buttons because it will reset
210 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
211 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
213 style
&= ~BS_DEFPUSHBUTTON
;
214 SendMessage(GetHwndOf(btnOldDefault
), BM_SETSTYLE
, style
, 1L);
218 // redraw the button - it will notice itself that it's not the
219 // default one any longer
220 btnOldDefault
->Refresh();
224 // set this button as the default
225 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
226 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
228 style
|= BS_DEFPUSHBUTTON
;
229 SendMessage(GetHwnd(), BM_SETSTYLE
, style
, 1L);
233 // ----------------------------------------------------------------------------
235 // ----------------------------------------------------------------------------
237 bool wxButton::SendClickEvent()
239 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
240 event
.SetEventObject(this);
242 return ProcessCommand(event
);
245 void wxButton::Command(wxCommandEvent
& event
)
247 ProcessCommand(event
);
250 // ----------------------------------------------------------------------------
251 // event/message handlers
252 // ----------------------------------------------------------------------------
254 bool wxButton::MSWCommand(WXUINT param
, WXWORD
WXUNUSED(id
))
256 bool processed
= FALSE
;
259 case 1: // message came from an accelerator
260 case BN_CLICKED
: // normal buttons send this
261 case BN_DOUBLECLICKED
: // owner-drawn ones also send this
262 processed
= SendClickEvent();
269 long wxButton::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
271 // when we receive focus, we want to become the default button in our
273 if ( nMsg
== WM_SETFOCUS
)
277 // let the default processign take place too
279 else if ( nMsg
== WM_LBUTTONDBLCLK
)
281 // emulate a click event to force an owner-drawn button to change its
282 // appearance - without this, it won't do it
283 (void)wxControl::MSWWindowProc(WM_LBUTTONDOWN
, wParam
, lParam
);
285 // and conitnue with processing the message normally as well
288 // let the base class do all real processing
289 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
292 // ----------------------------------------------------------------------------
293 // owner-drawn buttons support
294 // ----------------------------------------------------------------------------
300 static void DrawButtonText(HDC hdc
,
302 const wxString
& text
,
305 COLORREF colOld
= SetTextColor(hdc
, col
);
306 int modeOld
= SetBkMode(hdc
, TRANSPARENT
);
308 DrawText(hdc
, text
, text
.length(), pRect
,
309 DT_CENTER
| DT_VCENTER
| DT_SINGLELINE
);
311 SetBkMode(hdc
, modeOld
);
312 SetTextColor(hdc
, colOld
);
315 static void DrawRect(HDC hdc
, const RECT
& r
)
317 MoveToEx(hdc
, r
.left
, r
.top
, NULL
);
318 LineTo(hdc
, r
.right
, r
.top
);
319 LineTo(hdc
, r
.right
, r
.bottom
);
320 LineTo(hdc
, r
.left
, r
.bottom
);
321 LineTo(hdc
, r
.left
, r
.top
);
324 void wxButton::MakeOwnerDrawn()
326 long style
= GetWindowLong(GetHwnd(), GWL_STYLE
);
327 if ( (style
& BS_OWNERDRAW
) != BS_OWNERDRAW
)
330 style
|= BS_OWNERDRAW
;
331 SetWindowLong(GetHwnd(), GWL_STYLE
, style
);
335 bool wxButton::SetBackgroundColour(const wxColour
&colour
)
337 if ( !wxControl::SetBackgroundColour(colour
) )
350 bool wxButton::SetForegroundColour(const wxColour
&colour
)
352 if ( !wxControl::SetForegroundColour(colour
) )
366 The button frame looks like this normally:
369 WHHHHHHHHHHHHHHHHGB W = white (HILIGHT)
370 WH GB H = light grey (LIGHT)
371 WH GB G = dark grey (SHADOW)
372 WH GB B = black (DKSHADOW)
377 When the button is selected, the button becomes like this (the total button
378 size doesn't change):
389 When the button is pushed (while selected) it is like:
401 static void DrawButtonFrame(HDC hdc
, const RECT
& rectBtn
,
402 bool selected
, bool pushed
)
405 CopyRect(&r
, &rectBtn
);
407 HPEN hpenBlack
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DDKSHADOW
)),
408 hpenGrey
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DSHADOW
)),
409 hpenLightGr
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DLIGHT
)),
410 hpenWhite
= CreatePen(PS_SOLID
, 1, GetSysColor(COLOR_3DHILIGHT
));
412 HPEN hpenOld
= (HPEN
)SelectObject(hdc
, hpenBlack
);
421 (void)SelectObject(hdc
, hpenGrey
);
422 InflateRect(&r
, -1, -1);
432 InflateRect(&r
, -1, -1);
435 MoveToEx(hdc
, r
.left
, r
.bottom
, NULL
);
436 LineTo(hdc
, r
.right
, r
.bottom
);
437 LineTo(hdc
, r
.right
, r
.top
- 1);
439 (void)SelectObject(hdc
, hpenWhite
);
440 MoveToEx(hdc
, r
.left
, r
.bottom
- 1, NULL
);
441 LineTo(hdc
, r
.left
, r
.top
);
442 LineTo(hdc
, r
.right
, r
.top
);
444 (void)SelectObject(hdc
, hpenLightGr
);
445 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 2, NULL
);
446 LineTo(hdc
, r
.left
+ 1, r
.top
+ 1);
447 LineTo(hdc
, r
.right
- 1, r
.top
+ 1);
449 (void)SelectObject(hdc
, hpenGrey
);
450 MoveToEx(hdc
, r
.left
+ 1, r
.bottom
- 1, NULL
);
451 LineTo(hdc
, r
.right
- 1, r
.bottom
- 1);
452 LineTo(hdc
, r
.right
- 1, r
.top
);
455 (void)SelectObject(hdc
, hpenOld
);
456 DeleteObject(hpenWhite
);
457 DeleteObject(hpenLightGr
);
458 DeleteObject(hpenGrey
);
459 DeleteObject(hpenBlack
);
462 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT
*wxdis
)
464 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
)wxdis
;
467 CopyRect(&rectBtn
, &lpDIS
->rcItem
);
469 COLORREF colBg
= wxColourToRGB(GetBackgroundColour()),
470 colFg
= wxColourToRGB(GetForegroundColour());
472 HDC hdc
= lpDIS
->hDC
;
473 UINT state
= lpDIS
->itemState
;
475 // first, draw the background
476 HBRUSH hbrushBackground
= ::CreateSolidBrush(colBg
);
478 FillRect(hdc
, &rectBtn
, hbrushBackground
);
480 // draw the border for the current state
481 bool selected
= (state
& ODS_SELECTED
) != 0;
484 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
487 selected
= panel
->GetDefaultItem() == this;
490 bool pushed
= (SendMessage(GetHwnd(), BM_GETSTATE
, 0, 0) & BST_PUSHED
) != 0;
492 DrawButtonFrame(hdc
, rectBtn
, selected
, pushed
);
494 // draw the focus rect if needed
495 if ( state
& ODS_FOCUS
)
498 CopyRect(&rectFocus
, &rectBtn
);
500 // I don't know where does this constant come from, but this is how
501 // Windows draws them
502 InflateRect(&rectFocus
, -4, -4);
504 DrawFocusRect(hdc
, &rectFocus
);
509 // the label is shifted by 1 pixel to create "pushed" effect
510 OffsetRect(&rectBtn
, 1, 1);
513 DrawButtonText(hdc
, &rectBtn
, GetLabel(),
514 state
& ODS_DISABLED
? GetSysColor(COLOR_GRAYTEXT
)
517 ::DeleteObject(hbrushBackground
);
524 #endif // wxUSE_BUTTON