]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
wxDateTime::ParseFormat() and ParseTime() added (compile but don't work)
[wxWidgets.git] / src / msw / button.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
cd0b1709 2// Name: msw/button.cpp
2bda0e17
KB
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
edccf428 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
edccf428
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
cd0b1709 19
2bda0e17 20#ifdef __GNUG__
edccf428 21 #pragma implementation "button.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
edccf428 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
edccf428
VZ
32 #include "wx/button.h"
33 #include "wx/brush.h"
4e938f5b 34 #include "wx/panel.h"
8a4df159 35 #include "wx/bmpbuttn.h"
fb39c7ec
RR
36 #include "wx/settings.h"
37 #include "wx/dcscreen.h"
2bda0e17
KB
38#endif
39
40#include "wx/msw/private.h"
41
edccf428
VZ
42// ----------------------------------------------------------------------------
43// macros
44// ----------------------------------------------------------------------------
45
cd0b1709 46IMPLEMENT_DYNAMIC_CLASS(wxButton, wxControl)
2bda0e17 47
edccf428
VZ
48// this macro tries to adjust the default button height to a reasonable value
49// using the char height as the base
1c4a764c 50#define BUTTON_HEIGHT_FROM_CHAR_HEIGHT(cy) (11*EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy)/10)
2bda0e17 51
edccf428
VZ
52// ============================================================================
53// implementation
54// ============================================================================
55
56// ----------------------------------------------------------------------------
57// creation/destruction
58// ----------------------------------------------------------------------------
59
60bool 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)
2bda0e17 68{
8d99be5f 69 if ( !CreateBase(parent, id, pos, size, style, validator, name) )
edccf428
VZ
70 return FALSE;
71
edccf428
VZ
72 parent->AddChild((wxButton *)this);
73
cd0b1709
VZ
74 m_backgroundColour = parent->GetBackgroundColour();
75 m_foregroundColour = parent->GetForegroundColour();
76
edccf428
VZ
77
78 m_hWnd = (WXHWND)CreateWindowEx
79 (
80 MakeExtendedStyle(m_windowStyle),
223d09f6 81 wxT("BUTTON"),
edccf428
VZ
82 label,
83 WS_VISIBLE | WS_TABSTOP | WS_CHILD,
a77aa9d6 84 0, 0, 0, 0,
edccf428
VZ
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
3f3cec48
RR
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 {
678cd6de
VZ
104 if ((size.x == -1) && (nsize.x < 80))
105 nsize.x = 80;
106 if ((size.y == -1) && (nsize.y < 23))
107 nsize.y = 23;
3f3cec48
RR
108 SetSize( nsize );
109 }
110
2bda0e17 111 return TRUE;
2bda0e17
KB
112}
113
edccf428 114wxButton::~wxButton()
2bda0e17 115{
edccf428
VZ
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 }
2bda0e17
KB
125}
126
edccf428
VZ
127// ----------------------------------------------------------------------------
128// size management including autosizing
129// ----------------------------------------------------------------------------
130
f68586e5 131wxSize wxButton::DoGetBestSize() const
2bda0e17 132{
4438caf4
VZ
133 wxString label = wxGetWindowText(GetHWND());
134 int wBtn;
135 GetTextExtent(label, &wBtn, NULL);
edccf428 136
4438caf4
VZ
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);
edccf428 145
4438caf4 146 return wxSize(wBtn, hBtn);
2bda0e17
KB
147}
148
e1f36ff8
VZ
149/* static */
150wxSize wxButton::GetDefaultSize()
151{
8c3c31d4 152 static wxSize s_sizeBtn;
e1f36ff8 153
8c3c31d4
VZ
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 }
e1f36ff8 170
8c3c31d4 171 return s_sizeBtn;
e1f36ff8
VZ
172}
173
4438caf4
VZ
174// ----------------------------------------------------------------------------
175// set this button as the default one in its panel
176// ----------------------------------------------------------------------------
177
edccf428 178void wxButton::SetDefault()
2bda0e17 179{
edccf428 180 wxWindow *parent = GetParent();
5d1d2d46 181 wxButton *btnOldDefault = NULL;
edccf428
VZ
182 wxPanel *panel = wxDynamicCast(parent, wxPanel);
183 if ( panel )
5d1d2d46
VZ
184 {
185 btnOldDefault = panel->GetDefaultItem();
edccf428 186 panel->SetDefaultItem(this);
5d1d2d46 187 }
2bda0e17 188
edccf428
VZ
189 if ( parent )
190 {
191 SendMessage(GetWinHwnd(parent), DM_SETDEFID, m_windowId, 0L);
192 }
8ed57d93 193
cd0b1709 194 if ( btnOldDefault )
5d1d2d46
VZ
195 {
196 // remove the BS_DEFPUSHBUTTON style from the other button
197 long style = GetWindowLong(GetHwndOf(btnOldDefault), GWL_STYLE);
cd0b1709
VZ
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 }
5d1d2d46
VZ
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);
2bda0e17
KB
212}
213
edccf428
VZ
214// ----------------------------------------------------------------------------
215// helpers
216// ----------------------------------------------------------------------------
217
218bool wxButton::SendClickEvent()
2bda0e17 219{
edccf428
VZ
220 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
221 event.SetEventObject(this);
222
223 return ProcessCommand(event);
2bda0e17
KB
224}
225
edccf428 226void wxButton::Command(wxCommandEvent & event)
2bda0e17 227{
edccf428 228 ProcessCommand(event);
2bda0e17
KB
229}
230
edccf428
VZ
231// ----------------------------------------------------------------------------
232// event/message handlers
233// ----------------------------------------------------------------------------
2bda0e17 234
edccf428
VZ
235bool wxButton::MSWCommand(WXUINT param, WXWORD id)
236{
237 bool processed = FALSE;
57c0af52 238 switch ( param )
edccf428 239 {
678cd6de 240 case 1: // means that the message came from an accelerator
57c0af52
VZ
241 case BN_CLICKED:
242 processed = SendClickEvent();
243 break;
edccf428 244 }
2bda0e17 245
edccf428 246 return processed;
2bda0e17
KB
247}
248
678cd6de
VZ
249long 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}
cd0b1709
VZ
274
275// ----------------------------------------------------------------------------
276// owner-drawn buttons support
277// ----------------------------------------------------------------------------
278
279#ifdef __WIN32__
280
281// drawing helpers
282
283static 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
298static 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
307void 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
318bool 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
333bool 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
381static 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
435bool 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__