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 unsigned int GetCount() const = 0;
42 bool IsEmpty() const { return GetCount() == 0; }
44 virtual wxString
GetString(unsigned int n
) const = 0;
45 wxArrayString
GetStrings() const;
46 virtual void SetString(unsigned 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 unsigned int count
= GetCount();
55 for ( unsigned 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(unsigned int n
) const { return n
< GetCount(); }
86 inline bool IsValidInsert(unsigned int n
) const { return n
<= GetCount(); }
89 class WXDLLEXPORT wxItemContainer
: public wxItemContainerImmutable
92 wxItemContainer() { m_clientDataItemsType
= wxClientData_None
; }
93 virtual ~wxItemContainer();
98 int Append(const wxString
& item
)
99 { return DoAppend(item
); }
100 int Append(const wxString
& item
, void *clientData
)
101 { int n
= DoAppend(item
); SetClientData(n
, clientData
); return n
; }
102 int Append(const wxString
& item
, wxClientData
*clientData
)
103 { int n
= DoAppend(item
); SetClientObject(n
, clientData
); return n
; }
105 // only for rtti needs (separate name)
106 void AppendString( const wxString
& item
)
109 // append several items at once to the control
110 void Append(const wxArrayString
& strings
);
112 int Insert(const wxString
& item
, unsigned int pos
)
113 { return DoInsert(item
, pos
); }
114 int Insert(const wxString
& item
, unsigned int pos
, void *clientData
);
115 int Insert(const wxString
& item
, unsigned int pos
, wxClientData
*clientData
);
120 virtual void Clear() = 0;
121 virtual void Delete(unsigned int n
) = 0;
127 void SetClientData(unsigned int n
, void* clientData
);
128 void* GetClientData(unsigned int n
) const;
130 void SetClientObject(unsigned int n
, wxClientData
* clientData
);
131 wxClientData
* GetClientObject(unsigned int n
) const;
133 bool HasClientObjectData() const
134 { return m_clientDataItemsType
== wxClientData_Object
; }
135 bool HasClientUntypedData() const
136 { return m_clientDataItemsType
== wxClientData_Void
; }
139 virtual int DoAppend(const wxString
& item
) = 0;
140 virtual int DoInsert(const wxString
& item
, unsigned int pos
) = 0;
142 virtual void DoSetItemClientData(unsigned int n
, void* clientData
) = 0;
143 virtual void* DoGetItemClientData(unsigned int n
) const = 0;
144 virtual void DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
) = 0;
145 virtual wxClientData
* DoGetItemClientObject(unsigned int n
) const = 0;
148 // the type of the client data for the items
149 wxClientDataType m_clientDataItemsType
;
152 // this macro must (unfortunately) be used in any class deriving from both
153 // wxItemContainer and wxControl because otherwise there is ambiguity when
154 // calling GetClientXXX() functions -- the compiler can't choose between the
156 #define wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST \
157 void SetClientData(void *data) \
158 { wxEvtHandler::SetClientData(data); } \
159 void *GetClientData() const \
160 { return wxEvtHandler::GetClientData(); } \
161 void SetClientObject(wxClientData *data) \
162 { wxEvtHandler::SetClientObject(data); } \
163 wxClientData *GetClientObject() const \
164 { return wxEvtHandler::GetClientObject(); } \
165 void SetClientData(unsigned int n, void* clientData) \
166 { wxItemContainer::SetClientData(n, clientData); } \
167 void* GetClientData(unsigned int n) const \
168 { return wxItemContainer::GetClientData(n); } \
169 void SetClientObject(unsigned int n, wxClientData* clientData) \
170 { wxItemContainer::SetClientObject(n, clientData); } \
171 wxClientData* GetClientObject(unsigned int n) const \
172 { return wxItemContainer::GetClientObject(n); }
174 class WXDLLEXPORT wxControlWithItems
: public wxControl
, public wxItemContainer
177 wxControlWithItems() { }
178 virtual ~wxControlWithItems();
180 // we have to redefine these functions here to avoid ambiguities in classes
181 // deriving from us which would arise otherwise because both base classses
182 // have the methods with the same names - hopefully, a smart compiler can
183 // optimize away these simple inline wrappers so we don't suffer much from
185 wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST
187 // usually the controls like list/combo boxes have their own background
189 virtual bool ShouldInheritColours() const { return false; }
192 // we can't compute our best size before the items are added to the control
193 // which is done after calling SetInitialBestSize() (it is called from the
194 // base class ctor and the items are added in the derived class ctor), so
195 // don't do anything at all here as our size will be changed later anyhow
197 // of course, all derived classes *must* call SetBestSize() from their
198 // ctors for this to work!
199 virtual void SetInitialBestSize(const wxSize
& WXUNUSED(size
)) { }
201 // fill in the client object or data field of the event as appropriate
203 // calls InitCommandEvent() and, if n != wxNOT_FOUND, also sets the per
205 void InitCommandEventWithItems(wxCommandEvent
& event
, int n
);
208 DECLARE_ABSTRACT_CLASS(wxControlWithItems
)
209 DECLARE_NO_COPY_CLASS(wxControlWithItems
)
213 // ----------------------------------------------------------------------------
215 // ----------------------------------------------------------------------------
217 #endif // wxUSE_CONTROLS
219 #endif // _WX_CTRLSUB_H_BASE_