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