avoid code duplication by using helper functions
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45428 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -7,8 +7,11 @@
|
|||||||
// Licence: wxWindows licence
|
// Licence: wxWindows licence
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifndef __GTKLISTBOXH__
|
#ifndef _WX_GTK_LISTBOX_H_
|
||||||
#define __GTKLISTBOXH__
|
#define _WX_GTK_LISTBOX_H_
|
||||||
|
|
||||||
|
struct _GtkTreeEntry;
|
||||||
|
struct _GtkTreeIter;
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
// wxListBox
|
// wxListBox
|
||||||
@@ -94,7 +97,7 @@ public:
|
|||||||
|
|
||||||
bool m_blockEvent;
|
bool m_blockEvent;
|
||||||
|
|
||||||
struct _GtkTreeEntry* GtkGetEntry(int pos) const;
|
struct _GtkTreeEntry* GtkGetEntry(unsigned pos) const;
|
||||||
void GtkInsertItems(const wxArrayString& items,
|
void GtkInsertItems(const wxArrayString& items,
|
||||||
void** clientData, unsigned int pos);
|
void** clientData, unsigned int pos);
|
||||||
void GtkDeselectAll();
|
void GtkDeselectAll();
|
||||||
@@ -116,10 +119,19 @@ protected:
|
|||||||
virtual wxClientData* DoGetItemClientObject(unsigned int n) const;
|
virtual wxClientData* DoGetItemClientObject(unsigned int n) const;
|
||||||
virtual int DoListHitTest(const wxPoint& point) const;
|
virtual int DoListHitTest(const wxPoint& point) const;
|
||||||
|
|
||||||
|
// get the iterator for the given index, returns false if invalid
|
||||||
|
bool GtkGetIteratorFor(unsigned pos, _GtkTreeIter *iter) const;
|
||||||
|
|
||||||
|
// get the index for the given iterator, return wxNOT_FOUND on failure
|
||||||
|
int GtkGetIndexFor(_GtkTreeIter& iter) const;
|
||||||
|
|
||||||
|
// set the specified item
|
||||||
|
void GtkSetItem(_GtkTreeIter& iter, const _GtkTreeEntry *entry);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Init(); //common construction
|
void Init(); //common construction
|
||||||
|
|
||||||
DECLARE_DYNAMIC_CLASS(wxListBox)
|
DECLARE_DYNAMIC_CLASS(wxListBox)
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // __GTKLISTBOXH__
|
#endif // _WX_GTK_LISTBOX_H_
|
||||||
|
@@ -497,15 +497,8 @@ void wxListBox::GtkInsertItems(const wxArrayString& items,
|
|||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
if (pos != nCurCount)
|
if (pos != nCurCount)
|
||||||
{
|
{
|
||||||
gboolean res = gtk_tree_model_iter_nth_child(
|
wxCHECK_RET( GtkGetIteratorFor(pos, &iter),
|
||||||
GTK_TREE_MODEL(m_liststore),
|
wxT("internal wxListBox error in insertion") );
|
||||||
&iter, NULL, //NULL = parent = get first
|
|
||||||
(int)pos );
|
|
||||||
if(!res)
|
|
||||||
{
|
|
||||||
wxLogSysError(wxT("internal wxListBox error in insertion"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
pIter = &iter;
|
pIter = &iter;
|
||||||
}
|
}
|
||||||
@@ -526,19 +519,9 @@ void wxListBox::GtkInsertItems(const wxArrayString& items,
|
|||||||
GtkTreeIter itercur;
|
GtkTreeIter itercur;
|
||||||
gtk_list_store_insert_before(m_liststore, &itercur, pIter);
|
gtk_list_store_insert_before(m_liststore, &itercur, pIter);
|
||||||
|
|
||||||
#if wxUSE_CHECKLISTBOX
|
GtkSetItem(itercur, entry);
|
||||||
if (m_hasCheckBoxes)
|
|
||||||
{
|
|
||||||
gtk_list_store_set(m_liststore, &itercur,
|
|
||||||
0, FALSE, //FALSE == not toggled
|
|
||||||
1, entry, -1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
gtk_list_store_set(m_liststore, &itercur,
|
|
||||||
0, entry, -1);
|
|
||||||
|
|
||||||
g_object_unref (entry); //liststore always refs :)
|
g_object_unref (entry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -564,37 +547,11 @@ int wxListBox::DoAppend( const wxString& item )
|
|||||||
GtkTreeIter itercur;
|
GtkTreeIter itercur;
|
||||||
gtk_list_store_insert_before( m_liststore, &itercur, NULL );
|
gtk_list_store_insert_before( m_liststore, &itercur, NULL );
|
||||||
|
|
||||||
#if wxUSE_CHECKLISTBOX
|
GtkSetItem(itercur, entry);
|
||||||
if (m_hasCheckBoxes)
|
|
||||||
{
|
|
||||||
gtk_list_store_set( m_liststore, &itercur,
|
|
||||||
0, FALSE, //FALSE == not toggled
|
|
||||||
1, entry, -1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
gtk_list_store_set(m_liststore, &itercur,
|
|
||||||
0, entry, -1);
|
|
||||||
|
|
||||||
g_object_unref (entry); //liststore always refs :)
|
g_object_unref (entry);
|
||||||
|
|
||||||
GtkTreePath* path = gtk_tree_model_get_path(
|
return GtkGetIndexFor(itercur);
|
||||||
GTK_TREE_MODEL(m_liststore),
|
|
||||||
&itercur);
|
|
||||||
|
|
||||||
gint* pIntPath = gtk_tree_path_get_indices(path);
|
|
||||||
|
|
||||||
if (pIntPath == NULL)
|
|
||||||
{
|
|
||||||
wxLogSysError(wxT("internal wxListBox error in insertion"));
|
|
||||||
return wxNOT_FOUND;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = pIntPath[0];
|
|
||||||
|
|
||||||
gtk_tree_path_free( path );
|
|
||||||
|
|
||||||
return index;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxListBox::DoSetItems( const wxArrayString& items,
|
void wxListBox::DoSetItems( const wxArrayString& items,
|
||||||
@@ -624,38 +581,51 @@ void wxListBox::Delete(unsigned int n)
|
|||||||
InvalidateBestSize();
|
InvalidateBestSize();
|
||||||
|
|
||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
gboolean res = gtk_tree_model_iter_nth_child(
|
wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
|
||||||
GTK_TREE_MODEL(m_liststore),
|
|
||||||
&iter, NULL, //NULL = parent = get first
|
|
||||||
n
|
|
||||||
);
|
|
||||||
|
|
||||||
wxCHECK_RET( res, wxT("wrong listbox index") );
|
// this returns false if iter is invalid (e.g. deleting item at end) but
|
||||||
|
// since we don't use iter, we ignore the return value
|
||||||
//this returns false if iter is invalid (i.e. deleting item
|
|
||||||
//at end) but since we don't use iter, well... :)
|
|
||||||
gtk_list_store_remove(m_liststore, &iter);
|
gtk_list_store_remove(m_liststore, &iter);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// get GtkTreeEntry from position (note: you need to g_unref it if valid)
|
// helper functions for working with iterators
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
|
bool wxListBox::GtkGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
|
||||||
|
{
|
||||||
|
if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
|
||||||
|
iter, NULL, pos) )
|
||||||
|
{
|
||||||
|
wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int wxListBox::GtkGetIndexFor(GtkTreeIter& iter) const
|
||||||
|
{
|
||||||
|
GtkTreePath *path =
|
||||||
|
gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
|
||||||
|
|
||||||
|
gint* pIntPath = gtk_tree_path_get_indices(path);
|
||||||
|
|
||||||
|
wxCHECK_MSG( pIntPath, wxNOT_FOUND, _T("failed to get iterator path") );
|
||||||
|
|
||||||
|
int idx = pIntPath[0];
|
||||||
|
|
||||||
|
gtk_tree_path_free( path );
|
||||||
|
|
||||||
|
return idx;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get GtkTreeEntry from position (note: you need to g_unref it if valid)
|
||||||
|
GtkTreeEntry *wxListBox::GtkGetEntry(unsigned n) const
|
||||||
{
|
{
|
||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
gboolean res = gtk_tree_model_iter_nth_child(
|
if ( !GtkGetIteratorFor(n, &iter) )
|
||||||
GTK_TREE_MODEL(m_liststore),
|
|
||||||
&iter, NULL, //NULL = parent = get first
|
|
||||||
n );
|
|
||||||
|
|
||||||
if (!res)
|
|
||||||
{
|
|
||||||
wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
|
|
||||||
wxT("Passed in value was:[%i] List size:[%u]"),
|
|
||||||
n, wxListBox::GetCount() );
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
GtkTreeEntry* entry = NULL;
|
GtkTreeEntry* entry = NULL;
|
||||||
@@ -665,6 +635,23 @@ struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
|
|||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wxListBox::GtkSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
|
||||||
|
{
|
||||||
|
#if wxUSE_CHECKLISTBOX
|
||||||
|
if ( m_hasCheckBoxes )
|
||||||
|
{
|
||||||
|
gtk_list_store_set(m_liststore, &iter,
|
||||||
|
0, FALSE, // FALSE == not toggled
|
||||||
|
1, entry,
|
||||||
|
-1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif // wxUSE_CHECKLISTBOX
|
||||||
|
{
|
||||||
|
gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// client data
|
// client data
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -722,23 +709,11 @@ void wxListBox::SetString(unsigned int n, const wxString& label)
|
|||||||
|
|
||||||
// and update the model which will refresh the tree too
|
// and update the model which will refresh the tree too
|
||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
|
wxCHECK_RET( GtkGetIteratorFor(n, &iter), _T("failed to get iterator") );
|
||||||
&iter, NULL, n) )
|
|
||||||
{
|
|
||||||
wxFAIL_MSG( wxT("failed to get iterator") );
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if wxUSE_CHECKLISTBOX
|
// FIXME: this resets the checked status of a wxCheckListBox item
|
||||||
if (m_hasCheckBoxes)
|
|
||||||
{
|
GtkSetItem(iter, entry);
|
||||||
gtk_list_store_set(m_liststore, &iter,
|
|
||||||
0, FALSE, //FALSE == not toggled
|
|
||||||
1, entry, -1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
#endif
|
|
||||||
gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxString wxListBox::GetString(unsigned int n) const
|
wxString wxListBox::GetString(unsigned int n) const
|
||||||
@@ -796,14 +771,7 @@ int wxListBox::GetSelection() const
|
|||||||
if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
|
if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
|
||||||
return wxNOT_FOUND;
|
return wxNOT_FOUND;
|
||||||
|
|
||||||
GtkTreePath* path =
|
return GtkGetIndexFor(iter);
|
||||||
gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
|
|
||||||
|
|
||||||
int sel = gtk_tree_path_get_indices(path)[0];
|
|
||||||
|
|
||||||
gtk_tree_path_free(path);
|
|
||||||
|
|
||||||
return sel;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int wxListBox::GetSelections( wxArrayInt& aSelections ) const
|
int wxListBox::GetSelections( wxArrayInt& aSelections ) const
|
||||||
@@ -837,12 +805,7 @@ bool wxListBox::IsSelected( int n ) const
|
|||||||
GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
|
GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
|
||||||
|
|
||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
gboolean res = gtk_tree_model_iter_nth_child(
|
wxCHECK_MSG( GtkGetIteratorFor(n, &iter), false, wxT("Invalid index") );
|
||||||
GTK_TREE_MODEL(m_liststore),
|
|
||||||
&iter, NULL, //NULL = parent = get first
|
|
||||||
n );
|
|
||||||
|
|
||||||
wxCHECK_MSG( res, false, wxT("Invalid index") );
|
|
||||||
|
|
||||||
return gtk_tree_selection_iter_is_selected(selection, &iter);
|
return gtk_tree_selection_iter_is_selected(selection, &iter);
|
||||||
}
|
}
|
||||||
@@ -883,12 +846,7 @@ void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
|
|||||||
GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
|
GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
|
||||||
|
|
||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
gboolean res = gtk_tree_model_iter_nth_child(
|
wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
|
||||||
GTK_TREE_MODEL(m_liststore),
|
|
||||||
&iter, NULL, //NULL = parent = get first
|
|
||||||
n
|
|
||||||
);
|
|
||||||
wxCHECK_RET( res, wxT("Invalid index") );
|
|
||||||
|
|
||||||
m_blockEvent = blockEvent;
|
m_blockEvent = blockEvent;
|
||||||
|
|
||||||
@@ -910,12 +868,8 @@ void wxListBox::DoSetFirstItem( int n )
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
GtkTreeIter iter;
|
GtkTreeIter iter;
|
||||||
gtk_tree_model_iter_nth_child(
|
if ( !GtkGetIteratorFor(n, &iter) )
|
||||||
GTK_TREE_MODEL(m_liststore),
|
return;
|
||||||
&iter,
|
|
||||||
NULL, //NULL = parent = get first
|
|
||||||
n
|
|
||||||
);
|
|
||||||
|
|
||||||
GtkTreePath* path = gtk_tree_model_get_path(
|
GtkTreePath* path = gtk_tree_model_get_path(
|
||||||
GTK_TREE_MODEL(m_liststore), &iter);
|
GTK_TREE_MODEL(m_liststore), &iter);
|
||||||
|
Reference in New Issue
Block a user