]> git.saurik.com Git - wxWidgets.git/blame - src/msw/statbmp.cpp
::GetWindowText() -> wxGetWindowText
[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
31#ifndef WX_PRECOMP
9e3e0821 32 #include "wx/statbmp.h"
2bda0e17
KB
33#endif
34
35#include <stdio.h>
36#include "wx/msw/private.h"
37
9e3e0821
VZ
38// ---------------------------------------------------------------------------
39// macors
40// ---------------------------------------------------------------------------
41
2bda0e17 42#if !USE_SHARED_LIBRARY
9e3e0821 43 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
2bda0e17
KB
44#endif
45
9e3e0821
VZ
46// ===========================================================================
47// implementation
48// ===========================================================================
49
50// ---------------------------------------------------------------------------
51// wxStaticBitmap
52// ---------------------------------------------------------------------------
2bda0e17 53
debe6624 54bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id,
9e3e0821
VZ
55 const wxBitmap& bitmap,
56 const wxPoint& pos,
57 const wxSize& size,
58 long style,
59 const wxString& name)
2bda0e17 60{
9e3e0821
VZ
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";
58a33cb4 91 int winstyle = BS_OWNERDRAW;
9e3e0821
VZ
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
120bool 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
130void 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;
2bda0e17
KB
138}
139
bfc6fde4 140void wxStaticBitmap::DoSetSize(int x, int y, int width, int height, int sizeFlags)
2bda0e17 141{
9e3e0821
VZ
142 int currentX, currentY;
143 GetPosition(&currentX, &currentY);
144 int x1 = x;
145 int y1 = y;
2bda0e17 146
9e3e0821
VZ
147 if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
148 x1 = currentX;
149 if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
150 y1 = currentY;
2bda0e17 151
9e3e0821 152 AdjustForParentClientOrigin(x1, y1, sizeFlags);
81d66cf3 153
9e3e0821
VZ
154 int actualWidth = width;
155 int actualHeight = height;
2bda0e17 156
9e3e0821
VZ
157 int ww, hh;
158 GetSize(&ww, &hh);
2bda0e17 159
9e3e0821
VZ
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;
2bda0e17 165
9e3e0821
VZ
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;
2bda0e17 171
9e3e0821 172 MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE);
2bda0e17
KB
173}
174
175void wxStaticBitmap::SetBitmap(const wxBitmap& bitmap)
176{
9e3e0821
VZ
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);
2bda0e17
KB
209}
210
9e3e0821
VZ
211// under Win32 we use the standard static control style for this
212#ifdef __WIN16__
2bda0e17
KB
213bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
214{
2bda0e17
KB
215 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
216
9e3e0821 217 wxBitmap* bitmap = m_image.bitmap;
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}
246
9e3e0821
VZ
247long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
248 WXWPARAM wParam,
249 WXLPARAM lParam)
2bda0e17 250{
9e3e0821
VZ
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;
2bda0e17 255
9e3e0821 256 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
2bda0e17 257}
9e3e0821 258#endif // Win16