]> git.saurik.com Git - wxWidgets.git/blame - src/msw/button.cpp
SN: Added #pragma implementation needed by GCC - expect more to come
[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)!
be4017f8 201 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
cd0b1709
VZ
202 {
203 style &= ~BS_DEFPUSHBUTTON;
204 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
205 }
be4017f8
VZ
206 else
207 {
208 // redraw the button - it will notice itself that it's not the
209 // default one any longer
210 btnOldDefault->Refresh();
211 }
5d1d2d46
VZ
212 }
213
214 // set this button as the default
215 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
be4017f8
VZ
216 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
217 {
218 style |= BS_DEFPUSHBUTTON;
219 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
220 }
2bda0e17
KB
221}
222
edccf428
VZ
223// ----------------------------------------------------------------------------
224// helpers
225// ----------------------------------------------------------------------------
226
227bool wxButton::SendClickEvent()
2bda0e17 228{
edccf428
VZ
229 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
230 event.SetEventObject(this);
231
232 return ProcessCommand(event);
2bda0e17
KB
233}
234
edccf428 235void wxButton::Command(wxCommandEvent & event)
2bda0e17 236{
edccf428 237 ProcessCommand(event);
2bda0e17
KB
238}
239
edccf428
VZ
240// ----------------------------------------------------------------------------
241// event/message handlers
242// ----------------------------------------------------------------------------
2bda0e17 243
edccf428
VZ
244bool wxButton::MSWCommand(WXUINT param, WXWORD id)
245{
246 bool processed = FALSE;
57c0af52 247 switch ( param )
edccf428 248 {
678cd6de 249 case 1: // means that the message came from an accelerator
57c0af52
VZ
250 case BN_CLICKED:
251 processed = SendClickEvent();
252 break;
edccf428 253 }
2bda0e17 254
edccf428 255 return processed;
2bda0e17
KB
256}
257
678cd6de
VZ
258long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
259{
be4017f8
VZ
260 // when we receive focus, we want to become the default button in our
261 // parent panel
262 if ( nMsg == WM_SETFOCUS )
678cd6de 263 {
be4017f8
VZ
264 SetDefault();
265
266 // let the default processign take place too
678cd6de
VZ
267 }
268
269 // let the base class do all real processing
270 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
271}
cd0b1709
VZ
272
273// ----------------------------------------------------------------------------
274// owner-drawn buttons support
275// ----------------------------------------------------------------------------
276
277#ifdef __WIN32__
278
279// drawing helpers
280
281static void DrawButtonText(HDC hdc,
282 RECT *pRect,
283 const wxString& text,
284 COLORREF col)
285{
286 COLORREF colOld = SetTextColor(hdc, col);
287 int modeOld = SetBkMode(hdc, TRANSPARENT);
288
289 DrawText(hdc, text, text.length(), pRect,
290 DT_CENTER | DT_VCENTER | DT_SINGLELINE);
291
292 SetBkMode(hdc, modeOld);
293 SetTextColor(hdc, colOld);
294}
295
296static void DrawRect(HDC hdc, const RECT& r)
297{
298 MoveToEx(hdc, r.left, r.top, NULL);
299 LineTo(hdc, r.right, r.top);
300 LineTo(hdc, r.right, r.bottom);
301 LineTo(hdc, r.left, r.bottom);
302 LineTo(hdc, r.left, r.top);
303}
304
305void wxButton::MakeOwnerDrawn()
306{
307 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
9750fc42 308 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
cd0b1709
VZ
309 {
310 // make it so
311 style |= BS_OWNERDRAW;
312 SetWindowLong(GetHwnd(), GWL_STYLE, style);
313 }
314}
315
316bool wxButton::SetBackgroundColour(const wxColour &colour)
317{
318 if ( !wxControl::SetBackgroundColour(colour) )
319 {
320 // nothing to do
321 return FALSE;
322 }
323
324 MakeOwnerDrawn();
325
326 Refresh();
327
328 return TRUE;
329}
330
331bool wxButton::SetForegroundColour(const wxColour &colour)
332{
333 if ( !wxControl::SetForegroundColour(colour) )
334 {
335 // nothing to do
336 return FALSE;
337 }
338
339 MakeOwnerDrawn();
340
341 Refresh();
342
343 return TRUE;
344}
345
346/*
347 The button frame looks like this normally:
348
349 WWWWWWWWWWWWWWWWWWB
350 W GB
351 W GB
352 W GB where W, G, B are white, grey and black pixels
353 W GB
354 WGGGGGGGGGGGGGGGGGB
355 BBBBBBBBBBBBBBBBBBB
356
357 When the button is selected, the button becomes like this (the total button
358 size doesn't change):
359
360 BBBBBBBBBBBBBBBBBBB
361 BWWWWWWWWWWWWWWWWBB
362 BW GBB
363 BW GBB
364 BWGGGGGGGGGGGGGGGBB
365 BBBBBBBBBBBBBBBBBBB
366 BBBBBBBBBBBBBBBBBBB
367
368 When the button is pushed (while selected) it is like:
369
370 BBBBBBBBBBBBBBBBBBB
371 BGGGGGGGGGGGGGGGGGB
372 BG GB
373 BG GB
374 BG GB
375 BGGGGGGGGGGGGGGGGGB
376 BBBBBBBBBBBBBBBBBBB
377*/
378
379static void DrawButtonFrame(HDC hdc, const RECT& rectBtn,
380 bool selected, bool pushed)
381{
382 RECT r;
383 CopyRect(&r, &rectBtn);
384
385 HPEN hpenBlack = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DDKSHADOW)),
386 hpenGrey = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DSHADOW)),
387 hpenWhite = CreatePen(PS_SOLID, 1, GetSysColor(COLOR_3DHILIGHT));
388
389 HPEN hpenOld = (HPEN)SelectObject(hdc, hpenBlack);
390
391 r.right--;
392 r.bottom--;
393
394 if ( pushed )
395 {
396 DrawRect(hdc, r);
397
398 (void)SelectObject(hdc, hpenGrey);
399 InflateRect(&r, -1, -1);
400
401 DrawRect(hdc, r);
402 }
403 else // !pushed
404 {
405 if ( selected )
406 {
407 DrawRect(hdc, r);
408
409 InflateRect(&r, -1, -1);
410 }
411
412 MoveToEx(hdc, r.left, r.bottom, NULL);
413 LineTo(hdc, r.right, r.bottom);
414 LineTo(hdc, r.right, r.top - 1);
415
416 (void)SelectObject(hdc, hpenWhite);
417 MoveToEx(hdc, r.left, r.bottom - 1, NULL);
418 LineTo(hdc, r.left, r.top);
419 LineTo(hdc, r.right, r.top);
420
421 (void)SelectObject(hdc, hpenGrey);
422 MoveToEx(hdc, r.left + 1, r.bottom - 1, NULL);
423 LineTo(hdc, r.right - 1, r.bottom - 1);
424 LineTo(hdc, r.right - 1, r.top);
425 }
426
427 (void)SelectObject(hdc, hpenOld);
428 DeleteObject(hpenWhite);
429 DeleteObject(hpenGrey);
430 DeleteObject(hpenBlack);
431}
432
433bool wxButton::MSWOnDraw(WXDRAWITEMSTRUCT *wxdis)
434{
435 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)wxdis;
436
437 RECT rectBtn;
438 CopyRect(&rectBtn, &lpDIS->rcItem);
439
440 COLORREF colBg = wxColourToRGB(GetBackgroundColour()),
441 colFg = wxColourToRGB(GetForegroundColour());
442
443 HDC hdc = lpDIS->hDC;
444 UINT state = lpDIS->itemState;
445
446 // first, draw the background
447 HBRUSH hbrushBackground = ::CreateSolidBrush(colBg);
448
449 FillRect(hdc, &rectBtn, hbrushBackground);
450
451 // draw the border for the current state
452 bool selected = (state & ODS_SELECTED) != 0;
453 if ( !selected )
454 {
455 wxPanel *panel = wxDynamicCast(GetParent(), wxPanel);
456 if ( panel )
457 {
458 selected = panel->GetDefaultItem() == this;
459 }
460 }
461 bool pushed = (SendMessage(GetHwnd(), BM_GETSTATE, 0, 0) & BST_PUSHED) != 0;
462
463 DrawButtonFrame(hdc, rectBtn, selected, pushed);
464
465 // draw the focus rect if needed
466 if ( state & ODS_FOCUS )
467 {
468 RECT rectFocus;
469 CopyRect(&rectFocus, &rectBtn);
470
471 // I don't know where does this constant come from, but this is how
472 // Windows draws them
473 InflateRect(&rectFocus, -4, -4);
474
475 DrawFocusRect(hdc, &rectFocus);
476 }
477
9750fc42
VZ
478 if ( pushed )
479 {
480 // the label is shifted by 1 pixel to create "pushed" effect
481 OffsetRect(&rectBtn, 1, 1);
482 }
483
cd0b1709
VZ
484 DrawButtonText(hdc, &rectBtn, GetLabel(),
485 state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT)
486 : colFg);
487
488 ::DeleteObject(hbrushBackground);
489
cd0b1709
VZ
490 return TRUE;
491}
492
493#endif // __WIN32__