]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
replaced wxYield() call in PopupMenu() by a much safer wxYieldForCommandsOnly() ...
[wxWidgets.git] / src / msw / bmpbuttn.cpp
CommitLineData
2bda0e17
KB
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
fd3f686c 9// Licence: wxWindows license
2bda0e17
KB
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
10a0bdb1 24 #include "wx/bmpbuttn.h"
84f19880 25 #include "wx/log.h"
47e92c41 26 #include "wx/dcmemory.h"
2bda0e17
KB
27#endif
28
29#include "wx/msw/private.h"
30
2bda0e17 31IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
2bda0e17
KB
32
33#define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34
debe6624 35bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
2bda0e17 36 const wxPoint& pos,
debe6624 37 const wxSize& size, long style,
2bda0e17
KB
38 const wxValidator& validator,
39 const wxString& name)
40{
41 m_buttonBitmap = bitmap;
42 SetName(name);
11b6a93b
VZ
43
44#if wxUSE_VALIDATORS
2bda0e17 45 SetValidator(validator);
11b6a93b 46#endif // wxUSE_VALIDATORS
2bda0e17
KB
47
48 parent->AddChild(this);
49
5ab1fa8e
GRG
50 m_backgroundColour = parent->GetBackgroundColour();
51 m_foregroundColour = parent->GetForegroundColour();
2bda0e17
KB
52 m_windowStyle = style;
53 m_marginX = 0;
54 m_marginY = 0;
55
56 if ( style & wxBU_AUTODRAW )
57 {
fd3f686c
VZ
58 m_marginX = wxDEFAULT_BUTTON_MARGIN;
59 m_marginY = wxDEFAULT_BUTTON_MARGIN;
2bda0e17
KB
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())
fd3f686c 73 width = bitmap.GetWidth() + 2*m_marginX;
2bda0e17
KB
74
75 if ( height == -1 && bitmap.Ok())
fd3f686c 76 height = bitmap.GetHeight() + 2*m_marginY;
2bda0e17 77
f6bcfd97
BP
78 long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ;
79
b0766406
JS
80 if ( m_windowStyle & wxCLIP_SIBLINGS )
81 msStyle |= WS_CLIPSIBLINGS;
82
f6bcfd97
BP
83#ifdef __WIN32__
84 if(m_windowStyle & wxBU_LEFT)
85 msStyle |= BS_LEFT;
86 if(m_windowStyle & wxBU_RIGHT)
87 msStyle |= BS_RIGHT;
88 if(m_windowStyle & wxBU_TOP)
89 msStyle |= BS_TOP;
90 if(m_windowStyle & wxBU_BOTTOM)
91 msStyle |= BS_BOTTOM;
92#endif
93
42e69d6b
VZ
94 m_hWnd = (WXHWND)CreateWindowEx
95 (
96 0,
223d09f6
KB
97 wxT("BUTTON"),
98 wxT(""),
f6bcfd97 99 msStyle,
33ac7e6f 100 0, 0, 0, 0,
42e69d6b
VZ
101 GetWinHwnd(parent),
102 (HMENU)m_windowId,
103 wxGetInstance(),
104 NULL
105 );
2bda0e17
KB
106
107 // Subclass again for purposes of dialog editing mode
42e69d6b 108 SubclassWin(m_hWnd);
2bda0e17 109
5ab1fa8e 110 SetFont(parent->GetFont());
2bda0e17
KB
111
112 SetSize(x, y, width, height);
2bda0e17
KB
113
114 return TRUE;
115}
116
117void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
118{
119 m_buttonBitmap = bitmap;
120}
121
f6bcfd97
BP
122// VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
123#define FOCUS_MARGIN 3
124
2bda0e17
KB
125bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
126{
2bda0e17 127#if defined(__WIN95__)
ba681060 128 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
129 if (style & BS_BITMAP)
130 {
fd3f686c
VZ
131 // Let default procedure draw the bitmap, which is defined
132 // in the Windows resource.
133 return FALSE;
134 }
2bda0e17
KB
135#endif
136
137 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
47e92c41
GRG
138 HDC hDC = lpDIS->hDC;
139 UINT state = lpDIS->itemState;
140 bool isSelected = (state & ODS_SELECTED) != 0;
141 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
2bda0e17 142
47e92c41
GRG
143
144 // choose the bitmap to use depending on the button state
d9c8e68e 145 wxBitmap* bitmap;
2bda0e17 146
d9c8e68e 147 if ( isSelected && m_buttonBitmapSelected.Ok() )
42e69d6b
VZ
148 bitmap = &m_buttonBitmapSelected;
149 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
150 bitmap = &m_buttonBitmapFocus;
151 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
152 bitmap = &m_buttonBitmapDisabled;
d9c8e68e
VZ
153 else
154 bitmap = &m_buttonBitmap;
2bda0e17 155
42e69d6b
VZ
156 if ( !bitmap->Ok() )
157 return FALSE;
2bda0e17 158
47e92c41
GRG
159 // centre the bitmap in the control area
160 int x = lpDIS->rcItem.left;
161 int y = lpDIS->rcItem.top;
162 int width = lpDIS->rcItem.right - x;
163 int height = lpDIS->rcItem.bottom - y;
164 int wBmp = bitmap->GetWidth();
165 int hBmp = bitmap->GetHeight();
f6bcfd97
BP
166
167 int x1,y1;
168
169 if(m_windowStyle & wxBU_LEFT)
170 x1 = x + (FOCUS_MARGIN+1);
171 else if(m_windowStyle & wxBU_RIGHT)
172 x1 = x + (width - wBmp) - (FOCUS_MARGIN+1);
173 else
174 x1 = x + (width - wBmp) / 2;
175
176 if(m_windowStyle & wxBU_TOP)
177 y1 = y + (FOCUS_MARGIN+1);
178 else if(m_windowStyle & wxBU_BOTTOM)
179 y1 = y + (height - hBmp) - (FOCUS_MARGIN+1);
180 else
181 y1 = y + (height - hBmp) / 2;
2bda0e17 182
47e92c41 183 if ( isSelected && autoDraw )
d9c8e68e 184 {
47e92c41
GRG
185 x1++;
186 y1++;
d9c8e68e 187 }
2bda0e17 188
47e92c41 189 // draw the face, if auto-drawing
d9c8e68e
VZ
190 if ( autoDraw )
191 {
192 DrawFace((WXHDC) hDC,
193 lpDIS->rcItem.left, lpDIS->rcItem.top,
194 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
195 isSelected);
196 }
2bda0e17 197
47e92c41
GRG
198 // draw the bitmap
199 wxDC dst;
200 dst.SetHDC((WXHDC) hDC, FALSE);
201 dst.DrawBitmap(*bitmap, x1, y1, TRUE);
33ac7e6f 202
47e92c41 203 // draw focus / disabled state, if auto-drawing
d9c8e68e
VZ
204 if ( (state & ODS_DISABLED) && autoDraw )
205 {
206 DrawButtonDisable((WXHDC) hDC,
207 lpDIS->rcItem.left, lpDIS->rcItem.top,
208 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
209 TRUE);
210 }
211 else if ( (state & ODS_FOCUS) && autoDraw )
212 {
213 DrawButtonFocus((WXHDC) hDC,
214 lpDIS->rcItem.left,
215 lpDIS->rcItem.top,
216 lpDIS->rcItem.right,
217 lpDIS->rcItem.bottom,
218 isSelected);
219 }
2bda0e17 220
2bda0e17
KB
221 return TRUE;
222}
223
5ab1fa8e
GRG
224// GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
225
226#if defined(__WIN95__)
227
2bda0e17
KB
228void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
229{
d9c8e68e 230 HPEN oldp;
5ab1fa8e 231 HPEN penHiLight;
d9c8e68e
VZ
232 HPEN penLight;
233 HPEN penShadow;
5ab1fa8e 234 HPEN penDkShadow;
d9c8e68e 235 HBRUSH brushFace;
2bda0e17 236
5ab1fa8e
GRG
237 // create needed pens and brush
238 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
239 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
240 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
241 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
242 brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
243
244 // draw the rectangle
245 RECT rect;
246 rect.left = left;
247 rect.right = right;
248 rect.top = top;
249 rect.bottom = bottom;
250 FillRect((HDC) dc, &rect, brushFace);
251
252 // draw the border
253 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
254 MoveToEx((HDC) dc, left, top, NULL); LineTo((HDC) dc, right-1, top);
255 MoveToEx((HDC) dc, left, top+1, NULL); LineTo((HDC) dc, left, bottom-1);
256
257 SelectObject( (HDC) dc, sel? penShadow : penLight);
258 MoveToEx((HDC) dc, left+1, top+1, NULL); LineTo((HDC) dc, right-2, top+1);
259 MoveToEx((HDC) dc, left+1, top+2, NULL); LineTo((HDC) dc, left+1, bottom-2);
260
261 SelectObject( (HDC) dc, sel? penLight : penShadow);
262 MoveToEx((HDC) dc, left+1, bottom-2, NULL); LineTo((HDC) dc, right-1, bottom-2);
263 MoveToEx((HDC) dc, right-2, bottom-3, NULL); LineTo((HDC) dc, right-2, top);
264
265 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
266 MoveToEx((HDC) dc, left, bottom-1, NULL); LineTo((HDC) dc, right+2, bottom-1);
267 MoveToEx((HDC) dc, right-1, bottom-2, NULL); LineTo((HDC) dc, right-1, top-1);
268
269 // delete allocated resources
270 SelectObject((HDC) dc,oldp);
271 DeleteObject(penHiLight);
272 DeleteObject(penLight);
273 DeleteObject(penShadow);
274 DeleteObject(penDkShadow);
275 DeleteObject(brushFace);
276}
277
278#else
2bda0e17 279
5ab1fa8e
GRG
280void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
281{
282 HPEN oldp;
283 HPEN penBorder;
284 HPEN penLight;
285 HPEN penShadow;
286 HBRUSH brushFace;
2bda0e17 287
5ab1fa8e
GRG
288 // create needed pens and brush
289 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
290 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
291 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
292 brushFace = CreateSolidBrush(COLOR_BTNFACE);
2bda0e17 293
5ab1fa8e
GRG
294 // draw the rectangle
295 RECT rect;
296 rect.left = left;
297 rect.right = right;
298 rect.top = top;
299 rect.bottom = bottom;
300 FillRect((HDC) dc, &rect, brushFace);
2bda0e17 301
5ab1fa8e
GRG
302 // draw the border
303 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
d9c8e68e
VZ
304 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
305 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
306 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
307 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
2bda0e17 308
5ab1fa8e 309 SelectObject( (HDC) dc, penShadow);
fd3f686c
VZ
310 if (sel)
311 {
5ab1fa8e
GRG
312 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
313 LineTo((HDC) dc, left+1 ,top+1);
314 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c
VZ
315 }
316 else
317 {
5ab1fa8e
GRG
318 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
319 LineTo((HDC) dc, right-2 ,bottom-2);
320 LineTo((HDC) dc, right-2 ,top);
321
322 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
323 LineTo((HDC) dc, right-3 ,bottom-3);
324 LineTo((HDC) dc, right-3 ,top+1);
325
326 SelectObject( (HDC) dc, penLight);
327
328 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
329 LineTo((HDC) dc, left+1 ,top+1);
330 LineTo((HDC) dc, right-2 ,top+1);
fd3f686c 331 }
2bda0e17 332
5ab1fa8e
GRG
333 // delete allocated resources
334 SelectObject((HDC) dc,oldp);
d9c8e68e
VZ
335 DeleteObject(penBorder);
336 DeleteObject(penLight);
337 DeleteObject(penShadow);
338 DeleteObject(brushFace);
2bda0e17
KB
339}
340
5ab1fa8e
GRG
341#endif // defined(__WIN95__)
342
343
2bda0e17
KB
344void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
345{
fd3f686c
VZ
346 RECT rect;
347 rect.left = left;
348 rect.top = top;
349 rect.right = right;
350 rect.bottom = bottom;
5ab1fa8e
GRG
351 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
352
353 // GRG: the focus rectangle should not move when the button is pushed!
354/*
fd3f686c 355 if ( sel )
5ab1fa8e
GRG
356 OffsetRect( &rect, 1, 1 );
357*/
33ac7e6f 358 (void)sel;
5ab1fa8e 359 DrawFocusRect( (HDC) dc, &rect );
2bda0e17
KB
360}
361
362extern HBRUSH wxDisableButtonBrush;
363void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
364{
5ab1fa8e 365 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
2bda0e17 366
d9c8e68e 367 // VZ: what's this?? there is no such ROP AFAIK
ce3ed50d 368#ifdef __SALFORDC__
d9c8e68e 369 DWORD dwRop = 0xFA0089L;
ce3ed50d 370#else
d9c8e68e 371 DWORD dwRop = 0xFA0089UL;
ce3ed50d 372#endif
d9c8e68e
VZ
373
374 if ( with_marg )
375 {
376 left += m_marginX;
377 top += m_marginY;
378 right -= 2 * m_marginX;
379 bottom -= 2 * m_marginY;
380 }
381
382 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
383
5ab1fa8e 384 ::SelectObject( (HDC) dc, old );
2bda0e17
KB
385}
386
0655ad29
VZ
387void wxBitmapButton::SetDefault()
388{
389 wxButton::SetDefault();
390}