]> git.saurik.com Git - wxWidgets.git/blame - src/os2/button.cpp
missing commit, see #10269
[wxWidgets.git] / src / os2 / button.cpp
CommitLineData
0e320a79 1/////////////////////////////////////////////////////////////////////////////
f1e01716 2// Name: src/os2/button.cpp
0e320a79 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
f1e01716
WS
15#include "wx/button.h"
16
d88de032 17#ifndef WX_PRECOMP
1a75e76f 18 #include "wx/app.h"
d88de032
DW
19 #include "wx/brush.h"
20 #include "wx/panel.h"
21 #include "wx/bmpbuttn.h"
22 #include "wx/settings.h"
23 #include "wx/dcscreen.h"
a4a16252 24 #include "wx/scrolwin.h"
39ad4a54 25 #include "wx/toplevel.h"
0e320a79
DW
26#endif
27
5f7bcb48 28#include "wx/stockitem.h"
d88de032 29#include "wx/os2/private.h"
0e320a79 30
987da0d4
DW
31#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
32
33//
34// Should be at the very least less than winDEFAULT_BUTTON_MARGIN
35//
36#define FOCUS_MARGIN 3
37
38#ifndef BST_CHECKED
39#define BST_CHECKED 0x0001
40#endif
41
0e320a79
DW
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
65f3f920 81 ,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 {
f1e01716 92 return false;
987da0d4 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{
6c20e8f8 118 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
987da0d4 119
6c20e8f8 120 if (tlw)
d88de032 121 {
6c20e8f8 122 if (tlw->GetDefaultItem() == this)
d88de032 123 {
987da0d4
DW
124 //
125 // Don't leave the panel with invalid default item
126 //
6c20e8f8 127 tlw->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 231
b7276b44 232wxWindow *wxButton::SetDefault()
0e320a79 233{
430974f8 234 //
77ffb593 235 // Set this one as the default button both for wxWidgets and Windows
430974f8 236 //
b7276b44 237 wxWindow* pWinOldDefault = wxButtonBase::SetDefault();
cfcebdb1 238
6670f564
WS
239 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), false);
240 SetDefaultStyle( this, true );
94aff5ff
VZ
241
242 return pWinOldDefault;
430974f8
DW
243} // end of wxButton::SetDefault
244
245void wxButton::SetTmpDefault()
246{
6c20e8f8 247 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
430974f8 248
9a83f860 249 wxCHECK_RET( tlw, wxT("button without top level window?") );
430974f8 250
6c20e8f8 251 wxWindow* pWinOldDefault = tlw->GetDefaultItem();
cfcebdb1 252
6c20e8f8 253 tlw->SetTmpDefaultItem(this);
6670f564
WS
254 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), false);
255 SetDefaultStyle( this, true );
430974f8
DW
256} // end of wxButton::SetTmpDefault
257
258void wxButton::UnsetTmpDefault()
259{
6c20e8f8 260 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
430974f8 261
9a83f860 262 wxCHECK_RET( tlw, wxT("button without top level window?") );
987da0d4 263
6c20e8f8 264 tlw->SetTmpDefaultItem(NULL);
430974f8 265
6c20e8f8 266 wxWindow* pWinOldDefault = tlw->GetDefaultItem();
430974f8 267
6670f564
WS
268 SetDefaultStyle( this, false );
269 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), true );
430974f8
DW
270} // end of wxButton::UnsetTmpDefault
271
cfcebdb1
DW
272void wxButton::SetDefaultStyle(
273 wxButton* pBtn
274, bool bOn
275)
430974f8 276{
430974f8 277 long lStyle;
cfcebdb1
DW
278 //
279 // We may be called with NULL pointer -- simpler to do the check here than
280 // in the caller which does wxDynamicCast()
281 //
282 if (!pBtn)
283 return;
430974f8 284
cfcebdb1
DW
285 //
286 // First, let DefDlgProc() know about the new default button
287 //
288 if (bOn)
289 {
290 if (!wxTheApp->IsActive())
291 return;
292
293 //
294 // In OS/2 the dialog/panel doesn't really know it has a default
295 // button, the default button simply has that style. We'll just
296 // simulate by setting focus to it
297 //
298 pBtn->SetFocus();
299 }
300 lStyle = ::WinQueryWindowULong(GetHwndOf(pBtn), QWL_STYLE);
301 if (!(lStyle & BS_DEFAULT) == bOn)
987da0d4 302 {
987da0d4
DW
303 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
304 {
cfcebdb1 305 if (bOn)
d00110f6 306 lStyle |= BS_DEFAULT;
cfcebdb1
DW
307 else
308 lStyle &= ~BS_DEFAULT;
309 ::WinSetWindowULong(GetHwndOf(pBtn), QWL_STYLE, lStyle);
987da0d4
DW
310 }
311 else
312 {
cfcebdb1
DW
313 //
314 // Redraw the button - it will notice itself that it's not the
987da0d4 315 // default one any longer
cfcebdb1
DW
316 //
317 pBtn->Refresh();
430974f8 318 }
987da0d4 319 }
430974f8 320} // end of wxButton::UpdateDefaultStyle
0e320a79 321
d88de032
DW
322// ----------------------------------------------------------------------------
323// event/message handlers
324// ----------------------------------------------------------------------------
0e320a79 325
6670f564 326bool wxButton::OS2Command(WXUINT uParam, WXWORD WXUNUSED(wId))
0e320a79 327{
6670f564 328 bool bProcessed = false;
987da0d4
DW
329
330 switch (uParam)
d88de032 331 {
987da0d4
DW
332 case BN_CLICKED: // normal buttons send this
333 case BN_DBLCLICKED: // owner-drawn ones also send this
334 bProcessed = SendClickEvent();
d88de032
DW
335 break;
336 }
6670f564 337
987da0d4
DW
338 return bProcessed;
339} // end of wxButton::OS2Command
340
6670f564
WS
341WXHBRUSH wxButton::OnCtlColor( WXHDC WXUNUSED(pDC),
342 WXHWND WXUNUSED(pWnd),
343 WXUINT WXUNUSED(nCtlColor),
344 WXUINT WXUNUSED(uMessage),
345 WXWPARAM WXUNUSED(wParam),
346 WXLPARAM WXUNUSED(lParam) )
987da0d4 347{
6670f564
WS
348 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
349 ,wxSOLID
350 );
987da0d4
DW
351
352 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
353} // end of wxButton::OnCtlColor
354
355void wxButton::MakeOwnerDrawn()
0e320a79 356{
987da0d4 357 long lStyle = 0L;
d88de032 358
987da0d4
DW
359 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
360 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
361 {
362 //
363 // Make it so
364 //
365 lStyle |= BS_USERBUTTON;
366 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
367 }
bc5a847c
DW
368} // end of wxButton::MakeOwnerDrawn
369
370WXDWORD wxButton::OS2GetStyle(
371 long lStyle
372, WXDWORD* pdwExstyle
373) const
374{
375 //
376 // Buttons never have an external border, they draw their own one
377 //
378 WXDWORD dwStyle = wxControl::OS2GetStyle( (lStyle & ~wxBORDER_MASK) | wxBORDER_NONE
379 ,pdwExstyle
380 );
381
382 //
383 // We must use WS_CLIPSIBLINGS with the buttons or they would draw over
d13b34d3 384 // each other in any resizable dialog which has more than one button in
bc5a847c
DW
385 // the bottom
386 //
387 dwStyle |= WS_CLIPSIBLINGS;
388 return dwStyle;
389} // end of wxButton::OS2GetStyle
987da0d4 390
6670f564
WS
391MRESULT wxButton::WindowProc( WXUINT uMsg,
392 WXWPARAM wParam,
393 WXLPARAM lParam )
987da0d4
DW
394{
395 //
430974f8
DW
396 // When we receive focus, we want to temporary become the default button in
397 // our parent panel so that pressing "Enter" would activate us -- and when
398 // losing it we should restore the previous default button as well
987da0d4
DW
399 //
400 if (uMsg == WM_SETFOCUS)
401 {
430974f8
DW
402 if (SHORT1FROMMP(lParam) == TRUE)
403 SetTmpDefault();
404 else
405 UnsetTmpDefault();
987da0d4
DW
406
407 //
408 // Let the default processign take place too
409 //
410 }
411
412 else if (uMsg == WM_BUTTON1DBLCLK)
413 {
414 //
415 // Emulate a click event to force an owner-drawn button to change its
416 // appearance - without this, it won't do it
417 //
418 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
419 ,wParam
420 ,lParam
421 );
422
423 //
424 // And conitnue with processing the message normally as well
425 //
426 }
0e320a79 427
987da0d4
DW
428 //
429 // Let the base class do all real processing
430 //
431 return (wxControl::OS2WindowProc( uMsg
432 ,wParam
433 ,lParam
434 ));
bc5a847c 435} // end of wxWindowProc