1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "button.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #include "wx/button.h"
27 #include "wx/msw/private.h"
29 #if !USE_SHARED_LIBRARY
30 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
33 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
37 bool wxButton::MSWCommand(const WXUINT param
, const WXWORD id
)
39 if (param
== BN_CLICKED
)
41 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
42 event
.SetEventObject(this);
43 ProcessCommand(event
);
49 bool wxButton::Create(wxWindow
*parent
, const wxWindowID id
, const wxString
& label
,
51 const wxSize
& size
, const long style
,
52 const wxValidator
& validator
,
56 SetValidator(validator
);
58 parent
->AddChild((wxButton
*)this);
59 m_backgroundColour
= parent
->GetDefaultBackgroundColour() ;
60 m_foregroundColour
= parent
->GetDefaultForegroundColour() ;
62 m_windowStyle
= (long&)style
;
70 m_windowId
= NewControlId();
74 DWORD exStyle
= MakeExtendedStyle(m_windowStyle
);
76 CreateWindowEx(exStyle
, "BUTTON", label
, BS_PUSHBUTTON
| WS_TABSTOP
| WS_CHILD
,
77 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
78 wxGetInstance(), NULL
);
81 // if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
82 // Ctl3dSubclassCtl(wx_button);
85 m_hWnd
= (WXHWND
)wx_button
;
87 // Subclass again for purposes of dialog editing mode
88 SubclassWin((WXHWND
)wx_button
);
90 SetFont(* parent
->GetFont());
92 SetSize(x
, y
, width
, height
);
93 ShowWindow(wx_button
, SW_SHOW
);
98 void wxButton::SetSize(const int x
, const int y
, const int width
, const int height
, const int sizeFlags
)
100 int currentX
, currentY
;
101 GetPosition(¤tX
, ¤tY
);
104 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
106 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
109 int actualWidth
= width
;
110 int actualHeight
= height
;
117 GetWindowText((HWND
) GetHWND(), buf
, 300);
118 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,GetFont());
120 // If we're prepared to use the existing width, then...
121 if (width
== -1 && ((sizeFlags
& wxSIZE_AUTO_WIDTH
) != wxSIZE_AUTO_WIDTH
))
123 else if (width
== -1)
127 wxGetCharSize(GetHWND(), &cx
, &cy
,GetFont());
128 actualWidth
= (int)(current_width
+ 3*cx
) ;
131 // If we're prepared to use the existing height, then...
132 if (height
== -1 && ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) != wxSIZE_AUTO_HEIGHT
))
134 else if (height
== -1)
136 actualHeight
= (int)(cyf
*BUTTON_HEIGHT_FACTOR
) ;
139 MoveWindow((HWND
) GetHWND(), x1
, y1
, actualWidth
, actualHeight
, TRUE
);
142 if (!((width == -1) && (height == -1)))
144 #if WXWIN_COMPATIBILITY
145 GetEventHandler()->OldOnSize(width, height);
147 wxSizeEvent event(wxSize(width, height), m_windowId);
148 event.eventObject = this;
149 GetEventHandler()->ProcessEvent(event);
155 void wxButton::SetDefault(void)
157 wxWindow
*parent
= (wxWindow
*)GetParent();
159 parent
->SetDefaultItem(this);
163 SendMessage((HWND
) parent
->GetHWND(), DM_SETDEFID
, m_windowId
, 0L);
167 wxString
wxButton::GetLabel(void) const
169 GetWindowText((HWND
) GetHWND(), wxBuffer
, 300);
170 return wxString(wxBuffer
);
173 void wxButton::SetLabel(const wxString
& label
)
175 SetWindowText((HWND
) GetHWND(), (const char *) label
);
178 WXHBRUSH
wxButton::OnCtlColor(const WXHDC pDC
, const WXHWND pWnd
, const WXUINT nCtlColor
,
179 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
182 WXHBRUSH hBrush = (WXHBRUSH) MSWDefWindowProc(message, wParam, lParam);
183 // ::SetTextColor((HDC) pDC, GetSysColor(COLOR_BTNTEXT));
184 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(),
185 GetForegroundColour().Blue()));
189 // This doesn't in fact seem to make any difference at all - buttons are always
191 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
192 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
194 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
196 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
197 // has a zero usage count.
199 // backgroundBrush->RealizeResource();
200 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
203 void wxButton::Command (wxCommandEvent
& event
)
205 ProcessCommand (event
);