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