]> git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
DrawLine normalized; wxStaticBitmap fixed for Dialog Editor
[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 // 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;
87 m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
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 }
100
101 #ifdef __WIN32__
102 // create a static control with either SS_BITMAP or SS_ICON style depending
103 // on what we have here
104 const wxChar *classname = wxT("STATIC");
105 int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
106 #else // Win16
107 const wxChar *classname = wxT("BUTTON");
108 int winstyle = BS_OWNERDRAW;
109 #endif // Win32
110
111 m_hWnd = (WXHWND)::CreateWindow
112 (
113 classname,
114 wxT(""),
115 // NOT DISABLED!!! We want to move it in Dialog Editor.
116 winstyle | WS_CHILD | WS_VISIBLE, // | WS_DISABLED,
117 0, 0, 0, 0,
118 (HWND)parent->GetHWND(),
119 (HMENU)m_windowId,
120 wxGetInstance(),
121 NULL
122 );
123
124 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
125
126 SetImage(icon ? icon : &bitmap);
127 delete icon; // may be NULL, ok
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
139 bool wxStaticBitmap::ImageIsOk() const
140 {
141 return m_image && m_image->Ok();
142 }
143
144 void wxStaticBitmap::Free()
145 {
146 delete m_image;
147
148 m_image = NULL;
149 }
150
151 wxSize wxStaticBitmap::DoGetBestSize() const
152 {
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();
156 }
157
158 void wxStaticBitmap::SetImage(const wxGDIImage* image)
159 {
160 Free();
161
162 const wxIcon *icon = wxDynamicCast(image, wxIcon);
163 m_isIcon = icon != NULL;
164 if ( m_isIcon )
165 {
166 m_image = new wxIcon(*icon);
167 }
168 else
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 }
177
178 int x, y;
179 int w, h;
180 GetPosition(&x, &y);
181 GetSize(&w, &h);
182
183 #ifdef __WIN32__
184 HANDLE handle = (HANDLE)m_image->GetHandle();
185 ::SendMessage(GetHwnd(), STM_SETIMAGE,
186 m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
187 #endif // Win32
188
189 if ( ImageIsOk() )
190 {
191 int width = image->GetWidth(),
192 height = image->GetHeight();
193 if ( width && height )
194 {
195 w = width;
196 h = height;
197
198 ::MoveWindow(GetHwnd(), x, y, width, height, FALSE);
199 }
200 }
201
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);
208 }
209
210 // under Win32 we use the standard static control style for this
211 #ifdef __WIN16__
212 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
213 {
214 LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
215
216 wxCHECK_MSG( !m_isIcon, FALSE, _T("icons not supported in wxStaticBitmap") );
217
218 wxBitmap* bitmap = (wxBitmap *)m_image;
219 if ( !bitmap->Ok() )
220 return FALSE;
221
222 HDC hDC = lpDIS->hDC;
223 HDC memDC = ::CreateCompatibleDC(hDC);
224
225 HBITMAP old = (HBITMAP) ::SelectObject(memDC, (HBITMAP) bitmap->GetHBITMAP());
226
227 if (!old)
228 return FALSE;
229
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;
234
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));
238
239 ::BitBlt(hDC, x1, y1, bitmap->GetWidth(), bitmap->GetHeight(), memDC, 0, 0, SRCCOPY);
240
241 ::SelectObject(memDC, old);
242
243 ::DeleteDC(memDC);
244
245 return TRUE;
246 }
247 #endif // Win16
248
249 // We need this or the control can never be moved e.g. in Dialog Editor.
250 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
251 WXWPARAM wParam,
252 WXLPARAM lParam)
253 {
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;
258
259 return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
260 }
261