]> git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/treemodel.cpp
-DGTK_DISABLE_SINGLE_INCLUDES
[xonotic/netradiant.git] / radiant / treemodel.cpp
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
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.
11
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.
16
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
20  */
21
22 #include "treemodel.h"
23
24 #include "debugging/debugging.h"
25
26 #include <map>
27 #include <gtk/gtk.h>
28 #include <uilib/uilib.h>
29
30 #include "iscenegraph.h"
31 #include "nameable.h"
32
33 #include "generic/callback.h"
34 #include "scenelib.h"
35 #include "string/string.h"
36 #include "generic/reference.h"
37
38 inline Nameable* Node_getNameable( scene::Node& node ){
39         return NodeTypeCast<Nameable>::cast( node );
40 }
41
42 #if 0
43
44 #include "gtkutil/gtktreestore.h"
45
46 template<typename value_type>
47 inline void gtk_tree_model_get_pointer( GtkTreeModel* model, GtkTreeIter* iter, gint column, value_type** pointer ){
48         GValue value = GValue_default();
49         gtk_tree_model_get_value( model, iter, column, &value );
50         *pointer = (value_type*)g_value_get_pointer( &value );
51 }
52
53
54 typedef GtkTreeStore GraphTreeModel;
55
56 GtkTreeStore* graph_tree_model_new( graph_type* graph ){
57         return gtk_tree_store_new( 2, G_TYPE_POINTER, G_TYPE_POINTER );
58 }
59
60 void graph_tree_model_delete( GraphTreeModel* model ){
61         g_object_unref( G_OBJECT( model ) );
62 }
63
64
65 bool graph_tree_model_subtree_find_node( GraphTreeModel* model, GtkTreeIter* parent, const scene::Node& node, GtkTreeIter* iter ){
66         for ( gboolean success = gtk_tree_model_iter_children( GTK_TREE_MODEL( model ), iter, parent );
67                   success != FALSE;
68                   success = gtk_tree_model_iter_next( GTK_TREE_MODEL( model ), iter ) )
69         {
70                 scene::Node* current;
71                 gtk_tree_model_get_pointer( GTK_TREE_MODEL( model ), iter, 0, &current );
72                 if ( current == node ) {
73                         return true;
74                 }
75         }
76         return false;
77 }
78
79 typedef GtkTreeIter DoubleGtkTreeIter[2];
80
81 bool graph_tree_model_find_top( GraphTreeModel* model, const scene::Path& path, GtkTreeIter& iter ){
82         int swap = 0;
83         GtkTreeIter* parent_pointer = NULL;
84         GtkTreeIter parent;
85         for ( scene::Path::const_iterator i = path.begin(); i != path.end(); ++i )
86         {
87                 if ( !graph_tree_model_subtree_find_node( model, parent_pointer, *i, &iter ) ) {
88                         return false;
89                 }
90                 parent = iter;
91                 parent_pointer = &parent;
92         }
93         return true;
94 }
95
96 bool graph_tree_model_find_parent( GraphTreeModel* model, const scene::Path& path, GtkTreeIter& iter ){
97         int swap = 0;
98         GtkTreeIter* parent_pointer = NULL;
99         ASSERT_MESSAGE( path.size() > 1, "path too short" );
100         for ( scene::Path::const_iterator i = path.begin(); i != path.end() - 1; ++i )
101         {
102                 GtkTreeIter child;
103                 if ( !graph_tree_model_subtree_find_node( model, parent_pointer, *i, &child ) ) {
104                         return false;
105                 }
106                 iter = child;
107                 parent_pointer = &iter;
108         }
109         return true;
110 }
111
112 void node_attach_name_changed_callback( scene::Node& node, const Callback& callback ){
113         if ( node != 0 ) {
114                 Nameable* nameable = Node_getNameable( node );
115                 if ( nameable != 0 ) {
116                         nameable->attach( callback );
117                 }
118         }
119 }
120 void node_detach_name_changed_callback( scene::Node& node, const Callback& callback ){
121         if ( node != 0 ) {
122                 Nameable* nameable = Node_getNameable( node );
123                 if ( nameable != 0 ) {
124                         nameable->detach( callback );
125                 }
126         }
127 }
128
129 GraphTreeModel* scene_graph_get_tree_model(); // temp hack
130
131 void graph_tree_model_row_changed( const scene::Instance& instance ){
132         GraphTreeModel* model = scene_graph_get_tree_model();
133
134         GtkTreeIter child;
135         ASSERT_MESSAGE( graph_tree_model_find_top( model, instance.path(), child ), "RUNTIME ERROR" );
136
137         gtk_tree_store_set( GTK_TREE_STORE( model ), &child, 0, instance.path().top(), -1 );
138 }
139
140 void graph_tree_model_row_inserted( GraphTreeModel* model, const scene::Instance& instance ){
141         GtkTreeIter parent;
142         GtkTreeIter* parent_pointer = NULL;
143         if ( instance.path().size() != 1 ) {
144                 ASSERT_MESSAGE( graph_tree_model_find_parent( model, instance.path(), parent ), "RUNTIME ERROR" );
145                 parent_pointer = &parent;
146         }
147
148         gpointer node = instance.path().top();
149         gconstpointer selectable = Instance_getSelectable( instance );
150
151         GtkTreeIter child;
152         gtk_tree_store_append( GTK_TREE_STORE( model ), &child, parent_pointer );
153         gtk_tree_store_set( GTK_TREE_STORE( model ), &child, 0, node, 1, selectable, -1 );
154
155         node_attach_name_changed_callback( instance.path().top(), ConstReferenceCaller<scene::Instance, graph_tree_model_row_changed>( instance ) );
156 }
157
158 void graph_tree_model_row_deleted( GraphTreeModel* model, const scene::Instance& instance ){
159         GtkTreeIter child;
160         ASSERT_MESSAGE( graph_tree_model_find_top( model, instance.path(), child ), "RUNTIME ERROR" );
161
162         node_detach_name_changed_callback( instance.path().top(), ConstReferenceCaller<scene::Instance, graph_tree_model_row_changed>( instance ) );
163
164         gtk_tree_store_remove( GTK_TREE_STORE( model ), &child );
165 }
166
167 #elif 0
168
169 const char* node_get_name( scene::Node& node );
170
171 typedef scene::Node* NodePointer;
172
173 class NodeNameLess
174 {
175 public:
176 bool operator()( const NodePointer& self, const NodePointer& other ) const {
177         if ( self == 0 ) {
178                 return true;
179         }
180         if ( other == 0 ) {
181                 return false;
182         }
183         int result = string_compare( node_get_name( self ), node_get_name( other ) );
184         if ( result == 0 ) {
185                 return self < other;
186         }
187         return result < 0;
188 }
189 };
190
191 class PathNameLess
192 {
193 public:
194 bool operator()( const PathConstReference& self, const PathConstReference& other ) const {
195         return std::lexicographical_compare( self.get().begin(), self.get().end(), other.get().begin(), other.get().end(), NodeNameLess() );
196 }
197 };
198
199 typedef std::map<PathConstReference, scene::Instance*, PathNameLess> graph_type;
200
201 struct GraphTreeModel
202 {
203         GObject parent;
204
205         graph_type* graph;
206 };
207
208 struct GraphTreeModelClass
209 {
210         GObjectClass parent_class;
211 };
212
213 #define GRAPH_TREE_MODEL( p ) ( reinterpret_cast<GraphTreeModel*>( p ) )
214
215 static GtkTreeModelFlags graph_tree_model_get_flags( GtkTreeModel* tree_model ){
216         return GTK_TREE_MODEL_ITERS_PERSIST;
217 }
218
219 static gint graph_tree_model_get_n_columns( GtkTreeModel* tree_model ){
220         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
221         GraphTreeModel* graph_tree_model = (GraphTreeModel*) tree_model;
222
223         return 2;
224 }
225
226 static const gint c_stamp = 0xabcdef;
227
228 inline graph_type::iterator graph_iterator_read_tree_iter( GtkTreeIter* iter ){
229         ASSERT_MESSAGE( iter != 0,  "tree model error" );
230         ASSERT_MESSAGE( iter->user_data != 0,  "tree model error" );
231         ASSERT_MESSAGE( iter->stamp == c_stamp,  "tree model error" );
232         return *reinterpret_cast<graph_type::iterator*>( &iter->user_data );
233 }
234
235 inline void graph_iterator_write_tree_iter( graph_type::iterator i, GtkTreeIter* iter ){
236         ASSERT_MESSAGE( iter != 0,  "tree model error" );
237         iter->stamp = c_stamp;
238         *reinterpret_cast<graph_type::iterator*>( &iter->user_data ) = i;
239         ASSERT_MESSAGE( iter->user_data != 0,  "tree model error" );
240 }
241
242 static GType graph_tree_model_get_column_type( GtkTreeModel *tree_model, gint index ){
243         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
244         GraphTreeModel *graph_tree_model = (GraphTreeModel *) tree_model;
245
246         return G_TYPE_POINTER;
247 }
248
249 static gboolean graph_tree_model_get_iter( GtkTreeModel* tree_model, GtkTreeIter* iter, GtkTreePath* path ){
250         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
251         gint* indices = gtk_tree_path_get_indices( path );
252         gint depth = gtk_tree_path_get_depth( path );
253
254         g_return_val_if_fail( depth > 0, FALSE );
255
256         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
257
258         if ( graph.empty() ) {
259                 return FALSE;
260         }
261
262         GtkTreeIter tmp;
263         GtkTreeIter* parent = 0;
264
265         for ( gint i = 0; i < depth; i++ )
266         {
267                 if ( !gtk_tree_model_iter_nth_child( tree_model, iter, parent, indices[i] ) ) {
268                         return FALSE;
269                 }
270                 tmp = *iter;
271                 parent = &tmp;
272         }
273
274         return TRUE;
275 }
276
277 static GtkTreePath* graph_tree_model_get_path( GtkTreeModel* tree_model, GtkTreeIter* iter ){
278         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
279         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
280         graph_type::iterator i = graph_iterator_read_tree_iter( iter );
281
282         GtkTreePath* path = ui::TreePath();
283
284         for ( std::size_t depth = ( *i ).first.get().size(); depth != 0; --depth )
285         {
286                 std::size_t index = 0;
287
288                 while ( i != graph.begin() && ( *i ).first.get().size() >= depth )
289                 {
290                         --i;
291                         if ( ( *i ).first.get().size() == depth ) {
292                                 ++index;
293                         }
294                 }
295
296                 gtk_tree_path_prepend_index( path, index );
297         }
298
299         return path;
300 }
301
302
303 static void graph_tree_model_get_value( GtkTreeModel *tree_model, GtkTreeIter  *iter, gint column, GValue *value ){
304         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
305         ASSERT_MESSAGE( column == 0 || column == 1, "tree model error" );
306
307         graph_type::iterator i = graph_iterator_read_tree_iter( iter );
308
309         g_value_init( value, G_TYPE_POINTER );
310
311         if ( column == 0 ) {
312                 g_value_set_pointer( value, reinterpret_cast<gpointer>( ( *i ).first.get().top() ) );
313         }
314         else{
315                 g_value_set_pointer( value, reinterpret_cast<gpointer>( Instance_getSelectable( *( *i ).second ) ) );
316         }
317 }
318
319 static gboolean graph_tree_model_iter_next( GtkTreeModel  *tree_model, GtkTreeIter   *iter ){
320         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
321         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
322         graph_type::iterator i = graph_iterator_read_tree_iter( iter );
323         std::size_t depth = ( *i ).first.get().size();
324
325         ++i;
326
327         while ( i != graph.end() && ( *i ).first.get().size() > depth )
328         {
329                 ++i;
330         }
331
332         if ( i == graph.end() || ( *i ).first.get().size() != depth ) {
333                 return FALSE;
334         }
335
336         graph_iterator_write_tree_iter( i, iter );
337
338         return TRUE;
339 }
340
341 static gboolean graph_tree_model_iter_children( GtkTreeModel *tree_model, GtkTreeIter  *iter, GtkTreeIter  *parent ){
342         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
343         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
344         graph_type::iterator i = ( parent == 0 ) ? graph.begin() : graph_iterator_read_tree_iter( parent );
345         std::size_t depth = ( parent == 0 ) ? 1 : ( *i ).first.get().size() + 1;
346
347         if ( parent != 0 ) {
348                 ++i;
349         }
350
351         if ( i != graph.end() && ( *i ).first.get().size() == depth ) {
352                 graph_iterator_write_tree_iter( i, iter );
353                 return TRUE;
354         }
355
356         return FALSE;
357 }
358
359 static gboolean graph_tree_model_iter_has_child( GtkTreeModel *tree_model, GtkTreeIter  *iter ){
360         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
361         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
362         graph_type::iterator i = graph_iterator_read_tree_iter( iter );
363         std::size_t depth = ( *i ).first.get().size() + 1;
364
365         return ++i != graph.end() && ( *i ).first.get().size() == depth;
366 }
367
368 static gint graph_tree_model_iter_n_children( GtkTreeModel *tree_model, GtkTreeIter *parent ){
369         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
370         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
371         graph_type::iterator i = ( parent == 0 ) ? graph.begin() : graph_iterator_read_tree_iter( parent );
372         std::size_t depth = ( parent == 0 ) ? 1 : ( *i ).first.get().size() + 1;
373
374         if ( parent != 0 ) {
375                 ++i;
376         }
377
378         gint count = 0;
379         while ( i != graph.end() && ( *i ).first.get().size() >= depth )
380         {
381                 ++count;
382                 ++i;
383         }
384
385         return count;
386 }
387
388 static gboolean graph_tree_model_iter_nth_child( GtkTreeModel *tree_model, GtkTreeIter  *iter, GtkTreeIter  *parent, gint n ){
389         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
390         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
391         graph_type::iterator i = ( parent == 0 ) ? graph.begin() : graph_iterator_read_tree_iter( parent );
392         std::size_t depth = ( parent == 0 ) ? 1 : ( *i ).first.get().size() + 1;
393
394         if ( parent != 0 ) {
395                 ++i;
396         }
397
398         while ( i != graph.end() && ( *i ).first.get().size() >= depth )
399         {
400                 if ( ( *i ).first.get().size() == depth && n-- == 0 ) {
401                         graph_iterator_write_tree_iter( i, iter );
402                         return TRUE;
403                 }
404                 ++i;
405         }
406
407         return FALSE;
408 }
409
410 static gboolean graph_tree_model_iter_parent( GtkTreeModel *tree_model, GtkTreeIter  *iter, GtkTreeIter  *child ){
411         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
412         graph_type& graph = *GRAPH_TREE_MODEL( tree_model )->graph;
413         graph_type::iterator i = graph_iterator_read_tree_iter( child );
414         std::size_t depth = ( *i ).first.get().size();
415         if ( depth == 1 ) {
416                 return FALSE;
417         }
418         else
419         {
420                 do
421                 {
422                         --i;
423                 }
424                 while ( ( *i ).first.get().size() >= depth );
425                 graph_iterator_write_tree_iter( i, iter );
426                 return TRUE;
427         }
428 }
429
430 static GObjectClass *g_parent_class = 0;
431
432 static void graph_tree_model_init( GraphTreeModel *graph_tree_model ){
433         graph_tree_model->graph = 0;
434 }
435
436 static void graph_tree_model_finalize( GObject* object ){
437         GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( object );
438
439         /* must chain up */
440         ( *g_parent_class->finalize )( object );
441 }
442
443 static void graph_tree_model_class_init( GraphTreeModelClass *class_ ){
444         GObjectClass *object_class;
445
446         g_parent_class = (GObjectClass*)g_type_class_peek_parent( class_ );
447         object_class = (GObjectClass *) class_;
448
449         object_class->finalize = graph_tree_model_finalize;
450 }
451
452 static void graph_tree_model_tree_model_init( GtkTreeModelIface *iface ){
453         iface->get_flags = graph_tree_model_get_flags;
454         iface->get_n_columns = graph_tree_model_get_n_columns;
455         iface->get_column_type = graph_tree_model_get_column_type;
456         iface->get_iter = graph_tree_model_get_iter;
457         iface->get_path = graph_tree_model_get_path;
458         iface->get_value = graph_tree_model_get_value;
459         iface->iter_next = graph_tree_model_iter_next;
460         iface->iter_children = graph_tree_model_iter_children;
461         iface->iter_has_child = graph_tree_model_iter_has_child;
462         iface->iter_n_children = graph_tree_model_iter_n_children;
463         iface->iter_nth_child = graph_tree_model_iter_nth_child;
464         iface->iter_parent = graph_tree_model_iter_parent;
465 }
466
467 static gboolean graph_tree_model_row_draggable( GtkTreeDragSource *drag_source, GtkTreePath *path ){
468 #ifdef _DEBUG
469         gint depth = gtk_tree_path_get_depth( path );
470 #endif
471         return gtk_tree_path_get_depth( path ) > 1;
472 }
473
474 static gboolean graph_tree_model_drag_data_delete( GtkTreeDragSource *drag_source, GtkTreePath *path ){
475         GtkTreeIter iter;
476
477         if ( gtk_tree_model_get_iter( GTK_TREE_MODEL( drag_source ), &iter, path ) ) {
478                 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
479                 Path_deleteTop( ( *i ).first );
480                 return TRUE;
481         }
482         else
483         {
484                 return FALSE;
485         }
486 }
487
488 static gboolean graph_tree_model_drag_data_get( GtkTreeDragSource *drag_source, GtkTreePath *path, GtkSelectionData *selection_data ){
489         if ( gtk_tree_set_row_drag_data( selection_data, GTK_TREE_MODEL( drag_source ), path ) ) {
490                 return TRUE;
491         }
492         else
493         {
494                 /* FIXME handle text targets at least. */
495         }
496
497         return FALSE;
498 }
499
500 static void graph_tree_model_drag_source_init( GtkTreeDragSourceIface *iface ){
501         iface->row_draggable = graph_tree_model_row_draggable;
502         iface->drag_data_delete = graph_tree_model_drag_data_delete;
503         iface->drag_data_get = graph_tree_model_drag_data_get;
504 }
505
506 static gboolean graph_tree_model_drag_data_received( GtkTreeDragDest *drag_dest, GtkTreePath *dest, GtkSelectionData *selection_data ){
507         GtkTreeModel *tree_model = GTK_TREE_MODEL( drag_dest );
508
509         GtkTreeModel *src_model = 0;
510         GtkTreePath *src_path = 0;
511         if ( gtk_tree_get_row_drag_data( selection_data, &src_model, &src_path )
512                  && src_model == tree_model ) {
513                 /* Copy the given row to a new position */
514                 GtkTreeIter iter;
515
516                 if ( gtk_tree_model_get_iter( src_model, &iter, src_path ) ) {
517                         int bleh = 0;
518                 }
519         }
520         else
521         {
522                 /* FIXME maybe add some data targets eventually, or handle text
523                  * targets in the simple case.
524                  */
525         }
526
527         return FALSE;
528 }
529
530 static gboolean graph_tree_model_row_drop_possible( GtkTreeDragDest *drag_dest, GtkTreePath *dest_path, GtkSelectionData *selection_data ){
531         gboolean retval = FALSE;
532
533         GtkTreeModel *src_model = 0;
534         GtkTreePath *src_path = 0;
535         if ( gtk_tree_get_row_drag_data( selection_data, &src_model, &src_path ) != FALSE ) {
536                 /* can only drag to ourselves */
537                 if ( src_model == GTK_TREE_MODEL( drag_dest ) ) {
538                         /* Can't drop into ourself. */
539                         if ( !gtk_tree_path_is_ancestor( src_path, dest_path ) ) {
540                                 /* Can't drop if dest_path's parent doesn't exist */
541                                 if ( gtk_tree_path_get_depth( dest_path ) > 1 ) {
542                                         GtkTreePath* tmp = gtk_tree_path_copy( dest_path );
543                                         gtk_tree_path_up( tmp );
544
545                                         GtkTreeIter iter;
546                                         retval = gtk_tree_model_get_iter( GTK_TREE_MODEL( drag_dest ), &iter, tmp );
547
548                                         gtk_tree_path_free( tmp );
549                                 }
550                         }
551                 }
552
553                 gtk_tree_path_free( src_path );
554         }
555
556         return retval;
557 }
558
559 static void graph_tree_model_drag_dest_init( GtkTreeDragDestIface *iface ){
560         iface->drag_data_received = graph_tree_model_drag_data_received;
561         iface->row_drop_possible = graph_tree_model_row_drop_possible;
562 }
563
564 GType graph_tree_model_get_type( void ){
565         static GType graph_tree_model_type = 0;
566
567         if ( !graph_tree_model_type ) {
568                 static const GTypeInfo graph_tree_model_info =
569                 {
570                         sizeof( GraphTreeModelClass ),
571                         0, /* base_init */
572                         0, /* base_finalize */
573                         (GClassInitFunc) graph_tree_model_class_init,
574                         0, /* class_finalize */
575                         0, /* class_data */
576                         sizeof( GraphTreeModel ),
577                         0,        /* n_preallocs */
578                         (GInstanceInitFunc) graph_tree_model_init
579                 };
580
581                 static const GInterfaceInfo tree_model_info =
582                 {
583                         (GInterfaceInitFunc) graph_tree_model_tree_model_init,
584                         0,
585                         0
586                 };
587
588                 static const GInterfaceInfo drag_source_info =
589                 {
590                         (GInterfaceInitFunc) graph_tree_model_drag_source_init,
591                         0,
592                         0
593                 };
594
595                 static const GInterfaceInfo drag_dest_info =
596                 {
597                         (GInterfaceInitFunc) graph_tree_model_drag_dest_init,
598                         0,
599                         0
600                 };
601
602                 graph_tree_model_type = g_type_register_static( G_TYPE_OBJECT, "GraphTreeModel",
603                                                                                                                 &graph_tree_model_info, (GTypeFlags)0 );
604
605                 g_type_add_interface_static( graph_tree_model_type,
606                                                                          GTK_TYPE_TREE_MODEL,
607                                                                          &tree_model_info );
608                 g_type_add_interface_static( graph_tree_model_type,
609                                                                          GTK_TYPE_TREE_DRAG_SOURCE,
610                                                                          &drag_source_info );
611                 g_type_add_interface_static( graph_tree_model_type,
612                                                                          GTK_TYPE_TREE_DRAG_DEST,
613                                                                          &drag_dest_info );
614         }
615
616         return graph_tree_model_type;
617 }
618
619 GraphTreeModel* graph_tree_model_new(){
620         GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( g_object_new( graph_tree_model_get_type(), 0 ) );
621
622         graph_tree_model->graph = new graph_type;
623
624         return graph_tree_model;
625 }
626
627 void graph_tree_model_delete( GraphTreeModel* model ){
628         delete model->graph;
629         g_object_unref( G_OBJECT( model ) );
630 }
631
632
633 class TempNameable : public Nameable
634 {
635 const char* m_name;
636 public:
637 TempNameable( const char* name ) : m_name( name ){
638 }
639 const char* name() const {
640         return m_name;
641 }
642 void attach( const NameCallback& callback ){
643 }
644 void detach( const NameCallback& callback ){
645 }
646 };
647
648 void node_attach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
649         if ( &node != 0 ) {
650                 Nameable* nameable = Node_getNameable( node );
651                 if ( nameable != 0 ) {
652                         nameable->attach( callback );
653                 }
654         }
655 }
656 void node_detach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
657         if ( &node != 0 ) {
658                 Nameable* nameable = Node_getNameable( node );
659                 if ( nameable != 0 ) {
660                         nameable->detach( callback );
661                 }
662         }
663 }
664
665 GraphTreeModel* scene_graph_get_tree_model(); // temp hack
666
667 void graph_tree_model_row_inserted( GraphTreeModel* model, graph_type::iterator i ){
668         GtkTreeIter iter;
669         graph_iterator_write_tree_iter( i, &iter );
670
671         GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
672
673         gint depth = gtk_tree_path_get_depth( tree_path );
674         gint* indices = gtk_tree_path_get_indices( tree_path );
675
676         gtk_tree_model_row_inserted( GTK_TREE_MODEL( model ), tree_path, &iter );
677
678         gtk_tree_path_free( tree_path );
679 }
680
681 void graph_tree_model_row_deleted( GraphTreeModel* model, graph_type::iterator i ){
682         GtkTreeIter iter;
683         graph_iterator_write_tree_iter( i, &iter );
684
685         GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
686
687         gtk_tree_model_row_deleted( GTK_TREE_MODEL( model ), tree_path );
688
689         gtk_tree_path_free( tree_path );
690 }
691
692 #include "generic/referencecounted.h"
693
694 void graph_tree_model_set_name( const scene::Instance& instance, const char* name ){
695         GraphTreeModel* model = scene_graph_get_tree_model();
696
697         if ( string_empty( name ) ) { // hack!
698                 graph_type::iterator i = model->graph->find( PathConstReference( instance.path() ) );
699                 ASSERT_MESSAGE( i != model->graph->end(), "ERROR" );
700
701                 graph_tree_model_row_deleted( model, i );
702
703                 model->graph->erase( i );
704         }
705         else
706         {
707                 graph_type::iterator i = model->graph->insert( graph_type::value_type( PathConstReference( instance.path() ), &const_cast<scene::Instance&>( instance ) ) ).first;
708
709                 graph_tree_model_row_inserted( model, i );
710         }
711 }
712
713 void graph_tree_model_insert( GraphTreeModel* model, const scene::Instance& instance ){
714         graph_type::iterator i = model->graph->insert( graph_type::value_type( PathConstReference( instance.path() ), &const_cast<scene::Instance&>( instance ) ) ).first;
715
716         graph_tree_model_row_inserted( model, i );
717
718         node_attach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
719 }
720
721 void graph_tree_model_erase( GraphTreeModel* model, const scene::Instance& instance ){
722         node_detach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
723
724         graph_type::iterator i = model->graph->find( PathConstReference( instance.path() ) );
725         ASSERT_MESSAGE( i != model->graph->end(), "ERROR" );
726
727         graph_tree_model_row_deleted( model, i );
728
729         model->graph->erase( i );
730 }
731
732 #elif 1
733
734 class GraphTreeNode;
735 void graph_tree_model_row_changed( GraphTreeNode& node );
736
737 class GraphTreeNode
738 {
739 typedef std::map<std::pair<std::string, scene::Node*>, GraphTreeNode*> ChildNodes;
740 ChildNodes m_childnodes;
741 public:
742 Reference<scene::Instance> m_instance;
743 GraphTreeNode* m_parent;
744
745 typedef ChildNodes::iterator iterator;
746 typedef ChildNodes::key_type key_type;
747 typedef ChildNodes::value_type value_type;
748 typedef ChildNodes::size_type size_type;
749
750 GraphTreeNode( scene::Instance& instance ) : m_instance( instance ), m_parent( 0 ){
751         m_instance.get().setChildSelectedChangedCallback( RowChangedCaller( *this ) );
752 }
753 ~GraphTreeNode(){
754         m_instance.get().setChildSelectedChangedCallback( Callback() );
755         ASSERT_MESSAGE( empty(), "GraphTreeNode::~GraphTreeNode: memory leak" );
756 }
757
758 iterator begin(){
759         return m_childnodes.begin();
760 }
761 iterator end(){
762         return m_childnodes.end();
763 }
764
765 size_type size() const {
766         return m_childnodes.size();
767 }
768 bool empty() const {
769         return m_childnodes.empty();
770 }
771
772 iterator insert( const value_type& value ){
773         iterator i = m_childnodes.insert( value ).first;
774         ( *i ).second->m_parent = this;
775         return i;
776 }
777 void erase( iterator i ){
778         m_childnodes.erase( i );
779 }
780 iterator find( const key_type& key ){
781         return m_childnodes.find( key );
782 }
783
784 void swap( GraphTreeNode& other ){
785         std::swap( m_parent, other.m_parent );
786         std::swap( m_childnodes, other.m_childnodes );
787         std::swap( m_instance, other.m_instance );
788 }
789
790 void rowChanged(){
791         graph_tree_model_row_changed( *this );
792 }
793 typedef MemberCaller<GraphTreeNode, &GraphTreeNode::rowChanged> RowChangedCaller;
794 };
795
796 struct GraphTreeModel
797 {
798         GObject parent;
799
800         GraphTreeNode* m_graph;
801 };
802
803 struct GraphTreeModelClass
804 {
805         GObjectClass parent_class;
806 };
807
808 #define GRAPH_TREE_MODEL( p ) ( reinterpret_cast<GraphTreeModel*>( p ) )
809
810 static GtkTreeModelFlags graph_tree_model_get_flags( GtkTreeModel* tree_model ){
811         return GTK_TREE_MODEL_ITERS_PERSIST;
812 }
813
814 static gint graph_tree_model_get_n_columns( GtkTreeModel* tree_model ){
815         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
816         //GraphTreeModel* graph_tree_model = (GraphTreeModel*) tree_model;
817
818         return 2;
819 }
820
821 static const gint c_stamp = 0xabcdef;
822
823 inline GraphTreeNode::iterator graph_iterator_read_tree_iter( GtkTreeIter* iter ){
824         ASSERT_MESSAGE( iter != 0,  "tree model error" );
825         ASSERT_MESSAGE( iter->user_data != 0,  "tree model error" );
826         ASSERT_MESSAGE( iter->stamp == c_stamp,  "tree model error" );
827         return *reinterpret_cast<GraphTreeNode::iterator*>( &iter->user_data );
828 }
829
830 inline void graph_iterator_write_tree_iter( GraphTreeNode::iterator i, GtkTreeIter* iter ){
831         ASSERT_MESSAGE( iter != 0,  "tree model error" );
832         iter->stamp = c_stamp;
833         *reinterpret_cast<GraphTreeNode::iterator*>( &iter->user_data ) = i;
834         ASSERT_MESSAGE( iter->user_data != 0,  "tree model error" );
835 }
836
837 static GType graph_tree_model_get_column_type( GtkTreeModel *tree_model, gint index ){
838         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
839         //GraphTreeModel *graph_tree_model = (GraphTreeModel *) tree_model;
840
841         return G_TYPE_POINTER;
842 }
843
844 static gboolean graph_tree_model_get_iter( GtkTreeModel* tree_model, GtkTreeIter* iter, GtkTreePath* path ){
845         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
846         gint* indices = gtk_tree_path_get_indices( path );
847         gint depth = gtk_tree_path_get_depth( path );
848
849         g_return_val_if_fail( depth > 0, FALSE );
850
851         GraphTreeNode* graph = GRAPH_TREE_MODEL( tree_model )->m_graph;
852
853         if ( graph->empty() ) {
854                 return FALSE;
855         }
856
857         GtkTreeIter tmp;
858         GtkTreeIter* parent = 0;
859
860         for ( gint i = 0; i < depth; i++ )
861         {
862                 if ( !gtk_tree_model_iter_nth_child( tree_model, iter, parent, indices[i] ) ) {
863                         return FALSE;
864                 }
865                 tmp = *iter;
866                 parent = &tmp;
867         }
868
869         return TRUE;
870 }
871
872 static GtkTreePath* graph_tree_model_get_path( GtkTreeModel* tree_model, GtkTreeIter* iter ){
873         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
874         GraphTreeNode* graph = GRAPH_TREE_MODEL( tree_model )->m_graph;
875
876         GtkTreePath* path = ui::TreePath();
877
878         for ( GraphTreeNode* node = ( *graph_iterator_read_tree_iter( iter ) ).second; node != graph; node = node->m_parent )
879         {
880                 std::size_t index = 0;
881                 for ( GraphTreeNode::iterator i = node->m_parent->begin(); i != node->m_parent->end(); ++i, ++index )
882                 {
883                         if ( ( *i ).second == node ) {
884                                 gtk_tree_path_prepend_index( path, gint( index ) );
885                                 break;
886                         }
887                 }
888                 ASSERT_MESSAGE( index != node->m_parent->size(), "error resolving tree path" );
889         }
890
891         return path;
892 }
893
894
895 static void graph_tree_model_get_value( GtkTreeModel *tree_model, GtkTreeIter  *iter, gint column, GValue *value ){
896         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
897         ASSERT_MESSAGE( column == 0 || column == 1, "tree model error" );
898
899         GraphTreeNode::iterator i = graph_iterator_read_tree_iter( iter );
900
901         g_value_init( value, G_TYPE_POINTER );
902
903         if ( column == 0 ) {
904                 g_value_set_pointer( value, reinterpret_cast<gpointer>( ( *i ).first.second ) );
905         }
906         else
907         {
908                 g_value_set_pointer( value, reinterpret_cast<gpointer>( &( *i ).second->m_instance.get() ) );
909         }
910 }
911
912 static gboolean graph_tree_model_iter_next( GtkTreeModel  *tree_model, GtkTreeIter   *iter ){
913         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
914         GraphTreeNode::iterator i = graph_iterator_read_tree_iter( iter );
915         GraphTreeNode& parent = *( *i ).second->m_parent;
916
917         ASSERT_MESSAGE( i != parent.end(), "RUNTIME ERROR" );
918
919         if ( ++i == parent.end() ) {
920                 return FALSE;
921         }
922
923         graph_iterator_write_tree_iter( i, iter );
924
925         return TRUE;
926 }
927
928 static gboolean graph_tree_model_iter_children( GtkTreeModel *tree_model, GtkTreeIter  *iter, GtkTreeIter  *parent ){
929         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
930         GraphTreeNode& node = ( parent == 0 ) ? *GRAPH_TREE_MODEL( tree_model )->m_graph : *( *graph_iterator_read_tree_iter( parent ) ).second;
931         if ( !node.empty() ) {
932                 graph_iterator_write_tree_iter( node.begin(), iter );
933                 return TRUE;
934         }
935
936         return FALSE;
937 }
938
939 static gboolean graph_tree_model_iter_has_child( GtkTreeModel *tree_model, GtkTreeIter  *iter ){
940         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
941         GraphTreeNode& node = *( *graph_iterator_read_tree_iter( iter ) ).second;
942         return !node.empty();
943 }
944
945 static gint graph_tree_model_iter_n_children( GtkTreeModel *tree_model, GtkTreeIter *parent ){
946         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
947         GraphTreeNode& node = ( parent == 0 ) ? *GRAPH_TREE_MODEL( tree_model )->m_graph : *( *graph_iterator_read_tree_iter( parent ) ).second;
948         return static_cast<gint>( node.size() );
949 }
950
951 static gboolean graph_tree_model_iter_nth_child( GtkTreeModel *tree_model, GtkTreeIter  *iter, GtkTreeIter  *parent, gint n ){
952         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
953         GraphTreeNode& node = ( parent == 0 ) ? *GRAPH_TREE_MODEL( tree_model )->m_graph : *( *graph_iterator_read_tree_iter( parent ) ).second;
954         if ( static_cast<std::size_t>( n ) < node.size() ) {
955                 GraphTreeNode::iterator i = node.begin();
956                 std::advance( i, n );
957                 graph_iterator_write_tree_iter( i, iter );
958                 return TRUE;
959         }
960
961         return FALSE;
962 }
963
964 static gboolean graph_tree_model_iter_parent( GtkTreeModel *tree_model, GtkTreeIter  *iter, GtkTreeIter  *child ){
965         ASSERT_MESSAGE( tree_model != 0, "RUNTIME ERROR" );
966         GraphTreeNode& node = *( *graph_iterator_read_tree_iter( child ) ).second;
967         if ( node.m_parent != GRAPH_TREE_MODEL( tree_model )->m_graph ) {
968                 GraphTreeNode& parentParent = *node.m_parent->m_parent;
969                 for ( GraphTreeNode::iterator i = parentParent.begin(); i != parentParent.end(); ++i )
970                 {
971                         if ( ( *i ).second == node.m_parent ) {
972                                 graph_iterator_write_tree_iter( i, iter );
973                                 return TRUE;
974                         }
975                 }
976         }
977         return FALSE;
978 }
979
980 static GObjectClass *g_parent_class = 0;
981
982 namespace
983 {
984 scene::Node* g_null_node = 0;
985 }
986
987 class NullInstance : public scene::Instance
988 {
989 public:
990 NullInstance() : scene::Instance( scene::Path( makeReference( *g_null_node ) ), 0, 0, Static<InstanceTypeCastTable>::instance() ){
991 }
992 };
993
994 namespace
995 {
996 NullInstance g_null_instance;
997 }
998
999 static void graph_tree_model_init( GraphTreeModel *graph_tree_model ){
1000         graph_tree_model->m_graph = new GraphTreeNode( g_null_instance );
1001 }
1002
1003 static void graph_tree_model_finalize( GObject* object ){
1004         GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( object );
1005
1006         delete graph_tree_model->m_graph;
1007
1008         /* must chain up */
1009         ( *g_parent_class->finalize )( object );
1010 }
1011
1012 static void graph_tree_model_class_init( GraphTreeModelClass *class_ ){
1013         GObjectClass *object_class;
1014
1015         g_parent_class = (GObjectClass*)g_type_class_peek_parent( class_ );
1016         object_class = (GObjectClass *) class_;
1017
1018         object_class->finalize = graph_tree_model_finalize;
1019 }
1020
1021 static void graph_tree_model_tree_model_init( GtkTreeModelIface *iface ){
1022         iface->get_flags = graph_tree_model_get_flags;
1023         iface->get_n_columns = graph_tree_model_get_n_columns;
1024         iface->get_column_type = graph_tree_model_get_column_type;
1025         iface->get_iter = graph_tree_model_get_iter;
1026         iface->get_path = graph_tree_model_get_path;
1027         iface->get_value = graph_tree_model_get_value;
1028         iface->iter_next = graph_tree_model_iter_next;
1029         iface->iter_children = graph_tree_model_iter_children;
1030         iface->iter_has_child = graph_tree_model_iter_has_child;
1031         iface->iter_n_children = graph_tree_model_iter_n_children;
1032         iface->iter_nth_child = graph_tree_model_iter_nth_child;
1033         iface->iter_parent = graph_tree_model_iter_parent;
1034 }
1035
1036 GType graph_tree_model_get_type( void ){
1037         static GType graph_tree_model_type = 0;
1038
1039         if ( !graph_tree_model_type ) {
1040                 static const GTypeInfo graph_tree_model_info =
1041                 {
1042                         sizeof( GraphTreeModelClass ),
1043                         0, /* base_init */
1044                         0, /* base_finalize */
1045                         (GClassInitFunc) graph_tree_model_class_init,
1046                         0, /* class_finalize */
1047                         0, /* class_data */
1048                         sizeof( GraphTreeModel ),
1049                         0,        /* n_preallocs */
1050                         (GInstanceInitFunc) graph_tree_model_init,
1051                         0
1052                 };
1053
1054                 static const GInterfaceInfo tree_model_info =
1055                 {
1056                         (GInterfaceInitFunc) graph_tree_model_tree_model_init,
1057                         0,
1058                         0
1059                 };
1060
1061                 graph_tree_model_type = g_type_register_static( G_TYPE_OBJECT, "GraphTreeModel",
1062                                                                                                                 &graph_tree_model_info, (GTypeFlags)0 );
1063
1064                 g_type_add_interface_static( graph_tree_model_type,
1065                                                                          GTK_TYPE_TREE_MODEL,
1066                                                                          &tree_model_info );
1067         }
1068
1069         return graph_tree_model_type;
1070 }
1071
1072 GraphTreeModel* graph_tree_model_new(){
1073         GraphTreeModel* graph_tree_model = GRAPH_TREE_MODEL( g_object_new( graph_tree_model_get_type(), 0 ) );
1074
1075         return graph_tree_model;
1076 }
1077
1078 void graph_tree_model_delete( GraphTreeModel* model ){
1079         g_object_unref( G_OBJECT( model ) );
1080 }
1081
1082 void graph_tree_model_row_changed( GraphTreeModel* model, GraphTreeNode::iterator i ){
1083         GtkTreeIter iter;
1084         graph_iterator_write_tree_iter( i, &iter );
1085
1086         GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
1087
1088         gtk_tree_model_row_changed( GTK_TREE_MODEL( model ), tree_path, &iter );
1089
1090         gtk_tree_path_free( tree_path );
1091 }
1092
1093 void graph_tree_model_row_inserted( GraphTreeModel* model, GraphTreeNode::iterator i ){
1094         GtkTreeIter iter;
1095         graph_iterator_write_tree_iter( i, &iter );
1096
1097         GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
1098
1099         gtk_tree_model_row_inserted( GTK_TREE_MODEL( model ), tree_path, &iter );
1100
1101         gtk_tree_path_free( tree_path );
1102 }
1103
1104 void graph_tree_model_row_deleted( GraphTreeModel* model, GraphTreeNode::iterator i ){
1105         GtkTreeIter iter;
1106         graph_iterator_write_tree_iter( i, &iter );
1107
1108         GtkTreePath* tree_path = graph_tree_model_get_path( GTK_TREE_MODEL( model ), &iter );
1109
1110         gtk_tree_model_row_deleted( GTK_TREE_MODEL( model ), tree_path );
1111
1112         gtk_tree_path_free( tree_path );
1113 }
1114
1115 void graph_tree_model_row_inserted( GraphTreeModel& model, GraphTreeNode::iterator i ){
1116         graph_tree_model_row_inserted( &model, i );
1117 }
1118
1119 void graph_tree_model_row_deleted( GraphTreeModel& model, GraphTreeNode::iterator i ){
1120         graph_tree_model_row_deleted( &model, i );
1121 }
1122
1123 const char* node_get_name( scene::Node& node );
1124
1125 const char* node_get_name_safe( scene::Node& node ){
1126         if ( &node == 0 ) {
1127                 return "";
1128         }
1129         return node_get_name( node );
1130 }
1131
1132 GraphTreeNode* graph_tree_model_find_parent( GraphTreeModel* model, const scene::Path& path ){
1133         GraphTreeNode* parent = model->m_graph;
1134         for ( scene::Path::const_iterator i = path.begin(); i != path.end() - 1; ++i )
1135         {
1136                 GraphTreeNode::iterator child = parent->find( GraphTreeNode::key_type( node_get_name_safe( ( *i ).get() ), ( *i ).get_pointer() ) );
1137                 ASSERT_MESSAGE( child != parent->end(), "ERROR" );
1138                 parent = ( *child ).second;
1139         }
1140         return parent;
1141 }
1142
1143 void node_attach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
1144         if ( &node != 0 ) {
1145                 Nameable* nameable = Node_getNameable( node );
1146                 if ( nameable != 0 ) {
1147                         nameable->attach( callback );
1148                 }
1149         }
1150 }
1151 void node_detach_name_changed_callback( scene::Node& node, const NameCallback& callback ){
1152         if ( &node != 0 ) {
1153                 Nameable* nameable = Node_getNameable( node );
1154                 if ( nameable != 0 ) {
1155                         nameable->detach( callback );
1156                 }
1157         }
1158 }
1159
1160 GraphTreeModel* scene_graph_get_tree_model(); // temp hack
1161
1162 void graph_tree_node_foreach_pre( GraphTreeNode::iterator root, const Callback1<GraphTreeNode::iterator>& callback ){
1163         callback( root );
1164         for ( GraphTreeNode::iterator i = ( *root ).second->begin(); i != ( *root ).second->end(); ++i )
1165         {
1166                 graph_tree_node_foreach_pre( i, callback );
1167         }
1168 }
1169
1170 void graph_tree_node_foreach_post( GraphTreeNode::iterator root, const Callback1<GraphTreeNode::iterator>& callback ){
1171         for ( GraphTreeNode::iterator i = ( *root ).second->begin(); i != ( *root ).second->end(); ++i )
1172         {
1173                 graph_tree_node_foreach_post( i, callback );
1174         }
1175         callback( root );
1176 }
1177
1178 void graph_tree_model_row_changed( GraphTreeNode& node ){
1179         GraphTreeModel* model = scene_graph_get_tree_model();
1180         const scene::Instance& instance = node.m_instance.get();
1181
1182         GraphTreeNode::iterator i = node.m_parent->find( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ) );
1183
1184         graph_tree_model_row_changed( model, i );
1185 }
1186
1187 void graph_tree_model_set_name( const scene::Instance& instance, const char* name ){
1188         GraphTreeModel* model = scene_graph_get_tree_model();
1189         GraphTreeNode* parent = graph_tree_model_find_parent( model, instance.path() );
1190
1191         GraphTreeNode::iterator oldNode = parent->find( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ) );
1192         graph_tree_node_foreach_post( oldNode, ReferenceCaller1<GraphTreeModel, GraphTreeNode::iterator, graph_tree_model_row_deleted>( *model ) );
1193         GraphTreeNode* node( ( *oldNode ).second );
1194         parent->erase( oldNode );
1195
1196         GraphTreeNode::iterator newNode = parent->insert( GraphTreeNode::value_type( GraphTreeNode::key_type( name, &instance.path().top().get() ), node ) );
1197         graph_tree_node_foreach_pre( newNode, ReferenceCaller1<GraphTreeModel, GraphTreeNode::iterator, graph_tree_model_row_inserted>( *model ) );
1198 }
1199
1200 void graph_tree_model_insert( GraphTreeModel* model, const scene::Instance& instance ){
1201         GraphTreeNode* parent = graph_tree_model_find_parent( model, instance.path() );
1202
1203         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 ) ) ) );
1204
1205         graph_tree_model_row_inserted( model, i );
1206
1207         node_attach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
1208 }
1209
1210 void graph_tree_model_erase( GraphTreeModel* model, const scene::Instance& instance ){
1211         node_detach_name_changed_callback( instance.path().top(), ConstReferenceCaller1<scene::Instance, const char*, graph_tree_model_set_name>( instance ) );
1212
1213         GraphTreeNode* parent = graph_tree_model_find_parent( model, instance.path() );
1214
1215         GraphTreeNode::iterator i = parent->find( GraphTreeNode::key_type( node_get_name_safe( instance.path().top().get() ), instance.path().top().get_pointer() ) );
1216
1217         graph_tree_model_row_deleted( model, i );
1218
1219         GraphTreeNode* node( ( *i ).second );
1220         parent->erase( i );
1221         delete node;
1222 }
1223
1224
1225
1226 #endif
1227
1228
1229
1230
1231 #if 0
1232 class TestGraphTreeModel
1233 {
1234 public:
1235 TestGraphTreeModel(){
1236         gtk_init( 0, 0 );
1237
1238         graph_type graph;
1239
1240         scene::Node* root = *(scene::Node*)0xa0000000;
1241         scene::Node* node1 = (scene::Node*)0xa0000001;
1242         scene::Node* node2 = (scene::Node*)0xa0000002;
1243         scene::Node* node3 = (scene::Node*)0xa0000003;
1244         scene::Node* node4 = (scene::Node*)0xa0000004;
1245         scene::Instance* instance = (scene::Instance*)0xaaaaaaaa;
1246
1247         scene::Path rootpath( root );
1248
1249         graph.insert( graph_type::value_type( rootpath, instance ) );
1250
1251         rootpath.push( node1 );
1252         graph.insert( graph_type::value_type( rootpath, instance ) );
1253         rootpath.pop();
1254
1255         rootpath.push( node2 );
1256         graph.insert( graph_type::value_type( rootpath, instance ) );
1257         rootpath.push( node3 );
1258         graph.insert( graph_type::value_type( rootpath, instance ) );
1259         rootpath.pop();
1260         rootpath.push( node4 );
1261         graph.insert( graph_type::value_type( rootpath, instance ) );
1262         rootpath.pop();
1263         rootpath.pop();
1264
1265         GtkTreeModel* model = GTK_TREE_MODEL( graph_tree_model_new( &graph ) );
1266
1267         {
1268                 gint n_columns = gtk_tree_model_get_n_columns( model );
1269                 ASSERT_MESSAGE( n_columns == 2, "test failed!" );
1270         }
1271
1272         {
1273                 GType type = gtk_tree_model_get_column_type( model, 0 );
1274                 ASSERT_MESSAGE( type == G_TYPE_POINTER, "test failed!" );
1275         }
1276
1277         {
1278                 GType type = gtk_tree_model_get_column_type( model, 1 );
1279                 ASSERT_MESSAGE( type == G_TYPE_POINTER, "test failed!" );
1280         }
1281
1282
1283         {
1284                 GtkTreeIter iter;
1285                 gtk_tree_model_get_iter_first( model, &iter );
1286
1287                 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
1288                 ASSERT_MESSAGE( ( *i ).first.get().size() == 2 && ( *i ).first.get().top() == node1, "test failed!" );
1289         }
1290
1291         {
1292                 GtkTreeIter iter;
1293                 gtk_tree_model_get_iter_first( model, &iter );
1294
1295                 ASSERT_MESSAGE( gtk_tree_model_iter_has_child( model, &iter ) == FALSE, "test failed!" );
1296
1297                 ASSERT_MESSAGE( gtk_tree_model_iter_n_children( model, &iter ) == 0, "test failed!" );
1298
1299                 gtk_tree_model_iter_next( model, &iter );
1300
1301                 ASSERT_MESSAGE( gtk_tree_model_iter_has_child( model, &iter ) != FALSE, "test failed!" );
1302
1303                 ASSERT_MESSAGE( gtk_tree_model_iter_n_children( model, &iter ) == 2, "test failed!" );
1304
1305                 {
1306                         GtkTreeIter child;
1307                         gtk_tree_model_iter_nth_child( model, &child, &iter, 0 );
1308
1309                         scene::Node* test;
1310                         gtk_tree_model_get_value( model, &child, 0, (GValue*)&test );
1311                         ASSERT_MESSAGE( test == node3, "test failed!" );
1312
1313                         {
1314                                 GtkTreeIter parent;
1315                                 gtk_tree_model_iter_parent( model, &parent, &child );
1316
1317                                 scene::Node* test;
1318                                 gtk_tree_model_get_value( model, &parent, 0, (GValue*)&test );
1319                                 ASSERT_MESSAGE( test == node2, "test failed!" );
1320                         }
1321                 }
1322
1323                 {
1324                         GtkTreeIter child;
1325                         gtk_tree_model_iter_nth_child( model, &child, &iter, 1 );
1326
1327                         scene::Node* test;
1328                         gtk_tree_model_get_value( model, &child, 0, (GValue*)&test );
1329                         ASSERT_MESSAGE( test == node4, "test failed!" );
1330                 }
1331         }
1332
1333         {
1334                 GtkTreeIter iter;
1335                 std::size_t count = 0;
1336                 for ( gboolean good = gtk_tree_model_get_iter_first( model, &iter ); good; good = gtk_tree_model_iter_next( model, &iter ) )
1337                 {
1338                         scene::Node* test;
1339                         gtk_tree_model_get_value( model, &iter, 0, (GValue*)&test );
1340
1341                         ASSERT_MESSAGE( ( count == 0 && test == node1 ) || ( count == 1 && test == node2 ), "test failed!" );
1342                         ++count;
1343                 }
1344
1345                 ASSERT_MESSAGE( count == 2, "test failed!" );
1346
1347         }
1348
1349         {
1350                 GtkTreeIter iter;
1351                 gtk_tree_model_get_iter_first( model, &iter );
1352
1353                 scene::Node* test;
1354                 gtk_tree_model_get_value( model, &iter, 0, (GValue*)&test );
1355                 ASSERT_MESSAGE( test == node1, "test failed!" );
1356         }
1357
1358         {
1359                 GtkTreeIter iter;
1360                 GtkTreePath* path = ui::TreePath( "0" );
1361                 gtk_tree_model_get_iter( model, &iter, path );
1362                 gtk_tree_path_free( path );
1363
1364                 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
1365                 ASSERT_MESSAGE( ( *i ).first.get().size() == 2 && ( *i ).first.get().top() == node1, "test failed!" );
1366         }
1367
1368         {
1369                 GtkTreeIter iter;
1370                 GtkTreePath* path = ui::TreePath( "1" );
1371                 gtk_tree_model_get_iter( model, &iter, path );
1372                 gtk_tree_path_free( path );
1373
1374                 graph_type::iterator i = graph_iterator_read_tree_iter( &iter );
1375                 ASSERT_MESSAGE( ( *i ).first.get().size() == 2 && ( *i ).first.get().top() == node2, "test failed!" );
1376         }
1377
1378         {
1379                 GtkTreeIter iter;
1380                 graph_type::iterator i = graph.begin();
1381                 ++i;
1382                 graph_iterator_write_tree_iter( i, &iter );
1383
1384                 GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
1385
1386                 gint depth = gtk_tree_path_get_depth( path );
1387                 gint* indices = gtk_tree_path_get_indices( path );
1388
1389                 ASSERT_MESSAGE( depth == 1 && indices[0] == 0, "test failed!" );
1390
1391                 gtk_tree_path_free( path );
1392         }
1393
1394         {
1395                 GtkTreeIter iter;
1396                 graph_type::iterator i = graph.begin();
1397                 ++i;
1398                 ++i;
1399                 graph_iterator_write_tree_iter( i, &iter );
1400
1401                 GtkTreePath* path = gtk_tree_model_get_path( model, &iter );
1402
1403                 gint depth = gtk_tree_path_get_depth( path );
1404                 gint* indices = gtk_tree_path_get_indices( path );
1405
1406                 ASSERT_MESSAGE( depth == 1 && indices[0] == 1, "test failed!" );
1407
1408                 gtk_tree_path_free( path );
1409         }
1410 }
1411 };
1412
1413
1414 TestGraphTreeModel g_TestGraphTreeModel;
1415
1416 #endif