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