]>
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "bmpbuttn.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
26 #include "wx/bmpbuttn.h"
28 #include "wx/dcmemory.h"
31 #include "wx/msw/private.h"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 #if wxUSE_EXTENDED_RTTI
40 WX_DEFINE_FLAGS( wxBitmapButtonStyle
)
42 wxBEGIN_FLAGS( wxBitmapButtonStyle
)
43 // new style border flags, we put them first to
44 // use them for streaming out
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
)
52 // old style border flags
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
)
58 wxFLAGS_MEMBER(wxBORDER
)
60 // standard window styles
61 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
62 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
63 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
64 wxFLAGS_MEMBER(wxWANTS_CHARS
)
65 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
66 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
67 wxFLAGS_MEMBER(wxVSCROLL
)
68 wxFLAGS_MEMBER(wxHSCROLL
)
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
)
75 wxEND_FLAGS( wxBitmapButtonStyle
)
77 IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton
, wxButton
,"wx/bmpbuttn.h")
79 wxBEGIN_PROPERTIES_TABLE(wxBitmapButton
)
80 wxPROPERTY_FLAGS( WindowStyle
, wxBitmapButtonStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
81 wxEND_PROPERTIES_TABLE()
83 wxBEGIN_HANDLERS_TABLE(wxBitmapButton
)
84 wxEND_HANDLERS_TABLE()
86 wxCONSTRUCTOR_5( wxBitmapButton
, wxWindow
* , Parent
, wxWindowID
, Id
, wxBitmap
, Bitmap
, wxPoint
, Position
, wxSize
, Size
)
89 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton
, wxButton
)
95 long "style" , wxBU_AUTODRAW
102 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
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
)
126 m_marginX
= wxDEFAULT_BUTTON_MARGIN
;
127 m_marginY
= wxDEFAULT_BUTTON_MARGIN
;
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 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
187 #define FOCUS_MARGIN 3
189 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT
*item
)
192 long style
= GetWindowLong((HWND
) GetHWND(), GWL_STYLE
);
193 if (style
& BS_BITMAP
)
195 // Let default procedure draw the bitmap, which is defined
196 // in the Windows resource.
201 LPDRAWITEMSTRUCT lpDIS
= (LPDRAWITEMSTRUCT
) item
;
202 HDC hDC
= lpDIS
->hDC
;
203 UINT state
= lpDIS
->itemState
;
204 bool isSelected
= (state
& ODS_SELECTED
) != 0;
205 bool autoDraw
= (GetWindowStyleFlag() & wxBU_AUTODRAW
) != 0;
208 // choose the bitmap to use depending on the button state
211 if ( isSelected
&& m_bmpSelected
.Ok() )
212 bitmap
= &m_bmpSelected
;
213 else if ((state
& ODS_FOCUS
) && m_bmpFocus
.Ok())
214 bitmap
= &m_bmpFocus
;
215 else if ((state
& ODS_DISABLED
) && m_bmpDisabled
.Ok())
216 bitmap
= &m_bmpDisabled
;
218 bitmap
= &m_bmpNormal
;
223 // centre the bitmap in the control area
224 int x
= lpDIS
->rcItem
.left
;
225 int y
= lpDIS
->rcItem
.top
;
226 int width
= lpDIS
->rcItem
.right
- x
;
227 int height
= lpDIS
->rcItem
.bottom
- y
;
228 int wBmp
= bitmap
->GetWidth();
229 int hBmp
= bitmap
->GetHeight();
233 if(m_windowStyle
& wxBU_LEFT
)
234 x1
= x
+ (FOCUS_MARGIN
+1);
235 else if(m_windowStyle
& wxBU_RIGHT
)
236 x1
= x
+ (width
- wBmp
) - (FOCUS_MARGIN
+1);
238 x1
= x
+ (width
- wBmp
) / 2;
240 if(m_windowStyle
& wxBU_TOP
)
241 y1
= y
+ (FOCUS_MARGIN
+1);
242 else if(m_windowStyle
& wxBU_BOTTOM
)
243 y1
= y
+ (height
- hBmp
) - (FOCUS_MARGIN
+1);
245 y1
= y
+ (height
- hBmp
) / 2;
247 if ( isSelected
&& autoDraw
)
253 // draw the face, if auto-drawing
256 DrawFace((WXHDC
) hDC
,
257 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
258 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
264 dst
.SetHDC((WXHDC
) hDC
, false);
265 dst
.DrawBitmap(*bitmap
, x1
, y1
, true);
267 // draw focus / disabled state, if auto-drawing
268 if ( (state
& ODS_DISABLED
) && autoDraw
)
270 DrawButtonDisable((WXHDC
) hDC
,
271 lpDIS
->rcItem
.left
, lpDIS
->rcItem
.top
,
272 lpDIS
->rcItem
.right
, lpDIS
->rcItem
.bottom
,
275 else if ( (state
& ODS_FOCUS
) && autoDraw
)
277 DrawButtonFocus((WXHDC
) hDC
,
281 lpDIS
->rcItem
.bottom
,
288 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
290 #if defined(__WIN95__)
292 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
,
293 int right
, int bottom
, bool sel
)
302 // create needed pens and brush
303 penHiLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DHILIGHT
));
304 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DLIGHT
));
305 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DSHADOW
));
306 penDkShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_3DDKSHADOW
));
307 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
308 // Taking the background colour fits in better with
309 // Windows XP themes.
310 brushFace
= CreateSolidBrush(m_backgroundColour
.m_pixel
);
312 // draw the rectangle
317 rect
.bottom
= bottom
;
318 FillRect((HDC
) dc
, &rect
, brushFace
);
321 oldp
= (HPEN
) SelectObject( (HDC
) dc
, sel
? penDkShadow
: penHiLight
);
323 wxDrawLine((HDC
) dc
, left
, top
, right
-1, top
);
324 wxDrawLine((HDC
) dc
, left
, top
+1, left
, bottom
-1);
326 SelectObject( (HDC
) dc
, sel
? penShadow
: penLight
);
327 wxDrawLine((HDC
) dc
, left
+1, top
+1, right
-2, top
+1);
328 wxDrawLine((HDC
) dc
, left
+1, top
+2, left
+1, bottom
-2);
330 SelectObject( (HDC
) dc
, sel
? penLight
: penShadow
);
331 wxDrawLine((HDC
) dc
, left
+1, bottom
-2, right
-1, bottom
-2);
332 wxDrawLine((HDC
) dc
, right
-2, bottom
-3, right
-2, top
);
334 SelectObject( (HDC
) dc
, sel
? penHiLight
: penDkShadow
);
335 wxDrawLine((HDC
) dc
, left
, bottom
-1, right
+2, bottom
-1);
336 wxDrawLine((HDC
) dc
, right
-1, bottom
-2, right
-1, top
-1);
338 // delete allocated resources
339 SelectObject((HDC
) dc
,oldp
);
340 DeleteObject(penHiLight
);
341 DeleteObject(penLight
);
342 DeleteObject(penShadow
);
343 DeleteObject(penDkShadow
);
344 DeleteObject(brushFace
);
349 void wxBitmapButton::DrawFace( WXHDC dc
, int left
, int top
,
350 int right
, int bottom
, bool sel
)
358 // create needed pens and brush
359 penBorder
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_WINDOWFRAME
));
360 penShadow
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNSHADOW
));
361 penLight
= CreatePen(PS_SOLID
, 0, GetSysColor(COLOR_BTNHIGHLIGHT
));
362 brushFace
= CreateSolidBrush(COLOR_BTNFACE
);
364 // draw the rectangle
369 rect
.bottom
= bottom
;
370 FillRect((HDC
) dc
, &rect
, brushFace
);
373 oldp
= (HPEN
) SelectObject( (HDC
) dc
, penBorder
);
374 MoveToEx((HDC
) dc
,left
+1,top
,NULL
);LineTo((HDC
) dc
,right
-1,top
);
375 MoveToEx((HDC
) dc
,left
,top
+1,NULL
);LineTo((HDC
) dc
,left
,bottom
-1);
376 MoveToEx((HDC
) dc
,left
+1,bottom
-1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
377 MoveToEx((HDC
) dc
,right
-1,top
+1,NULL
);LineTo((HDC
) dc
,right
-1,bottom
-1);
379 SelectObject( (HDC
) dc
, penShadow
);
382 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
383 LineTo((HDC
) dc
, left
+1 ,top
+1);
384 LineTo((HDC
) dc
, right
-2 ,top
+1);
388 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
389 LineTo((HDC
) dc
, right
-2 ,bottom
-2);
390 LineTo((HDC
) dc
, right
-2 ,top
);
392 MoveToEx((HDC
) dc
,left
+2 ,bottom
-3 ,NULL
);
393 LineTo((HDC
) dc
, right
-3 ,bottom
-3);
394 LineTo((HDC
) dc
, right
-3 ,top
+1);
396 SelectObject( (HDC
) dc
, penLight
);
398 MoveToEx((HDC
) dc
,left
+1 ,bottom
-2 ,NULL
);
399 LineTo((HDC
) dc
, left
+1 ,top
+1);
400 LineTo((HDC
) dc
, right
-2 ,top
+1);
403 // delete allocated resources
404 SelectObject((HDC
) dc
,oldp
);
405 DeleteObject(penBorder
);
406 DeleteObject(penLight
);
407 DeleteObject(penShadow
);
408 DeleteObject(brushFace
);
411 #endif // defined(__WIN95__)
414 void wxBitmapButton::DrawButtonFocus( WXHDC dc
, int left
, int top
, int right
,
415 int bottom
, bool WXUNUSED(sel
) )
421 rect
.bottom
= bottom
;
422 InflateRect( &rect
, - FOCUS_MARGIN
, - FOCUS_MARGIN
);
424 // GRG: the focus rectangle should not move when the button is pushed!
427 OffsetRect( &rect, 1, 1 );
430 DrawFocusRect( (HDC
) dc
, &rect
);
434 wxBitmapButton::DrawButtonDisable( WXHDC dc
,
435 int left
, int top
, int right
, int bottom
,
438 if ( !m_brushDisabled
.Ok() )
440 // draw a bitmap with two black and two background colour pixels
443 dc
.SelectObject(bmp
);
444 dc
.SetPen(*wxBLACK_PEN
);
447 dc
.SetPen(GetBackgroundColour());
451 m_brushDisabled
= wxBrush(bmp
);
454 SelectInHDC
selectBrush((HDC
)dc
, GetHbrushOf(m_brushDisabled
));
456 // ROP for "dest |= pattern" operation -- as it doesn't have a standard
457 // name, give it our own
458 static const DWORD PATTERNPAINT
= 0xFA0089UL
;
464 right
-= 2 * m_marginX
;
465 bottom
-= 2 * m_marginY
;
468 ::PatBlt( (HDC
) dc
, left
, top
, right
, bottom
, PATTERNPAINT
);
471 void wxBitmapButton::SetDefault()
473 wxButton::SetDefault();
476 wxSize
wxBitmapButton::DoGetBestSize() const
479 if (m_bmpNormal
.Ok())
481 best
.x
= m_bmpNormal
.GetWidth() + 2*m_marginX
;
482 best
.y
= m_bmpNormal
.GetHeight() + 2*m_marginY
;
485 // all buttons have at least the standard size unless the user explicitly
486 // wants them to be of smaller size and used wxBU_EXACTFIT style when
487 // creating the button
488 if ( !HasFlag(wxBU_EXACTFIT
) )
490 wxSize sz
= GetDefaultSize();
500 #endif // wxUSE_BMPBUTTON