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"
24 #include "wx/os2/private.h"
26 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
29 // Should be at the very least less than winDEFAULT_BUTTON_MARGIN
31 #define FOCUS_MARGIN 3
34 #define BST_CHECKED 0x0001
37 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
41 bool wxButton::Create(
44 , const wxString
& rsLabel
49 , const wxValidator
& rValidator
51 , const wxString
& rsName
56 SetValidator(rValidator
);
58 m_windowStyle
= lStyle
;
59 pParent
->AddChild((wxButton
*)this);
61 m_windowId
= NewControlId();
64 lStyle
= WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
;
67 // OS/2 PM does not have Right/Left/Top/Bottom styles.
68 // We will have to define an additional style when we implement notebooks
69 // for a notebook page button
71 if (m_windowStyle
& wxCLIP_SIBLINGS
)
72 lStyle
|= WS_CLIPSIBLINGS
;
73 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
74 ,WC_BUTTON
// A Button class window
75 ,(PSZ
)rsLabel
.c_str() // Button text
76 ,lStyle
// Button style
77 ,0, 0, 0, 0 // Location and size
78 ,GetHwndOf(pParent
) // Owner handle
79 ,HWND_TOP
// Top of Z-Order
81 ,NULL
// No control data
82 ,NULL
// No Presentation parameters
90 // Subclass again for purposes of dialog editing mode
93 SetFont(pParent
->GetFont());
100 } // end of wxButton::Create
102 wxButton::~wxButton()
104 wxPanel
* pPanel
= wxDynamicCast(GetParent(), wxPanel
);
108 if (pPanel
->GetDefaultItem() == this)
111 // Don't leave the panel with invalid default item
113 pPanel
->SetDefaultItem(NULL
);
116 } // end of wxButton::~wxButton
118 // ----------------------------------------------------------------------------
119 // size management including autosizing
120 // ----------------------------------------------------------------------------
122 wxSize
wxButton::DoGetBestSize() const
124 wxString rsLabel
= wxGetWindowText(GetHWND());
129 GetTextExtent( rsLabel
134 wxGetCharSize( GetHWND()
141 // Add a margin - the button is wider than just its label
143 nWidthButton
+= 3 * nWidthChar
;
146 // The button height is proportional to the height of the font used
148 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
151 // Need a little extra to make it look right
153 nHeightButton
+= nHeightChar
/1.5;
155 wxSize vSize
= GetDefaultSize();
157 if (nWidthButton
> vSize
.x
)
158 vSize
.x
= nWidthButton
;
159 if (nHeightButton
> vSize
.y
)
160 vSize
.y
= nHeightButton
;
162 } // end of wxButton::DoGetBestSize
165 wxSize
wxButton::GetDefaultSize()
167 static wxSize vSizeBtn
;
173 vDc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
176 // The size of a standard button in the dialog units is 50x14,
177 // translate this to pixels
178 // NB1: the multipliers come from the Windows convention
179 // NB2: the extra +1/+2 were needed to get the size be the same as the
180 // size of the buttons in the standard dialog - I don't know how
181 // this happens, but on my system this size is 75x23 in pixels and
182 // 23*8 isn't even divisible by 14... Would be nice to understand
183 // why these constants are needed though!
184 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
185 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
188 } // end of wxButton::GetDefaultSize
190 void wxButton::Command (
191 wxCommandEvent
& rEvent
194 ProcessCommand (rEvent
);
195 } // end of wxButton::Command
197 // ----------------------------------------------------------------------------
199 // ----------------------------------------------------------------------------
201 bool wxButton::SendClickEvent()
203 wxCommandEvent
vEvent( wxEVT_COMMAND_BUTTON_CLICKED
207 vEvent
.SetEventObject(this);
208 return ProcessCommand(vEvent
);
209 } // end of wxButton::SendClickEvent
211 void wxButton::SetDefault()
213 wxWindow
* pParent
= GetParent();
214 wxButton
* pBtnOldDefault
= NULL
;
215 wxPanel
* pPanel
= wxDynamicCast(pParent
, wxPanel
);
220 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
222 pBtnOldDefault
= wxDynamicCast(pWinOldDefault
, wxButton
);
224 if (pBtnOldDefault
&& pBtnOldDefault
!= this)
227 // Remove the BS_DEFPUSHBUTTON style from the other button
229 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
);
232 // Don't do it with the owner drawn buttons because it will reset
233 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
235 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
237 lStyle
&= ~BS_DEFAULT
;
238 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
, lStyle
);
243 // Redraw the button - it will notice itself that it's not the
244 // default one any longer
246 pBtnOldDefault
->Refresh();
251 // Set this button as the default
253 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
254 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
256 lStyle
!= BS_DEFAULT
;
257 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
259 } // end of wxButton::SetDefault
261 // ----------------------------------------------------------------------------
262 // event/message handlers
263 // ----------------------------------------------------------------------------
265 bool wxButton::OS2Command(
270 bool bProcessed
= FALSE
;
274 case BN_CLICKED
: // normal buttons send this
275 case BN_DBLCLICKED
: // owner-drawn ones also send this
276 bProcessed
= SendClickEvent();
280 } // end of wxButton::OS2Command
282 WXHBRUSH
wxButton::OnCtlColor(
291 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
295 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
296 } // end of wxButton::OnCtlColor
298 void wxButton::MakeOwnerDrawn()
302 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
303 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
308 lStyle
|= BS_USERBUTTON
;
309 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
311 } // end of wxCButton::MakeOwnerDrawn
313 MRESULT
wxButton::WindowProc(
320 // When we receive focus, we want to become the default button in our
323 if (uMsg
== WM_SETFOCUS
)
328 // Let the default processign take place too
332 else if (uMsg
== WM_BUTTON1DBLCLK
)
335 // Emulate a click event to force an owner-drawn button to change its
336 // appearance - without this, it won't do it
338 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
344 // And conitnue with processing the message normally as well
349 // Let the base class do all real processing
351 return (wxControl::OS2WindowProc( uMsg
355 } // end of wxW indowProc