Refactor owner-drawing code.

Only keep common code in the base class and extract all menu/listbox-specific
stuff into derived classes.

This makes the code cleaner and more maintainable but introduces some problems
in wxCheckListBox appearance which will be fixed by the next patch.

Closes #10635.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63220 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-01-23 13:21:12 +00:00
parent 937d5b6075
commit 98fbab9e7b
26 changed files with 1638 additions and 1394 deletions

View File

@@ -152,9 +152,6 @@ protected:
bool m_updateHorizontalExtent;
virtual void OnInternalIdle();
// free memory (common part of Clear() and dtor)
void Free();
unsigned int m_noItems;
#if wxUSE_OWNER_DRAWN

View File

@@ -17,7 +17,8 @@
// ----------------------------------------------------------------------------
#if wxUSE_OWNER_DRAWN
#include "wx/ownerdrw.h" // base class
#include "wx/ownerdrw.h"
#include "wx/bitmap.h"
#endif
// ----------------------------------------------------------------------------
@@ -41,7 +42,6 @@ public:
// override base class virtuals
virtual void SetItemLabel(const wxString& strName);
virtual void SetCheckable(bool checkable);
virtual void Enable(bool bDoEnable = true);
virtual void Check(bool bDoCheck = true);
@@ -76,6 +76,48 @@ public:
);
#endif
#if wxUSE_OWNER_DRAWN
void SetBitmaps(const wxBitmap& bmpChecked,
const wxBitmap& bmpUnchecked = wxNullBitmap)
{
m_bmpChecked = bmpChecked;
m_bmpUnchecked = bmpUnchecked;
SetOwnerDrawn(true);
}
void SetBitmap(const wxBitmap& bmp, bool bChecked = true)
{
if ( bChecked )
m_bmpChecked = bmp;
else
m_bmpUnchecked = bmp;
SetOwnerDrawn(true);
}
void SetDisabledBitmap(const wxBitmap& bmpDisabled)
{
m_bmpDisabled = bmpDisabled;
SetOwnerDrawn(true);
}
const wxBitmap& GetBitmap(bool bChecked = true) const
{ return (bChecked ? m_bmpChecked : m_bmpUnchecked); }
const wxBitmap& GetDisabledBitmap() const
{ return m_bmpDisabled; }
// override wxOwnerDrawn base class virtuals
virtual wxString GetName() const;
virtual bool OnMeasureItem(size_t *pwidth, size_t *pheight);
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
protected:
virtual void GetFontToUse(wxFont& font) const;
#endif // wxUSE_OWNER_DRAWN
private:
// common part of all ctors
void Init();
@@ -93,6 +135,18 @@ private:
// does this item start a radio group?
bool m_isRadioGroupStart;
#if wxUSE_OWNER_DRAWN
// item bitmaps
wxBitmap m_bmpChecked, // bitmap to put near the item
m_bmpUnchecked, // (checked is used also for 'uncheckable' items)
m_bmpDisabled;
// static variables for cache some system settings
static wxFont ms_systemMenuFont;
static size_t ms_systemMenuHeight;
static bool ms_alwaysShowCues;
#endif // wxUSE_OWNER_DRAWN
DECLARE_DYNAMIC_CLASS_NO_COPY(wxMenuItem)
};

29
include/wx/msw/ownerdrw.h Normal file
View File

@@ -0,0 +1,29 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/msw/ownerdrw.h
// Purpose: wxOwnerDrawn class
// Author: Marcin Malich
// Modified by:
// Created: 2009-09-22
// RCS-ID: $Id$
// Copyright: (c) 2009 Marcin Malich <me@malcom.pl>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_OWNERDRW_H_
#define _WX_OWNERDRW_H_
#if wxUSE_OWNER_DRAWN
class WXDLLIMPEXP_CORE wxOwnerDrawn : public wxOwnerDrawnBase
{
public:
wxOwnerDrawn() {}
virtual ~wxOwnerDrawn() {}
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc,
wxODAction act, wxODStatus stat);
};
#endif // wxUSE_OWNER_DRAWN
#endif // _WX_OWNERDRW_H_

View File

@@ -23,7 +23,8 @@
// headers - only because ownerdrw.h is not always included and I don't want
// to write #ifdef's everywhere...
#if wxUSE_OWNER_DRAWN
#include "wx/ownerdrw.h"
#include "wx/ownerdrw.h"
#include "wx/bitmap.h"
#endif
// ----------------------------------------------------------------------------
@@ -66,7 +67,6 @@ public:
// Override base class virtuals
//
virtual void SetItemLabel(const wxString& rStrName);
virtual void SetCheckable(bool bCheckable);
virtual void Enable(bool bDoEnable = true);
virtual void Check(bool bDoCheck = true);
@@ -97,6 +97,48 @@ public:
//
MENUITEM m_vMenuData;
#if wxUSE_OWNER_DRAWN
void SetBitmaps(const wxBitmap& bmpChecked,
const wxBitmap& bmpUnchecked = wxNullBitmap)
{
m_bmpChecked = bmpChecked;
m_bmpUnchecked = bmpUnchecked;
SetOwnerDrawn(true);
}
void SetBitmap(const wxBitmap& bmp, bool bChecked = true)
{
if ( bChecked )
m_bmpChecked = bmp;
else
m_bmpUnchecked = bmp;
SetOwnerDrawn(true);
}
void SetDisabledBitmap(const wxBitmap& bmpDisabled)
{
m_bmpDisabled = bmpDisabled;
SetOwnerDrawn(true);
}
const wxBitmap& GetBitmap(bool bChecked = true) const
{ return (bChecked ? m_bmpChecked : m_bmpUnchecked); }
const wxBitmap& GetDisabledBitmap() const
{ return m_bmpDisabled; }
// override wxOwnerDrawn base class virtuals
virtual wxString GetName() const;
virtual bool OnMeasureItem(size_t *pwidth, size_t *pheight);
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
protected:
virtual void GetFontToUse(wxFont& font) const;
#endif // wxUSE_OWNER_DRAWN
private:
void Init();
@@ -117,6 +159,13 @@ private:
//
bool m_bIsRadioGroupStart;
#if wxUSE_OWNER_DRAWN
// item bitmaps
wxBitmap m_bmpChecked, // bitmap to put near the item
m_bmpUnchecked, // (checked is used also for 'uncheckable' items)
m_bmpDisabled;
#endif // wxUSE_OWNER_DRAWN
DECLARE_DYNAMIC_CLASS(wxMenuItem)
}; // end of CLASS wxMenuItem

29
include/wx/os2/ownerdrw.h Normal file
View File

@@ -0,0 +1,29 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/os2/ownerdrw.h
// Purpose: wxOwnerDrawn class
// Author: Marcin Malich
// Modified by:
// Created: 2009-09-22
// RCS-ID: $Id$
// Copyright: (c) 2009 Marcin Malich <me@malcom.pl>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_OWNERDRW_H_
#define _WX_OWNERDRW_H_
#if wxUSE_OWNER_DRAWN
class WXDLLIMPEXP_CORE wxOwnerDrawn : public wxOwnerDrawnBase
{
public:
wxOwnerDrawn() {}
virtual ~wxOwnerDrawn() {}
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc,
wxODAction act, wxODStatus stat);
};
#endif // wxUSE_OWNER_DRAWN
#endif // _WX_OWNERDRW_H_

View File

@@ -2,23 +2,22 @@
// Name: ownerdrw.h
// Purpose: interface for owner-drawn GUI elements
// Author: Vadim Zeitlin
// Modified by:
// Modified by: Marcin Malich
// Created: 11.11.97
// RCS-ID: $Id$
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _OWNERDRW_H
#define _OWNERDRW_H
#ifndef _WX_OWNERDRW_H_BASE
#define _WX_OWNERDRW_H_BASE
#include "wx/defs.h"
#if wxUSE_OWNER_DRAWN
#include "wx/bitmap.h"
#include "wx/colour.h"
#include "wx/font.h"
#include "wx/colour.h"
// ----------------------------------------------------------------------------
// wxOwnerDrawn - a mix-in base class, derive from it to implement owner-drawn
@@ -29,150 +28,116 @@
// element or one unchangeable bitmap otherwise.
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxOwnerDrawn
class WXDLLIMPEXP_CORE wxOwnerDrawnBase
{
public:
// ctor & dtor
wxOwnerDrawn(const wxString& str = wxEmptyString,
bool bCheckable = false,
bool bMenuItem = false); // FIXME kludge for colors
virtual ~wxOwnerDrawn();
wxOwnerDrawnBase()
{
m_ownerDrawn = false;
m_margin = ms_defaultMargin;
}
// fix appearance
void SetFont(const wxFont& font)
{ m_font = font; m_bOwnerDrawn = true; }
virtual ~wxOwnerDrawnBase() {}
wxFont& GetFont() const { return (wxFont &)m_font; }
void SetFont(const wxFont& font)
{ m_font = font; m_ownerDrawn = true; }
void SetTextColour(const wxColour& colText)
{ m_colText = colText; m_bOwnerDrawn = true; }
wxFont& GetFont() const
{ return (wxFont&) m_font; }
wxColour& GetTextColour() const { return (wxColour&) m_colText; }
void SetBackgroundColour(const wxColour& colBack)
{ m_colBack = colBack; m_bOwnerDrawn = true; }
void SetTextColour(const wxColour& colText)
{ m_colText = colText; m_ownerDrawn = true; }
wxColour& GetBackgroundColour() const
{ return (wxColour&) m_colBack ; }
wxColour& GetTextColour() const
{ return (wxColour&) m_colText; }
void SetBitmaps(const wxBitmap& bmpChecked,
const wxBitmap& bmpUnchecked = wxNullBitmap)
{ m_bmpChecked = bmpChecked;
m_bmpUnchecked = bmpUnchecked;
m_bOwnerDrawn = true; }
void SetBackgroundColour(const wxColour& colBack)
{ m_colBack = colBack; m_ownerDrawn = true; }
void SetBitmap(const wxBitmap& bmp, bool bChecked = true)
{
if ( bChecked )
m_bmpChecked = bmp;
else
m_bmpUnchecked = bmp;
m_bOwnerDrawn = true;
}
wxColour& GetBackgroundColour() const
{ return (wxColour&) m_colBack ; }
void SetDisabledBitmap( const wxBitmap& bmpDisabled )
{ m_bmpDisabled = bmpDisabled;
m_bOwnerDrawn = true; }
const wxBitmap& GetBitmap(bool bChecked = true) const
{ return (bChecked ? m_bmpChecked : m_bmpUnchecked); }
void SetMarginWidth(int width)
{ m_margin = width; }
const wxBitmap& GetDisabledBitmap() const
{ return m_bmpDisabled; }
int GetMarginWidth() const
{ return m_margin; }
// the height of the menu checkmark (or bitmap) is determined by the font
// for the current item, but the width should be always the same (for the
// items to be aligned), so by default it's taken to be the same as for
// the last item (and default width for the first one).
//
// NB: default is too small for bitmaps, but ok for checkmarks.
void SetMarginWidth(int nWidth)
{
ms_nLastMarginWidth = m_nMarginWidth = (size_t) nWidth;
if ( ((size_t) nWidth) != ms_nDefaultMarginWidth )
m_bOwnerDrawn = true;
}
static int GetDefaultMarginWidth()
{ return ms_defaultMargin; }
int GetMarginWidth() const { return (int) m_nMarginWidth; }
static int GetDefaultMarginWidth() { return (int) ms_nDefaultMarginWidth; }
// accessors
void SetName(const wxString& strName) { m_strName = strName; }
const wxString& GetName() const { return m_strName; }
void SetCheckable(bool checkable) { m_bCheckable = checkable; }
bool IsCheckable() const { return m_bCheckable; }
// get item name (with mnemonics if exist)
virtual wxString GetName() const = 0;
// this is for menu items only: accel string is drawn right aligned after the
// menu item if not empty
void SetAccelString(const wxString& strAccel) { m_strAccel = strAccel; }
// this function might seem strange, but if it returns false it means that
// no non-standard attribute are set, so there is no need for this control
// to be owner-drawn. Moreover, you can force owner-drawn to false if you
// want to change, say, the color for the item but only if it is owner-drawn
// (see wxMenuItem::wxMenuItem for example)
bool IsOwnerDrawn() const { return m_bOwnerDrawn; }
bool IsOwnerDrawn() const
{ return m_ownerDrawn; }
// switch on/off owner-drawing the item
void SetOwnerDrawn(bool ownerDrawn = true) { m_bOwnerDrawn = ownerDrawn; }
void ResetOwnerDrawn() { m_bOwnerDrawn = false; }
// switch on/off owner-drawing the item
void SetOwnerDrawn(bool ownerDrawn = true)
{ m_ownerDrawn = ownerDrawn; }
public:
// constants used in OnDrawItem
// (they have the same values as corresponding Win32 constants)
enum wxODAction
{
wxODDrawAll = 0x0001, // redraw entire control
wxODSelectChanged = 0x0002, // selection changed (see Status.Select)
wxODFocusChanged = 0x0004 // keyboard focus changed (see Status.Focus)
};
enum wxODStatus
{
wxODSelected = 0x0001, // control is currently selected
wxODGrayed = 0x0002, // item is to be grayed
wxODDisabled = 0x0004, // item is to be drawn as disabled
wxODChecked = 0x0008, // item is to be checked
wxODHasFocus = 0x0010, // item has the keyboard focus
wxODDefault = 0x0020, // item is the default item
wxODHidePrefix= 0x0100 // hide keyboard cues (w2k and xp only)
};
// constants used in OnDrawItem
// (they have the same values as corresponding Win32 constants)
enum wxODAction
{
wxODDrawAll = 0x0001, // redraw entire control
wxODSelectChanged = 0x0002, // selection changed (see Status.Select)
wxODFocusChanged = 0x0004 // keyboard focus changed (see Status.Focus)
};
// virtual functions to implement drawing (return true if processed)
virtual bool OnMeasureItem(size_t *pwidth, size_t *pheight);
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
enum wxODStatus
{
wxODSelected = 0x0001, // control is currently selected
wxODGrayed = 0x0002, // item is to be grayed
wxODDisabled = 0x0004, // item is to be drawn as disabled
wxODChecked = 0x0008, // item is to be checked
wxODHasFocus = 0x0010, // item has the keyboard focus
wxODDefault = 0x0020, // item is the default item
wxODHidePrefix= 0x0100 // hide keyboard cues (w2k and xp only)
};
// virtual functions to implement drawing (return true if processed)
virtual bool OnMeasureItem(size_t *width, size_t *height);
virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat) = 0;
protected:
// return true if this is a menu item
bool IsMenuItem() const;
// get the font to use, whether m_font is set or not
wxFont GetFontToUse() const;
wxString m_strName, // label for a manu item
m_strAccel; // the accel string ("Ctrl-F17") if any
// get the font and colour to use, whether it is set or not
virtual void GetFontToUse(wxFont& font) const;
virtual void GetColourToUse(wxODStatus stat, wxColour& colText, wxColour& colBack) const;
private:
static size_t ms_nDefaultMarginWidth; // menu check mark width
static size_t ms_nLastMarginWidth; // handy for aligning all items
bool m_ownerDrawn; // true if something is non standard
bool m_bCheckable, // used only for menu or check listbox items
m_bOwnerDrawn, // true if something is non standard
m_isMenuItem; // true if this is a menu item
wxFont m_font; // font to use for drawing
wxColour m_colText, // color ----"---"---"----
m_colBack; // background color
wxFont m_font; // font to use for drawing
wxColour m_colText, // color ----"---"---"----
m_colBack; // background color
wxBitmap m_bmpChecked, // bitmap to put near the item
m_bmpUnchecked, // (checked is used also for 'uncheckable' items)
m_bmpDisabled;
int m_margin; // space occupied by bitmap to the left of the item
size_t m_nHeight, // font height
m_nMarginWidth; // space occupied by bitmap to the left of the item
static int ms_defaultMargin;
};
// ----------------------------------------------------------------------------
// include the platform-specific class declaration
// ----------------------------------------------------------------------------
#if defined(__WXMSW__)
#include "wx/msw/ownerdrw.h"
#elif defined(__WXPM__)
#include "wx/os2/ownerdrw.h"
#endif
#endif // wxUSE_OWNER_DRAWN
#endif
// _OWNERDRW_H
#endif // _WX_OWNERDRW_H_BASE