]>
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 if ( HasClientObjectData() )
170 wxClientData
* clientDataOld
171 = static_cast<wxClientData
*>(DoGetItemClientData(n
));
173 delete clientDataOld
;
175 else // didn't have any client data so far
177 // now we have object client data
178 DoInitItemClientData();
180 SetClientDataType(wxClientData_Object
);
183 DoSetItemClientData(n
, data
);
186 wxClientData
*wxItemContainer::GetClientObject(unsigned int n
) const
188 wxCHECK_MSG( HasClientObjectData(), NULL
,
189 wxT("this window doesn't have object client data") );
191 return static_cast<wxClientData
*>(DoGetItemClientData(n
));
194 void wxItemContainer::SetClientData(unsigned int n
, void *data
)
196 if ( !HasClientData() )
198 DoInitItemClientData();
199 SetClientDataType(wxClientData_Void
);
202 wxASSERT_MSG( HasClientUntypedData(),
203 wxT("can't have both object and void client data") );
205 DoSetItemClientData(n
, data
);
208 void *wxItemContainer::GetClientData(unsigned int n
) const
210 wxCHECK_MSG( HasClientUntypedData(), NULL
,
211 wxT("this window doesn't have void client data") );
213 return DoGetItemClientData(n
);
216 void wxItemContainer::AssignNewItemClientData(unsigned int pos
,
219 wxClientDataType type
)
223 case wxClientData_Object
:
227 (reinterpret_cast<wxClientData
**>(clientData
))[n
]
231 case wxClientData_Void
:
232 SetClientData(pos
, clientData
[n
]);
236 wxFAIL_MSG( wxT("unknown client data type") );
239 case wxClientData_None
:
245 void wxItemContainer::ResetItemClientObject(unsigned int n
)
247 wxClientData
* const data
= GetClientObject(n
);
251 DoSetItemClientData(n
, NULL
);
255 // ============================================================================
256 // wxControlWithItems implementation
257 // ============================================================================
260 wxControlWithItemsBase::InitCommandEventWithItems(wxCommandEvent
& event
, int n
)
262 InitCommandEvent(event
);
264 if ( n
!= wxNOT_FOUND
)
266 if ( HasClientObjectData() )
267 event
.SetClientObject(GetClientObject(n
));
268 else if ( HasClientUntypedData() )
269 event
.SetClientData(GetClientData(n
));
273 #endif // wxUSE_CONTROLS