]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
added configtool, helpview and emulator makefiles
[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$
6c9a19aa
JS
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
9e3e0821
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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
1e6feb95
VZ
31#if wxUSE_STATBMP
32
0c589ad0
BM
33#include "wx/window.h"
34#include "wx/msw/private.h"
35
2bda0e17 36#ifndef WX_PRECOMP
0c589ad0 37 #include "wx/icon.h"
9e3e0821 38 #include "wx/statbmp.h"
2bda0e17
KB
39#endif
40
41#include <stdio.h>
2bda0e17 42
9e3e0821
VZ
43// ---------------------------------------------------------------------------
44// macors
45// ---------------------------------------------------------------------------
46
4004f41e 47IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
2bda0e17 48
066f1b7a
SC
49/*
50 TODO PROPERTIES :
51 bitmap
52*/
53
9e3e0821
VZ
54// ===========================================================================
55// implementation
56// ===========================================================================
57
58// ---------------------------------------------------------------------------
59// wxStaticBitmap
60// ---------------------------------------------------------------------------
2bda0e17 61
d8bffc13
MB
62// we may have either bitmap or icon: if a bitmap with mask is passed, we
63// will transform it to an icon ourselves because otherwise the mask will
64// be ignored by Windows
65// note that this function will create a new object every time
66// it is called even if the image needs no conversion
67
68#ifndef __WIN16__
69
70static wxGDIImage* ConvertImage( const wxGDIImage& bitmap )
71{
72 bool isIcon = bitmap.IsKindOf( CLASSINFO(wxIcon) );
73
74 if( !isIcon )
75 {
76 wxASSERT_MSG( wxDynamicCast(&bitmap, wxBitmap),
77 _T("not an icon and not a bitmap?") );
78
79 const wxBitmap& bmp = (const wxBitmap&)bitmap;
80 wxMask *mask = bmp.GetMask();
81 if ( mask && mask->GetMaskBitmap() )
82 {
83 wxIcon* icon = new wxIcon;
84 icon->CopyFromBitmap(bmp);
85
86 return icon;
87 }
88
89 return new wxBitmap( bmp );
90 }
91
92 // copying a bitmap is a cheap operation
93 return new wxIcon( (const wxIcon&)bitmap );
94}
95
96#endif
97
46a5e01e
VZ
98bool wxStaticBitmap::Create(wxWindow *parent,
99 wxWindowID id,
0d0512bd 100 const wxGDIImage& bitmap,
9e3e0821
VZ
101 const wxPoint& pos,
102 const wxSize& size,
103 long style,
104 const wxString& name)
2bda0e17 105{
46a5e01e
VZ
106 if ( !CreateControl(parent, id, pos, size, style, wxDefaultValidator, name) )
107 return FALSE;
9e3e0821 108
4004f41e
VZ
109 // we may have either bitmap or icon: if a bitmap with mask is passed, we
110 // will transform it to an icon ourselves because otherwise the mask will
111 // be ignored by Windows
d8bffc13 112 wxGDIImage *image = (wxGDIImage *)NULL;
9e3e0821 113 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
669f7a11
JS
114
115#ifdef __WIN16__
116 wxASSERT_MSG( !m_isIcon, "Icons are not supported in wxStaticBitmap under WIN16." );
d8bffc13 117 image = &bitmap;
46a5e01e 118#else // Win32
d8bffc13
MB
119 image = ConvertImage( bitmap );
120 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
46a5e01e 121#endif // Win16/32
9e3e0821 122
46a5e01e
VZ
123 // create the native control
124 if ( !MSWCreateControl(
9e3e0821 125#ifdef __WIN32__
46a5e01e 126 _T("STATIC"),
9e3e0821 127#else // Win16
46a5e01e
VZ
128 _T("BUTTON"),
129#endif // Win32/16
fda7962d 130 wxEmptyString, pos, size) )
46a5e01e
VZ
131 {
132 // control creation failed
133 return FALSE;
134 }
9e3e0821 135
d8bffc13 136 // no need to delete the new image
46a5e01e 137 SetImageNoCopy(image);
9e3e0821 138
46a5e01e
VZ
139 return TRUE;
140}
9e3e0821 141
65bc172c
VZ
142wxBorder wxStaticBitmap::GetDefaultBorder() const
143{
144 return wxBORDER_NONE;
145}
146
46a5e01e
VZ
147WXDWORD wxStaticBitmap::MSWGetStyle(long style, WXDWORD *exstyle) const
148{
149 WXDWORD msStyle = wxControl::MSWGetStyle(style, exstyle);
9e3e0821 150
46a5e01e
VZ
151#ifdef __WIN32__
152 // what kind of control are we?
153 msStyle |= m_isIcon ? SS_ICON : SS_BITMAP;
9e3e0821 154
46a5e01e
VZ
155 // we use SS_CENTERIMAGE to prevent the control from resizing the bitmap to
156 // fit to its size -- this is unexpected and doesn't happen in other ports
157 msStyle |= SS_CENTERIMAGE;
158#else // Win16
159 msStyle |= BS_OWNERDRAW;
160#endif // Win32/16
161
162 return msStyle;
9e3e0821
VZ
163}
164
165bool wxStaticBitmap::ImageIsOk() const
166{
0d0512bd 167 return m_image && m_image->Ok();
9e3e0821
VZ
168}
169
170void wxStaticBitmap::Free()
171{
0d0512bd 172 delete m_image;
9e3e0821 173
0d0512bd 174 m_image = NULL;
2bda0e17
KB
175}
176
f68586e5 177wxSize wxStaticBitmap::DoGetBestSize() const
2bda0e17 178{
4438caf4
VZ
179 // reuse the current size (as wxWindow does) instead of using some
180 // arbitrary default size (as wxControl, our immediate base class, does)
181 return wxWindow::DoGetBestSize();
2bda0e17
KB
182}
183
d8bffc13 184void wxStaticBitmap::SetImage( const wxGDIImage* image )
2bda0e17 185{
d8bffc13
MB
186 wxGDIImage* convertedImage = ConvertImage( *image );
187 SetImageNoCopy( convertedImage );
188}
4004f41e 189
d8bffc13
MB
190void wxStaticBitmap::SetImageNoCopy( wxGDIImage* image)
191{
192 Free();
4004f41e 193
d8bffc13
MB
194 m_isIcon = image->IsKindOf( CLASSINFO(wxIcon) );
195 // the image has already been copied
196 m_image = image;
9e3e0821
VZ
197
198 int x, y;
199 int w, h;
200 GetPosition(&x, &y);
201 GetSize(&w, &h);
9e3e0821
VZ
202
203#ifdef __WIN32__
0d0512bd 204 HANDLE handle = (HANDLE)m_image->GetHandle();
d8bffc13
MB
205 LONG style = ::GetWindowLong( (HWND)GetHWND(), GWL_STYLE ) ;
206 ::SetWindowLong( (HWND)GetHWND(), GWL_STYLE, ( style & ~( SS_BITMAP|SS_ICON ) ) |
207 ( m_isIcon ? SS_ICON : SS_BITMAP ) );
0d0512bd 208 ::SendMessage(GetHwnd(), STM_SETIMAGE,
9e3e0821
VZ
209 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
210#endif // Win32
211
212 if ( ImageIsOk() )
213 {
4004f41e
VZ
214 int width = image->GetWidth(),
215 height = image->GetHeight();
9e3e0821
VZ
216 if ( width && height )
217 {
7c545786
VZ
218 w = width;
219 h = height;
220
0d0512bd 221 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
9e3e0821
VZ
222 }
223 }
224
0d0512bd
VZ
225 RECT rect;
226 rect.left = x;
227 rect.top = y;
228 rect.right = x + w;
229 rect.bottom = y + h;
230 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
2bda0e17
KB
231}
232
9e3e0821
VZ
233// under Win32 we use the standard static control style for this
234#ifdef __WIN16__
2bda0e17
KB
235bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
236{
2bda0e17
KB
237 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
238
8f177c8e
VZ
239 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
240
241 wxBitmap* bitmap = (wxBitmap *)m_image;
fd3f686c
VZ
242 if ( !bitmap->Ok() )
243 return FALSE;
2bda0e17 244
fd3f686c
VZ
245 HDC hDC = lpDIS->hDC;
246 HDC memDC = ::CreateCompatibleDC(hDC);
2bda0e17 247
fd3f686c 248 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
2bda0e17 249
fd3f686c
VZ
250 if (!old)
251 return FALSE;
2bda0e17 252
9e3e0821
VZ
253 int x = lpDIS->rcItem.left;
254 int y = lpDIS->rcItem.top;
255 int width = lpDIS->rcItem.right - x;
256 int height = lpDIS->rcItem.bottom - y;
2bda0e17 257
fd3f686c
VZ
258 // Centre the bitmap in the control area
259 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
260 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
2bda0e17 261
fd3f686c 262 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
2bda0e17 263
fd3f686c 264 ::SelectObject(memDC, old);
2bda0e17
KB
265
266 ::DeleteDC(memDC);
267
268 return TRUE;
269}
d1e418ea 270#endif // Win16
2bda0e17 271
d1e418ea 272// We need this or the control can never be moved e.g. in Dialog Editor.
9e3e0821
VZ
273long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
274 WXWPARAM wParam,
275 WXLPARAM lParam)
2bda0e17 276{
4676948b 277#ifndef __WXWINCE__
9e3e0821
VZ
278 // Ensure that static items get messages. Some controls don't like this
279 // message to be intercepted (e.g. RichEdit), hence the tests.
280 if ( nMsg == WM_NCHITTEST )
281 return (long)HTCLIENT;
4676948b 282#endif
2bda0e17 283
9e3e0821 284 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17 285}
d1e418ea 286
1e6feb95 287#endif // wxUSE_STATBMP