Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[xonotic/netradiant.git] / radiant / entitylist.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 "entitylist.h"
23
24 #include "iselection.h"
25
26 #include <gtk/gtktreemodel.h>
27 #include <gtk/gtktreeview.h>
28 #include <gtk/gtktreeselection.h>
29 #include <gtk/gtkcellrenderertext.h>
30
31 #include "string/string.h"
32 #include "scenelib.h"
33 #include "nameable.h"
34 #include "signal/isignal.h"
35 #include "generic/object.h"
36
37 #include "gtkutil/widget.h"
38 #include "gtkutil/window.h"
39 #include "gtkutil/idledraw.h"
40 #include "gtkutil/accelerator.h"
41 #include "gtkutil/closure.h"
42
43 #include "treemodel.h"
44
45 void RedrawEntityList();
46 typedef FreeCaller<RedrawEntityList> RedrawEntityListCaller;
47
48 typedef struct _GtkTreeView GtkTreeView;
49
50 class EntityList
51 {
52 public:
53 enum EDirty
54 {
55         eDefault,
56         eSelection,
57         eInsertRemove,
58 };
59
60 EDirty m_dirty;
61
62 IdleDraw m_idleDraw;
63 WindowPositionTracker m_positionTracker;
64
65 GtkWindow* m_window;
66 GtkTreeView* m_tree_view;
67 GraphTreeModel* m_tree_model;
68 bool m_selection_disabled;
69
70 EntityList() :
71         m_dirty( EntityList::eDefault ),
72         m_idleDraw( RedrawEntityListCaller() ),
73         m_window( 0 ),
74         m_selection_disabled( false ){
75 }
76
77 bool visible() const {
78         return GTK_WIDGET_VISIBLE( GTK_WIDGET( m_window ) );
79 }
80 };
81
82 namespace
83 {
84 EntityList* g_EntityList;
85
86 inline EntityList& getEntityList(){
87         ASSERT_NOTNULL( g_EntityList );
88         return *g_EntityList;
89 }
90 }
91
92
93 inline Nameable* Node_getNameable( scene::Node& node ){
94         return NodeTypeCast<Nameable>::cast( node );
95 }
96
97 const char* node_get_name( scene::Node& node ){
98         Nameable* nameable = Node_getNameable( node );
99         return ( nameable != 0 )
100                    ? nameable->name()
101                    : "node";
102 }
103
104 template<typename value_type>
105 inline void gtk_tree_model_get_pointer( GtkTreeModel* model, GtkTreeIter* iter, gint column, value_type** pointer ){
106         GValue value = GValue_default();
107         gtk_tree_model_get_value( model, iter, column, &value );
108         *pointer = (value_type*)g_value_get_pointer( &value );
109 }
110
111
112
113 void entitylist_treeviewcolumn_celldatafunc( GtkTreeViewColumn* column, GtkCellRenderer* renderer, GtkTreeModel* model, GtkTreeIter* iter, gpointer data ){
114         scene::Node* node;
115         gtk_tree_model_get_pointer( model, iter, 0, &node );
116         scene::Instance* instance;
117         gtk_tree_model_get_pointer( model, iter, 1, &instance );
118         if ( node != 0 ) {
119                 gtk_cell_renderer_set_fixed_size( renderer, -1, -1 );
120                 char* name = const_cast<char*>( node_get_name( *node ) );
121                 g_object_set( G_OBJECT( renderer ), "text", name, "visible", TRUE, 0 );
122
123                 //globalOutputStream() << "rendering cell " << makeQuoted(name) << "\n";
124                 GtkStyle* style = gtk_widget_get_style( GTK_WIDGET( getEntityList().m_tree_view ) );
125                 if ( instance->childSelected() ) {
126                         g_object_set( G_OBJECT( renderer ), "cell-background-gdk", &style->base[GTK_STATE_ACTIVE], 0 );
127                 }
128                 else
129                 {
130                         g_object_set( G_OBJECT( renderer ), "cell-background-gdk", &style->base[GTK_STATE_NORMAL], 0 );
131                 }
132         }
133         else
134         {
135                 gtk_cell_renderer_set_fixed_size( renderer, -1, 0 );
136                 g_object_set( G_OBJECT( renderer ), "text", "", "visible", FALSE, 0 );
137         }
138 }
139
140 static gboolean entitylist_tree_select( GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer data ){
141         GtkTreeIter iter;
142         gtk_tree_model_get_iter( model, &iter, path );
143         scene::Node* node;
144         gtk_tree_model_get_pointer( model, &iter, 0, &node );
145         scene::Instance* instance;
146         gtk_tree_model_get_pointer( model, &iter, 1, &instance );
147         Selectable* selectable = Instance_getSelectable( *instance );
148
149         if ( node == 0 ) {
150                 if ( path_currently_selected != FALSE ) {
151                         getEntityList().m_selection_disabled = true;
152                         GlobalSelectionSystem().setSelectedAll( false );
153                         getEntityList().m_selection_disabled = false;
154                 }
155         }
156         else if ( selectable != 0 ) {
157                 getEntityList().m_selection_disabled = true;
158                 selectable->setSelected( path_currently_selected == FALSE );
159                 getEntityList().m_selection_disabled = false;
160                 return TRUE;
161         }
162
163         return FALSE;
164 }
165
166 static gboolean entitylist_tree_select_null( GtkTreeSelection *selection, GtkTreeModel *model, GtkTreePath *path, gboolean path_currently_selected, gpointer data ){
167         return TRUE;
168 }
169
170 void EntityList_ConnectSignals( GtkTreeView* view ){
171         GtkTreeSelection* select = gtk_tree_view_get_selection( view );
172         gtk_tree_selection_set_select_function( select, entitylist_tree_select, NULL, 0 );
173 }
174
175 void EntityList_DisconnectSignals( GtkTreeView* view ){
176         GtkTreeSelection* select = gtk_tree_view_get_selection( view );
177         gtk_tree_selection_set_select_function( select, entitylist_tree_select_null, 0, 0 );
178 }
179
180
181
182 gboolean treemodel_update_selection( GtkTreeModel* model, GtkTreePath* path, GtkTreeIter* iter, gpointer data ){
183         GtkTreeView* view = reinterpret_cast<GtkTreeView*>( data );
184
185         scene::Instance* instance;
186         gtk_tree_model_get_pointer( model, iter, 1, &instance );
187         Selectable* selectable = Instance_getSelectable( *instance );
188
189         if ( selectable != 0 ) {
190                 GtkTreeSelection* selection = gtk_tree_view_get_selection( view );
191                 if ( selectable->isSelected() ) {
192                         gtk_tree_selection_select_path( selection, path );
193                 }
194                 else
195                 {
196                         gtk_tree_selection_unselect_path( selection, path );
197                 }
198         }
199
200         return FALSE;
201 }
202
203 void EntityList_UpdateSelection( GtkTreeModel* model, GtkTreeView* view ){
204         EntityList_DisconnectSignals( view );
205         gtk_tree_model_foreach( model, treemodel_update_selection, view );
206         EntityList_ConnectSignals( view );
207 }
208
209
210 void RedrawEntityList(){
211         switch ( getEntityList().m_dirty )
212         {
213         case EntityList::eInsertRemove:
214         case EntityList::eSelection:
215                 EntityList_UpdateSelection( GTK_TREE_MODEL( getEntityList().m_tree_model ), getEntityList().m_tree_view );
216         default:
217                 break;
218         }
219         getEntityList().m_dirty = EntityList::eDefault;
220 }
221
222 void entitylist_queue_draw(){
223         getEntityList().m_idleDraw.queueDraw();
224 }
225
226 void EntityList_SelectionUpdate(){
227         if ( getEntityList().m_selection_disabled ) {
228                 return;
229         }
230
231         if ( getEntityList().m_dirty < EntityList::eSelection ) {
232                 getEntityList().m_dirty = EntityList::eSelection;
233         }
234         entitylist_queue_draw();
235 }
236
237 void EntityList_SelectionChanged( const Selectable& selectable ){
238         EntityList_SelectionUpdate();
239 }
240
241 void entitylist_treeview_rowcollapsed( GtkTreeView* view, GtkTreeIter* iter, GtkTreePath* path, gpointer user_data ){
242 }
243
244 void entitylist_treeview_row_expanded( GtkTreeView* view, GtkTreeIter* iter, GtkTreePath* path, gpointer user_data ){
245         EntityList_SelectionUpdate();
246 }
247
248
249 void EntityList_SetShown( bool shown ){
250         widget_set_visible( GTK_WIDGET( getEntityList().m_window ), shown );
251 }
252
253 void EntityList_toggleShown(){
254         EntityList_SetShown( !getEntityList().visible() );
255 }
256
257 gint graph_tree_model_compare_name( GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data ){
258         scene::Node* first;
259         gtk_tree_model_get( model, a, 0, (gpointer*)&first, -1 );
260         scene::Node* second;
261         gtk_tree_model_get( model, b, 0, (gpointer*)&second, -1 );
262         int result = 0;
263         if ( first != 0 && second != 0 ) {
264                 result = string_compare( node_get_name( *first ), node_get_name( *second ) );
265         }
266         if ( result == 0 ) {
267                 return ( first < second ) ? -1 : ( second < first ) ? 1 : 0;
268         }
269         return result;
270 }
271
272 extern GraphTreeModel* scene_graph_get_tree_model();
273 void AttachEntityTreeModel(){
274         getEntityList().m_tree_model = scene_graph_get_tree_model();
275
276         gtk_tree_view_set_model( getEntityList().m_tree_view, GTK_TREE_MODEL( getEntityList().m_tree_model ) );
277 }
278
279 void DetachEntityTreeModel(){
280         getEntityList().m_tree_model = 0;
281
282         gtk_tree_view_set_model( getEntityList().m_tree_view, 0 );
283 }
284
285 void EntityList_constructWindow( GtkWindow* main_window ){
286         ASSERT_MESSAGE( getEntityList().m_window == 0, "error" );
287
288         GtkWindow* window = create_persistent_floating_window( "Entity List", main_window );
289
290         gtk_window_add_accel_group( window, global_accel );
291
292         getEntityList().m_positionTracker.connect( window );
293
294
295         getEntityList().m_window = window;
296
297         {
298                 GtkScrolledWindow* scr = create_scrolled_window( GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
299                 gtk_container_add( GTK_CONTAINER( window ), GTK_WIDGET( scr ) );
300
301                 {
302                         GtkWidget* view = gtk_tree_view_new();
303                         gtk_tree_view_set_headers_visible( GTK_TREE_VIEW( view ), FALSE );
304
305                         GtkCellRenderer* renderer = gtk_cell_renderer_text_new();
306                         GtkTreeViewColumn* column = gtk_tree_view_column_new();
307                         gtk_tree_view_column_pack_start( column, renderer, TRUE );
308                         gtk_tree_view_column_set_cell_data_func( column, renderer, entitylist_treeviewcolumn_celldatafunc, 0, 0 );
309
310                         GtkTreeSelection* select = gtk_tree_view_get_selection( GTK_TREE_VIEW( view ) );
311                         gtk_tree_selection_set_mode( select, GTK_SELECTION_MULTIPLE );
312
313                         g_signal_connect( G_OBJECT( view ), "row_expanded", G_CALLBACK( entitylist_treeview_row_expanded ), 0 );
314                         g_signal_connect( G_OBJECT( view ), "row_collapsed", G_CALLBACK( entitylist_treeview_rowcollapsed ), 0 );
315
316                         gtk_tree_view_append_column( GTK_TREE_VIEW( view ), column );
317
318                         gtk_widget_show( view );
319                         gtk_container_add( GTK_CONTAINER( scr ), view );
320                         getEntityList().m_tree_view = GTK_TREE_VIEW( view );
321                 }
322         }
323
324         EntityList_ConnectSignals( getEntityList().m_tree_view );
325         AttachEntityTreeModel();
326 }
327
328 void EntityList_destroyWindow(){
329         DetachEntityTreeModel();
330         EntityList_DisconnectSignals( getEntityList().m_tree_view );
331         destroy_floating_window( getEntityList().m_window );
332 }
333
334 #include "preferencesystem.h"
335
336 #include "iselection.h"
337
338 namespace
339 {
340 scene::Node* nullNode = 0;
341 }
342
343 class NullSelectedInstance : public scene::Instance, public Selectable
344 {
345 class TypeCasts
346 {
347 InstanceTypeCastTable m_casts;
348 public:
349 TypeCasts(){
350         InstanceStaticCast<NullSelectedInstance, Selectable>::install( m_casts );
351 }
352 InstanceTypeCastTable& get(){
353         return m_casts;
354 }
355 };
356
357 public:
358 typedef LazyStatic<TypeCasts> StaticTypeCasts;
359
360 NullSelectedInstance() : Instance( scene::Path( makeReference( *nullNode ) ), 0, this, StaticTypeCasts::instance().get() ){
361 }
362
363 void setSelected( bool select ){
364         ERROR_MESSAGE( "error" );
365 }
366 bool isSelected() const {
367         return true;
368 }
369 };
370
371 typedef LazyStatic<NullSelectedInstance> StaticNullSelectedInstance;
372
373
374 void EntityList_Construct(){
375         graph_tree_model_insert( scene_graph_get_tree_model(), StaticNullSelectedInstance::instance() );
376
377         g_EntityList = new EntityList;
378
379         getEntityList().m_positionTracker.setPosition( c_default_window_pos );
380
381         GlobalPreferenceSystem().registerPreference( "EntityInfoDlg", WindowPositionTrackerImportStringCaller( getEntityList().m_positionTracker ), WindowPositionTrackerExportStringCaller( getEntityList().m_positionTracker ) );
382
383         typedef FreeCaller1<const Selectable&, EntityList_SelectionChanged> EntityListSelectionChangedCaller;
384         GlobalSelectionSystem().addSelectionChangeCallback( EntityListSelectionChangedCaller() );
385 }
386 void EntityList_Destroy(){
387         delete g_EntityList;
388
389         graph_tree_model_erase( scene_graph_get_tree_model(), StaticNullSelectedInstance::instance() );
390 }