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 void wxButton::DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
118 int currentX
, currentY
;
119 GetPosition(¤tX
, ¤tY
);
122 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
124 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
127 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
129 int actualWidth
= width
;
130 int actualHeight
= height
;
136 wxString buf
= wxGetWindowText(GetHWND());
137 GetTextExtent(buf
, ¤t_width
, &cyf
, NULL
, NULL
, &GetFont());
139 // If we're prepared to use the existing width, then...
140 if (width
== -1 && ((sizeFlags
& wxSIZE_AUTO_WIDTH
) != wxSIZE_AUTO_WIDTH
))
144 else if (width
== -1)
148 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
149 actualWidth
= (int)(current_width
+ 3*cx
) ;
152 // If we're prepared to use the existing height, then...
153 if (height
== -1 && ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) != wxSIZE_AUTO_HEIGHT
))
157 else if (height
== -1)
159 actualHeight
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cyf
);
162 MoveWindow(GetHwnd(), x1
, y1
, actualWidth
, actualHeight
, TRUE
);
165 void wxButton::SetDefault()
167 wxWindow
*parent
= GetParent();
168 wxPanel
*panel
= wxDynamicCast(parent
, wxPanel
);
170 panel
->SetDefaultItem(this);
174 SendMessage(GetWinHwnd(parent
), DM_SETDEFID
, m_windowId
, 0L);
177 SendMessage(GetHwnd(), BM_SETSTYLE
, BS_DEFPUSHBUTTON
, 1L);
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
184 bool wxButton::SendClickEvent()
186 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, GetId());
187 event
.SetEventObject(this);
189 return ProcessCommand(event
);
192 void wxButton::Command(wxCommandEvent
& event
)
194 ProcessCommand(event
);
197 // ----------------------------------------------------------------------------
198 // event/message handlers
199 // ----------------------------------------------------------------------------
201 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
203 bool processed
= FALSE
;
206 case 1: // 1 for accelerator
208 processed
= SendClickEvent();
215 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
,
222 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
224 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();