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