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