]> git.saurik.com Git - wxWidgets.git/blob - src/os2/button.cpp
use UNIX wxDir if available
[wxWidgets.git] / src / os2 / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: button.cpp
3 // Purpose: wxButton
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
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"
22 #endif
23
24 #include "wx/os2/private.h"
25
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
37 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
38
39 // Button
40
41 bool wxButton::Create(
42 wxWindow* pParent
43 , wxWindowID vId
44 , const wxString& rsLabel
45 , const wxPoint& rPos
46 , const wxSize& rSize
47 , long lStyle
48 #if wxUSE_VALIDATORS
49 , const wxValidator& rValidator
50 #endif
51 , const wxString& rsName
52 )
53 {
54 SetName(rsName);
55 #if wxUSE_VALIDATORS
56 SetValidator(rValidator);
57 #endif
58 m_windowStyle = lStyle;
59 pParent->AddChild((wxButton *)this);
60 if (vId == -1)
61 m_windowId = NewControlId();
62 else
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;
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
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 }
97
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
110
111 wxButton::~wxButton()
112 {
113 wxPanel* pPanel = wxDynamicCast(GetParent(), wxPanel);
114
115 if (pPanel)
116 {
117 if (pPanel->GetDefaultItem() == this)
118 {
119 //
120 // Don't leave the panel with invalid default item
121 //
122 pPanel->SetDefaultItem(NULL);
123 }
124 }
125 } // end of wxButton::~wxButton
126
127 // ----------------------------------------------------------------------------
128 // size management including autosizing
129 // ----------------------------------------------------------------------------
130
131 wxSize wxButton::DoGetBestSize() const
132 {
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
172
173 /* static */
174 wxSize wxButton::GetDefaultSize()
175 {
176 static wxSize vSizeBtn;
177
178 if (vSizeBtn.x == 0)
179 {
180 wxScreenDC vDc;
181
182 vDc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
183
184 //
185 // The size of a standard button in the dialog units is 50x14,
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!
193 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
194 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
195 }
196 return vSizeBtn;
197 } // end of wxButton::GetDefaultSize
198
199 void wxButton::Command (
200 wxCommandEvent& rEvent
201 )
202 {
203 ProcessCommand (rEvent);
204 } // end of wxButton::Command
205
206 // ----------------------------------------------------------------------------
207 // helpers
208 // ----------------------------------------------------------------------------
209
210 bool wxButton::SendClickEvent()
211 {
212 wxCommandEvent vEvent( wxEVT_COMMAND_BUTTON_CLICKED
213 ,GetId()
214 );
215
216 vEvent.SetEventObject(this);
217 return ProcessCommand(vEvent);
218 } // end of wxButton::SendClickEvent
219
220 void wxButton::SetDefault()
221 {
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 }
258
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
269
270 // ----------------------------------------------------------------------------
271 // event/message handlers
272 // ----------------------------------------------------------------------------
273
274 bool wxButton::OS2Command(
275 WXUINT uParam
276 , WXWORD wId
277 )
278 {
279 bool bProcessed = FALSE;
280
281 switch (uParam)
282 {
283 case BN_CLICKED: // normal buttons send this
284 case BN_DBLCLICKED: // owner-drawn ones also send this
285 bProcessed = SendClickEvent();
286 break;
287 }
288 return bProcessed;
289 } // end of wxButton::OS2Command
290
291 WXHBRUSH 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
307 void wxButton::MakeOwnerDrawn()
308 {
309 long lStyle = 0L;
310
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
322 MRESULT 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 }
356
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
365