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