]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/bmpbuttn.cpp
Funny resize behaviour fix.
[wxWidgets.git] / src / msw / bmpbuttn.cpp
... / ...
CommitLineData
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
9// Licence: wxWindows license
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
24 #include "wx/bmpbuttn.h"
25 #include "wx/log.h"
26 #include "wx/dcmemory.h"
27#endif
28
29#include "wx/msw/private.h"
30
31IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
32
33#define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34
35bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
36 const wxPoint& pos,
37 const wxSize& size, long style,
38 const wxValidator& validator,
39 const wxString& name)
40{
41 m_buttonBitmap = bitmap;
42 SetName(name);
43
44#if wxUSE_VALIDATORS
45 SetValidator(validator);
46#endif // wxUSE_VALIDATORS
47
48 parent->AddChild(this);
49
50 m_backgroundColour = parent->GetBackgroundColour();
51 m_foregroundColour = parent->GetForegroundColour();
52 m_windowStyle = style;
53 m_marginX = 0;
54 m_marginY = 0;
55
56 if ( style & wxBU_AUTODRAW )
57 {
58 m_marginX = wxDEFAULT_BUTTON_MARGIN;
59 m_marginY = wxDEFAULT_BUTTON_MARGIN;
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())
73 width = bitmap.GetWidth() + 2*m_marginX;
74
75 if ( height == -1 && bitmap.Ok())
76 height = bitmap.GetHeight() + 2*m_marginY;
77
78 m_hWnd = (WXHWND)CreateWindowEx
79 (
80 0,
81 wxT("BUTTON"),
82 wxT(""),
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 );
90
91 // Subclass again for purposes of dialog editing mode
92 SubclassWin(m_hWnd);
93
94 SetFont(parent->GetFont());
95
96 SetSize(x, y, width, height);
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{
108#if defined(__WIN95__)
109 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
110 if (style & BS_BITMAP)
111 {
112 // Let default procedure draw the bitmap, which is defined
113 // in the Windows resource.
114 return FALSE;
115 }
116#endif
117
118 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
119 HDC hDC = lpDIS->hDC;
120 UINT state = lpDIS->itemState;
121 bool isSelected = (state & ODS_SELECTED) != 0;
122 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
123
124
125 // choose the bitmap to use depending on the button state
126 wxBitmap* bitmap;
127
128 if ( isSelected && m_buttonBitmapSelected.Ok() )
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;
134 else
135 bitmap = &m_buttonBitmap;
136
137 if ( !bitmap->Ok() )
138 return FALSE;
139
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;
149
150 if ( isSelected && autoDraw )
151 {
152 x1++;
153 y1++;
154 }
155
156 // draw the face, if auto-drawing
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 }
164
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
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 }
187
188 return TRUE;
189}
190
191// GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
192
193#if defined(__WIN95__)
194
195void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
196{
197 HPEN oldp;
198 HPEN penHiLight;
199 HPEN penLight;
200 HPEN penShadow;
201 HPEN penDkShadow;
202 HBRUSH brushFace;
203
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
246
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;
254
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);
260
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);
268
269 // draw the border
270 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
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);
275
276 SelectObject( (HDC) dc, penShadow);
277 if (sel)
278 {
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);
282 }
283 else
284 {
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);
298 }
299
300 // delete allocated resources
301 SelectObject((HDC) dc,oldp);
302 DeleteObject(penBorder);
303 DeleteObject(penLight);
304 DeleteObject(penShadow);
305 DeleteObject(brushFace);
306}
307
308#endif // defined(__WIN95__)
309
310
311// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
312#define FOCUS_MARGIN 3
313
314void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
315{
316 RECT rect;
317 rect.left = left;
318 rect.top = top;
319 rect.right = right;
320 rect.bottom = bottom;
321 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
322
323 // GRG: the focus rectangle should not move when the button is pushed!
324/*
325 if ( sel )
326 OffsetRect( &rect, 1, 1 );
327*/
328 DrawFocusRect( (HDC) dc, &rect );
329}
330
331extern HBRUSH wxDisableButtonBrush;
332void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
333{
334 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
335
336 // VZ: what's this?? there is no such ROP AFAIK
337#ifdef __SALFORDC__
338 DWORD dwRop = 0xFA0089L;
339#else
340 DWORD dwRop = 0xFA0089UL;
341#endif
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
353 ::SelectObject( (HDC) dc, old );
354}
355
356void wxBitmapButton::SetDefault()
357{
358 wxButton::SetDefault();
359}