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_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
37 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
39 if (param
== BN_CLICKED
|| (param
== 1)) // 1 for accelerator
41 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
42 event
.SetEventObject(this);
43 ProcessCommand(event
);
49 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
56 SetValidator(validator
);
58 parent
->AddChild((wxButton
*)this);
59 m_backgroundColour
= parent
->GetBackgroundColour() ;
60 m_foregroundColour
= parent
->GetForegroundColour() ;
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(int x
, int y
, int width
, int height
, 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 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
111 int actualWidth
= width
;
112 int actualHeight
= height
;
119 GetWindowText((HWND
) GetHWND(), buf
, 300);
120 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,& GetFont());
122 // If we're prepared to use the existing width, then...
123 if (width
== -1 && ((sizeFlags
& wxSIZE_AUTO_WIDTH
) != wxSIZE_AUTO_WIDTH
))
125 else if (width
== -1)
129 wxGetCharSize(GetHWND(), &cx
, &cy
,& GetFont());
130 actualWidth
= (int)(current_width
+ 3*cx
) ;
133 // If we're prepared to use the existing height, then...
134 if (height
== -1 && ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) != wxSIZE_AUTO_HEIGHT
))
136 else if (height
== -1)
138 actualHeight
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cyf
);
141 MoveWindow((HWND
) GetHWND(), x1
, y1
, actualWidth
, actualHeight
, TRUE
);
144 void wxButton::SetDefault(void)
146 wxWindow
*parent
= (wxWindow
*)GetParent();
148 parent
->SetDefaultItem(this);
152 SendMessage((HWND
)parent
->GetHWND(), DM_SETDEFID
, m_windowId
, 0L);
155 SendMessage((HWND
)GetHWND(), BM_SETSTYLE
, BS_DEFPUSHBUTTON
, 1L);
158 wxString
wxButton::GetLabel(void) const
160 GetWindowText((HWND
) GetHWND(), wxBuffer
, 300);
161 return wxString(wxBuffer
);
164 void wxButton::SetLabel(const wxString
& label
)
166 SetWindowText((HWND
) GetHWND(), (const char *) label
);
169 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
170 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
173 WXHBRUSH hBrush = (WXHBRUSH) MSWDefWindowProc(message, wParam, lParam);
174 // ::SetTextColor((HDC) pDC, GetSysColor(COLOR_BTNTEXT));
175 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(),
176 GetForegroundColour().Blue()));
180 // This doesn't in fact seem to make any difference at all - buttons are always
182 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
183 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
185 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
187 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
188 // has a zero usage count.
190 // backgroundBrush->RealizeResource();
191 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
194 void wxButton::Command (wxCommandEvent
& event
)
196 ProcessCommand (event
);