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