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