]> git.saurik.com Git - wxWidgets.git/blob - src/common/ctrlsub.cpp
Correct id in the wxRichTextCtrl::OnUndo() documentation.
[wxWidgets.git] / src / common / ctrlsub.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/ctrlsub.cpp
3 // Purpose: wxItemContainer implementation
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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_CONTROLS
28
29 #ifndef WX_PRECOMP
30 #include "wx/ctrlsub.h"
31 #include "wx/arrstr.h"
32 #endif
33
34 IMPLEMENT_ABSTRACT_CLASS(wxControlWithItems, wxControl)
35
36 // ============================================================================
37 // wxItemContainerImmutable implementation
38 // ============================================================================
39
40 wxItemContainerImmutable::~wxItemContainerImmutable()
41 {
42 // this destructor is required for Darwin
43 }
44
45 // ----------------------------------------------------------------------------
46 // selection
47 // ----------------------------------------------------------------------------
48
49 wxString wxItemContainerImmutable::GetStringSelection() const
50 {
51 wxString s;
52
53 int sel = GetSelection();
54 if ( sel != wxNOT_FOUND )
55 s = GetString((unsigned int)sel);
56
57 return s;
58 }
59
60 bool wxItemContainerImmutable::SetStringSelection(const wxString& s)
61 {
62 const int sel = FindString(s);
63 if ( sel == wxNOT_FOUND )
64 return false;
65
66 SetSelection(sel);
67
68 return true;
69 }
70
71 wxArrayString wxItemContainerImmutable::GetStrings() const
72 {
73 wxArrayString result;
74
75 const unsigned int count = GetCount();
76 result.Alloc(count);
77 for ( unsigned int n = 0; n < count; n++ )
78 result.Add(GetString(n));
79
80 return result;
81 }
82
83 // ============================================================================
84 // wxItemContainer implementation
85 // ============================================================================
86
87 wxItemContainer::~wxItemContainer()
88 {
89 // this destructor is required for Darwin
90 }
91
92 // ----------------------------------------------------------------------------
93 // deleting items
94 // ----------------------------------------------------------------------------
95
96 void wxItemContainer::Clear()
97 {
98 if ( HasClientObjectData() )
99 {
100 const unsigned count = GetCount();
101 for ( unsigned i = 0; i < count; ++i )
102 ResetItemClientObject(i);
103 }
104
105 SetClientDataType(wxClientData_None);
106
107 DoClear();
108 }
109
110 void wxItemContainer::Delete(unsigned int pos)
111 {
112 wxCHECK_RET( pos < GetCount(), wxT("invalid index") );
113
114 if ( HasClientObjectData() )
115 ResetItemClientObject(pos);
116
117 DoDeleteOneItem(pos);
118
119 if ( IsEmpty() )
120 {
121 SetClientDataType(wxClientData_None);
122 }
123 }
124
125 // ----------------------------------------------------------------------------
126 //
127 // ----------------------------------------------------------------------------
128
129 int wxItemContainer::DoInsertItemsInLoop(const wxArrayStringsAdapter& items,
130 unsigned int pos,
131 void **clientData,
132 wxClientDataType type)
133 {
134 int n = wxNOT_FOUND;
135
136 const unsigned int count = items.GetCount();
137 for ( unsigned int i = 0; i < count; ++i )
138 {
139 n = DoInsertOneItem(items[i], pos++);
140 if ( n == wxNOT_FOUND )
141 break;
142
143 AssignNewItemClientData(n, clientData, i, type);
144 }
145
146 return n;
147 }
148
149 int
150 wxItemContainer::DoInsertOneItem(const wxString& WXUNUSED(item),
151 unsigned int WXUNUSED(pos))
152 {
153 wxFAIL_MSG( wxT("Must be overridden if DoInsertItemsInLoop() is used") );
154
155 return wxNOT_FOUND;
156 }
157
158
159 // ----------------------------------------------------------------------------
160 // client data
161 // ----------------------------------------------------------------------------
162
163 void wxItemContainer::SetClientObject(unsigned int n, wxClientData *data)
164 {
165 wxASSERT_MSG( !HasClientUntypedData(),
166 wxT("can't have both object and void client data") );
167
168 if ( HasClientObjectData() )
169 {
170 wxClientData * clientDataOld
171 = static_cast<wxClientData *>(DoGetItemClientData(n));
172 if ( clientDataOld )
173 delete clientDataOld;
174 }
175 else // didn't have any client data so far
176 {
177 // now we have object client data
178 DoInitItemClientData();
179
180 SetClientDataType(wxClientData_Object);
181 }
182
183 DoSetItemClientData(n, data);
184 }
185
186 wxClientData *wxItemContainer::GetClientObject(unsigned int n) const
187 {
188 wxCHECK_MSG( HasClientObjectData(), NULL,
189 wxT("this window doesn't have object client data") );
190
191 return static_cast<wxClientData *>(DoGetItemClientData(n));
192 }
193
194 wxClientData *wxItemContainer::DetachClientObject(unsigned int n)
195 {
196 wxClientData * const data = GetClientObject(n);
197 if ( data )
198 {
199 // reset the pointer as we don't own it any more
200 DoSetItemClientData(n, NULL);
201 }
202
203 return data;
204 }
205
206 void wxItemContainer::SetClientData(unsigned int n, void *data)
207 {
208 if ( !HasClientData() )
209 {
210 DoInitItemClientData();
211 SetClientDataType(wxClientData_Void);
212 }
213
214 wxASSERT_MSG( HasClientUntypedData(),
215 wxT("can't have both object and void client data") );
216
217 DoSetItemClientData(n, data);
218 }
219
220 void *wxItemContainer::GetClientData(unsigned int n) const
221 {
222 wxCHECK_MSG( HasClientUntypedData(), NULL,
223 wxT("this window doesn't have void client data") );
224
225 return DoGetItemClientData(n);
226 }
227
228 void wxItemContainer::AssignNewItemClientData(unsigned int pos,
229 void **clientData,
230 unsigned int n,
231 wxClientDataType type)
232 {
233 switch ( type )
234 {
235 case wxClientData_Object:
236 SetClientObject
237 (
238 pos,
239 (reinterpret_cast<wxClientData **>(clientData))[n]
240 );
241 break;
242
243 case wxClientData_Void:
244 SetClientData(pos, clientData[n]);
245 break;
246
247 default:
248 wxFAIL_MSG( wxT("unknown client data type") );
249 // fall through
250
251 case wxClientData_None:
252 // nothing to do
253 break;
254 }
255 }
256
257 void wxItemContainer::ResetItemClientObject(unsigned int n)
258 {
259 wxClientData * const data = GetClientObject(n);
260 if ( data )
261 {
262 delete data;
263 DoSetItemClientData(n, NULL);
264 }
265 }
266
267 // ============================================================================
268 // wxControlWithItems implementation
269 // ============================================================================
270
271 void
272 wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent& event, int n)
273 {
274 InitCommandEvent(event);
275
276 if ( n != wxNOT_FOUND )
277 {
278 if ( HasClientObjectData() )
279 event.SetClientObject(GetClientObject(n));
280 else if ( HasClientUntypedData() )
281 event.SetClientData(GetClientData(n));
282 }
283 }
284
285 #endif // wxUSE_CONTROLS