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