]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
wxCocoa: Better icon support
[wxWidgets.git] / src / msw / bmpbuttn.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/msw/bmpbuttn.cpp
2bda0e17
KB
3// Purpose: wxBitmapButton
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
2bda0e17
KB
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__
1e6feb95 20 #pragma hdrstop
2bda0e17
KB
21#endif
22
1e6feb95
VZ
23#if wxUSE_BMPBUTTON
24
2bda0e17 25#ifndef WX_PRECOMP
10a0bdb1 26 #include "wx/bmpbuttn.h"
84f19880 27 #include "wx/log.h"
47e92c41 28 #include "wx/dcmemory.h"
2bda0e17
KB
29#endif
30
31#include "wx/msw/private.h"
32
bc9fb572
JS
33// ----------------------------------------------------------------------------
34// macros
35// ----------------------------------------------------------------------------
36
37#if wxUSE_EXTENDED_RTTI
38
39WX_DEFINE_FLAGS( wxBitmapButtonStyle )
40
3ff066a4 41wxBEGIN_FLAGS( wxBitmapButtonStyle )
bc9fb572
JS
42 // new style border flags, we put them first to
43 // use them for streaming out
3ff066a4
SC
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)
bc9fb572
JS
50
51 // old style border flags
3ff066a4
SC
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)
cb0afb26 57 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
58
59 // standard window styles
3ff066a4
SC
60 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
61 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
62 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
63 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 64 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
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)
74wxEND_FLAGS( wxBitmapButtonStyle )
bc9fb572
JS
75
76IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton, wxButton,"wx/bmpbuttn.h")
77
3ff066a4
SC
78wxBEGIN_PROPERTIES_TABLE(wxBitmapButton)
79 wxPROPERTY_FLAGS( WindowStyle , wxBitmapButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
80wxEND_PROPERTIES_TABLE()
bc9fb572 81
3ff066a4
SC
82wxBEGIN_HANDLERS_TABLE(wxBitmapButton)
83wxEND_HANDLERS_TABLE()
bc9fb572 84
3ff066a4 85wxCONSTRUCTOR_5( wxBitmapButton , wxWindow* , Parent , wxWindowID , Id , wxBitmap , Bitmap , wxPoint , Position , wxSize , Size )
bc9fb572
JS
86
87#else
2bda0e17 88IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
bc9fb572 89#endif
2bda0e17 90
066f1b7a
SC
91/*
92TODO PROPERTIES :
93
94long "style" , wxBU_AUTODRAW
95bool "default" , 0
96bitmap "selected" ,
97bitmap "focus" ,
98bitmap "disabled" ,
99*/
100
2bda0e17
KB
101#define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
102
a265d21a
DS
103bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id,
104 const wxBitmap& bitmap,
105 const wxPoint& pos,
106 const wxSize& size, long style,
107 const wxValidator& wxVALIDATOR_PARAM(validator),
108 const wxString& name)
2bda0e17 109{
a265d21a
DS
110 m_bmpNormal = bitmap;
111 SetName(name);
11b6a93b
VZ
112
113#if wxUSE_VALIDATORS
a265d21a 114 SetValidator(validator);
11b6a93b 115#endif // wxUSE_VALIDATORS
2bda0e17 116
a265d21a 117 parent->AddChild(this);
2bda0e17 118
a265d21a
DS
119 m_backgroundColour = parent->GetBackgroundColour();
120 m_foregroundColour = parent->GetForegroundColour();
121 m_windowStyle = style;
2bda0e17 122
a265d21a
DS
123 if ( style & wxBU_AUTODRAW )
124 {
125 m_marginX = wxDEFAULT_BUTTON_MARGIN;
126 m_marginY = wxDEFAULT_BUTTON_MARGIN;
127 }
2bda0e17 128
a265d21a
DS
129 int x = pos.x;
130 int y = pos.y;
131 int width = size.x;
132 int height = size.y;
2bda0e17 133
a265d21a
DS
134 if (id == -1)
135 m_windowId = NewControlId();
136 else
137 m_windowId = id;
2bda0e17 138
a265d21a
DS
139 if ( width == -1 && bitmap.Ok())
140 width = bitmap.GetWidth() + 2*m_marginX;
2bda0e17 141
a265d21a
DS
142 if ( height == -1 && bitmap.Ok())
143 height = bitmap.GetHeight() + 2*m_marginY;
2bda0e17 144
a265d21a 145 long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ;
f6bcfd97 146
b0766406
JS
147 if ( m_windowStyle & wxCLIP_SIBLINGS )
148 msStyle |= WS_CLIPSIBLINGS;
149
f6bcfd97
BP
150#ifdef __WIN32__
151 if(m_windowStyle & wxBU_LEFT)
152 msStyle |= BS_LEFT;
153 if(m_windowStyle & wxBU_RIGHT)
154 msStyle |= BS_RIGHT;
155 if(m_windowStyle & wxBU_TOP)
156 msStyle |= BS_TOP;
157 if(m_windowStyle & wxBU_BOTTOM)
158 msStyle |= BS_BOTTOM;
159#endif
160
a265d21a 161 m_hWnd = (WXHWND) CreateWindowEx(
42e69d6b 162 0,
223d09f6 163 wxT("BUTTON"),
fda7962d 164 wxEmptyString,
f6bcfd97 165 msStyle,
33ac7e6f 166 0, 0, 0, 0,
42e69d6b
VZ
167 GetWinHwnd(parent),
168 (HMENU)m_windowId,
169 wxGetInstance(),
170 NULL
171 );
2bda0e17 172
a265d21a
DS
173 // Subclass again for purposes of dialog editing mode
174 SubclassWin(m_hWnd);
2bda0e17 175
a265d21a 176 SetFont(parent->GetFont());
2bda0e17 177
a265d21a 178 SetSize(x, y, width, height);
2bda0e17 179
a265d21a 180 return true;
2bda0e17
KB
181}
182
f6bcfd97
BP
183// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
184#define FOCUS_MARGIN 3
185
2bda0e17
KB
186bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
187{
4676948b 188#ifndef __WXWINCE__
ba681060 189 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
190 if (style & BS_BITMAP)
191 {
fd3f686c
VZ
192 // Let default procedure draw the bitmap, which is defined
193 // in the Windows resource.
a265d21a 194 return false;
fd3f686c 195 }
2bda0e17
KB
196#endif
197
198 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
47e92c41
GRG
199 HDC hDC = lpDIS->hDC;
200 UINT state = lpDIS->itemState;
201 bool isSelected = (state & ODS_SELECTED) != 0;
202 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
2bda0e17 203
47e92c41
GRG
204
205 // choose the bitmap to use depending on the button state
d9c8e68e 206 wxBitmap* bitmap;
2bda0e17 207
1e6feb95
VZ
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;
d9c8e68e 214 else
1e6feb95 215 bitmap = &m_bmpNormal;
2bda0e17 216
42e69d6b 217 if ( !bitmap->Ok() )
a265d21a 218 return false;
2bda0e17 219
47e92c41
GRG
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();
f6bcfd97 227
a265d21a
DS
228 int x1,y1;
229
f6bcfd97
BP
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;
2bda0e17 243
47e92c41 244 if ( isSelected && autoDraw )
d9c8e68e 245 {
47e92c41
GRG
246 x1++;
247 y1++;
d9c8e68e 248 }
2bda0e17 249
47e92c41 250 // draw the face, if auto-drawing
d9c8e68e
VZ
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 }
2bda0e17 258
47e92c41
GRG
259 // draw the bitmap
260 wxDC dst;
a265d21a
DS
261 dst.SetHDC((WXHDC) hDC, false);
262 dst.DrawBitmap(*bitmap, x1, y1, true);
33ac7e6f 263
47e92c41 264 // draw focus / disabled state, if auto-drawing
d9c8e68e
VZ
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,
a265d21a 270 true);
d9c8e68e
VZ
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 }
2bda0e17 281
a265d21a 282 return true;
2bda0e17
KB
283}
284
5ab1fa8e
GRG
285// GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
286
287#if defined(__WIN95__)
288
a265d21a
DS
289void wxBitmapButton::DrawFace( WXHDC dc, int left, int top,
290 int right, int bottom, bool sel )
2bda0e17 291{
d9c8e68e 292 HPEN oldp;
5ab1fa8e 293 HPEN penHiLight;
d9c8e68e
VZ
294 HPEN penLight;
295 HPEN penShadow;
5ab1fa8e 296 HPEN penDkShadow;
d9c8e68e 297 HBRUSH brushFace;
2bda0e17 298
5ab1fa8e
GRG
299 // create needed pens and brush
300 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
301 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
302 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
303 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
85b43fbf
JS
304 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
305 // Taking the background colour fits in better with
306 // Windows XP themes.
307 brushFace = CreateSolidBrush(m_backgroundColour.m_pixel);
5ab1fa8e
GRG
308
309 // draw the rectangle
310 RECT rect;
311 rect.left = left;
312 rect.right = right;
313 rect.top = top;
314 rect.bottom = bottom;
315 FillRect((HDC) dc, &rect, brushFace);
316
317 // draw the border
318 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
4676948b
JS
319
320 wxDrawLine((HDC) dc, left, top, right-1, top);
321 wxDrawLine((HDC) dc, left, top+1, left, bottom-1);
5ab1fa8e
GRG
322
323 SelectObject( (HDC) dc, sel? penShadow : penLight);
4676948b
JS
324 wxDrawLine((HDC) dc, left+1, top+1, right-2, top+1);
325 wxDrawLine((HDC) dc, left+1, top+2, left+1, bottom-2);
5ab1fa8e
GRG
326
327 SelectObject( (HDC) dc, sel? penLight : penShadow);
4676948b
JS
328 wxDrawLine((HDC) dc, left+1, bottom-2, right-1, bottom-2);
329 wxDrawLine((HDC) dc, right-2, bottom-3, right-2, top);
5ab1fa8e
GRG
330
331 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
4676948b
JS
332 wxDrawLine((HDC) dc, left, bottom-1, right+2, bottom-1);
333 wxDrawLine((HDC) dc, right-1, bottom-2, right-1, top-1);
5ab1fa8e
GRG
334
335 // delete allocated resources
336 SelectObject((HDC) dc,oldp);
337 DeleteObject(penHiLight);
338 DeleteObject(penLight);
339 DeleteObject(penShadow);
340 DeleteObject(penDkShadow);
341 DeleteObject(brushFace);
342}
343
344#else
2bda0e17 345
a265d21a
DS
346void wxBitmapButton::DrawFace( WXHDC dc, int left, int top,
347 int right, int bottom, bool sel )
5ab1fa8e
GRG
348{
349 HPEN oldp;
350 HPEN penBorder;
351 HPEN penLight;
352 HPEN penShadow;
353 HBRUSH brushFace;
2bda0e17 354
5ab1fa8e
GRG
355 // create needed pens and brush
356 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
357 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
358 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
359 brushFace = CreateSolidBrush(COLOR_BTNFACE);
2bda0e17 360
5ab1fa8e
GRG
361 // draw the rectangle
362 RECT rect;
363 rect.left = left;
364 rect.right = right;
365 rect.top = top;
366 rect.bottom = bottom;
367 FillRect((HDC) dc, &rect, brushFace);
2bda0e17 368
5ab1fa8e
GRG
369 // draw the border
370 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
d9c8e68e
VZ
371 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
372 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
373 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
374 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
2bda0e17 375
5ab1fa8e 376 SelectObject( (HDC) dc, penShadow);
fd3f686c
VZ
377 if (sel)
378 {
5ab1fa8e
GRG
379 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
380 LineTo((HDC) dc, left+1 ,top+1);
381 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c
VZ
382 }
383 else
384 {
5ab1fa8e
GRG
385 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
386 LineTo((HDC) dc, right-2 ,bottom-2);
387 LineTo((HDC) dc, right-2 ,top);
388
389 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
390 LineTo((HDC) dc, right-3 ,bottom-3);
391 LineTo((HDC) dc, right-3 ,top+1);
392
393 SelectObject( (HDC) dc, penLight);
394
395 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
396 LineTo((HDC) dc, left+1 ,top+1);
397 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c 398 }
2bda0e17 399
5ab1fa8e
GRG
400 // delete allocated resources
401 SelectObject((HDC) dc,oldp);
d9c8e68e
VZ
402 DeleteObject(penBorder);
403 DeleteObject(penLight);
404 DeleteObject(penShadow);
405 DeleteObject(brushFace);
2bda0e17
KB
406}
407
5ab1fa8e
GRG
408#endif // defined(__WIN95__)
409
410
a265d21a
DS
411void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right,
412 int bottom, bool WXUNUSED(sel) )
2bda0e17 413{
fd3f686c
VZ
414 RECT rect;
415 rect.left = left;
416 rect.top = top;
417 rect.right = right;
418 rect.bottom = bottom;
5ab1fa8e
GRG
419 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
420
421 // GRG: the focus rectangle should not move when the button is pushed!
422/*
fd3f686c 423 if ( sel )
5ab1fa8e
GRG
424 OffsetRect( &rect, 1, 1 );
425*/
a265d21a 426
5ab1fa8e 427 DrawFocusRect( (HDC) dc, &rect );
2bda0e17
KB
428}
429
430extern HBRUSH wxDisableButtonBrush;
a265d21a
DS
431void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right,
432 int bottom, bool with_marg )
2bda0e17 433{
5ab1fa8e 434 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
2bda0e17 435
2b268535
VZ
436 // ROP for "dest |= pattern" operation -- as it doesn't have a standard
437 // name, give it our own
a265d21a 438 static const DWORD PATTERNPAINT = 0xFA0089UL;
d9c8e68e
VZ
439
440 if ( with_marg )
441 {
442 left += m_marginX;
443 top += m_marginY;
444 right -= 2 * m_marginX;
445 bottom -= 2 * m_marginY;
446 }
447
a265d21a 448 ::PatBlt( (HDC) dc, left, top, right, bottom, PATTERNPAINT);
d9c8e68e 449
5ab1fa8e 450 ::SelectObject( (HDC) dc, old );
2bda0e17
KB
451}
452
0655ad29
VZ
453void wxBitmapButton::SetDefault()
454{
455 wxButton::SetDefault();
456}
1e6feb95
VZ
457
458#endif // wxUSE_BMPBUTTON