]> git.saurik.com Git - wxWidgets.git/blame - src/os2/button.cpp
wxDC::SetFont can now handle invalid font and doesn't assert anymore (other ports...
[wxWidgets.git] / src / os2 / button.cpp
CommitLineData
0e320a79
DW
1/////////////////////////////////////////////////////////////////////////////
2// Name: button.cpp
3// Purpose: wxButton
d88de032 4// Author: David Webster
0e320a79 5// Modified by:
d88de032 6// Created: 10/13/99
0e320a79 7// RCS-ID: $Id$
d88de032
DW
8// Copyright: (c) David Webster
9// Licence: wxWindows licence
0e320a79
DW
10/////////////////////////////////////////////////////////////////////////////
11
d88de032
DW
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifndef WX_PRECOMP
16 #include "wx/button.h"
17 #include "wx/brush.h"
18 #include "wx/panel.h"
19 #include "wx/bmpbuttn.h"
20 #include "wx/settings.h"
21 #include "wx/dcscreen.h"
0e320a79
DW
22#endif
23
d88de032 24#include "wx/os2/private.h"
0e320a79 25
987da0d4
DW
26#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
27
28//
29// Should be at the very least less than winDEFAULT_BUTTON_MARGIN
30//
31#define FOCUS_MARGIN 3
32
33#ifndef BST_CHECKED
34#define BST_CHECKED 0x0001
35#endif
36
0e320a79 37IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
0e320a79
DW
38
39// Button
40
987da0d4
DW
41bool wxButton::Create(
42 wxWindow* pParent
43, wxWindowID vId
44, const wxString& rsLabel
45, const wxPoint& rPos
46, const wxSize& rSize
47, long lStyle
5d4b632b 48#if wxUSE_VALIDATORS
987da0d4 49, const wxValidator& rValidator
5d4b632b 50#endif
987da0d4
DW
51, const wxString& rsName
52)
0e320a79 53{
987da0d4 54 SetName(rsName);
5d4b632b 55#if wxUSE_VALIDATORS
987da0d4 56 SetValidator(rValidator);
5d4b632b 57#endif
987da0d4
DW
58 m_windowStyle = lStyle;
59 pParent->AddChild((wxButton *)this);
60 if (vId == -1)
0e320a79
DW
61 m_windowId = NewControlId();
62 else
987da0d4
DW
63 m_windowId = vId;
64 lStyle = WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON;
65
66 //
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
70 //
71 if (m_windowStyle & wxCLIP_SIBLINGS )
72 lStyle |= WS_CLIPSIBLINGS;
73 m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle
74 ,WC_BUTTON // A Button class window
75 ,(PSZ)rsLabel.c_str() // Button text
76 ,lStyle // Button style
77 ,0, 0, 0, 0 // Location and size
78 ,GetHwndOf(pParent) // Owner handle
79 ,HWND_TOP // Top of Z-Order
80 ,vId // Identifier
81 ,NULL // No control data
82 ,NULL // No Presentation parameters
83 );
84 if (m_hWnd == 0)
85 {
86 return FALSE;
87 }
0e320a79 88
987da0d4
DW
89 //
90 // Subclass again for purposes of dialog editing mode
91 //
92 SubclassWin(m_hWnd);
93 SetFont(pParent->GetFont());
94 SetSize( rPos.x
95 ,rPos.y
96 ,rSize.x
97 ,rSize.y
98 );
99 return TRUE;
100} // end of wxButton::Create
0e320a79 101
d88de032 102wxButton::~wxButton()
0e320a79 103{
987da0d4
DW
104 wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel);
105
106 if (pPanel)
d88de032 107 {
987da0d4 108 if (pPanel->GetDefaultItem() == this)
d88de032 109 {
987da0d4
DW
110 //
111 // Don't leave the panel with invalid default item
112 //
113 pPanel->SetDefaultItem(NULL);
d88de032
DW
114 }
115 }
987da0d4 116} // end of wxButton::~wxButton
d88de032
DW
117
118// ----------------------------------------------------------------------------
119// size management including autosizing
120// ----------------------------------------------------------------------------
121
e78c4d50 122wxSize wxButton::DoGetBestSize() const
d88de032 123{
987da0d4
DW
124 wxString rsLabel = wxGetWindowText(GetHWND());
125 int nWidthButton;
126 int nWidthChar;
127 int nHeightChar;
128
129 GetTextExtent( rsLabel
130 ,&nWidthButton
131 ,NULL
132 );
133
134 wxGetCharSize( GetHWND()
135 ,&nWidthChar
136 ,&nHeightChar
137 ,(wxFont*)&GetFont()
138 );
139
140 //
141 // Add a margin - the button is wider than just its label
142 //
143 nWidthButton += 3 * nWidthChar;
144
145 //
146 // The button height is proportional to the height of the font used
147 //
148 int nHeightButton = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar);
149
150 //
151 // Need a little extra to make it look right
152 //
153 nHeightButton += nHeightChar/1.5;
154
155 wxSize vSize = GetDefaultSize();
156
157 if (nWidthButton > vSize.x)
158 vSize.x = nWidthButton;
159 if (nHeightButton > vSize.y)
160 vSize.y = nHeightButton;
161 return vSize;
162} // end of wxButton::DoGetBestSize
d88de032
DW
163
164/* static */
165wxSize wxButton::GetDefaultSize()
166{
987da0d4 167 static wxSize vSizeBtn;
d88de032 168
987da0d4 169 if (vSizeBtn.x == 0)
d88de032 170 {
987da0d4 171 wxScreenDC vDc;
d88de032 172
987da0d4
DW
173 vDc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
174
175 //
176 // The size of a standard button in the dialog units is 50x14,
d88de032
DW
177 // translate this to pixels
178 // NB1: the multipliers come from the Windows convention
179 // NB2: the extra +1/+2 were needed to get the size be the same as the
180 // size of the buttons in the standard dialog - I don't know how
181 // this happens, but on my system this size is 75x23 in pixels and
182 // 23*8 isn't even divisible by 14... Would be nice to understand
183 // why these constants are needed though!
987da0d4
DW
184 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
185 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
d88de032 186 }
987da0d4
DW
187 return vSizeBtn;
188} // end of wxButton::GetDefaultSize
d88de032 189
987da0d4
DW
190void wxButton::Command (
191 wxCommandEvent& rEvent
192)
d88de032 193{
987da0d4
DW
194 ProcessCommand (rEvent);
195} // end of wxButton::Command
d88de032
DW
196
197// ----------------------------------------------------------------------------
198// helpers
199// ----------------------------------------------------------------------------
200
201bool wxButton::SendClickEvent()
202{
987da0d4
DW
203 wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED
204 ,GetId()
205 );
d88de032 206
987da0d4
DW
207 vEvent.SetEventObject(this);
208 return ProcessCommand(vEvent);
209} // end of wxButton::SendClickEvent
0e320a79
DW
210
211void wxButton::SetDefault()
212{
987da0d4
DW
213 wxWindow* pParent = GetParent();
214 wxButton* pBtnOldDefault = NULL;
215 wxPanel* pPanel = wxDynamicCast(pParent, wxPanel);
216 long lStyle = 0L;
217
218 if (pParent)
219 {
220 wxWindow* pWinOldDefault = pParent->SetDefaultItem(this);
221
222 pBtnOldDefault = wxDynamicCast(pWinOldDefault, wxButton);
223 }
224 if (pBtnOldDefault && pBtnOldDefault != this)
225 {
226 //
227 // Remove the BS_DEFPUSHBUTTON style from the other button
228 //
229 lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE);
230
231 //
232 // Don't do it with the owner drawn buttons because it will reset
233 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
234 //
235 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
236 {
237 lStyle &= ~BS_DEFAULT;
238 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE, lStyle);
239 }
240 else
241 {
242 //
243 // Redraw the button - it will notice itself that it's not the
244 // default one any longer
245 //
246 pBtnOldDefault->Refresh();
247 }
248 }
0e320a79 249
987da0d4
DW
250 //
251 // Set this button as the default
252 //
253 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
254 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
255 {
256 lStyle != BS_DEFAULT;
257 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
258 }
259} // end of wxButton::SetDefault
0e320a79 260
d88de032
DW
261// ----------------------------------------------------------------------------
262// event/message handlers
263// ----------------------------------------------------------------------------
0e320a79 264
987da0d4
DW
265bool wxButton::OS2Command(
266 WXUINT uParam
267, WXWORD wId
268)
0e320a79 269{
987da0d4
DW
270 bool bProcessed = FALSE;
271
272 switch (uParam)
d88de032 273 {
987da0d4
DW
274 case BN_CLICKED: // normal buttons send this
275 case BN_DBLCLICKED: // owner-drawn ones also send this
276 bProcessed = SendClickEvent();
d88de032
DW
277 break;
278 }
987da0d4
DW
279 return bProcessed;
280} // end of wxButton::OS2Command
281
282WXHBRUSH wxButton::OnCtlColor(
283 WXHDC pDC
284, WXHWND pWnd
285, WXUINT nCtlColor
286, WXUINT uMessage
287, WXWPARAM wParam
288, WXLPARAM lParam
289)
290{
291 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
292 ,wxSOLID
293 );
294
295 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
296} // end of wxButton::OnCtlColor
297
298void wxButton::MakeOwnerDrawn()
0e320a79 299{
987da0d4 300 long lStyle = 0L;
d88de032 301
987da0d4
DW
302 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
303 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
304 {
305 //
306 // Make it so
307 //
308 lStyle |= BS_USERBUTTON;
309 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
310 }
311} // end of wxCButton::MakeOwnerDrawn
312
313MRESULT wxButton::WindowProc(
314 WXUINT uMsg
315, WXWPARAM wParam
316, WXLPARAM lParam
317)
318{
319 //
320 // When we receive focus, we want to become the default button in our
321 // parent panel
322 //
323 if (uMsg == WM_SETFOCUS)
324 {
325 SetDefault();
326
327 //
328 // Let the default processign take place too
329 //
330 }
331
332 else if (uMsg == WM_BUTTON1DBLCLK)
333 {
334 //
335 // Emulate a click event to force an owner-drawn button to change its
336 // appearance - without this, it won't do it
337 //
338 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
339 ,wParam
340 ,lParam
341 );
342
343 //
344 // And conitnue with processing the message normally as well
345 //
346 }
0e320a79 347
987da0d4
DW
348 //
349 // Let the base class do all real processing
350 //
351 return (wxControl::OS2WindowProc( uMsg
352 ,wParam
353 ,lParam
354 ));
355} // end of wxW indowProc
d88de032 356