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"
22 #include "wx/scrolwin.h"
25 #include "wx/os2/private.h"
27 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
30 // Should be at the very least less than winDEFAULT_BUTTON_MARGIN
32 #define FOCUS_MARGIN 3
35 #define BST_CHECKED 0x0001
38 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
42 bool wxButton::Create(
45 , const wxString
& rsLabel
50 , const wxValidator
& rValidator
52 , const wxString
& rsName
57 SetValidator(rValidator
);
59 m_windowStyle
= lStyle
;
60 pParent
->AddChild((wxButton
*)this);
62 m_windowId
= NewControlId();
65 lStyle
= WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
;
68 // OS/2 PM does not have Right/Left/Top/Bottom styles.
69 // We will have to define an additional style when we implement notebooks
70 // for a notebook page button
72 if (m_windowStyle
& wxCLIP_SIBLINGS
)
73 lStyle
|= WS_CLIPSIBLINGS
;
75 // If the parent is a scrolled window the controls must
76 // have this style or they will overlap the scrollbars
79 if (pParent
->IsKindOf(CLASSINFO(wxScrolledWindow
)) ||
80 pParent
->IsKindOf(CLASSINFO(wxGenericScrolledWindow
)))
81 lStyle
|= WS_CLIPSIBLINGS
;
83 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
84 ,WC_BUTTON
// A Button class window
85 ,(PSZ
)rsLabel
.c_str() // Button text
86 ,lStyle
// Button style
87 ,0, 0, 0, 0 // Location and size
88 ,GetHwndOf(pParent
) // Owner handle
89 ,HWND_TOP
// Top of Z-Order
91 ,NULL
// No control data
92 ,NULL
// No Presentation parameters
100 // Subclass again for purposes of dialog editing mode
103 SetFont(pParent
->GetFont());
110 } // end of wxButton::Create
112 wxButton::~wxButton()
114 wxPanel
* pPanel
= wxDynamicCast(GetParent(), wxPanel
);
118 if (pPanel
->GetDefaultItem() == this)
121 // Don't leave the panel with invalid default item
123 pPanel
->SetDefaultItem(NULL
);
126 } // end of wxButton::~wxButton
128 // ----------------------------------------------------------------------------
129 // size management including autosizing
130 // ----------------------------------------------------------------------------
132 wxSize
wxButton::DoGetBestSize() const
134 wxString rsLabel
= wxGetWindowText(GetHWND());
139 GetTextExtent( rsLabel
144 wxGetCharSize( GetHWND()
151 // Add a margin - the button is wider than just its label
153 nWidthButton
+= 3 * nWidthChar
;
156 // The button height is proportional to the height of the font used
158 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
161 // Need a little extra to make it look right
163 nHeightButton
+= nHeightChar
/1.5;
165 wxSize vSize
= GetDefaultSize();
167 if (nWidthButton
> vSize
.x
)
168 vSize
.x
= nWidthButton
;
169 if (nHeightButton
> vSize
.y
)
170 vSize
.y
= nHeightButton
;
172 } // end of wxButton::DoGetBestSize
175 wxSize
wxButton::GetDefaultSize()
177 static wxSize vSizeBtn
;
183 vDc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
186 // The size of a standard button in the dialog units is 50x14,
187 // translate this to pixels
188 // NB1: the multipliers come from the Windows convention
189 // NB2: the extra +1/+2 were needed to get the size be the same as the
190 // size of the buttons in the standard dialog - I don't know how
191 // this happens, but on my system this size is 75x23 in pixels and
192 // 23*8 isn't even divisible by 14... Would be nice to understand
193 // why these constants are needed though!
194 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
195 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
198 } // end of wxButton::GetDefaultSize
200 void wxButton::Command (
201 wxCommandEvent
& rEvent
204 ProcessCommand (rEvent
);
205 } // end of wxButton::Command
207 // ----------------------------------------------------------------------------
209 // ----------------------------------------------------------------------------
211 bool wxButton::SendClickEvent()
213 wxCommandEvent
vEvent( wxEVT_COMMAND_BUTTON_CLICKED
217 vEvent
.SetEventObject(this);
218 return ProcessCommand(vEvent
);
219 } // end of wxButton::SendClickEvent
221 void wxButton::SetDefault()
223 wxWindow
* pParent
= GetParent();
224 wxButton
* pBtnOldDefault
= NULL
;
225 wxPanel
* pPanel
= wxDynamicCast(pParent
, wxPanel
);
230 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
232 pBtnOldDefault
= wxDynamicCast(pWinOldDefault
, wxButton
);
234 if (pBtnOldDefault
&& pBtnOldDefault
!= this)
237 // Remove the BS_DEFPUSHBUTTON style from the other button
239 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
);
242 // Don't do it with the owner drawn buttons because it will reset
243 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
245 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
247 lStyle
&= ~BS_DEFAULT
;
248 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
, lStyle
);
253 // Redraw the button - it will notice itself that it's not the
254 // default one any longer
256 pBtnOldDefault
->Refresh();
261 // Set this button as the default
263 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
264 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
266 lStyle
!= BS_DEFAULT
;
267 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
269 } // end of wxButton::SetDefault
271 // ----------------------------------------------------------------------------
272 // event/message handlers
273 // ----------------------------------------------------------------------------
275 bool wxButton::OS2Command(
280 bool bProcessed
= FALSE
;
284 case BN_CLICKED
: // normal buttons send this
285 case BN_DBLCLICKED
: // owner-drawn ones also send this
286 bProcessed
= SendClickEvent();
290 } // end of wxButton::OS2Command
292 WXHBRUSH
wxButton::OnCtlColor(
301 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
305 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
306 } // end of wxButton::OnCtlColor
308 void wxButton::MakeOwnerDrawn()
312 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
313 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
318 lStyle
|= BS_USERBUTTON
;
319 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
321 } // end of wxCButton::MakeOwnerDrawn
323 MRESULT
wxButton::WindowProc(
330 // When we receive focus, we want to become the default button in our
333 if (uMsg
== WM_SETFOCUS
)
338 // Let the default processign take place too
342 else if (uMsg
== WM_BUTTON1DBLCLK
)
345 // Emulate a click event to force an owner-drawn button to change its
346 // appearance - without this, it won't do it
348 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
354 // And conitnue with processing the message normally as well
359 // Let the base class do all real processing
361 return (wxControl::OS2WindowProc( uMsg
365 } // end of wxW indowProc