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