]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bmpbuttn.cpp
fixing mngw32 (cross)compilation
[wxWidgets.git] / src / msw / bmpbuttn.cpp
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
31 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
32
33 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34
35 bool 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 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
91 m_hWnd = (WXHWND)CreateWindowEx
92 (
93 0,
94 wxT("BUTTON"),
95 wxT(""),
96 msStyle,
97 0, 0, 0, 0,
98 GetWinHwnd(parent),
99 (HMENU)m_windowId,
100 wxGetInstance(),
101 NULL
102 );
103
104 // Subclass again for purposes of dialog editing mode
105 SubclassWin(m_hWnd);
106
107 SetFont(parent->GetFont());
108
109 SetSize(x, y, width, height);
110
111 return TRUE;
112 }
113
114 void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
115 {
116 m_buttonBitmap = bitmap;
117 }
118
119 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
120 #define FOCUS_MARGIN 3
121
122 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
123 {
124 #if defined(__WIN95__)
125 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
126 if (style & BS_BITMAP)
127 {
128 // Let default procedure draw the bitmap, which is defined
129 // in the Windows resource.
130 return FALSE;
131 }
132 #endif
133
134 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
135 HDC hDC = lpDIS->hDC;
136 UINT state = lpDIS->itemState;
137 bool isSelected = (state & ODS_SELECTED) != 0;
138 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
139
140
141 // choose the bitmap to use depending on the button state
142 wxBitmap* bitmap;
143
144 if ( isSelected && m_buttonBitmapSelected.Ok() )
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;
150 else
151 bitmap = &m_buttonBitmap;
152
153 if ( !bitmap->Ok() )
154 return FALSE;
155
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();
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;
179
180 if ( isSelected && autoDraw )
181 {
182 x1++;
183 y1++;
184 }
185
186 // draw the face, if auto-drawing
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 }
194
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
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 }
217
218 return TRUE;
219 }
220
221 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
222
223 #if defined(__WIN95__)
224
225 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
226 {
227 HPEN oldp;
228 HPEN penHiLight;
229 HPEN penLight;
230 HPEN penShadow;
231 HPEN penDkShadow;
232 HBRUSH brushFace;
233
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
276
277 void 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;
284
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);
290
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);
298
299 // draw the border
300 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
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);
305
306 SelectObject( (HDC) dc, penShadow);
307 if (sel)
308 {
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);
312 }
313 else
314 {
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);
328 }
329
330 // delete allocated resources
331 SelectObject((HDC) dc,oldp);
332 DeleteObject(penBorder);
333 DeleteObject(penLight);
334 DeleteObject(penShadow);
335 DeleteObject(brushFace);
336 }
337
338 #endif // defined(__WIN95__)
339
340
341 void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
342 {
343 RECT rect;
344 rect.left = left;
345 rect.top = top;
346 rect.right = right;
347 rect.bottom = bottom;
348 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
349
350 // GRG: the focus rectangle should not move when the button is pushed!
351 /*
352 if ( sel )
353 OffsetRect( &rect, 1, 1 );
354 */
355 DrawFocusRect( (HDC) dc, &rect );
356 }
357
358 extern HBRUSH wxDisableButtonBrush;
359 void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
360 {
361 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
362
363 // VZ: what's this?? there is no such ROP AFAIK
364 #ifdef __SALFORDC__
365 DWORD dwRop = 0xFA0089L;
366 #else
367 DWORD dwRop = 0xFA0089UL;
368 #endif
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
380 ::SelectObject( (HDC) dc, old );
381 }
382
383 void wxBitmapButton::SetDefault()
384 {
385 wxButton::SetDefault();
386 }