]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
SN: Disable some features not supported by EMX (#ifndef __EMX__)
[wxWidgets.git] / src / msw / statbmp.cpp
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
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "statbmp.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/window.h"
32 #include "wx/msw/private.h"
33
34 #ifndef WX_PRECOMP
35 #include "wx/icon.h"
36 #include "wx/statbmp.h"
37 #endif
38
39 #include <stdio.h>
40
41 // ---------------------------------------------------------------------------
42 // macors
43 // ---------------------------------------------------------------------------
44
45 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
46
47 // ===========================================================================
48 // implementation
49 // ===========================================================================
50
51 // ---------------------------------------------------------------------------
52 // wxStaticBitmap
53 // ---------------------------------------------------------------------------
54
55 bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id,
56 const wxGDIImage& bitmap,
57 const wxPoint& pos,
58 const wxSize& size,
59 long style,
60 const wxString& name)
61 {
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
83 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
84
85 #ifdef __WIN32__
86 // create a static control with either SS_BITMAP or SS_ICON style depending
87 // on what we have here
88 const wxChar *classname = wxT("STATIC");
89 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
90 #else // Win16
91 const wxChar *classname = wxT("BUTTON");
92 int winstyle = BS_OWNERDRAW;
93 #endif // Win32
94
95 m_hWnd = (WXHWND)::CreateWindow
96 (
97 classname,
98 wxT(""),
99 winstyle | WS_CHILD | WS_VISIBLE,
100 0, 0, 0, 0,
101 (HWND)parent->GetHWND(),
102 (HMENU)m_windowId,
103 wxGetInstance(),
104 NULL
105 );
106
107 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
108
109 SetImage(bitmap);
110
111 // Subclass again for purposes of dialog editing mode
112 SubclassWin(m_hWnd);
113
114 SetFont(GetParent()->GetFont());
115
116 SetSize(x, y, width, height);
117
118 return TRUE;
119 }
120
121 bool wxStaticBitmap::ImageIsOk() const
122 {
123 return m_image && m_image->Ok();
124 }
125
126 void wxStaticBitmap::Free()
127 {
128 delete m_image;
129
130 m_image = NULL;
131 }
132
133 wxSize wxStaticBitmap::DoGetBestSize() const
134 {
135 // reuse the current size (as wxWindow does) instead of using some
136 // arbitrary default size (as wxControl, our immediate base class, does)
137 return wxWindow::DoGetBestSize();
138 }
139
140 void wxStaticBitmap::SetImage(const wxGDIImage& bitmap)
141 {
142 Free();
143
144 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
145 if ( m_isIcon )
146 m_image = new wxIcon((const wxIcon&)bitmap);
147 else
148 m_image = new wxBitmap((const wxBitmap &)bitmap);
149
150 int x, y;
151 int w, h;
152 GetPosition(&x, &y);
153 GetSize(&w, &h);
154
155 #ifdef __WIN32__
156 HANDLE handle = (HANDLE)m_image->GetHandle();
157 ::SendMessage(GetHwnd(), STM_SETIMAGE,
158 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
159 #endif // Win32
160
161 if ( ImageIsOk() )
162 {
163 int width = bitmap.GetWidth(),
164 height = bitmap.GetHeight();
165 if ( width && height )
166 {
167 w = width;
168 h = height;
169
170 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
171 }
172 }
173
174 RECT rect;
175 rect.left = x;
176 rect.top = y;
177 rect.right = x + w;
178 rect.bottom = y + h;
179 InvalidateRect(GetHwndOf(GetParent()), &rect, TRUE);
180 }
181
182 // under Win32 we use the standard static control style for this
183 #ifdef __WIN16__
184 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
185 {
186 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
187
188 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
189
190 wxBitmap* bitmap = (wxBitmap *)m_image;
191 if ( !bitmap->Ok() )
192 return FALSE;
193
194 HDC hDC = lpDIS->hDC;
195 HDC memDC = ::CreateCompatibleDC(hDC);
196
197 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
198
199 if (!old)
200 return FALSE;
201
202 int x = lpDIS->rcItem.left;
203 int y = lpDIS->rcItem.top;
204 int width = lpDIS->rcItem.right - x;
205 int height = lpDIS->rcItem.bottom - y;
206
207 // Centre the bitmap in the control area
208 int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
209 int y1 = (int) (y + ((height - bitmap->GetHeight()) / 2));
210
211 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
212
213 ::SelectObject(memDC, old);
214
215 ::DeleteDC(memDC);
216
217 return TRUE;
218 }
219 #endif // Win16
220
221 // We need this or the control can never be moved e.g. in Dialog Editor.
222 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
223 WXWPARAM wParam,
224 WXLPARAM lParam)
225 {
226 // Ensure that static items get messages. Some controls don't like this
227 // message to be intercepted (e.g. RichEdit), hence the tests.
228 if ( nMsg == WM_NCHITTEST )
229 return (long)HTCLIENT;
230
231 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
232 }
233