Fixed sizing&positioning and an error in subclassing.
[wxWidgets.git] / src / os2 / statbmp.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: statbmp.cpp
3 // Purpose: wxStaticBitmap
4 // Author: David Webster
5 // Modified by:
6 // Created: 11/27/99
7 // RCS-ID: $Id$
8 // Copyright: (c) David Webster
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #include "wx/dcclient.h"
16 #include "wx/window.h"
17 #include "wx/os2/private.h"
18
19 #ifndef WX_PRECOMP
20 #include "wx/icon.h"
21 #include "wx/statbmp.h"
22 #endif
23
24 #include <stdio.h>
25
26 // ---------------------------------------------------------------------------
27 // macros
28 // ---------------------------------------------------------------------------
29
30 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
31
32 BEGIN_EVENT_TABLE(wxStaticBitmap, wxWindow)
33 EVT_PAINT(wxStaticBitmap::OnPaint)
34 END_EVENT_TABLE()
35
36 static wxGDIImage* ConvertImage(
37 const wxGDIImage& rBitmap
38 )
39 {
40 bool bIsIcon = rBitmap.IsKindOf( CLASSINFO(wxIcon) );
41
42 if(!bIsIcon )
43 {
44 wxASSERT_MSG( wxDynamicCast(&rBitmap, wxBitmap),
45 _T("not an icon and not a bitmap?") );
46
47 const wxBitmap& rBmp = (const wxBitmap&)rBitmap;
48 wxMask* pMask = rBmp.GetMask();
49
50 if (pMask && pMask->GetMaskBitmap())
51 {
52 wxIcon* pIcon = new wxIcon;
53
54 pIcon->CopyFromBitmap(rBmp);
55 return pIcon;
56 }
57 return new wxBitmap(rBmp);
58 }
59
60 // copying a bitmap is a cheap operation
61 return new wxIcon( (const wxIcon&)rBitmap );
62 } // end of ConvertImage
63
64 // ---------------------------------------------------------------------------
65 // wxStaticBitmap
66 // ---------------------------------------------------------------------------
67
68 bool wxStaticBitmap::Create( wxWindow* pParent,
69 wxWindowID nId,
70 const wxGDIImage& rBitmap,
71 const wxPoint& rPos,
72 const wxSize& WXUNUSED(rSize),
73 long lStyle,
74 const wxString& rName )
75 {
76 ERRORID vError;
77 wxString sError;
78
79 Init();
80
81 SetName(rName);
82 if (pParent)
83 pParent->AddChild(this);
84
85 if (nId == -1)
86 m_windowId = (int)NewControlId();
87 else
88 m_windowId = nId;
89
90 m_windowStyle = lStyle;
91
92 int nX= rPos.x;
93 int nY = rPos.y;
94 char zId[16];
95
96 m_windowStyle = lStyle;
97
98 m_bIsIcon = rBitmap.IsKindOf(CLASSINFO(wxIcon));
99
100 //
101 // For now we only support an ICON
102 //
103 int nWinstyle = SS_ICON;
104
105 m_hWnd = (WXHWND)::WinCreateWindow( pParent->GetHWND()
106 ,(PSZ)wxCanvasClassName
107 ,zId
108 ,nWinstyle | WS_VISIBLE
109 ,0,0,0,0
110 ,pParent->GetHWND()
111 ,HWND_TOP
112 ,m_windowId
113 ,NULL
114 ,NULL
115 );
116 if (!m_hWnd)
117 {
118 vError = ::WinGetLastError(wxGetInstance());
119 sError = wxPMErrorToStr(vError);
120 return false;
121 }
122 wxCHECK_MSG( m_hWnd, FALSE, wxT("Failed to create static bitmap") );
123 m_pImage = ConvertImage(rBitmap);
124 ::WinSendMsg( m_hWnd,
125 SM_SETHANDLE,
126 MPFROMHWND(rBitmap.GetHandle()),
127 (MPARAM)0);
128
129 // Subclass again for purposes of dialog editing mode
130 SubclassWin(m_hWnd);
131 SetSize(nX, nY, m_pImage->GetWidth(), m_pImage->GetHeight());
132
133 return true;
134 } // end of wxStaticBitmap::Create
135
136 bool wxStaticBitmap::ImageIsOk() const
137 {
138 return(m_pImage && m_pImage->Ok());
139 }
140
141 void wxStaticBitmap::Free()
142 {
143 if (m_pImage)
144 delete m_pImage;
145 m_pImage = NULL;
146 } // end of wxStaticBitmap::Free
147
148 wxSize wxStaticBitmap::DoGetBestSize() const
149 {
150 //
151 // Reuse the current size (as wxWindow does) instead of using some
152 // arbitrary default size (as wxControl, our immediate base class, does)
153 //
154 return wxWindow::DoGetBestSize();
155 }
156
157 void wxStaticBitmap::OnPaint (
158 wxPaintEvent& WXUNUSED(rEvent)
159 )
160 {
161 wxPaintDC vDc(this);
162 wxBitmap* pBitmap;
163
164 if (m_pImage->IsKindOf(CLASSINFO(wxIcon)))
165 {
166 wxIcon* pIcon;
167
168 pIcon = wxDynamicCast(m_pImage, wxIcon);
169 pBitmap = new wxBitmap(*pIcon);
170 vDc.DrawBitmap(*pBitmap, 0, 0);
171 delete pBitmap;
172 }
173 else
174 {
175 pBitmap = wxDynamicCast(m_pImage, wxBitmap);
176 vDc.DrawBitmap(*pBitmap, 0, 0);
177 }
178 } // end of wxStaticBitmap::OnPaint
179
180 void wxStaticBitmap::SetImage(
181 const wxGDIImage& rBitmap
182 )
183 {
184 int nX = 0;
185 int nY = 0;
186 int nWidth = 0;
187 int nHeight = 0;
188
189 Free();
190 ::WinSendMsg( GetHwnd()
191 ,SM_SETHANDLE
192 ,MPFROMHWND(rBitmap.GetHandle())
193 ,NULL
194 );
195 m_pImage = ConvertImage(rBitmap);
196
197 GetPosition(&nX, &nY);
198 GetSize(&nWidth, &nHeight);
199 // Convert to OS/2 coordinate system
200 nY = wxWindow::GetOS2ParentHeight(GetParent()) - nY - nHeight;
201
202 RECTL vRect;
203
204 vRect.xLeft = nX;
205 vRect.yTop = nY + nHeight;
206 vRect.xRight = nX + nWidth;
207 vRect.yBottom = nY;
208
209 ::WinInvalidateRect(GetHwndOf(GetParent()), &vRect, TRUE);
210 }
211
212 MRESULT wxStaticBitmap::OS2WindowProc(
213 WXUINT uMsg
214 , WXWPARAM wParam
215 , WXLPARAM lParam
216 )
217 {
218 return wxWindow::OS2WindowProc(uMsg, wParam, lParam);
219 } // end of wxStaticBitmap::OS2WindowProc