]>
git.saurik.com Git - wxWidgets.git/blob - src/common/ctrlsub.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/ctrlsub.cpp
3 // Purpose: wxItemContainer implementation
4 // Author: Vadim Zeitlin
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
29 #include "wx/ctrlsub.h"
30 #include "wx/arrstr.h"
33 IMPLEMENT_ABSTRACT_CLASS(wxControlWithItems
, wxControl
)
35 // ============================================================================
36 // wxItemContainerImmutable implementation
37 // ============================================================================
39 wxItemContainerImmutable::~wxItemContainerImmutable()
41 // this destructor is required for Darwin
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 wxString
wxItemContainerImmutable::GetStringSelection() const
52 int sel
= GetSelection();
53 if ( sel
!= wxNOT_FOUND
)
54 s
= GetString((unsigned int)sel
);
59 bool wxItemContainerImmutable::SetStringSelection(const wxString
& s
)
61 const int sel
= FindString(s
);
62 if ( sel
== wxNOT_FOUND
)
70 wxArrayString
wxItemContainerImmutable::GetStrings() const
74 const unsigned int count
= GetCount();
76 for ( unsigned int n
= 0; n
< count
; n
++ )
77 result
.Add(GetString(n
));
82 // ============================================================================
83 // wxItemContainer implementation
84 // ============================================================================
86 wxItemContainer::~wxItemContainer()
88 // this destructor is required for Darwin
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 void wxItemContainer::Clear()
97 if ( HasClientObjectData() )
99 const unsigned count
= GetCount();
100 for ( unsigned i
= 0; i
< count
; ++i
)
101 ResetItemClientObject(i
);
104 SetClientDataType(wxClientData_None
);
109 void wxItemContainer::Delete(unsigned int pos
)
111 wxCHECK_RET( pos
< GetCount(), wxT("invalid index") );
113 if ( HasClientObjectData() )
114 ResetItemClientObject(pos
);
116 DoDeleteOneItem(pos
);
120 SetClientDataType(wxClientData_None
);
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 int wxItemContainer::DoInsertItemsInLoop(const wxArrayStringsAdapter
& items
,
131 wxClientDataType type
)
135 const unsigned int count
= items
.GetCount();
136 for ( unsigned int i
= 0; i
< count
; ++i
)
138 n
= DoInsertOneItem(items
[i
], pos
++);
139 if ( n
== wxNOT_FOUND
)
142 AssignNewItemClientData(n
, clientData
, i
, type
);
149 wxItemContainer::DoInsertOneItem(const wxString
& WXUNUSED(item
),
150 unsigned int WXUNUSED(pos
))
152 wxFAIL_MSG( wxT("Must be overridden if DoInsertItemsInLoop() is used") );
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 void wxItemContainer::SetClientObject(unsigned int n
, wxClientData
*data
)
164 wxASSERT_MSG( !HasClientUntypedData(),
165 wxT("can't have both object and void client data") );
167 wxCHECK_RET( IsValid(n
), "Invalid index passed to SetClientObject()" );
169 if ( HasClientObjectData() )
171 wxClientData
* clientDataOld
172 = static_cast<wxClientData
*>(DoGetItemClientData(n
));
174 delete clientDataOld
;
176 else // didn't have any client data so far
178 // now we have object client data
179 DoInitItemClientData();
181 SetClientDataType(wxClientData_Object
);
184 DoSetItemClientData(n
, data
);
187 wxClientData
*wxItemContainer::GetClientObject(unsigned int n
) const
189 wxCHECK_MSG( HasClientObjectData(), NULL
,
190 wxT("this window doesn't have object client data") );
192 wxCHECK_MSG( IsValid(n
), NULL
,
193 "Invalid index passed to GetClientObject()" );
195 return static_cast<wxClientData
*>(DoGetItemClientData(n
));
198 wxClientData
*wxItemContainer::DetachClientObject(unsigned int n
)
200 wxClientData
* const data
= GetClientObject(n
);
203 // reset the pointer as we don't own it any more
204 DoSetItemClientData(n
, NULL
);
210 void wxItemContainer::SetClientData(unsigned int n
, void *data
)
212 if ( !HasClientData() )
214 DoInitItemClientData();
215 SetClientDataType(wxClientData_Void
);
218 wxASSERT_MSG( HasClientUntypedData(),
219 wxT("can't have both object and void client data") );
221 wxCHECK_RET( IsValid(n
), "Invalid index passed to SetClientData()" );
223 DoSetItemClientData(n
, data
);
226 void *wxItemContainer::GetClientData(unsigned int n
) const
228 wxCHECK_MSG( HasClientUntypedData(), NULL
,
229 wxT("this window doesn't have void client data") );
231 wxCHECK_MSG( IsValid(n
), NULL
,
232 "Invalid index passed to GetClientData()" );
234 return DoGetItemClientData(n
);
237 void wxItemContainer::AssignNewItemClientData(unsigned int pos
,
240 wxClientDataType type
)
244 case wxClientData_Object
:
248 (reinterpret_cast<wxClientData
**>(clientData
))[n
]
252 case wxClientData_Void
:
253 SetClientData(pos
, clientData
[n
]);
257 wxFAIL_MSG( wxT("unknown client data type") );
260 case wxClientData_None
:
266 void wxItemContainer::ResetItemClientObject(unsigned int n
)
268 wxClientData
* const data
= GetClientObject(n
);
272 DoSetItemClientData(n
, NULL
);
276 // ============================================================================
277 // wxControlWithItems implementation
278 // ============================================================================
281 wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent
& event
, int n
)
283 InitCommandEvent(event
);
285 if ( n
!= wxNOT_FOUND
)
287 if ( HasClientObjectData() )
288 event
.SetClientObject(GetClientObject(n
));
289 else if ( HasClientUntypedData() )
290 event
.SetClientData(GetClientData(n
));
294 void wxControlWithItemsBase::SendSelectionChangedEvent(wxEventType eventType
)
296 const int n
= GetSelection();
297 if ( n
== wxNOT_FOUND
)
300 wxCommandEvent
event(eventType
, m_windowId
);
302 event
.SetEventObject(this);
303 event
.SetString(GetStringSelection());
304 InitCommandEventWithItems(event
, n
);
306 HandleWindowEvent(event
);
309 #endif // wxUSE_CONTROLS