2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "treemodel.h"
24 #include "debugging/debugging.h"
27 #include <gtk/gtktreemodel.h>
28 #include <gtk/gtktreednd.h>
29 #include <gtk/gtkmain.h>
30 #include <uilib/uilib.h>
32 #include "iscenegraph.h"
35 #include "generic/callback.h"
37 #include "string/string.h"
38 #include "generic/reference.h"
40 inline Nameable* Node_getNameable( scene::Node& node ){
41 return NodeTypeCast<Nameable>::cast( node );
46 #include "gtkutil/gtktreestore.h"
48 template<typename value_type>
49 inline void gtk_tree_model_get_pointer( GtkTreeModel* model, GtkTreeIter* iter, gint column, value_type** pointer ){
50 GValue value = GValue_default();
51 gtk_tree_model_get_value( model, iter, column, &value );
52 *pointer = (value_type*)g_value_get_pointer( &value );
56 typedef GtkTreeStore GraphTreeModel;
58 GtkTreeStore* graph_tree_model_new( graph_type* graph ){
59 return gtk_tree_store_new( 2, G_TYPE_POINTER, G_TYPE_POINTER );
62 void graph_tree_model_delete( GraphTreeModel* model ){
63 g_object_unref( G_OBJECT( model ) );
67 bool graph_tree_model_subtree_find_node( GraphTreeModel* model, GtkTreeIter* parent, const scene::Node& node, GtkTreeIter* iter ){
68 for ( gboolean success = gtk_tree_model_iter_children( GTK_TREE_MODEL( model ), iter, parent );
70 success = gtk_tree_model_iter_next( GTK_TREE_MODEL( model ), iter ) )
73 gtk_tree_model_get_pointer( GTK_TREE_MODEL( model ), iter, 0, ¤t );
74 if ( current == node ) {
81 typedef GtkTreeIter DoubleGtkTreeIter[2];
83 bool graph_tree_model_find_top( GraphTreeModel* model, const scene::Path& path, GtkTreeIter& iter ){
85 GtkTreeIter* parent_pointer = NULL;
87 for ( scene::Path::const_iterator i = path.begin(); i != path.end(); ++i )
89 if ( !graph_tree_model_subtree_find_node( model, parent_pointer, *i, &iter ) ) {
93 parent_pointer = &parent;
98 bool graph_tree_model_find_parent( GraphTreeModel* model, const scene::Path& path, GtkTreeIter& iter ){
100 GtkTreeIter* parent_pointer = NULL;
101 ASSERT_MESSAGE( path.size() > 1, "path too short" );
102 for ( scene::Path::const_iterator i = path.begin(); i != path.end() - 1; ++i )
105 if ( !graph_tree_model_subtree_find_node( model, parent_pointer, *i, &child ) ) {
109 parent_pointer = &iter;
114 void node_attach_name_changed_callback( scene::Node& node, const Callback& callback ){
116 Nameable* nameable = Node_getNameable( node );
117 if ( nameable != 0 ) {
118 nameable->attach( callback );
122 void node_detach_name_changed_callback( scene::Node& node, const Callback& callback ){
124 Nameable* nameable = Node_getNameable( node );
125 if ( nameable != 0 ) {
126 nameable->detach( callback );
131 GraphTreeModel* scene_graph_get_tree_model(); // temp hack
133 void graph_tree_model_row_changed( const scene::Instance& instance ){
134 GraphTreeModel* model = scene_graph_get_tree_model();
137 ASSERT_MESSAGE( graph_tree_model_find_top( model, instance.path(), child ), "RUNTIME ERROR" );
139 gtk_tree_store_set( GTK_TREE_STORE( model ), &child, 0, instance.path().top(), -1 );
142 void graph_tree_model_row_inserted( GraphTreeModel* model, const scene::Instance& instance ){
144 GtkTreeIter* parent_pointer = NULL;
145 if ( instance.path().size() != 1 ) {
146 ASSERT_MESSAGE( graph_tree_model_find_parent( model, instance.path(), parent ), "RUNTIME ERROR" );
147 parent_pointer = &parent;
150 gpointer node = instance.path().top();
151 gconstpointer selectable = Instance_getSelectable( instance );
154 gtk_tree_store_append( GTK_TREE_STORE( model ), &child, parent_pointer );
155 gtk_tree_store_set( GTK_TREE_STORE( model ), &child, 0, node, 1, selectable, -1 );
157 node_attach_name_changed_callback( instance.path().top(), ConstReferenceCaller<scene::Instance, graph_tree_model_row_changed>( instance ) );
160 void graph_tree_model_row_deleted( GraphTreeModel* model, const scene::Instance& instance ){
162 ASSERT_MESSAGE( graph_tree_model_find_top( model, instance.path(), child ), "RUNTIME ERROR" );
164 node_detach_name_changed_callback( instance.path().top(), ConstReferenceCaller<scene::Instance, graph_tree_model_row_changed>( instance ) );
166 gtk_tree_store_remove( GTK_TREE_STORE( model ), &child );
171 const char* node_get_name( scene::Node& node );
173 typedef scene::Node* NodePointer;
178 bool operator()( const NodePointer& self, const NodePointer& other ) const {
185 int result = string_compare( node_get_name( self ), node_get_name( other ) );
196 bool operator()( const PathConstReference& self, const PathConstReference& other ) const {
197 return std::lexicographical_compare( self.get().begin(), self.get().end(), other.get().begin(), other.get().end(), NodeNameLess() );
201 typedef std::map<PathConstReference, scene::Instance*, PathNameLess> graph_type;
203 struct GraphTreeModel
210 struct GraphTreeModelClass
212 GObjectClass parent_class;
215 #define GRAPH_TREE_MODEL( p ) ( reinterpret_cast<GraphTreeModel*>( p ) )
217 static GtkTreeModelFlags graph_tree_model_get_flags( GtkTreeModel* tree_model ){
218 return GTK_TREE_MODEL_ITERS_PERSIST;
221 static gint graph_tree_model_get_n_columns( GtkTreeModel* tree_model ){
222 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
223 GraphTreeModel* graph_tree_model = (GraphTreeModel*) tree_model;
228 static const gint c_stamp = 0xabcdef;
230 inline graph_type::iterator graph_iterator_read_tree_iter( GtkTreeIter* iter ){
231 ASSERT_MESSAGE( iter != 0, "tree model error" );
232 ASSERT_MESSAGE( iter->user_data != 0, "tree model error" );
233 ASSERT_MESSAGE( iter->stamp == c_stamp, "tree model error" );
234 return *reinterpret_cast<graph_type::iterator*>( &iter->user_data );
237 inline void graph_iterator_write_tree_iter( graph_type::iterator i, GtkTreeIter* iter ){
238 ASSERT_MESSAGE( iter != 0, "tree model error" );
239 iter->stamp = c_stamp;
240 *reinterpret_cast<graph_type::iterator*>( &iter->user_data ) = i;
241 ASSERT_MESSAGE( iter->user_data != 0, "tree model error" );
244 static GType graph_tree_model_get_column_type( GtkTreeModel *tree_model, gint index ){
245 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
246 GraphTreeModel *graph_tree_model = (GraphTreeModel *) tree_model;
248 return G_TYPE_POINTER;
251 static gboolean graph_tree_model_get_iter( GtkTreeModel* tree_model, GtkTreeIter* iter, GtkTreePath* path ){
252 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
253 gint* indices = gtk_tree_path_get_indices( path );
254 gint depth = gtk_tree_path_get_depth( path );
256 g_return_val_if_fail( depth > 0, FALSE );
258 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
260 if ( graph.empty() ) {
265 GtkTreeIter* parent = 0;
267 for ( gint i = 0; i < depth; i++ )
269 if ( !gtk_tree_model_iter_nth_child( tree_model, iter, parent, indices[i] ) ) {
279 static GtkTreePath* graph_tree_model_get_path( GtkTreeModel* tree_model, GtkTreeIter* iter ){
280 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
281 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
282 graph_type::iterator i = graph_iterator_read_tree_iter( iter );
284 GtkTreePath* path = ui::TreePath();
286 for ( std::size_t depth = ( *i ).first.get().size(); depth != 0; --depth )
288 std::size_t index = 0;
290 while ( i != graph.begin() && ( *i ).first.get().size() >= depth )
293 if ( ( *i ).first.get().size() == depth ) {
298 gtk_tree_path_prepend_index( path, index );
305 static void graph_tree_model_get_value( GtkTreeModel *tree_model, GtkTreeIter *iter, gint column, GValue *value ){
306 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
307 ASSERT_MESSAGE( column == 0 || column == 1, "tree model error" );
309 graph_type::iterator i = graph_iterator_read_tree_iter( iter );
311 g_value_init( value, G_TYPE_POINTER );
314 g_value_set_pointer( value, reinterpret_cast<gpointer>( ( *i ).first.get().top() ) );
317 g_value_set_pointer( value, reinterpret_cast<gpointer>( Instance_getSelectable( *( *i ).second ) ) );
321 static gboolean graph_tree_model_iter_next( GtkTreeModel *tree_model, GtkTreeIter *iter ){
322 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
323 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
324 graph_type::iterator i = graph_iterator_read_tree_iter( iter );
325 std::size_t depth = ( *i ).first.get().size();
329 while ( i != graph.end() && ( *i ).first.get().size() > depth )
334 if ( i == graph.end() || ( *i ).first.get().size() != depth ) {
338 graph_iterator_write_tree_iter( i, iter );
343 static gboolean graph_tree_model_iter_children( GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent ){
344 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
345 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
346 graph_type::iterator i = ( parent == 0 ) ? graph.begin() : graph_iterator_read_tree_iter( parent );
347 std::size_t depth = ( parent == 0 ) ? 1 : ( *i ).first.get().size() + 1;
353 if ( i != graph.end() && ( *i ).first.get().size() == depth ) {
354 graph_iterator_write_tree_iter( i, iter );
361 static gboolean graph_tree_model_iter_has_child( GtkTreeModel *tree_model, GtkTreeIter *iter ){
362 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
363 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
364 graph_type::iterator i = graph_iterator_read_tree_iter( iter );
365 std::size_t depth = ( *i ).first.get().size() + 1;
367 return ++i != graph.end() && ( *i ).first.get().size() == depth;
370 static gint graph_tree_model_iter_n_children( GtkTreeModel *tree_model, GtkTreeIter *parent ){
371 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
372 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
373 graph_type::iterator i = ( parent == 0 ) ? graph.begin() : graph_iterator_read_tree_iter( parent );
374 std::size_t depth = ( parent == 0 ) ? 1 : ( *i ).first.get().size() + 1;
381 while ( i != graph.end() && ( *i ).first.get().size() >= depth )
390 static gboolean graph_tree_model_iter_nth_child( GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent, gint n ){
391 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
392 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
393 graph_type::iterator i = ( parent == 0 ) ? graph.begin() : graph_iterator_read_tree_iter( parent );
394 std::size_t depth = ( parent == 0 ) ? 1 : ( *i ).first.get().size() + 1;
400 while ( i != graph.end() && ( *i ).first.get().size() >= depth )
402 if ( ( *i ).first.get().size() == depth && n-- == 0 ) {
403 graph_iterator_write_tree_iter( i, iter );
412 static gboolean graph_tree_model_iter_parent( GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *child ){
413 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
414 graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
415 graph_type::iterator i = graph_iterator_read_tree_iter( child );
416 std::size_t depth = ( *i ).first.get().size();
426 while ( ( *i ).first.get().size() >= depth );
427 graph_iterator_write_tree_iter( i, iter );
432 static GObjectClass *g_parent_class = 0;
434 static void graph_tree_model_init( GraphTreeModel *graph_tree_model ){
435 graph_tree_model->graph = 0;
438 static void graph_tree_model_finalize( GObject* object ){
439 GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( object );
442 ( *g_parent_class->finalize )( object );
445 static void graph_tree_model_class_init( GraphTreeModelClass *class_ ){
446 GObjectClass *object_class;
448 g_parent_class = (GObjectClass*)g_type_class_peek_parent( class_ );
449 object_class = (GObjectClass *) class_;
451 object_class->finalize = graph_tree_model_finalize;
454 static void graph_tree_model_tree_model_init( GtkTreeModelIface *iface ){
455 iface->get_flags = graph_tree_model_get_flags;
456 iface->get_n_columns = graph_tree_model_get_n_columns;
457 iface->get_column_type = graph_tree_model_get_column_type;
458 iface->get_iter = graph_tree_model_get_iter;
459 iface->get_path = graph_tree_model_get_path;
460 iface->get_value = graph_tree_model_get_value;
461 iface->iter_next = graph_tree_model_iter_next;
462 iface->iter_children = graph_tree_model_iter_children;
463 iface->iter_has_child = graph_tree_model_iter_has_child;
464 iface->iter_n_children = graph_tree_model_iter_n_children;
465 iface->iter_nth_child = graph_tree_model_iter_nth_child;
466 iface->iter_parent = graph_tree_model_iter_parent;
469 static gboolean graph_tree_model_row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path ){
471 gint depth = gtk_tree_path_get_depth( path );
473 return gtk_tree_path_get_depth( path ) > 1;
476 static gboolean graph_tree_model_drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath *path ){
479 if ( gtk_tree_model_get_iter( GTK_TREE_MODEL( drag_source ), &iter, path ) ) {
480 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
481 Path_deleteTop( ( *i ).first );
490 static gboolean graph_tree_model_drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path, GtkSelectionData *selection_data ){
491 if ( gtk_tree_set_row_drag_data( selection_data, GTK_TREE_MODEL( drag_source ), path ) ) {
496 /* FIXME handle text targets at least. */
502 static void graph_tree_model_drag_source_init( GtkTreeDragSourceIface *iface ){
503 iface->row_draggable = graph_tree_model_row_draggable;
504 iface->drag_data_delete = graph_tree_model_drag_data_delete;
505 iface->drag_data_get = graph_tree_model_drag_data_get;
508 static gboolean graph_tree_model_drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest, GtkSelectionData *selection_data ){
509 GtkTreeModel *tree_model = GTK_TREE_MODEL( drag_dest );
511 GtkTreeModel *src_model = 0;
512 GtkTreePath *src_path = 0;
513 if ( gtk_tree_get_row_drag_data( selection_data, &src_model, &src_path )
514 && src_model == tree_model ) {
515 /* Copy the given row to a new position */
518 if ( gtk_tree_model_get_iter( src_model, &iter, src_path ) ) {
524 /* FIXME maybe add some data targets eventually, or handle text
525 * targets in the simple case.
532 static gboolean graph_tree_model_row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path, GtkSelectionData *selection_data ){
533 gboolean retval = FALSE;
535 GtkTreeModel *src_model = 0;
536 GtkTreePath *src_path = 0;
537 if ( gtk_tree_get_row_drag_data( selection_data, &src_model, &src_path ) != FALSE ) {
538 /* can only drag to ourselves */
539 if ( src_model == GTK_TREE_MODEL( drag_dest ) ) {
540 /* Can't drop into ourself. */
541 if ( !gtk_tree_path_is_ancestor( src_path, dest_path ) ) {
542 /* Can't drop if dest_path's parent doesn't exist */
543 if ( gtk_tree_path_get_depth( dest_path ) > 1 ) {
544 GtkTreePath* tmp = gtk_tree_path_copy( dest_path );
545 gtk_tree_path_up( tmp );
548 retval = gtk_tree_model_get_iter( GTK_TREE_MODEL( drag_dest ), &iter, tmp );
550 gtk_tree_path_free( tmp );
555 gtk_tree_path_free( src_path );
561 static void graph_tree_model_drag_dest_init( GtkTreeDragDestIface *iface ){
562 iface->drag_data_received = graph_tree_model_drag_data_received;
563 iface->row_drop_possible = graph_tree_model_row_drop_possible;
566 GType graph_tree_model_get_type( void ){
567 static GType graph_tree_model_type = 0;
569 if ( !graph_tree_model_type ) {
570 static const GTypeInfo graph_tree_model_info =
572 sizeof( GraphTreeModelClass ),
574 0, /* base_finalize */
575 (GClassInitFunc) graph_tree_model_class_init,
576 0, /* class_finalize */
578 sizeof( GraphTreeModel ),
580 (GInstanceInitFunc) graph_tree_model_init
583 static const GInterfaceInfo tree_model_info =
585 (GInterfaceInitFunc) graph_tree_model_tree_model_init,
590 static const GInterfaceInfo drag_source_info =
592 (GInterfaceInitFunc) graph_tree_model_drag_source_init,
597 static const GInterfaceInfo drag_dest_info =
599 (GInterfaceInitFunc) graph_tree_model_drag_dest_init,
604 graph_tree_model_type = g_type_register_static( G_TYPE_OBJECT, "GraphTreeModel",
605 &graph_tree_model_info, (GTypeFlags)0 );
607 g_type_add_interface_static( graph_tree_model_type,
610 g_type_add_interface_static( graph_tree_model_type,
611 GTK_TYPE_TREE_DRAG_SOURCE,
613 g_type_add_interface_static( graph_tree_model_type,
614 GTK_TYPE_TREE_DRAG_DEST,
618 return graph_tree_model_type;
621 GraphTreeModel* graph_tree_model_new(){
622 GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( g_object_new( graph_tree_model_get_type(), 0 ) );
624 graph_tree_model->graph = new graph_type;
626 return graph_tree_model;
629 void graph_tree_model_delete( GraphTreeModel* model ){
631 g_object_unref( G_OBJECT( model ) );
635 class TempNameable : public Nameable
639 TempNameable( const char* name ) : m_name( name ){
641 const char* name() const {
644 void attach( const NameCallback& callback ){
646 void detach( const NameCallback& callback ){
650 void node_attach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
652 Nameable* nameable = Node_getNameable( node );
653 if ( nameable != 0 ) {
654 nameable->attach( callback );
658 void node_detach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
660 Nameable* nameable = Node_getNameable( node );
661 if ( nameable != 0 ) {
662 nameable->detach( callback );
667 GraphTreeModel* scene_graph_get_tree_model(); // temp hack
669 void graph_tree_model_row_inserted( GraphTreeModel* model, graph_type::iterator i ){
671 graph_iterator_write_tree_iter( i, &iter );
673 GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
675 gint depth = gtk_tree_path_get_depth( tree_path );
676 gint* indices = gtk_tree_path_get_indices( tree_path );
678 gtk_tree_model_row_inserted( GTK_TREE_MODEL( model ), tree_path, &iter );
680 gtk_tree_path_free( tree_path );
683 void graph_tree_model_row_deleted( GraphTreeModel* model, graph_type::iterator i ){
685 graph_iterator_write_tree_iter( i, &iter );
687 GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
689 gtk_tree_model_row_deleted( GTK_TREE_MODEL( model ), tree_path );
691 gtk_tree_path_free( tree_path );
694 #include "generic/referencecounted.h"
696 void graph_tree_model_set_name( const scene::Instance& instance, const char* name ){
697 GraphTreeModel* model = scene_graph_get_tree_model();
699 if ( string_empty( name ) ) { // hack!
700 graph_type::iterator i = model->graph->find( PathConstReference( instance.path() ) );
701 ASSERT_MESSAGE( i != model->graph->end(), "ERROR" );
703 graph_tree_model_row_deleted( model, i );
705 model->graph->erase( i );
709 graph_type::iterator i = model->graph->insert( graph_type::value_type( PathConstReference( instance.path() ), &const_cast<scene::Instance&>( instance ) ) ).first;
711 graph_tree_model_row_inserted( model, i );
715 void graph_tree_model_insert( GraphTreeModel* model, const scene::Instance& instance ){
716 graph_type::iterator i = model->graph->insert( graph_type::value_type( PathConstReference( instance.path() ), &const_cast<scene::Instance&>( instance ) ) ).first;
718 graph_tree_model_row_inserted( model, i );
720 node_attach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
723 void graph_tree_model_erase( GraphTreeModel* model, const scene::Instance& instance ){
724 node_detach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
726 graph_type::iterator i = model->graph->find( PathConstReference( instance.path() ) );
727 ASSERT_MESSAGE( i != model->graph->end(), "ERROR" );
729 graph_tree_model_row_deleted( model, i );
731 model->graph->erase( i );
737 void graph_tree_model_row_changed( GraphTreeNode& node );
741 typedef std::map<std::pair<std::string, scene::Node*>, GraphTreeNode*> ChildNodes;
742 ChildNodes m_childnodes;
744 Reference<scene::Instance> m_instance;
745 GraphTreeNode* m_parent;
747 typedef ChildNodes::iterator iterator;
748 typedef ChildNodes::key_type key_type;
749 typedef ChildNodes::value_type value_type;
750 typedef ChildNodes::size_type size_type;
752 GraphTreeNode( scene::Instance& instance ) : m_instance( instance ), m_parent( 0 ){
753 m_instance.get().setChildSelectedChangedCallback( RowChangedCaller( *this ) );
756 m_instance.get().setChildSelectedChangedCallback( Callback() );
757 ASSERT_MESSAGE( empty(), "GraphTreeNode::~GraphTreeNode: memory leak" );
761 return m_childnodes.begin();
764 return m_childnodes.end();
767 size_type size() const {
768 return m_childnodes.size();
771 return m_childnodes.empty();
774 iterator insert( const value_type& value ){
775 iterator i = m_childnodes.insert( value ).first;
776 ( *i ).second->m_parent = this;
779 void erase( iterator i ){
780 m_childnodes.erase( i );
782 iterator find( const key_type& key ){
783 return m_childnodes.find( key );
786 void swap( GraphTreeNode& other ){
787 std::swap( m_parent, other.m_parent );
788 std::swap( m_childnodes, other.m_childnodes );
789 std::swap( m_instance, other.m_instance );
793 graph_tree_model_row_changed( *this );
795 typedef MemberCaller<GraphTreeNode, &GraphTreeNode::rowChanged> RowChangedCaller;
798 struct GraphTreeModel
802 GraphTreeNode* m_graph;
805 struct GraphTreeModelClass
807 GObjectClass parent_class;
810 #define GRAPH_TREE_MODEL( p ) ( reinterpret_cast<GraphTreeModel*>( p ) )
812 static GtkTreeModelFlags graph_tree_model_get_flags( GtkTreeModel* tree_model ){
813 return GTK_TREE_MODEL_ITERS_PERSIST;
816 static gint graph_tree_model_get_n_columns( GtkTreeModel* tree_model ){
817 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
818 //GraphTreeModel* graph_tree_model = (GraphTreeModel*) tree_model;
823 static const gint c_stamp = 0xabcdef;
825 inline GraphTreeNode::iterator graph_iterator_read_tree_iter( GtkTreeIter* iter ){
826 ASSERT_MESSAGE( iter != 0, "tree model error" );
827 ASSERT_MESSAGE( iter->user_data != 0, "tree model error" );
828 ASSERT_MESSAGE( iter->stamp == c_stamp, "tree model error" );
829 return *reinterpret_cast<GraphTreeNode::iterator*>( &iter->user_data );
832 inline void graph_iterator_write_tree_iter( GraphTreeNode::iterator i, GtkTreeIter* iter ){
833 ASSERT_MESSAGE( iter != 0, "tree model error" );
834 iter->stamp = c_stamp;
835 *reinterpret_cast<GraphTreeNode::iterator*>( &iter->user_data ) = i;
836 ASSERT_MESSAGE( iter->user_data != 0, "tree model error" );
839 static GType graph_tree_model_get_column_type( GtkTreeModel *tree_model, gint index ){
840 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
841 //GraphTreeModel *graph_tree_model = (GraphTreeModel *) tree_model;
843 return G_TYPE_POINTER;
846 static gboolean graph_tree_model_get_iter( GtkTreeModel* tree_model, GtkTreeIter* iter, GtkTreePath* path ){
847 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
848 gint* indices = gtk_tree_path_get_indices( path );
849 gint depth = gtk_tree_path_get_depth( path );
851 g_return_val_if_fail( depth > 0, FALSE );
853 GraphTreeNode* graph = GRAPH_TREE_MODEL( tree_model )->m_graph;
855 if ( graph->empty() ) {
860 GtkTreeIter* parent = 0;
862 for ( gint i = 0; i < depth; i++ )
864 if ( !gtk_tree_model_iter_nth_child( tree_model, iter, parent, indices[i] ) ) {
874 static GtkTreePath* graph_tree_model_get_path( GtkTreeModel* tree_model, GtkTreeIter* iter ){
875 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
876 GraphTreeNode* graph = GRAPH_TREE_MODEL( tree_model )->m_graph;
878 GtkTreePath* path = ui::TreePath();
880 for ( GraphTreeNode* node = ( *graph_iterator_read_tree_iter( iter ) ).second; node != graph; node = node->m_parent )
882 std::size_t index = 0;
883 for ( GraphTreeNode::iterator i = node->m_parent->begin(); i != node->m_parent->end(); ++i, ++index )
885 if ( ( *i ).second == node ) {
886 gtk_tree_path_prepend_index( path, gint( index ) );
890 ASSERT_MESSAGE( index != node->m_parent->size(), "error resolving tree path" );
897 static void graph_tree_model_get_value( GtkTreeModel *tree_model, GtkTreeIter *iter, gint column, GValue *value ){
898 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
899 ASSERT_MESSAGE( column == 0 || column == 1, "tree model error" );
901 GraphTreeNode::iterator i = graph_iterator_read_tree_iter( iter );
903 g_value_init( value, G_TYPE_POINTER );
906 g_value_set_pointer( value, reinterpret_cast<gpointer>( ( *i ).first.second ) );
910 g_value_set_pointer( value, reinterpret_cast<gpointer>( &( *i ).second->m_instance.get() ) );
914 static gboolean graph_tree_model_iter_next( GtkTreeModel *tree_model, GtkTreeIter *iter ){
915 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
916 GraphTreeNode::iterator i = graph_iterator_read_tree_iter( iter );
917 GraphTreeNode& parent = *( *i ).second->m_parent;
919 ASSERT_MESSAGE( i != parent.end(), "RUNTIME ERROR" );
921 if ( ++i == parent.end() ) {
925 graph_iterator_write_tree_iter( i, iter );
930 static gboolean graph_tree_model_iter_children( GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent ){
931 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
932 GraphTreeNode& node = ( parent == 0 ) ? *GRAPH_TREE_MODEL( tree_model )->m_graph : *( *graph_iterator_read_tree_iter( parent ) ).second;
933 if ( !node.empty() ) {
934 graph_iterator_write_tree_iter( node.begin(), iter );
941 static gboolean graph_tree_model_iter_has_child( GtkTreeModel *tree_model, GtkTreeIter *iter ){
942 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
943 GraphTreeNode& node = *( *graph_iterator_read_tree_iter( iter ) ).second;
944 return !node.empty();
947 static gint graph_tree_model_iter_n_children( GtkTreeModel *tree_model, GtkTreeIter *parent ){
948 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
949 GraphTreeNode& node = ( parent == 0 ) ? *GRAPH_TREE_MODEL( tree_model )->m_graph : *( *graph_iterator_read_tree_iter( parent ) ).second;
950 return static_cast<gint>( node.size() );
953 static gboolean graph_tree_model_iter_nth_child( GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *parent, gint n ){
954 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
955 GraphTreeNode& node = ( parent == 0 ) ? *GRAPH_TREE_MODEL( tree_model )->m_graph : *( *graph_iterator_read_tree_iter( parent ) ).second;
956 if ( static_cast<std::size_t>( n ) < node.size() ) {
957 GraphTreeNode::iterator i = node.begin();
958 std::advance( i, n );
959 graph_iterator_write_tree_iter( i, iter );
966 static gboolean graph_tree_model_iter_parent( GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreeIter *child ){
967 ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
968 GraphTreeNode& node = *( *graph_iterator_read_tree_iter( child ) ).second;
969 if ( node.m_parent != GRAPH_TREE_MODEL( tree_model )->m_graph ) {
970 GraphTreeNode& parentParent = *node.m_parent->m_parent;
971 for ( GraphTreeNode::iterator i = parentParent.begin(); i != parentParent.end(); ++i )
973 if ( ( *i ).second == node.m_parent ) {
974 graph_iterator_write_tree_iter( i, iter );
982 static GObjectClass *g_parent_class = 0;
986 scene::Node* g_null_node = 0;
989 class NullInstance : public scene::Instance
992 NullInstance() : scene::Instance( scene::Path( makeReference( *g_null_node ) ), 0, 0, Static<InstanceTypeCastTable>::instance() ){
998 NullInstance g_null_instance;
1001 static void graph_tree_model_init( GraphTreeModel *graph_tree_model ){
1002 graph_tree_model->m_graph = new GraphTreeNode( g_null_instance );
1005 static void graph_tree_model_finalize( GObject* object ){
1006 GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( object );
1008 delete graph_tree_model->m_graph;
1011 ( *g_parent_class->finalize )( object );
1014 static void graph_tree_model_class_init( GraphTreeModelClass *class_ ){
1015 GObjectClass *object_class;
1017 g_parent_class = (GObjectClass*)g_type_class_peek_parent( class_ );
1018 object_class = (GObjectClass *) class_;
1020 object_class->finalize = graph_tree_model_finalize;
1023 static void graph_tree_model_tree_model_init( GtkTreeModelIface *iface ){
1024 iface->get_flags = graph_tree_model_get_flags;
1025 iface->get_n_columns = graph_tree_model_get_n_columns;
1026 iface->get_column_type = graph_tree_model_get_column_type;
1027 iface->get_iter = graph_tree_model_get_iter;
1028 iface->get_path = graph_tree_model_get_path;
1029 iface->get_value = graph_tree_model_get_value;
1030 iface->iter_next = graph_tree_model_iter_next;
1031 iface->iter_children = graph_tree_model_iter_children;
1032 iface->iter_has_child = graph_tree_model_iter_has_child;
1033 iface->iter_n_children = graph_tree_model_iter_n_children;
1034 iface->iter_nth_child = graph_tree_model_iter_nth_child;
1035 iface->iter_parent = graph_tree_model_iter_parent;
1038 GType graph_tree_model_get_type( void ){
1039 static GType graph_tree_model_type = 0;
1041 if ( !graph_tree_model_type ) {
1042 static const GTypeInfo graph_tree_model_info =
1044 sizeof( GraphTreeModelClass ),
1046 0, /* base_finalize */
1047 (GClassInitFunc) graph_tree_model_class_init,
1048 0, /* class_finalize */
1050 sizeof( GraphTreeModel ),
1051 0, /* n_preallocs */
1052 (GInstanceInitFunc) graph_tree_model_init,
1056 static const GInterfaceInfo tree_model_info =
1058 (GInterfaceInitFunc) graph_tree_model_tree_model_init,
1063 graph_tree_model_type = g_type_register_static( G_TYPE_OBJECT, "GraphTreeModel",
1064 &graph_tree_model_info, (GTypeFlags)0 );
1066 g_type_add_interface_static( graph_tree_model_type,
1067 GTK_TYPE_TREE_MODEL,
1071 return graph_tree_model_type;
1074 GraphTreeModel* graph_tree_model_new(){
1075 GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( g_object_new( graph_tree_model_get_type(), 0 ) );
1077 return graph_tree_model;
1080 void graph_tree_model_delete( GraphTreeModel* model ){
1081 g_object_unref( G_OBJECT( model ) );
1084 void graph_tree_model_row_changed( GraphTreeModel* model, GraphTreeNode::iterator i ){
1086 graph_iterator_write_tree_iter( i, &iter );
1088 GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
1090 gtk_tree_model_row_changed( GTK_TREE_MODEL( model ), tree_path, &iter );
1092 gtk_tree_path_free( tree_path );
1095 void graph_tree_model_row_inserted( GraphTreeModel* model, GraphTreeNode::iterator i ){
1097 graph_iterator_write_tree_iter( i, &iter );
1099 GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
1101 gtk_tree_model_row_inserted( GTK_TREE_MODEL( model ), tree_path, &iter );
1103 gtk_tree_path_free( tree_path );
1106 void graph_tree_model_row_deleted( GraphTreeModel* model, GraphTreeNode::iterator i ){
1108 graph_iterator_write_tree_iter( i, &iter );
1110 GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
1112 gtk_tree_model_row_deleted( GTK_TREE_MODEL( model ), tree_path );
1114 gtk_tree_path_free( tree_path );
1117 void graph_tree_model_row_inserted( GraphTreeModel& model, GraphTreeNode::iterator i ){
1118 graph_tree_model_row_inserted( &model, i );
1121 void graph_tree_model_row_deleted( GraphTreeModel& model, GraphTreeNode::iterator i ){
1122 graph_tree_model_row_deleted( &model, i );
1125 const char* node_get_name( scene::Node& node );
1127 const char* node_get_name_safe( scene::Node& node ){
1131 return node_get_name( node );
1134 GraphTreeNode* graph_tree_model_find_parent( GraphTreeModel* model, const scene::Path& path ){
1135 GraphTreeNode* parent = model->m_graph;
1136 for ( scene::Path::const_iterator i = path.begin(); i != path.end() - 1; ++i )
1138 GraphTreeNode::iterator child = parent->find( GraphTreeNode::key_type( node_get_name_safe( ( *i ).get() ), ( *i ).get_pointer() ) );
1139 ASSERT_MESSAGE( child != parent->end(), "ERROR" );
1140 parent = ( *child ).second;
1145 void node_attach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
1147 Nameable* nameable = Node_getNameable( node );
1148 if ( nameable != 0 ) {
1149 nameable->attach( callback );
1153 void node_detach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
1155 Nameable* nameable = Node_getNameable( node );
1156 if ( nameable != 0 ) {
1157 nameable->detach( callback );
1162 GraphTreeModel* scene_graph_get_tree_model(); // temp hack
1164 void graph_tree_node_foreach_pre( GraphTreeNode::iterator root, const Callback1<GraphTreeNode::iterator>& callback ){
1166 for ( GraphTreeNode::iterator i = ( *root ).second->begin(); i != ( *root ).second->end(); ++i )
1168 graph_tree_node_foreach_pre( i, callback );
1172 void graph_tree_node_foreach_post( GraphTreeNode::iterator root, const Callback1<GraphTreeNode::iterator>& callback ){
1173 for ( GraphTreeNode::iterator i = ( *root ).second->begin(); i != ( *root ).second->end(); ++i )
1175 graph_tree_node_foreach_post( i, callback );
1180 void graph_tree_model_row_changed( GraphTreeNode& node ){
1181 GraphTreeModel* model = scene_graph_get_tree_model();
1182 const scene::Instance& instance = node.m_instance.get();
1184 GraphTreeNode::iterator i = node.m_parent->find( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ) );
1186 graph_tree_model_row_changed( model, i );
1189 void graph_tree_model_set_name( const scene::Instance& instance, const char* name ){
1190 GraphTreeModel* model = scene_graph_get_tree_model();
1191 GraphTreeNode* parent = graph_tree_model_find_parent( model, instance.path() );
1193 GraphTreeNode::iterator oldNode = parent->find( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ) );
1194 graph_tree_node_foreach_post( oldNode, ReferenceCaller1<GraphTreeModel, GraphTreeNode::iterator, graph_tree_model_row_deleted>( *model ) );
1195 GraphTreeNode* node( ( *oldNode ).second );
1196 parent->erase( oldNode );
1198 GraphTreeNode::iterator newNode = parent->insert( GraphTreeNode::value_type( GraphTreeNode::key_type( name, &instance.path().top().get() ), node ) );
1199 graph_tree_node_foreach_pre( newNode, ReferenceCaller1<GraphTreeModel, GraphTreeNode::iterator, graph_tree_model_row_inserted>( *model ) );
1202 void graph_tree_model_insert( GraphTreeModel* model, const scene::Instance& instance ){
1203 GraphTreeNode* parent = graph_tree_model_find_parent( model, instance.path() );
1205 GraphTreeNode::iterator i = parent->insert( GraphTreeNode::value_type( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ), new GraphTreeNode( const_cast<scene::Instance&>( instance ) ) ) );
1207 graph_tree_model_row_inserted( model, i );
1209 node_attach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
1212 void graph_tree_model_erase( GraphTreeModel* model, const scene::Instance& instance ){
1213 node_detach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
1215 GraphTreeNode* parent = graph_tree_model_find_parent( model, instance.path() );
1217 GraphTreeNode::iterator i = parent->find( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ) );
1219 graph_tree_model_row_deleted( model, i );
1221 GraphTreeNode* node( ( *i ).second );
1234 class TestGraphTreeModel
1237 TestGraphTreeModel(){
1242 scene::Node* root = *(scene::Node*)0xa0000000;
1243 scene::Node* node1 = (scene::Node*)0xa0000001;
1244 scene::Node* node2 = (scene::Node*)0xa0000002;
1245 scene::Node* node3 = (scene::Node*)0xa0000003;
1246 scene::Node* node4 = (scene::Node*)0xa0000004;
1247 scene::Instance* instance = (scene::Instance*)0xaaaaaaaa;
1249 scene::Path rootpath( root );
1251 graph.insert( graph_type::value_type( rootpath, instance ) );
1253 rootpath.push( node1 );
1254 graph.insert( graph_type::value_type( rootpath, instance ) );
1257 rootpath.push( node2 );
1258 graph.insert( graph_type::value_type( rootpath, instance ) );
1259 rootpath.push( node3 );
1260 graph.insert( graph_type::value_type( rootpath, instance ) );
1262 rootpath.push( node4 );
1263 graph.insert( graph_type::value_type( rootpath, instance ) );
1267 GtkTreeModel* model = GTK_TREE_MODEL( graph_tree_model_new( &graph ) );
1270 gint n_columns = gtk_tree_model_get_n_columns( model );
1271 ASSERT_MESSAGE( n_columns == 2, "test failed!" );
1275 GType type = gtk_tree_model_get_column_type( model, 0 );
1276 ASSERT_MESSAGE( type == G_TYPE_POINTER, "test failed!" );
1280 GType type = gtk_tree_model_get_column_type( model, 1 );
1281 ASSERT_MESSAGE( type == G_TYPE_POINTER, "test failed!" );
1287 gtk_tree_model_get_iter_first( model, &iter );
1289 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
1290 ASSERT_MESSAGE( ( *i ).first.get().size() == 2 && ( *i ).first.get().top() == node1, "test failed!" );
1295 gtk_tree_model_get_iter_first( model, &iter );
1297 ASSERT_MESSAGE( gtk_tree_model_iter_has_child( model, &iter ) == FALSE, "test failed!" );
1299 ASSERT_MESSAGE( gtk_tree_model_iter_n_children( model, &iter ) == 0, "test failed!" );
1301 gtk_tree_model_iter_next( model, &iter );
1303 ASSERT_MESSAGE( gtk_tree_model_iter_has_child( model, &iter ) != FALSE, "test failed!" );
1305 ASSERT_MESSAGE( gtk_tree_model_iter_n_children( model, &iter ) == 2, "test failed!" );
1309 gtk_tree_model_iter_nth_child( model, &child, &iter, 0 );
1312 gtk_tree_model_get_value( model, &child, 0, (GValue*)&test );
1313 ASSERT_MESSAGE( test == node3, "test failed!" );
1317 gtk_tree_model_iter_parent( model, &parent, &child );
1320 gtk_tree_model_get_value( model, &parent, 0, (GValue*)&test );
1321 ASSERT_MESSAGE( test == node2, "test failed!" );
1327 gtk_tree_model_iter_nth_child( model, &child, &iter, 1 );
1330 gtk_tree_model_get_value( model, &child, 0, (GValue*)&test );
1331 ASSERT_MESSAGE( test == node4, "test failed!" );
1337 std::size_t count = 0;
1338 for ( gboolean good = gtk_tree_model_get_iter_first( model, &iter ); good; good = gtk_tree_model_iter_next( model, &iter ) )
1341 gtk_tree_model_get_value( model, &iter, 0, (GValue*)&test );
1343 ASSERT_MESSAGE( ( count == 0 && test == node1 ) || ( count == 1 && test == node2 ), "test failed!" );
1347 ASSERT_MESSAGE( count == 2, "test failed!" );
1353 gtk_tree_model_get_iter_first( model, &iter );
1356 gtk_tree_model_get_value( model, &iter, 0, (GValue*)&test );
1357 ASSERT_MESSAGE( test == node1, "test failed!" );
1362 GtkTreePath* path = ui::TreePath( "0" );
1363 gtk_tree_model_get_iter( model, &iter, path );
1364 gtk_tree_path_free( path );
1366 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
1367 ASSERT_MESSAGE( ( *i ).first.get().size() == 2 && ( *i ).first.get().top() == node1, "test failed!" );
1372 GtkTreePath* path = ui::TreePath( "1" );
1373 gtk_tree_model_get_iter( model, &iter, path );
1374 gtk_tree_path_free( path );
1376 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
1377 ASSERT_MESSAGE( ( *i ).first.get().size() == 2 && ( *i ).first.get().top() == node2, "test failed!" );
1382 graph_type::iterator i = graph.begin();
1384 graph_iterator_write_tree_iter( i, &iter );
1386 GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
1388 gint depth = gtk_tree_path_get_depth( path );
1389 gint* indices = gtk_tree_path_get_indices( path );
1391 ASSERT_MESSAGE( depth == 1 && indices[0] == 0, "test failed!" );
1393 gtk_tree_path_free( path );
1398 graph_type::iterator i = graph.begin();
1401 graph_iterator_write_tree_iter( i, &iter );
1403 GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
1405 gint depth = gtk_tree_path_get_depth( path );
1406 gint* indices = gtk_tree_path_get_indices( path );
1408 ASSERT_MESSAGE( depth == 1 && indices[0] == 1, "test failed!" );
1410 gtk_tree_path_free( path );
1416 TestGraphTreeModel g_TestGraphTreeModel;