]> git.saurik.com Git - wxWidgets.git/blob - src/os2/button.cpp
moved test for valid controlpart into this method
[wxWidgets.git] / src / os2 / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose: wxButton
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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"
22 #endif
23
24 #include "wx/os2/private.h"
25
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
37 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
38
39 // Button
40
41 bool wxButton::Create(
42 wxWindow* pParent
43 , wxWindowID vId
44 , const wxString& rsLabel
45 , const wxPoint& rPos
46 , const wxSize& rSize
47 , long lStyle
48 #if wxUSE_VALIDATORS
49 , const wxValidator& rValidator
50 #endif
51 , const wxString& rsName
52 )
53 {
54 SetName(rsName);
55 #if wxUSE_VALIDATORS
56 SetValidator(rValidator);
57 #endif
58 m_windowStyle = lStyle;
59 pParent->AddChild((wxButton *)this);
60 if (vId == -1)
61 m_windowId = NewControlId();
62 else
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 }
88
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
101
102 wxButton::~wxButton()
103 {
104 wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel);
105
106 if (pPanel)
107 {
108 if (pPanel->GetDefaultItem() == this)
109 {
110 //
111 // Don't leave the panel with invalid default item
112 //
113 pPanel->SetDefaultItem(NULL);
114 }
115 }
116 } // end of wxButton::~wxButton
117
118 // ----------------------------------------------------------------------------
119 // size management including autosizing
120 // ----------------------------------------------------------------------------
121
122 wxSize wxButton::DoGetBestSize() const
123 {
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
163
164 /* static */
165 wxSize wxButton::GetDefaultSize()
166 {
167 static wxSize vSizeBtn;
168
169 if (vSizeBtn.x == 0)
170 {
171 wxScreenDC vDc;
172
173 vDc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
174
175 //
176 // The size of a standard button in the dialog units is 50x14,
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!
184 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
185 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
186 }
187 return vSizeBtn;
188 } // end of wxButton::GetDefaultSize
189
190 void wxButton::Command (
191 wxCommandEvent& rEvent
192 )
193 {
194 ProcessCommand (rEvent);
195 } // end of wxButton::Command
196
197 // ----------------------------------------------------------------------------
198 // helpers
199 // ----------------------------------------------------------------------------
200
201 bool wxButton::SendClickEvent()
202 {
203 wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED
204 ,GetId()
205 );
206
207 vEvent.SetEventObject(this);
208 return ProcessCommand(vEvent);
209 } // end of wxButton::SendClickEvent
210
211 void wxButton::SetDefault()
212 {
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 }
249
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
260
261 // ----------------------------------------------------------------------------
262 // event/message handlers
263 // ----------------------------------------------------------------------------
264
265 bool wxButton::OS2Command(
266 WXUINT uParam
267 , WXWORD wId
268 )
269 {
270 bool bProcessed = FALSE;
271
272 switch (uParam)
273 {
274 case BN_CLICKED: // normal buttons send this
275 case BN_DBLCLICKED: // owner-drawn ones also send this
276 bProcessed = SendClickEvent();
277 break;
278 }
279 return bProcessed;
280 } // end of wxButton::OS2Command
281
282 WXHBRUSH 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
298 void wxButton::MakeOwnerDrawn()
299 {
300 long lStyle = 0L;
301
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
313 MRESULT 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 }
347
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
356