]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
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 | |
fd3f686c | 9 | // Licence: wxWindows license |
2bda0e17 KB |
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 | |
10a0bdb1 | 24 | #include "wx/bmpbuttn.h" |
84f19880 | 25 | #include "wx/log.h" |
2bda0e17 KB |
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 | ||
debe6624 | 36 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap, |
2bda0e17 | 37 | const wxPoint& pos, |
debe6624 | 38 | const wxSize& size, long style, |
2bda0e17 KB |
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 | ||
fd71308f JS |
48 | m_backgroundColour = parent->GetBackgroundColour() ; |
49 | m_foregroundColour = parent->GetForegroundColour() ; | |
2bda0e17 KB |
50 | m_windowStyle = style; |
51 | m_marginX = 0; | |
52 | m_marginY = 0; | |
53 | ||
54 | if ( style & wxBU_AUTODRAW ) | |
55 | { | |
fd3f686c VZ |
56 | m_marginX = wxDEFAULT_BUTTON_MARGIN; |
57 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
2bda0e17 KB |
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()) | |
fd3f686c | 71 | width = bitmap.GetWidth() + 2*m_marginX; |
2bda0e17 KB |
72 | |
73 | if ( height == -1 && bitmap.Ok()) | |
fd3f686c | 74 | height = bitmap.GetHeight() + 2*m_marginY; |
2bda0e17 | 75 | |
42e69d6b VZ |
76 | m_hWnd = (WXHWND)CreateWindowEx |
77 | ( | |
78 | 0, | |
223d09f6 KB |
79 | wxT("BUTTON"), |
80 | wxT(""), | |
42e69d6b VZ |
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 | ); | |
2bda0e17 KB |
88 | |
89 | // Subclass again for purposes of dialog editing mode | |
42e69d6b | 90 | SubclassWin(m_hWnd); |
2bda0e17 | 91 | |
c0ed460c | 92 | SetFont(parent->GetFont()) ; |
2bda0e17 KB |
93 | |
94 | SetSize(x, y, width, height); | |
2bda0e17 KB |
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 | { | |
2bda0e17 | 106 | #if defined(__WIN95__) |
ba681060 | 107 | long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE); |
fd3f686c VZ |
108 | if (style & BS_BITMAP) |
109 | { | |
fd3f686c VZ |
110 | // Let default procedure draw the bitmap, which is defined |
111 | // in the Windows resource. | |
112 | return FALSE; | |
113 | } | |
2bda0e17 KB |
114 | #endif |
115 | ||
116 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; | |
117 | ||
d9c8e68e VZ |
118 | // choose the bitmap to use depending on the buttons state |
119 | wxBitmap* bitmap; | |
2bda0e17 | 120 | |
42e69d6b | 121 | UINT state = lpDIS->itemState; |
d9c8e68e VZ |
122 | bool isSelected = (state & ODS_SELECTED) != 0; |
123 | if ( isSelected && m_buttonBitmapSelected.Ok() ) | |
42e69d6b VZ |
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; | |
d9c8e68e VZ |
129 | else |
130 | bitmap = &m_buttonBitmap; | |
2bda0e17 | 131 | |
42e69d6b VZ |
132 | if ( !bitmap->Ok() ) |
133 | return FALSE; | |
2bda0e17 | 134 | |
d9c8e68e | 135 | // draw it on the memory DC |
42e69d6b VZ |
136 | HDC hDC = lpDIS->hDC; |
137 | HDC memDC = ::CreateCompatibleDC(hDC); | |
2bda0e17 | 138 | |
42e69d6b | 139 | HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP()); |
2bda0e17 | 140 | |
42e69d6b | 141 | if (!old) |
d9c8e68e VZ |
142 | { |
143 | wxLogLastError(_T("SelectObject")); | |
144 | ||
42e69d6b | 145 | return FALSE; |
d9c8e68e | 146 | } |
2bda0e17 | 147 | |
42e69d6b VZ |
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; | |
2bda0e17 | 152 | |
d9c8e68e VZ |
153 | int wBmp = bitmap->GetWidth(), |
154 | hBmp = bitmap->GetHeight(); | |
155 | ||
42e69d6b | 156 | // Draw the face, if auto-drawing |
d9c8e68e VZ |
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 | } | |
2bda0e17 | 165 | |
fd3f686c | 166 | // Centre the bitmap in the control area |
d9c8e68e VZ |
167 | int x1 = x + (width - wBmp) / 2; |
168 | int y1 = y + (height - hBmp) / 2; | |
2bda0e17 | 169 | |
d9c8e68e | 170 | if ( isSelected && autoDraw ) |
fd3f686c | 171 | { |
d9c8e68e VZ |
172 | x1++; |
173 | y1++; | |
fd3f686c | 174 | } |
2bda0e17 | 175 | |
d9c8e68e | 176 | BOOL ok; |
4fe5383d VZ |
177 | wxMask *mask = bitmap->GetMask(); |
178 | if ( mask ) | |
179 | { | |
d9c8e68e VZ |
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 | ); | |
4fe5383d VZ |
190 | } |
191 | else | |
192 | { | |
d9c8e68e VZ |
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()")); | |
4fe5383d | 201 | } |
2bda0e17 | 202 | |
d9c8e68e VZ |
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 | } | |
2bda0e17 | 219 | |
fd3f686c | 220 | ::SelectObject(memDC, old); |
2bda0e17 KB |
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 | { | |
d9c8e68e VZ |
229 | HPEN oldp; |
230 | HBRUSH oldb ; | |
2bda0e17 | 231 | |
d9c8e68e VZ |
232 | HPEN penBorder; |
233 | HPEN penLight; | |
234 | HPEN penShadow; | |
235 | HBRUSH brushFace; | |
236 | COLORREF ms_color; | |
2bda0e17 | 237 | |
fd3f686c VZ |
238 | ms_color = GetSysColor(COLOR_WINDOWFRAME) ; |
239 | penBorder = CreatePen(PS_SOLID,0,ms_color) ; | |
2bda0e17 | 240 | |
fd3f686c VZ |
241 | ms_color = GetSysColor(COLOR_BTNSHADOW) ; |
242 | penShadow = CreatePen(PS_SOLID,0,ms_color) ; | |
2bda0e17 | 243 | |
fd3f686c VZ |
244 | ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ; |
245 | penLight = CreatePen(PS_SOLID,0,ms_color) ; | |
2bda0e17 | 246 | |
fd3f686c VZ |
247 | ms_color = GetSysColor(COLOR_BTNFACE) ; |
248 | brushFace = CreateSolidBrush(ms_color) ; | |
2bda0e17 | 249 | |
fd3f686c VZ |
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) ; | |
d9c8e68e VZ |
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); | |
2bda0e17 | 258 | |
fd3f686c VZ |
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) ; | |
2bda0e17 | 283 | |
d9c8e68e VZ |
284 | DeleteObject(penBorder); |
285 | DeleteObject(penLight); | |
286 | DeleteObject(penShadow); | |
287 | DeleteObject(brushFace); | |
2bda0e17 KB |
288 | } |
289 | ||
4fe5383d VZ |
290 | // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN |
291 | #define FOCUS_MARGIN 3 | |
2bda0e17 KB |
292 | |
293 | void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
294 | { | |
fd3f686c VZ |
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 ) ; | |
2bda0e17 KB |
304 | } |
305 | ||
306 | extern HBRUSH wxDisableButtonBrush; | |
307 | void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg ) | |
308 | { | |
fd3f686c | 309 | HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ; |
2bda0e17 | 310 | |
d9c8e68e | 311 | // VZ: what's this?? there is no such ROP AFAIK |
ce3ed50d | 312 | #ifdef __SALFORDC__ |
d9c8e68e | 313 | DWORD dwRop = 0xFA0089L; |
ce3ed50d | 314 | #else |
d9c8e68e | 315 | DWORD dwRop = 0xFA0089UL; |
ce3ed50d | 316 | #endif |
d9c8e68e VZ |
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 | ||
fd3f686c | 328 | ::SelectObject( (HDC) dc, old ) ; |
2bda0e17 KB |
329 | } |
330 | ||
0655ad29 VZ |
331 | void wxBitmapButton::SetDefault() |
332 | { | |
333 | wxButton::SetDefault(); | |
334 | } |