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