]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
Fixed crash-on-exit bug due to status bar being deleted twice (MDI apps);
[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"
47e92c41 26 #include "wx/dcmemory.h"
2bda0e17
KB
27#endif
28
29#include "wx/msw/private.h"
30
2bda0e17 31IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
2bda0e17
KB
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);
11b6a93b
VZ
43
44#if wxUSE_VALIDATORS
2bda0e17 45 SetValidator(validator);
11b6a93b 46#endif // wxUSE_VALIDATORS
2bda0e17
KB
47
48 parent->AddChild(this);
49
5ab1fa8e
GRG
50 m_backgroundColour = parent->GetBackgroundColour();
51 m_foregroundColour = parent->GetForegroundColour();
2bda0e17
KB
52 m_windowStyle = style;
53 m_marginX = 0;
54 m_marginY = 0;
55
56 if ( style & wxBU_AUTODRAW )
57 {
fd3f686c
VZ
58 m_marginX = wxDEFAULT_BUTTON_MARGIN;
59 m_marginY = wxDEFAULT_BUTTON_MARGIN;
2bda0e17
KB
60 }
61
62 int x = pos.x;
63 int y = pos.y;
64 int width = size.x;
65 int height = size.y;
66
67 if (id == -1)
68 m_windowId = NewControlId();
69 else
70 m_windowId = id;
71
72 if ( width == -1 && bitmap.Ok())
fd3f686c 73 width = bitmap.GetWidth() + 2*m_marginX;
2bda0e17
KB
74
75 if ( height == -1 && bitmap.Ok())
fd3f686c 76 height = bitmap.GetHeight() + 2*m_marginY;
2bda0e17 77
42e69d6b
VZ
78 m_hWnd = (WXHWND)CreateWindowEx
79 (
80 0,
223d09f6
KB
81 wxT("BUTTON"),
82 wxT(""),
42e69d6b
VZ
83 WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ,
84 0, 0, 0, 0,
85 GetWinHwnd(parent),
86 (HMENU)m_windowId,
87 wxGetInstance(),
88 NULL
89 );
2bda0e17
KB
90
91 // Subclass again for purposes of dialog editing mode
42e69d6b 92 SubclassWin(m_hWnd);
2bda0e17 93
5ab1fa8e 94 SetFont(parent->GetFont());
2bda0e17
KB
95
96 SetSize(x, y, width, height);
2bda0e17
KB
97
98 return TRUE;
99}
100
101void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
102{
103 m_buttonBitmap = bitmap;
104}
105
106bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
107{
2bda0e17 108#if defined(__WIN95__)
ba681060 109 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
110 if (style & BS_BITMAP)
111 {
fd3f686c
VZ
112 // Let default procedure draw the bitmap, which is defined
113 // in the Windows resource.
114 return FALSE;
115 }
2bda0e17
KB
116#endif
117
118 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
47e92c41
GRG
119 HDC hDC = lpDIS->hDC;
120 UINT state = lpDIS->itemState;
121 bool isSelected = (state & ODS_SELECTED) != 0;
122 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
2bda0e17 123
47e92c41
GRG
124
125 // choose the bitmap to use depending on the button state
d9c8e68e 126 wxBitmap* bitmap;
2bda0e17 127
d9c8e68e 128 if ( isSelected && m_buttonBitmapSelected.Ok() )
42e69d6b
VZ
129 bitmap = &m_buttonBitmapSelected;
130 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
131 bitmap = &m_buttonBitmapFocus;
132 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
133 bitmap = &m_buttonBitmapDisabled;
d9c8e68e
VZ
134 else
135 bitmap = &m_buttonBitmap;
2bda0e17 136
42e69d6b
VZ
137 if ( !bitmap->Ok() )
138 return FALSE;
2bda0e17 139
47e92c41
GRG
140 // centre the bitmap in the control area
141 int x = lpDIS->rcItem.left;
142 int y = lpDIS->rcItem.top;
143 int width = lpDIS->rcItem.right - x;
144 int height = lpDIS->rcItem.bottom - y;
145 int wBmp = bitmap->GetWidth();
146 int hBmp = bitmap->GetHeight();
147 int x1 = x + (width - wBmp) / 2;
148 int y1 = y + (height - hBmp) / 2;
2bda0e17 149
47e92c41 150 if ( isSelected && autoDraw )
d9c8e68e 151 {
47e92c41
GRG
152 x1++;
153 y1++;
d9c8e68e 154 }
2bda0e17 155
47e92c41 156 // draw the face, if auto-drawing
d9c8e68e
VZ
157 if ( autoDraw )
158 {
159 DrawFace((WXHDC) hDC,
160 lpDIS->rcItem.left, lpDIS->rcItem.top,
161 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
162 isSelected);
163 }
2bda0e17 164
47e92c41
GRG
165 // draw the bitmap
166 wxDC dst;
167 dst.SetHDC((WXHDC) hDC, FALSE);
168 dst.DrawBitmap(*bitmap, x1, y1, TRUE);
169
170 // draw focus / disabled state, if auto-drawing
d9c8e68e
VZ
171 if ( (state & ODS_DISABLED) && autoDraw )
172 {
173 DrawButtonDisable((WXHDC) hDC,
174 lpDIS->rcItem.left, lpDIS->rcItem.top,
175 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
176 TRUE);
177 }
178 else if ( (state & ODS_FOCUS) && autoDraw )
179 {
180 DrawButtonFocus((WXHDC) hDC,
181 lpDIS->rcItem.left,
182 lpDIS->rcItem.top,
183 lpDIS->rcItem.right,
184 lpDIS->rcItem.bottom,
185 isSelected);
186 }
2bda0e17 187
2bda0e17
KB
188 return TRUE;
189}
190
5ab1fa8e
GRG
191// GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
192
193#if defined(__WIN95__)
194
2bda0e17
KB
195void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
196{
d9c8e68e 197 HPEN oldp;
5ab1fa8e 198 HPEN penHiLight;
d9c8e68e
VZ
199 HPEN penLight;
200 HPEN penShadow;
5ab1fa8e 201 HPEN penDkShadow;
d9c8e68e 202 HBRUSH brushFace;
2bda0e17 203
5ab1fa8e
GRG
204 // create needed pens and brush
205 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
206 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
207 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
208 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
209 brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
210
211 // draw the rectangle
212 RECT rect;
213 rect.left = left;
214 rect.right = right;
215 rect.top = top;
216 rect.bottom = bottom;
217 FillRect((HDC) dc, &rect, brushFace);
218
219 // draw the border
220 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
221 MoveToEx((HDC) dc, left, top, NULL); LineTo((HDC) dc, right-1, top);
222 MoveToEx((HDC) dc, left, top+1, NULL); LineTo((HDC) dc, left, bottom-1);
223
224 SelectObject( (HDC) dc, sel? penShadow : penLight);
225 MoveToEx((HDC) dc, left+1, top+1, NULL); LineTo((HDC) dc, right-2, top+1);
226 MoveToEx((HDC) dc, left+1, top+2, NULL); LineTo((HDC) dc, left+1, bottom-2);
227
228 SelectObject( (HDC) dc, sel? penLight : penShadow);
229 MoveToEx((HDC) dc, left+1, bottom-2, NULL); LineTo((HDC) dc, right-1, bottom-2);
230 MoveToEx((HDC) dc, right-2, bottom-3, NULL); LineTo((HDC) dc, right-2, top);
231
232 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
233 MoveToEx((HDC) dc, left, bottom-1, NULL); LineTo((HDC) dc, right+2, bottom-1);
234 MoveToEx((HDC) dc, right-1, bottom-2, NULL); LineTo((HDC) dc, right-1, top-1);
235
236 // delete allocated resources
237 SelectObject((HDC) dc,oldp);
238 DeleteObject(penHiLight);
239 DeleteObject(penLight);
240 DeleteObject(penShadow);
241 DeleteObject(penDkShadow);
242 DeleteObject(brushFace);
243}
244
245#else
2bda0e17 246
5ab1fa8e
GRG
247void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
248{
249 HPEN oldp;
250 HPEN penBorder;
251 HPEN penLight;
252 HPEN penShadow;
253 HBRUSH brushFace;
2bda0e17 254
5ab1fa8e
GRG
255 // create needed pens and brush
256 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
257 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
258 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
259 brushFace = CreateSolidBrush(COLOR_BTNFACE);
2bda0e17 260
5ab1fa8e
GRG
261 // draw the rectangle
262 RECT rect;
263 rect.left = left;
264 rect.right = right;
265 rect.top = top;
266 rect.bottom = bottom;
267 FillRect((HDC) dc, &rect, brushFace);
2bda0e17 268
5ab1fa8e
GRG
269 // draw the border
270 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
d9c8e68e
VZ
271 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
272 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
273 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
274 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
2bda0e17 275
5ab1fa8e 276 SelectObject( (HDC) dc, penShadow);
fd3f686c
VZ
277 if (sel)
278 {
5ab1fa8e
GRG
279 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
280 LineTo((HDC) dc, left+1 ,top+1);
281 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c
VZ
282 }
283 else
284 {
5ab1fa8e
GRG
285 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
286 LineTo((HDC) dc, right-2 ,bottom-2);
287 LineTo((HDC) dc, right-2 ,top);
288
289 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
290 LineTo((HDC) dc, right-3 ,bottom-3);
291 LineTo((HDC) dc, right-3 ,top+1);
292
293 SelectObject( (HDC) dc, penLight);
294
295 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
296 LineTo((HDC) dc, left+1 ,top+1);
297 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c 298 }
2bda0e17 299
5ab1fa8e
GRG
300 // delete allocated resources
301 SelectObject((HDC) dc,oldp);
d9c8e68e
VZ
302 DeleteObject(penBorder);
303 DeleteObject(penLight);
304 DeleteObject(penShadow);
305 DeleteObject(brushFace);
2bda0e17
KB
306}
307
5ab1fa8e
GRG
308#endif // defined(__WIN95__)
309
310
4fe5383d
VZ
311// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
312#define FOCUS_MARGIN 3
2bda0e17
KB
313
314void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
315{
fd3f686c
VZ
316 RECT rect;
317 rect.left = left;
318 rect.top = top;
319 rect.right = right;
320 rect.bottom = bottom;
5ab1fa8e
GRG
321 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
322
323 // GRG: the focus rectangle should not move when the button is pushed!
324/*
fd3f686c 325 if ( sel )
5ab1fa8e
GRG
326 OffsetRect( &rect, 1, 1 );
327*/
328 DrawFocusRect( (HDC) dc, &rect );
2bda0e17
KB
329}
330
331extern HBRUSH wxDisableButtonBrush;
332void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
333{
5ab1fa8e 334 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
2bda0e17 335
d9c8e68e 336 // VZ: what's this?? there is no such ROP AFAIK
ce3ed50d 337#ifdef __SALFORDC__
d9c8e68e 338 DWORD dwRop = 0xFA0089L;
ce3ed50d 339#else
d9c8e68e 340 DWORD dwRop = 0xFA0089UL;
ce3ed50d 341#endif
d9c8e68e
VZ
342
343 if ( with_marg )
344 {
345 left += m_marginX;
346 top += m_marginY;
347 right -= 2 * m_marginX;
348 bottom -= 2 * m_marginY;
349 }
350
351 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
352
5ab1fa8e 353 ::SelectObject( (HDC) dc, old );
2bda0e17
KB
354}
355
0655ad29
VZ
356void wxBitmapButton::SetDefault()
357{
358 wxButton::SetDefault();
359}