]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
SCROLLWIN instead of SCROLL again
[wxWidgets.git] / src / msw / statbmp.cpp
CommitLineData
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
4004f41e 45IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
2bda0e17 46
9e3e0821
VZ
47// ===========================================================================
48// implementation
49// ===========================================================================
50
51// ---------------------------------------------------------------------------
52// wxStaticBitmap
53// ---------------------------------------------------------------------------
2bda0e17 54
debe6624 55bool 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
4004f41e
VZ
83 // we may have either bitmap or icon: if a bitmap with mask is passed, we
84 // will transform it to an icon ourselves because otherwise the mask will
85 // be ignored by Windows
86 wxIcon *icon = (wxIcon *)NULL;
9e3e0821 87 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
4004f41e
VZ
88 if ( !m_isIcon )
89 {
90 const wxBitmap& bmp = (const wxBitmap&)bitmap;
91 wxMask *mask = bmp.GetMask();
92 if ( mask && mask->GetMaskBitmap() )
93 {
94 icon = new wxIcon;
95 icon->CopyFromBitmap(bmp);
96
97 m_isIcon = TRUE;
98 }
99 }
9e3e0821
VZ
100
101#ifdef __WIN32__
102 // create a static control with either SS_BITMAP or SS_ICON style depending
103 // on what we have here
223d09f6 104 const wxChar *classname = wxT("STATIC");
9e3e0821
VZ
105 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
106#else // Win16
223d09f6 107 const wxChar *classname = wxT("BUTTON");
58a33cb4 108 int winstyle = BS_OWNERDRAW;
9e3e0821
VZ
109#endif // Win32
110
111 m_hWnd = (WXHWND)::CreateWindow
112 (
113 classname,
223d09f6 114 wxT(""),
4004f41e 115 winstyle | WS_CHILD | WS_VISIBLE | WS_DISABLED,
9e3e0821
VZ
116 0, 0, 0, 0,
117 (HWND)parent->GetHWND(),
118 (HMENU)m_windowId,
119 wxGetInstance(),
120 NULL
121 );
122
223d09f6 123 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
9e3e0821 124
4004f41e
VZ
125 SetImage(icon ? icon : &bitmap);
126 delete icon; // may be NULL, ok
9e3e0821
VZ
127
128 // Subclass again for purposes of dialog editing mode
129 SubclassWin(m_hWnd);
130
131 SetFont(GetParent()->GetFont());
132
133 SetSize(x, y, width, height);
134
135 return TRUE;
136}
137
138bool wxStaticBitmap::ImageIsOk() const
139{
0d0512bd 140 return m_image && m_image->Ok();
9e3e0821
VZ
141}
142
143void wxStaticBitmap::Free()
144{
0d0512bd 145 delete m_image;
9e3e0821 146
0d0512bd 147 m_image = NULL;
2bda0e17
KB
148}
149
f68586e5 150wxSize wxStaticBitmap::DoGetBestSize() const
2bda0e17 151{
4438caf4
VZ
152 // reuse the current size (as wxWindow does) instead of using some
153 // arbitrary default size (as wxControl, our immediate base class, does)
154 return wxWindow::DoGetBestSize();
2bda0e17
KB
155}
156
4004f41e 157void wxStaticBitmap::SetImage(const wxGDIImage* image)
2bda0e17 158{
9e3e0821
VZ
159 Free();
160
4004f41e
VZ
161 const wxIcon *icon = wxDynamicCast(image, wxIcon);
162 m_isIcon = icon != NULL;
9e3e0821 163 if ( m_isIcon )
4004f41e
VZ
164 {
165 m_image = new wxIcon(*icon);
166 }
9e3e0821 167 else
4004f41e
VZ
168 {
169 wxASSERT_MSG( wxDynamicCast(image, wxBitmap),
170 _T("not an icon and not a bitmap?") );
171
172 const wxBitmap *bitmap = (wxBitmap *)image;
173
174 m_image = new wxBitmap(*bitmap);
175 }
9e3e0821
VZ
176
177 int x, y;
178 int w, h;
179 GetPosition(&x, &y);
180 GetSize(&w, &h);
9e3e0821
VZ
181
182#ifdef __WIN32__
0d0512bd
VZ
183 HANDLE handle = (HANDLE)m_image->GetHandle();
184 ::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821
VZ
185 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
186#endif // Win32
187
188 if ( ImageIsOk() )
189 {
4004f41e
VZ
190 int width = image->GetWidth(),
191 height = image->GetHeight();
9e3e0821
VZ
192 if ( width && height )
193 {
7c545786
VZ
194 w = width;
195 h = height;
196
0d0512bd 197 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
198 }
199 }
200
0d0512bd
VZ
201 RECT rect;
202 rect.left = x;
203 rect.top = y;
204 rect.right = x + w;
205 rect.bottom = y + h;
206 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
207}
208
9e3e0821
VZ
209// under Win32 we use the standard static control style for this
210#ifdef __WIN16__
2bda0e17
KB
211bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
212{
2bda0e17
KB
213 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
214
8f177c8e
VZ
215 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
216
217 wxBitmap* bitmap = (wxBitmap *)m_image;
fd3f686c
VZ
218 if ( !bitmap->Ok() )
219 return FALSE;
2bda0e17 220
fd3f686c
VZ
221 HDC hDC = lpDIS->hDC;
222 HDC memDC = ::CreateCompatibleDC(hDC);
2bda0e17 223
fd3f686c 224 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17 225
fd3f686c
VZ
226 if (!old)
227 return FALSE;
2bda0e17 228
9e3e0821
VZ
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;
2bda0e17 233
fd3f686c
VZ
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));
2bda0e17 237
fd3f686c 238 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
2bda0e17 239
fd3f686c 240 ::SelectObject(memDC, old);
2bda0e17
KB
241
242 ::DeleteDC(memDC);
243
244 return TRUE;
245}
d1e418ea 246#endif // Win16
2bda0e17 247
d1e418ea 248// We need this or the control can never be moved e.g. in Dialog Editor.
9e3e0821
VZ
249long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
250 WXWPARAM wParam,
251 WXLPARAM lParam)
2bda0e17 252{
9e3e0821
VZ
253 // Ensure that static items get messages. Some controls don't like this
254 // message to be intercepted (e.g. RichEdit), hence the tests.
255 if ( nMsg == WM_NCHITTEST )
256 return (long)HTCLIENT;
2bda0e17 257
9e3e0821 258 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17 259}
d1e418ea 260