Toolbar fixes and weekly catch-up.
[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 #include "wx/scrolwin.h"
23 #endif
24
25 #include "wx/os2/private.h"
26
27 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
28
29 //
30 // Should be at the very least less than winDEFAULT_BUTTON_MARGIN
31 //
32 #define FOCUS_MARGIN 3
33
34 #ifndef BST_CHECKED
35 #define BST_CHECKED 0x0001
36 #endif
37
38 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
39
40 // Button
41
42 bool wxButton::Create(
43 wxWindow* pParent
44 , wxWindowID vId
45 , const wxString& rsLabel
46 , const wxPoint& rPos
47 , const wxSize& rSize
48 , long lStyle
49 #if wxUSE_VALIDATORS
50 , const wxValidator& rValidator
51 #endif
52 , const wxString& rsName
53 )
54 {
55 SetName(rsName);
56 #if wxUSE_VALIDATORS
57 SetValidator(rValidator);
58 #endif
59 m_windowStyle = lStyle;
60 pParent->AddChild((wxButton *)this);
61 if (vId == -1)
62 m_windowId = NewControlId();
63 else
64 m_windowId = vId;
65 lStyle = WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON;
66
67 //
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
71 //
72 if (m_windowStyle & wxCLIP_SIBLINGS )
73 lStyle |= WS_CLIPSIBLINGS;
74
75 m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle
76 ,WC_BUTTON // A Button class window
77 ,(PSZ)rsLabel.c_str() // Button text
78 ,lStyle // Button style
79 ,0, 0, 0, 0 // Location and size
80 ,GetHwndOf(pParent) // Owner handle
81 ,HWND_TOP // Top of Z-Order
82 ,vId // Identifier
83 ,NULL // No control data
84 ,NULL // No Presentation parameters
85 );
86 if (m_hWnd == 0)
87 {
88 return FALSE;
89 }
90
91 //
92 // Subclass again for purposes of dialog editing mode
93 //
94 SubclassWin(m_hWnd);
95 wxFont* pButtonFont = new wxFont( 8
96 ,wxSWISS
97 ,wxNORMAL
98 ,wxNORMAL
99 );
100 SetFont(*pButtonFont);
101 SetXComp(0);
102 SetYComp(0);
103 SetSize( rPos.x
104 ,rPos.y
105 ,rSize.x
106 ,rSize.y
107 );
108 delete pButtonFont;
109 return TRUE;
110 } // end of wxButton::Create
111
112 wxButton::~wxButton()
113 {
114 wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel);
115
116 if (pPanel)
117 {
118 if (pPanel->GetDefaultItem() == this)
119 {
120 //
121 // Don't leave the panel with invalid default item
122 //
123 pPanel->SetDefaultItem(NULL);
124 }
125 }
126 } // end of wxButton::~wxButton
127
128 // ----------------------------------------------------------------------------
129 // size management including autosizing
130 // ----------------------------------------------------------------------------
131
132 wxSize wxButton::DoGetBestSize() const
133 {
134 wxString rsLabel = wxGetWindowText(GetHWND());
135 int nWidthButton;
136 int nWidthChar;
137 int nHeightChar;
138
139 GetTextExtent( rsLabel
140 ,&nWidthButton
141 ,NULL
142 );
143
144 wxGetCharSize( GetHWND()
145 ,&nWidthChar
146 ,&nHeightChar
147 ,(wxFont*)&GetFont()
148 );
149
150 //
151 // Add a margin - the button is wider than just its label
152 //
153 nWidthButton += 3 * nWidthChar;
154
155 //
156 // The button height is proportional to the height of the font used
157 //
158 int nHeightButton = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar);
159
160 //
161 // Need a little extra to make it look right
162 //
163 nHeightButton += nHeightChar/1.5;
164
165 if (!HasFlag(wxBU_EXACTFIT))
166 {
167 wxSize vSize = GetDefaultSize();
168
169 if (nWidthButton > vSize.x)
170 vSize.x = nWidthButton;
171 if (nHeightButton > vSize.y)
172 vSize.y = nHeightButton;
173 return vSize;
174 }
175 return wxSize( nWidthButton
176 ,nHeightButton
177 );
178 } // end of wxButton::DoGetBestSize
179
180 /* static */
181 wxSize wxButton::GetDefaultSize()
182 {
183 static wxSize vSizeBtn;
184
185 if (vSizeBtn.x == 0)
186 {
187 wxScreenDC vDc;
188
189 vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
190
191 //
192 // The size of a standard button in the dialog units is 50x14,
193 // translate this to pixels
194 // NB1: the multipliers come from the Windows convention
195 // NB2: the extra +1/+2 were needed to get the size be the same as the
196 // size of the buttons in the standard dialog - I don't know how
197 // this happens, but on my system this size is 75x23 in pixels and
198 // 23*8 isn't even divisible by 14... Would be nice to understand
199 // why these constants are needed though!
200 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
201 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
202 }
203 return vSizeBtn;
204 } // end of wxButton::GetDefaultSize
205
206 void wxButton::Command (
207 wxCommandEvent& rEvent
208 )
209 {
210 ProcessCommand (rEvent);
211 } // end of wxButton::Command
212
213 // ----------------------------------------------------------------------------
214 // helpers
215 // ----------------------------------------------------------------------------
216
217 bool wxButton::SendClickEvent()
218 {
219 wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED
220 ,GetId()
221 );
222
223 vEvent.SetEventObject(this);
224 return ProcessCommand(vEvent);
225 } // end of wxButton::SendClickEvent
226
227 void wxButton::SetDefault()
228 {
229 wxWindow* pParent = GetParent();
230
231 wxCHECK_RET( pParent, _T("button without parent?") );
232
233 //
234 // Set this one as the default button both for wxWindows and Windows
235 //
236 wxWindow* pWinOldDefault = pParent->SetDefaultItem(this);
237 UpdateDefaultStyle( this
238 ,pWinOldDefault
239 );
240 } // end of wxButton::SetDefault
241
242 void wxButton::SetTmpDefault()
243 {
244 wxWindow* pParent = GetParent();
245
246 wxCHECK_RET( pParent, _T("button without parent?") );
247
248 wxWindow* pWinOldDefault = pParent->GetDefaultItem();
249 pParent->SetTmpDefaultItem(this);
250 if (pWinOldDefault != this)
251 {
252 UpdateDefaultStyle( this
253 ,pWinOldDefault
254 );
255 }
256 //else: no styles to update
257 } // end of wxButton::SetTmpDefault
258
259 void wxButton::UnsetTmpDefault()
260 {
261 wxWindow* pParent = GetParent();
262
263 wxCHECK_RET( pParent, _T("button without parent?") );
264
265 pParent->SetTmpDefaultItem(NULL);
266
267 wxWindow* pWinOldDefault = pParent->GetDefaultItem();
268
269 if (pWinOldDefault != this)
270 {
271 UpdateDefaultStyle( pWinOldDefault
272 ,this
273 );
274 }
275 //else: we had been default before anyhow
276 } // end of wxButton::UnsetTmpDefault
277
278 void wxButton::UpdateDefaultStyle(
279 wxWindow* pWinDefault
280 , wxWindow* pWinOldDefault)
281 {
282 wxButton* pBtnOldDefault = wxDynamicCast(pWinOldDefault, wxButton);
283 long lStyle;
284
285 if ( pBtnOldDefault && pBtnOldDefault != pWinDefault )
286 {
287 lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE);
288 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
289 {
290 lStyle &= ~BS_DEFAULT;
291 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE, lStyle);
292 }
293 else
294 {
295 // redraw the button - it will notice itself that it's not the
296 // default one any longer
297 pBtnOldDefault->Refresh();
298 }
299 }
300
301 wxButton* pBtnDefault = wxDynamicCast(pWinDefault, wxButton);
302
303 if (pBtnDefault)
304 {
305 lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnDefault), QWL_STYLE);
306 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
307 {
308 lStyle != BS_DEFAULT;
309 ::WinSetWindowULong(GetHwndOf(pBtnDefault), QWL_STYLE, lStyle);
310 }
311 }
312 } // end of wxButton::UpdateDefaultStyle
313
314 // ----------------------------------------------------------------------------
315 // event/message handlers
316 // ----------------------------------------------------------------------------
317
318 bool wxButton::OS2Command(
319 WXUINT uParam
320 , WXWORD wId
321 )
322 {
323 bool bProcessed = FALSE;
324
325 switch (uParam)
326 {
327 case BN_CLICKED: // normal buttons send this
328 case BN_DBLCLICKED: // owner-drawn ones also send this
329 bProcessed = SendClickEvent();
330 break;
331 }
332 return bProcessed;
333 } // end of wxButton::OS2Command
334
335 WXHBRUSH wxButton::OnCtlColor(
336 WXHDC pDC
337 , WXHWND pWnd
338 , WXUINT nCtlColor
339 , WXUINT uMessage
340 , WXWPARAM wParam
341 , WXLPARAM lParam
342 )
343 {
344 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
345 ,wxSOLID
346 );
347
348 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
349 } // end of wxButton::OnCtlColor
350
351 void wxButton::MakeOwnerDrawn()
352 {
353 long lStyle = 0L;
354
355 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
356 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
357 {
358 //
359 // Make it so
360 //
361 lStyle |= BS_USERBUTTON;
362 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
363 }
364 } // end of wxButton::MakeOwnerDrawn
365
366 WXDWORD wxButton::OS2GetStyle(
367 long lStyle
368 , WXDWORD* pdwExstyle
369 ) const
370 {
371 //
372 // Buttons never have an external border, they draw their own one
373 //
374 WXDWORD dwStyle = wxControl::OS2GetStyle( (lStyle & ~wxBORDER_MASK) | wxBORDER_NONE
375 ,pdwExstyle
376 );
377
378 //
379 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
380 // each other in any resizeable dialog which has more than one button in
381 // the bottom
382 //
383 dwStyle |= WS_CLIPSIBLINGS;
384 return dwStyle;
385 } // end of wxButton::OS2GetStyle
386
387 MRESULT wxButton::WindowProc(
388 WXUINT uMsg
389 , WXWPARAM wParam
390 , WXLPARAM lParam
391 )
392 {
393 //
394 // When we receive focus, we want to temporary become the default button in
395 // our parent panel so that pressing "Enter" would activate us -- and when
396 // losing it we should restore the previous default button as well
397 //
398 if (uMsg == WM_SETFOCUS)
399 {
400 if (SHORT1FROMMP(lParam) == TRUE)
401 SetTmpDefault();
402 else
403 UnsetTmpDefault();
404
405 //
406 // Let the default processign take place too
407 //
408 }
409
410 else if (uMsg == WM_BUTTON1DBLCLK)
411 {
412 //
413 // Emulate a click event to force an owner-drawn button to change its
414 // appearance - without this, it won't do it
415 //
416 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
417 ,wParam
418 ,lParam
419 );
420
421 //
422 // And conitnue with processing the message normally as well
423 //
424 }
425
426 //
427 // Let the base class do all real processing
428 //
429 return (wxControl::OS2WindowProc( uMsg
430 ,wParam
431 ,lParam
432 ));
433 } // end of wxWindowProc
434