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"
28 #include "wx/msw/private.h"
30 #if !USE_SHARED_LIBRARY
31 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
34 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
38 bool wxButton::MSWCommand(WXUINT param
, WXWORD id
)
40 if (param
== BN_CLICKED
|| (param
== 1)) // 1 for accelerator
42 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
43 event
.SetEventObject(this);
44 ProcessCommand(event
);
50 bool wxButton::Create(wxWindow
*parent
, wxWindowID id
, const wxString
& label
,
52 const wxSize
& size
, long style
,
53 const wxValidator
& validator
,
57 SetValidator(validator
);
59 parent
->AddChild((wxButton
*)this);
60 m_backgroundColour
= parent
->GetBackgroundColour() ;
61 m_foregroundColour
= parent
->GetForegroundColour() ;
63 m_windowStyle
= (long&)style
;
71 m_windowId
= NewControlId();
75 DWORD exStyle
= MakeExtendedStyle(m_windowStyle
);
77 CreateWindowEx(exStyle
, "BUTTON", label
, BS_PUSHBUTTON
| WS_TABSTOP
| WS_CHILD
,
78 0, 0, 0, 0, (HWND
) parent
->GetHWND(), (HMENU
)m_windowId
,
79 wxGetInstance(), NULL
);
82 // if (!(GetParent()->GetWindowStyleFlag() & wxUSER_COLOURS))
83 // Ctl3dSubclassCtl(wx_button);
86 m_hWnd
= (WXHWND
)wx_button
;
88 // Subclass again for purposes of dialog editing mode
89 SubclassWin((WXHWND
)wx_button
);
91 SetFont(parent
->GetFont());
93 SetSize(x
, y
, width
, height
);
94 ShowWindow(wx_button
, SW_SHOW
);
99 void wxButton::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
101 int currentX
, currentY
;
102 GetPosition(¤tX
, ¤tY
);
105 if (x
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
107 if (y
== -1 || (sizeFlags
& wxSIZE_ALLOW_MINUS_ONE
))
110 AdjustForParentClientOrigin(x1
, y1
, sizeFlags
);
112 int actualWidth
= width
;
113 int actualHeight
= height
;
120 GetWindowText((HWND
) GetHWND(), buf
, 300);
121 GetTextExtent(buf
, ¤t_width
, &cyf
,NULL
,NULL
,
124 // If we're prepared to use the existing width, then...
125 if (width
== -1 && ((sizeFlags
& wxSIZE_AUTO_WIDTH
) != wxSIZE_AUTO_WIDTH
))
127 else if (width
== -1)
131 wxGetCharSize(GetHWND(), &cx
, &cy
, & this->GetFont());
132 actualWidth
= (int)(current_width
+ 3*cx
) ;
135 // If we're prepared to use the existing height, then...
136 if (height
== -1 && ((sizeFlags
& wxSIZE_AUTO_HEIGHT
) != wxSIZE_AUTO_HEIGHT
))
138 else if (height
== -1)
140 actualHeight
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cyf
);
143 MoveWindow((HWND
) GetHWND(), x1
, y1
, actualWidth
, actualHeight
, TRUE
);
146 void wxButton::SetDefault(void)
148 wxWindow
*parent
= (wxWindow
*)GetParent();
150 parent
->SetDefaultItem(this);
154 SendMessage((HWND
)parent
->GetHWND(), DM_SETDEFID
, m_windowId
, 0L);
157 SendMessage((HWND
)GetHWND(), BM_SETSTYLE
, BS_DEFPUSHBUTTON
, 1L);
160 wxString
wxButton::GetLabel(void) const
162 GetWindowText((HWND
) GetHWND(), wxBuffer
, 300);
163 return wxString(wxBuffer
);
166 void wxButton::SetLabel(const wxString
& label
)
168 SetWindowText((HWND
) GetHWND(), (const char *) label
);
171 WXHBRUSH
wxButton::OnCtlColor(WXHDC pDC
, WXHWND pWnd
, WXUINT nCtlColor
,
172 WXUINT message
, WXWPARAM wParam
, WXLPARAM lParam
)
175 WXHBRUSH hBrush = (WXHBRUSH) MSWDefWindowProc(message, wParam, lParam);
176 // ::SetTextColor((HDC) pDC, GetSysColor(COLOR_BTNTEXT));
177 ::SetTextColor((HDC) pDC, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(),
178 GetForegroundColour().Blue()));
182 // This doesn't in fact seem to make any difference at all - buttons are always
184 ::SetBkColor((HDC
) pDC
, RGB(GetBackgroundColour().Red(), GetBackgroundColour().Green(), GetBackgroundColour().Blue()));
185 ::SetTextColor((HDC
) pDC
, RGB(GetForegroundColour().Red(), GetForegroundColour().Green(), GetForegroundColour().Blue()));
187 wxBrush
*backgroundBrush
= wxTheBrushList
->FindOrCreateBrush(GetBackgroundColour(), wxSOLID
);
189 // Note that this will be cleaned up in wxApp::OnIdle, if backgroundBrush
190 // has a zero usage count.
192 // backgroundBrush->RealizeResource();
193 return (WXHBRUSH
) backgroundBrush
->GetResourceHandle();
196 void wxButton::Command (wxCommandEvent
& event
)
198 ProcessCommand (event
);