]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/statbmp.cpp
   1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     wxStaticBitmap 
   4 // Author:      Julian Smart 
   8 // Copyright:   (c) Julian Smart and Markus Holzem 
   9 // Licence:     wxWindows license 
  10 ///////////////////////////////////////////////////////////////////////////// 
  12 // =========================================================================== 
  14 // =========================================================================== 
  16 // --------------------------------------------------------------------------- 
  18 // --------------------------------------------------------------------------- 
  21     #pragma implementation "statbmp.h" 
  24 // For compilers that support precompilation, includes "wx.h". 
  25 #include "wx/wxprec.h" 
  33 #include "wx/window.h" 
  34 #include "wx/msw/private.h" 
  38     #include "wx/statbmp.h" 
  43 // --------------------------------------------------------------------------- 
  45 // --------------------------------------------------------------------------- 
  47 IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap
, wxControl
) 
  49 // =========================================================================== 
  51 // =========================================================================== 
  53 // --------------------------------------------------------------------------- 
  55 // --------------------------------------------------------------------------- 
  57 // we may have either bitmap or icon: if a bitmap with mask is passed, we 
  58 // will transform it to an icon ourselves because otherwise the mask will 
  59 // be ignored by Windows 
  60 // note that this function will create a new object every time 
  61 // it is called even if the image needs no conversion 
  65 static wxGDIImage
* ConvertImage( const wxGDIImage
& bitmap 
) 
  67     bool isIcon 
= bitmap
.IsKindOf( CLASSINFO(wxIcon
) ); 
  71         wxASSERT_MSG( wxDynamicCast(&bitmap
, wxBitmap
), 
  72                       _T("not an icon and not a bitmap?") ); 
  74         const wxBitmap
& bmp 
= (const wxBitmap
&)bitmap
; 
  75         wxMask 
*mask 
= bmp
.GetMask(); 
  76         if ( mask 
&& mask
->GetMaskBitmap() ) 
  78             wxIcon
* icon 
= new wxIcon
; 
  79             icon
->CopyFromBitmap(bmp
); 
  84         return new wxBitmap( bmp 
); 
  87     // copying a bitmap is a cheap operation 
  88     return new wxIcon( (const wxIcon
&)bitmap 
); 
  93 bool wxStaticBitmap::Create(wxWindow 
*parent
, wxWindowID id
, 
  94                             const wxGDIImage
& bitmap
, 
 104         parent
->AddChild(this); 
 106     m_backgroundColour 
= parent
->GetBackgroundColour() ; 
 107     m_foregroundColour 
= parent
->GetForegroundColour() ; 
 110         m_windowId 
= (int)NewControlId(); 
 119     m_windowStyle 
= style
; 
 121     // we may have either bitmap or icon: if a bitmap with mask is passed, we 
 122     // will transform it to an icon ourselves because otherwise the mask will 
 123     // be ignored by Windows 
 124     wxGDIImage 
*image 
= (wxGDIImage 
*)NULL
; 
 125     m_isIcon 
= bitmap
.IsKindOf(CLASSINFO(wxIcon
)); 
 128     wxASSERT_MSG( !m_isIcon
, "Icons are not supported in wxStaticBitmap under WIN16." ); 
 133     image 
= ConvertImage( bitmap 
); 
 134     m_isIcon 
= image
->IsKindOf( CLASSINFO(wxIcon
) ); 
 138     // create a static control with either SS_BITMAP or SS_ICON style depending 
 139     // on what we have here 
 140     const wxChar 
*classname 
= wxT("STATIC"); 
 141     int winstyle 
= m_isIcon 
? SS_ICON 
: SS_BITMAP
; 
 143     const wxChar 
*classname 
= wxT("BUTTON"); 
 144     int winstyle 
= BS_OWNERDRAW
; 
 147     if ( m_windowStyle 
& wxCLIP_SIBLINGS 
) 
 148         winstyle 
|= WS_CLIPSIBLINGS
; 
 151     m_hWnd 
= (WXHWND
)::CreateWindow
 
 155                         // NOT DISABLED!!! We want to move it in Dialog Editor. 
 156                         winstyle 
| WS_CHILD 
| WS_VISIBLE 
/* | WS_CLIPSIBLINGS */ , // | WS_DISABLED, 
 158                         (HWND
)parent
->GetHWND(), 
 164     wxCHECK_MSG( m_hWnd
, FALSE
, wxT("Failed to create static bitmap") ); 
 166     // no need to delete the new image 
 167     SetImageNoCopy( image 
); 
 169     // Subclass again for purposes of dialog editing mode 
 172     SetFont(GetParent()->GetFont()); 
 174     SetSize(x
, y
, width
, height
); 
 179 bool wxStaticBitmap::ImageIsOk() const 
 181     return m_image 
&& m_image
->Ok(); 
 184 void wxStaticBitmap::Free() 
 191 wxSize 
wxStaticBitmap::DoGetBestSize() const 
 193     // reuse the current size (as wxWindow does) instead of using some 
 194     // arbitrary default size (as wxControl, our immediate base class, does) 
 195     return wxWindow::DoGetBestSize(); 
 198 void wxStaticBitmap::SetImage( const wxGDIImage
* image 
) 
 200     wxGDIImage
* convertedImage 
= ConvertImage( *image 
); 
 201     SetImageNoCopy( convertedImage 
); 
 204 void wxStaticBitmap::SetImageNoCopy( wxGDIImage
* image
) 
 208     m_isIcon 
= image
->IsKindOf( CLASSINFO(wxIcon
) ); 
 209     // the image has already been copied 
 218     HANDLE handle 
= (HANDLE
)m_image
->GetHandle(); 
 219     LONG style 
= ::GetWindowLong( (HWND
)GetHWND(), GWL_STYLE 
) ; 
 220     ::SetWindowLong( (HWND
)GetHWND(), GWL_STYLE
, ( style 
& ~( SS_BITMAP
|SS_ICON 
) ) | 
 221                      ( m_isIcon 
? SS_ICON 
: SS_BITMAP 
) ); 
 222     ::SendMessage(GetHwnd(), STM_SETIMAGE
, 
 223                   m_isIcon 
? IMAGE_ICON 
: IMAGE_BITMAP
, (LPARAM
)handle
); 
 228         int width 
= image
->GetWidth(), 
 229             height 
= image
->GetHeight(); 
 230         if ( width 
&& height 
) 
 235             ::MoveWindow(GetHwnd(), x
, y
, width
, height
, FALSE
); 
 244     InvalidateRect(GetHwndOf(GetParent()), &rect
, TRUE
); 
 247 // under Win32 we use the standard static control style for this 
 249 bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT 
*item
) 
 251     LPDRAWITEMSTRUCT lpDIS 
= (LPDRAWITEMSTRUCT
) item
; 
 253     wxCHECK_MSG( !m_isIcon
, FALSE
, _T("icons not supported in wxStaticBitmap") ); 
 255     wxBitmap
* bitmap 
= (wxBitmap 
*)m_image
; 
 259     HDC hDC 
= lpDIS
->hDC
; 
 260     HDC memDC 
= ::CreateCompatibleDC(hDC
); 
 262     HBITMAP old 
= (HBITMAP
) ::SelectObject(memDC
, (HBITMAP
) bitmap
->GetHBITMAP()); 
 267     int x 
= lpDIS
->rcItem
.left
; 
 268     int y 
= lpDIS
->rcItem
.top
; 
 269     int width 
= lpDIS
->rcItem
.right 
- x
; 
 270     int height 
= lpDIS
->rcItem
.bottom 
- y
; 
 272     // Centre the bitmap in the control area 
 273     int x1 
= (int) (x 
+ ((width 
- bitmap
->GetWidth()) / 2)); 
 274     int y1 
= (int) (y 
+ ((height 
- bitmap
->GetHeight()) / 2)); 
 276     ::BitBlt(hDC
, x1
, y1
, bitmap
->GetWidth(), bitmap
->GetHeight(), memDC
, 0, 0, SRCCOPY
); 
 278     ::SelectObject(memDC
, old
); 
 286 // We need this or the control can never be moved e.g. in Dialog Editor. 
 287 long wxStaticBitmap::MSWWindowProc(WXUINT nMsg
, 
 291     // Ensure that static items get messages. Some controls don't like this 
 292     // message to be intercepted (e.g. RichEdit), hence the tests. 
 293     if ( nMsg 
== WM_NCHITTEST 
) 
 294         return (long)HTCLIENT
; 
 296     return wxWindow::MSWWindowProc(nMsg
, wParam
, lParam
); 
 299 #endif // wxUSE_STATBMP