]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bmpbuttn.cpp
wxSIZE_ALLOW_MINUS_ONE handling corrected
[wxWidgets.git] / src / msw / bmpbuttn.cpp
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
9 // Licence: wxWindows license
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
24 #include "wx/bmpbuttn.h"
25 #include "wx/log.h"
26 #endif
27
28 #include "wx/msw/private.h"
29
30 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
31
32 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
33
34 bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
35 const wxPoint& pos,
36 const wxSize& size, long style,
37 const wxValidator& validator,
38 const wxString& name)
39 {
40 m_buttonBitmap = bitmap;
41 SetName(name);
42 SetValidator(validator);
43
44 parent->AddChild(this);
45
46 m_backgroundColour = parent->GetBackgroundColour() ;
47 m_foregroundColour = parent->GetForegroundColour() ;
48 m_windowStyle = style;
49 m_marginX = 0;
50 m_marginY = 0;
51
52 if ( style & wxBU_AUTODRAW )
53 {
54 m_marginX = wxDEFAULT_BUTTON_MARGIN;
55 m_marginY = wxDEFAULT_BUTTON_MARGIN;
56 }
57
58 int x = pos.x;
59 int y = pos.y;
60 int width = size.x;
61 int height = size.y;
62
63 if (id == -1)
64 m_windowId = NewControlId();
65 else
66 m_windowId = id;
67
68 if ( width == -1 && bitmap.Ok())
69 width = bitmap.GetWidth() + 2*m_marginX;
70
71 if ( height == -1 && bitmap.Ok())
72 height = bitmap.GetHeight() + 2*m_marginY;
73
74 m_hWnd = (WXHWND)CreateWindowEx
75 (
76 0,
77 wxT("BUTTON"),
78 wxT(""),
79 WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ,
80 0, 0, 0, 0,
81 GetWinHwnd(parent),
82 (HMENU)m_windowId,
83 wxGetInstance(),
84 NULL
85 );
86
87 // Subclass again for purposes of dialog editing mode
88 SubclassWin(m_hWnd);
89
90 SetFont(parent->GetFont()) ;
91
92 SetSize(x, y, width, height);
93
94 return TRUE;
95 }
96
97 void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
98 {
99 m_buttonBitmap = bitmap;
100 }
101
102 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
103 {
104 #if defined(__WIN95__)
105 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
106 if (style & BS_BITMAP)
107 {
108 // Let default procedure draw the bitmap, which is defined
109 // in the Windows resource.
110 return FALSE;
111 }
112 #endif
113
114 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
115
116 // choose the bitmap to use depending on the buttons state
117 wxBitmap* bitmap;
118
119 UINT state = lpDIS->itemState;
120 bool isSelected = (state & ODS_SELECTED) != 0;
121 if ( isSelected && m_buttonBitmapSelected.Ok() )
122 bitmap = &m_buttonBitmapSelected;
123 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
124 bitmap = &m_buttonBitmapFocus;
125 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
126 bitmap = &m_buttonBitmapDisabled;
127 else
128 bitmap = &m_buttonBitmap;
129
130 if ( !bitmap->Ok() )
131 return FALSE;
132
133 // draw it on the memory DC
134 HDC hDC = lpDIS->hDC;
135 HDC memDC = ::CreateCompatibleDC(hDC);
136
137 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
138
139 if (!old)
140 {
141 wxLogLastError(_T("SelectObject"));
142
143 return FALSE;
144 }
145
146 int x = lpDIS->rcItem.left;
147 int y = lpDIS->rcItem.top;
148 int width = lpDIS->rcItem.right - x;
149 int height = lpDIS->rcItem.bottom - y;
150
151 int wBmp = bitmap->GetWidth(),
152 hBmp = bitmap->GetHeight();
153
154 // Draw the face, if auto-drawing
155 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
156 if ( autoDraw )
157 {
158 DrawFace((WXHDC) hDC,
159 lpDIS->rcItem.left, lpDIS->rcItem.top,
160 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
161 isSelected);
162 }
163
164 // Centre the bitmap in the control area
165 int x1 = x + (width - wBmp) / 2;
166 int y1 = y + (height - hBmp) / 2;
167
168 if ( isSelected && autoDraw )
169 {
170 x1++;
171 y1++;
172 }
173
174 BOOL ok;
175
176 // no MaskBlt() under Win16
177 #ifdef __WIN32__
178 wxMask *mask = bitmap->GetMask();
179 if ( mask )
180 {
181 // the fg ROP is applied for the pixels of the mask bitmap which are 1
182 // (for a wxMask this means that this is a non transparent pixel), the
183 // bg ROP is applied for all the others
184
185 wxColour colBg = GetBackgroundColour();
186 HBRUSH hbrBackground =
187 ::CreateSolidBrush(RGB(colBg.Red(), colBg.Green(), colBg.Blue()));
188 HBRUSH hbrOld = (HBRUSH)::SelectObject(hDC, hbrBackground);
189
190 ok = ::MaskBlt(
191 hDC, x1, y1, wBmp, hBmp, // dst
192 memDC, 0, 0, // src
193 (HBITMAP)mask->GetMaskBitmap(), 0, 0, // mask
194 MAKEROP4(SRCCOPY, // fg ROP
195 PATCOPY) // bg ROP
196 );
197
198 ::SelectObject(hDC, hbrOld);
199 ::DeleteObject(hbrBackground);
200 }
201 else
202 #endif // Win32
203 {
204 ok = ::BitBlt(hDC, x1, y1, wBmp, hBmp, // dst
205 memDC, 0, 0, // src
206 SRCCOPY); // ROP
207 }
208
209 if ( !ok )
210 {
211 wxLogLastError(_T("Mask/BitBlt()"));
212 }
213
214 if ( (state & ODS_DISABLED) && autoDraw )
215 {
216 DrawButtonDisable((WXHDC) hDC,
217 lpDIS->rcItem.left, lpDIS->rcItem.top,
218 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
219 TRUE);
220 }
221 else if ( (state & ODS_FOCUS) && autoDraw )
222 {
223 DrawButtonFocus((WXHDC) hDC,
224 lpDIS->rcItem.left,
225 lpDIS->rcItem.top,
226 lpDIS->rcItem.right,
227 lpDIS->rcItem.bottom,
228 isSelected);
229 }
230
231 ::SelectObject(memDC, old);
232
233 ::DeleteDC(memDC);
234
235 return TRUE;
236 }
237
238 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
239 {
240 HPEN oldp;
241 HBRUSH oldb ;
242
243 HPEN penBorder;
244 HPEN penLight;
245 HPEN penShadow;
246 HBRUSH brushFace;
247 COLORREF ms_color;
248
249 ms_color = GetSysColor(COLOR_WINDOWFRAME) ;
250 penBorder = CreatePen(PS_SOLID,0,ms_color) ;
251
252 ms_color = GetSysColor(COLOR_BTNSHADOW) ;
253 penShadow = CreatePen(PS_SOLID,0,ms_color) ;
254
255 ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ;
256 penLight = CreatePen(PS_SOLID,0,ms_color) ;
257
258 ms_color = GetSysColor(COLOR_BTNFACE) ;
259 brushFace = CreateSolidBrush(ms_color) ;
260
261 oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ;
262 oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ;
263 Rectangle( (HDC) dc, left, top, right, bottom ) ;
264 SelectObject( (HDC) dc, penBorder) ;
265 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
266 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
267 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
268 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
269
270 SelectObject( (HDC) dc, penShadow) ;
271 if (sel)
272 {
273 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
274 LineTo((HDC) dc, left+1 ,top+1) ;
275 LineTo((HDC) dc, right-2 ,top+1) ;
276 }
277 else
278 {
279 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
280 LineTo((HDC) dc, right-2 ,bottom-2) ;
281 LineTo((HDC) dc, right-2 ,top) ;
282 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ;
283 LineTo((HDC) dc, right-3 ,bottom-3) ;
284 LineTo((HDC) dc, right-3 ,top+1) ;
285
286 SelectObject( (HDC) dc, penLight) ;
287
288 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
289 LineTo((HDC) dc, left+1 ,top+1) ;
290 LineTo((HDC) dc, right-2 ,top+1) ;
291 }
292 SelectObject((HDC) dc,oldp) ;
293 SelectObject((HDC) dc,oldb) ;
294
295 DeleteObject(penBorder);
296 DeleteObject(penLight);
297 DeleteObject(penShadow);
298 DeleteObject(brushFace);
299 }
300
301 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
302 #define FOCUS_MARGIN 3
303
304 void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
305 {
306 RECT rect;
307 rect.left = left;
308 rect.top = top;
309 rect.right = right;
310 rect.bottom = bottom;
311 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ;
312 if ( sel )
313 OffsetRect( &rect, 1, 1 ) ;
314 DrawFocusRect( (HDC) dc, &rect ) ;
315 }
316
317 extern HBRUSH wxDisableButtonBrush;
318 void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
319 {
320 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ;
321
322 // VZ: what's this?? there is no such ROP AFAIK
323 #ifdef __SALFORDC__
324 DWORD dwRop = 0xFA0089L;
325 #else
326 DWORD dwRop = 0xFA0089UL;
327 #endif
328
329 if ( with_marg )
330 {
331 left += m_marginX;
332 top += m_marginY;
333 right -= 2 * m_marginX;
334 bottom -= 2 * m_marginY;
335 }
336
337 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
338
339 ::SelectObject( (HDC) dc, old ) ;
340 }
341
342 void wxBitmapButton::SetDefault()
343 {
344 wxButton::SetDefault();
345 }