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