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
,
35 const wxValidator
& validator
,
39 SetValidator(validator
);
40 m_windowStyle
= style
;
42 parent
->AddChild((wxButton
*)this);
45 m_windowId
= NewControlId();
49 // TODO: create button
56 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
59 if ( panel
->GetDefaultItem() == this )
61 // don't leave the panel with invalid default item
62 panel
->SetDefaultItem(NULL
);
67 // ----------------------------------------------------------------------------
68 // size management including autosizing
69 // ----------------------------------------------------------------------------
71 wxSize
wxButton::DoGetBestSize()
73 wxString label
= wxGetWindowText(GetHWND());
75 GetTextExtent(label
, &wBtn
, NULL
);
78 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
80 // add a margin - the button is wider than just its label
83 // the button height is proportional to the height of the font used
84 int hBtn
= 0;// TODO: BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
86 return wxSize(wBtn
, hBtn
);
90 wxSize
wxButton::GetDefaultSize()
92 static wxSize s_sizeBtn
;
94 if ( s_sizeBtn
.x
== 0 )
97 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
99 // the size of a standard button in the dialog units is 50x14,
100 // translate this to pixels
101 // NB1: the multipliers come from the Windows convention
102 // NB2: the extra +1/+2 were needed to get the size be the same as the
103 // size of the buttons in the standard dialog - I don't know how
104 // this happens, but on my system this size is 75x23 in pixels and
105 // 23*8 isn't even divisible by 14... Would be nice to understand
106 // why these constants are needed though!
107 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
108 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
114 void wxButton::Command (wxCommandEvent
& event
)
116 ProcessCommand (event
);
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 bool wxButton::SendClickEvent()
125 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
126 event
.SetEventObject(this);
128 return ProcessCommand(event
);
131 void wxButton::SetDefault()
133 wxWindow
*parent
= GetParent();
134 wxButton
*btnOldDefault
= NULL
;
135 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
137 panel
->SetDefaultItem(this);
139 // TODO: make button the default
142 // ----------------------------------------------------------------------------
143 // event/message handlers
144 // ----------------------------------------------------------------------------
146 bool wxButton::OS2Command(WXUINT param
, WXWORD id
)
148 bool processed
= FALSE
;
153 case 1: // 1 for accelerator
155 processed = SendClickEvent();
162 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
169 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
171 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();