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