]>
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
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
30 #include "wx/ctrlsub.h"
31 #include "wx/arrstr.h"
34 IMPLEMENT_ABSTRACT_CLASS(wxControlWithItems
, wxControl
)
36 // ============================================================================
37 // wxItemContainerImmutable implementation
38 // ============================================================================
40 wxItemContainerImmutable::~wxItemContainerImmutable()
42 // this destructor is required for Darwin
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 wxString
wxItemContainerImmutable::GetStringSelection() const
53 int sel
= GetSelection();
54 if ( sel
!= wxNOT_FOUND
)
55 s
= GetString((unsigned int)sel
);
60 bool wxItemContainerImmutable::SetStringSelection(const wxString
& s
)
62 const int sel
= FindString(s
);
63 if ( sel
== wxNOT_FOUND
)
71 wxArrayString
wxItemContainerImmutable::GetStrings() const
75 const unsigned int count
= GetCount();
77 for ( unsigned int n
= 0; n
< count
; n
++ )
78 result
.Add(GetString(n
));
83 // ============================================================================
84 // wxItemContainer implementation
85 // ============================================================================
87 wxItemContainer::~wxItemContainer()
89 // this destructor is required for Darwin
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 void wxItemContainer::Clear()
98 if ( HasClientObjectData() )
100 const unsigned count
= GetCount();
101 for ( unsigned i
= 0; i
< count
; ++i
)
102 ResetItemClientObject(i
);
105 SetClientDataType(wxClientData_None
);
110 void wxItemContainer::Delete(unsigned int pos
)
112 wxCHECK_RET( pos
< GetCount(), wxT("invalid index") );
114 if ( HasClientObjectData() )
115 ResetItemClientObject(pos
);
117 DoDeleteOneItem(pos
);
121 SetClientDataType(wxClientData_None
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 int wxItemContainer::DoInsertItemsInLoop(const wxArrayStringsAdapter
& items
,
132 wxClientDataType type
)
136 const unsigned int count
= items
.GetCount();
137 for ( unsigned int i
= 0; i
< count
; ++i
)
139 n
= DoInsertOneItem(items
[i
], pos
++);
140 if ( n
== wxNOT_FOUND
)
143 AssignNewItemClientData(n
, clientData
, i
, type
);
150 wxItemContainer::DoInsertOneItem(const wxString
& WXUNUSED(item
),
151 unsigned int WXUNUSED(pos
))
153 wxFAIL_MSG( wxT("Must be overridden if DoInsertItemsInLoop() is used") );
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxItemContainer::SetClientObject(unsigned int n
, wxClientData
*data
)
165 wxASSERT_MSG( !HasClientUntypedData(),
166 wxT("can't have both object and void client data") );
168 wxCHECK_RET( IsValid(n
), "Invalid index passed to SetClientObject()" );
170 if ( HasClientObjectData() )
172 wxClientData
* clientDataOld
173 = static_cast<wxClientData
*>(DoGetItemClientData(n
));
175 delete clientDataOld
;
177 else // didn't have any client data so far
179 // now we have object client data
180 DoInitItemClientData();
182 SetClientDataType(wxClientData_Object
);
185 DoSetItemClientData(n
, data
);
188 wxClientData
*wxItemContainer::GetClientObject(unsigned int n
) const
190 wxCHECK_MSG( HasClientObjectData(), NULL
,
191 wxT("this window doesn't have object client data") );
193 wxCHECK_MSG( IsValid(n
), NULL
,
194 "Invalid index passed to GetClientObject()" );
196 return static_cast<wxClientData
*>(DoGetItemClientData(n
));
199 wxClientData
*wxItemContainer::DetachClientObject(unsigned int n
)
201 wxClientData
* const data
= GetClientObject(n
);
204 // reset the pointer as we don't own it any more
205 DoSetItemClientData(n
, NULL
);
211 void wxItemContainer::SetClientData(unsigned int n
, void *data
)
213 if ( !HasClientData() )
215 DoInitItemClientData();
216 SetClientDataType(wxClientData_Void
);
219 wxASSERT_MSG( HasClientUntypedData(),
220 wxT("can't have both object and void client data") );
222 wxCHECK_RET( IsValid(n
), "Invalid index passed to SetClientData()" );
224 DoSetItemClientData(n
, data
);
227 void *wxItemContainer::GetClientData(unsigned int n
) const
229 wxCHECK_MSG( HasClientUntypedData(), NULL
,
230 wxT("this window doesn't have void client data") );
232 wxCHECK_MSG( IsValid(n
), NULL
,
233 "Invalid index passed to GetClientData()" );
235 return DoGetItemClientData(n
);
238 void wxItemContainer::AssignNewItemClientData(unsigned int pos
,
241 wxClientDataType type
)
245 case wxClientData_Object
:
249 (reinterpret_cast<wxClientData
**>(clientData
))[n
]
253 case wxClientData_Void
:
254 SetClientData(pos
, clientData
[n
]);
258 wxFAIL_MSG( wxT("unknown client data type") );
261 case wxClientData_None
:
267 void wxItemContainer::ResetItemClientObject(unsigned int n
)
269 wxClientData
* const data
= GetClientObject(n
);
273 DoSetItemClientData(n
, NULL
);
277 // ============================================================================
278 // wxControlWithItems implementation
279 // ============================================================================
282 wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent
& event
, int n
)
284 InitCommandEvent(event
);
286 if ( n
!= wxNOT_FOUND
)
288 if ( HasClientObjectData() )
289 event
.SetClientObject(GetClientObject(n
));
290 else if ( HasClientUntypedData() )
291 event
.SetClientData(GetClientData(n
));
295 void wxControlWithItemsBase::SendSelectionChangedEvent(wxEventType eventType
)
297 const int n
= GetSelection();
298 if ( n
== wxNOT_FOUND
)
301 wxCommandEvent
event(eventType
, m_windowId
);
303 event
.SetEventObject(this);
304 event
.SetString(GetStringSelection());
305 InitCommandEventWithItems(event
, n
);
307 HandleWindowEvent(event
);
310 #endif // wxUSE_CONTROLS