]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bmpbuttn.cpp
fix for generating set/kill focus events for wxRadioBox (also fixes TABbing in a...
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 // ----------------------------------------------------------------------------
34 // macros
35 // ----------------------------------------------------------------------------
36
37 #if wxUSE_EXTENDED_RTTI
38
39 WX_DEFINE_FLAGS( wxBitmapButtonStyle )
40
41 wxBEGIN_FLAGS( wxBitmapButtonStyle )
42 // new style border flags, we put them first to
43 // use them for streaming out
44 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
45 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
46 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
47 wxFLAGS_MEMBER(wxBORDER_RAISED)
48 wxFLAGS_MEMBER(wxBORDER_STATIC)
49 wxFLAGS_MEMBER(wxBORDER_NONE)
50
51 // old style border flags
52 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
53 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
54 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
55 wxFLAGS_MEMBER(wxRAISED_BORDER)
56 wxFLAGS_MEMBER(wxSTATIC_BORDER)
57 wxFLAGS_MEMBER(wxNO_BORDER)
58
59 // standard window styles
60 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
61 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
62 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
63 wxFLAGS_MEMBER(wxWANTS_CHARS)
64 wxFLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE)
65 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
66 wxFLAGS_MEMBER(wxVSCROLL)
67 wxFLAGS_MEMBER(wxHSCROLL)
68
69 wxFLAGS_MEMBER(wxBU_AUTODRAW)
70 wxFLAGS_MEMBER(wxBU_LEFT)
71 wxFLAGS_MEMBER(wxBU_RIGHT)
72 wxFLAGS_MEMBER(wxBU_TOP)
73 wxFLAGS_MEMBER(wxBU_BOTTOM)
74 wxEND_FLAGS( wxBitmapButtonStyle )
75
76 IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton, wxButton,"wx/bmpbuttn.h")
77
78 wxBEGIN_PROPERTIES_TABLE(wxBitmapButton)
79 wxPROPERTY_FLAGS( WindowStyle , wxBitmapButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
80 wxEND_PROPERTIES_TABLE()
81
82 wxBEGIN_HANDLERS_TABLE(wxBitmapButton)
83 wxEND_HANDLERS_TABLE()
84
85 wxCONSTRUCTOR_5( wxBitmapButton , wxWindow* , Parent , wxWindowID , Id , wxBitmap , Bitmap , wxPoint , Position , wxSize , Size )
86
87 #else
88 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
89 #endif
90
91 /*
92 TODO PROPERTIES :
93
94 long "style" , wxBU_AUTODRAW
95 bool "default" , 0
96 bitmap "selected" ,
97 bitmap "focus" ,
98 bitmap "disabled" ,
99 */
100
101 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
102
103 bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
104 const wxPoint& pos,
105 const wxSize& size, long style,
106 const wxValidator& wxVALIDATOR_PARAM(validator),
107 const wxString& name)
108 {
109 m_bmpNormal = bitmap;
110 SetName(name);
111
112 #if wxUSE_VALIDATORS
113 SetValidator(validator);
114 #endif // wxUSE_VALIDATORS
115
116 parent->AddChild(this);
117
118 m_backgroundColour = parent->GetBackgroundColour();
119 m_foregroundColour = parent->GetForegroundColour();
120 m_windowStyle = style;
121
122 if ( style & wxBU_AUTODRAW )
123 {
124 m_marginX = wxDEFAULT_BUTTON_MARGIN;
125 m_marginY = wxDEFAULT_BUTTON_MARGIN;
126 }
127
128 int x = pos.x;
129 int y = pos.y;
130 int width = size.x;
131 int height = size.y;
132
133 if (id == -1)
134 m_windowId = NewControlId();
135 else
136 m_windowId = id;
137
138 if ( width == -1 && bitmap.Ok())
139 width = bitmap.GetWidth() + 2*m_marginX;
140
141 if ( height == -1 && bitmap.Ok())
142 height = bitmap.GetHeight() + 2*m_marginY;
143
144 long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ;
145
146 if ( m_windowStyle & wxCLIP_SIBLINGS )
147 msStyle |= WS_CLIPSIBLINGS;
148
149 #ifdef __WIN32__
150 if(m_windowStyle & wxBU_LEFT)
151 msStyle |= BS_LEFT;
152 if(m_windowStyle & wxBU_RIGHT)
153 msStyle |= BS_RIGHT;
154 if(m_windowStyle & wxBU_TOP)
155 msStyle |= BS_TOP;
156 if(m_windowStyle & wxBU_BOTTOM)
157 msStyle |= BS_BOTTOM;
158 #endif
159
160 m_hWnd = (WXHWND)CreateWindowEx
161 (
162 0,
163 wxT("BUTTON"),
164 wxEmptyString,
165 msStyle,
166 0, 0, 0, 0,
167 GetWinHwnd(parent),
168 (HMENU)m_windowId,
169 wxGetInstance(),
170 NULL
171 );
172
173 // Subclass again for purposes of dialog editing mode
174 SubclassWin(m_hWnd);
175
176 SetFont(parent->GetFont());
177
178 SetSize(x, y, width, height);
179
180 return TRUE;
181 }
182
183 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
184 #define FOCUS_MARGIN 3
185
186 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
187 {
188 #ifndef __WXWINCE__
189 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
190 if (style & BS_BITMAP)
191 {
192 // Let default procedure draw the bitmap, which is defined
193 // in the Windows resource.
194 return FALSE;
195 }
196 #endif
197
198 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
199 HDC hDC = lpDIS->hDC;
200 UINT state = lpDIS->itemState;
201 bool isSelected = (state & ODS_SELECTED) != 0;
202 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
203
204
205 // choose the bitmap to use depending on the button state
206 wxBitmap* bitmap;
207
208 if ( isSelected && m_bmpSelected.Ok() )
209 bitmap = &m_bmpSelected;
210 else if ((state & ODS_FOCUS) && m_bmpFocus.Ok())
211 bitmap = &m_bmpFocus;
212 else if ((state & ODS_DISABLED) && m_bmpDisabled.Ok())
213 bitmap = &m_bmpDisabled;
214 else
215 bitmap = &m_bmpNormal;
216
217 if ( !bitmap->Ok() )
218 return FALSE;
219
220 // centre the bitmap in the control area
221 int x = lpDIS->rcItem.left;
222 int y = lpDIS->rcItem.top;
223 int width = lpDIS->rcItem.right - x;
224 int height = lpDIS->rcItem.bottom - y;
225 int wBmp = bitmap->GetWidth();
226 int hBmp = bitmap->GetHeight();
227
228 int x1,y1;
229
230 if(m_windowStyle & wxBU_LEFT)
231 x1 = x + (FOCUS_MARGIN+1);
232 else if(m_windowStyle & wxBU_RIGHT)
233 x1 = x + (width - wBmp) - (FOCUS_MARGIN+1);
234 else
235 x1 = x + (width - wBmp) / 2;
236
237 if(m_windowStyle & wxBU_TOP)
238 y1 = y + (FOCUS_MARGIN+1);
239 else if(m_windowStyle & wxBU_BOTTOM)
240 y1 = y + (height - hBmp) - (FOCUS_MARGIN+1);
241 else
242 y1 = y + (height - hBmp) / 2;
243
244 if ( isSelected && autoDraw )
245 {
246 x1++;
247 y1++;
248 }
249
250 // draw the face, if auto-drawing
251 if ( autoDraw )
252 {
253 DrawFace((WXHDC) hDC,
254 lpDIS->rcItem.left, lpDIS->rcItem.top,
255 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
256 isSelected);
257 }
258
259 // draw the bitmap
260 wxDC dst;
261 dst.SetHDC((WXHDC) hDC, FALSE);
262 dst.DrawBitmap(*bitmap, x1, y1, TRUE);
263
264 // draw focus / disabled state, if auto-drawing
265 if ( (state & ODS_DISABLED) && autoDraw )
266 {
267 DrawButtonDisable((WXHDC) hDC,
268 lpDIS->rcItem.left, lpDIS->rcItem.top,
269 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
270 TRUE);
271 }
272 else if ( (state & ODS_FOCUS) && autoDraw )
273 {
274 DrawButtonFocus((WXHDC) hDC,
275 lpDIS->rcItem.left,
276 lpDIS->rcItem.top,
277 lpDIS->rcItem.right,
278 lpDIS->rcItem.bottom,
279 isSelected);
280 }
281
282 return TRUE;
283 }
284
285 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
286
287 #if defined(__WIN95__)
288
289 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
290 {
291 HPEN oldp;
292 HPEN penHiLight;
293 HPEN penLight;
294 HPEN penShadow;
295 HPEN penDkShadow;
296 HBRUSH brushFace;
297
298 // create needed pens and brush
299 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
300 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
301 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
302 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
303 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
304 // Taking the background colour fits in better with
305 // Windows XP themes.
306 brushFace = CreateSolidBrush(m_backgroundColour.m_pixel);
307
308 // draw the rectangle
309 RECT rect;
310 rect.left = left;
311 rect.right = right;
312 rect.top = top;
313 rect.bottom = bottom;
314 FillRect((HDC) dc, &rect, brushFace);
315
316 // draw the border
317 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
318
319 wxDrawLine((HDC) dc, left, top, right-1, top);
320 wxDrawLine((HDC) dc, left, top+1, left, bottom-1);
321
322 SelectObject( (HDC) dc, sel? penShadow : penLight);
323 wxDrawLine((HDC) dc, left+1, top+1, right-2, top+1);
324 wxDrawLine((HDC) dc, left+1, top+2, left+1, bottom-2);
325
326 SelectObject( (HDC) dc, sel? penLight : penShadow);
327 wxDrawLine((HDC) dc, left+1, bottom-2, right-1, bottom-2);
328 wxDrawLine((HDC) dc, right-2, bottom-3, right-2, top);
329
330 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
331 wxDrawLine((HDC) dc, left, bottom-1, right+2, bottom-1);
332 wxDrawLine((HDC) dc, right-1, bottom-2, right-1, top-1);
333
334 // delete allocated resources
335 SelectObject((HDC) dc,oldp);
336 DeleteObject(penHiLight);
337 DeleteObject(penLight);
338 DeleteObject(penShadow);
339 DeleteObject(penDkShadow);
340 DeleteObject(brushFace);
341 }
342
343 #else
344
345 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
346 {
347 HPEN oldp;
348 HPEN penBorder;
349 HPEN penLight;
350 HPEN penShadow;
351 HBRUSH brushFace;
352
353 // create needed pens and brush
354 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
355 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
356 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
357 brushFace = CreateSolidBrush(COLOR_BTNFACE);
358
359 // draw the rectangle
360 RECT rect;
361 rect.left = left;
362 rect.right = right;
363 rect.top = top;
364 rect.bottom = bottom;
365 FillRect((HDC) dc, &rect, brushFace);
366
367 // draw the border
368 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
369 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
370 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
371 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
372 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
373
374 SelectObject( (HDC) dc, penShadow);
375 if (sel)
376 {
377 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
378 LineTo((HDC) dc, left+1 ,top+1);
379 LineTo((HDC) dc, right-2 ,top+1);
380 }
381 else
382 {
383 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
384 LineTo((HDC) dc, right-2 ,bottom-2);
385 LineTo((HDC) dc, right-2 ,top);
386
387 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
388 LineTo((HDC) dc, right-3 ,bottom-3);
389 LineTo((HDC) dc, right-3 ,top+1);
390
391 SelectObject( (HDC) dc, penLight);
392
393 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
394 LineTo((HDC) dc, left+1 ,top+1);
395 LineTo((HDC) dc, right-2 ,top+1);
396 }
397
398 // delete allocated resources
399 SelectObject((HDC) dc,oldp);
400 DeleteObject(penBorder);
401 DeleteObject(penLight);
402 DeleteObject(penShadow);
403 DeleteObject(brushFace);
404 }
405
406 #endif // defined(__WIN95__)
407
408
409 void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
410 {
411 RECT rect;
412 rect.left = left;
413 rect.top = top;
414 rect.right = right;
415 rect.bottom = bottom;
416 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
417
418 // GRG: the focus rectangle should not move when the button is pushed!
419 /*
420 if ( sel )
421 OffsetRect( &rect, 1, 1 );
422 */
423 (void)sel;
424 DrawFocusRect( (HDC) dc, &rect );
425 }
426
427 extern HBRUSH wxDisableButtonBrush;
428 void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
429 {
430 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
431
432 // VZ: what's this?? there is no such ROP AFAIK
433 #ifdef __SALFORDC__
434 DWORD dwRop = 0xFA0089L;
435 #else
436 DWORD dwRop = 0xFA0089UL;
437 #endif
438
439 if ( with_marg )
440 {
441 left += m_marginX;
442 top += m_marginY;
443 right -= 2 * m_marginX;
444 bottom -= 2 * m_marginY;
445 }
446
447 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
448
449 ::SelectObject( (HDC) dc, old );
450 }
451
452 void wxBitmapButton::SetDefault()
453 {
454 wxButton::SetDefault();
455 }
456
457 #endif // wxUSE_BMPBUTTON