]>
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 m_clientDataItemsType
= wxClientData_None
;
110 void wxItemContainer::Delete(unsigned int pos
)
112 wxCHECK_RET( pos
< GetCount(), _T("invalid index") );
114 if ( HasClientObjectData() )
115 ResetItemClientObject(pos
);
117 DoDeleteOneItem(pos
);
121 m_clientDataItemsType
= 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( _T("Must be overridden if DoInsertItemsInLoop() is used") );
159 // ----------------------------------------------------------------------------
161 // ----------------------------------------------------------------------------
163 void wxItemContainer::SetClientObject(unsigned int n
, wxClientData
*data
)
165 wxASSERT_MSG( m_clientDataItemsType
== wxClientData_Object
||
166 m_clientDataItemsType
== wxClientData_None
,
167 wxT("can't have both object and void client data") );
169 if ( m_clientDataItemsType
== wxClientData_Object
)
171 wxClientData
* clientDataOld
172 = wx_static_cast(wxClientData
*, DoGetItemClientData(n
));
174 delete clientDataOld
;
176 else // m_clientDataItemsType == wxClientData_None
178 // now we have object client data
179 DoInitItemClientData();
181 m_clientDataItemsType
= wxClientData_Object
;
184 DoSetItemClientData(n
, data
);
187 wxClientData
*wxItemContainer::GetClientObject(unsigned int n
) const
189 wxCHECK_MSG( m_clientDataItemsType
== wxClientData_Object
, NULL
,
190 wxT("this window doesn't have object client data") );
192 return wx_static_cast(wxClientData
*, DoGetItemClientData(n
));
195 void wxItemContainer::SetClientData(unsigned int n
, void *data
)
197 if ( m_clientDataItemsType
== wxClientData_None
)
199 DoInitItemClientData();
200 m_clientDataItemsType
= wxClientData_Void
;
203 wxASSERT_MSG( m_clientDataItemsType
== wxClientData_Void
,
204 wxT("can't have both object and void client data") );
206 DoSetItemClientData(n
, data
);
209 void *wxItemContainer::GetClientData(unsigned int n
) const
211 wxCHECK_MSG( m_clientDataItemsType
== wxClientData_Void
, NULL
,
212 wxT("this window doesn't have void client data") );
214 return DoGetItemClientData(n
);
217 void wxItemContainer::AssignNewItemClientData(unsigned int pos
,
220 wxClientDataType type
)
224 case wxClientData_Object
:
228 (wx_reinterpret_cast(wxClientData
**, clientData
))[n
]
232 case wxClientData_Void
:
233 SetClientData(pos
, clientData
[n
]);
237 wxFAIL_MSG( _T("unknown client data type") );
240 case wxClientData_None
:
246 void wxItemContainer::ResetItemClientObject(unsigned int n
)
248 wxClientData
* const data
= GetClientObject(n
);
252 DoSetItemClientData(n
, NULL
);
256 // ============================================================================
257 // wxControlWithItems implementation
258 // ============================================================================
261 wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent
& event
, int n
)
263 InitCommandEvent(event
);
265 if ( n
!= wxNOT_FOUND
)
267 if ( HasClientObjectData() )
268 event
.SetClientObject(GetClientObject(n
));
269 else if ( HasClientUntypedData() )
270 event
.SetClientData(GetClientData(n
));
274 #endif // wxUSE_CONTROLS