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 { return _("Link"); }
43 if (type == SP_TYPE_CIRCLE)
44 { return _("Circle"); }
45 if (type == SP_TYPE_ELLIPSE)
46 { return _("Ellipse"); }
47 if (type == SP_TYPE_FLOWTEXT)
48 { return _("Flowed text"); }
49 if (type == SP_TYPE_GROUP)
50 { return _("Group"); }
51 if (type == SP_TYPE_IMAGE)
52 { return _("Image"); }
53 if (type == SP_TYPE_LINE)
54 { return _("Line"); }
55 if (type == SP_TYPE_PATH)
56 { return _("Path"); }
57 if (type == SP_TYPE_POLYGON)
58 { return _("Polygon"); }
59 if (type == SP_TYPE_POLYLINE)
60 { return _("Polyline"); }
61 if (type == SP_TYPE_RECT)
62 { return _("Rectangle"); }
63 if (type == SP_TYPE_3DBOX)
64 { return _("3D Box"); }
65 if (type == SP_TYPE_TEXT)
66 { return _("Text"); }
67 // TRANSLATORS: only translate "string" in "context|string".
68 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
69 if (type == SP_TYPE_USE)
70 { return Q_("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)
97 : _context(stack)
98 {
99 selection->connectChanged(sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection));
100 _updateMessageFromSelection(selection);
101 }
103 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
104 GSList const *items = selection->itemList();
106 char const *when_selected = _("Click selection to toggle scale/rotation handles");
107 if (!items) { // no items
108 _context.set(Inkscape::NORMAL_MESSAGE, _("No objects selected. Click, Shift+click, or drag around objects to select."));
109 } else {
110 SPItem *item = SP_ITEM(items->data);
111 SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
112 SPObject *root = selection->desktop()->currentRoot();
114 // Layer name
115 gchar *layer_name;
116 if (layer == root) {
117 layer_name = g_strdup(_("root"));
118 } else {
119 char const *layer_label;
120 bool is_label = false;
121 if (layer && layer->label()) {
122 layer_label = layer->label();
123 is_label = true;
124 } else {
125 layer_label = layer->defaultLabel();
126 }
127 char *quoted_layer_label = xml_quote_strdup(layer_label);
128 if (is_label) {
129 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
130 } else {
131 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
132 }
133 g_free(quoted_layer_label);
134 }
136 // Parent name
137 SPObject *parent = SP_OBJECT_PARENT (item);
138 gchar *parent_label = SP_OBJECT_ID(parent);
139 char *quoted_parent_label = xml_quote_strdup(parent_label);
140 gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
141 g_free(quoted_parent_label);
143 gchar *in_phrase;
144 guint num_layers = selection->numberOfLayers();
145 guint num_parents = selection->numberOfParents();
146 if (num_layers == 1) {
147 if (num_parents == 1) {
148 if (layer == parent)
149 in_phrase = g_strdup_printf(_(" in %s"), layer_name);
150 else
151 in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
152 } else {
153 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
154 }
155 } else {
156 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
157 }
158 g_free (layer_name);
159 g_free (parent_name);
161 if (!items->next) { // one item
162 char *item_desc = sp_item_description(item);
163 if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
164 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
165 item_desc, in_phrase,
166 _("Use <b>Shift+D</b> to look up original"), when_selected);
167 } else if (SP_IS_TEXT_TEXTPATH(item)) {
168 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
169 item_desc, in_phrase,
170 _("Use <b>Shift+D</b> to look up path"), when_selected);
171 } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
172 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
173 item_desc, in_phrase,
174 _("Use <b>Shift+D</b> to look up frame"), when_selected);
175 } else {
176 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
177 item_desc, in_phrase, when_selected);
178 }
179 g_free(item_desc);
180 } else { // multiple items
181 int object_count = g_slist_length((GSList *)items);
183 const gchar *objects_str = NULL;
184 GSList *terms = collect_terms ((GSList *)items);
185 int n_terms = g_slist_length(terms);
186 if (n_terms == 0) {
187 objects_str = g_strdup_printf (
188 // this is only used with 2 or more objects
189 ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count),
190 object_count);
191 } else if (n_terms == 1) {
192 objects_str = g_strdup_printf (
193 // this is only used with 2 or more objects
194 ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
195 object_count, (gchar *) terms->data);
196 } else if (n_terms == 2) {
197 objects_str = g_strdup_printf (
198 // this is only used with 2 or more objects
199 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),
200 object_count, (gchar *) terms->data, (gchar *) terms->next->data);
201 } else if (n_terms == 3) {
202 objects_str = g_strdup_printf (
203 // this is only used with 2 or more objects
204 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),
205 object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
206 } else {
207 objects_str = g_strdup_printf (
208 // this is only used with 2 or more objects
209 ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count),
210 object_count, n_terms);
211 }
212 g_slist_free (terms);
214 _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, when_selected);
216 if (objects_str)
217 g_free ((gchar *) objects_str);
218 }
220 g_free(in_phrase);
221 }
222 }
224 }
226 /*
227 Local Variables:
228 mode:c++
229 c-file-style:"stroustrup"
230 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
231 indent-tabs-mode:nil
232 fill-column:99
233 End:
234 */
235 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :