1 /////////////////////////////////////////////////////////////////////////////
4 // Author: David Webster
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
16 #include "wx/button.h"
19 #include "wx/bmpbuttn.h"
20 #include "wx/settings.h"
21 #include "wx/dcscreen.h"
24 #include "wx/os2/private.h"
26 #if !USE_SHARED_LIBRARY
27 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
32 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
34 const wxSize
& size
, long style
,
36 const wxValidator
& validator
,
42 SetValidator(validator
);
44 m_windowStyle
= style
;
46 parent
->AddChild((wxButton
*)this);
49 m_windowId
= NewControlId();
53 // TODO: create button
60 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
63 if ( panel
->GetDefaultItem() == this )
65 // don't leave the panel with invalid default item
66 panel
->SetDefaultItem(NULL
);
71 // ----------------------------------------------------------------------------
72 // size management including autosizing
73 // ----------------------------------------------------------------------------
75 wxSize
wxButton::DoGetBestSize() const
77 wxString label
= wxGetWindowText(GetHWND());
79 GetTextExtent(label
, &wBtn
, NULL
);
82 wxGetCharSize(GetHWND(), &wChar
, &hChar
, (wxFont
*)&GetFont());
84 // add a margin - the button is wider than just its label
87 // the button height is proportional to the height of the font used
88 int hBtn
= 0;// TODO: BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
90 return wxSize(wBtn
, hBtn
);
94 wxSize
wxButton::GetDefaultSize()
96 static wxSize s_sizeBtn
;
98 if ( s_sizeBtn
.x
== 0 )
101 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
103 // the size of a standard button in the dialog units is 50x14,
104 // translate this to pixels
105 // NB1: the multipliers come from the Windows convention
106 // NB2: the extra +1/+2 were needed to get the size be the same as the
107 // size of the buttons in the standard dialog - I don't know how
108 // this happens, but on my system this size is 75x23 in pixels and
109 // 23*8 isn't even divisible by 14... Would be nice to understand
110 // why these constants are needed though!
111 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
112 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
118 void wxButton::Command (wxCommandEvent
& event
)
120 ProcessCommand (event
);
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 bool wxButton::SendClickEvent()
129 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
130 event
.SetEventObject(this);
132 return ProcessCommand(event
);
135 void wxButton::SetDefault()
137 wxWindow
*parent
= GetParent();
138 wxButton
*btnOldDefault
= NULL
;
139 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
141 panel
->SetDefaultItem(this);
143 // TODO: make button the default
146 // ----------------------------------------------------------------------------
147 // event/message handlers
148 // ----------------------------------------------------------------------------
150 bool wxButton::OS2Command(WXUINT param
, WXWORD id
)
152 bool processed
= FALSE
;
157 case 1: // 1 for accelerator
159 processed = SendClickEvent();
166 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
173 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
175 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();