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 # if defined(__VISAGECPP__)
37 const wxValidator
* validator
,
39 const wxValidator
& validator
,
46 SetValidator(validator
);
48 m_windowStyle
= style
;
50 parent
->AddChild((wxButton
*)this);
53 m_windowId
= NewControlId();
57 // TODO: create button
64 wxPanel
*panel
= wxDynamicCast(GetParent(), wxPanel
);
67 if ( panel
->GetDefaultItem() == this )
69 // don't leave the panel with invalid default item
70 panel
->SetDefaultItem(NULL
);
75 // ----------------------------------------------------------------------------
76 // size management including autosizing
77 // ----------------------------------------------------------------------------
79 wxSize
wxButton::DoGetBestSize()
81 wxString label
= wxGetWindowText(GetHWND());
83 GetTextExtent(label
, &wBtn
, NULL
);
86 wxGetCharSize(GetHWND(), &wChar
, &hChar
, &GetFont());
88 // add a margin - the button is wider than just its label
91 // the button height is proportional to the height of the font used
92 int hBtn
= 0;// TODO: BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
94 return wxSize(wBtn
, hBtn
);
98 wxSize
wxButton::GetDefaultSize()
100 static wxSize s_sizeBtn
;
102 if ( s_sizeBtn
.x
== 0 )
105 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
107 // the size of a standard button in the dialog units is 50x14,
108 // translate this to pixels
109 // NB1: the multipliers come from the Windows convention
110 // NB2: the extra +1/+2 were needed to get the size be the same as the
111 // size of the buttons in the standard dialog - I don't know how
112 // this happens, but on my system this size is 75x23 in pixels and
113 // 23*8 isn't even divisible by 14... Would be nice to understand
114 // why these constants are needed though!
115 s_sizeBtn
.x
= (50 * (dc
.GetCharWidth() + 1))/4;
116 s_sizeBtn
.y
= ((14 * dc
.GetCharHeight()) + 2)/8;
122 void wxButton::Command (wxCommandEvent
& event
)
124 ProcessCommand (event
);
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
131 bool wxButton::SendClickEvent()
133 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
134 event
.SetEventObject(this);
136 return ProcessCommand(event
);
139 void wxButton::SetDefault()
141 wxWindow
*parent
= GetParent();
142 wxButton
*btnOldDefault
= NULL
;
143 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
145 panel
->SetDefaultItem(this);
147 // TODO: make button the default
150 // ----------------------------------------------------------------------------
151 // event/message handlers
152 // ----------------------------------------------------------------------------
154 bool wxButton::OS2Command(WXUINT param
, WXWORD id
)
156 bool processed
= FALSE
;
161 case 1: // 1 for accelerator
163 processed = SendClickEvent();
170 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
177 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
179 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();