]> git.saurik.com Git - wxWidgets.git/blame - src/msw/bmpbuttn.cpp
Also the radiobox was unitialised.
[wxWidgets.git] / src / msw / bmpbuttn.cpp
CommitLineData
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
24#include "wx/bmpbuttn.h"
25#endif
26
27#include "wx/msw/private.h"
28
29#if !USE_SHARED_LIBRARY
30IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
31#endif
32
33#define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
34
debe6624 35bool wxBitmapButton::Create(wxWindow *parent, wxWindowID id, const wxBitmap& bitmap,
2bda0e17 36 const wxPoint& pos,
debe6624 37 const wxSize& size, long style,
2bda0e17
KB
38 const wxValidator& validator,
39 const wxString& name)
40{
41 m_buttonBitmap = bitmap;
42 SetName(name);
43 SetValidator(validator);
44
45 parent->AddChild(this);
46
fd71308f
JS
47 m_backgroundColour = parent->GetBackgroundColour() ;
48 m_foregroundColour = parent->GetForegroundColour() ;
2bda0e17
KB
49 m_windowStyle = style;
50 m_marginX = 0;
51 m_marginY = 0;
52
53 if ( style & wxBU_AUTODRAW )
54 {
fd3f686c
VZ
55 m_marginX = wxDEFAULT_BUTTON_MARGIN;
56 m_marginY = wxDEFAULT_BUTTON_MARGIN;
2bda0e17
KB
57 }
58
59 int x = pos.x;
60 int y = pos.y;
61 int width = size.x;
62 int height = size.y;
63
64 if (id == -1)
65 m_windowId = NewControlId();
66 else
67 m_windowId = id;
68
69 if ( width == -1 && bitmap.Ok())
fd3f686c 70 width = bitmap.GetWidth() + 2*m_marginX;
2bda0e17
KB
71
72 if ( height == -1 && bitmap.Ok())
fd3f686c 73 height = bitmap.GetHeight() + 2*m_marginY;
2bda0e17
KB
74
75 HWND wx_button =
76 CreateWindowEx(0, "BUTTON", "", BS_OWNERDRAW | WS_TABSTOP | WS_CHILD,
77 0, 0, 0, 0, (HWND) parent->GetHWND(), (HMENU)m_windowId,
78 wxGetInstance(), NULL);
79
80 m_hWnd = (WXHWND)wx_button;
81
82 // Subclass again for purposes of dialog editing mode
83 SubclassWin((WXHWND)wx_button);
84
c0ed460c 85 SetFont(parent->GetFont()) ;
2bda0e17
KB
86
87 SetSize(x, y, width, height);
88 ShowWindow(wx_button, SW_SHOW);
89
90 return TRUE;
91}
92
93void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap)
94{
95 m_buttonBitmap = bitmap;
96}
97
98bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item)
99{
2bda0e17 100#if defined(__WIN95__)
ba681060 101 long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
fd3f686c
VZ
102 if (style & BS_BITMAP)
103 {
104 // Should we call Default() here?
105// Default();
106
107 // Let default procedure draw the bitmap, which is defined
108 // in the Windows resource.
109 return FALSE;
110 }
2bda0e17
KB
111#endif
112
113 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
114
fd3f686c 115 wxBitmap* bitmap = &m_buttonBitmap;
2bda0e17
KB
116
117 UINT state = lpDIS->itemState;
118 if ((state & ODS_SELECTED) && m_buttonBitmapSelected.Ok())
119 bitmap = &m_buttonBitmapSelected;
120 else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok())
121 bitmap = &m_buttonBitmapFocus;
122 else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok())
123 bitmap = &m_buttonBitmapDisabled;
124
125 if ( !bitmap->Ok() )
126 return FALSE;
127
128 HDC hDC = lpDIS->hDC;
129 HDC memDC = ::CreateCompatibleDC(hDC);
130
c4e7c2aa 131 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17
KB
132
133 if (!old)
134 return FALSE;
135
2bda0e17
KB
136 int x = lpDIS->rcItem.left;
137 int y = lpDIS->rcItem.top;
138 int width = lpDIS->rcItem.right - x;
139 int height = lpDIS->rcItem.bottom - y;
140
141 // Draw the face, if auto-drawing
142 if ( GetWindowStyleFlag() & wxBU_AUTODRAW )
143 DrawFace((WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom,
144 ((state & ODS_SELECTED) == ODS_SELECTED));
145
fd3f686c
VZ
146 // Centre the bitmap in the control area
147 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
148 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
2bda0e17 149
fd3f686c
VZ
150 if ( (state & ODS_SELECTED) && (GetWindowStyleFlag() & wxBU_AUTODRAW) )
151 {
152 x1 ++;
153 y1 ++;
154 }
2bda0e17 155
fd3f686c 156 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
2bda0e17 157
fd3f686c
VZ
158 if ( (state & ODS_DISABLED) && (GetWindowStyleFlag() & wxBU_AUTODRAW) )
159 DrawButtonDisable( (WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, TRUE ) ;
160 else if ( (state & ODS_FOCUS) && (GetWindowStyleFlag() & wxBU_AUTODRAW) )
161 DrawButtonFocus( (WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, ((state & ODS_SELECTED) == ODS_SELECTED));
2bda0e17 162
fd3f686c 163 ::SelectObject(memDC, old);
2bda0e17
KB
164
165 ::DeleteDC(memDC);
166
167 return TRUE;
168}
169
170void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel )
171{
fd3f686c
VZ
172 HPEN oldp;
173 HBRUSH oldb ;
2bda0e17
KB
174
175 HPEN penBorder;
176 HPEN penLight;
177 HPEN penShadow;
178 HBRUSH brushFace;
179 COLORREF ms_color;
180
fd3f686c
VZ
181 ms_color = GetSysColor(COLOR_WINDOWFRAME) ;
182 penBorder = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 183
fd3f686c
VZ
184 ms_color = GetSysColor(COLOR_BTNSHADOW) ;
185 penShadow = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 186
fd3f686c
VZ
187 ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ;
188 penLight = CreatePen(PS_SOLID,0,ms_color) ;
2bda0e17 189
fd3f686c
VZ
190 ms_color = GetSysColor(COLOR_BTNFACE) ;
191 brushFace = CreateSolidBrush(ms_color) ;
2bda0e17 192
fd3f686c
VZ
193 oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ;
194 oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ;
195 Rectangle( (HDC) dc, left, top, right, bottom ) ;
196 SelectObject( (HDC) dc, penBorder) ;
2bda0e17
KB
197 MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top);
198 MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1);
199 MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1);
200 MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1);
201
fd3f686c
VZ
202 SelectObject( (HDC) dc, penShadow) ;
203 if (sel)
204 {
205 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
206 LineTo((HDC) dc, left+1 ,top+1) ;
207 LineTo((HDC) dc, right-2 ,top+1) ;
208 }
209 else
210 {
211 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
212 LineTo((HDC) dc, right-2 ,bottom-2) ;
213 LineTo((HDC) dc, right-2 ,top) ;
214 MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ;
215 LineTo((HDC) dc, right-3 ,bottom-3) ;
216 LineTo((HDC) dc, right-3 ,top+1) ;
217
218 SelectObject( (HDC) dc, penLight) ;
219
220 MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ;
221 LineTo((HDC) dc, left+1 ,top+1) ;
222 LineTo((HDC) dc, right-2 ,top+1) ;
223 }
224 SelectObject((HDC) dc,oldp) ;
225 SelectObject((HDC) dc,oldb) ;
2bda0e17
KB
226
227 DeleteObject(penBorder);
228 DeleteObject(penLight);
229 DeleteObject(penShadow);
230 DeleteObject(brushFace);
231}
232
233#define FOCUS_MARGIN 6
234
235void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel )
236{
fd3f686c
VZ
237 RECT rect;
238 rect.left = left;
239 rect.top = top;
240 rect.right = right;
241 rect.bottom = bottom;
242 InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ;
243 if ( sel )
244 OffsetRect( &rect, 1, 1 ) ;
245 DrawFocusRect( (HDC) dc, &rect ) ;
2bda0e17
KB
246}
247
248extern HBRUSH wxDisableButtonBrush;
249void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg )
250{
fd3f686c 251 HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ;
2bda0e17 252
fd3f686c
VZ
253 if ( with_marg )
254 ::PatBlt( (HDC) dc, left + m_marginX, top + m_marginY,
255 right - 2 * m_marginX, bottom - 2 * m_marginY,
ce3ed50d
JS
256#ifdef __SALFORDC__
257 0xfa0089L ) ;
258#else
259 0xfa0089UL ) ;
260#endif
261 else ::PatBlt( (HDC) dc, left, top, right, bottom,
262#ifdef __SALFORDC__
263 0xfa0089L ) ;
264#else
265 0xfa0089UL ) ;
266#endif
fd3f686c 267 ::SelectObject( (HDC) dc, old ) ;
2bda0e17
KB
268}
269