]> git.saurik.com Git - wxWidgets.git/blob - src/os2/statbmp.cpp
Use unsigned char for XBM bitmaps data.
[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 wxT("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 wxDELETE(m_pImage);
145 } // end of wxStaticBitmap::Free
146
147 wxSize wxStaticBitmap::DoGetBestSize() const
148 {
149 //
150 // Reuse the current size (as wxWindow does) instead of using some
151 // arbitrary default size (as wxControl, our immediate base class, does)
152 //
153 return wxWindow::DoGetBestSize();
154 }
155
156 void wxStaticBitmap::OnPaint ( wxPaintEvent& WXUNUSED(rEvent) )
157 {
158 wxPaintDC vDc(this);
159 wxBitmap* pBitmap;
160
161 if (m_pImage->IsKindOf(CLASSINFO(wxIcon)))
162 {
163 wxIcon* pIcon;
164
165 pIcon = wxDynamicCast(m_pImage, wxIcon);
166 pBitmap = new wxBitmap(*pIcon);
167 vDc.DrawBitmap(*pBitmap, 0, 0);
168 delete pBitmap;
169 }
170 else
171 {
172 pBitmap = wxDynamicCast(m_pImage, wxBitmap);
173 vDc.DrawBitmap(*pBitmap, 0, 0);
174 }
175 } // end of wxStaticBitmap::OnPaint
176
177 void wxStaticBitmap::SetImage( const wxGDIImage& rBitmap )
178 {
179 int nX = 0;
180 int nY = 0;
181 int nWidth = 0;
182 int nHeight = 0;
183
184 Free();
185 ::WinSendMsg( GetHwnd()
186 ,SM_SETHANDLE
187 ,MPFROMHWND(rBitmap.GetHandle())
188 ,NULL
189 );
190 m_pImage = ConvertImage(rBitmap);
191
192 GetPosition(&nX, &nY);
193 GetSize(&nWidth, &nHeight);
194 // Convert to OS/2 coordinate system
195 nY = wxWindow::GetOS2ParentHeight(GetParent()) - nY - nHeight;
196
197 RECTL vRect;
198
199 vRect.xLeft = nX;
200 vRect.yTop = nY + nHeight;
201 vRect.xRight = nX + nWidth;
202 vRect.yBottom = nY;
203
204 ::WinInvalidateRect(GetHwndOf(GetParent()), &vRect, TRUE);
205 }
206
207 MRESULT wxStaticBitmap::OS2WindowProc(
208 WXUINT uMsg
209 , WXWPARAM wParam
210 , WXLPARAM lParam
211 )
212 {
213 return wxWindow::OS2WindowProc(uMsg, wParam, lParam);
214 } // end of wxStaticBitmap::OS2WindowProc