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"
17 #include "wx/button.h"
20 #include "wx/bmpbuttn.h"
21 #include "wx/settings.h"
22 #include "wx/dcscreen.h"
23 #include "wx/scrolwin.h"
26 #include "wx/os2/private.h"
28 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
31 // Should be at the very least less than winDEFAULT_BUTTON_MARGIN
33 #define FOCUS_MARGIN 3
36 #define BST_CHECKED 0x0001
39 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
43 bool wxButton::Create(
46 , const wxString
& rsLabel
51 , const wxValidator
& rValidator
53 , const wxString
& rsName
58 SetValidator(rValidator
);
60 m_windowStyle
= lStyle
;
61 pParent
->AddChild((wxButton
*)this);
63 m_windowId
= NewControlId();
66 lStyle
= WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
;
69 // OS/2 PM does not have Right/Left/Top/Bottom styles.
70 // We will have to define an additional style when we implement notebooks
71 // for a notebook page button
73 if (m_windowStyle
& wxCLIP_SIBLINGS
)
74 lStyle
|= WS_CLIPSIBLINGS
;
76 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
77 ,WC_BUTTON
// A Button class window
78 ,(PSZ
)rsLabel
.c_str() // Button text
79 ,lStyle
// Button style
80 ,0, 0, 0, 0 // Location and size
81 ,GetHwndOf(pParent
) // Owner handle
82 ,HWND_TOP
// Top of Z-Order
84 ,NULL
// No control data
85 ,NULL
// No Presentation parameters
93 // Subclass again for purposes of dialog editing mode
96 wxFont
* pButtonFont
= new wxFont( 8
101 SetFont(*pButtonFont
);
111 } // end of wxButton::Create
113 wxButton::~wxButton()
115 wxPanel
* pPanel
= wxDynamicCast(GetParent(), wxPanel
);
119 if (pPanel
->GetDefaultItem() == this)
122 // Don't leave the panel with invalid default item
124 pPanel
->SetDefaultItem(NULL
);
127 } // end of wxButton::~wxButton
129 // ----------------------------------------------------------------------------
130 // size management including autosizing
131 // ----------------------------------------------------------------------------
133 wxSize
wxButton::DoGetBestSize() const
135 wxString rsLabel
= wxGetWindowText(GetHWND());
140 GetTextExtent( rsLabel
145 wxGetCharSize( GetHWND()
152 // Add a margin - the button is wider than just its label
154 nWidthButton
+= 3 * nWidthChar
;
157 // The button height is proportional to the height of the font used
159 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
162 // Need a little extra to make it look right
164 nHeightButton
+= nHeightChar
/1.5;
166 if (!HasFlag(wxBU_EXACTFIT
))
168 wxSize vSize
= GetDefaultSize();
170 if (nWidthButton
> vSize
.x
)
171 vSize
.x
= nWidthButton
;
172 if (nHeightButton
> vSize
.y
)
173 vSize
.y
= nHeightButton
;
176 return wxSize( nWidthButton
179 } // end of wxButton::DoGetBestSize
182 wxSize
wxButton::GetDefaultSize()
184 static wxSize vSizeBtn
;
190 vDc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
193 // The size of a standard button in the dialog units is 50x14,
194 // translate this to pixels
195 // NB1: the multipliers come from the Windows convention
196 // NB2: the extra +1/+2 were needed to get the size be the same as the
197 // size of the buttons in the standard dialog - I don't know how
198 // this happens, but on my system this size is 75x23 in pixels and
199 // 23*8 isn't even divisible by 14... Would be nice to understand
200 // why these constants are needed though!
201 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
202 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
205 } // end of wxButton::GetDefaultSize
207 void wxButton::Command (
208 wxCommandEvent
& rEvent
211 ProcessCommand (rEvent
);
212 } // end of wxButton::Command
214 // ----------------------------------------------------------------------------
216 // ----------------------------------------------------------------------------
218 bool wxButton::SendClickEvent()
220 wxCommandEvent
vEvent( wxEVT_COMMAND_BUTTON_CLICKED
224 vEvent
.SetEventObject(this);
225 return ProcessCommand(vEvent
);
226 } // end of wxButton::SendClickEvent
228 void wxButton::SetDefault()
230 wxWindow
* pParent
= GetParent();
232 wxCHECK_RET( pParent
, _T("button without parent?") );
235 // Set this one as the default button both for wxWindows and Windows
237 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
239 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
242 SetDefaultStyle( this
245 } // end of wxButton::SetDefault
247 void wxButton::SetTmpDefault()
249 wxWindow
* pParent
= GetParent();
251 wxCHECK_RET( pParent
, _T("button without parent?") );
253 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
255 pParent
->SetTmpDefaultItem(this);
256 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
259 SetDefaultStyle( this
262 } // end of wxButton::SetTmpDefault
264 void wxButton::UnsetTmpDefault()
266 wxWindow
* pParent
= GetParent();
268 wxCHECK_RET( pParent
, _T("button without parent?") );
270 pParent
->SetTmpDefaultItem(NULL
);
272 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
274 SetDefaultStyle( this
277 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
280 } // end of wxButton::UnsetTmpDefault
282 void wxButton::SetDefaultStyle(
289 // We may be called with NULL pointer -- simpler to do the check here than
290 // in the caller which does wxDynamicCast()
296 // First, let DefDlgProc() know about the new default button
300 if (!wxTheApp
->IsActive())
304 // In OS/2 the dialog/panel doesn't really know it has a default
305 // button, the default button simply has that style. We'll just
306 // simulate by setting focus to it
310 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtn
), QWL_STYLE
);
311 if (!(lStyle
& BS_DEFAULT
) == bOn
)
313 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
318 lStyle
&= ~BS_DEFAULT
;
319 ::WinSetWindowULong(GetHwndOf(pBtn
), QWL_STYLE
, lStyle
);
324 // Redraw the button - it will notice itself that it's not the
325 // default one any longer
330 } // end of wxButton::UpdateDefaultStyle
332 // ----------------------------------------------------------------------------
333 // event/message handlers
334 // ----------------------------------------------------------------------------
336 bool wxButton::OS2Command(
341 bool bProcessed
= FALSE
;
345 case BN_CLICKED
: // normal buttons send this
346 case BN_DBLCLICKED
: // owner-drawn ones also send this
347 bProcessed
= SendClickEvent();
351 } // end of wxButton::OS2Command
353 WXHBRUSH
wxButton::OnCtlColor(
362 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
366 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
367 } // end of wxButton::OnCtlColor
369 void wxButton::MakeOwnerDrawn()
373 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
374 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
379 lStyle
|= BS_USERBUTTON
;
380 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
382 } // end of wxButton::MakeOwnerDrawn
384 WXDWORD
wxButton::OS2GetStyle(
386 , WXDWORD
* pdwExstyle
390 // Buttons never have an external border, they draw their own one
392 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
397 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
398 // each other in any resizeable dialog which has more than one button in
401 dwStyle
|= WS_CLIPSIBLINGS
;
403 } // end of wxButton::OS2GetStyle
405 MRESULT
wxButton::WindowProc(
412 // When we receive focus, we want to temporary become the default button in
413 // our parent panel so that pressing "Enter" would activate us -- and when
414 // losing it we should restore the previous default button as well
416 if (uMsg
== WM_SETFOCUS
)
418 if (SHORT1FROMMP(lParam
) == TRUE
)
424 // Let the default processign take place too
428 else if (uMsg
== WM_BUTTON1DBLCLK
)
431 // Emulate a click event to force an owner-drawn button to change its
432 // appearance - without this, it won't do it
434 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
440 // And conitnue with processing the message normally as well
445 // Let the base class do all real processing
447 return (wxControl::OS2WindowProc( uMsg
451 } // end of wxWindowProc