]>
git.saurik.com Git - wxWidgets.git/blob - samples/widgets/itemcontainer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: itemcontainer.cpp
4 // Purpose: Part of the widgets sample showing wxComboBox
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
17 // for compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
24 // for all others, include the necessary headers
31 #include "wx/ctrlsub.h"
32 #include "itemcontainer.h"
35 // Help track client data objects in wxItemContainer instances.
36 class TrackedClientData
: public wxClientData
40 TrackedClientData(ItemContainerWidgetsPage
* tracker
, int value
)
44 m_tracker
->StartTrackingData();
47 virtual ~TrackedClientData()
49 m_tracker
->StopTrackingData();
58 ItemContainerWidgetsPage
*m_tracker
;
61 wxDECLARE_NO_COPY_CLASS(TrackedClientData
);
64 // ============================================================================
66 // ============================================================================
68 ItemContainerWidgetsPage::ItemContainerWidgetsPage(WidgetsBookCtrl
*book
,
69 wxImageList
*image_list
,
70 const char *const icon
[])
71 : WidgetsPage(book
, image_list
, icon
), m_trackedDataObjects(0)
73 m_items
.Add(wxT("This"));
74 m_items
.Add(wxT("is"));
75 m_items
.Add(wxT("a"));
76 m_items
.Add(wxT("List"));
77 m_items
.Add(wxT("of"));
78 m_items
.Add(wxT("strings"));
79 m_itemsSorted
= m_items
;
82 ItemContainerWidgetsPage::~ItemContainerWidgetsPage()
86 wxClientData
* ItemContainerWidgetsPage::CreateClientData(int value
)
88 return new TrackedClientData(this, value
);
91 void ItemContainerWidgetsPage::StartTrackingData()
93 ++m_trackedDataObjects
;
96 void ItemContainerWidgetsPage::StopTrackingData()
98 --m_trackedDataObjects
;
101 bool ItemContainerWidgetsPage::VerifyAllClientDataDestroyed()
103 if ( m_trackedDataObjects
)
105 wxString message
= wxT("Bug in managing wxClientData: ");
106 if ( m_trackedDataObjects
> 0 )
107 message
<< m_trackedDataObjects
<< wxT(" lost objects");
109 message
<< (-m_trackedDataObjects
) << wxT(" extra deletes");
117 void ItemContainerWidgetsPage::StartTest(const wxString
& label
)
119 m_container
->Clear();
120 wxLogMessage(wxT("Test - %s:"), label
.c_str());
123 void ItemContainerWidgetsPage::EndTest(const wxArrayString
& items
)
125 const unsigned count
= m_container
->GetCount();
127 bool ok
= count
== items
.GetCount();
130 wxFAIL_MSG(wxT("Item count does not match."));
134 for ( unsigned i
= 0; i
< count
; ++i
)
136 wxString str
= m_container
->GetString(i
);
137 if ( str
!= items
[i
] )
139 wxFAIL_MSG(wxString::Format(
140 wxT("Wrong string \"%s\" at position %d (expected \"%s\")"),
141 str
.c_str(), i
, items
[i
].c_str()));
146 if ( m_container
->HasClientUntypedData() )
148 void *data
= m_container
->GetClientData(i
);
149 if ( data
&& !VerifyClientData((wxUIntPtr
)data
, str
) )
155 else if ( m_container
->HasClientObjectData() )
157 TrackedClientData
* obj
= (TrackedClientData
*)m_container
->GetClientObject(i
);
158 if ( obj
&& !VerifyClientData(obj
->GetValue(), str
) )
168 wxLogMessage(DumpContainerData(items
));
172 m_container
->Clear();
173 ok
&= VerifyAllClientDataDestroyed();
175 wxLogMessage(wxT("...%s"), ok
? wxT("passed") : wxT("failed"));
179 ItemContainerWidgetsPage::DumpContainerData(const wxArrayString
& expected
) const
182 str
<< wxT("Current content:\n");
185 for ( i
= 0; i
< m_container
->GetCount(); ++i
)
187 str
<< wxT(" - ") << m_container
->GetString(i
) << wxT(" [");
188 if ( m_container
->HasClientObjectData() )
191 obj
= (TrackedClientData
*)m_container
->GetClientObject(i
);
193 str
<< obj
->GetValue();
195 else if ( m_container
->HasClientUntypedData() )
197 void *data
= m_container
->GetClientData(i
);
199 str
<< (wxUIntPtr
)data
;
204 str
<< wxT("Expected content:\n");
205 for ( i
= 0; i
< expected
.GetCount(); ++i
)
207 const wxString
& item
= expected
[i
];
208 str
<< wxT(" - ") << item
<< wxT("[");
209 for( unsigned j
= 0; j
< m_items
.GetCount(); ++j
)
211 if ( m_items
[j
] == item
)
220 bool ItemContainerWidgetsPage::VerifyClientData(wxUIntPtr i
, const wxString
& str
)
222 if ( i
> m_items
.GetCount() || m_items
[i
] != str
)
224 wxLogMessage(wxT("Client data for '%s' does not match."), str
.c_str());
233 ItemContainerWidgetsPage::MakeArray(const wxSortedArrayString
& sorted
)
237 const size_t count
= sorted
.size();
239 for ( size_t n
= 0; n
< count
; n
++ )
240 a
.push_back(sorted
[n
]);
245 void ItemContainerWidgetsPage::OnButtonTestItemContainer(wxCommandEvent
&)
247 m_container
= GetContainer();
248 wxASSERT_MSG(m_container
, wxT("Widget must have a test widget"));
250 wxLogMessage(wxT("wxItemContainer test for %s, %s:"),
251 GetWidget()->GetClassInfo()->GetClassName(),
252 (m_container
->IsSorted() ? "Sorted" : "Unsorted"));
255 expected_result
= m_container
->IsSorted() ? MakeArray(m_itemsSorted
)
258 StartTest(wxT("Append one item"));
259 wxString item
= m_items
[0];
260 m_container
->Append(item
);
261 EndTest(wxArrayString(1, &item
));
263 StartTest(wxT("Append some items"));
264 m_container
->Append(m_items
);
265 EndTest(expected_result
);
267 StartTest(wxT("Append some items with data objects"));
268 wxClientData
**objects
= new wxClientData
*[m_items
.GetCount()];
270 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
271 objects
[i
] = CreateClientData(i
);
272 m_container
->Append(m_items
, objects
);
273 EndTest(expected_result
);
276 StartTest(wxT("Append some items with data"));
277 void **data
= new void *[m_items
.GetCount()];
278 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
279 data
[i
] = wxUIntToPtr(i
);
280 m_container
->Append(m_items
, data
);
281 EndTest(expected_result
);
284 StartTest(wxT("Append some items with data, one by one"));
285 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
286 m_container
->Append(m_items
[i
], wxUIntToPtr(i
));
287 EndTest(expected_result
);
289 StartTest(wxT("Append some items with data objects, one by one"));
290 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
291 m_container
->Append(m_items
[i
], CreateClientData(i
));
292 EndTest(expected_result
);
294 if ( !m_container
->IsSorted() )
296 StartTest(wxT("Insert in reverse order with data, one by one"));
297 for ( unsigned i
= m_items
.GetCount(); i
; --i
)
298 m_container
->Insert(m_items
[i
- 1], 0, wxUIntToPtr(i
- 1));
299 EndTest(expected_result
);