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
15 #include "helper/sp-marshal.h"
16 #include <glibmm/i18n.h>
17 #include "../xml/node-event-vector.h"
18 #include "sp-xmlview-attr-list.h"
20 static void sp_xmlview_attr_list_class_init (SPXMLViewAttrListClass * klass);
21 static void sp_xmlview_attr_list_init (SPXMLViewAttrList * list);
22 static void sp_xmlview_attr_list_destroy (GtkObject * object);
24 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);
26 static GtkCListClass * parent_class = NULL;
28 static Inkscape::XML::NodeEventVector repr_events = {
29 NULL, /* child_added */
30 NULL, /* child_removed */
31 event_attr_changed,
32 NULL, /* content_changed */
33 NULL /* order_changed */
34 };
36 GtkWidget *
37 sp_xmlview_attr_list_new (Inkscape::XML::Node * repr)
38 {
39 SPXMLViewAttrList * list;
41 list = (SPXMLViewAttrList*)g_object_new (SP_TYPE_XMLVIEW_ATTR_LIST, "n_columns", 2, NULL);
43 gtk_clist_set_column_title (GTK_CLIST (list), 0, _("Attribute"));
44 gtk_clist_set_column_title (GTK_CLIST (list), 1, _("Value"));
45 gtk_clist_column_titles_show (GTK_CLIST (list));
47 gtk_clist_column_titles_passive (GTK_CLIST (list));
48 gtk_clist_set_column_auto_resize (GTK_CLIST (list), 0, TRUE);
49 gtk_clist_set_column_auto_resize (GTK_CLIST (list), 1, TRUE);
50 gtk_clist_set_sort_column (GTK_CLIST (list), 0);
51 gtk_clist_set_auto_sort (GTK_CLIST (list), TRUE);
53 sp_xmlview_attr_list_set_repr (list, repr);
55 return (GtkWidget *) list;
56 }
58 void
59 sp_xmlview_attr_list_set_repr (SPXMLViewAttrList * list, Inkscape::XML::Node * repr)
60 {
61 if ( repr == list->repr ) return;
62 gtk_clist_freeze (GTK_CLIST (list));
63 if (list->repr) {
64 gtk_clist_clear (GTK_CLIST (list));
65 sp_repr_remove_listener_by_data (list->repr, list);
66 Inkscape::GC::release(list->repr);
67 }
68 list->repr = repr;
69 if (repr) {
70 Inkscape::GC::anchor(repr);
71 sp_repr_add_listener (repr, &repr_events, list);
72 sp_repr_synthesize_events (repr, &repr_events, list);
73 }
74 gtk_clist_thaw (GTK_CLIST (list));
75 }
77 GtkType
78 sp_xmlview_attr_list_get_type (void)
79 {
80 static GtkType type = 0;
82 if (!type) {
83 static const GtkTypeInfo info = {
84 "SPXMLViewAttrList",
85 sizeof (SPXMLViewAttrList),
86 sizeof (SPXMLViewAttrListClass),
87 (GtkClassInitFunc) sp_xmlview_attr_list_class_init,
88 (GtkObjectInitFunc) sp_xmlview_attr_list_init,
89 NULL, NULL, NULL
90 };
91 type = gtk_type_unique (GTK_TYPE_CLIST, &info);
92 }
94 return type;
95 }
97 void
98 sp_xmlview_attr_list_class_init (SPXMLViewAttrListClass * klass)
99 {
100 GtkObjectClass * object_class;
102 object_class = (GtkObjectClass *) klass;
103 object_class->destroy = sp_xmlview_attr_list_destroy;
105 parent_class = (GtkCListClass*)gtk_type_class (GTK_TYPE_CLIST);
107 g_signal_new ( "row-value-changed",
108 G_TYPE_FROM_CLASS(klass),
109 G_SIGNAL_RUN_FIRST,
110 G_STRUCT_OFFSET (SPXMLViewAttrListClass, row_changed),
111 NULL, NULL,
112 sp_marshal_NONE__UINT,
113 G_TYPE_NONE, 1,
114 G_TYPE_UINT);
115 }
117 void
118 sp_xmlview_attr_list_init (SPXMLViewAttrList * list)
119 {
120 list->repr = NULL;
121 }
123 void
124 sp_xmlview_attr_list_destroy (GtkObject * object)
125 {
126 SPXMLViewAttrList * list;
128 list = SP_XMLVIEW_ATTR_LIST (object);
130 sp_xmlview_attr_list_set_repr (list, NULL);
132 GTK_OBJECT_CLASS (parent_class)->destroy (object);
133 }
135 void
136 event_attr_changed (Inkscape::XML::Node * /*repr*/,
137 const gchar * name,
138 const gchar * /*old_value*/,
139 const gchar * new_value,
140 bool /*is_interactive*/,
141 gpointer data)
142 {
143 gint row;
144 SPXMLViewAttrList * list;
145 gchar new_text[128 + 4];
146 gchar *gtktext;
148 list = SP_XMLVIEW_ATTR_LIST (data);
150 gtk_clist_freeze (GTK_CLIST (list));
152 if (new_value) {
153 strncpy (new_text, new_value, 128);
154 if (strlen (new_value) >= 128) {
155 strcpy (new_text + 128, "...");
156 }
157 gtktext = new_text;
158 } else {
159 gtktext = NULL;
160 }
162 row = gtk_clist_find_row_from_data (GTK_CLIST (list), GINT_TO_POINTER (g_quark_from_string (name)));
163 if (row != -1) {
164 if (new_value) {
165 gtk_clist_set_text (GTK_CLIST (list), row, 1, gtktext);
166 } else {
167 gtk_clist_remove (GTK_CLIST (list), row);
168 }
169 } else if (new_value != NULL) {
170 const gchar * text[2];
172 text[0] = name;
173 text[1] = gtktext;
175 row = gtk_clist_append (GTK_CLIST (list), (gchar **)text);
176 gtk_clist_set_row_data (GTK_CLIST (list), row, GINT_TO_POINTER (g_quark_from_string (name)));
177 }
179 gtk_clist_thaw (GTK_CLIST (list));
181 // send a "changed" signal so widget owners will know I've updated
182 g_signal_emit_by_name(G_OBJECT (list), "row-value-changed", row );
183 }