2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #include "entitylist.h"
24 #include "iselection.h"
26 #include <uilib/uilib.h>
29 #include "string/string.h"
32 #include "signal/isignal.h"
33 #include "generic/object.h"
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"
41 #include "treemodel.h"
43 void RedrawEntityList();
44 typedef FreeCaller<void(), RedrawEntityList> RedrawEntityListCaller;
60 WindowPositionTracker m_positionTracker;
63 ui::TreeView m_tree_view{ui::null};
64 ui::TreeModel m_tree_model{ui::null};
65 bool m_selection_disabled;
68 m_dirty( EntityList::eDefault ),
69 m_idleDraw( RedrawEntityListCaller() ),
71 m_selection_disabled( false ){
75 return m_window.visible();
81 EntityList* g_EntityList;
83 inline EntityList& getEntityList(){
84 ASSERT_NOTNULL( g_EntityList );
90 inline Nameable* Node_getNameable( scene::Node& node ){
91 return NodeTypeCast<Nameable>::cast( node );
94 const char* node_get_name( scene::Node& node ){
95 Nameable* nameable = Node_getNameable( node );
96 return ( nameable != 0 )
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 );
110 void entitylist_treeviewcolumn_celldatafunc( ui::TreeViewColumn column, ui::CellRenderer renderer, ui::TreeModel model, GtkTreeIter* iter, gpointer data ){
112 gtk_tree_model_get_pointer( model, iter, 0, &node );
113 scene::Instance* instance;
114 gtk_tree_model_get_pointer( model, iter, 1, &instance );
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 );
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 );
127 g_object_set( G_OBJECT( renderer ), "cell-background-gdk", &style->base[GTK_STATE_NORMAL], NULL );
132 gtk_cell_renderer_set_fixed_size( renderer, -1, 0 );
133 g_object_set( G_OBJECT( renderer ), "text", "", "visible", FALSE, NULL );
137 static gboolean entitylist_tree_select( ui::TreeSelection selection, ui::TreeModel model, ui::TreePath path, gboolean path_currently_selected, gpointer data ){
139 gtk_tree_model_get_iter( model, &iter, path );
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 );
147 if ( path_currently_selected != FALSE ) {
148 getEntityList().m_selection_disabled = true;
149 GlobalSelectionSystem().setSelectedAll( false );
150 getEntityList().m_selection_disabled = false;
153 else if ( selectable != 0 ) {
154 getEntityList().m_selection_disabled = true;
155 selectable->setSelected( path_currently_selected == FALSE );
156 getEntityList().m_selection_disabled = false;
163 static gboolean entitylist_tree_select_null( ui::TreeSelection selection, ui::TreeModel model, ui::TreePath path, gboolean path_currently_selected, gpointer data ){
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 );
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 );
179 gboolean treemodel_update_selection( ui::TreeModel model, ui::TreePath path, GtkTreeIter* iter, gpointer data ){
180 auto view = ui::TreeView::from( data );
182 scene::Instance* instance;
183 gtk_tree_model_get_pointer( model, iter, 1, &instance );
184 Selectable* selectable = Instance_getSelectable( *instance );
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 );
193 gtk_tree_selection_unselect_path( selection, path );
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 );
207 void RedrawEntityList(){
208 switch ( getEntityList().m_dirty )
210 case EntityList::eInsertRemove:
211 case EntityList::eSelection:
212 EntityList_UpdateSelection( getEntityList().m_tree_model, getEntityList().m_tree_view );
216 getEntityList().m_dirty = EntityList::eDefault;
219 void entitylist_queue_draw(){
220 getEntityList().m_idleDraw.queueDraw();
223 void EntityList_SelectionUpdate(){
224 if ( getEntityList().m_selection_disabled ) {
228 if ( getEntityList().m_dirty < EntityList::eSelection ) {
229 getEntityList().m_dirty = EntityList::eSelection;
231 entitylist_queue_draw();
234 void EntityList_SelectionChanged( const Selectable& selectable ){
235 EntityList_SelectionUpdate();
238 void entitylist_treeview_rowcollapsed( ui::TreeView view, GtkTreeIter* iter, ui::TreePath path, gpointer user_data ){
241 void entitylist_treeview_row_expanded( ui::TreeView view, GtkTreeIter* iter, ui::TreePath path, gpointer user_data ){
242 EntityList_SelectionUpdate();
246 void EntityList_SetShown( bool shown ){
247 getEntityList().m_window.visible(shown);
250 void EntityList_toggleShown(){
251 EntityList_SetShown( !getEntityList().visible() );
254 gint graph_tree_model_compare_name( ui::TreeModel model, GtkTreeIter *a, GtkTreeIter *b, gpointer user_data ){
256 gtk_tree_model_get( model, a, 0, (gpointer*)&first, -1 );
258 gtk_tree_model_get( model, b, 0, (gpointer*)&second, -1 );
260 if ( first != 0 && second != 0 ) {
261 result = string_compare( node_get_name( *first ), node_get_name( *second ) );
264 return ( first < second ) ? -1 : ( second < first ) ? 1 : 0;
269 extern GraphTreeModel* scene_graph_get_tree_model();
270 void AttachEntityTreeModel(){
271 getEntityList().m_tree_model = ui::TreeModel::from(scene_graph_get_tree_model());
273 gtk_tree_view_set_model( getEntityList().m_tree_view, getEntityList().m_tree_model );
276 void DetachEntityTreeModel(){
277 getEntityList().m_tree_model = ui::TreeModel(ui::null);
279 gtk_tree_view_set_model( getEntityList().m_tree_view, 0 );
282 void EntityList_constructWindow( ui::Window main_window ){
283 ASSERT_TRUE( !getEntityList().m_window );
285 auto window = ui::Window(create_persistent_floating_window( "Entity List", main_window ));
287 window.add_accel_group(global_accel);
289 getEntityList().m_positionTracker.connect( window );
292 getEntityList().m_window = window;
295 auto scr = create_scrolled_window( ui::Policy::AUTOMATIC, ui::Policy::AUTOMATIC );
299 auto view = ui::TreeView(ui::New);
300 gtk_tree_view_set_headers_visible(view, FALSE );
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 );
307 auto select = gtk_tree_view_get_selection(view );
308 gtk_tree_selection_set_mode( select, GTK_SELECTION_MULTIPLE );
310 view.connect( "row_expanded", G_CALLBACK( entitylist_treeview_row_expanded ), 0 );
311 view.connect( "row_collapsed", G_CALLBACK( entitylist_treeview_rowcollapsed ), 0 );
313 gtk_tree_view_append_column(view, column );
317 getEntityList().m_tree_view = view;
321 EntityList_ConnectSignals( getEntityList().m_tree_view );
322 AttachEntityTreeModel();
325 void EntityList_destroyWindow(){
326 DetachEntityTreeModel();
327 EntityList_DisconnectSignals( getEntityList().m_tree_view );
328 destroy_floating_window( getEntityList().m_window );
331 #include "preferencesystem.h"
333 #include "iselection.h"
337 scene::Node* nullNode = 0;
340 class NullSelectedInstance : public scene::Instance, public Selectable
344 InstanceTypeCastTable m_casts;
347 InstanceStaticCast<NullSelectedInstance, Selectable>::install( m_casts );
349 InstanceTypeCastTable& get(){
355 typedef LazyStatic<TypeCasts> StaticTypeCasts;
357 NullSelectedInstance() : Instance( scene::Path( makeReference( *nullNode ) ), 0, this, StaticTypeCasts::instance().get() ){
360 void setSelected( bool select ){
361 ERROR_MESSAGE( "error" );
363 bool isSelected() const {
368 typedef LazyStatic<NullSelectedInstance> StaticNullSelectedInstance;
371 void EntityList_Construct(){
372 graph_tree_model_insert( scene_graph_get_tree_model(), StaticNullSelectedInstance::instance() );
374 g_EntityList = new EntityList;
376 getEntityList().m_positionTracker.setPosition( c_default_window_pos );
378 GlobalPreferenceSystem().registerPreference( "EntityInfoDlg", make_property<WindowPositionTracker_String>( getEntityList().m_positionTracker ) );
380 typedef FreeCaller<void(const Selectable&), EntityList_SelectionChanged> EntityListSelectionChangedCaller;
381 GlobalSelectionSystem().addSelectionChangeCallback( EntityListSelectionChangedCaller() );
383 void EntityList_Destroy(){
386 graph_tree_model_erase( scene_graph_get_tree_model(), StaticNullSelectedInstance::instance() );