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 // If the parent is a scrolled window the controls must
76 // have this style or they will overlap the scrollbars
79 if (pParent
->IsKindOf(CLASSINFO(wxScrolledWindow
)) ||
80 pParent
->IsKindOf(CLASSINFO(wxGenericScrolledWindow
)))
81 lStyle
|= WS_CLIPSIBLINGS
;
83 m_hWnd
= (WXHWND
)::WinCreateWindow( GetHwndOf(pParent
) // Parent handle
84 ,WC_BUTTON
// A Button class window
85 ,(PSZ
)rsLabel
.c_str() // Button text
86 ,lStyle
// Button style
87 ,0, 0, 0, 0 // Location and size
88 ,GetHwndOf(pParent
) // Owner handle
89 ,HWND_TOP
// Top of Z-Order
91 ,NULL
// No control data
92 ,NULL
// No Presentation parameters
100 // Subclass again for purposes of dialog editing mode
103 wxFont
* pButtonFont
= new wxFont( 8
108 SetFont(*pButtonFont
);
117 } // end of wxButton::Create
119 wxButton::~wxButton()
121 wxPanel
* pPanel
= wxDynamicCast(GetParent(), wxPanel
);
125 if (pPanel
->GetDefaultItem() == this)
128 // Don't leave the panel with invalid default item
130 pPanel
->SetDefaultItem(NULL
);
133 } // end of wxButton::~wxButton
135 // ----------------------------------------------------------------------------
136 // size management including autosizing
137 // ----------------------------------------------------------------------------
139 wxSize
wxButton::DoGetBestSize() const
141 wxString rsLabel
= wxGetWindowText(GetHWND());
146 GetTextExtent( rsLabel
151 wxGetCharSize( GetHWND()
158 // Add a margin - the button is wider than just its label
160 nWidthButton
+= 3 * nWidthChar
;
163 // The button height is proportional to the height of the font used
165 int nHeightButton
= BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar
);
168 // Need a little extra to make it look right
170 nHeightButton
+= nHeightChar
/1.5;
172 wxSize vSize
= GetDefaultSize();
174 if (nWidthButton
> vSize
.x
)
175 vSize
.x
= nWidthButton
;
176 if (nHeightButton
> vSize
.y
)
177 vSize
.y
= nHeightButton
;
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();
231 wxButton
* pBtnOldDefault
= NULL
;
232 wxPanel
* pPanel
= wxDynamicCast(pParent
, wxPanel
);
237 wxWindow
* pWinOldDefault
= pParent
->SetDefaultItem(this);
239 pBtnOldDefault
= wxDynamicCast(pWinOldDefault
, wxButton
);
241 if (pBtnOldDefault
&& pBtnOldDefault
!= this)
244 // Remove the BS_DEFPUSHBUTTON style from the other button
246 lStyle
= ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
);
249 // Don't do it with the owner drawn buttons because it will reset
250 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
252 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
254 lStyle
&= ~BS_DEFAULT
;
255 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault
), QWL_STYLE
, lStyle
);
260 // Redraw the button - it will notice itself that it's not the
261 // default one any longer
263 pBtnOldDefault
->Refresh();
268 // Set this button as the default
270 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
271 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
273 lStyle
!= BS_DEFAULT
;
274 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
276 } // end of wxButton::SetDefault
278 // ----------------------------------------------------------------------------
279 // event/message handlers
280 // ----------------------------------------------------------------------------
282 bool wxButton::OS2Command(
287 bool bProcessed
= FALSE
;
291 case BN_CLICKED
: // normal buttons send this
292 case BN_DBLCLICKED
: // owner-drawn ones also send this
293 bProcessed
= SendClickEvent();
297 } // end of wxButton::OS2Command
299 WXHBRUSH
wxButton::OnCtlColor(
308 wxBrush
* pBackgroundBrush
= wxTheBrushList
->FindOrCreateBrush( GetBackgroundColour()
312 return (WXHBRUSH
)pBackgroundBrush
->GetResourceHandle();
313 } // end of wxButton::OnCtlColor
315 void wxButton::MakeOwnerDrawn()
319 lStyle
= ::WinQueryWindowULong(GetHwnd(), QWL_STYLE
);
320 if ((lStyle
& BS_USERBUTTON
) != BS_USERBUTTON
)
325 lStyle
|= BS_USERBUTTON
;
326 ::WinSetWindowULong(GetHwnd(), QWL_STYLE
, lStyle
);
328 } // end of wxCButton::MakeOwnerDrawn
330 MRESULT
wxButton::WindowProc(
337 // When we receive focus, we want to become the default button in our
340 if (uMsg
== WM_SETFOCUS
)
345 // Let the default processign take place too
349 else if (uMsg
== WM_BUTTON1DBLCLK
)
352 // Emulate a click event to force an owner-drawn button to change its
353 // appearance - without this, it won't do it
355 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
361 // And conitnue with processing the message normally as well
366 // Let the base class do all real processing
368 return (wxControl::OS2WindowProc( uMsg
372 } // end of wxW indowProc