hack to allow icons in wxStaticBitmap as well as bitmaps
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1971 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
// Created: 01/02/97
|
// Created: 01/02/97
|
||||||
// RCS-ID: $Id$
|
// RCS-ID: $Id$
|
||||||
// Copyright: (c) Julian Smart
|
// Copyright: (c) Julian Smart
|
||||||
// Licence: wxWindows licence
|
// Licence: wxWindows licence
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifndef _WX_STATBMP_H_
|
#ifndef _WX_STATBMP_H_
|
||||||
@@ -20,47 +20,72 @@
|
|||||||
|
|
||||||
WXDLLEXPORT_DATA(extern const char*) wxStaticBitmapNameStr;
|
WXDLLEXPORT_DATA(extern const char*) wxStaticBitmapNameStr;
|
||||||
|
|
||||||
|
// a control showing an icon or a bitmap
|
||||||
class WXDLLEXPORT wxStaticBitmap : public wxControl
|
class WXDLLEXPORT wxStaticBitmap : public wxControl
|
||||||
{
|
{
|
||||||
DECLARE_DYNAMIC_CLASS(wxStaticBitmap)
|
DECLARE_DYNAMIC_CLASS(wxStaticBitmap)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
wxStaticBitmap() { }
|
wxStaticBitmap() { Init(); }
|
||||||
|
|
||||||
wxStaticBitmap(wxWindow *parent, wxWindowID id,
|
wxStaticBitmap(wxWindow *parent,
|
||||||
const wxBitmap& label,
|
wxWindowID id,
|
||||||
const wxPoint& pos = wxDefaultPosition,
|
const wxBitmap& label,
|
||||||
const wxSize& size = wxDefaultSize,
|
const wxPoint& pos = wxDefaultPosition,
|
||||||
long style = 0,
|
const wxSize& size = wxDefaultSize,
|
||||||
const wxString& name = wxStaticBitmapNameStr)
|
long style = 0,
|
||||||
|
const wxString& name = wxStaticBitmapNameStr)
|
||||||
{
|
{
|
||||||
Create(parent, id, label, pos, size, style, name);
|
Create(parent, id, label, pos, size, style, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Create(wxWindow *parent, wxWindowID id,
|
bool Create(wxWindow *parent,
|
||||||
const wxBitmap& label,
|
wxWindowID id,
|
||||||
const wxPoint& pos = wxDefaultPosition,
|
const wxBitmap& label,
|
||||||
const wxSize& size = wxDefaultSize,
|
const wxPoint& pos = wxDefaultPosition,
|
||||||
long style = 0,
|
const wxSize& size = wxDefaultSize,
|
||||||
const wxString& name = wxStaticBitmapNameStr);
|
long style = 0,
|
||||||
|
const wxString& name = wxStaticBitmapNameStr);
|
||||||
|
|
||||||
|
virtual ~wxStaticBitmap() { Free(); }
|
||||||
|
|
||||||
|
virtual void SetIcon(const wxIcon& icon) { SetBitmap(icon); }
|
||||||
virtual void SetBitmap(const wxBitmap& bitmap);
|
virtual void SetBitmap(const wxBitmap& bitmap);
|
||||||
|
|
||||||
virtual void Command(wxCommandEvent& WXUNUSED(event)) { }
|
// assert failure is provoked by an attempt to get an icon from bitmap or
|
||||||
virtual void ProcessCommand(wxCommandEvent& WXUNUSED(event)) { }
|
// vice versa
|
||||||
|
const wxIcon& GetIcon() const
|
||||||
wxBitmap& GetBitmap(void) const { return (wxBitmap&) m_messageBitmap; }
|
{ wxASSERT( m_isIcon ); return *m_image.icon; }
|
||||||
|
const wxBitmap& GetBitmap() const
|
||||||
|
{ wxASSERT( !m_isIcon ); return *m_image.bitmap; }
|
||||||
|
|
||||||
// overriden base class virtuals
|
// overriden base class virtuals
|
||||||
virtual bool AcceptsFocus() const { return FALSE; }
|
virtual bool AcceptsFocus() const { return FALSE; }
|
||||||
|
|
||||||
// Implementation
|
// IMPLEMENTATION
|
||||||
|
virtual void Command(wxCommandEvent& WXUNUSED(event)) { }
|
||||||
|
virtual void ProcessCommand(wxCommandEvent& WXUNUSED(event)) { }
|
||||||
|
|
||||||
|
#ifdef __WIN16__
|
||||||
virtual bool MSWOnDraw(WXDRAWITEMSTRUCT *item);
|
virtual bool MSWOnDraw(WXDRAWITEMSTRUCT *item);
|
||||||
virtual long MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
|
virtual long MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
|
||||||
|
#endif // __WIN16__
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
wxBitmap m_messageBitmap;
|
void Init() { m_isIcon = TRUE; m_image.icon = NULL; }
|
||||||
|
void Free();
|
||||||
|
|
||||||
|
// TRUE if icon/bitmap is valid
|
||||||
|
bool ImageIsOk() const;
|
||||||
|
|
||||||
|
// we can have either an icon or a bitmap
|
||||||
|
bool m_isIcon;
|
||||||
|
union
|
||||||
|
{
|
||||||
|
wxIcon *icon;
|
||||||
|
wxBitmap *bitmap;
|
||||||
|
} m_image;
|
||||||
|
|
||||||
virtual void DoSetSize(int x, int y,
|
virtual void DoSetSize(int x, int y,
|
||||||
int width, int height,
|
int width, int height,
|
||||||
int sizeFlags = wxSIZE_AUTO);
|
int sizeFlags = wxSIZE_AUTO);
|
||||||
|
@@ -9,156 +9,212 @@
|
|||||||
// Licence: wxWindows license
|
// Licence: wxWindows license
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// ===========================================================================
|
||||||
|
// declarations
|
||||||
|
// ===========================================================================
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// headers
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifdef __GNUG__
|
#ifdef __GNUG__
|
||||||
#pragma implementation "statbmp.h"
|
#pragma implementation "statbmp.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// For compilers that support precompilation, includes "wx.h".
|
// For compilers that support precompilation, includes "wx.h".
|
||||||
#include "wx/wxprec.h"
|
#include "wx/wxprec.h"
|
||||||
|
|
||||||
#ifdef __BORLANDC__
|
#ifdef __BORLANDC__
|
||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef WX_PRECOMP
|
#ifndef WX_PRECOMP
|
||||||
#include "wx/statbmp.h"
|
#include "wx/statbmp.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "wx/msw/private.h"
|
#include "wx/msw/private.h"
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// macors
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
#if !USE_SHARED_LIBRARY
|
#if !USE_SHARED_LIBRARY
|
||||||
IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
|
IMPLEMENT_DYNAMIC_CLASS(wxStaticBitmap, wxControl)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
// ===========================================================================
|
||||||
* wxStaticBitmap
|
// implementation
|
||||||
*/
|
// ===========================================================================
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// wxStaticBitmap
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id,
|
bool wxStaticBitmap::Create(wxWindow *parent, wxWindowID id,
|
||||||
const wxBitmap& bitmap,
|
const wxBitmap& bitmap,
|
||||||
const wxPoint& pos,
|
const wxPoint& pos,
|
||||||
const wxSize& size,
|
const wxSize& size,
|
||||||
long style,
|
long style,
|
||||||
const wxString& name)
|
const wxString& name)
|
||||||
{
|
{
|
||||||
m_messageBitmap = bitmap;
|
Init();
|
||||||
SetName(name);
|
|
||||||
if (parent) parent->AddChild(this);
|
|
||||||
|
|
||||||
m_backgroundColour = parent->GetBackgroundColour() ;
|
SetName(name);
|
||||||
m_foregroundColour = parent->GetForegroundColour() ;
|
if (parent)
|
||||||
|
parent->AddChild(this);
|
||||||
|
|
||||||
if ( id == -1 )
|
m_backgroundColour = parent->GetBackgroundColour() ;
|
||||||
m_windowId = (int)NewControlId();
|
m_foregroundColour = parent->GetForegroundColour() ;
|
||||||
else
|
|
||||||
m_windowId = id;
|
|
||||||
|
|
||||||
int x = pos.x;
|
if ( id == -1 )
|
||||||
int y = pos.y;
|
m_windowId = (int)NewControlId();
|
||||||
int width = size.x;
|
else
|
||||||
int height = size.y;
|
m_windowId = id;
|
||||||
|
|
||||||
if ( width < 0 && bitmap.Ok() )
|
int x = pos.x;
|
||||||
width = bitmap.GetWidth();
|
int y = pos.y;
|
||||||
if ( height < 0 && bitmap.Ok() )
|
int width = size.x;
|
||||||
height = bitmap.GetHeight();
|
int height = size.y;
|
||||||
|
|
||||||
m_windowStyle = style;
|
m_windowStyle = style;
|
||||||
|
|
||||||
// Use an ownerdraw button to produce a static bitmap, since there's
|
m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
|
||||||
// no ownerdraw static.
|
|
||||||
// TODO: perhaps this should be a static item, with style SS_BITMAP.
|
|
||||||
m_hWnd = (WXHWND)CreateWindow
|
|
||||||
(
|
|
||||||
"BUTTON",
|
|
||||||
"",
|
|
||||||
BS_OWNERDRAW | WS_TABSTOP | WS_CHILD | WS_VISIBLE,
|
|
||||||
0, 0, 0, 0,
|
|
||||||
(HWND)parent->GetHWND(),
|
|
||||||
(HMENU)m_windowId,
|
|
||||||
wxGetInstance(),
|
|
||||||
NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
// Subclass again for purposes of dialog editing mode
|
#ifdef __WIN32__
|
||||||
SubclassWin(m_hWnd);
|
// create a static control with either SS_BITMAP or SS_ICON style depending
|
||||||
|
// on what we have here
|
||||||
|
const char *classname = "STATIC";
|
||||||
|
int winstyle = m_isIcon ? SS_ICON : SS_BITMAP;
|
||||||
|
#else // Win16
|
||||||
|
const char *classname = "BUTTON";
|
||||||
|
int winstyle = BS_OWNERDRAWN;
|
||||||
|
#endif // Win32
|
||||||
|
|
||||||
SetFont(GetParent()->GetFont());
|
m_hWnd = (WXHWND)::CreateWindow
|
||||||
|
(
|
||||||
|
classname,
|
||||||
|
"",
|
||||||
|
winstyle | WS_CHILD | WS_VISIBLE,
|
||||||
|
0, 0, 0, 0,
|
||||||
|
(HWND)parent->GetHWND(),
|
||||||
|
(HMENU)m_windowId,
|
||||||
|
wxGetInstance(),
|
||||||
|
NULL
|
||||||
|
);
|
||||||
|
|
||||||
SetSize(x, y, width, height);
|
wxCHECK_MSG( m_hWnd, FALSE, "Failed to create static bitmap" );
|
||||||
return TRUE;
|
|
||||||
|
SetBitmap(bitmap);
|
||||||
|
|
||||||
|
// Subclass again for purposes of dialog editing mode
|
||||||
|
SubclassWin(m_hWnd);
|
||||||
|
|
||||||
|
SetFont(GetParent()->GetFont());
|
||||||
|
|
||||||
|
SetSize(x, y, width, height);
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool wxStaticBitmap::ImageIsOk() const
|
||||||
|
{
|
||||||
|
if ( m_isIcon && m_image.icon )
|
||||||
|
return m_image.icon->Ok();
|
||||||
|
else if ( m_image.bitmap )
|
||||||
|
return m_image.bitmap->Ok();
|
||||||
|
else
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxStaticBitmap::Free()
|
||||||
|
{
|
||||||
|
if ( m_isIcon )
|
||||||
|
delete m_image.icon;
|
||||||
|
else
|
||||||
|
delete m_image.bitmap;
|
||||||
|
|
||||||
|
m_image.icon = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxStaticBitmap::DoSetSize(int x, int y, int width, int height, int sizeFlags)
|
void wxStaticBitmap::DoSetSize(int x, int y, int width, int height, int sizeFlags)
|
||||||
{
|
{
|
||||||
int currentX, currentY;
|
int currentX, currentY;
|
||||||
GetPosition(¤tX, ¤tY);
|
GetPosition(¤tX, ¤tY);
|
||||||
int x1 = x;
|
int x1 = x;
|
||||||
int y1 = y;
|
int y1 = y;
|
||||||
|
|
||||||
if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
|
if (x == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
|
||||||
x1 = currentX;
|
x1 = currentX;
|
||||||
if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
|
if (y == -1 || (sizeFlags & wxSIZE_ALLOW_MINUS_ONE))
|
||||||
y1 = currentY;
|
y1 = currentY;
|
||||||
|
|
||||||
AdjustForParentClientOrigin(x1, y1, sizeFlags);
|
AdjustForParentClientOrigin(x1, y1, sizeFlags);
|
||||||
|
|
||||||
int actualWidth = width;
|
int actualWidth = width;
|
||||||
int actualHeight = height;
|
int actualHeight = height;
|
||||||
|
|
||||||
int ww, hh;
|
int ww, hh;
|
||||||
GetSize(&ww, &hh);
|
GetSize(&ww, &hh);
|
||||||
|
|
||||||
// If we're prepared to use the existing width, then...
|
// If we're prepared to use the existing width, then...
|
||||||
if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH))
|
if (width == -1 && ((sizeFlags & wxSIZE_AUTO_WIDTH) != wxSIZE_AUTO_WIDTH))
|
||||||
actualWidth = ww;
|
actualWidth = ww;
|
||||||
else actualWidth = width;
|
else
|
||||||
|
actualWidth = width;
|
||||||
|
|
||||||
// If we're prepared to use the existing height, then...
|
// If we're prepared to use the existing height, then...
|
||||||
if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT))
|
if (height == -1 && ((sizeFlags & wxSIZE_AUTO_HEIGHT) != wxSIZE_AUTO_HEIGHT))
|
||||||
actualHeight = hh;
|
actualHeight = hh;
|
||||||
else actualHeight = height;
|
else
|
||||||
|
actualHeight = height;
|
||||||
|
|
||||||
MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE);
|
MoveWindow((HWND) GetHWND(), x1, y1, actualWidth, actualHeight, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxStaticBitmap::SetBitmap(const wxBitmap& bitmap)
|
void wxStaticBitmap::SetBitmap(const wxBitmap& bitmap)
|
||||||
{
|
{
|
||||||
m_messageBitmap = bitmap;
|
Free();
|
||||||
|
|
||||||
int x, y;
|
m_isIcon = bitmap.IsKindOf(CLASSINFO(wxIcon));
|
||||||
int w, h;
|
if ( m_isIcon )
|
||||||
GetPosition(&x, &y);
|
m_image.icon = new wxIcon((const wxIcon&)bitmap);
|
||||||
GetSize(&w, &h);
|
else
|
||||||
RECT rect;
|
m_image.bitmap = new wxBitmap(bitmap);
|
||||||
rect.left = x; rect.top = y; rect.right = x + w; rect.bottom = y + h;
|
|
||||||
|
|
||||||
if ( bitmap.Ok() )
|
int x, y;
|
||||||
MoveWindow((HWND) GetHWND(), x, y, bitmap.GetWidth(), bitmap.GetHeight(),
|
int w, h;
|
||||||
FALSE);
|
GetPosition(&x, &y);
|
||||||
|
GetSize(&w, &h);
|
||||||
InvalidateRect((HWND) GetParent()->GetHWND(), &rect, TRUE);
|
RECT rect = { x, y, x + w, y + h };
|
||||||
|
|
||||||
|
#ifdef __WIN32__
|
||||||
|
HANDLE handle = m_isIcon ? (HANDLE)m_image.icon->GetHICON()
|
||||||
|
: (HANDLE)m_image.bitmap->GetHBITMAP();
|
||||||
|
::SendMessage((HWND)m_hWnd, STM_SETIMAGE,
|
||||||
|
m_isIcon ? IMAGE_ICON : IMAGE_BITMAP, (LPARAM)handle);
|
||||||
|
#endif // Win32
|
||||||
|
|
||||||
|
if ( ImageIsOk() )
|
||||||
|
{
|
||||||
|
int width = bitmap.GetWidth(),
|
||||||
|
height = bitmap.GetHeight();
|
||||||
|
if ( width && height )
|
||||||
|
{
|
||||||
|
::MoveWindow((HWND)GetHWND(), x, y, width, height, FALSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
InvalidateRect((HWND)GetParent()->GetHWND(), &rect, TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// under Win32 we use the standard static control style for this
|
||||||
|
#ifdef __WIN16__
|
||||||
bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
|
bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
|
||||||
{
|
{
|
||||||
long style = GetWindowLong((HWND) GetHWND(), GWL_STYLE);
|
|
||||||
#if defined(__WIN32__) && defined(SS_BITMAP)
|
|
||||||
if ((style & 0xFF) == SS_BITMAP)
|
|
||||||
{
|
|
||||||
// Should we call Default() here?
|
|
||||||
// Default();
|
|
||||||
|
|
||||||
// Let default procedure draw the bitmap, which is defined
|
|
||||||
// in the Windows resource.
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
|
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT) item;
|
||||||
|
|
||||||
wxBitmap* bitmap = &m_messageBitmap;
|
wxBitmap* bitmap = m_image.bitmap;
|
||||||
if ( !bitmap->Ok() )
|
if ( !bitmap->Ok() )
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
@@ -170,10 +226,10 @@ bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
|
|||||||
if (!old)
|
if (!old)
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
int x = lpDIS->rcItem.left;
|
int x = lpDIS->rcItem.left;
|
||||||
int y = lpDIS->rcItem.top;
|
int y = lpDIS->rcItem.top;
|
||||||
int width = lpDIS->rcItem.right - x;
|
int width = lpDIS->rcItem.right - x;
|
||||||
int height = lpDIS->rcItem.bottom - y;
|
int height = lpDIS->rcItem.bottom - y;
|
||||||
|
|
||||||
// Centre the bitmap in the control area
|
// Centre the bitmap in the control area
|
||||||
int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
|
int x1 = (int) (x + ((width - bitmap->GetWidth()) / 2));
|
||||||
@@ -188,13 +244,15 @@ bool wxStaticBitmap::MSWOnDraw(WXDRAWITEMSTRUCT *item)
|
|||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
long wxStaticBitmap::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
|
long wxStaticBitmap::MSWWindowProc(WXUINT nMsg,
|
||||||
|
WXWPARAM wParam,
|
||||||
|
WXLPARAM lParam)
|
||||||
{
|
{
|
||||||
// Ensure that static items get messages. Some controls don't like this
|
// Ensure that static items get messages. Some controls don't like this
|
||||||
// message to be intercepted (e.g. RichEdit), hence the tests.
|
// message to be intercepted (e.g. RichEdit), hence the tests.
|
||||||
if (nMsg == WM_NCHITTEST)
|
if ( nMsg == WM_NCHITTEST )
|
||||||
return (long)HTCLIENT;
|
return (long)HTCLIENT;
|
||||||
|
|
||||||
return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
|
return wxWindow::MSWWindowProc(nMsg, wParam, lParam);
|
||||||
}
|
}
|
||||||
|
#endif // Win16
|
||||||
|
Reference in New Issue
Block a user