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 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
76 ,WC_BUTTON
// A Button class window
77 ,(PSZ
)rsLabel
.c_str() // Button text
78 ,lStyle
// Button style
79 ,0, 0, 0, 0 // Location and size
80 ,GetHwndOf(pParent
) // Owner handle
81 ,HWND_TOP
// Top of Z-Order
83 ,NULL
// No control data
84 ,NULL
// No Presentation parameters
92 // Subclass again for purposes of dialog editing mode
95 wxFont
* pButtonFont
= new wxFont( 8
100 SetFont(*pButtonFont
);
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();
225 wxCHECK_RET( pParent
, _T("button without parent?") );
228 // Set this one as the default button both for wxWindows and Windows
230 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
231 UpdateDefaultStyle( this
234 } // end of wxButton::SetDefault
236 void wxButton::SetTmpDefault()
238 wxWindow
* pParent
= GetParent();
240 wxCHECK_RET( pParent
, _T("button without parent?") );
242 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
243 pParent
->SetTmpDefaultItem(this);
244 if (pWinOldDefault
!= this)
246 UpdateDefaultStyle( this
250 //else: no styles to update
251 } // end of wxButton::SetTmpDefault
253 void wxButton::UnsetTmpDefault()
255 wxWindow
* pParent
= GetParent();
257 wxCHECK_RET( pParent
, _T("button without parent?") );
259 pParent
->SetTmpDefaultItem(NULL
);
261 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
263 if (pWinOldDefault
!= this)
265 UpdateDefaultStyle( pWinOldDefault
269 //else: we had been default before anyhow
270 } // end of wxButton::UnsetTmpDefault
272 void wxButton::UpdateDefaultStyle(
273 wxWindow
* pWinDefault
274 , wxWindow
* pWinOldDefault
)
276 wxButton
* pBtnOldDefault
= wxDynamicCast(pWinOldDefault
, wxButton
);
279 if ( pBtnOldDefault
&& pBtnOldDefault
!= pWinDefault
)
281 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
);
282 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
284 lStyle
&= ~BS_DEFAULT
;
285 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
, lStyle
);
289 // redraw the button - it will notice itself that it's not the
290 // default one any longer
291 pBtnOldDefault
->Refresh();
295 wxButton
* pBtnDefault
= wxDynamicCast(pWinDefault
, wxButton
);
299 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnDefault
), QWL_STYLE
);
300 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
302 lStyle
!= BS_DEFAULT
;
303 ::WinSetWindowULong(GetHwndOf(pBtnDefault
), QWL_STYLE
, lStyle
);
306 } // end of wxButton::UpdateDefaultStyle
308 // ----------------------------------------------------------------------------
309 // event/message handlers
310 // ----------------------------------------------------------------------------
312 bool wxButton::OS2Command(
317 bool bProcessed
= FALSE
;
321 case BN_CLICKED
: // normal buttons send this
322 case BN_DBLCLICKED
: // owner-drawn ones also send this
323 bProcessed
= SendClickEvent();
327 } // end of wxButton::OS2Command
329 WXHBRUSH
wxButton::OnCtlColor(
338 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
342 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
343 } // end of wxButton::OnCtlColor
345 void wxButton::MakeOwnerDrawn()
349 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
350 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
355 lStyle
|= BS_USERBUTTON
;
356 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
358 } // end of wxButton::MakeOwnerDrawn
360 WXDWORD
wxButton::OS2GetStyle(
362 , WXDWORD
* pdwExstyle
366 // Buttons never have an external border, they draw their own one
368 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
373 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
374 // each other in any resizeable dialog which has more than one button in
377 dwStyle
|= WS_CLIPSIBLINGS
;
379 } // end of wxButton::OS2GetStyle
381 MRESULT
wxButton::WindowProc(
388 // When we receive focus, we want to temporary become the default button in
389 // our parent panel so that pressing "Enter" would activate us -- and when
390 // losing it we should restore the previous default button as well
392 if (uMsg
== WM_SETFOCUS
)
394 if (SHORT1FROMMP(lParam
) == TRUE
)
400 // Let the default processign take place too
404 else if (uMsg
== WM_BUTTON1DBLCLK
)
407 // Emulate a click event to force an owner-drawn button to change its
408 // appearance - without this, it won't do it
410 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
416 // And conitnue with processing the message normally as well
421 // Let the base class do all real processing
423 return (wxControl::OS2WindowProc( uMsg
427 } // end of wxWindowProc