]> git.saurik.com Git - wxWidgets.git/blame - src/os2/button.cpp
implemented URLs blocking in wxHtmlWindow
[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"
0e320a79
DW
22#endif
23
d88de032 24#include "wx/os2/private.h"
0e320a79 25
987da0d4
DW
26#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
27
28//
29// Should be at the very least less than winDEFAULT_BUTTON_MARGIN
30//
31#define FOCUS_MARGIN 3
32
33#ifndef BST_CHECKED
34#define BST_CHECKED 0x0001
35#endif
36
0e320a79 37IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
0e320a79
DW
38
39// Button
40
987da0d4
DW
41bool wxButton::Create(
42 wxWindow* pParent
43, wxWindowID vId
44, const wxString& rsLabel
45, const wxPoint& rPos
46, const wxSize& rSize
47, long lStyle
5d4b632b 48#if wxUSE_VALIDATORS
987da0d4 49, const wxValidator& rValidator
5d4b632b 50#endif
987da0d4
DW
51, const wxString& rsName
52)
0e320a79 53{
987da0d4 54 SetName(rsName);
5d4b632b 55#if wxUSE_VALIDATORS
987da0d4 56 SetValidator(rValidator);
5d4b632b 57#endif
987da0d4
DW
58 m_windowStyle = lStyle;
59 pParent->AddChild((wxButton *)this);
60 if (vId == -1)
0e320a79
DW
61 m_windowId = NewControlId();
62 else
987da0d4
DW
63 m_windowId = vId;
64 lStyle = WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON;
65
66 //
67 // OS/2 PM does not have Right/Left/Top/Bottom styles.
68 // We will have to define an additional style when we implement notebooks
69 // for a notebook page button
70 //
71 if (m_windowStyle & wxCLIP_SIBLINGS )
72 lStyle |= WS_CLIPSIBLINGS;
5d44b24e
DW
73 //
74 // If the parent is a scrolled window the controls must
75 // have this style or they will overlap the scrollbars
76 //
77 if (pParent)
78 if (pParent->IsKindOf(CLASSINFO(wxScrolledWindow)) ||
79 pParent->IsKindOf(CLASSINFO(wxGenericScrolledWindow)))
80 lStyle |= WS_CLIPSIBLINGS;
81
987da0d4
DW
82 m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle
83 ,WC_BUTTON // A Button class window
84 ,(PSZ)rsLabel.c_str() // Button text
85 ,lStyle // Button style
86 ,0, 0, 0, 0 // Location and size
87 ,GetHwndOf(pParent) // Owner handle
88 ,HWND_TOP // Top of Z-Order
89 ,vId // Identifier
90 ,NULL // No control data
91 ,NULL // No Presentation parameters
92 );
93 if (m_hWnd == 0)
94 {
95 return FALSE;
96 }
0e320a79 97
987da0d4
DW
98 //
99 // Subclass again for purposes of dialog editing mode
100 //
101 SubclassWin(m_hWnd);
102 SetFont(pParent->GetFont());
103 SetSize( rPos.x
104 ,rPos.y
105 ,rSize.x
106 ,rSize.y
107 );
108 return TRUE;
109} // end of wxButton::Create
0e320a79 110
d88de032 111wxButton::~wxButton()
0e320a79 112{
987da0d4
DW
113 wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel);
114
115 if (pPanel)
d88de032 116 {
987da0d4 117 if (pPanel->GetDefaultItem() == this)
d88de032 118 {
987da0d4
DW
119 //
120 // Don't leave the panel with invalid default item
121 //
122 pPanel->SetDefaultItem(NULL);
d88de032
DW
123 }
124 }
987da0d4 125} // end of wxButton::~wxButton
d88de032
DW
126
127// ----------------------------------------------------------------------------
128// size management including autosizing
129// ----------------------------------------------------------------------------
130
e78c4d50 131wxSize wxButton::DoGetBestSize() const
d88de032 132{
987da0d4
DW
133 wxString rsLabel = wxGetWindowText(GetHWND());
134 int nWidthButton;
135 int nWidthChar;
136 int nHeightChar;
137
138 GetTextExtent( rsLabel
139 ,&nWidthButton
140 ,NULL
141 );
142
143 wxGetCharSize( GetHWND()
144 ,&nWidthChar
145 ,&nHeightChar
146 ,(wxFont*)&GetFont()
147 );
148
149 //
150 // Add a margin - the button is wider than just its label
151 //
152 nWidthButton += 3 * nWidthChar;
153
154 //
155 // The button height is proportional to the height of the font used
156 //
157 int nHeightButton = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(nHeightChar);
158
159 //
160 // Need a little extra to make it look right
161 //
162 nHeightButton += nHeightChar/1.5;
163
164 wxSize vSize = GetDefaultSize();
165
166 if (nWidthButton > vSize.x)
167 vSize.x = nWidthButton;
168 if (nHeightButton > vSize.y)
169 vSize.y = nHeightButton;
170 return vSize;
171} // end of wxButton::DoGetBestSize
d88de032
DW
172
173/* static */
174wxSize wxButton::GetDefaultSize()
175{
987da0d4 176 static wxSize vSizeBtn;
d88de032 177
987da0d4 178 if (vSizeBtn.x == 0)
d88de032 179 {
987da0d4 180 wxScreenDC vDc;
d88de032 181
a756f210 182 vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
987da0d4
DW
183
184 //
185 // The size of a standard button in the dialog units is 50x14,
d88de032
DW
186 // translate this to pixels
187 // NB1: the multipliers come from the Windows convention
188 // NB2: the extra +1/+2 were needed to get the size be the same as the
189 // size of the buttons in the standard dialog - I don't know how
190 // this happens, but on my system this size is 75x23 in pixels and
191 // 23*8 isn't even divisible by 14... Would be nice to understand
192 // why these constants are needed though!
987da0d4
DW
193 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
194 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
d88de032 195 }
987da0d4
DW
196 return vSizeBtn;
197} // end of wxButton::GetDefaultSize
d88de032 198
987da0d4
DW
199void wxButton::Command (
200 wxCommandEvent& rEvent
201)
d88de032 202{
987da0d4
DW
203 ProcessCommand (rEvent);
204} // end of wxButton::Command
d88de032
DW
205
206// ----------------------------------------------------------------------------
207// helpers
208// ----------------------------------------------------------------------------
209
210bool wxButton::SendClickEvent()
211{
987da0d4
DW
212 wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED
213 ,GetId()
214 );
d88de032 215
987da0d4
DW
216 vEvent.SetEventObject(this);
217 return ProcessCommand(vEvent);
218} // end of wxButton::SendClickEvent
0e320a79
DW
219
220void wxButton::SetDefault()
221{
987da0d4
DW
222 wxWindow* pParent = GetParent();
223 wxButton* pBtnOldDefault = NULL;
224 wxPanel* pPanel = wxDynamicCast(pParent, wxPanel);
225 long lStyle = 0L;
226
227 if (pParent)
228 {
229 wxWindow* pWinOldDefault = pParent->SetDefaultItem(this);
230
231 pBtnOldDefault = wxDynamicCast(pWinOldDefault, wxButton);
232 }
233 if (pBtnOldDefault && pBtnOldDefault != this)
234 {
235 //
236 // Remove the BS_DEFPUSHBUTTON style from the other button
237 //
238 lStyle = ::WinQueryWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE);
239
240 //
241 // Don't do it with the owner drawn buttons because it will reset
242 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
243 //
244 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
245 {
246 lStyle &= ~BS_DEFAULT;
247 ::WinSetWindowULong(GetHwndOf(pBtnOldDefault), QWL_STYLE, lStyle);
248 }
249 else
250 {
251 //
252 // Redraw the button - it will notice itself that it's not the
253 // default one any longer
254 //
255 pBtnOldDefault->Refresh();
256 }
257 }
0e320a79 258
987da0d4
DW
259 //
260 // Set this button as the default
261 //
262 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
263 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
264 {
265 lStyle != BS_DEFAULT;
266 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
267 }
268} // end of wxButton::SetDefault
0e320a79 269
d88de032
DW
270// ----------------------------------------------------------------------------
271// event/message handlers
272// ----------------------------------------------------------------------------
0e320a79 273
987da0d4
DW
274bool wxButton::OS2Command(
275 WXUINT uParam
276, WXWORD wId
277)
0e320a79 278{
987da0d4
DW
279 bool bProcessed = FALSE;
280
281 switch (uParam)
d88de032 282 {
987da0d4
DW
283 case BN_CLICKED: // normal buttons send this
284 case BN_DBLCLICKED: // owner-drawn ones also send this
285 bProcessed = SendClickEvent();
d88de032
DW
286 break;
287 }
987da0d4
DW
288 return bProcessed;
289} // end of wxButton::OS2Command
290
291WXHBRUSH wxButton::OnCtlColor(
292 WXHDC pDC
293, WXHWND pWnd
294, WXUINT nCtlColor
295, WXUINT uMessage
296, WXWPARAM wParam
297, WXLPARAM lParam
298)
299{
300 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
301 ,wxSOLID
302 );
303
304 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
305} // end of wxButton::OnCtlColor
306
307void wxButton::MakeOwnerDrawn()
0e320a79 308{
987da0d4 309 long lStyle = 0L;
d88de032 310
987da0d4
DW
311 lStyle = ::WinQueryWindowULong(GetHwnd(), QWL_STYLE);
312 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
313 {
314 //
315 // Make it so
316 //
317 lStyle |= BS_USERBUTTON;
318 ::WinSetWindowULong(GetHwnd(), QWL_STYLE, lStyle);
319 }
320} // end of wxCButton::MakeOwnerDrawn
321
322MRESULT wxButton::WindowProc(
323 WXUINT uMsg
324, WXWPARAM wParam
325, WXLPARAM lParam
326)
327{
328 //
329 // When we receive focus, we want to become the default button in our
330 // parent panel
331 //
332 if (uMsg == WM_SETFOCUS)
333 {
334 SetDefault();
335
336 //
337 // Let the default processign take place too
338 //
339 }
340
341 else if (uMsg == WM_BUTTON1DBLCLK)
342 {
343 //
344 // Emulate a click event to force an owner-drawn button to change its
345 // appearance - without this, it won't do it
346 //
347 (void)wxControl::OS2WindowProc( WM_BUTTON1DOWN
348 ,wParam
349 ,lParam
350 );
351
352 //
353 // And conitnue with processing the message normally as well
354 //
355 }
0e320a79 356
987da0d4
DW
357 //
358 // Let the base class do all real processing
359 //
360 return (wxControl::OS2WindowProc( uMsg
361 ,wParam
362 ,lParam
363 ));
364} // end of wxW indowProc
d88de032 365