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