]>
Commit | Line | Data |
---|---|---|
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 | |
9 | // Licence: wxWindows license | |
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 | |
30 | IMPLEMENT_DYNAMIC_CLASS(wxBitmapButton, wxButton) | |
31 | #endif | |
32 | ||
33 | #define BUTTON_HEIGHT_FACTOR (EDIT_CONTROL_FACTOR * 1.1) | |
34 | ||
debe6624 | 35 | bool 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 | ||
47 | m_backgroundColour = parent->GetDefaultBackgroundColour() ; | |
48 | m_foregroundColour = parent->GetDefaultForegroundColour() ; | |
49 | m_windowStyle = style; | |
50 | m_marginX = 0; | |
51 | m_marginY = 0; | |
52 | ||
53 | if ( style & wxBU_AUTODRAW ) | |
54 | { | |
55 | m_marginX = wxDEFAULT_BUTTON_MARGIN; | |
56 | m_marginY = wxDEFAULT_BUTTON_MARGIN; | |
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()) | |
70 | width = bitmap.GetWidth() + 2*m_marginX; | |
71 | ||
72 | if ( height == -1 && bitmap.Ok()) | |
73 | height = bitmap.GetHeight() + 2*m_marginY; | |
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 | ||
85 | // TODO? If in future we have a facility for having a label as well | |
86 | // as a bitmap, set the font. | |
87 | // SetFont(parent->GetFont()) ; | |
88 | ||
89 | SetSize(x, y, width, height); | |
90 | ShowWindow(wx_button, SW_SHOW); | |
91 | ||
92 | return TRUE; | |
93 | } | |
94 | ||
95 | void wxBitmapButton::SetBitmapLabel(const wxBitmap& bitmap) | |
96 | { | |
97 | m_buttonBitmap = bitmap; | |
98 | } | |
99 | ||
100 | bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item) | |
101 | { | |
102 | long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE); | |
103 | #if defined(__WIN95__) | |
104 | if (style & BS_BITMAP) | |
105 | { | |
106 | // Should we call Default() here? | |
107 | // Default(); | |
108 | ||
109 | // Let default procedure draw the bitmap, which is defined | |
110 | // in the Windows resource. | |
111 | return FALSE; | |
112 | } | |
113 | #endif | |
114 | ||
115 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; | |
116 | ||
117 | wxBitmap* bitmap = &m_buttonBitmap; | |
118 | ||
119 | UINT state = lpDIS->itemState; | |
120 | if ((state & ODS_SELECTED) && m_buttonBitmapSelected.Ok()) | |
121 | bitmap = &m_buttonBitmapSelected; | |
122 | else if ((state & ODS_FOCUS) && m_buttonBitmapFocus.Ok()) | |
123 | bitmap = &m_buttonBitmapFocus; | |
124 | else if ((state & ODS_DISABLED) && m_buttonBitmapDisabled.Ok()) | |
125 | bitmap = &m_buttonBitmapDisabled; | |
126 | ||
127 | if ( !bitmap->Ok() ) | |
128 | return FALSE; | |
129 | ||
130 | HDC hDC = lpDIS->hDC; | |
131 | HDC memDC = ::CreateCompatibleDC(hDC); | |
132 | ||
c4e7c2aa | 133 | HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP()); |
2bda0e17 KB |
134 | |
135 | if (!old) | |
136 | return FALSE; | |
137 | ||
138 | RECT rect = lpDIS->rcItem; | |
139 | ||
140 | int x = lpDIS->rcItem.left; | |
141 | int y = lpDIS->rcItem.top; | |
142 | int width = lpDIS->rcItem.right - x; | |
143 | int height = lpDIS->rcItem.bottom - y; | |
144 | ||
145 | // Draw the face, if auto-drawing | |
146 | if ( GetWindowStyleFlag() & wxBU_AUTODRAW ) | |
147 | DrawFace((WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, | |
148 | ((state & ODS_SELECTED) == ODS_SELECTED)); | |
149 | ||
150 | // Centre the bitmap in the control area | |
151 | int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2)); | |
152 | int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2)); | |
153 | ||
154 | if ( (state & ODS_SELECTED) && (GetWindowStyleFlag() & wxBU_AUTODRAW) ) | |
155 | { | |
156 | x1 ++; | |
157 | y1 ++; | |
158 | } | |
159 | ||
160 | ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY); | |
161 | ||
162 | if ( (state & ODS_DISABLED) && (GetWindowStyleFlag() & wxBU_AUTODRAW) ) | |
163 | DrawButtonDisable( (WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, TRUE ) ; | |
164 | else if ( (state & ODS_FOCUS) && (GetWindowStyleFlag() & wxBU_AUTODRAW) ) | |
165 | DrawButtonFocus( (WXHDC) hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom, ((state & ODS_SELECTED) == ODS_SELECTED)); | |
166 | ||
167 | ::SelectObject(memDC, old); | |
168 | ||
169 | ::DeleteDC(memDC); | |
170 | ||
171 | return TRUE; | |
172 | } | |
173 | ||
174 | void wxBitmapButton::DrawFace( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
175 | { | |
176 | HPEN oldp; | |
177 | HBRUSH oldb ; | |
178 | ||
179 | HPEN penBorder; | |
180 | HPEN penLight; | |
181 | HPEN penShadow; | |
182 | HBRUSH brushFace; | |
183 | COLORREF ms_color; | |
184 | ||
185 | ms_color = GetSysColor(COLOR_WINDOWFRAME) ; | |
186 | penBorder = CreatePen(PS_SOLID,0,ms_color) ; | |
187 | ||
188 | ms_color = GetSysColor(COLOR_BTNSHADOW) ; | |
189 | penShadow = CreatePen(PS_SOLID,0,ms_color) ; | |
190 | ||
191 | ms_color = GetSysColor(COLOR_BTNHIGHLIGHT) ; | |
192 | penLight = CreatePen(PS_SOLID,0,ms_color) ; | |
193 | ||
194 | ms_color = GetSysColor(COLOR_BTNFACE) ; | |
195 | brushFace = CreateSolidBrush(ms_color) ; | |
196 | ||
c4e7c2aa JS |
197 | oldp = (HPEN) SelectObject( (HDC) dc, GetStockObject( NULL_PEN ) ) ; |
198 | oldb = (HBRUSH) SelectObject( (HDC) dc, brushFace ) ; | |
2bda0e17 KB |
199 | Rectangle( (HDC) dc, left, top, right, bottom ) ; |
200 | SelectObject( (HDC) dc, penBorder) ; | |
201 | MoveToEx((HDC) dc,left+1,top,NULL);LineTo((HDC) dc,right-1,top); | |
202 | MoveToEx((HDC) dc,left,top+1,NULL);LineTo((HDC) dc,left,bottom-1); | |
203 | MoveToEx((HDC) dc,left+1,bottom-1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
204 | MoveToEx((HDC) dc,right-1,top+1,NULL);LineTo((HDC) dc,right-1,bottom-1); | |
205 | ||
206 | SelectObject( (HDC) dc, penShadow) ; | |
207 | if (sel) | |
208 | { | |
209 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ; | |
210 | LineTo((HDC) dc, left+1 ,top+1) ; | |
211 | LineTo((HDC) dc, right-2 ,top+1) ; | |
212 | } | |
213 | else | |
214 | { | |
215 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ; | |
216 | LineTo((HDC) dc, right-2 ,bottom-2) ; | |
217 | LineTo((HDC) dc, right-2 ,top) ; | |
218 | MoveToEx((HDC) dc,left+2 ,bottom-3 ,NULL) ; | |
219 | LineTo((HDC) dc, right-3 ,bottom-3) ; | |
220 | LineTo((HDC) dc, right-3 ,top+1) ; | |
221 | ||
222 | SelectObject( (HDC) dc, penLight) ; | |
223 | ||
224 | MoveToEx((HDC) dc,left+1 ,bottom-2 ,NULL) ; | |
225 | LineTo((HDC) dc, left+1 ,top+1) ; | |
226 | LineTo((HDC) dc, right-2 ,top+1) ; | |
227 | } | |
228 | SelectObject((HDC) dc,oldp) ; | |
229 | SelectObject((HDC) dc,oldb) ; | |
230 | ||
231 | DeleteObject(penBorder); | |
232 | DeleteObject(penLight); | |
233 | DeleteObject(penShadow); | |
234 | DeleteObject(brushFace); | |
235 | } | |
236 | ||
237 | #define FOCUS_MARGIN 6 | |
238 | ||
239 | void wxBitmapButton::DrawButtonFocus( WXHDC dc, int left, int top, int right, int bottom, bool sel ) | |
240 | { | |
241 | RECT rect; | |
242 | rect.left = left; | |
243 | rect.top = top; | |
244 | rect.right = right; | |
245 | rect.bottom = bottom; | |
246 | InflateRect( &rect, - FOCUS_MARGIN, - FOCUS_MARGIN ) ; | |
247 | if ( sel ) | |
248 | OffsetRect( &rect, 1, 1 ) ; | |
249 | DrawFocusRect( (HDC) dc, &rect ) ; | |
250 | } | |
251 | ||
252 | extern HBRUSH wxDisableButtonBrush; | |
253 | void wxBitmapButton::DrawButtonDisable( WXHDC dc, int left, int top, int right, int bottom, bool with_marg ) | |
254 | { | |
c4e7c2aa | 255 | HBRUSH old = (HBRUSH) SelectObject( (HDC) dc, wxDisableButtonBrush ) ; |
2bda0e17 KB |
256 | |
257 | if ( with_marg ) | |
258 | ::PatBlt( (HDC) dc, left + m_marginX, top + m_marginY, | |
259 | right - 2 * m_marginX, bottom - 2 * m_marginY, | |
260 | 0xfa0089ul ) ; | |
261 | else ::PatBlt( (HDC) dc, left, top, right, bottom, 0xfa0089ul ) ; | |
262 | ||
263 | ::SelectObject( (HDC) dc, old ) ; | |
264 | } | |
265 |