]> git.saurik.com Git - wxWidgets.git/blame - src/os2/button.cpp
Changed syntax of test for -fvtable-thunks so it works.
[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"
a4a16252 22 #include "wx/scrolwin.h"
0e320a79
DW
23#endif
24
d88de032 25#include "wx/os2/private.h"
0e320a79 26
987da0d4
DW
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
0e320a79 38IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
0e320a79
DW
39
40// Button
41
987da0d4
DW
42bool wxButton::Create(
43 wxWindow* pParent
44, wxWindowID vId
45, const wxString& rsLabel
46, const wxPoint& rPos
47, const wxSize& rSize
48, long lStyle
5d4b632b 49#if wxUSE_VALIDATORS
987da0d4 50, const wxValidator& rValidator
5d4b632b 51#endif
987da0d4
DW
52, const wxString& rsName
53)
0e320a79 54{
987da0d4 55 SetName(rsName);
5d4b632b 56#if wxUSE_VALIDATORS
987da0d4 57 SetValidator(rValidator);
5d4b632b 58#endif
987da0d4
DW
59 m_windowStyle = lStyle;
60 pParent->AddChild((wxButton *)this);
61 if (vId == -1)
0e320a79
DW
62 m_windowId = NewControlId();
63 else
987da0d4
DW
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;
5d44b24e 74
987da0d4
DW
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 }
0e320a79 90
987da0d4
DW
91 //
92 // Subclass again for purposes of dialog editing mode
93 //
94 SubclassWin(m_hWnd);
2c1e8f2e
DW
95 wxFont* pButtonFont = new wxFont( 8
96 ,wxSWISS
97 ,wxNORMAL
98 ,wxNORMAL
99 );
100 SetFont(*pButtonFont);
101 SetXComp(0);
102 SetYComp(0);
987da0d4
DW
103 SetSize( rPos.x
104 ,rPos.y
105 ,rSize.x
106 ,rSize.y
107 );
b3260bce 108 delete pButtonFont;
987da0d4
DW
109 return TRUE;
110} // end of wxButton::Create
0e320a79 111
d88de032 112wxButton::~wxButton()
0e320a79 113{
987da0d4
DW
114 wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel);
115
116 if (pPanel)
d88de032 117 {
987da0d4 118 if (pPanel->GetDefaultItem() == this)
d88de032 119 {
987da0d4
DW
120 //
121 // Don't leave the panel with invalid default item
122 //
123 pPanel->SetDefaultItem(NULL);
d88de032
DW
124 }
125 }
987da0d4 126} // end of wxButton::~wxButton
d88de032
DW
127
128// ----------------------------------------------------------------------------
129// size management including autosizing
130// ----------------------------------------------------------------------------
131
e78c4d50 132wxSize wxButton::DoGetBestSize() const
d88de032 133{
987da0d4
DW
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
97d74dd2
DW
165 if (!HasFlag(wxBU_EXACTFIT))
166 {
167 wxSize vSize = GetDefaultSize();
987da0d4 168
97d74dd2
DW
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 );
987da0d4 178} // end of wxButton::DoGetBestSize
d88de032
DW
179
180/* static */
181wxSize wxButton::GetDefaultSize()
182{
987da0d4 183 static wxSize vSizeBtn;
d88de032 184
987da0d4 185 if (vSizeBtn.x == 0)
d88de032 186 {
987da0d4 187 wxScreenDC vDc;
d88de032 188
a756f210 189 vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
987da0d4
DW
190
191 //
192 // The size of a standard button in the dialog units is 50x14,
d88de032
DW
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!
987da0d4
DW
200 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
201 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
d88de032 202 }
987da0d4
DW
203 return vSizeBtn;
204} // end of wxButton::GetDefaultSize
d88de032 205
987da0d4
DW
206void wxButton::Command (
207 wxCommandEvent& rEvent
208)
d88de032 209{
987da0d4
DW
210 ProcessCommand (rEvent);
211} // end of wxButton::Command
d88de032
DW
212
213// ----------------------------------------------------------------------------
214// helpers
215// ----------------------------------------------------------------------------
216
217bool wxButton::SendClickEvent()
218{
987da0d4
DW
219 wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED
220 ,GetId()
221 );
d88de032 222
987da0d4
DW
223 vEvent.SetEventObject(this);
224 return ProcessCommand(vEvent);
225} // end of wxButton::SendClickEvent
0e320a79
DW
226
227void wxButton::SetDefault()
228{
987da0d4 229 wxWindow* pParent = GetParent();
987da0d4 230
430974f8
DW
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);
cfcebdb1
DW
237
238 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton)
239 ,FALSE
240 );
241 SetDefaultStyle( this
242 ,TRUE
243 );
430974f8
DW
244} // end of wxButton::SetDefault
245
246void wxButton::SetTmpDefault()
247{
248 wxWindow* pParent = GetParent();
249
250 wxCHECK_RET( pParent, _T("button without parent?") );
251
252 wxWindow* pWinOldDefault = pParent->GetDefaultItem();
cfcebdb1 253
430974f8 254 pParent->SetTmpDefaultItem(this);
cfcebdb1
DW
255 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton)
256 ,FALSE
257 );
258 SetDefaultStyle( this
259 ,TRUE
260 );
430974f8
DW
261} // end of wxButton::SetTmpDefault
262
263void wxButton::UnsetTmpDefault()
264{
265 wxWindow* pParent = GetParent();
266
267 wxCHECK_RET( pParent, _T("button without parent?") );
987da0d4 268
430974f8
DW
269 pParent->SetTmpDefaultItem(NULL);
270
271 wxWindow* pWinOldDefault = pParent->GetDefaultItem();
272
cfcebdb1
DW
273 SetDefaultStyle( this
274 ,FALSE
275 );
276 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton)
277 ,TRUE
278 );
430974f8
DW
279} // end of wxButton::UnsetTmpDefault
280
cfcebdb1
DW
281void wxButton::SetDefaultStyle(
282 wxButton* pBtn
283, bool bOn
284)
430974f8 285{
430974f8 286 long lStyle;
cfcebdb1
DW
287 //
288 // We may be called with NULL pointer -- simpler to do the check here than
289 // in the caller which does wxDynamicCast()
290 //
291 if (!pBtn)
292 return;
430974f8 293
cfcebdb1
DW
294 //
295 // First, let DefDlgProc() know about the new default button
296 //
297 if (bOn)
298 {
299 if (!wxTheApp->IsActive())
300 return;
301
302 //
303 // In OS/2 the dialog/panel doesn't really know it has a default
304 // button, the default button simply has that style. We'll just
305 // simulate by setting focus to it
306 //
307 pBtn->SetFocus();
308 }
309 lStyle = ::WinQueryWindowULong(GetHwndOf(pBtn), QWL_STYLE);
310 if (!(lStyle & BS_DEFAULT) == bOn)
987da0d4 311 {
987da0d4
DW
312 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
313 {
cfcebdb1
DW
314 if (bOn)
315 lStyle | BS_DEFAULT;
316 else
317 lStyle &= ~BS_DEFAULT;
318 ::WinSetWindowULong(GetHwndOf(pBtn), QWL_STYLE, lStyle);
987da0d4
DW
319 }
320 else
321 {
cfcebdb1
DW
322 //
323 // Redraw the button - it will notice itself that it's not the
987da0d4 324 // default one any longer
cfcebdb1
DW
325 //
326 pBtn->Refresh();
430974f8 327 }
987da0d4 328 }
430974f8 329} // end of wxButton::UpdateDefaultStyle
0e320a79 330
d88de032
DW
331// ----------------------------------------------------------------------------
332// event/message handlers
333// ----------------------------------------------------------------------------
0e320a79 334
987da0d4
DW
335bool wxButton::OS2Command(
336 WXUINT uParam
337, WXWORD wId
338)
0e320a79 339{
987da0d4
DW
340 bool bProcessed = FALSE;
341
342 switch (uParam)
d88de032 343 {
987da0d4
DW
344 case BN_CLICKED: // normal buttons send this
345 case BN_DBLCLICKED: // owner-drawn ones also send this
346 bProcessed = SendClickEvent();
d88de032
DW
347 break;
348 }
987da0d4
DW
349 return bProcessed;
350} // end of wxButton::OS2Command
351
352WXHBRUSH wxButton::OnCtlColor(
353 WXHDC pDC
354, WXHWND pWnd
355, WXUINT nCtlColor
356, WXUINT uMessage
357, WXWPARAM wParam
358, WXLPARAM lParam
359)
360{
361 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
362 ,wxSOLID
363 );
364
365 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
366} // end of wxButton::OnCtlColor
367
368void wxButton::MakeOwnerDrawn()
0e320a79 369{
987da0d4 370 long lStyle = 0L;
d88de032 371
987da0d4
DW
372 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
373 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
374 {
375 //
376 // Make it so
377 //
378 lStyle |= BS_USERBUTTON;
379 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
380 }
bc5a847c
DW
381} // end of wxButton::MakeOwnerDrawn
382
383WXDWORD wxButton::OS2GetStyle(
384 long lStyle
385, WXDWORD* pdwExstyle
386) const
387{
388 //
389 // Buttons never have an external border, they draw their own one
390 //
391 WXDWORD dwStyle = wxControl::OS2GetStyle( (lStyle & ~wxBORDER_MASK) | wxBORDER_NONE
392 ,pdwExstyle
393 );
394
395 //
396 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
397 // each other in any resizeable dialog which has more than one button in
398 // the bottom
399 //
400 dwStyle |= WS_CLIPSIBLINGS;
401 return dwStyle;
402} // end of wxButton::OS2GetStyle
987da0d4
DW
403
404MRESULT wxButton::WindowProc(
405 WXUINT uMsg
406, WXWPARAM wParam
407, WXLPARAM lParam
408)
409{
410 //
430974f8
DW
411 // When we receive focus, we want to temporary become the default button in
412 // our parent panel so that pressing "Enter" would activate us -- and when
413 // losing it we should restore the previous default button as well
987da0d4
DW
414 //
415 if (uMsg == WM_SETFOCUS)
416 {
430974f8
DW
417 if (SHORT1FROMMP(lParam) == TRUE)
418 SetTmpDefault();
419 else
420 UnsetTmpDefault();
987da0d4
DW
421
422 //
423 // Let the default processign take place too
424 //
425 }
426
427 else if (uMsg == WM_BUTTON1DBLCLK)
428 {
429 //
430 // Emulate a click event to force an owner-drawn button to change its
431 // appearance - without this, it won't do it
432 //
433 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
434 ,wParam
435 ,lParam
436 );
437
438 //
439 // And conitnue with processing the message normally as well
440 //
441 }
0e320a79 442
987da0d4
DW
443 //
444 // Let the base class do all real processing
445 //
446 return (wxControl::OS2WindowProc( uMsg
447 ,wParam
448 ,lParam
449 ));
bc5a847c 450} // end of wxWindowProc
d88de032 451