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
50 , 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
;
74 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
75 ,WC_BUTTON
// A Button class window
76 ,(PSZ
)rsLabel
.c_str() // Button text
77 ,lStyle
// Button style
78 ,0, 0, 0, 0 // Location and size
79 ,GetHwndOf(pParent
) // Owner handle
80 ,HWND_TOP
// Top of Z-Order
82 ,NULL
// No control data
83 ,NULL
// No Presentation parameters
91 // Subclass again for purposes of dialog editing mode
94 wxFont
* pButtonFont
= new wxFont( 8
99 SetFont(*pButtonFont
);
109 } // end of wxButton::Create
111 wxButton::~wxButton()
113 wxPanel
* pPanel
= wxDynamicCast(GetParent(), wxPanel
);
117 if (pPanel
->GetDefaultItem() == this)
120 // Don't leave the panel with invalid default item
122 pPanel
->SetDefaultItem(NULL
);
125 } // end of wxButton::~wxButton
127 // ----------------------------------------------------------------------------
128 // size management including autosizing
129 // ----------------------------------------------------------------------------
131 wxSize
wxButton::DoGetBestSize() const
133 wxString rsLabel
= wxGetWindowText(GetHWND());
138 GetTextExtent( rsLabel
143 wxGetCharSize( GetHWND()
150 // Add a margin - the button is wider than just its label
152 nWidthButton
+= 3 * nWidthChar
;
155 // The button height is proportional to the height of the font used
157 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
160 // Need a little extra to make it look right
162 nHeightButton
+= nHeightChar
/1.5;
164 if (!HasFlag(wxBU_EXACTFIT
))
166 wxSize vSize
= GetDefaultSize();
168 if (nWidthButton
> vSize
.x
)
169 vSize
.x
= nWidthButton
;
170 if (nHeightButton
> vSize
.y
)
171 vSize
.y
= nHeightButton
;
174 return wxSize( nWidthButton
177 } // end of wxButton::DoGetBestSize
180 wxSize
wxButton::GetDefaultSize()
182 static wxSize vSizeBtn
;
188 vDc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
191 // The size of a standard button in the dialog units is 50x14,
192 // translate this to pixels
193 // NB1: the multipliers come from the Windows convention
194 // NB2: the extra +1/+2 were needed to get the size be the same as the
195 // size of the buttons in the standard dialog - I don't know how
196 // this happens, but on my system this size is 75x23 in pixels and
197 // 23*8 isn't even divisible by 14... Would be nice to understand
198 // why these constants are needed though!
199 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
200 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
203 } // end of wxButton::GetDefaultSize
205 void wxButton::Command (
206 wxCommandEvent
& rEvent
209 ProcessCommand (rEvent
);
210 } // end of wxButton::Command
212 // ----------------------------------------------------------------------------
214 // ----------------------------------------------------------------------------
216 bool wxButton::SendClickEvent()
218 wxCommandEvent
vEvent( wxEVT_COMMAND_BUTTON_CLICKED
222 vEvent
.SetEventObject(this);
223 return ProcessCommand(vEvent
);
224 } // end of wxButton::SendClickEvent
226 void wxButton::SetDefault()
228 wxWindow
* pParent
= GetParent();
230 wxCHECK_RET( pParent
, _T("button without parent?") );
233 // Set this one as the default button both for wxWindows and Windows
235 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
237 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
240 SetDefaultStyle( this
243 } // end of wxButton::SetDefault
245 void wxButton::SetTmpDefault()
247 wxWindow
* pParent
= GetParent();
249 wxCHECK_RET( pParent
, _T("button without parent?") );
251 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
253 pParent
->SetTmpDefaultItem(this);
254 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
257 SetDefaultStyle( this
260 } // end of wxButton::SetTmpDefault
262 void wxButton::UnsetTmpDefault()
264 wxWindow
* pParent
= GetParent();
266 wxCHECK_RET( pParent
, _T("button without parent?") );
268 pParent
->SetTmpDefaultItem(NULL
);
270 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
272 SetDefaultStyle( this
275 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
)
278 } // end of wxButton::UnsetTmpDefault
280 void wxButton::SetDefaultStyle(
287 // We may be called with NULL pointer -- simpler to do the check here than
288 // in the caller which does wxDynamicCast()
294 // First, let DefDlgProc() know about the new default button
298 if (!wxTheApp
->IsActive())
302 // In OS/2 the dialog/panel doesn't really know it has a default
303 // button, the default button simply has that style. We'll just
304 // simulate by setting focus to it
308 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtn
), QWL_STYLE
);
309 if (!(lStyle
& BS_DEFAULT
) == bOn
)
311 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
316 lStyle
&= ~BS_DEFAULT
;
317 ::WinSetWindowULong(GetHwndOf(pBtn
), QWL_STYLE
, lStyle
);
322 // Redraw the button - it will notice itself that it's not the
323 // default one any longer
328 } // end of wxButton::UpdateDefaultStyle
330 // ----------------------------------------------------------------------------
331 // event/message handlers
332 // ----------------------------------------------------------------------------
334 bool wxButton::OS2Command(
339 bool bProcessed
= FALSE
;
343 case BN_CLICKED
: // normal buttons send this
344 case BN_DBLCLICKED
: // owner-drawn ones also send this
345 bProcessed
= SendClickEvent();
349 } // end of wxButton::OS2Command
351 WXHBRUSH
wxButton::OnCtlColor(
360 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
364 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
365 } // end of wxButton::OnCtlColor
367 void wxButton::MakeOwnerDrawn()
371 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
372 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
377 lStyle
|= BS_USERBUTTON
;
378 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
380 } // end of wxButton::MakeOwnerDrawn
382 WXDWORD
wxButton::OS2GetStyle(
384 , WXDWORD
* pdwExstyle
388 // Buttons never have an external border, they draw their own one
390 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
395 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
396 // each other in any resizeable dialog which has more than one button in
399 dwStyle
|= WS_CLIPSIBLINGS
;
401 } // end of wxButton::OS2GetStyle
403 MRESULT
wxButton::WindowProc(
410 // When we receive focus, we want to temporary become the default button in
411 // our parent panel so that pressing "Enter" would activate us -- and when
412 // losing it we should restore the previous default button as well
414 if (uMsg
== WM_SETFOCUS
)
416 if (SHORT1FROMMP(lParam
) == TRUE
)
422 // Let the default processign take place too
426 else if (uMsg
== WM_BUTTON1DBLCLK
)
429 // Emulate a click event to force an owner-drawn button to change its
430 // appearance - without this, it won't do it
432 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
438 // And conitnue with processing the message normally as well
443 // Let the base class do all real processing
445 return (wxControl::OS2WindowProc( uMsg
449 } // end of wxWindowProc