]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: statbmp.cpp | |
3 | // Purpose: wxStaticBitmap | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
fd3f686c | 9 | // Licence: wxWindows license |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
9e3e0821 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 | 20 | #ifdef __GNUG__ |
9e3e0821 | 21 | #pragma implementation "statbmp.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
9e3e0821 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
0c589ad0 BM |
31 | #include "wx/window.h" |
32 | #include "wx/msw/private.h" | |
33 | ||
2bda0e17 | 34 | #ifndef WX_PRECOMP |
0c589ad0 | 35 | #include "wx/icon.h" |
9e3e0821 | 36 | #include "wx/statbmp.h" |
2bda0e17 KB |
37 | #endif |
38 | ||
39 | #include <stdio.h> | |
2bda0e17 | 40 | |
9e3e0821 VZ |
41 | // --------------------------------------------------------------------------- |
42 | // macors | |
43 | // --------------------------------------------------------------------------- | |
44 | ||
9e3e0821 | 45 | IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl) |
2bda0e17 | 46 | |
9e3e0821 VZ |
47 | // =========================================================================== |
48 | // implementation | |
49 | // =========================================================================== | |
50 | ||
51 | // --------------------------------------------------------------------------- | |
52 | // wxStaticBitmap | |
53 | // --------------------------------------------------------------------------- | |
2bda0e17 | 54 | |
debe6624 | 55 | bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id, |
0d0512bd | 56 | const wxGDIImage& bitmap, |
9e3e0821 VZ |
57 | const wxPoint& pos, |
58 | const wxSize& size, | |
59 | long style, | |
60 | const wxString& name) | |
2bda0e17 | 61 | { |
9e3e0821 VZ |
62 | Init(); |
63 | ||
64 | SetName(name); | |
65 | if (parent) | |
66 | parent->AddChild(this); | |
67 | ||
68 | m_backgroundColour = parent->GetBackgroundColour() ; | |
69 | m_foregroundColour = parent->GetForegroundColour() ; | |
70 | ||
71 | if ( id == -1 ) | |
72 | m_windowId = (int)NewControlId(); | |
73 | else | |
74 | m_windowId = id; | |
75 | ||
76 | int x = pos.x; | |
77 | int y = pos.y; | |
78 | int width = size.x; | |
79 | int height = size.y; | |
80 | ||
81 | m_windowStyle = style; | |
82 | ||
83 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); | |
84 | ||
85 | #ifdef __WIN32__ | |
86 | // create a static control with either SS_BITMAP or SS_ICON style depending | |
87 | // on what we have here | |
223d09f6 | 88 | const wxChar *classname = wxT("STATIC"); |
9e3e0821 VZ |
89 | int winstyle = m_isIcon ? SS_ICON : SS_BITMAP; |
90 | #else // Win16 | |
223d09f6 | 91 | const wxChar *classname = wxT("BUTTON"); |
58a33cb4 | 92 | int winstyle = BS_OWNERDRAW; |
9e3e0821 VZ |
93 | #endif // Win32 |
94 | ||
95 | m_hWnd = (WXHWND)::CreateWindow | |
96 | ( | |
97 | classname, | |
223d09f6 | 98 | wxT(""), |
9e3e0821 VZ |
99 | winstyle | WS_CHILD | WS_VISIBLE, |
100 | 0, 0, 0, 0, | |
101 | (HWND)parent->GetHWND(), | |
102 | (HMENU)m_windowId, | |
103 | wxGetInstance(), | |
104 | NULL | |
105 | ); | |
106 | ||
223d09f6 | 107 | wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") ); |
9e3e0821 | 108 | |
0d0512bd | 109 | SetImage(bitmap); |
9e3e0821 VZ |
110 | |
111 | // Subclass again for purposes of dialog editing mode | |
112 | SubclassWin(m_hWnd); | |
113 | ||
114 | SetFont(GetParent()->GetFont()); | |
115 | ||
116 | SetSize(x, y, width, height); | |
117 | ||
118 | return TRUE; | |
119 | } | |
120 | ||
121 | bool wxStaticBitmap::ImageIsOk() const | |
122 | { | |
0d0512bd | 123 | return m_image && m_image->Ok(); |
9e3e0821 VZ |
124 | } |
125 | ||
126 | void wxStaticBitmap::Free() | |
127 | { | |
0d0512bd | 128 | delete m_image; |
9e3e0821 | 129 | |
0d0512bd | 130 | m_image = NULL; |
2bda0e17 KB |
131 | } |
132 | ||
f68586e5 | 133 | wxSize wxStaticBitmap::DoGetBestSize() const |
2bda0e17 | 134 | { |
4438caf4 VZ |
135 | // reuse the current size (as wxWindow does) instead of using some |
136 | // arbitrary default size (as wxControl, our immediate base class, does) | |
137 | return wxWindow::DoGetBestSize(); | |
2bda0e17 KB |
138 | } |
139 | ||
0d0512bd | 140 | void wxStaticBitmap::SetImage(const wxGDIImage& bitmap) |
2bda0e17 | 141 | { |
9e3e0821 VZ |
142 | Free(); |
143 | ||
144 | m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon)); | |
145 | if ( m_isIcon ) | |
0d0512bd | 146 | m_image = new wxIcon((const wxIcon&)bitmap); |
9e3e0821 | 147 | else |
0d0512bd | 148 | m_image = new wxBitmap((const wxBitmap &)bitmap); |
9e3e0821 VZ |
149 | |
150 | int x, y; | |
151 | int w, h; | |
152 | GetPosition(&x, &y); | |
153 | GetSize(&w, &h); | |
9e3e0821 VZ |
154 | |
155 | #ifdef __WIN32__ | |
0d0512bd VZ |
156 | HANDLE handle = (HANDLE)m_image->GetHandle(); |
157 | ::SendMessage(GetHwnd(), STM_SETIMAGE, | |
9e3e0821 VZ |
158 | m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle); |
159 | #endif // Win32 | |
160 | ||
161 | if ( ImageIsOk() ) | |
162 | { | |
163 | int width = bitmap.GetWidth(), | |
164 | height = bitmap.GetHeight(); | |
165 | if ( width && height ) | |
166 | { | |
7c545786 VZ |
167 | w = width; |
168 | h = height; | |
169 | ||
0d0512bd | 170 | ::MoveWindow(GetHwnd(), x, y, width, height, FALSE); |
9e3e0821 VZ |
171 | } |
172 | } | |
173 | ||
0d0512bd VZ |
174 | RECT rect; |
175 | rect.left = x; | |
176 | rect.top = y; | |
177 | rect.right = x + w; | |
178 | rect.bottom = y + h; | |
179 | InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE); | |
2bda0e17 KB |
180 | } |
181 | ||
9e3e0821 VZ |
182 | // under Win32 we use the standard static control style for this |
183 | #ifdef __WIN16__ | |
2bda0e17 KB |
184 | bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item) |
185 | { | |
2bda0e17 KB |
186 | LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item; |
187 | ||
8f177c8e VZ |
188 | wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") ); |
189 | ||
190 | wxBitmap* bitmap = (wxBitmap *)m_image; | |
fd3f686c VZ |
191 | if ( !bitmap->Ok() ) |
192 | return FALSE; | |
2bda0e17 | 193 | |
fd3f686c VZ |
194 | HDC hDC = lpDIS->hDC; |
195 | HDC memDC = ::CreateCompatibleDC(hDC); | |
2bda0e17 | 196 | |
fd3f686c | 197 | HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP()); |
2bda0e17 | 198 | |
fd3f686c VZ |
199 | if (!old) |
200 | return FALSE; | |
2bda0e17 | 201 | |
9e3e0821 VZ |
202 | int x = lpDIS->rcItem.left; |
203 | int y = lpDIS->rcItem.top; | |
204 | int width = lpDIS->rcItem.right - x; | |
205 | int height = lpDIS->rcItem.bottom - y; | |
2bda0e17 | 206 | |
fd3f686c VZ |
207 | // Centre the bitmap in the control area |
208 | int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2)); | |
209 | int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2)); | |
2bda0e17 | 210 | |
fd3f686c | 211 | ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY); |
2bda0e17 | 212 | |
fd3f686c | 213 | ::SelectObject(memDC, old); |
2bda0e17 KB |
214 | |
215 | ::DeleteDC(memDC); | |
216 | ||
217 | return TRUE; | |
218 | } | |
219 | ||
9e3e0821 VZ |
220 | long wxStaticBitmap::MSWWindowProc(WXUINT nMsg, |
221 | WXWPARAM wParam, | |
222 | WXLPARAM lParam) | |
2bda0e17 | 223 | { |
9e3e0821 VZ |
224 | // Ensure that static items get messages. Some controls don't like this |
225 | // message to be intercepted (e.g. RichEdit), hence the tests. | |
226 | if ( nMsg == WM_NCHITTEST ) | |
227 | return (long)HTCLIENT; | |
2bda0e17 | 228 | |
9e3e0821 | 229 | return wxWindow::MSWWindowProc(nMsg, wParam, lParam); |
2bda0e17 | 230 | } |
9e3e0821 | 231 | #endif // Win16 |