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