Bitmap button updates
[wxWidgets.git] / src / os2 / bmpbuttn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bmpbuttn.cpp
3 // Purpose: wxBitmapButton
4 // Author: David Webster
5 // Modified by:
6 // Created: 10/13/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if wxUSE_BMPBUTTON
16
17 #ifndef WX_PRECOMP
18 #include "wx/bmpbuttn.h"
19 #endif
20
21 #include "wx/os2/private.h"
22
23
24 IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton)
25
26 #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1)
27
28 bool wxBitmapButton::Create(
29 wxWindow* pParent
30 , wxWindowID vId
31 , const wxBitmap& rBitmap
32 , const wxPoint& rPos
33 , const wxSize& rSize
34 , long lStyle
35 #if wxUSE_VALIDATORS
36 , const wxValidator& rValidator
37 #endif
38 , const wxString& rsName
39 )
40 {
41 m_bmpNormal = rBitmap;
42 SetName(rsName);
43 #if wxUSE_VALIDATORS
44 SetValidator(rValidator);
45 #endif
46
47 pParent->AddChild(this);
48
49 m_backgroundColour = pParent->GetBackgroundColour() ;
50 m_foregroundColour = pParent->GetForegroundColour() ;
51 m_windowStyle = lStyle;
52
53 if (lStyle & wxBU_AUTODRAW)
54 {
55 m_marginX = wxDEFAULT_BUTTON_MARGIN;
56 m_marginY = wxDEFAULT_BUTTON_MARGIN;
57 }
58
59 int nX = rPos.x;
60 int nY = rPos.y;
61 int nWidth = rSize.x;
62 int nHeight = rSize.y;
63
64 if (vId == -1)
65 m_windowId = NewControlId();
66 else
67 m_windowId = vId;
68
69 if (nWidth == -1 && rBitmap.Ok())
70 nWidth = rBitmap.GetWidth() + 2 * m_marginX;
71
72 if (nHeight == -1 && rBitmap.Ok())
73 nHeight = rBitmap.GetHeight() + 2 * m_marginY;
74
75 ULONG ulOS2Style = WS_VISIBLE | WS_TABSTOP | BS_USERBUTTON;
76
77 if (m_windowStyle & wxCLIP_SIBLINGS)
78 ulOS2Style |= WS_CLIPSIBLINGS;
79
80 m_hWnd = (WXHWND)::WinCreateWindow( GetHwndOf(pParent)
81 ,WC_BUTTON
82 ,wxT("")
83 ,ulOS2Style
84 ,0, 0, 0, 0
85 ,GetHwndOf(pParent)
86 ,HWND_TOP
87 ,m_windowId
88 ,NULL
89 ,NULL
90 );
91
92 //
93 //Subclass again for purposes of dialog editing mode
94 //
95 SubclassWin(m_hWnd);
96 SetFont(*wxSMALL_FONT);
97 SetSize( nX
98 ,nY
99 ,nWidth
100 ,nHeight
101 );
102 return TRUE;
103 } // end of wxBitmapButton::Create
104
105 bool wxBitmapButton::OS2OnDraw(
106 WXDRAWITEMSTRUCT* pItem
107 )
108 {
109 PUSERBUTTON pUser = (PUSERBUTTON)pItem;
110 bool bAutoDraw = (GetWindowStyleFlag() & wxBU_AUTODRAW) != 0;
111
112 if (!pUser)
113 return FALSE;
114
115 wxBitmap* pBitmap;
116 RECTL vRect;
117 bool bIsSelected = pUser->fsState & BDS_HILITED;
118 wxClientDC vDc(this);
119
120 if (bIsSelected && m_bmpSelected.Ok())
121 pBitmap = &m_bmpSelected;
122 else if ((pUser->fsState & BDS_DEFAULT) && m_bmpFocus.Ok())
123 pBitmap = &m_bmpFocus;
124 else if ((pUser->fsState & BDS_DISABLED) && m_bmpDisabled.Ok())
125 pBitmap = &m_bmpDisabled;
126 else
127 pBitmap = &m_bmpNormal;
128
129 if (!pBitmap->Ok() )
130 return FALSE;
131
132
133 //
134 // Centre the bitmap in the control area
135 //
136 int nX = 0;
137 int nY = 0;
138 int nX1 = 0;
139 int nY1 = 0;
140 int nWidth = vDc.m_vRclPaint.xRight - vDc.m_vRclPaint.xLeft;
141 int nHeight = vDc.m_vRclPaint.xRight - vDc.m_vRclPaint.xLeft;
142 int nBmpWidth = pBitmap->GetWidth();
143 int nBmpHeight = pBitmap->GetHeight();
144
145 nX1 = nX + (nWidth - nBmpWidth) / 2;
146 nY1 = nX + (nHeight - nBmpHeight) / 2;
147
148 if (bIsSelected && bAutoDraw)
149 {
150 nX1++;
151 nY1++;
152 }
153
154 //
155 // Draw the button face
156 //
157 {
158 DrawFace( vDc
159 ,bIsSelected
160 );
161 }
162
163 //
164 // Draw the bitmap
165 //
166 vDc.DrawBitmap( *pBitmap
167 ,nX1
168 ,nY1
169 ,TRUE
170 );
171
172 //
173 // Draw focus / disabled state, if auto-drawing
174 //
175 if ((pUser->fsState == BDS_DISABLED) && bAutoDraw)
176 {
177 DrawButtonDisable( vDc
178 ,*pBitmap
179 );
180 }
181 else if ((pUser->fsState == BDS_DEFAULT) && bAutoDraw)
182 {
183 DrawButtonFocus(vDc);
184 }
185 return TRUE;
186 } // end of wxBitmapButton::OS2OnDraw
187
188 void wxBitmapButton::DrawFace (
189 wxClientDC& rDC
190 , bool bSel
191 )
192 {
193 //
194 // Set up drawing colors
195 //
196 wxPen vHiLitePen(wxColour(255, 255, 255), 1, wxSOLID); // White
197 wxPen vLitePen(wxColour(223, 223, 223), 1, wxSOLID); // Very Light Grey
198 wxPen vShadowPen(wxColour(191, 191, 191), 1, wxSOLID); // Medium Grey
199 wxPen vDarkShadowPen(wxColour(128, 128, 128), 1, wxSOLID);
200 wxColour vFaceColor(wxColour(204, 204, 204)); // Light Grey
201
202 //
203 // Draw the main button face
204 //
205 ::WinFillRect(rDC.GetHPS(), &rDC.m_vRclPaint, vFaceColor.GetPixel());
206
207 //
208 // Draw the border
209 //
210 rDC.SetPen(bSel ? vDarkShadowPen : vHiLitePen);
211 rDC.DrawLine( rDC.m_vRclPaint.xLeft
212 ,rDC.m_vRclPaint.yTop
213 ,rDC.m_vRclPaint.xRight - 1
214 ,rDC.m_vRclPaint.yTop
215 );
216 rDC.DrawLine( rDC.m_vRclPaint.xLeft
217 ,rDC.m_vRclPaint.yTop + 1
218 ,rDC.m_vRclPaint.xLeft
219 ,rDC.m_vRclPaint.yBottom - 1
220 );
221
222 rDC.SetPen(bSel ? vShadowPen : vLitePen);
223 rDC.DrawLine( rDC.m_vRclPaint.xLeft + 1
224 ,rDC.m_vRclPaint.yTop + 1
225 ,rDC.m_vRclPaint.xRight - 2
226 ,rDC.m_vRclPaint.yTop + 1
227 );
228 rDC.DrawLine( rDC.m_vRclPaint.xLeft + 1
229 ,rDC.m_vRclPaint.yTop + 2
230 ,rDC.m_vRclPaint.xLeft + 1
231 ,rDC.m_vRclPaint.yBottom - 2
232 );
233
234 rDC.SetPen(bSel ? vLitePen : vShadowPen);
235 rDC.DrawLine( rDC.m_vRclPaint.xLeft + 1
236 ,rDC.m_vRclPaint.yBottom - 2
237 ,rDC.m_vRclPaint.xRight - 1
238 ,rDC.m_vRclPaint.yBottom - 2
239 );
240 rDC.DrawLine( rDC.m_vRclPaint.xRight - 2
241 ,rDC.m_vRclPaint.yBottom - 3
242 ,rDC.m_vRclPaint.xRight - 2
243 ,rDC.m_vRclPaint.yTop
244 );
245
246 rDC.SetPen(bSel ? vDarkShadowPen : vHiLitePen);
247 rDC.DrawLine( rDC.m_vRclPaint.xLeft
248 ,rDC.m_vRclPaint.yBottom - 1
249 ,rDC.m_vRclPaint.xRight + 2
250 ,rDC.m_vRclPaint.yBottom - 1
251 );
252 rDC.DrawLine( rDC.m_vRclPaint.xRight - 1
253 ,rDC.m_vRclPaint.yBottom - 2
254 ,rDC.m_vRclPaint.xRight - 1
255 ,rDC.m_vRclPaint.yTop - 1
256 );
257 } // end of wxBitmapButton::DrawFace
258
259 void wxBitmapButton::DrawButtonFocus (
260 wxClientDC& rDC
261 )
262 {
263 wxPen vBlackPen(wxColour(0, 0, 0), 2, wxSOLID);
264
265 //
266 // Draw a thick black line around the outside of the button
267 //
268 rDC.SetPen(vBlackPen);
269 rDC.DrawLine( rDC.m_vRclPaint.xLeft
270 ,rDC.m_vRclPaint.yTop
271 ,rDC.m_vRclPaint.xRight
272 ,rDC.m_vRclPaint.yTop
273 );
274 rDC.DrawLine( rDC.m_vRclPaint.xRight
275 ,rDC.m_vRclPaint.yTop
276 ,rDC.m_vRclPaint.xRight
277 ,rDC.m_vRclPaint.yBottom
278 );
279 rDC.DrawLine( rDC.m_vRclPaint.xRight
280 ,rDC.m_vRclPaint.yBottom
281 ,rDC.m_vRclPaint.xLeft
282 ,rDC.m_vRclPaint.yBottom
283 );
284 rDC.DrawLine( rDC.m_vRclPaint.xLeft
285 ,rDC.m_vRclPaint.yBottom
286 ,rDC.m_vRclPaint.xLeft
287 ,rDC.m_vRclPaint.yTop
288 );
289 } // end of wxBitmapButton::DrawButtonFocus
290
291 void wxBitmapButton::DrawButtonDisable(
292 wxClientDC& rDC
293 , wxBitmap& rBmp
294 )
295 {
296 wxPen vGreyPen(wxColour(128, 128, 128), 2, wxSOLID);
297
298 //
299 // Draw a thick black line around the outside of the button
300 //
301 rDC.SetPen(vGreyPen);
302 rDC.DrawLine( rDC.m_vRclPaint.xLeft
303 ,rDC.m_vRclPaint.yTop
304 ,rDC.m_vRclPaint.xRight
305 ,rDC.m_vRclPaint.yTop
306 );
307 rDC.DrawLine( rDC.m_vRclPaint.xRight
308 ,rDC.m_vRclPaint.yTop
309 ,rDC.m_vRclPaint.xRight
310 ,rDC.m_vRclPaint.yBottom
311 );
312 rDC.DrawLine( rDC.m_vRclPaint.xRight
313 ,rDC.m_vRclPaint.yBottom
314 ,rDC.m_vRclPaint.xLeft
315 ,rDC.m_vRclPaint.yBottom
316 );
317 rDC.DrawLine( rDC.m_vRclPaint.xLeft
318 ,rDC.m_vRclPaint.yBottom
319 ,rDC.m_vRclPaint.xLeft
320 ,rDC.m_vRclPaint.yTop
321 );
322 wxDisableBitmap(rBmp, vGreyPen.GetColour().GetPixel());
323 } // end of wxBitmapButton::DrawButtonDisable
324
325 void wxBitmapButton::SetDefault()
326 {
327 wxButton::SetDefault();
328 }
329
330 #endif // ndef for wxUSE_BMPBUTTON
331