1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/ctrlsub.h (read: "wxConTRoL with SUBitems")
3 // Purpose: wxControlWithItems interface
4 // Author: Vadim Zeitlin
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_CTRLSUB_H_BASE_
13 #define _WX_CTRLSUB_H_BASE_
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "controlwithitems.h"
23 #include "wx/control.h" // base class
25 // ----------------------------------------------------------------------------
26 // wxItemContainer defines an interface which is implemented by all controls
27 // which have string subitems each of which may be selected.
29 // It is decomposed in wxItemContainerImmutable which omits all methods
30 // adding/removing items and is used by wxRadioBox and wxItemContainer itself.
32 // Examples: wxListBox, wxCheckListBox, wxChoice and wxComboBox (which
33 // implements an extended interface deriving from this one)
34 // ----------------------------------------------------------------------------
36 class WXDLLEXPORT wxItemContainerImmutable
39 wxItemContainerImmutable() { }
40 virtual ~wxItemContainerImmutable();
45 virtual int GetCount() const = 0;
46 bool IsEmpty() const { return GetCount() == 0; }
48 virtual wxString
GetString(int n
) const = 0;
49 wxArrayString
GetStrings() const;
50 virtual void SetString(int n
, const wxString
& s
) = 0;
51 virtual int FindString(const wxString
& s
) const = 0;
57 virtual void SetSelection(int n
) = 0;
58 virtual int GetSelection() const = 0;
60 // set selection to the specified string, return false if not found
61 bool SetStringSelection(const wxString
& s
);
63 // return the selected string or empty string if none
64 wxString
GetStringSelection() const;
66 // this is the same as SetSelection( for single-selection controls but
67 // reads better for multi-selection ones
68 void Select(int n
) { SetSelection(n
); }
73 // check that the index is valid
74 inline bool IsValid(int n
) const { return n
>= 0 && n
< GetCount(); }
77 class WXDLLEXPORT wxItemContainer
: public wxItemContainerImmutable
80 wxItemContainer() { m_clientDataItemsType
= wxClientData_None
; }
81 virtual ~wxItemContainer();
86 int Append(const wxString
& item
)
87 { return DoAppend(item
); }
88 int Append(const wxString
& item
, void *clientData
)
89 { int n
= DoAppend(item
); SetClientData(n
, clientData
); return n
; }
90 int Append(const wxString
& item
, wxClientData
*clientData
)
91 { int n
= DoAppend(item
); SetClientObject(n
, clientData
); return n
; }
93 // only for rtti needs (separate name)
94 void AppendString( const wxString
& item
)
97 // append several items at once to the control
98 void Append(const wxArrayString
& strings
);
100 int Insert(const wxString
& item
, int pos
)
101 { return DoInsert(item
, pos
); }
102 int Insert(const wxString
& item
, int pos
, void *clientData
);
103 int Insert(const wxString
& item
, int pos
, wxClientData
*clientData
);
108 virtual void Clear() = 0;
109 virtual void Delete(int n
) = 0;
115 void SetClientData(int n
, void* clientData
);
116 void* GetClientData(int n
) const;
118 void SetClientObject(int n
, wxClientData
* clientData
);
119 wxClientData
* GetClientObject(int n
) const;
121 bool HasClientObjectData() const
122 { return m_clientDataItemsType
== wxClientData_Object
; }
123 bool HasClientUntypedData() const
124 { return m_clientDataItemsType
== wxClientData_Void
; }
126 #if WXWIN_COMPATIBILITY_2_2
127 // compatibility - these functions are deprecated, use the new ones
129 wxDEPRECATED( int Number() const );
130 #endif // WXWIN_COMPATIBILITY_2_2
133 virtual int DoAppend(const wxString
& item
) = 0;
134 virtual int DoInsert(const wxString
& item
, int pos
) = 0;
136 virtual void DoSetItemClientData(int n
, void* clientData
) = 0;
137 virtual void* DoGetItemClientData(int n
) const = 0;
138 virtual void DoSetItemClientObject(int n
, wxClientData
* clientData
) = 0;
139 virtual wxClientData
* DoGetItemClientObject(int n
) const = 0;
141 // the type of the client data for the items
142 wxClientDataType m_clientDataItemsType
;
145 // this macro must (unfortunately) be used in any class deriving from both
146 // wxItemContainer and wxControl because otherwise there is ambiguity when
147 // calling GetClientXXX() functions -- the compiler can't choose between the
149 #define wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST \
150 void SetClientData(void *data) \
151 { wxControl::SetClientData(data); } \
152 void *GetClientData() const \
153 { return wxControl::GetClientData(); } \
154 void SetClientObject(wxClientData *data) \
155 { wxControl::SetClientObject(data); } \
156 wxClientData *GetClientObject() const \
157 { return wxControl::GetClientObject(); } \
158 void SetClientData(int n, void* clientData) \
159 { wxItemContainer::SetClientData(n, clientData); } \
160 void* GetClientData(int n) const \
161 { return wxItemContainer::GetClientData(n); } \
162 void SetClientObject(int n, wxClientData* clientData) \
163 { wxItemContainer::SetClientObject(n, clientData); } \
164 wxClientData* GetClientObject(int n) const \
165 { return wxItemContainer::GetClientObject(n); }
167 class WXDLLEXPORT wxControlWithItems
: public wxControl
, public wxItemContainer
170 wxControlWithItems() { }
171 virtual ~wxControlWithItems();
173 // we have to redefine these functions here to avoid ambiguities in classes
174 // deriving from us which would arise otherwise because both base classses
175 // have the methods with the same names - hopefully, a smart compiler can
176 // optimize away these simple inline wrappers so we don't suffer much from
178 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
180 // usually the controls like list/combo boxes have their own background
182 virtual bool ShouldInheritColours() const { return false; }
185 // we can't compute our best size before the items are added to the control
186 // which is done after calling SetInitialBestSize() (it is called from the
187 // base class ctor and the items are added in the derived class ctor), so
188 // don't do anything at all here as our size will be changed later anyhow
190 // of course, all derived classes *must* call SetBestSize() from their
191 // ctors for this to work!
192 virtual void SetInitialBestSize(const wxSize
& WXUNUSED(size
)) { }
195 DECLARE_NO_COPY_CLASS(wxControlWithItems
)
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
203 #if WXWIN_COMPATIBILITY_2_2
205 inline int wxItemContainer::Number() const
210 #endif // WXWIN_COMPATIBILITY_2_2
212 #endif // wxUSE_CONTROLS
214 #endif // _WX_CTRLSUB_H_BASE_