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