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