]>
Commit | Line | Data |
---|---|---|
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 | #include "wx/dcmemory.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/msw/private.h" | |
30 | ||
31 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) | |
32 | ||
33 | #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1) | |
34 | ||
35 | bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap, | |
36 | const wxPoint& pos, | |
37 | const wxSize& size, long style, | |
38 | const wxValidator& validator, | |
39 | const wxString& name) | |
40 | { | |
41 | m_buttonBitmap = bitmap; | |
42 | SetName(name); | |
43 | ||
44 | #if wxUSE_VALIDATORS | |
45 | SetValidator(validator); | |
46 | #endif // wxUSE_VALIDATORS | |
47 | ||
48 | parent->AddChild(this); | |
49 | ||
50 | m_backgroundColour = parent->GetBackgroundColour(); | |
51 | m_foregroundColour = parent->GetForegroundColour(); | |
52 | m_windowStyle = style; | |
53 | m_marginX = 0; | |
54 | m_marginY = 0; | |
55 | ||
56 | if ( style & wxBU_AUTODRAW ) | |
57 | { | |
58 | m_marginX = wxDEFAULT_BUTTON_MARGIN; | |
59 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
60 | } | |
61 | ||
62 | int x = pos.x; | |
63 | int y = pos.y; | |
64 | int width = size.x; | |
65 | int height = size.y; | |
66 | ||
67 | if (id == -1) | |
68 | m_windowId = NewControlId(); | |
69 | else | |
70 | m_windowId = id; | |
71 | ||
72 | if ( width == -1 && bitmap.Ok()) | |
73 | width = bitmap.GetWidth() + 2*m_marginX; | |
74 | ||
75 | if ( height == -1 && bitmap.Ok()) | |
76 | height = bitmap.GetHeight() + 2*m_marginY; | |
77 | ||
78 | long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ; | |
79 | ||
80 | if ( m_windowStyle & wxCLIP_SIBLINGS ) | |
81 | msStyle |= WS_CLIPSIBLINGS; | |
82 | ||
83 | #ifdef __WIN32__ | |
84 | if(m_windowStyle & wxBU_LEFT) | |
85 | msStyle |= BS_LEFT; | |
86 | if(m_windowStyle & wxBU_RIGHT) | |
87 | msStyle |= BS_RIGHT; | |
88 | if(m_windowStyle & wxBU_TOP) | |
89 | msStyle |= BS_TOP; | |
90 | if(m_windowStyle & wxBU_BOTTOM) | |
91 | msStyle |= BS_BOTTOM; | |
92 | #endif | |
93 | ||
94 | m_hWnd = (WXHWND)CreateWindowEx | |
95 | ( | |
96 | 0, | |
97 | wxT("BUTTON"), | |
98 | wxT(""), | |
99 | msStyle, | |
100 | 0, 0, 0, 0, | |
101 | GetWinHwnd(parent), | |
102 | (HMENU)m_windowId, | |
103 | wxGetInstance(), | |
104 | NULL | |
105 | ); | |
106 | ||
107 | // Subclass again for purposes of dialog editing mode | |
108 | SubclassWin(m_hWnd); | |
109 | ||
110 | SetFont(parent->GetFont()); | |
111 | ||
112 | SetSize(x, y, width, height); | |
113 | ||
114 | return TRUE; | |
115 | } | |
116 | ||
117 | void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap) | |
118 | { | |
119 | m_buttonBitmap = bitmap; | |
120 | } | |
121 | ||
122 | // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN | |
123 | #define FOCUS_MARGIN 3 | |
124 | ||
125 | bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
126 | { | |
127 | #if defined(__WIN95__) | |
128 | long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE); | |
129 | if (style & BS_BITMAP) | |
130 | { | |
131 | // Let default procedure draw the bitmap, which is defined | |
132 | // in the Windows resource. | |
133 | return FALSE; | |
134 | } | |
135 | #endif | |
136 | ||
137 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; | |
138 | HDC hDC = lpDIS->hDC; | |
139 | UINT state = lpDIS->itemState; | |
140 | bool isSelected = (state & ODS_SELECTED) != 0; | |
141 | bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0; | |
142 | ||
143 | ||
144 | // choose the bitmap to use depending on the button state | |
145 | wxBitmap* bitmap; | |
146 | ||
147 | if ( isSelected && m_buttonBitmapSelected.Ok() ) | |
148 | bitmap = &m_buttonBitmapSelected; | |
149 | else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok()) | |
150 | bitmap = &m_buttonBitmapFocus; | |
151 | else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok()) | |
152 | bitmap = &m_buttonBitmapDisabled; | |
153 | else | |
154 | bitmap = &m_buttonBitmap; | |
155 | ||
156 | if ( !bitmap->Ok() ) | |
157 | return FALSE; | |
158 | ||
159 | // centre the bitmap in the control area | |
160 | int x = lpDIS->rcItem.left; | |
161 | int y = lpDIS->rcItem.top; | |
162 | int width = lpDIS->rcItem.right - x; | |
163 | int height = lpDIS->rcItem.bottom - y; | |
164 | int wBmp = bitmap->GetWidth(); | |
165 | int hBmp = bitmap->GetHeight(); | |
166 | ||
167 | int x1,y1; | |
168 | ||
169 | if(m_windowStyle & wxBU_LEFT) | |
170 | x1 = x + (FOCUS_MARGIN+1); | |
171 | else if(m_windowStyle & wxBU_RIGHT) | |
172 | x1 = x + (width - wBmp) - (FOCUS_MARGIN+1); | |
173 | else | |
174 | x1 = x + (width - wBmp) / 2; | |
175 | ||
176 | if(m_windowStyle & wxBU_TOP) | |
177 | y1 = y + (FOCUS_MARGIN+1); | |
178 | else if(m_windowStyle & wxBU_BOTTOM) | |
179 | y1 = y + (height - hBmp) - (FOCUS_MARGIN+1); | |
180 | else | |
181 | y1 = y + (height - hBmp) / 2; | |
182 | ||
183 | if ( isSelected && autoDraw ) | |
184 | { | |
185 | x1++; | |
186 | y1++; | |
187 | } | |
188 | ||
189 | // draw the face, if auto-drawing | |
190 | if ( autoDraw ) | |
191 | { | |
192 | DrawFace((WXHDC) hDC, | |
193 | lpDIS->rcItem.left, lpDIS->rcItem.top, | |
194 | lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
195 | isSelected); | |
196 | } | |
197 | ||
198 | // draw the bitmap | |
199 | wxDC dst; | |
200 | dst.SetHDC((WXHDC) hDC, FALSE); | |
201 | dst.DrawBitmap(*bitmap, x1, y1, TRUE); | |
202 | ||
203 | // draw focus / disabled state, if auto-drawing | |
204 | if ( (state & ODS_DISABLED) && autoDraw ) | |
205 | { | |
206 | DrawButtonDisable((WXHDC) hDC, | |
207 | lpDIS->rcItem.left, lpDIS->rcItem.top, | |
208 | lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
209 | TRUE); | |
210 | } | |
211 | else if ( (state & ODS_FOCUS) && autoDraw ) | |
212 | { | |
213 | DrawButtonFocus((WXHDC) hDC, | |
214 | lpDIS->rcItem.left, | |
215 | lpDIS->rcItem.top, | |
216 | lpDIS->rcItem.right, | |
217 | lpDIS->rcItem.bottom, | |
218 | isSelected); | |
219 | } | |
220 | ||
221 | return TRUE; | |
222 | } | |
223 | ||
224 | // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF | |
225 | ||
226 | #if defined(__WIN95__) | |
227 | ||
228 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
229 | { | |
230 | HPEN oldp; | |
231 | HPEN penHiLight; | |
232 | HPEN penLight; | |
233 | HPEN penShadow; | |
234 | HPEN penDkShadow; | |
235 | HBRUSH brushFace; | |
236 | ||
237 | // create needed pens and brush | |
238 | penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT)); | |
239 | penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT)); | |
240 | penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW)); | |
241 | penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW)); | |
242 | brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE)); | |
243 | ||
244 | // draw the rectangle | |
245 | RECT rect; | |
246 | rect.left = left; | |
247 | rect.right = right; | |
248 | rect.top = top; | |
249 | rect.bottom = bottom; | |
250 | FillRect((HDC) dc, &rect, brushFace); | |
251 | ||
252 | // draw the border | |
253 | oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight); | |
254 | MoveToEx((HDC) dc, left, top, NULL); LineTo((HDC) dc, right-1, top); | |
255 | MoveToEx((HDC) dc, left, top+1, NULL); LineTo((HDC) dc, left, bottom-1); | |
256 | ||
257 | SelectObject( (HDC) dc, sel? penShadow : penLight); | |
258 | MoveToEx((HDC) dc, left+1, top+1, NULL); LineTo((HDC) dc, right-2, top+1); | |
259 | MoveToEx((HDC) dc, left+1, top+2, NULL); LineTo((HDC) dc, left+1, bottom-2); | |
260 | ||
261 | SelectObject( (HDC) dc, sel? penLight : penShadow); | |
262 | MoveToEx((HDC) dc, left+1, bottom-2, NULL); LineTo((HDC) dc, right-1, bottom-2); | |
263 | MoveToEx((HDC) dc, right-2, bottom-3, NULL); LineTo((HDC) dc, right-2, top); | |
264 | ||
265 | SelectObject( (HDC) dc, sel? penHiLight : penDkShadow); | |
266 | MoveToEx((HDC) dc, left, bottom-1, NULL); LineTo((HDC) dc, right+2, bottom-1); | |
267 | MoveToEx((HDC) dc, right-1, bottom-2, NULL); LineTo((HDC) dc, right-1, top-1); | |
268 | ||
269 | // delete allocated resources | |
270 | SelectObject((HDC) dc,oldp); | |
271 | DeleteObject(penHiLight); | |
272 | DeleteObject(penLight); | |
273 | DeleteObject(penShadow); | |
274 | DeleteObject(penDkShadow); | |
275 | DeleteObject(brushFace); | |
276 | } | |
277 | ||
278 | #else | |
279 | ||
280 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
281 | { | |
282 | HPEN oldp; | |
283 | HPEN penBorder; | |
284 | HPEN penLight; | |
285 | HPEN penShadow; | |
286 | HBRUSH brushFace; | |
287 | ||
288 | // create needed pens and brush | |
289 | penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME)); | |
290 | penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW)); | |
291 | penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT)); | |
292 | brushFace = CreateSolidBrush(COLOR_BTNFACE); | |
293 | ||
294 | // draw the rectangle | |
295 | RECT rect; | |
296 | rect.left = left; | |
297 | rect.right = right; | |
298 | rect.top = top; | |
299 | rect.bottom = bottom; | |
300 | FillRect((HDC) dc, &rect, brushFace); | |
301 | ||
302 | // draw the border | |
303 | oldp = (HPEN) SelectObject( (HDC) dc, penBorder); | |
304 | MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top); | |
305 | MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1); | |
306 | MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
307 | MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
308 | ||
309 | SelectObject( (HDC) dc, penShadow); | |
310 | if (sel) | |
311 | { | |
312 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
313 | LineTo((HDC) dc, left+1 ,top+1); | |
314 | LineTo((HDC) dc, right-2 ,top+1); | |
315 | } | |
316 | else | |
317 | { | |
318 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
319 | LineTo((HDC) dc, right-2 ,bottom-2); | |
320 | LineTo((HDC) dc, right-2 ,top); | |
321 | ||
322 | MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL); | |
323 | LineTo((HDC) dc, right-3 ,bottom-3); | |
324 | LineTo((HDC) dc, right-3 ,top+1); | |
325 | ||
326 | SelectObject( (HDC) dc, penLight); | |
327 | ||
328 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL); | |
329 | LineTo((HDC) dc, left+1 ,top+1); | |
330 | LineTo((HDC) dc, right-2 ,top+1); | |
331 | } | |
332 | ||
333 | // delete allocated resources | |
334 | SelectObject((HDC) dc,oldp); | |
335 | DeleteObject(penBorder); | |
336 | DeleteObject(penLight); | |
337 | DeleteObject(penShadow); | |
338 | DeleteObject(brushFace); | |
339 | } | |
340 | ||
341 | #endif // defined(__WIN95__) | |
342 | ||
343 | ||
344 | void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
345 | { | |
346 | RECT rect; | |
347 | rect.left = left; | |
348 | rect.top = top; | |
349 | rect.right = right; | |
350 | rect.bottom = bottom; | |
351 | InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ); | |
352 | ||
353 | // GRG: the focus rectangle should not move when the button is pushed! | |
354 | /* | |
355 | if ( sel ) | |
356 | OffsetRect( &rect, 1, 1 ); | |
357 | */ | |
358 | (void)sel; | |
359 | DrawFocusRect( (HDC) dc, &rect ); | |
360 | } | |
361 | ||
362 | extern HBRUSH wxDisableButtonBrush; | |
363 | void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg ) | |
364 | { | |
365 | HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ); | |
366 | ||
367 | // VZ: what's this?? there is no such ROP AFAIK | |
368 | #ifdef __SALFORDC__ | |
369 | DWORD dwRop = 0xFA0089L; | |
370 | #else | |
371 | DWORD dwRop = 0xFA0089UL; | |
372 | #endif | |
373 | ||
374 | if ( with_marg ) | |
375 | { | |
376 | left += m_marginX; | |
377 | top += m_marginY; | |
378 | right -= 2 * m_marginX; | |
379 | bottom -= 2 * m_marginY; | |
380 | } | |
381 | ||
382 | ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop); | |
383 | ||
384 | ::SelectObject( (HDC) dc, old ); | |
385 | } | |
386 | ||
387 | void wxBitmapButton::SetDefault() | |
388 | { | |
389 | wxButton::SetDefault(); | |
390 | } |