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 if (!HasFlag(wxBU_EXACTFIT
))
167 wxSize vSize
= GetDefaultSize();
169 if (nWidthButton
> vSize
.x
)
170 vSize
.x
= nWidthButton
;
171 if (nHeightButton
> vSize
.y
)
172 vSize
.y
= nHeightButton
;
175 return wxSize( nWidthButton
178 } // end of wxButton::DoGetBestSize
181 wxSize
wxButton::GetDefaultSize()
183 static wxSize vSizeBtn
;
189 vDc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
192 // The size of a standard button in the dialog units is 50x14,
193 // translate this to pixels
194 // NB1: the multipliers come from the Windows convention
195 // NB2: the extra +1/+2 were needed to get the size be the same as the
196 // size of the buttons in the standard dialog - I don't know how
197 // this happens, but on my system this size is 75x23 in pixels and
198 // 23*8 isn't even divisible by 14... Would be nice to understand
199 // why these constants are needed though!
200 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
201 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
204 } // end of wxButton::GetDefaultSize
206 void wxButton::Command (
207 wxCommandEvent
& rEvent
210 ProcessCommand (rEvent
);
211 } // end of wxButton::Command
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 bool wxButton::SendClickEvent()
219 wxCommandEvent
vEvent( wxEVT_COMMAND_BUTTON_CLICKED
223 vEvent
.SetEventObject(this);
224 return ProcessCommand(vEvent
);
225 } // end of wxButton::SendClickEvent
227 void wxButton::SetDefault()
229 wxWindow
* pParent
= GetParent();
231 wxCHECK_RET( pParent
, _T("button without parent?") );
234 // Set this one as the default button both for wxWindows and Windows
236 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
237 UpdateDefaultStyle( this
240 } // end of wxButton::SetDefault
242 void wxButton::SetTmpDefault()
244 wxWindow
* pParent
= GetParent();
246 wxCHECK_RET( pParent
, _T("button without parent?") );
248 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
249 pParent
->SetTmpDefaultItem(this);
250 if (pWinOldDefault
!= this)
252 UpdateDefaultStyle( this
256 //else: no styles to update
257 } // end of wxButton::SetTmpDefault
259 void wxButton::UnsetTmpDefault()
261 wxWindow
* pParent
= GetParent();
263 wxCHECK_RET( pParent
, _T("button without parent?") );
265 pParent
->SetTmpDefaultItem(NULL
);
267 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
269 if (pWinOldDefault
!= this)
271 UpdateDefaultStyle( pWinOldDefault
275 //else: we had been default before anyhow
276 } // end of wxButton::UnsetTmpDefault
278 void wxButton::UpdateDefaultStyle(
279 wxWindow
* pWinDefault
280 , wxWindow
* pWinOldDefault
)
282 wxButton
* pBtnOldDefault
= wxDynamicCast(pWinOldDefault
, wxButton
);
285 if ( pBtnOldDefault
&& pBtnOldDefault
!= pWinDefault
)
287 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
);
288 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
290 lStyle
&= ~BS_DEFAULT
;
291 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
, lStyle
);
295 // redraw the button - it will notice itself that it's not the
296 // default one any longer
297 pBtnOldDefault
->Refresh();
301 wxButton
* pBtnDefault
= wxDynamicCast(pWinDefault
, wxButton
);
305 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnDefault
), QWL_STYLE
);
306 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
308 lStyle
!= BS_DEFAULT
;
309 ::WinSetWindowULong(GetHwndOf(pBtnDefault
), QWL_STYLE
, lStyle
);
312 } // end of wxButton::UpdateDefaultStyle
314 // ----------------------------------------------------------------------------
315 // event/message handlers
316 // ----------------------------------------------------------------------------
318 bool wxButton::OS2Command(
323 bool bProcessed
= FALSE
;
327 case BN_CLICKED
: // normal buttons send this
328 case BN_DBLCLICKED
: // owner-drawn ones also send this
329 bProcessed
= SendClickEvent();
333 } // end of wxButton::OS2Command
335 WXHBRUSH
wxButton::OnCtlColor(
344 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
348 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
349 } // end of wxButton::OnCtlColor
351 void wxButton::MakeOwnerDrawn()
355 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
356 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
361 lStyle
|= BS_USERBUTTON
;
362 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
364 } // end of wxButton::MakeOwnerDrawn
366 WXDWORD
wxButton::OS2GetStyle(
368 , WXDWORD
* pdwExstyle
372 // Buttons never have an external border, they draw their own one
374 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
379 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
380 // each other in any resizeable dialog which has more than one button in
383 dwStyle
|= WS_CLIPSIBLINGS
;
385 } // end of wxButton::OS2GetStyle
387 MRESULT
wxButton::WindowProc(
394 // When we receive focus, we want to temporary become the default button in
395 // our parent panel so that pressing "Enter" would activate us -- and when
396 // losing it we should restore the previous default button as well
398 if (uMsg
== WM_SETFOCUS
)
400 if (SHORT1FROMMP(lParam
) == TRUE
)
406 // Let the default processign take place too
410 else if (uMsg
== WM_BUTTON1DBLCLK
)
413 // Emulate a click event to force an owner-drawn button to change its
414 // appearance - without this, it won't do it
416 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
422 // And conitnue with processing the message normally as well
427 // Let the base class do all real processing
429 return (wxControl::OS2WindowProc( uMsg
433 } // end of wxWindowProc