]> git.saurik.com Git - wxWidgets.git/blob - src/os2/bmpbuttn.cpp
Fixed bug [ 657949 ] Segmentation fault in ~wxFileHistory
[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() + 4 * m_marginX;
71
72 if (nHeight == -1 && rBitmap.Ok())
73 nHeight = rBitmap.GetHeight() + 4 * 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), 2, wxSOLID); // White
197 wxPen vDarkShadowPen(wxColour(85, 85, 85), 2, wxSOLID);
198 wxColour vFaceColor(wxColour(204, 204, 204)); // Light Grey
199
200 //
201 // Draw the main button face
202 //
203 ::WinFillRect(rDC.GetHPS(), &rDC.m_vRclPaint, vFaceColor.GetPixel());
204
205 //
206 // Draw the border
207 //
208 rDC.SetPen(bSel ? vDarkShadowPen : vHiLitePen);
209 rDC.DrawLine( rDC.m_vRclPaint.xLeft + 1
210 ,rDC.m_vRclPaint.yTop - 1
211 ,rDC.m_vRclPaint.xRight - 1
212 ,rDC.m_vRclPaint.yTop - 1
213 );
214 rDC.DrawLine( rDC.m_vRclPaint.xLeft + 1
215 ,rDC.m_vRclPaint.yTop - 1
216 ,rDC.m_vRclPaint.xLeft + 1
217 ,rDC.m_vRclPaint.yBottom + 1
218 );
219
220 rDC.SetPen(bSel ? vHiLitePen : vDarkShadowPen);
221 rDC.DrawLine( rDC.m_vRclPaint.xLeft + 1
222 ,rDC.m_vRclPaint.yBottom + 1
223 ,rDC.m_vRclPaint.xRight - 1
224 ,rDC.m_vRclPaint.yBottom + 1
225 );
226 rDC.DrawLine( rDC.m_vRclPaint.xRight - 1
227 ,rDC.m_vRclPaint.yTop - 1
228 ,rDC.m_vRclPaint.xRight - 1
229 ,rDC.m_vRclPaint.yBottom + 1
230 );
231
232 } // end of wxBitmapButton::DrawFace
233
234 void wxBitmapButton::DrawButtonFocus (
235 wxClientDC& rDC
236 )
237 {
238 wxPen vBlackPen(wxColour(0, 0, 0), 2, wxSOLID);
239
240 //
241 // Draw a thick black line around the outside of the button
242 //
243 rDC.SetPen(vBlackPen);
244 rDC.DrawLine( rDC.m_vRclPaint.xLeft
245 ,rDC.m_vRclPaint.yTop
246 ,rDC.m_vRclPaint.xRight
247 ,rDC.m_vRclPaint.yTop
248 );
249 rDC.DrawLine( rDC.m_vRclPaint.xRight
250 ,rDC.m_vRclPaint.yTop
251 ,rDC.m_vRclPaint.xRight
252 ,rDC.m_vRclPaint.yBottom
253 );
254 rDC.DrawLine( rDC.m_vRclPaint.xRight
255 ,rDC.m_vRclPaint.yBottom
256 ,rDC.m_vRclPaint.xLeft
257 ,rDC.m_vRclPaint.yBottom
258 );
259 rDC.DrawLine( rDC.m_vRclPaint.xLeft
260 ,rDC.m_vRclPaint.yBottom
261 ,rDC.m_vRclPaint.xLeft
262 ,rDC.m_vRclPaint.yTop
263 );
264 } // end of wxBitmapButton::DrawButtonFocus
265
266 void wxBitmapButton::DrawButtonDisable(
267 wxClientDC& rDC
268 , wxBitmap& rBmp
269 )
270 {
271 wxPen vGreyPen(wxColour(128, 128, 128), 2, wxSOLID);
272
273 //
274 // Draw a thick black line around the outside of the button
275 //
276 rDC.SetPen(vGreyPen);
277 rDC.DrawLine( rDC.m_vRclPaint.xLeft
278 ,rDC.m_vRclPaint.yTop
279 ,rDC.m_vRclPaint.xRight
280 ,rDC.m_vRclPaint.yTop
281 );
282 rDC.DrawLine( rDC.m_vRclPaint.xRight
283 ,rDC.m_vRclPaint.yTop
284 ,rDC.m_vRclPaint.xRight
285 ,rDC.m_vRclPaint.yBottom
286 );
287 rDC.DrawLine( rDC.m_vRclPaint.xRight
288 ,rDC.m_vRclPaint.yBottom
289 ,rDC.m_vRclPaint.xLeft
290 ,rDC.m_vRclPaint.yBottom
291 );
292 rDC.DrawLine( rDC.m_vRclPaint.xLeft
293 ,rDC.m_vRclPaint.yBottom
294 ,rDC.m_vRclPaint.xLeft
295 ,rDC.m_vRclPaint.yTop
296 );
297 wxDisableBitmap(rBmp, vGreyPen.GetColour().GetPixel());
298 } // end of wxBitmapButton::DrawButtonDisable
299
300 void wxBitmapButton::SetDefault()
301 {
302 wxButton::SetDefault();
303 }
304
305 #endif // ndef for wxUSE_BMPBUTTON
306