1 /*
2 * Specialization of GtkCList for the XML tree view
3 *
4 * Authors:
5 * MenTaLguY <mental@rydia.net>
6 *
7 * Copyright (C) 2002 MenTaLguY
8 *
9 * Released under the GNU GPL; see COPYING for details
10 */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif
16 #include <cstring>
17 #include <glibmm/i18n.h>
19 #include "helper/sp-marshal.h"
20 #include "../xml/node-event-vector.h"
21 #include "sp-xmlview-attr-list.h"
23 static void sp_xmlview_attr_list_class_init (SPXMLViewAttrListClass * klass);
24 static void sp_xmlview_attr_list_init (SPXMLViewAttrList * list);
25 static void sp_xmlview_attr_list_destroy (GtkObject * object);
27 static void event_attr_changed (Inkscape::XML::Node * repr, const gchar * name, const gchar * old_value, const gchar * new_value, bool is_interactive, gpointer data);
29 static GtkCListClass * parent_class = NULL;
31 static Inkscape::XML::NodeEventVector repr_events = {
32 NULL, /* child_added */
33 NULL, /* child_removed */
34 event_attr_changed,
35 NULL, /* content_changed */
36 NULL /* order_changed */
37 };
39 GtkWidget *
40 sp_xmlview_attr_list_new (Inkscape::XML::Node * repr)
41 {
42 SPXMLViewAttrList * list;
44 list = (SPXMLViewAttrList*)g_object_new (SP_TYPE_XMLVIEW_ATTR_LIST, "n_columns", 2, NULL);
46 gtk_clist_set_column_title (GTK_CLIST (list), 0, _("Attribute"));
47 gtk_clist_set_column_title (GTK_CLIST (list), 1, _("Value"));
48 gtk_clist_column_titles_show (GTK_CLIST (list));
50 gtk_clist_column_titles_passive (GTK_CLIST (list));
51 gtk_clist_set_column_auto_resize (GTK_CLIST (list), 0, TRUE);
52 gtk_clist_set_column_auto_resize (GTK_CLIST (list), 1, TRUE);
53 gtk_clist_set_sort_column (GTK_CLIST (list), 0);
54 gtk_clist_set_auto_sort (GTK_CLIST (list), TRUE);
56 sp_xmlview_attr_list_set_repr (list, repr);
58 return (GtkWidget *) list;
59 }
61 void
62 sp_xmlview_attr_list_set_repr (SPXMLViewAttrList * list, Inkscape::XML::Node * repr)
63 {
64 if ( repr == list->repr ) return;
65 gtk_clist_freeze (GTK_CLIST (list));
66 if (list->repr) {
67 gtk_clist_clear (GTK_CLIST (list));
68 sp_repr_remove_listener_by_data (list->repr, list);
69 Inkscape::GC::release(list->repr);
70 }
71 list->repr = repr;
72 if (repr) {
73 Inkscape::GC::anchor(repr);
74 sp_repr_add_listener (repr, &repr_events, list);
75 sp_repr_synthesize_events (repr, &repr_events, list);
76 }
77 gtk_clist_thaw (GTK_CLIST (list));
78 }
80 GType sp_xmlview_attr_list_get_type(void)
81 {
82 static GType type = 0;
84 if (!type) {
85 GTypeInfo info = {
86 sizeof(SPXMLViewAttrListClass),
87 0, // base_init
88 0, // base_finalize
89 (GClassInitFunc)sp_xmlview_attr_list_class_init,
90 0, // class_finalize
91 0, // class_data
92 sizeof(SPXMLViewAttrList),
93 0, // n_preallocs
94 (GInstanceInitFunc)sp_xmlview_attr_list_init,
95 0 // value_table
96 };
97 type = g_type_register_static(GTK_TYPE_CLIST, "SPXMLViewAttrList", &info, static_cast<GTypeFlags>(0));
98 }
100 return type;
101 }
103 void
104 sp_xmlview_attr_list_class_init (SPXMLViewAttrListClass * klass)
105 {
106 GtkObjectClass * object_class;
108 object_class = (GtkObjectClass *) klass;
109 object_class->destroy = sp_xmlview_attr_list_destroy;
111 parent_class = (GtkCListClass*)gtk_type_class (GTK_TYPE_CLIST);
113 g_signal_new ( "row-value-changed",
114 G_TYPE_FROM_CLASS(klass),
115 G_SIGNAL_RUN_FIRST,
116 G_STRUCT_OFFSET (SPXMLViewAttrListClass, row_changed),
117 NULL, NULL,
118 g_cclosure_marshal_VOID__UINT,
119 G_TYPE_NONE, 1,
120 G_TYPE_UINT);
121 }
123 void
124 sp_xmlview_attr_list_init (SPXMLViewAttrList * list)
125 {
126 list->repr = NULL;
127 }
129 void
130 sp_xmlview_attr_list_destroy (GtkObject * object)
131 {
132 SPXMLViewAttrList * list;
134 list = SP_XMLVIEW_ATTR_LIST (object);
136 sp_xmlview_attr_list_set_repr (list, NULL);
138 GTK_OBJECT_CLASS (parent_class)->destroy (object);
139 }
141 void
142 event_attr_changed (Inkscape::XML::Node * /*repr*/,
143 const gchar * name,
144 const gchar * /*old_value*/,
145 const gchar * new_value,
146 bool /*is_interactive*/,
147 gpointer data)
148 {
149 gint row;
150 SPXMLViewAttrList * list;
151 gchar new_text[128 + 4];
152 gchar *gtktext;
154 list = SP_XMLVIEW_ATTR_LIST (data);
156 gtk_clist_freeze (GTK_CLIST (list));
158 if (new_value) {
159 strncpy (new_text, new_value, 128);
160 if (strlen (new_value) >= 128) {
161 strcpy (new_text + 128, "...");
162 }
163 gtktext = new_text;
164 } else {
165 gtktext = NULL;
166 }
168 row = gtk_clist_find_row_from_data (GTK_CLIST (list), GINT_TO_POINTER (g_quark_from_string (name)));
169 if (row != -1) {
170 if (new_value) {
171 gtk_clist_set_text (GTK_CLIST (list), row, 1, gtktext);
172 } else {
173 gtk_clist_remove (GTK_CLIST (list), row);
174 }
175 } else if (new_value != NULL) {
176 const gchar * text[2];
178 text[0] = name;
179 text[1] = gtktext;
181 row = gtk_clist_append (GTK_CLIST (list), (gchar **)text);
182 gtk_clist_set_row_data (GTK_CLIST (list), row, GINT_TO_POINTER (g_quark_from_string (name)));
183 }
185 gtk_clist_thaw (GTK_CLIST (list));
187 // send a "changed" signal so widget owners will know I've updated
188 g_signal_emit_by_name(G_OBJECT (list), "row-value-changed", row );
189 }