1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/button.cpp
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"
15 #include "wx/button.h"
21 #include "wx/bmpbuttn.h"
22 #include "wx/settings.h"
23 #include "wx/dcscreen.h"
24 #include "wx/scrolwin.h"
27 #include "wx/stockitem.h"
28 #include "wx/os2/private.h"
30 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
33 // Should be at the very least less than winDEFAULT_BUTTON_MARGIN
35 #define FOCUS_MARGIN 3
38 #define BST_CHECKED 0x0001
41 IMPLEMENT_DYNAMIC_CLASS(wxButton
, wxControl
)
45 bool wxButton::Create( wxWindow
* pParent
,
47 const wxString
& rsLbl
,
51 const wxValidator
& rValidator
,
52 const wxString
& rsName
)
54 wxString
rsLabel(rsLbl
);
55 if (rsLabel
.empty() && wxIsStockID(vId
))
56 rsLabel
= wxGetStockLabel(vId
);
58 wxString sLabel
= ::wxPMTextToLabel(rsLabel
);
62 SetValidator(rValidator
);
64 m_windowStyle
= lStyle
;
65 pParent
->AddChild((wxButton
*)this);
67 m_windowId
= NewControlId();
70 lStyle
= WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
;
73 // OS/2 PM does not have Right/Left/Top/Bottom styles.
74 // We will have to define an additional style when we implement notebooks
75 // for a notebook page button
77 if (m_windowStyle
& wxCLIP_SIBLINGS
)
78 lStyle
|= WS_CLIPSIBLINGS
;
80 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
81 ,WC_BUTTON
// A Button class window
82 ,(PSZ
)sLabel
.c_str() // Button text
83 ,lStyle
// Button style
84 ,0, 0, 0, 0 // Location and size
85 ,GetHwndOf(pParent
) // Owner handle
86 ,HWND_TOP
// Top of Z-Order
88 ,NULL
// No control data
89 ,NULL
// No Presentation parameters
97 // Subclass again for purposes of dialog editing mode
100 wxFont
* pButtonFont
= new wxFont( 8
105 SetFont(*pButtonFont
);
115 } // end of wxButton::Create
117 wxButton::~wxButton()
119 wxPanel
* pPanel
= wxDynamicCast(GetParent(), wxPanel
);
123 if (pPanel
->GetDefaultItem() == this)
126 // Don't leave the panel with invalid default item
128 pPanel
->SetDefaultItem(NULL
);
131 } // end of wxButton::~wxButton
133 // ----------------------------------------------------------------------------
134 // size management including autosizing
135 // ----------------------------------------------------------------------------
137 wxSize
wxButton::DoGetBestSize() const
139 wxString rsLabel
= wxGetWindowText(GetHWND());
143 wxFont vFont
= (wxFont
)GetFont();
145 GetTextExtent( rsLabel
150 wxGetCharSize( GetHWND()
157 // Add a margin - the button is wider than just its label
159 nWidthButton
+= 3 * nWidthChar
;
162 // The button height is proportional to the height of the font used
164 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
167 // Need a little extra to make it look right
169 nHeightButton
+= (int)(nHeightChar
/1.5);
171 if (!HasFlag(wxBU_EXACTFIT
))
173 wxSize vSize
= GetDefaultSize();
175 if (nWidthButton
> vSize
.x
)
176 vSize
.x
= nWidthButton
;
177 if (nHeightButton
> vSize
.y
)
178 vSize
.y
= nHeightButton
;
181 return wxSize( nWidthButton
184 } // end of wxButton::DoGetBestSize
187 wxSize
wxButton::GetDefaultSize()
189 static wxSize vSizeBtn
;
195 vDc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
198 // The size of a standard button in the dialog units is 50x14,
199 // translate this to pixels
200 // NB1: the multipliers come from the Windows convention
201 // NB2: the extra +1/+2 were needed to get the size be the same as the
202 // size of the buttons in the standard dialog - I don't know how
203 // this happens, but on my system this size is 75x23 in pixels and
204 // 23*8 isn't even divisible by 14... Would be nice to understand
205 // why these constants are needed though!
206 vSizeBtn
.x
= (50 * (vDc
.GetCharWidth() + 1))/4;
207 vSizeBtn
.y
= ((14 * vDc
.GetCharHeight()) + 2)/8;
210 } // end of wxButton::GetDefaultSize
212 void wxButton::Command (
213 wxCommandEvent
& rEvent
216 ProcessCommand (rEvent
);
217 } // end of wxButton::Command
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 bool wxButton::SendClickEvent()
225 wxCommandEvent
vEvent( wxEVT_COMMAND_BUTTON_CLICKED
229 vEvent
.SetEventObject(this);
230 return ProcessCommand(vEvent
);
231 } // end of wxButton::SendClickEvent
233 void wxButton::SetDefault()
235 wxWindow
* pParent
= GetParent();
237 wxCHECK_RET( pParent
, _T("button without parent?") );
240 // Set this one as the default button both for wxWidgets and Windows
242 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
244 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
), false);
245 SetDefaultStyle( this, true );
246 } // end of wxButton::SetDefault
248 void wxButton::SetTmpDefault()
250 wxWindow
* pParent
= GetParent();
252 wxCHECK_RET( pParent
, _T("button without parent?") );
254 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
256 pParent
->SetTmpDefaultItem(this);
257 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
), false);
258 SetDefaultStyle( this, true );
259 } // end of wxButton::SetTmpDefault
261 void wxButton::UnsetTmpDefault()
263 wxWindow
* pParent
= GetParent();
265 wxCHECK_RET( pParent
, _T("button without parent?") );
267 pParent
->SetTmpDefaultItem(NULL
);
269 wxWindow
* pWinOldDefault
= pParent
->GetDefaultItem();
271 SetDefaultStyle( this, false );
272 SetDefaultStyle( wxDynamicCast(pWinOldDefault
, wxButton
), true );
273 } // end of wxButton::UnsetTmpDefault
275 void wxButton::SetDefaultStyle(
282 // We may be called with NULL pointer -- simpler to do the check here than
283 // in the caller which does wxDynamicCast()
289 // First, let DefDlgProc() know about the new default button
293 if (!wxTheApp
->IsActive())
297 // In OS/2 the dialog/panel doesn't really know it has a default
298 // button, the default button simply has that style. We'll just
299 // simulate by setting focus to it
303 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtn
), QWL_STYLE
);
304 if (!(lStyle
& BS_DEFAULT
) == bOn
)
306 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
309 lStyle
|= BS_DEFAULT
;
311 lStyle
&= ~BS_DEFAULT
;
312 ::WinSetWindowULong(GetHwndOf(pBtn
), QWL_STYLE
, lStyle
);
317 // Redraw the button - it will notice itself that it's not the
318 // default one any longer
323 } // end of wxButton::UpdateDefaultStyle
325 // ----------------------------------------------------------------------------
326 // event/message handlers
327 // ----------------------------------------------------------------------------
329 bool wxButton::OS2Command(WXUINT uParam
, WXWORD
WXUNUSED(wId
))
331 bool bProcessed
= false;
335 case BN_CLICKED
: // normal buttons send this
336 case BN_DBLCLICKED
: // owner-drawn ones also send this
337 bProcessed
= SendClickEvent();
342 } // end of wxButton::OS2Command
344 WXHBRUSH
wxButton::OnCtlColor( WXHDC
WXUNUSED(pDC
),
345 WXHWND
WXUNUSED(pWnd
),
346 WXUINT
WXUNUSED(nCtlColor
),
347 WXUINT
WXUNUSED(uMessage
),
348 WXWPARAM
WXUNUSED(wParam
),
349 WXLPARAM
WXUNUSED(lParam
) )
351 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
355 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
356 } // end of wxButton::OnCtlColor
358 void wxButton::MakeOwnerDrawn()
362 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
363 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
368 lStyle
|= BS_USERBUTTON
;
369 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
371 } // end of wxButton::MakeOwnerDrawn
373 WXDWORD
wxButton::OS2GetStyle(
375 , WXDWORD
* pdwExstyle
379 // Buttons never have an external border, they draw their own one
381 WXDWORD dwStyle
= wxControl::OS2GetStyle( (lStyle
& ~wxBORDER_MASK
) | wxBORDER_NONE
386 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
387 // each other in any resizeable dialog which has more than one button in
390 dwStyle
|= WS_CLIPSIBLINGS
;
392 } // end of wxButton::OS2GetStyle
394 MRESULT
wxButton::WindowProc( WXUINT uMsg
,
399 // When we receive focus, we want to temporary become the default button in
400 // our parent panel so that pressing "Enter" would activate us -- and when
401 // losing it we should restore the previous default button as well
403 if (uMsg
== WM_SETFOCUS
)
405 if (SHORT1FROMMP(lParam
) == TRUE
)
411 // Let the default processign take place too
415 else if (uMsg
== WM_BUTTON1DBLCLK
)
418 // Emulate a click event to force an owner-drawn button to change its
419 // appearance - without this, it won't do it
421 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
427 // And conitnue with processing the message normally as well
432 // Let the base class do all real processing
434 return (wxControl::OS2WindowProc( uMsg
438 } // end of wxWindowProc