1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        ownerdrw.h
3 // Purpose:     interface for owner-drawn GUI elements
4 // Author:      Vadim Zeitlin
5 // Modified by:
6 // Created:     11.11.97
7 // RCS-ID:      $Id: ownerdrw.h 62511 2009-10-30 14:11:03Z JMS $
8 // Copyright:   (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence:     wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef   _OWNERDRW_H
13 #define   _OWNERDRW_H
14 
15 #include "wx/defs.h"
16 
17 #if wxUSE_OWNER_DRAWN
18 
19 #include "wx/bitmap.h"
20 #include "wx/colour.h"
21 #include "wx/font.h"
22 
23 // ----------------------------------------------------------------------------
24 // wxOwnerDrawn - a mix-in base class, derive from it to implement owner-drawn
25 //                behaviour
26 //
27 // wxOwnerDrawn supports drawing of an item with non standard font, color and
28 // also supports 3 bitmaps: either a checked/unchecked bitmap for a checkable
29 // element or one unchangeable bitmap otherwise.
30 // ----------------------------------------------------------------------------
31 
32 class WXDLLEXPORT wxOwnerDrawn
33 {
34 public:
35   // ctor & dtor
36   wxOwnerDrawn(const wxString& str = wxEmptyString,
37                bool bCheckable = false,
38                bool bMenuItem = false); // FIXME kludge for colors
39   virtual ~wxOwnerDrawn();
40 
41   // fix appearance
SetFont(const wxFont & font)42   void SetFont(const wxFont& font)
43       { m_font = font; m_bOwnerDrawn = true; }
44 
GetFont()45   wxFont& GetFont() const { return (wxFont &)m_font; }
46 
SetTextColour(const wxColour & colText)47   void SetTextColour(const wxColour& colText)
48       { m_colText = colText; m_bOwnerDrawn = true; }
49 
GetTextColour()50   wxColour& GetTextColour() const { return (wxColour&) m_colText; }
51 
SetBackgroundColour(const wxColour & colBack)52   void SetBackgroundColour(const wxColour& colBack)
53       { m_colBack = colBack; m_bOwnerDrawn = true; }
54 
GetBackgroundColour()55   wxColour& GetBackgroundColour() const
56       { return (wxColour&) m_colBack ; }
57 
58   void SetBitmaps(const wxBitmap& bmpChecked,
59                   const wxBitmap& bmpUnchecked = wxNullBitmap)
60       { m_bmpChecked = bmpChecked;
61         m_bmpUnchecked = bmpUnchecked;
62         m_bOwnerDrawn = true; }
63 
SetBitmap(const wxBitmap & bmpChecked)64   void SetBitmap(const wxBitmap& bmpChecked)
65       { m_bmpChecked = bmpChecked;
66         m_bOwnerDrawn = true; }
67 
SetDisabledBitmap(const wxBitmap & bmpDisabled)68   void SetDisabledBitmap( const wxBitmap& bmpDisabled )
69       { m_bmpDisabled = bmpDisabled;
70         m_bOwnerDrawn = true; }
71 
72   const wxBitmap& GetBitmap(bool bChecked = true) const
73       { return (bChecked ? m_bmpChecked : m_bmpUnchecked); }
74 
GetDisabledBitmap()75   const wxBitmap& GetDisabledBitmap() const
76       { return m_bmpDisabled; }
77 
78   // the height of the menu checkmark (or bitmap) is determined by the font
79   // for the current item, but the width should be always the same (for the
80   // items to be aligned), so by default it's taken to be the same as for
81   // the last item (and default width for the first one).
82   //
83   // NB: default is too small for bitmaps, but ok for checkmarks.
SetMarginWidth(int nWidth)84   void SetMarginWidth(int nWidth)
85   {
86       ms_nLastMarginWidth = m_nMarginWidth = (size_t) nWidth;
87       if ( ((size_t) nWidth) != ms_nDefaultMarginWidth )
88           m_bOwnerDrawn = true;
89   }
90 
GetMarginWidth()91   int GetMarginWidth() const { return (int) m_nMarginWidth; }
GetDefaultMarginWidth()92   static int GetDefaultMarginWidth() { return (int) ms_nDefaultMarginWidth; }
93 
94   // accessors
SetName(const wxString & strName)95   void SetName(const wxString& strName)  { m_strName = strName; }
GetName()96   const wxString& GetName() const { return m_strName;    }
SetCheckable(bool checkable)97   void SetCheckable(bool checkable) { m_bCheckable = checkable; }
IsCheckable()98   bool IsCheckable() const { return m_bCheckable; }
99 
100   // this is for menu items only: accel string is drawn right aligned after the
101   // menu item if not empty
SetAccelString(const wxString & strAccel)102   void SetAccelString(const wxString& strAccel) { m_strAccel = strAccel; }
103 
104   // this function might seem strange, but if it returns false it means that
105   // no non-standard attribute are set, so there is no need for this control
106   // to be owner-drawn. Moreover, you can force owner-drawn to false if you
107   // want to change, say, the color for the item but only if it is owner-drawn
108   // (see wxMenuItem::wxMenuItem for example)
IsOwnerDrawn()109   bool IsOwnerDrawn() const { return m_bOwnerDrawn;   }
110 
111   // switch on/off owner-drawing the item
112   void SetOwnerDrawn(bool ownerDrawn = true) { m_bOwnerDrawn = ownerDrawn; }
ResetOwnerDrawn()113   void ResetOwnerDrawn() { m_bOwnerDrawn = false;  }
114 
115 public:
116   // constants used in OnDrawItem
117   // (they have the same values as corresponding Win32 constants)
118   enum wxODAction
119   {
120     wxODDrawAll       = 0x0001,   // redraw entire control
121     wxODSelectChanged = 0x0002,   // selection changed (see Status.Select)
122     wxODFocusChanged  = 0x0004    // keyboard focus changed (see Status.Focus)
123   };
124 
125   enum wxODStatus
126   {
127     wxODSelected  = 0x0001,         // control is currently selected
128     wxODGrayed    = 0x0002,         // item is to be grayed
129     wxODDisabled  = 0x0004,         // item is to be drawn as disabled
130     wxODChecked   = 0x0008,         // item is to be checked
131     wxODHasFocus  = 0x0010,         // item has the keyboard focus
132     wxODDefault   = 0x0020,         // item is the default item
133     wxODHidePrefix= 0x0100          // hide keyboard cues (w2k and xp only)
134   };
135 
136   // virtual functions to implement drawing (return true if processed)
137   virtual bool OnMeasureItem(size_t *pwidth, size_t *pheight);
138   virtual bool OnDrawItem(wxDC& dc, const wxRect& rc, wxODAction act, wxODStatus stat);
139 
140 protected:
141   // return true if this is a menu item
142   bool IsMenuItem() const;
143 
144   // get the font to use, whether m_font is set or not
145   wxFont GetFontToUse() const;
146 
147   // Same as wxOwnerDrawn::SetMarginWidth() but does not affect
148   // ms_nLastMarginWidth. Exists solely to work around bug #4068,
149   // and will not exist in wxWidgets 2.9.0 and later.
SetOwnMarginWidth(int nWidth)150   void SetOwnMarginWidth(int nWidth)
151   {
152       m_nMarginWidth = (size_t) nWidth;
153       if ( ((size_t) nWidth) != ms_nDefaultMarginWidth )
154           m_bOwnerDrawn = true;
155   }
156 
157 
158   wxString  m_strName,      // label for a manu item
159             m_strAccel;     // the accel string ("Ctrl-F17") if any
160 
161 private:
162   static size_t ms_nDefaultMarginWidth; // menu check mark width
163   static size_t ms_nLastMarginWidth;    // handy for aligning all items
164 
165   bool      m_bCheckable,   // used only for menu or check listbox items
166             m_bOwnerDrawn,  // true if something is non standard
167             m_isMenuItem;   // true if this is a menu item
168 
169   wxFont    m_font;         // font to use for drawing
170   wxColour  m_colText,      // color ----"---"---"----
171             m_colBack;      // background color
172   wxBitmap  m_bmpChecked,   // bitmap to put near the item
173             m_bmpUnchecked, // (checked is used also for 'uncheckable' items)
174             m_bmpDisabled;
175 
176   size_t    m_nHeight,      // font height
177             m_nMinHeight,   // minimum height, as determined by user's system settings
178             m_nMarginWidth; // space occupied by bitmap to the left of the item
179 };
180 
181 #endif // wxUSE_OWNER_DRAWN
182 
183 #endif
184   // _OWNERDRW_H
185