+ gtk_tree_view_column_set_sort_order( column, GTK_SORT_DESCENDING );
+
+ gtk_tree_view_column_set_sort_indicator( column, TRUE );
+}
+
+bool wxDataViewColumn::IsSortOrderAscending() const
+{
+ GtkTreeViewColumn *column = GTK_TREE_VIEW_COLUMN(m_column);
+
+ return (gtk_tree_view_column_get_sort_order( column ) != GTK_SORT_DESCENDING);
+}
+
+void wxDataViewColumn::SetMinWidth( int width )
+{
+ gtk_tree_view_column_set_min_width( GTK_TREE_VIEW_COLUMN(m_column), width );
+}
+
+int wxDataViewColumn::GetMinWidth() const
+{
+ return gtk_tree_view_column_get_min_width( GTK_TREE_VIEW_COLUMN(m_column) );
+}
+
+int wxDataViewColumn::GetWidth() const
+{
+ return gtk_tree_view_column_get_width( GTK_TREE_VIEW_COLUMN(m_column) );
+}
+
+void wxDataViewColumn::SetWidth( int width )
+{
+ if ( width == wxCOL_WIDTH_AUTOSIZE )
+ {
+ // NB: this disables user resizing
+ gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_AUTOSIZE );
+ }
+ else
+ {
+ if ( width == wxCOL_WIDTH_DEFAULT )
+ {
+ // TODO find a better calculation
+ width = wxDVC_DEFAULT_WIDTH;
+ }
+
+ gtk_tree_view_column_set_sizing( GTK_TREE_VIEW_COLUMN(m_column), GTK_TREE_VIEW_COLUMN_FIXED );
+ gtk_tree_view_column_set_fixed_width( GTK_TREE_VIEW_COLUMN(m_column), width );
+ }
+}
+
+void wxDataViewColumn::SetReorderable( bool reorderable )
+{
+ gtk_tree_view_column_set_reorderable( GTK_TREE_VIEW_COLUMN(m_column), reorderable );
+}
+
+bool wxDataViewColumn::IsReorderable() const
+{
+ return gtk_tree_view_column_get_reorderable( GTK_TREE_VIEW_COLUMN(m_column) ) != 0;
+}
+
+//-----------------------------------------------------------------------------
+// wxGtkTreeModelNode
+//-----------------------------------------------------------------------------
+
+#if 0
+class wxGtkTreeModelChildWithPos
+{
+public:
+ unsigned int pos;
+ void *id;
+};
+
+static
+int wxGtkTreeModelChildWithPosCmp( const void* data1, const void* data2, const void* user_data )
+{
+ const wxGtkTreeModelChildWithPos* child1 = (const wxGtkTreeModelChildWithPos*) data1;
+ const wxGtkTreeModelChildWithPos* child2 = (const wxGtkTreeModelChildWithPos*) data2;
+ const wxDataViewCtrlInternal *internal = (const wxDataViewCtrlInternal *) user_data;
+ int ret = internal->GetDataViewModel()->Compare( child1->id, child2->id,
+ internal->GetSortColumn(), (internal->GetSortOrder() == GTK_SORT_DESCENDING) );
+
+ return ret;
+}
+#else
+static
+int LINKAGEMODE wxGtkTreeModelChildPtrCmp( void*** data1, void*** data2 )
+{
+ return gs_internal->GetDataViewModel()->Compare( wxDataViewItem(**data1), wxDataViewItem(**data2),
+ gs_internal->GetSortColumn(), (gs_internal->GetSortOrder() == GTK_SORT_ASCENDING) );
+}
+
+WX_DEFINE_ARRAY_PTR( void**, wxGtkTreeModelChildrenPtr );
+#endif
+
+void wxGtkTreeModelNode::Resort()
+{
+ size_t child_count = GetChildCount();
+ if (child_count == 0)
+ return;
+
+ size_t node_count = GetNodesCount();
+
+ if (child_count == 1)
+ {
+ if (node_count == 1)
+ {
+ wxGtkTreeModelNode *node = m_nodes.Item( 0 );
+ node->Resort();
+ }
+ return;
+ }
+
+ gint *new_order = new gint[child_count];
+
+#if 1
+ // m_children has the original *void
+ // ptrs points to these
+ wxGtkTreeModelChildrenPtr ptrs;
+ size_t i;
+ for (i = 0; i < child_count; i++)
+ ptrs.Add( &(m_children[i]) );
+ // Sort the ptrs
+ gs_internal = m_internal;
+ ptrs.Sort( &wxGtkTreeModelChildPtrCmp );
+
+ wxGtkTreeModelChildren temp;
+ void** base_ptr = &(m_children[0]);
+ // Transfer positions to new_order array and
+ // IDs to temp
+ for (i = 0; i < child_count; i++)
+ {
+ new_order[i] = ptrs[i] - base_ptr;
+ temp.Add( *ptrs[i] );
+ }
+
+ // Transfer IDs back to m_children
+ m_children.Clear();
+ WX_APPEND_ARRAY( temp, m_children );
+#endif
+#if 0
+ // Too slow
+
+ // Build up array with IDs and original positions
+ wxGtkTreeModelChildWithPos* temp = new wxGtkTreeModelChildWithPos[child_count];
+ size_t i;
+ for (i = 0; i < child_count; i++)
+ {
+ temp[i].pos = i;
+ temp[i].id = m_children[i];
+ }
+ // Sort array keeping original positions
+ wxQsort( temp, child_count, sizeof(wxGtkTreeModelChildWithPos),
+ &wxGtkTreeModelChildWithPosCmp, m_internal );
+ // Transfer positions to new_order array and
+ // IDs to m_children
+ m_children.Clear();
+ for (i = 0; i < child_count; i++)
+ {
+ new_order[i] = temp[i].pos;
+ m_children.Add( temp[i].id );
+ }
+ // Delete array
+ delete [] temp;
+#endif
+
+#if 0
+ // Too slow
+
+ wxGtkTreeModelChildren temp;
+ WX_APPEND_ARRAY( temp, m_children );
+
+ gs_internal = m_internal;
+ m_children.Sort( &wxGtkTreeModelChildCmp );
+
+ unsigned int pos;
+ for (pos = 0; pos < child_count; pos++)
+ {
+ void *id = m_children.Item( pos );
+ int old_pos = temp.Index( id );
+ new_order[pos] = old_pos;
+ }
+#endif
+
+ GtkTreeModel *gtk_tree_model = GTK_TREE_MODEL( m_internal->GetGtkModel() );
+
+ GtkTreeIter iter;
+ iter.user_data = GetItem().GetID();
+ iter.stamp = m_internal->GetGtkModel()->stamp;
+
+ gtk_tree_model_rows_reordered( gtk_tree_model,
+ wxGtkTreePath(m_internal->get_path(&iter)), &iter, new_order );
+
+ delete [] new_order;
+
+ unsigned int pos;
+ for (pos = 0; pos < node_count; pos++)
+ {
+ wxGtkTreeModelNode *node = m_nodes.Item( pos );
+ node->Resort();
+ }
+}
+
+//-----------------------------------------------------------------------------
+// wxDataViewCtrlInternal
+//-----------------------------------------------------------------------------
+
+wxDataViewCtrlInternal::wxDataViewCtrlInternal( wxDataViewCtrl *owner, wxDataViewModel *wx_model )
+{
+ m_owner = owner;
+ m_wx_model = wx_model;
+
+ m_root = NULL;
+ m_sort_order = GTK_SORT_ASCENDING;
+ m_sort_column = -1;
+ m_dataview_sort_column = NULL;
+
+ m_dragDataObject = NULL;
+ m_dropDataObject = NULL;
+
+ m_dirty = false;
+
+ m_gtk_model = wxgtk_tree_model_new();
+ m_gtk_model->internal = this;
+
+ m_notifier = new wxGtkDataViewModelNotifier( wx_model, this );
+
+ wx_model->AddNotifier( m_notifier );
+
+ // g_object_unref( gtk_model ); ???
+
+ if (!m_wx_model->IsVirtualListModel())
+ InitTree();
+
+ gtk_tree_view_set_model( GTK_TREE_VIEW(m_owner->GtkGetTreeView()), GTK_TREE_MODEL(m_gtk_model) );
+}
+
+wxDataViewCtrlInternal::~wxDataViewCtrlInternal()
+{
+ m_wx_model->RemoveNotifier( m_notifier );
+
+ // remove the model from the GtkTreeView before it gets destroyed
+ gtk_tree_view_set_model( GTK_TREE_VIEW( m_owner->GtkGetTreeView() ), NULL );
+
+ g_object_unref( m_gtk_model );
+
+ delete m_dragDataObject;
+ delete m_dropDataObject;
+}
+
+void wxDataViewCtrlInternal::ScheduleRefresh()
+{
+ m_dirty = true;
+}
+
+void wxDataViewCtrlInternal::OnInternalIdle()
+{
+ if (m_dirty)
+ {
+ GtkWidget *widget = m_owner->GtkGetTreeView();
+ gtk_widget_queue_draw( widget );
+ m_dirty = false;
+ }
+}
+
+void wxDataViewCtrlInternal::InitTree()
+{
+ wxDataViewItem item;
+ m_root = new wxGtkTreeModelNode( NULL, item, this );
+
+ BuildBranch( m_root );
+}
+
+void wxDataViewCtrlInternal::BuildBranch( wxGtkTreeModelNode *node )
+{
+ if (node->GetChildCount() == 0)
+ {
+ wxDataViewItemArray children;
+ unsigned int count = m_wx_model->GetChildren( node->GetItem(), children );
+
+ unsigned int pos;
+ for (pos = 0; pos < count; pos++)
+ {
+ wxDataViewItem child = children[pos];
+
+ if (m_wx_model->IsContainer( child ))
+ node->AddNode( new wxGtkTreeModelNode( node, child, this ) );
+ else
+ node->AddLeaf( child.GetID() );
+
+ // Don't send any events here
+ }
+ }
+}
+
+// GTK+ dnd iface
+
+bool wxDataViewCtrlInternal::EnableDragSource( const wxDataFormat &format )
+{
+ wxGtkString atom_str( gdk_atom_name( format ) );
+ m_dragSourceTargetEntryTarget = wxCharBuffer( atom_str );
+
+ m_dragSourceTargetEntry.target = m_dragSourceTargetEntryTarget.data();
+ m_dragSourceTargetEntry.flags = 0;
+ m_dragSourceTargetEntry.info = static_cast<guint>(-1);
+
+ gtk_tree_view_enable_model_drag_source( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
+ GDK_BUTTON1_MASK, &m_dragSourceTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
+
+ return true;
+}
+
+bool wxDataViewCtrlInternal::EnableDropTarget( const wxDataFormat &format )
+{
+ wxGtkString atom_str( gdk_atom_name( format ) );
+ m_dropTargetTargetEntryTarget = wxCharBuffer( atom_str );
+
+ m_dropTargetTargetEntry.target = m_dropTargetTargetEntryTarget.data();
+ m_dropTargetTargetEntry.flags = 0;
+ m_dropTargetTargetEntry.info = static_cast<guint>(-1);
+
+ gtk_tree_view_enable_model_drag_dest( GTK_TREE_VIEW(m_owner->GtkGetTreeView() ),
+ &m_dropTargetTargetEntry, 1, (GdkDragAction) GDK_ACTION_COPY );
+
+ return true;
+}
+
+gboolean wxDataViewCtrlInternal::row_draggable( GtkTreeDragSource *WXUNUSED(drag_source),
+ GtkTreePath *path )
+{
+ delete m_dragDataObject;
+ m_dragDataObject = NULL;
+
+ wxDataViewItem item(GetOwner()->GTKPathToItem(path));
+ if ( !item )
+ return FALSE;
+
+ wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, m_owner->GetId() );
+ event.SetEventObject( m_owner );
+ event.SetItem( item );
+ event.SetModel( m_wx_model );
+ gint x, y;
+ gtk_widget_get_pointer(m_owner->GtkGetTreeView(), &x, &y);
+ event.SetPosition(x, y);
+ if (!m_owner->HandleWindowEvent( event ))
+ return FALSE;
+
+ if (!event.IsAllowed())
+ return FALSE;
+
+ wxDataObject *obj = event.GetDataObject();
+ if (!obj)
+ return FALSE;
+
+ m_dragDataObject = obj;
+
+ return TRUE;
+}
+
+gboolean
+wxDataViewCtrlInternal::drag_data_delete(GtkTreeDragSource *WXUNUSED(drag_source),
+ GtkTreePath *WXUNUSED(path))
+{
+ return FALSE;
+}
+
+gboolean wxDataViewCtrlInternal::drag_data_get( GtkTreeDragSource *WXUNUSED(drag_source),
+ GtkTreePath *path, GtkSelectionData *selection_data )
+{
+ wxDataViewItem item(GetOwner()->GTKPathToItem(path));
+ if ( !item )
+ return FALSE;
+
+ GdkAtom target = gtk_selection_data_get_target(selection_data);
+ if (!m_dragDataObject->IsSupported(target))
+ return FALSE;
+
+ size_t size = m_dragDataObject->GetDataSize(target);
+ if (size == 0)
+ return FALSE;
+
+ void *buf = malloc( size );
+
+ gboolean res = FALSE;
+ if (m_dragDataObject->GetDataHere(target, buf))
+ {
+ res = TRUE;
+
+ gtk_selection_data_set(selection_data, target,
+ 8, (const guchar*) buf, size );
+ }
+
+ free( buf );
+
+ return res;
+}
+
+gboolean
+wxDataViewCtrlInternal::drag_data_received(GtkTreeDragDest *WXUNUSED(drag_dest),
+ GtkTreePath *path,
+ GtkSelectionData *selection_data)
+{
+ wxDataViewItem item(GetOwner()->GTKPathToItem(path));
+ if ( !item )
+ return FALSE;
+
+ wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_DROP, m_owner->GetId() );
+ event.SetEventObject( m_owner );
+ event.SetItem( item );
+ event.SetModel( m_wx_model );
+ event.SetDataFormat(gtk_selection_data_get_target(selection_data));
+ event.SetDataSize(gtk_selection_data_get_length(selection_data));
+ event.SetDataBuffer(const_cast<guchar*>(gtk_selection_data_get_data(selection_data)));
+ if (!m_owner->HandleWindowEvent( event ))
+ return FALSE;
+
+ if (!event.IsAllowed())
+ return FALSE;
+
+ return TRUE;
+}
+
+gboolean
+wxDataViewCtrlInternal::row_drop_possible(GtkTreeDragDest *WXUNUSED(drag_dest),
+ GtkTreePath *path,
+ GtkSelectionData *selection_data)
+{
+ wxDataViewItem item(GetOwner()->GTKPathToItem(path));
+ if ( !item )
+ return FALSE;
+
+ wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, m_owner->GetId() );
+ event.SetEventObject( m_owner );
+ event.SetItem( item );
+ event.SetModel( m_wx_model );
+ event.SetDataFormat(gtk_selection_data_get_target(selection_data));
+ event.SetDataSize(gtk_selection_data_get_length(selection_data));
+ if (!m_owner->HandleWindowEvent( event ))
+ return FALSE;
+
+ if (!event.IsAllowed())
+ return FALSE;
+
+ return TRUE;
+}
+
+// notifications from wxDataViewModel
+
+bool wxDataViewCtrlInternal::Cleared()
+{
+ if (m_root)
+ {
+ delete m_root;
+ m_root = NULL;
+ }
+
+ InitTree();
+
+ ScheduleRefresh();
+
+ return true;
+}
+
+void wxDataViewCtrlInternal::Resort()
+{
+ if (!m_wx_model->IsVirtualListModel())
+ m_root->Resort();
+
+ ScheduleRefresh();
+}
+
+bool wxDataViewCtrlInternal::ItemAdded( const wxDataViewItem &parent, const wxDataViewItem &item )
+{
+ if (!m_wx_model->IsVirtualListModel())
+ {
+ wxGtkTreeModelNode *parent_node = FindNode( parent );
+ wxCHECK_MSG(parent_node, false,
+ "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
+
+ wxDataViewItemArray modelSiblings;
+ m_wx_model->GetChildren(parent, modelSiblings);
+ const int modelSiblingsSize = modelSiblings.size();
+
+ int posInModel = modelSiblings.Index(item, /*fromEnd=*/true);
+ wxCHECK_MSG( posInModel != wxNOT_FOUND, false, "adding non-existent item?" );
+
+ const wxGtkTreeModelChildren& nodeSiblings = parent_node->GetChildren();
+ const int nodeSiblingsSize = nodeSiblings.size();
+
+ int nodePos = 0;
+
+ if ( posInModel == modelSiblingsSize - 1 )
+ {
+ nodePos = nodeSiblingsSize;
+ }
+ else if ( modelSiblingsSize == nodeSiblingsSize + 1 )
+ {
+ // This is the simple case when our node tree already matches the
+ // model and only this one item is missing.
+ nodePos = posInModel;
+ }
+ else
+ {
+ // It's possible that a larger discrepancy between the model and
+ // our realization exists. This can happen e.g. when adding a bunch
+ // of items to the model and then calling ItemsAdded() just once
+ // afterwards. In this case, we must find the right position by
+ // looking at sibling items.
+
+ // append to the end if we won't find a better position:
+ nodePos = nodeSiblingsSize;
+
+ for ( int nextItemPos = posInModel + 1;
+ nextItemPos < modelSiblingsSize;
+ nextItemPos++ )
+ {
+ int nextNodePos = parent_node->FindChildByItem(modelSiblings[nextItemPos]);
+ if ( nextNodePos != wxNOT_FOUND )
+ {
+ nodePos = nextNodePos;
+ break;
+ }
+ }
+ }
+
+ if (m_wx_model->IsContainer( item ))
+ parent_node->InsertNode( new wxGtkTreeModelNode( parent_node, item, this ), nodePos );
+ else
+ parent_node->InsertLeaf( item.GetID(), nodePos );
+ }
+
+ ScheduleRefresh();
+
+ return true;
+}
+
+bool wxDataViewCtrlInternal::ItemDeleted( const wxDataViewItem &parent, const wxDataViewItem &item )
+{
+ if (!m_wx_model->IsVirtualListModel())
+ {
+ wxGtkTreeModelNode *parent_node = FindNode( parent );
+ wxASSERT_MSG(parent_node,
+ "Did you forget a call to ItemAdded()? The parent node is unknown to the wxGtkTreeModel");
+
+ parent_node->DeleteChild( item.GetID() );
+ }
+
+ ScheduleRefresh();
+
+ return true;
+}
+
+bool wxDataViewCtrlInternal::ItemChanged( const wxDataViewItem &item )
+{
+ wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
+ event.SetEventObject( m_owner );
+ event.SetModel( m_owner->GetModel() );
+ event.SetItem( item );
+ m_owner->HandleWindowEvent( event );
+
+ return true;
+}
+
+bool wxDataViewCtrlInternal::ValueChanged( const wxDataViewItem &item, unsigned int view_column )
+{
+ wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, m_owner->GetId() );
+ event.SetEventObject( m_owner );
+ event.SetModel( m_owner->GetModel() );
+ event.SetColumn( view_column );
+ event.SetDataViewColumn( GetOwner()->GetColumn(view_column) );
+ event.SetItem( item );
+ m_owner->HandleWindowEvent( event );
+
+ return true;
+}
+
+// GTK+ model iface
+
+GtkTreeModelFlags wxDataViewCtrlInternal::get_flags()
+{
+ int flags = 0;
+
+ if ( m_wx_model->IsListModel() )
+ flags |= GTK_TREE_MODEL_LIST_ONLY;
+
+ if ( !m_wx_model->IsVirtualListModel() )
+ flags |= GTK_TREE_MODEL_ITERS_PERSIST;
+
+ return GtkTreeModelFlags(flags);
+}
+
+gboolean wxDataViewCtrlInternal::get_iter( GtkTreeIter *iter, GtkTreePath *path )
+{
+
+ if (m_wx_model->IsVirtualListModel())
+ {
+ wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
+
+ unsigned int i = (unsigned int)gtk_tree_path_get_indices (path)[0];
+
+ if (i >= wx_model->GetCount())
+ return FALSE;
+
+ iter->stamp = m_gtk_model->stamp;
+ // user_data is just the index +1
+ iter->user_data = wxUIntToPtr(i+1);
+
+ return TRUE;
+ }
+ else
+ {
+ int depth = gtk_tree_path_get_depth( path );
+
+ wxGtkTreeModelNode *node = m_root;
+
+ int i;
+ for (i = 0; i < depth; i++)
+ {
+ BuildBranch( node );
+
+ gint pos = gtk_tree_path_get_indices (path)[i];
+ if (pos < 0) return FALSE;
+ if ((size_t)pos >= node->GetChildCount()) return FALSE;
+
+ void* id = node->GetChildren().Item( (size_t) pos );
+
+ if (i == depth-1)
+ {
+ iter->stamp = m_gtk_model->stamp;
+ iter->user_data = id;
+ return TRUE;
+ }
+
+ size_t count = node->GetNodes().GetCount();
+ size_t pos2;
+ for (pos2 = 0; pos2 < count; pos2++)
+ {
+ wxGtkTreeModelNode *child_node = node->GetNodes().Item( pos2 );
+ if (child_node->GetItem().GetID() == id)
+ {
+ node = child_node;
+ break;
+ }
+ }
+ }
+ }
+
+ return FALSE;
+}
+
+GtkTreePath *wxDataViewCtrlInternal::get_path( GtkTreeIter *iter )
+{
+ // When this is called from ItemDeleted(), the item is already
+ // deleted in the model.
+
+ GtkTreePath *retval = gtk_tree_path_new ();
+
+ if (m_wx_model->IsVirtualListModel())
+ {
+ // iter is root, add nothing
+ if (!iter->user_data)
+ return retval;
+
+ // user_data is just the index +1
+ int i = ( (wxUIntPtr) iter->user_data ) -1;
+ gtk_tree_path_append_index (retval, i);
+ }
+ else
+ {
+ void *id = iter->user_data;
+
+ wxGtkTreeModelNode *node = FindParentNode( iter );
+ while (node)
+ {
+ int pos = node->GetChildren().Index( id );
+
+ gtk_tree_path_prepend_index( retval, pos );
+
+ id = node->GetItem().GetID();
+ node = node->GetParent();
+ }
+ }
+
+ return retval;
+}
+
+gboolean wxDataViewCtrlInternal::iter_next( GtkTreeIter *iter )
+{
+ if (m_wx_model->IsVirtualListModel())
+ {
+ wxDataViewVirtualListModel *wx_model = (wxDataViewVirtualListModel*) m_wx_model;
+
+ // user_data is just the index +1
+ int n = ( (wxUIntPtr) iter->user_data ) -1;
+
+ if (n == -1)
+ {
+ iter->user_data = NULL;
+ return FALSE;
+ }
+
+ if (n >= (int) wx_model->GetCount()-1)
+ {
+ iter->user_data = NULL;
+ return FALSE;
+ }
+
+ // user_data is just the index +1 (+2 because we need the next)
+ iter->user_data = wxUIntToPtr(n+2);
+ }
+ else
+ {
+ wxGtkTreeModelNode *parent = FindParentNode( iter );
+ if( parent == NULL )
+ {
+ iter->user_data = NULL;
+ return FALSE;
+ }
+
+ int pos = parent->GetChildren().Index( iter->user_data );
+
+ if (pos == (int) parent->GetChildCount()-1)
+ {
+ iter->user_data = NULL;
+ return FALSE;
+ }
+
+ iter->user_data = parent->GetChildren().Item( pos+1 );
+ }
+
+ return TRUE;