]> git.saurik.com Git - wxWidgets.git/blob - src/msw/bmpbuttn.cpp
compilation fixes, doesn't work yet
[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 wxMask *mask = bitmap->GetMask();
176 if ( mask )
177 {
178 // the fg ROP is applied for the pixels of the mask bitmap which are 1
179 // (for a wxMask this means that this is a non transparent pixel), the
180 // bg ROP is applied for all the others
181
182 wxColour colBg = GetBackgroundColour();
183 HBRUSH hbrBackground =
184 ::CreateSolidBrush(RGB(colBg.Red(), colBg.Green(), colBg.Blue()));
185 HBRUSH hbrOld = (HBRUSH)::SelectObject(hDC, hbrBackground);
186
187 ok = ::MaskBlt(
188 hDC, x1, y1, wBmp, hBmp, // dst
189 memDC, 0, 0, // src
190 (HBITMAP)mask->GetMaskBitmap(), 0, 0, // mask
191 MAKEROP4(SRCCOPY, // fg ROP
192 PATCOPY) // bg ROP
193 );
194
195 ::SelectObject(hDC, hbrOld);
196 ::DeleteObject(hbrBackground);
197 }
198 else
199 {
200 ok = ::BitBlt(hDC, x1, y1, wBmp, hBmp, // dst
201 memDC, 0, 0, // src
202 SRCCOPY); // ROP
203 }
204
205 if ( !ok )
206 {
207 wxLogLastError(_T("Mask/BitBlt()"));
208 }
209
210 if ( (state & ODS_DISABLED) && autoDraw )
211 {
212 DrawButtonDisable((WXHDC) hDC,
213 lpDIS->rcItem.left, lpDIS->rcItem.top,
214 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
215 TRUE);
216 }
217 else if ( (state & ODS_FOCUS) && autoDraw )
218 {
219 DrawButtonFocus((WXHDC) hDC,
220 lpDIS->rcItem.left,
221 lpDIS->rcItem.top,
222 lpDIS->rcItem.right,
223 lpDIS->rcItem.bottom,
224 isSelected);
225 }
226
227 ::SelectObject(memDC, old);
228
229 ::DeleteDC(memDC);
230
231 return TRUE;
232 }
233
234 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
235 {
236 HPEN oldp;
237 HBRUSH oldb ;
238
239 HPEN penBorder;
240 HPEN penLight;
241 HPEN penShadow;
242 HBRUSH brushFace;
243 COLORREF ms_color;
244
245 ms_color = GetSysColor(COLOR_WINDOWFRAME) ;
246 penBorder = CreatePen(PS_SOLID,0,ms_color) ;
247
248 ms_color = GetSysColor(COLOR_BTNSHADOW) ;
249 penShadow = CreatePen(PS_SOLID,0,ms_color) ;
250
251 ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ;
252 penLight = CreatePen(PS_SOLID,0,ms_color) ;
253
254 ms_color = GetSysColor(COLOR_BTNFACE) ;
255 brushFace = CreateSolidBrush(ms_color) ;
256
257 oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ;
258 oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ;
259 Rectangle( (HDC) dc, left, top, right, bottom ) ;
260 SelectObject( (HDC) dc, penBorder) ;
261 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
262 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
263 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
264 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
265
266 SelectObject( (HDC) dc, penShadow) ;
267 if (sel)
268 {
269 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
270 LineTo((HDC) dc, left+1 ,top+1) ;
271 LineTo((HDC) dc, right-2 ,top+1) ;
272 }
273 else
274 {
275 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
276 LineTo((HDC) dc, right-2 ,bottom-2) ;
277 LineTo((HDC) dc, right-2 ,top) ;
278 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ;
279 LineTo((HDC) dc, right-3 ,bottom-3) ;
280 LineTo((HDC) dc, right-3 ,top+1) ;
281
282 SelectObject( (HDC) dc, penLight) ;
283
284 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
285 LineTo((HDC) dc, left+1 ,top+1) ;
286 LineTo((HDC) dc, right-2 ,top+1) ;
287 }
288 SelectObject((HDC) dc,oldp) ;
289 SelectObject((HDC) dc,oldb) ;
290
291 DeleteObject(penBorder);
292 DeleteObject(penLight);
293 DeleteObject(penShadow);
294 DeleteObject(brushFace);
295 }
296
297 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
298 #define FOCUS_MARGIN 3
299
300 void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
301 {
302 RECT rect;
303 rect.left = left;
304 rect.top = top;
305 rect.right = right;
306 rect.bottom = bottom;
307 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ;
308 if ( sel )
309 OffsetRect( &rect, 1, 1 ) ;
310 DrawFocusRect( (HDC) dc, &rect ) ;
311 }
312
313 extern HBRUSH wxDisableButtonBrush;
314 void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
315 {
316 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ;
317
318 // VZ: what's this?? there is no such ROP AFAIK
319 #ifdef __SALFORDC__
320 DWORD dwRop = 0xFA0089L;
321 #else
322 DWORD dwRop = 0xFA0089UL;
323 #endif
324
325 if ( with_marg )
326 {
327 left += m_marginX;
328 top += m_marginY;
329 right -= 2 * m_marginX;
330 bottom -= 2 * m_marginY;
331 }
332
333 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
334
335 ::SelectObject( (HDC) dc, old ) ;
336 }
337
338 void wxBitmapButton::SetDefault()
339 {
340 wxButton::SetDefault();
341 }