]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
Various small fixes and tweaks
[wxWidgets.git] / src / msw / bmpbuttn.cpp
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: bmpbuttn.cpp
3// Purpose: wxBitmapButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
fd3f686c 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
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__
20#pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
10a0bdb1 24 #include "wx/bmpbuttn.h"
2bda0e17
KB
25#endif
26
27#include "wx/msw/private.h"
28
29#if !USE_SHARED_LIBRARY
30IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
31#endif
32
33#define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34
debe6624 35bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
2bda0e17 36 const wxPoint& pos,
debe6624 37 const wxSize& size, long style,
2bda0e17
KB
38 const wxValidator& validator,
39 const wxString& name)
40{
41 m_buttonBitmap = bitmap;
42 SetName(name);
43 SetValidator(validator);
44
45 parent->AddChild(this);
46
fd71308f
JS
47 m_backgroundColour = parent->GetBackgroundColour() ;
48 m_foregroundColour = parent->GetForegroundColour() ;
2bda0e17
KB
49 m_windowStyle = style;
50 m_marginX = 0;
51 m_marginY = 0;
52
53 if ( style & wxBU_AUTODRAW )
54 {
fd3f686c
VZ
55 m_marginX = wxDEFAULT_BUTTON_MARGIN;
56 m_marginY = wxDEFAULT_BUTTON_MARGIN;
2bda0e17
KB
57 }
58
59 int x = pos.x;
60 int y = pos.y;
61 int width = size.x;
62 int height = size.y;
63
64 if (id == -1)
65 m_windowId = NewControlId();
66 else
67 m_windowId = id;
68
69 if ( width == -1 && bitmap.Ok())
fd3f686c 70 width = bitmap.GetWidth() + 2*m_marginX;
2bda0e17
KB
71
72 if ( height == -1 && bitmap.Ok())
fd3f686c 73 height = bitmap.GetHeight() + 2*m_marginY;
2bda0e17 74
42e69d6b
VZ
75 m_hWnd = (WXHWND)CreateWindowEx
76 (
77 0,
223d09f6
KB
78 wxT("BUTTON"),
79 wxT(""),
42e69d6b
VZ
80 WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ,
81 0, 0, 0, 0,
82 GetWinHwnd(parent),
83 (HMENU)m_windowId,
84 wxGetInstance(),
85 NULL
86 );
2bda0e17
KB
87
88 // Subclass again for purposes of dialog editing mode
42e69d6b 89 SubclassWin(m_hWnd);
2bda0e17 90
c0ed460c 91 SetFont(parent->GetFont()) ;
2bda0e17
KB
92
93 SetSize(x, y, width, height);
2bda0e17
KB
94
95 return TRUE;
96}
97
98void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
99{
100 m_buttonBitmap = bitmap;
101}
102
103bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
104{
2bda0e17 105#if defined(__WIN95__)
ba681060 106 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
107 if (style & BS_BITMAP)
108 {
fd3f686c
VZ
109 // Let default procedure draw the bitmap, which is defined
110 // in the Windows resource.
111 return FALSE;
112 }
2bda0e17
KB
113#endif
114
115 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
116
fd3f686c 117 wxBitmap* bitmap = &m_buttonBitmap;
2bda0e17 118
42e69d6b
VZ
119 UINT state = lpDIS->itemState;
120 if ((state & ODS_SELECTED) && m_buttonBitmapSelected.Ok())
121 bitmap = &m_buttonBitmapSelected;
122 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
123 bitmap = &m_buttonBitmapFocus;
124 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
125 bitmap = &m_buttonBitmapDisabled;
2bda0e17 126
42e69d6b
VZ
127 if ( !bitmap->Ok() )
128 return FALSE;
2bda0e17 129
42e69d6b
VZ
130 HDC hDC = lpDIS->hDC;
131 HDC memDC = ::CreateCompatibleDC(hDC);
2bda0e17 132
42e69d6b 133 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17 134
42e69d6b
VZ
135 if (!old)
136 return FALSE;
2bda0e17 137
42e69d6b
VZ
138 int x = lpDIS->rcItem.left;
139 int y = lpDIS->rcItem.top;
140 int width = lpDIS->rcItem.right - x;
141 int height = lpDIS->rcItem.bottom - y;
2bda0e17 142
42e69d6b
VZ
143 // Draw the face, if auto-drawing
144 if ( GetWindowStyleFlag() & wxBU_AUTODRAW )
145 DrawFace((WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom,
2bda0e17
KB
146 ((state & ODS_SELECTED) == ODS_SELECTED));
147
fd3f686c
VZ
148 // Centre the bitmap in the control area
149 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
150 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
2bda0e17 151
fd3f686c
VZ
152 if ( (state & ODS_SELECTED) && (GetWindowStyleFlag() & wxBU_AUTODRAW) )
153 {
154 x1 ++;
155 y1 ++;
156 }
2bda0e17 157
4fe5383d
VZ
158 wxMask *mask = bitmap->GetMask();
159 if ( mask )
160 {
161 ::MaskBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), // dst
162 memDC, 0, 0, // src
163 (HBITMAP)mask->GetMaskBitmap(), 0, 0, // mask
164 MAKEROP4(SRCPAINT, SRCCOPY));
165 }
166 else
167 {
168 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), // dst
169 memDC, 0, 0, // src
170 SRCCOPY);
171 }
2bda0e17 172
fd3f686c
VZ
173 if ( (state & ODS_DISABLED) && (GetWindowStyleFlag() & wxBU_AUTODRAW) )
174 DrawButtonDisable( (WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, TRUE ) ;
175 else if ( (state & ODS_FOCUS) && (GetWindowStyleFlag() & wxBU_AUTODRAW) )
176 DrawButtonFocus( (WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, ((state & ODS_SELECTED) == ODS_SELECTED));
2bda0e17 177
fd3f686c 178 ::SelectObject(memDC, old);
2bda0e17
KB
179
180 ::DeleteDC(memDC);
181
182 return TRUE;
183}
184
185void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
186{
fd3f686c
VZ
187 HPEN oldp;
188 HBRUSH oldb ;
2bda0e17
KB
189
190 HPEN penBorder;
191 HPEN penLight;
192 HPEN penShadow;
193 HBRUSH brushFace;
194 COLORREF ms_color;
195
fd3f686c
VZ
196 ms_color = GetSysColor(COLOR_WINDOWFRAME) ;
197 penBorder = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 198
fd3f686c
VZ
199 ms_color = GetSysColor(COLOR_BTNSHADOW) ;
200 penShadow = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 201
fd3f686c
VZ
202 ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ;
203 penLight = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 204
fd3f686c
VZ
205 ms_color = GetSysColor(COLOR_BTNFACE) ;
206 brushFace = CreateSolidBrush(ms_color) ;
2bda0e17 207
fd3f686c
VZ
208 oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ;
209 oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ;
210 Rectangle( (HDC) dc, left, top, right, bottom ) ;
211 SelectObject( (HDC) dc, penBorder) ;
2bda0e17
KB
212 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
213 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
214 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
215 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
216
fd3f686c
VZ
217 SelectObject( (HDC) dc, penShadow) ;
218 if (sel)
219 {
220 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
221 LineTo((HDC) dc, left+1 ,top+1) ;
222 LineTo((HDC) dc, right-2 ,top+1) ;
223 }
224 else
225 {
226 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
227 LineTo((HDC) dc, right-2 ,bottom-2) ;
228 LineTo((HDC) dc, right-2 ,top) ;
229 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ;
230 LineTo((HDC) dc, right-3 ,bottom-3) ;
231 LineTo((HDC) dc, right-3 ,top+1) ;
232
233 SelectObject( (HDC) dc, penLight) ;
234
235 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
236 LineTo((HDC) dc, left+1 ,top+1) ;
237 LineTo((HDC) dc, right-2 ,top+1) ;
238 }
239 SelectObject((HDC) dc,oldp) ;
240 SelectObject((HDC) dc,oldb) ;
2bda0e17
KB
241
242 DeleteObject(penBorder);
243 DeleteObject(penLight);
244 DeleteObject(penShadow);
245 DeleteObject(brushFace);
246}
247
4fe5383d
VZ
248// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
249#define FOCUS_MARGIN 3
2bda0e17
KB
250
251void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
252{
fd3f686c
VZ
253 RECT rect;
254 rect.left = left;
255 rect.top = top;
256 rect.right = right;
257 rect.bottom = bottom;
258 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ;
259 if ( sel )
260 OffsetRect( &rect, 1, 1 ) ;
261 DrawFocusRect( (HDC) dc, &rect ) ;
2bda0e17
KB
262}
263
264extern HBRUSH wxDisableButtonBrush;
265void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
266{
fd3f686c 267 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ;
2bda0e17 268
fd3f686c
VZ
269 if ( with_marg )
270 ::PatBlt( (HDC) dc, left + m_marginX, top + m_marginY,
271 right - 2 * m_marginX, bottom - 2 * m_marginY,
ce3ed50d
JS
272#ifdef __SALFORDC__
273 0xfa0089L ) ;
274#else
275 0xfa0089UL ) ;
276#endif
277 else ::PatBlt( (HDC) dc, left, top, right, bottom,
278#ifdef __SALFORDC__
279 0xfa0089L ) ;
280#else
281 0xfa0089UL ) ;
282#endif
fd3f686c 283 ::SelectObject( (HDC) dc, old ) ;
2bda0e17
KB
284}
285
0655ad29
VZ
286void wxBitmapButton::SetDefault()
287{
288 wxButton::SetDefault();
289}