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