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