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_
19 #include "wx/control.h" // base class
21 // ----------------------------------------------------------------------------
22 // wxItemContainer defines an interface which is implemented by all controls
23 // which have string subitems each of which may be selected.
25 // It is decomposed in wxItemContainerImmutable which omits all methods
26 // adding/removing items and is used by wxRadioBox and wxItemContainer itself.
28 // Examples: wxListBox, wxCheckListBox, wxChoice and wxComboBox (which
29 // implements an extended interface deriving from this one)
30 // ----------------------------------------------------------------------------
32 class WXDLLEXPORT wxItemContainerImmutable
35 wxItemContainerImmutable() { }
36 virtual ~wxItemContainerImmutable();
41 virtual int GetCount() const = 0;
42 bool IsEmpty() const { return GetCount() == 0; }
44 virtual wxString
GetString(int n
) const = 0;
45 wxArrayString
GetStrings() const;
46 virtual void SetString(int n
, const wxString
& s
) = 0;
48 // finding string natively is either case sensitive or insensitive
49 // but never both so fall back to this base version for not
50 // supported search type
51 virtual int FindString(const wxString
& s
, bool bCase
= false) const
53 int count
= GetCount();
55 for ( int i
= 0; i
< count
; i
++ )
57 if (GetString(i
).IsSameAs( s
, bCase
))
68 virtual void SetSelection(int n
) = 0;
69 virtual int GetSelection() const = 0;
71 // set selection to the specified string, return false if not found
72 bool SetStringSelection(const wxString
& s
);
74 // return the selected string or empty string if none
75 wxString
GetStringSelection() const;
77 // this is the same as SetSelection( for single-selection controls but
78 // reads better for multi-selection ones
79 void Select(int n
) { SetSelection(n
); }
84 // check that the index is valid
85 inline bool IsValid(int n
) const { return n
>= 0 && n
< GetCount(); }
88 class WXDLLEXPORT wxItemContainer
: public wxItemContainerImmutable
91 wxItemContainer() { m_clientDataItemsType
= wxClientData_None
; }
92 virtual ~wxItemContainer();
97 int Append(const wxString
& item
)
98 { return DoAppend(item
); }
99 int Append(const wxString
& item
, void *clientData
)
100 { int n
= DoAppend(item
); SetClientData(n
, clientData
); return n
; }
101 int Append(const wxString
& item
, wxClientData
*clientData
)
102 { int n
= DoAppend(item
); SetClientObject(n
, clientData
); return n
; }
104 // only for rtti needs (separate name)
105 void AppendString( const wxString
& item
)
108 // append several items at once to the control
109 void Append(const wxArrayString
& strings
);
111 int Insert(const wxString
& item
, int pos
)
112 { return DoInsert(item
, pos
); }
113 int Insert(const wxString
& item
, int pos
, void *clientData
);
114 int Insert(const wxString
& item
, int pos
, wxClientData
*clientData
);
119 virtual void Clear() = 0;
120 virtual void Delete(int n
) = 0;
126 void SetClientData(int n
, void* clientData
);
127 void* GetClientData(int n
) const;
129 void SetClientObject(int n
, wxClientData
* clientData
);
130 wxClientData
* GetClientObject(int n
) const;
132 bool HasClientObjectData() const
133 { return m_clientDataItemsType
== wxClientData_Object
; }
134 bool HasClientUntypedData() const
135 { return m_clientDataItemsType
== wxClientData_Void
; }
138 virtual int DoAppend(const wxString
& item
) = 0;
139 virtual int DoInsert(const wxString
& item
, int pos
) = 0;
141 virtual void DoSetItemClientData(int n
, void* clientData
) = 0;
142 virtual void* DoGetItemClientData(int n
) const = 0;
143 virtual void DoSetItemClientObject(int n
, wxClientData
* clientData
) = 0;
144 virtual wxClientData
* DoGetItemClientObject(int n
) const = 0;
146 // the type of the client data for the items
147 wxClientDataType m_clientDataItemsType
;
150 // this macro must (unfortunately) be used in any class deriving from both
151 // wxItemContainer and wxControl because otherwise there is ambiguity when
152 // calling GetClientXXX() functions -- the compiler can't choose between the
154 #define wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST \
155 void SetClientData(void *data) \
156 { wxControl::SetClientData(data); } \
157 void *GetClientData() const \
158 { return wxControl::GetClientData(); } \
159 void SetClientObject(wxClientData *data) \
160 { wxControl::SetClientObject(data); } \
161 wxClientData *GetClientObject() const \
162 { return wxControl::GetClientObject(); } \
163 void SetClientData(int n, void* clientData) \
164 { wxItemContainer::SetClientData(n, clientData); } \
165 void* GetClientData(int n) const \
166 { return wxItemContainer::GetClientData(n); } \
167 void SetClientObject(int n, wxClientData* clientData) \
168 { wxItemContainer::SetClientObject(n, clientData); } \
169 wxClientData* GetClientObject(int n) const \
170 { return wxItemContainer::GetClientObject(n); }
172 class WXDLLEXPORT wxControlWithItems
: public wxControl
, public wxItemContainer
175 wxControlWithItems() { }
176 virtual ~wxControlWithItems();
178 // we have to redefine these functions here to avoid ambiguities in classes
179 // deriving from us which would arise otherwise because both base classses
180 // have the methods with the same names - hopefully, a smart compiler can
181 // optimize away these simple inline wrappers so we don't suffer much from
183 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
185 // usually the controls like list/combo boxes have their own background
187 virtual bool ShouldInheritColours() const { return false; }
190 // we can't compute our best size before the items are added to the control
191 // which is done after calling SetInitialBestSize() (it is called from the
192 // base class ctor and the items are added in the derived class ctor), so
193 // don't do anything at all here as our size will be changed later anyhow
195 // of course, all derived classes *must* call SetBestSize() from their
196 // ctors for this to work!
197 virtual void SetInitialBestSize(const wxSize
& WXUNUSED(size
)) { }
200 DECLARE_ABSTRACT_CLASS(wxControlWithItems
)
201 DECLARE_NO_COPY_CLASS(wxControlWithItems
)
205 // ----------------------------------------------------------------------------
207 // ----------------------------------------------------------------------------
209 #endif // wxUSE_CONTROLS
211 #endif // _WX_CTRLSUB_H_BASE_