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