]> git.saurik.com Git - wxWidgets.git/blob - src/msw/button.cpp
1. coloured buttons seem to work
[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) != BS_OWNERDRAW )
202 {
203 style &= ~BS_DEFPUSHBUTTON;
204 SendMessage(GetHwndOf(btnOldDefault), BM_SETSTYLE, style, 1L);
205 }
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 }
212 }
213
214 // set this button as the default
215 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
216 if ( (style & BS_OWNERDRAW) != BS_OWNERDRAW )
217 {
218 style |= BS_DEFPUSHBUTTON;
219 SendMessage(GetHwnd(), BM_SETSTYLE, style, 1L);
220 }
221 }
222
223 // ----------------------------------------------------------------------------
224 // helpers
225 // ----------------------------------------------------------------------------
226
227 bool wxButton::SendClickEvent()
228 {
229 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, GetId());
230 event.SetEventObject(this);
231
232 return ProcessCommand(event);
233 }
234
235 void wxButton::Command(wxCommandEvent & event)
236 {
237 ProcessCommand(event);
238 }
239
240 // ----------------------------------------------------------------------------
241 // event/message handlers
242 // ----------------------------------------------------------------------------
243
244 bool wxButton::MSWCommand(WXUINT param, WXWORD id)
245 {
246 bool processed = FALSE;
247 switch ( param )
248 {
249 case 1: // means that the message came from an accelerator
250 case BN_CLICKED:
251 processed = SendClickEvent();
252 break;
253 }
254
255 return processed;
256 }
257
258 long wxButton::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
259 {
260 // when we receive focus, we want to become the default button in our
261 // parent panel
262 if ( nMsg == WM_SETFOCUS )
263 {
264 SetDefault();
265
266 // let the default processign take place too
267 }
268
269 // let the base class do all real processing
270 return wxControl::MSWWindowProc(nMsg, wParam, lParam);
271 }
272
273 // ----------------------------------------------------------------------------
274 // owner-drawn buttons support
275 // ----------------------------------------------------------------------------
276
277 #ifdef __WIN32__
278
279 // drawing helpers
280
281 static 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
296 static 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
305 void wxButton::MakeOwnerDrawn()
306 {
307 long style = GetWindowLong(GetHwnd(), GWL_STYLE);
308 if ( !(style & BS_OWNERDRAW) )
309 {
310 // make it so
311 style |= BS_OWNERDRAW;
312 SetWindowLong(GetHwnd(), GWL_STYLE, style);
313 }
314 }
315
316 bool 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
331 bool 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
379 static 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
433 bool 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
478 DrawButtonText(hdc, &rectBtn, GetLabel(),
479 state & ODS_DISABLED ? GetSysColor(COLOR_GRAYTEXT)
480 : colFg);
481
482 ::DeleteObject(hbrushBackground);
483
484 return TRUE;
485 }
486
487 #endif // __WIN32__