]>
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
7 // License: wxWindows license
8 /////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
18 // for compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
25 // for all others, include the necessary headers
32 #include "wx/ctrlsub.h"
33 #include "itemcontainer.h"
36 // Help track client data objects in wxItemContainer instances.
37 class TrackedClientData
: public wxClientData
41 TrackedClientData(ItemContainerWidgetsPage
* tracker
, int value
)
45 m_tracker
->StartTrackingData();
48 virtual ~TrackedClientData()
50 m_tracker
->StopTrackingData();
59 ItemContainerWidgetsPage
*m_tracker
;
62 wxDECLARE_NO_COPY_CLASS(TrackedClientData
);
65 // ============================================================================
67 // ============================================================================
69 ItemContainerWidgetsPage::ItemContainerWidgetsPage(WidgetsBookCtrl
*book
,
70 wxImageList
*image_list
,
71 const char *const icon
[])
72 : WidgetsPage(book
, image_list
, icon
), m_trackedDataObjects(0)
74 m_items
.Add(wxT("This"));
75 m_items
.Add(wxT("is"));
76 m_items
.Add(wxT("a"));
77 m_items
.Add(wxT("List"));
78 m_items
.Add(wxT("of"));
79 m_items
.Add(wxT("strings"));
80 m_itemsSorted
= m_items
;
83 ItemContainerWidgetsPage::~ItemContainerWidgetsPage()
87 wxClientData
* ItemContainerWidgetsPage::CreateClientData(int value
)
89 return new TrackedClientData(this, value
);
92 void ItemContainerWidgetsPage::StartTrackingData()
94 ++m_trackedDataObjects
;
97 void ItemContainerWidgetsPage::StopTrackingData()
99 --m_trackedDataObjects
;
102 bool ItemContainerWidgetsPage::VerifyAllClientDataDestroyed()
104 if ( m_trackedDataObjects
)
106 wxString message
= wxT("Bug in managing wxClientData: ");
107 if ( m_trackedDataObjects
> 0 )
108 message
<< m_trackedDataObjects
<< wxT(" lost objects");
110 message
<< (-m_trackedDataObjects
) << wxT(" extra deletes");
118 void ItemContainerWidgetsPage::StartTest(const wxString
& label
)
120 m_container
->Clear();
121 wxLogMessage(wxT("Test - %s:"), label
.c_str());
124 void ItemContainerWidgetsPage::EndTest(const wxArrayString
& items
)
126 const unsigned count
= m_container
->GetCount();
128 bool ok
= count
== items
.GetCount();
131 wxFAIL_MSG(wxT("Item count does not match."));
135 for ( unsigned i
= 0; i
< count
; ++i
)
137 wxString str
= m_container
->GetString(i
);
138 if ( str
!= items
[i
] )
140 wxFAIL_MSG(wxString::Format(
141 wxT("Wrong string \"%s\" at position %d (expected \"%s\")"),
142 str
.c_str(), i
, items
[i
].c_str()));
147 if ( m_container
->HasClientUntypedData() )
149 void *data
= m_container
->GetClientData(i
);
150 if ( data
&& !VerifyClientData((wxUIntPtr
)data
, str
) )
156 else if ( m_container
->HasClientObjectData() )
158 TrackedClientData
* obj
= (TrackedClientData
*)m_container
->GetClientObject(i
);
159 if ( obj
&& !VerifyClientData(obj
->GetValue(), str
) )
169 wxLogMessage(DumpContainerData(items
));
173 m_container
->Clear();
174 ok
&= VerifyAllClientDataDestroyed();
176 wxLogMessage(wxT("...%s"), ok
? wxT("passed") : wxT("failed"));
180 ItemContainerWidgetsPage::DumpContainerData(const wxArrayString
& expected
) const
183 str
<< wxT("Current content:\n");
186 for ( i
= 0; i
< m_container
->GetCount(); ++i
)
188 str
<< wxT(" - ") << m_container
->GetString(i
) << wxT(" [");
189 if ( m_container
->HasClientObjectData() )
192 obj
= (TrackedClientData
*)m_container
->GetClientObject(i
);
194 str
<< obj
->GetValue();
196 else if ( m_container
->HasClientUntypedData() )
198 void *data
= m_container
->GetClientData(i
);
200 str
<< (wxUIntPtr
)data
;
205 str
<< wxT("Expected content:\n");
206 for ( i
= 0; i
< expected
.GetCount(); ++i
)
208 const wxString
& item
= expected
[i
];
209 str
<< wxT(" - ") << item
<< wxT("[");
210 for( unsigned j
= 0; j
< m_items
.GetCount(); ++j
)
212 if ( m_items
[j
] == item
)
221 bool ItemContainerWidgetsPage::VerifyClientData(wxUIntPtr i
, const wxString
& str
)
223 if ( i
> m_items
.GetCount() || m_items
[i
] != str
)
225 wxLogMessage(wxT("Client data for '%s' does not match."), str
.c_str());
234 ItemContainerWidgetsPage::MakeArray(const wxSortedArrayString
& sorted
)
238 const size_t count
= sorted
.size();
240 for ( size_t n
= 0; n
< count
; n
++ )
241 a
.push_back(sorted
[n
]);
246 void ItemContainerWidgetsPage::OnButtonTestItemContainer(wxCommandEvent
&)
248 m_container
= GetContainer();
249 wxASSERT_MSG(m_container
, wxT("Widget must have a test widget"));
251 wxLogMessage(wxT("wxItemContainer test for %s, %s:"),
252 GetWidget()->GetClassInfo()->GetClassName(),
253 (m_container
->IsSorted() ? "Sorted" : "Unsorted"));
256 expected_result
= m_container
->IsSorted() ? MakeArray(m_itemsSorted
)
259 StartTest(wxT("Append one item"));
260 wxString item
= m_items
[0];
261 m_container
->Append(item
);
262 EndTest(wxArrayString(1, &item
));
264 StartTest(wxT("Append some items"));
265 m_container
->Append(m_items
);
266 EndTest(expected_result
);
268 StartTest(wxT("Append some items with data objects"));
269 wxClientData
**objects
= new wxClientData
*[m_items
.GetCount()];
271 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
272 objects
[i
] = CreateClientData(i
);
273 m_container
->Append(m_items
, objects
);
274 EndTest(expected_result
);
277 StartTest(wxT("Append some items with data"));
278 void **data
= new void *[m_items
.GetCount()];
279 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
280 data
[i
] = wxUIntToPtr(i
);
281 m_container
->Append(m_items
, data
);
282 EndTest(expected_result
);
285 StartTest(wxT("Append some items with data, one by one"));
286 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
287 m_container
->Append(m_items
[i
], wxUIntToPtr(i
));
288 EndTest(expected_result
);
290 StartTest(wxT("Append some items with data objects, one by one"));
291 for ( i
= 0; i
< m_items
.GetCount(); ++i
)
292 m_container
->Append(m_items
[i
], CreateClientData(i
));
293 EndTest(expected_result
);
295 if ( !m_container
->IsSorted() )
297 StartTest(wxT("Insert in reverse order with data, one by one"));
298 for ( unsigned i
= m_items
.GetCount(); i
; --i
)
299 m_container
->Insert(m_items
[i
- 1], 0, wxUIntToPtr(i
- 1));
300 EndTest(expected_result
);