]>
Commit | Line | Data |
---|---|---|
6c8a980f VZ |
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$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets team |
65571936 | 9 | // Licence: wxWindows licence |
6c8a980f VZ |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifndef _WX_CTRLSUB_H_BASE_ | |
13 | #define _WX_CTRLSUB_H_BASE_ | |
14 | ||
2ecf902b WS |
15 | #include "wx/defs.h" |
16 | ||
1e6feb95 VZ |
17 | #if wxUSE_CONTROLS |
18 | ||
6c8a980f VZ |
19 | #include "wx/control.h" // base class |
20 | ||
21 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 22 | // wxItemContainer defines an interface which is implemented by all controls |
6c8a980f VZ |
23 | // which have string subitems each of which may be selected. |
24 | // | |
8ba7c771 VZ |
25 | // It is decomposed in wxItemContainerImmutable which omits all methods |
26 | // adding/removing items and is used by wxRadioBox and wxItemContainer itself. | |
27 | // | |
1e6feb95 VZ |
28 | // Examples: wxListBox, wxCheckListBox, wxChoice and wxComboBox (which |
29 | // implements an extended interface deriving from this one) | |
6c8a980f VZ |
30 | // ---------------------------------------------------------------------------- |
31 | ||
8ba7c771 VZ |
32 | class WXDLLEXPORT wxItemContainerImmutable |
33 | { | |
34 | public: | |
35 | wxItemContainerImmutable() { } | |
36 | virtual ~wxItemContainerImmutable(); | |
37 | ||
38 | // accessing strings | |
39 | // ----------------- | |
40 | ||
8228b893 | 41 | virtual size_t GetCount() const = 0; |
8ba7c771 VZ |
42 | bool IsEmpty() const { return GetCount() == 0; } |
43 | ||
44 | virtual wxString GetString(int n) const = 0; | |
45 | wxArrayString GetStrings() const; | |
46 | virtual void SetString(int n, const wxString& s) = 0; | |
853dcc57 WS |
47 | |
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 | |
52 | { | |
8228b893 | 53 | size_t count = GetCount(); |
853dcc57 | 54 | |
8228b893 | 55 | for ( size_t i = 0; i < count ; ++i ) |
853dcc57 WS |
56 | { |
57 | if (GetString(i).IsSameAs( s , bCase )) | |
58 | return i; | |
59 | } | |
60 | ||
61 | return wxNOT_FOUND; | |
62 | } | |
8ba7c771 VZ |
63 | |
64 | ||
65 | // selection | |
66 | // --------- | |
67 | ||
68 | virtual void SetSelection(int n) = 0; | |
69 | virtual int GetSelection() const = 0; | |
70 | ||
71 | // set selection to the specified string, return false if not found | |
72 | bool SetStringSelection(const wxString& s); | |
73 | ||
74 | // return the selected string or empty string if none | |
75 | wxString GetStringSelection() const; | |
76 | ||
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); } | |
80 | ||
789f6795 WS |
81 | |
82 | protected: | |
83 | ||
84 | // check that the index is valid | |
8228b893 WS |
85 | // FIXME: once api will move to size_t, drop >= 0 check |
86 | inline bool IsValid(int n) const { return n >= 0 && (size_t)n < GetCount(); } | |
87 | inline bool IsValidInsert(int n) const { return n >= 0 && (size_t)n <= GetCount(); } | |
8ba7c771 VZ |
88 | }; |
89 | ||
90 | class WXDLLEXPORT wxItemContainer : public wxItemContainerImmutable | |
6c8a980f VZ |
91 | { |
92 | public: | |
6463b9f5 | 93 | wxItemContainer() { m_clientDataItemsType = wxClientData_None; } |
799ea011 | 94 | virtual ~wxItemContainer(); |
6c8a980f VZ |
95 | |
96 | // adding items | |
97 | // ------------ | |
98 | ||
1e6feb95 VZ |
99 | int Append(const wxString& item) |
100 | { return DoAppend(item); } | |
101 | int Append(const wxString& item, void *clientData) | |
102 | { int n = DoAppend(item); SetClientData(n, clientData); return n; } | |
103 | int Append(const wxString& item, wxClientData *clientData) | |
104 | { int n = DoAppend(item); SetClientObject(n, clientData); return n; } | |
6c8a980f | 105 | |
1978421a SC |
106 | // only for rtti needs (separate name) |
107 | void AppendString( const wxString& item) | |
64fa6f16 | 108 | { Append( item ); } |
1978421a | 109 | |
0e0bc921 VZ |
110 | // append several items at once to the control |
111 | void Append(const wxArrayString& strings); | |
112 | ||
243dbf1a VZ |
113 | int Insert(const wxString& item, int pos) |
114 | { return DoInsert(item, pos); } | |
115 | int Insert(const wxString& item, int pos, void *clientData); | |
116 | int Insert(const wxString& item, int pos, wxClientData *clientData); | |
117 | ||
6c8a980f VZ |
118 | // deleting items |
119 | // -------------- | |
120 | ||
121 | virtual void Clear() = 0; | |
122 | virtual void Delete(int n) = 0; | |
123 | ||
6c8a980f VZ |
124 | // misc |
125 | // ---- | |
126 | ||
127 | // client data stuff | |
128 | void SetClientData(int n, void* clientData); | |
129 | void* GetClientData(int n) const; | |
130 | ||
131 | void SetClientObject(int n, wxClientData* clientData); | |
132 | wxClientData* GetClientObject(int n) const; | |
133 | ||
134 | bool HasClientObjectData() const | |
1e6feb95 | 135 | { return m_clientDataItemsType == wxClientData_Object; } |
6c8a980f | 136 | bool HasClientUntypedData() const |
1e6feb95 | 137 | { return m_clientDataItemsType == wxClientData_Void; } |
6c8a980f | 138 | |
6c8a980f VZ |
139 | protected: |
140 | virtual int DoAppend(const wxString& item) = 0; | |
243dbf1a | 141 | virtual int DoInsert(const wxString& item, int pos) = 0; |
6c8a980f VZ |
142 | |
143 | virtual void DoSetItemClientData(int n, void* clientData) = 0; | |
144 | virtual void* DoGetItemClientData(int n) const = 0; | |
145 | virtual void DoSetItemClientObject(int n, wxClientData* clientData) = 0; | |
146 | virtual wxClientData* DoGetItemClientObject(int n) const = 0; | |
147 | ||
148 | // the type of the client data for the items | |
149 | wxClientDataType m_clientDataItemsType; | |
150 | }; | |
151 | ||
0ae0bb79 VZ |
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 | |
155 | // two versions | |
156 | #define wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST \ | |
157 | void SetClientData(void *data) \ | |
158 | { wxControl::SetClientData(data); } \ | |
159 | void *GetClientData() const \ | |
160 | { return wxControl::GetClientData(); } \ | |
161 | void SetClientObject(wxClientData *data) \ | |
162 | { wxControl::SetClientObject(data); } \ | |
163 | wxClientData *GetClientObject() const \ | |
164 | { return wxControl::GetClientObject(); } \ | |
165 | void SetClientData(int n, void* clientData) \ | |
166 | { wxItemContainer::SetClientData(n, clientData); } \ | |
167 | void* GetClientData(int n) const \ | |
168 | { return wxItemContainer::GetClientData(n); } \ | |
169 | void SetClientObject(int n, wxClientData* clientData) \ | |
170 | { wxItemContainer::SetClientObject(n, clientData); } \ | |
171 | wxClientData* GetClientObject(int n) const \ | |
172 | { return wxItemContainer::GetClientObject(n); } | |
173 | ||
1e6feb95 VZ |
174 | class WXDLLEXPORT wxControlWithItems : public wxControl, public wxItemContainer |
175 | { | |
176 | public: | |
6463b9f5 | 177 | wxControlWithItems() { } |
7c720ce6 | 178 | virtual ~wxControlWithItems(); |
0ae0bb79 | 179 | |
1e6feb95 VZ |
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 | |
184 | // this | |
0ae0bb79 | 185 | wxCONTROL_ITEMCONTAINER_CLIENTDATAOBJECT_RECAST |
fc7a2a60 | 186 | |
d4864e97 VZ |
187 | // usually the controls like list/combo boxes have their own background |
188 | // colour | |
189 | virtual bool ShouldInheritColours() const { return false; } | |
190 | ||
929bd5fd VZ |
191 | protected: |
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 | |
196 | // | |
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)) { } | |
200 | ||
fc7a2a60 | 201 | private: |
1440bc7a | 202 | DECLARE_ABSTRACT_CLASS(wxControlWithItems) |
aa3d520a | 203 | DECLARE_NO_COPY_CLASS(wxControlWithItems) |
1e6feb95 VZ |
204 | }; |
205 | ||
c6179a84 VZ |
206 | |
207 | // ---------------------------------------------------------------------------- | |
208 | // inline functions | |
209 | // ---------------------------------------------------------------------------- | |
210 | ||
1e6feb95 | 211 | #endif // wxUSE_CONTROLS |
6c8a980f | 212 | |
1e6feb95 | 213 | #endif // _WX_CTRLSUB_H_BASE_ |