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