Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / src / os2 / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/os2/button.cpp
3 // Purpose: wxButton
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // Copyright: (c) David Webster
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/button.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/app.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"
23 #include "wx/scrolwin.h"
24 #include "wx/toplevel.h"
25 #endif
26
27 #include "wx/stockitem.h"
28 #include "wx/os2/private.h"
29
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
41 // Button
42
43 bool 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)
51 {
52 wxString rsLabel(rsLbl);
53 if (rsLabel.empty() && wxIsStockID(vId))
54 rsLabel = wxGetStockLabel(vId);
55
56 wxString sLabel = ::wxPMTextToLabel(rsLabel);
57
58 SetName(rsName);
59 #if wxUSE_VALIDATORS
60 SetValidator(rValidator);
61 #endif
62 m_windowStyle = lStyle;
63 pParent->AddChild((wxButton *)this);
64 if (vId == -1)
65 m_windowId = NewControlId();
66 else
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;
77
78 m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent) // Parent handle
79 ,WC_BUTTON // A Button class window
80 ,sLabel.c_str() // Button text
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 {
91 return false;
92 }
93
94 //
95 // Subclass again for purposes of dialog editing mode
96 //
97 SubclassWin(m_hWnd);
98 wxFont* pButtonFont = new wxFont( 8
99 ,wxSWISS
100 ,wxNORMAL
101 ,wxNORMAL
102 );
103 SetFont(*pButtonFont);
104 SetXComp(0);
105 SetYComp(0);
106 SetSize( rPos.x
107 ,rPos.y
108 ,rSize.x
109 ,rSize.y
110 );
111 delete pButtonFont;
112 return true;
113 } // end of wxButton::Create
114
115 wxButton::~wxButton()
116 {
117 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
118
119 if (tlw)
120 {
121 if (tlw->GetDefaultItem() == this)
122 {
123 //
124 // Don't leave the panel with invalid default item
125 //
126 tlw->SetDefaultItem(NULL);
127 }
128 }
129 } // end of wxButton::~wxButton
130
131 // ----------------------------------------------------------------------------
132 // size management including autosizing
133 // ----------------------------------------------------------------------------
134
135 wxSize wxButton::DoGetBestSize() const
136 {
137 wxString rsLabel = wxGetWindowText(GetHWND());
138 int nWidthButton;
139 int nWidthChar;
140 int nHeightChar;
141 wxFont vFont = (wxFont)GetFont();
142
143 GetTextExtent( rsLabel
144 ,&nWidthButton
145 ,NULL
146 );
147
148 wxGetCharSize( GetHWND()
149 ,&nWidthChar
150 ,&nHeightChar
151 ,&vFont
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 //
167 nHeightButton += (int)(nHeightChar/1.5);
168
169 if (!HasFlag(wxBU_EXACTFIT))
170 {
171 wxSize vSize = GetDefaultSize();
172
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 );
182 } // end of wxButton::DoGetBestSize
183
184 /* static */
185 wxSize wxButton::GetDefaultSize()
186 {
187 static wxSize vSizeBtn;
188
189 if (vSizeBtn.x == 0)
190 {
191 wxScreenDC vDc;
192
193 vDc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
194
195 //
196 // The size of a standard button in the dialog units is 50x14,
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!
204 vSizeBtn.x = (50 * (vDc.GetCharWidth() + 1))/4;
205 vSizeBtn.y = ((14 * vDc.GetCharHeight()) + 2)/8;
206 }
207 return vSizeBtn;
208 } // end of wxButton::GetDefaultSize
209
210 void wxButton::Command (
211 wxCommandEvent& rEvent
212 )
213 {
214 ProcessCommand (rEvent);
215 } // end of wxButton::Command
216
217 // ----------------------------------------------------------------------------
218 // helpers
219 // ----------------------------------------------------------------------------
220
221 bool wxButton::SendClickEvent()
222 {
223 wxCommandEvent vEvent( wxEVT_BUTTON
224 ,GetId()
225 );
226
227 vEvent.SetEventObject(this);
228 return ProcessCommand(vEvent);
229 } // end of wxButton::SendClickEvent
230
231 wxWindow *wxButton::SetDefault()
232 {
233 //
234 // Set this one as the default button both for wxWidgets and Windows
235 //
236 wxWindow* pWinOldDefault = wxButtonBase::SetDefault();
237
238 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), false);
239 SetDefaultStyle( this, true );
240
241 return pWinOldDefault;
242 } // end of wxButton::SetDefault
243
244 void wxButton::SetTmpDefault()
245 {
246 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
247
248 wxCHECK_RET( tlw, wxT("button without top level window?") );
249
250 wxWindow* pWinOldDefault = tlw->GetDefaultItem();
251
252 tlw->SetTmpDefaultItem(this);
253 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), false);
254 SetDefaultStyle( this, true );
255 } // end of wxButton::SetTmpDefault
256
257 void wxButton::UnsetTmpDefault()
258 {
259 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow);
260
261 wxCHECK_RET( tlw, wxT("button without top level window?") );
262
263 tlw->SetTmpDefaultItem(NULL);
264
265 wxWindow* pWinOldDefault = tlw->GetDefaultItem();
266
267 SetDefaultStyle( this, false );
268 SetDefaultStyle( wxDynamicCast(pWinOldDefault, wxButton), true );
269 } // end of wxButton::UnsetTmpDefault
270
271 void wxButton::SetDefaultStyle(
272 wxButton* pBtn
273 , bool bOn
274 )
275 {
276 long lStyle;
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;
283
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)
301 {
302 if ((lStyle & BS_USERBUTTON) != BS_USERBUTTON)
303 {
304 if (bOn)
305 lStyle |= BS_DEFAULT;
306 else
307 lStyle &= ~BS_DEFAULT;
308 ::WinSetWindowULong(GetHwndOf(pBtn), QWL_STYLE, lStyle);
309 }
310 else
311 {
312 //
313 // Redraw the button - it will notice itself that it's not the
314 // default one any longer
315 //
316 pBtn->Refresh();
317 }
318 }
319 } // end of wxButton::UpdateDefaultStyle
320
321 // ----------------------------------------------------------------------------
322 // event/message handlers
323 // ----------------------------------------------------------------------------
324
325 bool wxButton::OS2Command(WXUINT uParam, WXWORD WXUNUSED(wId))
326 {
327 bool bProcessed = false;
328
329 switch (uParam)
330 {
331 case BN_CLICKED: // normal buttons send this
332 case BN_DBLCLICKED: // owner-drawn ones also send this
333 bProcessed = SendClickEvent();
334 break;
335 }
336
337 return bProcessed;
338 } // end of wxButton::OS2Command
339
340 WXHBRUSH 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) )
346 {
347 wxBrush* pBackgroundBrush = wxTheBrushList->FindOrCreateBrush( GetBackgroundColour()
348 ,wxSOLID
349 );
350
351 return (WXHBRUSH)pBackgroundBrush->GetResourceHandle();
352 } // end of wxButton::OnCtlColor
353
354 void wxButton::MakeOwnerDrawn()
355 {
356 long lStyle = 0L;
357
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 }
367 } // end of wxButton::MakeOwnerDrawn
368
369 WXDWORD 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
383 // each other in any resizable dialog which has more than one button in
384 // the bottom
385 //
386 dwStyle |= WS_CLIPSIBLINGS;
387 return dwStyle;
388 } // end of wxButton::OS2GetStyle
389
390 MRESULT wxButton::WindowProc( WXUINT uMsg,
391 WXWPARAM wParam,
392 WXLPARAM lParam )
393 {
394 //
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
398 //
399 if (uMsg == WM_SETFOCUS)
400 {
401 if (SHORT1FROMMP(lParam) == TRUE)
402 SetTmpDefault();
403 else
404 UnsetTmpDefault();
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 }
426
427 //
428 // Let the base class do all real processing
429 //
430 return (wxControl::OS2WindowProc( uMsg
431 ,wParam
432 ,lParam
433 ));
434 } // end of wxWindowProc