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