1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "button.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
31 #include "wx/button.h"
35 #include "wx/msw/private.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #if !USE_SHARED_LIBRARY
42 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
45 // this macro tries to adjust the default button height to a reasonable value
46 // using the char height as the base
47 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
49 // ============================================================================
51 // ============================================================================
53 // ----------------------------------------------------------------------------
54 // creation/destruction
55 // ----------------------------------------------------------------------------
57 bool wxButton::Create(wxWindow
*parent
,
59 const wxString
& label
,
63 const wxValidator
& validator
,
66 if ( !CreateBase(parent
, id
, pos
, size
, style
, name
) )
69 SetValidator(validator
);
71 parent
->AddChild((wxButton
*)this);
73 m_backgroundColour
= parent
->GetBackgroundColour() ;
74 m_foregroundColour
= parent
->GetForegroundColour() ;
76 m_hWnd
= (WXHWND
)CreateWindowEx
78 MakeExtendedStyle(m_windowStyle
),
81 WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
,
89 // Subclass again for purposes of dialog editing mode
92 SetFont(parent
->GetFont());
94 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
101 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
104 if ( panel
->GetDefaultItem() == this )
106 // don't leave the panel with invalid default item
107 panel
->SetDefaultItem(NULL
);
112 // ----------------------------------------------------------------------------
113 // size management including autosizing
114 // ----------------------------------------------------------------------------
116 wxSize
wxButton::DoGetBestSize()
118 wxString label
= wxGetWindowText(GetHWND());
120 GetTextExtent(label
, &wBtn
, NULL
);
123 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
125 // add a margin - the button is wider than just its label
128 // the button height is proportional to the height of the font used
129 int hBtn
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar
);
131 return wxSize(wBtn
, hBtn
);
134 // ----------------------------------------------------------------------------
135 // set this button as the default one in its panel
136 // ----------------------------------------------------------------------------
138 void wxButton::SetDefault()
140 wxWindow
*parent
= GetParent();
141 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
143 panel
->SetDefaultItem(this);
147 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
150 SendMessage(GetHwnd(), BM_SETSTYLE
, BS_DEFPUSHBUTTON
, 1L);
153 // ----------------------------------------------------------------------------
155 // ----------------------------------------------------------------------------
157 bool wxButton::SendClickEvent()
159 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
160 event
.SetEventObject(this);
162 return ProcessCommand(event
);
165 void wxButton::Command(wxCommandEvent
& event
)
167 ProcessCommand(event
);
170 // ----------------------------------------------------------------------------
171 // event/message handlers
172 // ----------------------------------------------------------------------------
174 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
176 bool processed
= FALSE
;
179 case 1: // 1 for accelerator
181 processed
= SendClickEvent();
188 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
195 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
197 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();