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