Applied patch [ 832096 ] Final separation for GUI and console for Open Watcom
[wxWidgets.git] / src / msw / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 #if wxUSE_BMPBUTTON
24
25 #ifndef WX_PRECOMP
26 #include "wx/bmpbuttn.h"
27 #include "wx/log.h"
28 #include "wx/dcmemory.h"
29 #endif
30
31 #include "wx/msw/private.h"
32
33 // ----------------------------------------------------------------------------
34 // macros
35 // ----------------------------------------------------------------------------
36
37 #if wxUSE_EXTENDED_RTTI
38
39 WX_DEFINE_FLAGS( wxBitmapButtonStyle )
40
41 wxBEGIN_FLAGS( wxBitmapButtonStyle )
42 // new style border flags, we put them first to
43 // use them for streaming out
44 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
45 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
46 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
47 wxFLAGS_MEMBER(wxBORDER_RAISED)
48 wxFLAGS_MEMBER(wxBORDER_STATIC)
49 wxFLAGS_MEMBER(wxBORDER_NONE)
50
51 // old style border flags
52 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
53 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
54 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
55 wxFLAGS_MEMBER(wxRAISED_BORDER)
56 wxFLAGS_MEMBER(wxSTATIC_BORDER)
57 wxFLAGS_MEMBER(wxBORDER)
58
59 // standard window styles
60 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
61 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
62 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
63 wxFLAGS_MEMBER(wxWANTS_CHARS)
64 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
65 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
66 wxFLAGS_MEMBER(wxVSCROLL)
67 wxFLAGS_MEMBER(wxHSCROLL)
68
69 wxFLAGS_MEMBER(wxBU_AUTODRAW)
70 wxFLAGS_MEMBER(wxBU_LEFT)
71 wxFLAGS_MEMBER(wxBU_RIGHT)
72 wxFLAGS_MEMBER(wxBU_TOP)
73 wxFLAGS_MEMBER(wxBU_BOTTOM)
74 wxEND_FLAGS( wxBitmapButtonStyle )
75
76 IMPLEMENT_DYNAMIC_CLASS_XTI(wxBitmapButton, wxButton,"wx/bmpbuttn.h")
77
78 wxBEGIN_PROPERTIES_TABLE(wxBitmapButton)
79 wxPROPERTY_FLAGS( WindowStyle , wxBitmapButtonStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
80 wxEND_PROPERTIES_TABLE()
81
82 wxBEGIN_HANDLERS_TABLE(wxBitmapButton)
83 wxEND_HANDLERS_TABLE()
84
85 wxCONSTRUCTOR_5( wxBitmapButton , wxWindow* , Parent , wxWindowID , Id , wxBitmap , Bitmap , wxPoint , Position , wxSize , Size )
86
87 #else
88 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
89 #endif
90
91 /*
92 TODO PROPERTIES :
93
94 long "style" , wxBU_AUTODRAW
95 bool "default" , 0
96 bitmap "selected" ,
97 bitmap "focus" ,
98 bitmap "disabled" ,
99 */
100
101 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
102
103 wxBitmapButtonBase::wxBitmapButtonBase()
104 : m_bmpNormal(),
105 m_bmpSelected(),
106 m_bmpFocus(),
107 m_bmpDisabled(),
108 m_marginX(0),
109 m_marginY(0)
110 {
111 }
112
113 bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
114 const wxPoint& pos,
115 const wxSize& size, long style,
116 const wxValidator& wxVALIDATOR_PARAM(validator),
117 const wxString& name)
118 {
119 m_bmpNormal = bitmap;
120 SetName(name);
121
122 #if wxUSE_VALIDATORS
123 SetValidator(validator);
124 #endif // wxUSE_VALIDATORS
125
126 parent->AddChild(this);
127
128 m_backgroundColour = parent->GetBackgroundColour();
129 m_foregroundColour = parent->GetForegroundColour();
130 m_windowStyle = style;
131
132 if ( style & wxBU_AUTODRAW )
133 {
134 m_marginX = wxDEFAULT_BUTTON_MARGIN;
135 m_marginY = wxDEFAULT_BUTTON_MARGIN;
136 }
137
138 int x = pos.x;
139 int y = pos.y;
140 int width = size.x;
141 int height = size.y;
142
143 if (id == -1)
144 m_windowId = NewControlId();
145 else
146 m_windowId = id;
147
148 if ( width == -1 && bitmap.Ok())
149 width = bitmap.GetWidth() + 2*m_marginX;
150
151 if ( height == -1 && bitmap.Ok())
152 height = bitmap.GetHeight() + 2*m_marginY;
153
154 long msStyle = WS_VISIBLE | WS_TABSTOP | WS_CHILD | BS_OWNERDRAW ;
155
156 if ( m_windowStyle & wxCLIP_SIBLINGS )
157 msStyle |= WS_CLIPSIBLINGS;
158
159 #ifdef __WIN32__
160 if(m_windowStyle & wxBU_LEFT)
161 msStyle |= BS_LEFT;
162 if(m_windowStyle & wxBU_RIGHT)
163 msStyle |= BS_RIGHT;
164 if(m_windowStyle & wxBU_TOP)
165 msStyle |= BS_TOP;
166 if(m_windowStyle & wxBU_BOTTOM)
167 msStyle |= BS_BOTTOM;
168 #endif
169
170 m_hWnd = (WXHWND)CreateWindowEx
171 (
172 0,
173 wxT("BUTTON"),
174 wxEmptyString,
175 msStyle,
176 0, 0, 0, 0,
177 GetWinHwnd(parent),
178 (HMENU)m_windowId,
179 wxGetInstance(),
180 NULL
181 );
182
183 // Subclass again for purposes of dialog editing mode
184 SubclassWin(m_hWnd);
185
186 SetFont(parent->GetFont());
187
188 SetSize(x, y, width, height);
189
190 return TRUE;
191 }
192
193 // VZ: should be at the very least less than wxDEFAULT_BUTTON_MARGIN
194 #define FOCUS_MARGIN 3
195
196 bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
197 {
198 #ifndef __WXWINCE__
199 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
200 if (style & BS_BITMAP)
201 {
202 // Let default procedure draw the bitmap, which is defined
203 // in the Windows resource.
204 return FALSE;
205 }
206 #endif
207
208 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
209 HDC hDC = lpDIS->hDC;
210 UINT state = lpDIS->itemState;
211 bool isSelected = (state & ODS_SELECTED) != 0;
212 bool autoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
213
214
215 // choose the bitmap to use depending on the button state
216 wxBitmap* bitmap;
217
218 if ( isSelected && m_bmpSelected.Ok() )
219 bitmap = &m_bmpSelected;
220 else if ((state & ODS_FOCUS) && m_bmpFocus.Ok())
221 bitmap = &m_bmpFocus;
222 else if ((state & ODS_DISABLED) && m_bmpDisabled.Ok())
223 bitmap = &m_bmpDisabled;
224 else
225 bitmap = &m_bmpNormal;
226
227 if ( !bitmap->Ok() )
228 return FALSE;
229
230 // centre the bitmap in the control area
231 int x = lpDIS->rcItem.left;
232 int y = lpDIS->rcItem.top;
233 int width = lpDIS->rcItem.right - x;
234 int height = lpDIS->rcItem.bottom - y;
235 int wBmp = bitmap->GetWidth();
236 int hBmp = bitmap->GetHeight();
237
238 int x1,y1;
239
240 if(m_windowStyle & wxBU_LEFT)
241 x1 = x + (FOCUS_MARGIN+1);
242 else if(m_windowStyle & wxBU_RIGHT)
243 x1 = x + (width - wBmp) - (FOCUS_MARGIN+1);
244 else
245 x1 = x + (width - wBmp) / 2;
246
247 if(m_windowStyle & wxBU_TOP)
248 y1 = y + (FOCUS_MARGIN+1);
249 else if(m_windowStyle & wxBU_BOTTOM)
250 y1 = y + (height - hBmp) - (FOCUS_MARGIN+1);
251 else
252 y1 = y + (height - hBmp) / 2;
253
254 if ( isSelected && autoDraw )
255 {
256 x1++;
257 y1++;
258 }
259
260 // draw the face, if auto-drawing
261 if ( autoDraw )
262 {
263 DrawFace((WXHDC) hDC,
264 lpDIS->rcItem.left, lpDIS->rcItem.top,
265 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
266 isSelected);
267 }
268
269 // draw the bitmap
270 wxDC dst;
271 dst.SetHDC((WXHDC) hDC, FALSE);
272 dst.DrawBitmap(*bitmap, x1, y1, TRUE);
273
274 // draw focus / disabled state, if auto-drawing
275 if ( (state & ODS_DISABLED) && autoDraw )
276 {
277 DrawButtonDisable((WXHDC) hDC,
278 lpDIS->rcItem.left, lpDIS->rcItem.top,
279 lpDIS->rcItem.right, lpDIS->rcItem.bottom,
280 TRUE);
281 }
282 else if ( (state & ODS_FOCUS) && autoDraw )
283 {
284 DrawButtonFocus((WXHDC) hDC,
285 lpDIS->rcItem.left,
286 lpDIS->rcItem.top,
287 lpDIS->rcItem.right,
288 lpDIS->rcItem.bottom,
289 isSelected);
290 }
291
292 return TRUE;
293 }
294
295 // GRG Feb/2000, support for bmp buttons with Win95/98 standard LNF
296
297 #if defined(__WIN95__)
298
299 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
300 {
301 HPEN oldp;
302 HPEN penHiLight;
303 HPEN penLight;
304 HPEN penShadow;
305 HPEN penDkShadow;
306 HBRUSH brushFace;
307
308 // create needed pens and brush
309 penHiLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DHILIGHT));
310 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DLIGHT));
311 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DSHADOW));
312 penDkShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_3DDKSHADOW));
313 // brushFace = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
314 // Taking the background colour fits in better with
315 // Windows XP themes.
316 brushFace = CreateSolidBrush(m_backgroundColour.m_pixel);
317
318 // draw the rectangle
319 RECT rect;
320 rect.left = left;
321 rect.right = right;
322 rect.top = top;
323 rect.bottom = bottom;
324 FillRect((HDC) dc, &rect, brushFace);
325
326 // draw the border
327 oldp = (HPEN) SelectObject( (HDC) dc, sel? penDkShadow : penHiLight);
328
329 wxDrawLine((HDC) dc, left, top, right-1, top);
330 wxDrawLine((HDC) dc, left, top+1, left, bottom-1);
331
332 SelectObject( (HDC) dc, sel? penShadow : penLight);
333 wxDrawLine((HDC) dc, left+1, top+1, right-2, top+1);
334 wxDrawLine((HDC) dc, left+1, top+2, left+1, bottom-2);
335
336 SelectObject( (HDC) dc, sel? penLight : penShadow);
337 wxDrawLine((HDC) dc, left+1, bottom-2, right-1, bottom-2);
338 wxDrawLine((HDC) dc, right-2, bottom-3, right-2, top);
339
340 SelectObject( (HDC) dc, sel? penHiLight : penDkShadow);
341 wxDrawLine((HDC) dc, left, bottom-1, right+2, bottom-1);
342 wxDrawLine((HDC) dc, right-1, bottom-2, right-1, top-1);
343
344 // delete allocated resources
345 SelectObject((HDC) dc,oldp);
346 DeleteObject(penHiLight);
347 DeleteObject(penLight);
348 DeleteObject(penShadow);
349 DeleteObject(penDkShadow);
350 DeleteObject(brushFace);
351 }
352
353 #else
354
355 void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
356 {
357 HPEN oldp;
358 HPEN penBorder;
359 HPEN penLight;
360 HPEN penShadow;
361 HBRUSH brushFace;
362
363 // create needed pens and brush
364 penBorder = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
365 penShadow = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNSHADOW));
366 penLight = CreatePen(PS_SOLID, 0, GetSysColor(COLOR_BTNHIGHLIGHT));
367 brushFace = CreateSolidBrush(COLOR_BTNFACE);
368
369 // draw the rectangle
370 RECT rect;
371 rect.left = left;
372 rect.right = right;
373 rect.top = top;
374 rect.bottom = bottom;
375 FillRect((HDC) dc, &rect, brushFace);
376
377 // draw the border
378 oldp = (HPEN) SelectObject( (HDC) dc, penBorder);
379 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
380 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
381 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
382 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
383
384 SelectObject( (HDC) dc, penShadow);
385 if (sel)
386 {
387 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
388 LineTo((HDC) dc, left+1 ,top+1);
389 LineTo((HDC) dc, right-2 ,top+1);
390 }
391 else
392 {
393 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
394 LineTo((HDC) dc, right-2 ,bottom-2);
395 LineTo((HDC) dc, right-2 ,top);
396
397 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL);
398 LineTo((HDC) dc, right-3 ,bottom-3);
399 LineTo((HDC) dc, right-3 ,top+1);
400
401 SelectObject( (HDC) dc, penLight);
402
403 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL);
404 LineTo((HDC) dc, left+1 ,top+1);
405 LineTo((HDC) dc, right-2 ,top+1);
406 }
407
408 // delete allocated resources
409 SelectObject((HDC) dc,oldp);
410 DeleteObject(penBorder);
411 DeleteObject(penLight);
412 DeleteObject(penShadow);
413 DeleteObject(brushFace);
414 }
415
416 #endif // defined(__WIN95__)
417
418
419 void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
420 {
421 RECT rect;
422 rect.left = left;
423 rect.top = top;
424 rect.right = right;
425 rect.bottom = bottom;
426 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN );
427
428 // GRG: the focus rectangle should not move when the button is pushed!
429 /*
430 if ( sel )
431 OffsetRect( &rect, 1, 1 );
432 */
433 (void)sel;
434 DrawFocusRect( (HDC) dc, &rect );
435 }
436
437 extern HBRUSH wxDisableButtonBrush;
438 void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
439 {
440 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush );
441
442 // VZ: what's this?? there is no such ROP AFAIK
443 #ifdef __SALFORDC__
444 DWORD dwRop = 0xFA0089L;
445 #else
446 DWORD dwRop = 0xFA0089UL;
447 #endif
448
449 if ( with_marg )
450 {
451 left += m_marginX;
452 top += m_marginY;
453 right -= 2 * m_marginX;
454 bottom -= 2 * m_marginY;
455 }
456
457 ::PatBlt( (HDC) dc, left, top, right, bottom, dwRop);
458
459 ::SelectObject( (HDC) dc, old );
460 }
461
462 void wxBitmapButton::SetDefault()
463 {
464 wxButton::SetDefault();
465 }
466
467 #endif // wxUSE_BMPBUTTON