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