]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
Added a constructor that allows creation of independent wxControl
[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"
84f19880 25 #include "wx/log.h"
2bda0e17
KB
26#endif
27
28#include "wx/msw/private.h"
29
2bda0e17 30IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
2bda0e17
KB
31
32#define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
33
debe6624 34bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
2bda0e17 35 const wxPoint& pos,
debe6624 36 const wxSize& size, long style,
2bda0e17
KB
37 const wxValidator& validator,
38 const wxString& name)
39{
40 m_buttonBitmap = bitmap;
41 SetName(name);
42 SetValidator(validator);
43
44 parent->AddChild(this);
45
fd71308f
JS
46 m_backgroundColour = parent->GetBackgroundColour() ;
47 m_foregroundColour = parent->GetForegroundColour() ;
2bda0e17
KB
48 m_windowStyle = style;
49 m_marginX = 0;
50 m_marginY = 0;
51
52 if ( style & wxBU_AUTODRAW )
53 {
fd3f686c
VZ
54 m_marginX = wxDEFAULT_BUTTON_MARGIN;
55 m_marginY = wxDEFAULT_BUTTON_MARGIN;
2bda0e17
KB
56 }
57
58 int x = pos.x;
59 int y = pos.y;
60 int width = size.x;
61 int height = size.y;
62
63 if (id == -1)
64 m_windowId = NewControlId();
65 else
66 m_windowId = id;
67
68 if ( width == -1 && bitmap.Ok())
fd3f686c 69 width = bitmap.GetWidth() + 2*m_marginX;
2bda0e17
KB
70
71 if ( height == -1 && bitmap.Ok())
fd3f686c 72 height = bitmap.GetHeight() + 2*m_marginY;
2bda0e17 73
42e69d6b
VZ
74 m_hWnd = (WXHWND)CreateWindowEx
75 (
76 0,
223d09f6
KB
77 wxT("BUTTON"),
78 wxT(""),
42e69d6b
VZ
79 WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ,
80 0, 0, 0, 0,
81 GetWinHwnd(parent),
82 (HMENU)m_windowId,
83 wxGetInstance(),
84 NULL
85 );
2bda0e17
KB
86
87 // Subclass again for purposes of dialog editing mode
42e69d6b 88 SubclassWin(m_hWnd);
2bda0e17 89
c0ed460c 90 SetFont(parent->GetFont()) ;
2bda0e17
KB
91
92 SetSize(x, y, width, height);
2bda0e17
KB
93
94 return TRUE;
95}
96
97void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
98{
99 m_buttonBitmap = bitmap;
100}
101
102bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
103{
2bda0e17 104#if defined(__WIN95__)
ba681060 105 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
106 if (style & BS_BITMAP)
107 {
fd3f686c
VZ
108 // Let default procedure draw the bitmap, which is defined
109 // in the Windows resource.
110 return FALSE;
111 }
2bda0e17
KB
112#endif
113
114 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
115
d9c8e68e
VZ
116 // choose the bitmap to use depending on the buttons state
117 wxBitmap* bitmap;
2bda0e17 118
42e69d6b 119 UINT state = lpDIS->itemState;
d9c8e68e
VZ
120 bool isSelected = (state & ODS_SELECTED) != 0;
121 if ( isSelected && m_buttonBitmapSelected.Ok() )
42e69d6b
VZ
122 bitmap = &m_buttonBitmapSelected;
123 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
124 bitmap = &m_buttonBitmapFocus;
125 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
126 bitmap = &m_buttonBitmapDisabled;
d9c8e68e
VZ
127 else
128 bitmap = &m_buttonBitmap;
2bda0e17 129
42e69d6b
VZ
130 if ( !bitmap->Ok() )
131 return FALSE;
2bda0e17 132
d9c8e68e 133 // draw it on the memory DC
42e69d6b
VZ
134 HDC hDC = lpDIS->hDC;
135 HDC memDC = ::CreateCompatibleDC(hDC);
2bda0e17 136
42e69d6b 137 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17 138
42e69d6b 139 if (!old)
d9c8e68e
VZ
140 {
141 wxLogLastError(_T("SelectObject"));
142
42e69d6b 143 return FALSE;
d9c8e68e 144 }
2bda0e17 145
42e69d6b
VZ
146 int x = lpDIS->rcItem.left;
147 int y = lpDIS->rcItem.top;
148 int width = lpDIS->rcItem.right - x;
149 int height = lpDIS->rcItem.bottom - y;
2bda0e17 150
d9c8e68e
VZ
151 int wBmp = bitmap->GetWidth(),
152 hBmp = bitmap->GetHeight();
153
42e69d6b 154 // Draw the face, if auto-drawing
d9c8e68e
VZ
155 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
156 if ( autoDraw )
157 {
158 DrawFace((WXHDC) hDC,
159 lpDIS->rcItem.left, lpDIS->rcItem.top,
160 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
161 isSelected);
162 }
2bda0e17 163
fd3f686c 164 // Centre the bitmap in the control area
d9c8e68e
VZ
165 int x1 = x + (width - wBmp) / 2;
166 int y1 = y + (height - hBmp) / 2;
2bda0e17 167
d9c8e68e 168 if ( isSelected && autoDraw )
fd3f686c 169 {
d9c8e68e
VZ
170 x1++;
171 y1++;
fd3f686c 172 }
2bda0e17 173
d9c8e68e 174 BOOL ok;
8f177c8e
VZ
175
176 // no MaskBlt() under Win16
177#ifdef __WIN32__
4fe5383d
VZ
178 wxMask *mask = bitmap->GetMask();
179 if ( mask )
180 {
d9c8e68e
VZ
181 // the fg ROP is applied for the pixels of the mask bitmap which are 1
182 // (for a wxMask this means that this is a non transparent pixel), the
183 // bg ROP is applied for all the others
2e8005cc
VZ
184
185 wxColour colBg = GetBackgroundColour();
186 HBRUSH hbrBackground =
187 ::CreateSolidBrush(RGB(colBg.Red(), colBg.Green(), colBg.Blue()));
188 HBRUSH hbrOld = (HBRUSH)::SelectObject(hDC, hbrBackground);
189
d9c8e68e
VZ
190 ok = ::MaskBlt(
191 hDC, x1, y1, wBmp, hBmp, // dst
192 memDC, 0, 0, // src
193 (HBITMAP)mask->GetMaskBitmap(), 0, 0, // mask
194 MAKEROP4(SRCCOPY, // fg ROP
2e8005cc 195 PATCOPY) // bg ROP
d9c8e68e 196 );
2e8005cc
VZ
197
198 ::SelectObject(hDC, hbrOld);
199 ::DeleteObject(hbrBackground);
4fe5383d
VZ
200 }
201 else
8f177c8e 202#endif // Win32
4fe5383d 203 {
d9c8e68e
VZ
204 ok = ::BitBlt(hDC, x1, y1, wBmp, hBmp, // dst
205 memDC, 0, 0, // src
206 SRCCOPY); // ROP
207 }
208
209 if ( !ok )
210 {
211 wxLogLastError(_T("Mask/BitBlt()"));
4fe5383d 212 }
2bda0e17 213
d9c8e68e
VZ
214 if ( (state & ODS_DISABLED) && autoDraw )
215 {
216 DrawButtonDisable((WXHDC) hDC,
217 lpDIS->rcItem.left, lpDIS->rcItem.top,
218 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
219 TRUE);
220 }
221 else if ( (state & ODS_FOCUS) && autoDraw )
222 {
223 DrawButtonFocus((WXHDC) hDC,
224 lpDIS->rcItem.left,
225 lpDIS->rcItem.top,
226 lpDIS->rcItem.right,
227 lpDIS->rcItem.bottom,
228 isSelected);
229 }
2bda0e17 230
fd3f686c 231 ::SelectObject(memDC, old);
2bda0e17
KB
232
233 ::DeleteDC(memDC);
234
235 return TRUE;
236}
237
238void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
239{
d9c8e68e
VZ
240 HPEN oldp;
241 HBRUSH oldb ;
2bda0e17 242
d9c8e68e
VZ
243 HPEN penBorder;
244 HPEN penLight;
245 HPEN penShadow;
246 HBRUSH brushFace;
247 COLORREF ms_color;
2bda0e17 248
fd3f686c
VZ
249 ms_color = GetSysColor(COLOR_WINDOWFRAME) ;
250 penBorder = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 251
fd3f686c
VZ
252 ms_color = GetSysColor(COLOR_BTNSHADOW) ;
253 penShadow = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 254
fd3f686c
VZ
255 ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ;
256 penLight = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 257
fd3f686c
VZ
258 ms_color = GetSysColor(COLOR_BTNFACE) ;
259 brushFace = CreateSolidBrush(ms_color) ;
2bda0e17 260
fd3f686c
VZ
261 oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ;
262 oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ;
263 Rectangle( (HDC) dc, left, top, right, bottom ) ;
264 SelectObject( (HDC) dc, penBorder) ;
d9c8e68e
VZ
265 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
266 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
267 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
268 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
2bda0e17 269
fd3f686c
VZ
270 SelectObject( (HDC) dc, penShadow) ;
271 if (sel)
272 {
273 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
274 LineTo((HDC) dc, left+1 ,top+1) ;
275 LineTo((HDC) dc, right-2 ,top+1) ;
276 }
277 else
278 {
279 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
280 LineTo((HDC) dc, right-2 ,bottom-2) ;
281 LineTo((HDC) dc, right-2 ,top) ;
282 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ;
283 LineTo((HDC) dc, right-3 ,bottom-3) ;
284 LineTo((HDC) dc, right-3 ,top+1) ;
285
286 SelectObject( (HDC) dc, penLight) ;
287
288 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
289 LineTo((HDC) dc, left+1 ,top+1) ;
290 LineTo((HDC) dc, right-2 ,top+1) ;
291 }
292 SelectObject((HDC) dc,oldp) ;
293 SelectObject((HDC) dc,oldb) ;
2bda0e17 294
d9c8e68e
VZ
295 DeleteObject(penBorder);
296 DeleteObject(penLight);
297 DeleteObject(penShadow);
298 DeleteObject(brushFace);
2bda0e17
KB
299}
300
4fe5383d
VZ
301// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
302#define FOCUS_MARGIN 3
2bda0e17
KB
303
304void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
305{
fd3f686c
VZ
306 RECT rect;
307 rect.left = left;
308 rect.top = top;
309 rect.right = right;
310 rect.bottom = bottom;
311 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ;
312 if ( sel )
313 OffsetRect( &rect, 1, 1 ) ;
314 DrawFocusRect( (HDC) dc, &rect ) ;
2bda0e17
KB
315}
316
317extern HBRUSH wxDisableButtonBrush;
318void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
319{
fd3f686c 320 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ;
2bda0e17 321
d9c8e68e 322 // VZ: what's this?? there is no such ROP AFAIK
ce3ed50d 323#ifdef __SALFORDC__
d9c8e68e 324 DWORD dwRop = 0xFA0089L;
ce3ed50d 325#else
d9c8e68e 326 DWORD dwRop = 0xFA0089UL;
ce3ed50d 327#endif
d9c8e68e
VZ
328
329 if ( with_marg )
330 {
331 left += m_marginX;
332 top += m_marginY;
333 right -= 2 * m_marginX;
334 bottom -= 2 * m_marginY;
335 }
336
337 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
338
fd3f686c 339 ::SelectObject( (HDC) dc, old ) ;
2bda0e17
KB
340}
341
0655ad29
VZ
342void wxBitmapButton::SetDefault()
343{
344 wxButton::SetDefault();
345}