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);
238 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
241 SetDefaultStyle( this
244 } // end of wxButton::SetDefault
246 void wxButton::SetTmpDefault()
248 wxWindow
* pParent
= GetParent();
250 wxCHECK_RET( pParent
, _T("button without parent?") );
252 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
254 pParent
->SetTmpDefaultItem(this);
255 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
258 SetDefaultStyle( this
261 } // end of wxButton::SetTmpDefault
263 void wxButton::UnsetTmpDefault()
265 wxWindow
* pParent
= GetParent();
267 wxCHECK_RET( pParent
, _T("button without parent?") );
269 pParent
->SetTmpDefaultItem(NULL
);
271 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
273 SetDefaultStyle( this
276 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
279 } // end of wxButton::UnsetTmpDefault
281 void wxButton::SetDefaultStyle(
288 // We may be called with NULL pointer -- simpler to do the check here than
289 // in the caller which does wxDynamicCast()
295 // First, let DefDlgProc() know about the new default button
299 if (!wxTheApp
->IsActive())
303 // In OS/2 the dialog/panel doesn't really know it has a default
304 // button, the default button simply has that style. We'll just
305 // simulate by setting focus to it
309 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtn
), QWL_STYLE
);
310 if (!(lStyle
& BS_DEFAULT
) == bOn
)
312 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
317 lStyle
&= ~BS_DEFAULT
;
318 ::WinSetWindowULong(GetHwndOf(pBtn
), QWL_STYLE
, lStyle
);
323 // Redraw the button - it will notice itself that it's not the
324 // default one any longer
329 } // end of wxButton::UpdateDefaultStyle
331 // ----------------------------------------------------------------------------
332 // event/message handlers
333 // ----------------------------------------------------------------------------
335 bool wxButton::OS2Command(
340 bool bProcessed
= FALSE
;
344 case BN_CLICKED
: // normal buttons send this
345 case BN_DBLCLICKED
: // owner-drawn ones also send this
346 bProcessed
= SendClickEvent();
350 } // end of wxButton::OS2Command
352 WXHBRUSH
wxButton::OnCtlColor(
361 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
365 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
366 } // end of wxButton::OnCtlColor
368 void wxButton::MakeOwnerDrawn()
372 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
373 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
378 lStyle
|= BS_USERBUTTON
;
379 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
381 } // end of wxButton::MakeOwnerDrawn
383 WXDWORD
wxButton::OS2GetStyle(
385 , WXDWORD
* pdwExstyle
389 // Buttons never have an external border, they draw their own one
391 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
396 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
397 // each other in any resizeable dialog which has more than one button in
400 dwStyle
|= WS_CLIPSIBLINGS
;
402 } // end of wxButton::OS2GetStyle
404 MRESULT
wxButton::WindowProc(
411 // When we receive focus, we want to temporary become the default button in
412 // our parent panel so that pressing "Enter" would activate us -- and when
413 // losing it we should restore the previous default button as well
415 if (uMsg
== WM_SETFOCUS
)
417 if (SHORT1FROMMP(lParam
) == TRUE
)
423 // Let the default processign take place too
427 else if (uMsg
== WM_BUTTON1DBLCLK
)
430 // Emulate a click event to force an owner-drawn button to change its
431 // appearance - without this, it won't do it
433 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
439 // And conitnue with processing the message normally as well
444 // Let the base class do all real processing
446 return (wxControl::OS2WindowProc( uMsg
450 } // end of wxWindowProc