]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
22 #include "wx/bmpbuttn.h"
24 #include "wx/dcmemory.h"
27 #include "wx/msw/private.h"
30 // ----------------------------------------------------------------------------
32 // ----------------------------------------------------------------------------
34 #if wxUSE_EXTENDED_RTTI
36 WX_DEFINE_FLAGS( wxBitmapButtonStyle
)
38 wxBEGIN_FLAGS( wxBitmapButtonStyle
)
39 // new style border flags, we put them first to
40 // use them for streaming out
41 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
42 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
43 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
44 wxFLAGS_MEMBER(wxBORDER_RAISED
)
45 wxFLAGS_MEMBER(wxBORDER_STATIC
)
46 wxFLAGS_MEMBER(wxBORDER_NONE
)
48 // old style border flags
49 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
50 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
51 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
52 wxFLAGS_MEMBER(wxRAISED_BORDER
)
53 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
54 wxFLAGS_MEMBER(wxBORDER
)
56 // standard window styles
57 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
58 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
59 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
60 wxFLAGS_MEMBER(wxWANTS_CHARS
)
61 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
62 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
63 wxFLAGS_MEMBER(wxVSCROLL
)
64 wxFLAGS_MEMBER(wxHSCROLL
)
66 wxFLAGS_MEMBER(wxBU_AUTODRAW
)
67 wxFLAGS_MEMBER(wxBU_LEFT
)
68 wxFLAGS_MEMBER(wxBU_RIGHT
)
69 wxFLAGS_MEMBER(wxBU_TOP
)
70 wxFLAGS_MEMBER(wxBU_BOTTOM
)
71 wxEND_FLAGS( wxBitmapButtonStyle
)
73 IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton
, wxButton
,"wx/bmpbuttn.h")
75 wxBEGIN_PROPERTIES_TABLE(wxBitmapButton
)
76 wxPROPERTY_FLAGS( WindowStyle
, wxBitmapButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
77 wxEND_PROPERTIES_TABLE()
79 wxBEGIN_HANDLERS_TABLE(wxBitmapButton
)
80 wxEND_HANDLERS_TABLE()
82 wxCONSTRUCTOR_5( wxBitmapButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxBitmap
, Bitmap
, wxPoint
, Position
, wxSize
, Size
)
85 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
88 BEGIN_EVENT_TABLE(wxBitmapButton
, wxBitmapButtonBase
)
89 EVT_SYS_COLOUR_CHANGED(wxBitmapButton::OnSysColourChanged
)
90 EVT_ENTER_WINDOW(wxBitmapButton::OnMouseEnterOrLeave
)
91 EVT_LEAVE_WINDOW(wxBitmapButton::OnMouseEnterOrLeave
)
97 long "style" , wxBU_AUTODRAW
104 bool wxBitmapButton::Create(wxWindow
*parent
, wxWindowID id
,
105 const wxBitmap
& bitmap
,
107 const wxSize
& size
, long style
,
108 const wxValidator
& wxVALIDATOR_PARAM(validator
),
109 const wxString
& name
)
111 m_bmpNormal
= bitmap
;
115 SetValidator(validator
);
116 #endif // wxUSE_VALIDATORS
118 parent
->AddChild(this);
120 m_backgroundColour
= parent
->GetBackgroundColour();
121 m_foregroundColour
= parent
->GetForegroundColour();
122 m_windowStyle
= style
;
124 if ( style
& wxBU_AUTODRAW
)
131 m_windowId
= NewControlId();
135 long msStyle
= WS_VISIBLE
| WS_TABSTOP
| WS_CHILD
| BS_OWNERDRAW
;
137 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
138 msStyle
|= WS_CLIPSIBLINGS
;
141 if(m_windowStyle
& wxBU_LEFT
)
143 if(m_windowStyle
& wxBU_RIGHT
)
145 if(m_windowStyle
& wxBU_TOP
)
147 if(m_windowStyle
& wxBU_BOTTOM
)
148 msStyle
|= BS_BOTTOM
;
151 m_hWnd
= (WXHWND
) CreateWindowEx(
163 // Subclass again for purposes of dialog editing mode
172 bool wxBitmapButton::SetBackgroundColour(const wxColour
& colour
)
174 if ( !wxBitmapButtonBase::SetBackgroundColour(colour
) )
180 // invalidate the brush, it will be recreated the next time it's needed
181 m_brushDisabled
= wxNullBrush
;
186 void wxBitmapButton::OnSysColourChanged(wxSysColourChangedEvent
& event
)
188 m_brushDisabled
= wxNullBrush
;
192 // this change affects our current state
199 void wxBitmapButton::OnMouseEnterOrLeave(wxMouseEvent
& event
)
201 if ( IsEnabled() && m_bmpHover
.Ok() )
207 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
208 #define FOCUS_MARGIN 3
210 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
213 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
214 if (style
& BS_BITMAP
)
216 // Let default procedure draw the bitmap, which is defined
217 // in the Windows resource.
222 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
223 HDC hDC
= lpDIS
->hDC
;
224 UINT state
= lpDIS
->itemState
;
225 bool isSelected
= (state
& ODS_SELECTED
) != 0;
226 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
229 // choose the bitmap to use depending on the button state
232 if ( isSelected
&& m_bmpSelected
.Ok() )
233 bitmap
= &m_bmpSelected
;
234 else if ( m_bmpHover
.Ok() && IsMouseInWindow() )
235 bitmap
= &m_bmpHover
;
236 else if ((state
& ODS_FOCUS
) && m_bmpFocus
.Ok())
237 bitmap
= &m_bmpFocus
;
238 else if ((state
& ODS_DISABLED
) && m_bmpDisabled
.Ok())
239 bitmap
= &m_bmpDisabled
;
241 bitmap
= &m_bmpNormal
;
246 // centre the bitmap in the control area
247 int x
= lpDIS
->rcItem
.left
;
248 int y
= lpDIS
->rcItem
.top
;
249 int width
= lpDIS
->rcItem
.right
- x
;
250 int height
= lpDIS
->rcItem
.bottom
- y
;
251 int wBmp
= bitmap
->GetWidth();
252 int hBmp
= bitmap
->GetHeight();
256 if(m_windowStyle
& wxBU_LEFT
)
257 x1
= x
+ (FOCUS_MARGIN
+1);
258 else if(m_windowStyle
& wxBU_RIGHT
)
259 x1
= x
+ (width
- wBmp
) - (FOCUS_MARGIN
+1);
261 x1
= x
+ (width
- wBmp
) / 2;
263 if(m_windowStyle
& wxBU_TOP
)
264 y1
= y
+ (FOCUS_MARGIN
+1);
265 else if(m_windowStyle
& wxBU_BOTTOM
)
266 y1
= y
+ (height
- hBmp
) - (FOCUS_MARGIN
+1);
268 y1
= y
+ (height
- hBmp
) / 2;
270 if ( isSelected
&& autoDraw
)
276 // draw the face, if auto-drawing
279 DrawFace((WXHDC
) hDC
,
280 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
281 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
287 dst
.SetHDC((WXHDC
) hDC
, false);
288 dst
.DrawBitmap(*bitmap
, x1
, y1
, true);
290 // draw focus / disabled state, if auto-drawing
291 if ( (state
& ODS_DISABLED
) && autoDraw
)
293 DrawButtonDisable((WXHDC
) hDC
,
294 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
295 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
298 else if ( (state
& ODS_FOCUS
) && autoDraw
)
300 DrawButtonFocus((WXHDC
) hDC
,
304 lpDIS
->rcItem
.bottom
,
311 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
313 #if defined(__WIN95__)
315 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
,
316 int right
, int bottom
, bool sel
)
325 // create needed pens and brush
326 penHiLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DHILIGHT
));
327 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DLIGHT
));
328 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DSHADOW
));
329 penDkShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DDKSHADOW
));
330 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
331 // Taking the background colour fits in better with
332 // Windows XP themes.
333 brushFace
= CreateSolidBrush(m_backgroundColour
.m_pixel
);
335 // draw the rectangle
340 rect
.bottom
= bottom
;
341 FillRect((HDC
) dc
, &rect
, brushFace
);
344 oldp
= (HPEN
) SelectObject( (HDC
) dc
, sel
? penDkShadow
: penHiLight
);
346 wxDrawLine((HDC
) dc
, left
, top
, right
-1, top
);
347 wxDrawLine((HDC
) dc
, left
, top
+1, left
, bottom
-1);
349 SelectObject( (HDC
) dc
, sel
? penShadow
: penLight
);
350 wxDrawLine((HDC
) dc
, left
+1, top
+1, right
-2, top
+1);
351 wxDrawLine((HDC
) dc
, left
+1, top
+2, left
+1, bottom
-2);
353 SelectObject( (HDC
) dc
, sel
? penLight
: penShadow
);
354 wxDrawLine((HDC
) dc
, left
+1, bottom
-2, right
-1, bottom
-2);
355 wxDrawLine((HDC
) dc
, right
-2, bottom
-3, right
-2, top
);
357 SelectObject( (HDC
) dc
, sel
? penHiLight
: penDkShadow
);
358 wxDrawLine((HDC
) dc
, left
, bottom
-1, right
+2, bottom
-1);
359 wxDrawLine((HDC
) dc
, right
-1, bottom
-2, right
-1, top
-1);
361 // delete allocated resources
362 SelectObject((HDC
) dc
,oldp
);
363 DeleteObject(penHiLight
);
364 DeleteObject(penLight
);
365 DeleteObject(penShadow
);
366 DeleteObject(penDkShadow
);
367 DeleteObject(brushFace
);
372 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
,
373 int right
, int bottom
, bool sel
)
381 // create needed pens and brush
382 penBorder
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOWFRAME
));
383 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNSHADOW
));
384 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNHIGHLIGHT
));
385 brushFace
= CreateSolidBrush(COLOR_BTNFACE
);
387 // draw the rectangle
392 rect
.bottom
= bottom
;
393 FillRect((HDC
) dc
, &rect
, brushFace
);
396 oldp
= (HPEN
) SelectObject( (HDC
) dc
, penBorder
);
397 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
398 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
399 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
400 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
402 SelectObject( (HDC
) dc
, penShadow
);
405 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
406 LineTo((HDC
) dc
, left
+1 ,top
+1);
407 LineTo((HDC
) dc
, right
-2 ,top
+1);
411 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
412 LineTo((HDC
) dc
, right
-2 ,bottom
-2);
413 LineTo((HDC
) dc
, right
-2 ,top
);
415 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
);
416 LineTo((HDC
) dc
, right
-3 ,bottom
-3);
417 LineTo((HDC
) dc
, right
-3 ,top
+1);
419 SelectObject( (HDC
) dc
, penLight
);
421 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
422 LineTo((HDC
) dc
, left
+1 ,top
+1);
423 LineTo((HDC
) dc
, right
-2 ,top
+1);
426 // delete allocated resources
427 SelectObject((HDC
) dc
,oldp
);
428 DeleteObject(penBorder
);
429 DeleteObject(penLight
);
430 DeleteObject(penShadow
);
431 DeleteObject(brushFace
);
434 #endif // defined(__WIN95__)
437 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
,
438 int bottom
, bool WXUNUSED(sel
) )
444 rect
.bottom
= bottom
;
445 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
);
447 // GRG: the focus rectangle should not move when the button is pushed!
450 OffsetRect( &rect, 1, 1 );
453 DrawFocusRect( (HDC
) dc
, &rect
);
457 wxBitmapButton::DrawButtonDisable( WXHDC dc
,
458 int left
, int top
, int right
, int bottom
,
461 if ( !m_brushDisabled
.Ok() )
463 // draw a bitmap with two black and two background colour pixels
466 dc
.SelectObject(bmp
);
467 dc
.SetPen(*wxBLACK_PEN
);
470 dc
.SetPen(GetBackgroundColour());
474 m_brushDisabled
= wxBrush(bmp
);
477 SelectInHDC
selectBrush((HDC
)dc
, GetHbrushOf(m_brushDisabled
));
479 // ROP for "dest |= pattern" operation -- as it doesn't have a standard
480 // name, give it our own
481 static const DWORD PATTERNPAINT
= 0xFA0089UL
;
487 right
-= 2 * m_marginX
;
488 bottom
-= 2 * m_marginY
;
491 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, PATTERNPAINT
);
494 void wxBitmapButton::SetDefault()
496 wxButton::SetDefault();
499 wxSize
wxBitmapButton::DoGetBestSize() const
501 if ( m_bmpNormal
.Ok() )
503 wxSize
best(m_bmpNormal
.GetWidth() + 2*m_marginX
,
504 m_bmpNormal
.GetHeight() + 2*m_marginY
);
509 // no idea what our best size should be, defer to the base class
510 return wxBitmapButtonBase::DoGetBestSize();
513 #endif // wxUSE_BMPBUTTON