]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
1. wxLongLong and wxDateTime compilation fixed for the compilers without native
[wxWidgets.git] / src / msw / button.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/button.cpp
3 // Purpose: wxButton
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "button.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/button.h"
33 #include "wx/brush.h"
34 #include "wx/panel.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
38 #endif
39
40 #include "wx/msw/private.h"
41
42 // ----------------------------------------------------------------------------
43 // macros
44 // ----------------------------------------------------------------------------
45
46 IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
47
48 // this macro tries to adjust the default button height to a reasonable value
49 // using the char height as the base
50 #define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
51
52 // ============================================================================
53 // implementation
54 // ============================================================================
55
56 // ----------------------------------------------------------------------------
57 // creation/destruction
58 // ----------------------------------------------------------------------------
59
60 bool wxButton::Create(wxWindow *parent,
61 wxWindowID id,
62 const wxString& label,
63 const wxPoint& pos,
64 const wxSize& size,
65 long style,
66 const wxValidator& validator,
67 const wxString& name)
68 {
69 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
70 return FALSE;
71
72 parent->AddChild((wxButton *)this);
73
74 m_backgroundColour = parent->GetBackgroundColour();
75 m_foregroundColour = parent->GetForegroundColour();
76
77
78 m_hWnd = (WXHWND)CreateWindowEx
79 (
80 MakeExtendedStyle(m_windowStyle),
81 wxT("BUTTON"),
82 label,
83 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
84 0, 0, 0, 0,
85 GetWinHwnd(parent),
86 (HMENU)m_windowId,
87 wxGetInstance(),
88 NULL
89 );
90
91 // Subclass again for purposes of dialog editing mode
92 SubclassWin(m_hWnd);
93
94 SetFont(parent->GetFont());
95
96 SetSize(pos.x, pos.y, size.x, size.y);
97
98 // bad hack added by Robert to make buttons at least
99 // 80 pixels wide. There are probably better ways...
100 // TODO. FIXME.
101 wxSize nsize( GetSize() );
102 if ((nsize.x < 80) || (nsize.y < 23))
103 {
104 if ((size.x == -1) && (nsize.x < 80))
105 nsize.x = 80;
106 if ((size.y == -1) && (nsize.y < 23))
107 nsize.y = 23;
108 SetSize( nsize );
109 }
110
111 return TRUE;
112 }
113
114 wxButton::~wxButton()
115 {
116 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
117 if ( panel )
118 {
119 if ( panel->GetDefaultItem() == this )
120 {
121 // don't leave the panel with invalid default item
122 panel->SetDefaultItem(NULL);
123 }
124 }
125 }
126
127 // ----------------------------------------------------------------------------
128 // size management including autosizing
129 // ----------------------------------------------------------------------------
130
131 wxSize wxButton::DoGetBestSize() const
132 {
133 wxString label = wxGetWindowText(GetHWND());
134 int wBtn;
135 GetTextExtent(label, &wBtn, NULL);
136
137 int wChar, hChar;
138 wxGetCharSize(GetHWND(), &wChar, &hChar, &GetFont());
139
140 // add a margin - the button is wider than just its label
141 wBtn += 3*wChar;
142
143 // the button height is proportional to the height of the font used
144 int hBtn = BUTTON_HEIGHT_FROM_CHAR_HEIGHT(hChar);
145
146 return wxSize(wBtn, hBtn);
147 }
148
149 /* static */
150 wxSize wxButton::GetDefaultSize()
151 {
152 static wxSize s_sizeBtn;
153
154 if ( s_sizeBtn.x == 0 )
155 {
156 wxScreenDC dc;
157 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
158
159 // the size of a standard button in the dialog units is 50x14,
160 // translate this to pixels
161 // NB1: the multipliers come from the Windows convention
162 // NB2: the extra +1/+2 were needed to get the size be the same as the
163 // size of the buttons in the standard dialog - I don't know how
164 // this happens, but on my system this size is 75x23 in pixels and
165 // 23*8 isn't even divisible by 14... Would be nice to understand
166 // why these constants are needed though!
167 s_sizeBtn.x = (50 * (dc.GetCharWidth() + 1))/4;
168 s_sizeBtn.y = ((14 * dc.GetCharHeight()) + 2)/8;
169 }
170
171 return s_sizeBtn;
172 }
173
174 // ----------------------------------------------------------------------------
175 // set this button as the default one in its panel
176 // ----------------------------------------------------------------------------
177
178 void wxButton::SetDefault()
179 {
180 wxWindow *parent = GetParent();
181 wxButton *btnOldDefault = NULL;
182 wxPanel *panel = wxDynamicCast(parent, wxPanel);
183 if ( panel )
184 {
185 btnOldDefault = panel->GetDefaultItem();
186 panel->SetDefaultItem(this);
187 }
188
189 if ( parent )
190 {
191 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
192 }
193
194 if ( btnOldDefault )
195 {
196 // remove the BS_DEFPUSHBUTTON style from the other button
197 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
198
199 // don't do it with the owner drawn buttons because it will reset
200 // BS_OWNERDRAW style bit too (BS_OWNERDRAW & BS_DEFPUSHBUTTON != 0)!
201 if ( !(style & BS_OWNERDRAW) )
202 {
203 style &= ~BS_DEFPUSHBUTTON;
204 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
205 }
206 }
207
208 // set this button as the default
209 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
210 style |= BS_DEFPUSHBUTTON;
211 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
212 }
213
214 // ----------------------------------------------------------------------------
215 // helpers
216 // ----------------------------------------------------------------------------
217
218 bool wxButton::SendClickEvent()
219 {
220 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
221 event.SetEventObject(this);
222
223 return ProcessCommand(event);
224 }
225
226 void wxButton::Command(wxCommandEvent & event)
227 {
228 ProcessCommand(event);
229 }
230
231 // ----------------------------------------------------------------------------
232 // event/message handlers
233 // ----------------------------------------------------------------------------
234
235 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
236 {
237 bool processed = FALSE;
238 switch ( param )
239 {
240 case 1: // means that the message came from an accelerator
241 case BN_CLICKED:
242 processed = SendClickEvent();
243 break;
244 }
245
246 return processed;
247 }
248
249 long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
250 {
251 // make sure that we won't have BS_DEFPUSHBUTTON style any more if the
252 // focus is being transfered to another button with the same parent -
253 // otherwise, we could finish with 2 default buttons inside one panel
254 if ( (nMsg == WM_KILLFOCUS) &&
255 (GetWindowLong(GetHwnd(), GWL_STYLE) & BS_DEFPUSHBUTTON) )
256 {
257 wxWindow *parent = GetParent();
258 wxWindow *win = wxFindWinFromHandle((WXHWND)wParam);
259 if ( win && win->GetParent() == parent )
260 {
261 wxPanel *panel = wxDynamicCast(parent, wxPanel);
262 if ( panel )
263 {
264 panel->SetDefaultItem(this);
265 }
266 // else: I don't know what to do - we'll still have the problem
267 // with multiple default buttons in a dialog...
268 }
269 }
270
271 // let the base class do all real processing
272 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
273 }
274
275 // ----------------------------------------------------------------------------
276 // owner-drawn buttons support
277 // ----------------------------------------------------------------------------
278
279 #ifdef __WIN32__
280
281 // drawing helpers
282
283 static void DrawButtonText(HDC hdc,
284 RECT *pRect,
285 const wxString& text,
286 COLORREF col)
287 {
288 COLORREF colOld = SetTextColor(hdc, col);
289 int modeOld = SetBkMode(hdc, TRANSPARENT);
290
291 DrawText(hdc, text, text.length(), pRect,
292 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
293
294 SetBkMode(hdc, modeOld);
295 SetTextColor(hdc, colOld);
296 }
297
298 static void DrawRect(HDC hdc, const RECT& r)
299 {
300 MoveToEx(hdc, r.left, r.top, NULL);
301 LineTo(hdc, r.right, r.top);
302 LineTo(hdc, r.right, r.bottom);
303 LineTo(hdc, r.left, r.bottom);
304 LineTo(hdc, r.left, r.top);
305 }
306
307 void wxButton::MakeOwnerDrawn()
308 {
309 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
310 if ( !(style & BS_OWNERDRAW) )
311 {
312 // make it so
313 style |= BS_OWNERDRAW;
314 SetWindowLong(GetHwnd(), GWL_STYLE, style);
315 }
316 }
317
318 bool wxButton::SetBackgroundColour(const wxColour &colour)
319 {
320 if ( !wxControl::SetBackgroundColour(colour) )
321 {
322 // nothing to do
323 return FALSE;
324 }
325
326 MakeOwnerDrawn();
327
328 Refresh();
329
330 return TRUE;
331 }
332
333 bool wxButton::SetForegroundColour(const wxColour &colour)
334 {
335 if ( !wxControl::SetForegroundColour(colour) )
336 {
337 // nothing to do
338 return FALSE;
339 }
340
341 MakeOwnerDrawn();
342
343 Refresh();
344
345 return TRUE;
346 }
347
348 /*
349 The button frame looks like this normally:
350
351 WWWWWWWWWWWWWWWWWWB
352 W GB
353 W GB
354 W GB where W, G, B are white, grey and black pixels
355 W GB
356 WGGGGGGGGGGGGGGGGGB
357 BBBBBBBBBBBBBBBBBBB
358
359 When the button is selected, the button becomes like this (the total button
360 size doesn't change):
361
362 BBBBBBBBBBBBBBBBBBB
363 BWWWWWWWWWWWWWWWWBB
364 BW GBB
365 BW GBB
366 BWGGGGGGGGGGGGGGGBB
367 BBBBBBBBBBBBBBBBBBB
368 BBBBBBBBBBBBBBBBBBB
369
370 When the button is pushed (while selected) it is like:
371
372 BBBBBBBBBBBBBBBBBBB
373 BGGGGGGGGGGGGGGGGGB
374 BG GB
375 BG GB
376 BG GB
377 BGGGGGGGGGGGGGGGGGB
378 BBBBBBBBBBBBBBBBBBB
379 */
380
381 static void DrawButtonFrame(HDC hdc, const RECT& rectBtn,
382 bool selected, bool pushed)
383 {
384 RECT r;
385 CopyRect(&r, &rectBtn);
386
387 HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)),
388 hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)),
389 hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
390
391 HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack);
392
393 r.right--;
394 r.bottom--;
395
396 if ( pushed )
397 {
398 DrawRect(hdc, r);
399
400 (void)SelectObject(hdc, hpenGrey);
401 InflateRect(&r, -1, -1);
402
403 DrawRect(hdc, r);
404 }
405 else // !pushed
406 {
407 if ( selected )
408 {
409 DrawRect(hdc, r);
410
411 InflateRect(&r, -1, -1);
412 }
413
414 MoveToEx(hdc, r.left, r.bottom, NULL);
415 LineTo(hdc, r.right, r.bottom);
416 LineTo(hdc, r.right, r.top - 1);
417
418 (void)SelectObject(hdc, hpenWhite);
419 MoveToEx(hdc, r.left, r.bottom - 1, NULL);
420 LineTo(hdc, r.left, r.top);
421 LineTo(hdc, r.right, r.top);
422
423 (void)SelectObject(hdc, hpenGrey);
424 MoveToEx(hdc, r.left + 1, r.bottom - 1, NULL);
425 LineTo(hdc, r.right - 1, r.bottom - 1);
426 LineTo(hdc, r.right - 1, r.top);
427 }
428
429 (void)SelectObject(hdc, hpenOld);
430 DeleteObject(hpenWhite);
431 DeleteObject(hpenGrey);
432 DeleteObject(hpenBlack);
433 }
434
435 bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
436 {
437 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis;
438
439 RECT rectBtn;
440 CopyRect(&rectBtn, &lpDIS->rcItem);
441
442 COLORREF colBg = wxColourToRGB(GetBackgroundColour()),
443 colFg = wxColourToRGB(GetForegroundColour());
444
445 HDC hdc = lpDIS->hDC;
446 UINT state = lpDIS->itemState;
447
448 // first, draw the background
449 HBRUSH hbrushBackground = ::CreateSolidBrush(colBg);
450
451 FillRect(hdc, &rectBtn, hbrushBackground);
452
453 // draw the border for the current state
454 bool selected = (state & ODS_SELECTED) != 0;
455 if ( !selected )
456 {
457 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
458 if ( panel )
459 {
460 selected = panel->GetDefaultItem() == this;
461 }
462 }
463 bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;
464
465 DrawButtonFrame(hdc, rectBtn, selected, pushed);
466
467 // draw the focus rect if needed
468 if ( state & ODS_FOCUS )
469 {
470 RECT rectFocus;
471 CopyRect(&rectFocus, &rectBtn);
472
473 // I don't know where does this constant come from, but this is how
474 // Windows draws them
475 InflateRect(&rectFocus, -4, -4);
476
477 DrawFocusRect(hdc, &rectFocus);
478 }
479
480 DrawButtonText(hdc, &rectBtn, GetLabel(),
481 state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT)
482 : colFg);
483
484 ::DeleteObject(hbrushBackground);
485
486 #if 0
487 wxString s = "button state: ";
488 if ( selected )
489 s += "selected ";
490 if ( pushed )
491 s += "pushed ";
492 if ( state & ODS_FOCUS )
493 s += "focused ";
494 wxLogStatus(s);
495 #endif
496
497 return TRUE;
498 }
499
500 #endif // __WIN32__