1 /*
2 * Inkscape::SelectionDescriber - shows messages describing selection
3 *
4 * Authors:
5 * MenTaLguY <mental@rydia.net>
6 * bulia byak <buliabyak@users.sf.net>
7 *
8 * Copyright (C) 2004-2006 Authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
17 #include <glibmm/i18n.h>
18 #include "xml/quote.h"
19 #include "selection.h"
20 #include "selection-describer.h"
21 #include "desktop.h"
22 #include "sp-textpath.h"
23 #include "sp-offset.h"
24 #include "sp-flowtext.h"
25 #include "sp-use.h"
26 #include "sp-rect.h"
27 #include "box3d.h"
28 #include "sp-ellipse.h"
29 #include "sp-star.h"
30 #include "sp-anchor.h"
31 #include "sp-image.h"
32 #include "sp-path.h"
33 #include "sp-line.h"
34 #include "sp-use.h"
35 #include "sp-polyline.h"
36 #include "sp-spiral.h"
38 const gchar *
39 type2term(GType type)
40 {
41 if (type == SP_TYPE_ANCHOR)
42 //TRANSLATORS: "Link" means internet link (anchor)
43 { return C_("Web", "Link"); }
44 if (type == SP_TYPE_CIRCLE)
45 { return _("Circle"); }
46 if (type == SP_TYPE_ELLIPSE)
47 { return _("Ellipse"); }
48 if (type == SP_TYPE_FLOWTEXT)
49 { return _("Flowed text"); }
50 if (type == SP_TYPE_GROUP)
51 { return _("Group"); }
52 if (type == SP_TYPE_IMAGE)
53 { return _("Image"); }
54 if (type == SP_TYPE_LINE)
55 { return _("Line"); }
56 if (type == SP_TYPE_PATH)
57 { return _("Path"); }
58 if (type == SP_TYPE_POLYGON)
59 { return _("Polygon"); }
60 if (type == SP_TYPE_POLYLINE)
61 { return _("Polyline"); }
62 if (type == SP_TYPE_RECT)
63 { return _("Rectangle"); }
64 if (type == SP_TYPE_BOX3D)
65 { return _("3D Box"); }
66 if (type == SP_TYPE_TEXT)
67 { return _("Text"); }
68 if (type == SP_TYPE_USE)
69 // TRANSLATORS: "Clone" is a noun, type of object
70 { return C_("Object", "Clone"); }
71 if (type == SP_TYPE_ARC)
72 { return _("Ellipse"); }
73 if (type == SP_TYPE_OFFSET)
74 { return _("Offset path"); }
75 if (type == SP_TYPE_SPIRAL)
76 { return _("Spiral"); }
77 if (type == SP_TYPE_STAR)
78 { return _("Star"); }
79 return NULL;
80 }
82 GSList *collect_terms (GSList *items)
83 {
84 GSList *r = NULL;
85 for (GSList *i = items; i != NULL; i = i->next) {
86 const gchar *term = type2term (G_OBJECT_TYPE(i->data));
87 if (term != NULL && g_slist_find (r, term) == NULL)
88 r = g_slist_prepend (r, (void *) term);
89 }
90 return r;
91 }
94 namespace Inkscape {
96 SelectionDescriber::SelectionDescriber(Inkscape::Selection *selection, MessageStack *stack, char *when_selected, char *when_nothing)
97 : _context(stack),
98 _when_selected (when_selected),
99 _when_nothing (when_nothing)
100 {
101 _selection_changed_connection = new sigc::connection (
102 selection->connectChanged(
103 sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection)));
104 _selection_modified_connection = new sigc::connection (
105 selection->connectModified(
106 sigc::mem_fun(*this, &SelectionDescriber::_selectionModified)));
107 _updateMessageFromSelection(selection);
108 }
110 SelectionDescriber::~SelectionDescriber()
111 {
112 _selection_changed_connection->disconnect();
113 _selection_modified_connection->disconnect();
114 delete _selection_changed_connection;
115 delete _selection_modified_connection;
116 }
118 void SelectionDescriber::_selectionModified(Inkscape::Selection *selection, guint /*flags*/)
119 {
120 _updateMessageFromSelection(selection);
121 }
123 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
124 GSList const *items = selection->itemList();
126 if (!items) { // no items
127 _context.set(Inkscape::NORMAL_MESSAGE, _when_nothing);
128 } else {
129 SPItem *item = SP_ITEM(items->data);
130 SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
131 SPObject *root = selection->desktop()->currentRoot();
133 // Layer name
134 gchar *layer_name;
135 if (layer == root) {
136 layer_name = g_strdup(_("root"));
137 } else {
138 char const *layer_label;
139 bool is_label = false;
140 if (layer && layer->label()) {
141 layer_label = layer->label();
142 is_label = true;
143 } else {
144 layer_label = layer->defaultLabel();
145 }
146 char *quoted_layer_label = xml_quote_strdup(layer_label);
147 if (is_label) {
148 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
149 } else {
150 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
151 }
152 g_free(quoted_layer_label);
153 }
155 // Parent name
156 SPObject *parent = SP_OBJECT_PARENT (item);
157 gchar const *parent_label = parent->getId();
158 char *quoted_parent_label = xml_quote_strdup(parent_label);
159 gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
160 g_free(quoted_parent_label);
162 gchar *in_phrase;
163 guint num_layers = selection->numberOfLayers();
164 guint num_parents = selection->numberOfParents();
165 if (num_layers == 1) {
166 if (num_parents == 1) {
167 if (layer == parent)
168 in_phrase = g_strdup_printf(_(" in %s"), layer_name);
169 else
170 in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
171 } else {
172 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
173 }
174 } else {
175 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
176 }
177 g_free (layer_name);
178 g_free (parent_name);
180 if (!items->next) { // one item
181 char *item_desc = sp_item_description(item);
182 if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
183 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
184 item_desc, in_phrase,
185 _("Use <b>Shift+D</b> to look up original"), _when_selected);
186 } else if (SP_IS_TEXT_TEXTPATH(item)) {
187 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
188 item_desc, in_phrase,
189 _("Use <b>Shift+D</b> to look up path"), _when_selected);
190 } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
191 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
192 item_desc, in_phrase,
193 _("Use <b>Shift+D</b> to look up frame"), _when_selected);
194 } else {
195 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
196 item_desc, in_phrase, _when_selected);
197 }
198 g_free(item_desc);
199 } else { // multiple items
200 int object_count = g_slist_length((GSList *)items);
202 const gchar *objects_str = NULL;
203 GSList *terms = collect_terms ((GSList *)items);
204 int n_terms = g_slist_length(terms);
205 if (n_terms == 0) {
206 objects_str = g_strdup_printf (
207 // this is only used with 2 or more objects
208 ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count),
209 object_count);
210 } else if (n_terms == 1) {
211 objects_str = g_strdup_printf (
212 // this is only used with 2 or more objects
213 ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
214 object_count, (gchar *) terms->data);
215 } else if (n_terms == 2) {
216 objects_str = g_strdup_printf (
217 // this is only used with 2 or more objects
218 ngettext("<b>%i</b> object of types <b>%s</b>, <b>%s</b>", "<b>%i</b> objects of types <b>%s</b>, <b>%s</b>", object_count),
219 object_count, (gchar *) terms->data, (gchar *) terms->next->data);
220 } else if (n_terms == 3) {
221 objects_str = g_strdup_printf (
222 // this is only used with 2 or more objects
223 ngettext("<b>%i</b> object of types <b>%s</b>, <b>%s</b>, <b>%s</b>", "<b>%i</b> objects of types <b>%s</b>, <b>%s</b>, <b>%s</b>", object_count),
224 object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
225 } else {
226 objects_str = g_strdup_printf (
227 // this is only used with 2 or more objects
228 ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count),
229 object_count, n_terms);
230 }
231 g_slist_free (terms);
233 _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, _when_selected);
235 if (objects_str)
236 g_free ((gchar *) objects_str);
237 }
239 g_free(in_phrase);
240 }
241 }
243 }
245 /*
246 Local Variables:
247 mode:c++
248 c-file-style:"stroustrup"
249 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
250 indent-tabs-mode:nil
251 fill-column:99
252 End:
253 */
254 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :