]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
-Wundef fixes
[wxWidgets.git] / src / msw / bmpbuttn.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/msw/bmpbuttn.cpp
2bda0e17
KB
3// Purpose: wxBitmapButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2bda0e17
KB
13#pragma implementation "bmpbuttn.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
1e6feb95 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95
VZ
23#if wxUSE_BMPBUTTON
24
2bda0e17 25#ifndef WX_PRECOMP
10a0bdb1 26 #include "wx/bmpbuttn.h"
84f19880 27 #include "wx/log.h"
47e92c41 28 #include "wx/dcmemory.h"
2bda0e17
KB
29#endif
30
31#include "wx/msw/private.h"
d50dbf7c 32#include "wx/image.h"
2bda0e17 33
bc9fb572
JS
34// ----------------------------------------------------------------------------
35// macros
36// ----------------------------------------------------------------------------
37
38#if wxUSE_EXTENDED_RTTI
39
40WX_DEFINE_FLAGS( wxBitmapButtonStyle )
41
3ff066a4 42wxBEGIN_FLAGS( wxBitmapButtonStyle )
bc9fb572
JS
43 // new style border flags, we put them first to
44 // use them for streaming out
3ff066a4
SC
45 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
46 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
47 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
48 wxFLAGS_MEMBER(wxBORDER_RAISED)
49 wxFLAGS_MEMBER(wxBORDER_STATIC)
50 wxFLAGS_MEMBER(wxBORDER_NONE)
fcf90ee1 51
bc9fb572 52 // old style border flags
3ff066a4
SC
53 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
54 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
55 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
56 wxFLAGS_MEMBER(wxRAISED_BORDER)
57 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 58 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
59
60 // standard window styles
3ff066a4
SC
61 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
62 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
63 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
64 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 65 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
66 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
67 wxFLAGS_MEMBER(wxVSCROLL)
68 wxFLAGS_MEMBER(wxHSCROLL)
69
70 wxFLAGS_MEMBER(wxBU_AUTODRAW)
71 wxFLAGS_MEMBER(wxBU_LEFT)
72 wxFLAGS_MEMBER(wxBU_RIGHT)
73 wxFLAGS_MEMBER(wxBU_TOP)
74 wxFLAGS_MEMBER(wxBU_BOTTOM)
75wxEND_FLAGS( wxBitmapButtonStyle )
bc9fb572
JS
76
77IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton, wxButton,"wx/bmpbuttn.h")
78
3ff066a4 79wxBEGIN_PROPERTIES_TABLE(wxBitmapButton)
af498247 80 wxPROPERTY_FLAGS( WindowStyle , wxBitmapButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 81wxEND_PROPERTIES_TABLE()
bc9fb572 82
3ff066a4
SC
83wxBEGIN_HANDLERS_TABLE(wxBitmapButton)
84wxEND_HANDLERS_TABLE()
bc9fb572 85
3ff066a4 86wxCONSTRUCTOR_5( wxBitmapButton , wxWindow* , Parent , wxWindowID , Id , wxBitmap , Bitmap , wxPoint , Position , wxSize , Size )
bc9fb572
JS
87
88#else
2bda0e17 89IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
bc9fb572 90#endif
2bda0e17 91
a6fd0fde
VZ
92BEGIN_EVENT_TABLE(wxBitmapButton, wxBitmapButtonBase)
93 EVT_SYS_COLOUR_CHANGED(wxBitmapButton::OnSysColourChanged)
94END_EVENT_TABLE()
95
066f1b7a
SC
96/*
97TODO PROPERTIES :
98
99long "style" , wxBU_AUTODRAW
100bool "default" , 0
101bitmap "selected" ,
102bitmap "focus" ,
103bitmap "disabled" ,
104*/
105
a265d21a
DS
106bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id,
107 const wxBitmap& bitmap,
108 const wxPoint& pos,
109 const wxSize& size, long style,
110 const wxValidator& wxVALIDATOR_PARAM(validator),
111 const wxString& name)
2bda0e17 112{
a265d21a
DS
113 m_bmpNormal = bitmap;
114 SetName(name);
11b6a93b
VZ
115
116#if wxUSE_VALIDATORS
a265d21a 117 SetValidator(validator);
11b6a93b 118#endif // wxUSE_VALIDATORS
2bda0e17 119
a265d21a 120 parent->AddChild(this);
2bda0e17 121
a265d21a
DS
122 m_backgroundColour = parent->GetBackgroundColour();
123 m_foregroundColour = parent->GetForegroundColour();
124 m_windowStyle = style;
2bda0e17 125
a265d21a
DS
126 if ( style & wxBU_AUTODRAW )
127 {
128 m_marginX = wxDEFAULT_BUTTON_MARGIN;
129 m_marginY = wxDEFAULT_BUTTON_MARGIN;
130 }
2bda0e17 131
fcf90ee1 132 if (id == wxID_ANY)
a265d21a
DS
133 m_windowId = NewControlId();
134 else
135 m_windowId = id;
2bda0e17 136
a265d21a 137 long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ;
f6bcfd97 138
b0766406
JS
139 if ( m_windowStyle & wxCLIP_SIBLINGS )
140 msStyle |= WS_CLIPSIBLINGS;
141
f6bcfd97
BP
142#ifdef __WIN32__
143 if(m_windowStyle & wxBU_LEFT)
144 msStyle |= BS_LEFT;
145 if(m_windowStyle & wxBU_RIGHT)
146 msStyle |= BS_RIGHT;
147 if(m_windowStyle & wxBU_TOP)
148 msStyle |= BS_TOP;
149 if(m_windowStyle & wxBU_BOTTOM)
150 msStyle |= BS_BOTTOM;
151#endif
152
a265d21a 153 m_hWnd = (WXHWND) CreateWindowEx(
42e69d6b 154 0,
223d09f6 155 wxT("BUTTON"),
fda7962d 156 wxEmptyString,
f6bcfd97 157 msStyle,
33ac7e6f 158 0, 0, 0, 0,
42e69d6b
VZ
159 GetWinHwnd(parent),
160 (HMENU)m_windowId,
161 wxGetInstance(),
162 NULL
163 );
2bda0e17 164
a265d21a
DS
165 // Subclass again for purposes of dialog editing mode
166 SubclassWin(m_hWnd);
2bda0e17 167
9f884528
RD
168 SetPosition(pos);
169 SetBestSize(size);
2bda0e17 170
a265d21a 171 return true;
2bda0e17
KB
172}
173
d50dbf7c
VZ
174bool wxBitmapButton::SetBackgroundColour(const wxColour& colour)
175{
176 if ( !wxBitmapButtonBase::SetBackgroundColour(colour) )
177 {
178 // didn't change
179 return false;
180 }
181
182 // invalidate the brush, it will be recreated the next time it's needed
183 m_brushDisabled = wxNullBrush;
184
185 return true;
186}
187
a6fd0fde
VZ
188void wxBitmapButton::OnSysColourChanged(wxSysColourChangedEvent& event)
189{
190 m_brushDisabled = wxNullBrush;
191
192 if ( !IsEnabled() )
193 {
194 // this change affects our current state
195 Refresh();
196 }
197
198 event.Skip();
199}
200
f6bcfd97
BP
201// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
202#define FOCUS_MARGIN 3
203
2bda0e17
KB
204bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
205{
4676948b 206#ifndef __WXWINCE__
ba681060 207 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
208 if (style & BS_BITMAP)
209 {
fd3f686c
VZ
210 // Let default procedure draw the bitmap, which is defined
211 // in the Windows resource.
a265d21a 212 return false;
fd3f686c 213 }
2bda0e17
KB
214#endif
215
216 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
47e92c41
GRG
217 HDC hDC = lpDIS->hDC;
218 UINT state = lpDIS->itemState;
219 bool isSelected = (state & ODS_SELECTED) != 0;
220 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
2bda0e17 221
47e92c41
GRG
222
223 // choose the bitmap to use depending on the button state
d9c8e68e 224 wxBitmap* bitmap;
2bda0e17 225
1e6feb95
VZ
226 if ( isSelected && m_bmpSelected.Ok() )
227 bitmap = &m_bmpSelected;
228 else if ((state & ODS_FOCUS) && m_bmpFocus.Ok())
229 bitmap = &m_bmpFocus;
230 else if ((state & ODS_DISABLED) && m_bmpDisabled.Ok())
231 bitmap = &m_bmpDisabled;
d9c8e68e 232 else
1e6feb95 233 bitmap = &m_bmpNormal;
2bda0e17 234
42e69d6b 235 if ( !bitmap->Ok() )
a265d21a 236 return false;
2bda0e17 237
47e92c41
GRG
238 // centre the bitmap in the control area
239 int x = lpDIS->rcItem.left;
240 int y = lpDIS->rcItem.top;
241 int width = lpDIS->rcItem.right - x;
242 int height = lpDIS->rcItem.bottom - y;
243 int wBmp = bitmap->GetWidth();
244 int hBmp = bitmap->GetHeight();
f6bcfd97 245
a265d21a
DS
246 int x1,y1;
247
f6bcfd97
BP
248 if(m_windowStyle & wxBU_LEFT)
249 x1 = x + (FOCUS_MARGIN+1);
250 else if(m_windowStyle & wxBU_RIGHT)
251 x1 = x + (width - wBmp) - (FOCUS_MARGIN+1);
252 else
253 x1 = x + (width - wBmp) / 2;
254
255 if(m_windowStyle & wxBU_TOP)
256 y1 = y + (FOCUS_MARGIN+1);
257 else if(m_windowStyle & wxBU_BOTTOM)
258 y1 = y + (height - hBmp) - (FOCUS_MARGIN+1);
259 else
260 y1 = y + (height - hBmp) / 2;
2bda0e17 261
47e92c41 262 if ( isSelected && autoDraw )
d9c8e68e 263 {
47e92c41
GRG
264 x1++;
265 y1++;
d9c8e68e 266 }
2bda0e17 267
47e92c41 268 // draw the face, if auto-drawing
d9c8e68e
VZ
269 if ( autoDraw )
270 {
271 DrawFace((WXHDC) hDC,
272 lpDIS->rcItem.left, lpDIS->rcItem.top,
273 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
274 isSelected);
275 }
2bda0e17 276
47e92c41
GRG
277 // draw the bitmap
278 wxDC dst;
a265d21a
DS
279 dst.SetHDC((WXHDC) hDC, false);
280 dst.DrawBitmap(*bitmap, x1, y1, true);
33ac7e6f 281
47e92c41 282 // draw focus / disabled state, if auto-drawing
d9c8e68e
VZ
283 if ( (state & ODS_DISABLED) && autoDraw )
284 {
285 DrawButtonDisable((WXHDC) hDC,
286 lpDIS->rcItem.left, lpDIS->rcItem.top,
287 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
a265d21a 288 true);
d9c8e68e
VZ
289 }
290 else if ( (state & ODS_FOCUS) && autoDraw )
291 {
292 DrawButtonFocus((WXHDC) hDC,
293 lpDIS->rcItem.left,
294 lpDIS->rcItem.top,
295 lpDIS->rcItem.right,
296 lpDIS->rcItem.bottom,
297 isSelected);
298 }
2bda0e17 299
a265d21a 300 return true;
2bda0e17
KB
301}
302
5ab1fa8e
GRG
303// GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
304
305#if defined(__WIN95__)
306
a265d21a
DS
307void wxBitmapButton::DrawFace( WXHDC dc, int left, int top,
308 int right, int bottom, bool sel )
2bda0e17 309{
d9c8e68e 310 HPEN oldp;
5ab1fa8e 311 HPEN penHiLight;
d9c8e68e
VZ
312 HPEN penLight;
313 HPEN penShadow;
5ab1fa8e 314 HPEN penDkShadow;
d9c8e68e 315 HBRUSH brushFace;
2bda0e17 316
5ab1fa8e
GRG
317 // create needed pens and brush
318 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
319 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
320 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
321 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
85b43fbf
JS
322 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
323 // Taking the background colour fits in better with
324 // Windows XP themes.
325 brushFace = CreateSolidBrush(m_backgroundColour.m_pixel);
5ab1fa8e
GRG
326
327 // draw the rectangle
328 RECT rect;
329 rect.left = left;
330 rect.right = right;
331 rect.top = top;
332 rect.bottom = bottom;
333 FillRect((HDC) dc, &rect, brushFace);
334
335 // draw the border
336 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
4676948b
JS
337
338 wxDrawLine((HDC) dc, left, top, right-1, top);
339 wxDrawLine((HDC) dc, left, top+1, left, bottom-1);
5ab1fa8e
GRG
340
341 SelectObject( (HDC) dc, sel? penShadow : penLight);
4676948b
JS
342 wxDrawLine((HDC) dc, left+1, top+1, right-2, top+1);
343 wxDrawLine((HDC) dc, left+1, top+2, left+1, bottom-2);
5ab1fa8e
GRG
344
345 SelectObject( (HDC) dc, sel? penLight : penShadow);
4676948b
JS
346 wxDrawLine((HDC) dc, left+1, bottom-2, right-1, bottom-2);
347 wxDrawLine((HDC) dc, right-2, bottom-3, right-2, top);
5ab1fa8e
GRG
348
349 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
4676948b
JS
350 wxDrawLine((HDC) dc, left, bottom-1, right+2, bottom-1);
351 wxDrawLine((HDC) dc, right-1, bottom-2, right-1, top-1);
5ab1fa8e
GRG
352
353 // delete allocated resources
354 SelectObject((HDC) dc,oldp);
355 DeleteObject(penHiLight);
356 DeleteObject(penLight);
357 DeleteObject(penShadow);
358 DeleteObject(penDkShadow);
359 DeleteObject(brushFace);
360}
361
362#else
2bda0e17 363
a265d21a
DS
364void wxBitmapButton::DrawFace( WXHDC dc, int left, int top,
365 int right, int bottom, bool sel )
5ab1fa8e
GRG
366{
367 HPEN oldp;
368 HPEN penBorder;
369 HPEN penLight;
370 HPEN penShadow;
371 HBRUSH brushFace;
2bda0e17 372
5ab1fa8e
GRG
373 // create needed pens and brush
374 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
375 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
376 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
377 brushFace = CreateSolidBrush(COLOR_BTNFACE);
2bda0e17 378
5ab1fa8e
GRG
379 // draw the rectangle
380 RECT rect;
381 rect.left = left;
382 rect.right = right;
383 rect.top = top;
384 rect.bottom = bottom;
385 FillRect((HDC) dc, &rect, brushFace);
2bda0e17 386
5ab1fa8e
GRG
387 // draw the border
388 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
d9c8e68e
VZ
389 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
390 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
391 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
392 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
2bda0e17 393
5ab1fa8e 394 SelectObject( (HDC) dc, penShadow);
fd3f686c
VZ
395 if (sel)
396 {
5ab1fa8e
GRG
397 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
398 LineTo((HDC) dc, left+1 ,top+1);
399 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c
VZ
400 }
401 else
402 {
5ab1fa8e
GRG
403 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
404 LineTo((HDC) dc, right-2 ,bottom-2);
405 LineTo((HDC) dc, right-2 ,top);
406
407 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
408 LineTo((HDC) dc, right-3 ,bottom-3);
409 LineTo((HDC) dc, right-3 ,top+1);
410
411 SelectObject( (HDC) dc, penLight);
412
413 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
414 LineTo((HDC) dc, left+1 ,top+1);
415 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c 416 }
2bda0e17 417
5ab1fa8e
GRG
418 // delete allocated resources
419 SelectObject((HDC) dc,oldp);
d9c8e68e
VZ
420 DeleteObject(penBorder);
421 DeleteObject(penLight);
422 DeleteObject(penShadow);
423 DeleteObject(brushFace);
2bda0e17
KB
424}
425
5ab1fa8e
GRG
426#endif // defined(__WIN95__)
427
428
a265d21a
DS
429void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right,
430 int bottom, bool WXUNUSED(sel) )
2bda0e17 431{
fd3f686c
VZ
432 RECT rect;
433 rect.left = left;
434 rect.top = top;
435 rect.right = right;
436 rect.bottom = bottom;
5ab1fa8e
GRG
437 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
438
439 // GRG: the focus rectangle should not move when the button is pushed!
440/*
fd3f686c 441 if ( sel )
5ab1fa8e
GRG
442 OffsetRect( &rect, 1, 1 );
443*/
a265d21a 444
5ab1fa8e 445 DrawFocusRect( (HDC) dc, &rect );
2bda0e17
KB
446}
447
d50dbf7c
VZ
448void
449wxBitmapButton::DrawButtonDisable( WXHDC dc,
450 int left, int top, int right, int bottom,
451 bool with_marg )
2bda0e17 452{
d50dbf7c
VZ
453 if ( !m_brushDisabled.Ok() )
454 {
455 // draw a bitmap with two black and two background colour pixels
456 wxBitmap bmp(2, 2);
457 wxMemoryDC dc;
458 dc.SelectObject(bmp);
459 dc.SetPen(*wxBLACK_PEN);
460 dc.DrawPoint(0, 0);
461 dc.DrawPoint(1, 1);
462 dc.SetPen(GetBackgroundColour());
463 dc.DrawPoint(0, 1);
464 dc.DrawPoint(1, 0);
465
466 m_brushDisabled = wxBrush(bmp);
467 }
468
469 SelectInHDC selectBrush((HDC)dc, GetHbrushOf(m_brushDisabled));
2bda0e17 470
2b268535
VZ
471 // ROP for "dest |= pattern" operation -- as it doesn't have a standard
472 // name, give it our own
a265d21a 473 static const DWORD PATTERNPAINT = 0xFA0089UL;
d9c8e68e
VZ
474
475 if ( with_marg )
476 {
477 left += m_marginX;
478 top += m_marginY;
479 right -= 2 * m_marginX;
480 bottom -= 2 * m_marginY;
481 }
482
a265d21a 483 ::PatBlt( (HDC) dc, left, top, right, bottom, PATTERNPAINT);
2bda0e17
KB
484}
485
0655ad29
VZ
486void wxBitmapButton::SetDefault()
487{
488 wxButton::SetDefault();
489}
1e6feb95 490
c046274e
RD
491wxSize wxBitmapButton::DoGetBestSize() const
492{
9266e0ec 493 if ( m_bmpNormal.Ok() )
c046274e 494 {
31582e4e 495 wxSize best(m_bmpNormal.GetWidth() + 2*m_marginX,
9266e0ec 496 m_bmpNormal.GetHeight() + 2*m_marginY);
31582e4e
RD
497 CacheBestSize(best);
498 return best;
c046274e 499 }
e8153940 500
9266e0ec
VZ
501 // no idea what our best size should be, defer to the base class
502 return wxBitmapButtonBase::DoGetBestSize();
c046274e
RD
503}
504
1e6feb95 505#endif // wxUSE_BMPBUTTON
e8153940 506