]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
Changed name to wxFIXED_MINSIZE since that is more descriptive of what
[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
c046274e
RD
139 if ( bitmap.Ok() )
140 {
141 wxSize newSize = DoGetBestSize();
142 if ( width == -1 )
143 width = newSize.x;
144 if ( height == -1 )
145 height = newSize.y;
146 }
2bda0e17 147
a265d21a 148 long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ;
f6bcfd97 149
b0766406
JS
150 if ( m_windowStyle & wxCLIP_SIBLINGS )
151 msStyle |= WS_CLIPSIBLINGS;
152
f6bcfd97
BP
153#ifdef __WIN32__
154 if(m_windowStyle & wxBU_LEFT)
155 msStyle |= BS_LEFT;
156 if(m_windowStyle & wxBU_RIGHT)
157 msStyle |= BS_RIGHT;
158 if(m_windowStyle & wxBU_TOP)
159 msStyle |= BS_TOP;
160 if(m_windowStyle & wxBU_BOTTOM)
161 msStyle |= BS_BOTTOM;
162#endif
163
a265d21a 164 m_hWnd = (WXHWND) CreateWindowEx(
42e69d6b 165 0,
223d09f6 166 wxT("BUTTON"),
fda7962d 167 wxEmptyString,
f6bcfd97 168 msStyle,
33ac7e6f 169 0, 0, 0, 0,
42e69d6b
VZ
170 GetWinHwnd(parent),
171 (HMENU)m_windowId,
172 wxGetInstance(),
173 NULL
174 );
2bda0e17 175
a265d21a
DS
176 // Subclass again for purposes of dialog editing mode
177 SubclassWin(m_hWnd);
2bda0e17 178
a265d21a 179 SetFont(parent->GetFont());
2bda0e17 180
a265d21a 181 SetSize(x, y, width, height);
2bda0e17 182
a265d21a 183 return true;
2bda0e17
KB
184}
185
f6bcfd97
BP
186// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
187#define FOCUS_MARGIN 3
188
2bda0e17
KB
189bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
190{
4676948b 191#ifndef __WXWINCE__
ba681060 192 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
193 if (style & BS_BITMAP)
194 {
fd3f686c
VZ
195 // Let default procedure draw the bitmap, which is defined
196 // in the Windows resource.
a265d21a 197 return false;
fd3f686c 198 }
2bda0e17
KB
199#endif
200
201 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
47e92c41
GRG
202 HDC hDC = lpDIS->hDC;
203 UINT state = lpDIS->itemState;
204 bool isSelected = (state & ODS_SELECTED) != 0;
205 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
2bda0e17 206
47e92c41
GRG
207
208 // choose the bitmap to use depending on the button state
d9c8e68e 209 wxBitmap* bitmap;
2bda0e17 210
1e6feb95
VZ
211 if ( isSelected && m_bmpSelected.Ok() )
212 bitmap = &m_bmpSelected;
213 else if ((state & ODS_FOCUS) && m_bmpFocus.Ok())
214 bitmap = &m_bmpFocus;
215 else if ((state & ODS_DISABLED) && m_bmpDisabled.Ok())
216 bitmap = &m_bmpDisabled;
d9c8e68e 217 else
1e6feb95 218 bitmap = &m_bmpNormal;
2bda0e17 219
42e69d6b 220 if ( !bitmap->Ok() )
a265d21a 221 return false;
2bda0e17 222
47e92c41
GRG
223 // centre the bitmap in the control area
224 int x = lpDIS->rcItem.left;
225 int y = lpDIS->rcItem.top;
226 int width = lpDIS->rcItem.right - x;
227 int height = lpDIS->rcItem.bottom - y;
228 int wBmp = bitmap->GetWidth();
229 int hBmp = bitmap->GetHeight();
f6bcfd97 230
a265d21a
DS
231 int x1,y1;
232
f6bcfd97
BP
233 if(m_windowStyle & wxBU_LEFT)
234 x1 = x + (FOCUS_MARGIN+1);
235 else if(m_windowStyle & wxBU_RIGHT)
236 x1 = x + (width - wBmp) - (FOCUS_MARGIN+1);
237 else
238 x1 = x + (width - wBmp) / 2;
239
240 if(m_windowStyle & wxBU_TOP)
241 y1 = y + (FOCUS_MARGIN+1);
242 else if(m_windowStyle & wxBU_BOTTOM)
243 y1 = y + (height - hBmp) - (FOCUS_MARGIN+1);
244 else
245 y1 = y + (height - hBmp) / 2;
2bda0e17 246
47e92c41 247 if ( isSelected && autoDraw )
d9c8e68e 248 {
47e92c41
GRG
249 x1++;
250 y1++;
d9c8e68e 251 }
2bda0e17 252
47e92c41 253 // draw the face, if auto-drawing
d9c8e68e
VZ
254 if ( autoDraw )
255 {
256 DrawFace((WXHDC) hDC,
257 lpDIS->rcItem.left, lpDIS->rcItem.top,
258 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
259 isSelected);
260 }
2bda0e17 261
47e92c41
GRG
262 // draw the bitmap
263 wxDC dst;
a265d21a
DS
264 dst.SetHDC((WXHDC) hDC, false);
265 dst.DrawBitmap(*bitmap, x1, y1, true);
33ac7e6f 266
47e92c41 267 // draw focus / disabled state, if auto-drawing
d9c8e68e
VZ
268 if ( (state & ODS_DISABLED) && autoDraw )
269 {
270 DrawButtonDisable((WXHDC) hDC,
271 lpDIS->rcItem.left, lpDIS->rcItem.top,
272 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
a265d21a 273 true);
d9c8e68e
VZ
274 }
275 else if ( (state & ODS_FOCUS) && autoDraw )
276 {
277 DrawButtonFocus((WXHDC) hDC,
278 lpDIS->rcItem.left,
279 lpDIS->rcItem.top,
280 lpDIS->rcItem.right,
281 lpDIS->rcItem.bottom,
282 isSelected);
283 }
2bda0e17 284
a265d21a 285 return true;
2bda0e17
KB
286}
287
5ab1fa8e
GRG
288// GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
289
290#if defined(__WIN95__)
291
a265d21a
DS
292void wxBitmapButton::DrawFace( WXHDC dc, int left, int top,
293 int right, int bottom, bool sel )
2bda0e17 294{
d9c8e68e 295 HPEN oldp;
5ab1fa8e 296 HPEN penHiLight;
d9c8e68e
VZ
297 HPEN penLight;
298 HPEN penShadow;
5ab1fa8e 299 HPEN penDkShadow;
d9c8e68e 300 HBRUSH brushFace;
2bda0e17 301
5ab1fa8e
GRG
302 // create needed pens and brush
303 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
304 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
305 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
306 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
85b43fbf
JS
307 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
308 // Taking the background colour fits in better with
309 // Windows XP themes.
310 brushFace = CreateSolidBrush(m_backgroundColour.m_pixel);
5ab1fa8e
GRG
311
312 // draw the rectangle
313 RECT rect;
314 rect.left = left;
315 rect.right = right;
316 rect.top = top;
317 rect.bottom = bottom;
318 FillRect((HDC) dc, &rect, brushFace);
319
320 // draw the border
321 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
4676948b
JS
322
323 wxDrawLine((HDC) dc, left, top, right-1, top);
324 wxDrawLine((HDC) dc, left, top+1, left, bottom-1);
5ab1fa8e
GRG
325
326 SelectObject( (HDC) dc, sel? penShadow : penLight);
4676948b
JS
327 wxDrawLine((HDC) dc, left+1, top+1, right-2, top+1);
328 wxDrawLine((HDC) dc, left+1, top+2, left+1, bottom-2);
5ab1fa8e
GRG
329
330 SelectObject( (HDC) dc, sel? penLight : penShadow);
4676948b
JS
331 wxDrawLine((HDC) dc, left+1, bottom-2, right-1, bottom-2);
332 wxDrawLine((HDC) dc, right-2, bottom-3, right-2, top);
5ab1fa8e
GRG
333
334 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
4676948b
JS
335 wxDrawLine((HDC) dc, left, bottom-1, right+2, bottom-1);
336 wxDrawLine((HDC) dc, right-1, bottom-2, right-1, top-1);
5ab1fa8e
GRG
337
338 // delete allocated resources
339 SelectObject((HDC) dc,oldp);
340 DeleteObject(penHiLight);
341 DeleteObject(penLight);
342 DeleteObject(penShadow);
343 DeleteObject(penDkShadow);
344 DeleteObject(brushFace);
345}
346
347#else
2bda0e17 348
a265d21a
DS
349void wxBitmapButton::DrawFace( WXHDC dc, int left, int top,
350 int right, int bottom, bool sel )
5ab1fa8e
GRG
351{
352 HPEN oldp;
353 HPEN penBorder;
354 HPEN penLight;
355 HPEN penShadow;
356 HBRUSH brushFace;
2bda0e17 357
5ab1fa8e
GRG
358 // create needed pens and brush
359 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
360 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
361 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
362 brushFace = CreateSolidBrush(COLOR_BTNFACE);
2bda0e17 363
5ab1fa8e
GRG
364 // draw the rectangle
365 RECT rect;
366 rect.left = left;
367 rect.right = right;
368 rect.top = top;
369 rect.bottom = bottom;
370 FillRect((HDC) dc, &rect, brushFace);
2bda0e17 371
5ab1fa8e
GRG
372 // draw the border
373 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
d9c8e68e
VZ
374 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
375 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
376 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
377 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
2bda0e17 378
5ab1fa8e 379 SelectObject( (HDC) dc, penShadow);
fd3f686c
VZ
380 if (sel)
381 {
5ab1fa8e
GRG
382 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
383 LineTo((HDC) dc, left+1 ,top+1);
384 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c
VZ
385 }
386 else
387 {
5ab1fa8e
GRG
388 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
389 LineTo((HDC) dc, right-2 ,bottom-2);
390 LineTo((HDC) dc, right-2 ,top);
391
392 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
393 LineTo((HDC) dc, right-3 ,bottom-3);
394 LineTo((HDC) dc, right-3 ,top+1);
395
396 SelectObject( (HDC) dc, penLight);
397
398 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
399 LineTo((HDC) dc, left+1 ,top+1);
400 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c 401 }
2bda0e17 402
5ab1fa8e
GRG
403 // delete allocated resources
404 SelectObject((HDC) dc,oldp);
d9c8e68e
VZ
405 DeleteObject(penBorder);
406 DeleteObject(penLight);
407 DeleteObject(penShadow);
408 DeleteObject(brushFace);
2bda0e17
KB
409}
410
5ab1fa8e
GRG
411#endif // defined(__WIN95__)
412
413
a265d21a
DS
414void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right,
415 int bottom, bool WXUNUSED(sel) )
2bda0e17 416{
fd3f686c
VZ
417 RECT rect;
418 rect.left = left;
419 rect.top = top;
420 rect.right = right;
421 rect.bottom = bottom;
5ab1fa8e
GRG
422 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
423
424 // GRG: the focus rectangle should not move when the button is pushed!
425/*
fd3f686c 426 if ( sel )
5ab1fa8e
GRG
427 OffsetRect( &rect, 1, 1 );
428*/
a265d21a 429
5ab1fa8e 430 DrawFocusRect( (HDC) dc, &rect );
2bda0e17
KB
431}
432
433extern HBRUSH wxDisableButtonBrush;
a265d21a
DS
434void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right,
435 int bottom, bool with_marg )
2bda0e17 436{
5ab1fa8e 437 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
2bda0e17 438
2b268535
VZ
439 // ROP for "dest |= pattern" operation -- as it doesn't have a standard
440 // name, give it our own
a265d21a 441 static const DWORD PATTERNPAINT = 0xFA0089UL;
d9c8e68e
VZ
442
443 if ( with_marg )
444 {
445 left += m_marginX;
446 top += m_marginY;
447 right -= 2 * m_marginX;
448 bottom -= 2 * m_marginY;
449 }
450
a265d21a 451 ::PatBlt( (HDC) dc, left, top, right, bottom, PATTERNPAINT);
d9c8e68e 452
5ab1fa8e 453 ::SelectObject( (HDC) dc, old );
2bda0e17
KB
454}
455
0655ad29
VZ
456void wxBitmapButton::SetDefault()
457{
458 wxButton::SetDefault();
459}
1e6feb95 460
c046274e
RD
461wxSize wxBitmapButton::DoGetBestSize() const
462{
463 wxSize best;
464 if (m_bmpNormal.Ok())
465 {
466 best.x = m_bmpNormal.GetWidth() + 2*m_marginX;
467 best.y = m_bmpNormal.GetHeight() + 2*m_marginY;
468 }
e8153940
JS
469
470 // all buttons have at least the standard size unless the user explicitly
471 // wants them to be of smaller size and used wxBU_EXACTFIT style when
472 // creating the button
473 if ( !HasFlag(wxBU_EXACTFIT) )
474 {
475 wxSize sz = GetDefaultSize();
476 if (best.x > sz.x)
477 sz.x = best.x;
478 if (best.y > sz.y)
479 sz.y = best.y;
480 }
481
c046274e
RD
482 return best;
483}
484
1e6feb95 485#endif // wxUSE_BMPBUTTON
e8153940 486