]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: wx/ctrlsub.h (read: "wxConTRoL with SUBitems") | |
3 | // Purpose: wxControlWithItems interface | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 22.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets team | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _WX_CTRLSUB_H_BASE_ | |
13 | #define _WX_CTRLSUB_H_BASE_ | |
14 | ||
15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) | |
16 | #pragma interface "controlwithitems.h" | |
17 | #endif | |
18 | ||
19 | #include "wx/defs.h" | |
20 | ||
21 | #if wxUSE_CONTROLS | |
22 | ||
23 | #include "wx/control.h" // base class | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // wxItemContainer defines an interface which is implemented by all controls | |
27 | // which have string subitems each of which may be selected. | |
28 | // | |
29 | // It is decomposed in wxItemContainerImmutable which omits all methods | |
30 | // adding/removing items and is used by wxRadioBox and wxItemContainer itself. | |
31 | // | |
32 | // Examples: wxListBox, wxCheckListBox, wxChoice and wxComboBox (which | |
33 | // implements an extended interface deriving from this one) | |
34 | // ---------------------------------------------------------------------------- | |
35 | ||
36 | class WXDLLEXPORT wxItemContainerImmutable | |
37 | { | |
38 | public: | |
39 | wxItemContainerImmutable() { } | |
40 | virtual ~wxItemContainerImmutable(); | |
41 | ||
42 | // accessing strings | |
43 | // ----------------- | |
44 | ||
45 | virtual int GetCount() const = 0; | |
46 | bool IsEmpty() const { return GetCount() == 0; } | |
47 | ||
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; | |
52 | ||
53 | ||
54 | // selection | |
55 | // --------- | |
56 | ||
57 | virtual void SetSelection(int n) = 0; | |
58 | virtual int GetSelection() const = 0; | |
59 | ||
60 | // set selection to the specified string, return false if not found | |
61 | bool SetStringSelection(const wxString& s); | |
62 | ||
63 | // return the selected string or empty string if none | |
64 | wxString GetStringSelection() const; | |
65 | ||
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); } | |
69 | ||
70 | ||
71 | protected: | |
72 | ||
73 | // check that the index is valid | |
74 | inline bool IsValid(int n) const { return n >= 0 && n < GetCount(); } | |
75 | }; | |
76 | ||
77 | class WXDLLEXPORT wxItemContainer : public wxItemContainerImmutable | |
78 | { | |
79 | public: | |
80 | wxItemContainer() { m_clientDataItemsType = wxClientData_None; } | |
81 | virtual ~wxItemContainer(); | |
82 | ||
83 | // adding items | |
84 | // ------------ | |
85 | ||
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; } | |
92 | ||
93 | // only for rtti needs (separate name) | |
94 | void AppendString( const wxString& item) | |
95 | { Append( item ); } | |
96 | ||
97 | // append several items at once to the control | |
98 | void Append(const wxArrayString& strings); | |
99 | ||
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); | |
104 | ||
105 | // deleting items | |
106 | // -------------- | |
107 | ||
108 | virtual void Clear() = 0; | |
109 | virtual void Delete(int n) = 0; | |
110 | ||
111 | // misc | |
112 | // ---- | |
113 | ||
114 | // client data stuff | |
115 | void SetClientData(int n, void* clientData); | |
116 | void* GetClientData(int n) const; | |
117 | ||
118 | void SetClientObject(int n, wxClientData* clientData); | |
119 | wxClientData* GetClientObject(int n) const; | |
120 | ||
121 | bool HasClientObjectData() const | |
122 | { return m_clientDataItemsType == wxClientData_Object; } | |
123 | bool HasClientUntypedData() const | |
124 | { return m_clientDataItemsType == wxClientData_Void; } | |
125 | ||
126 | #if WXWIN_COMPATIBILITY_2_2 | |
127 | // compatibility - these functions are deprecated, use the new ones | |
128 | // instead | |
129 | wxDEPRECATED( int Number() const ); | |
130 | #endif // WXWIN_COMPATIBILITY_2_2 | |
131 | ||
132 | protected: | |
133 | virtual int DoAppend(const wxString& item) = 0; | |
134 | virtual int DoInsert(const wxString& item, int pos) = 0; | |
135 | ||
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; | |
140 | ||
141 | // the type of the client data for the items | |
142 | wxClientDataType m_clientDataItemsType; | |
143 | }; | |
144 | ||
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 | |
148 | // two versions | |
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); } | |
166 | ||
167 | class WXDLLEXPORT wxControlWithItems : public wxControl, public wxItemContainer | |
168 | { | |
169 | public: | |
170 | wxControlWithItems() { } | |
171 | virtual ~wxControlWithItems(); | |
172 | ||
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 | |
177 | // this | |
178 | wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST | |
179 | ||
180 | // usually the controls like list/combo boxes have their own background | |
181 | // colour | |
182 | virtual bool ShouldInheritColours() const { return false; } | |
183 | ||
184 | protected: | |
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 | |
189 | // | |
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)) { } | |
193 | ||
194 | private: | |
195 | DECLARE_NO_COPY_CLASS(wxControlWithItems) | |
196 | }; | |
197 | ||
198 | ||
199 | // ---------------------------------------------------------------------------- | |
200 | // inline functions | |
201 | // ---------------------------------------------------------------------------- | |
202 | ||
203 | #if WXWIN_COMPATIBILITY_2_2 | |
204 | ||
205 | inline int wxItemContainer::Number() const | |
206 | { | |
207 | return GetCount(); | |
208 | } | |
209 | ||
210 | #endif // WXWIN_COMPATIBILITY_2_2 | |
211 | ||
212 | #endif // wxUSE_CONTROLS | |
213 | ||
214 | #endif // _WX_CTRLSUB_H_BASE_ | |
215 |