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: only translate and put "Link" in the translation. It means internet link (anchor)
43 { return Q_("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 // TRANSLATORS: only translate "string" in "context|string".
69 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
70 if (type == SP_TYPE_USE)
71 { return Q_("object|Clone"); }
72 if (type == SP_TYPE_ARC)
73 { return _("Ellipse"); }
74 if (type == SP_TYPE_OFFSET)
75 { return _("Offset path"); }
76 if (type == SP_TYPE_SPIRAL)
77 { return _("Spiral"); }
78 if (type == SP_TYPE_STAR)
79 { return _("Star"); }
80 return NULL;
81 }
83 GSList *collect_terms (GSList *items)
84 {
85 GSList *r = NULL;
86 for (GSList *i = items; i != NULL; i = i->next) {
87 const gchar *term = type2term (G_OBJECT_TYPE(i->data));
88 if (term != NULL && g_slist_find (r, term) == NULL)
89 r = g_slist_prepend (r, (void *) term);
90 }
91 return r;
92 }
95 namespace Inkscape {
97 SelectionDescriber::SelectionDescriber(Inkscape::Selection *selection, MessageStack *stack)
98 : _context(stack)
99 {
100 selection->connectChanged(sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection));
101 _updateMessageFromSelection(selection);
102 }
104 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
105 GSList const *items = selection->itemList();
107 char const *when_selected = _("Click selection to toggle scale/rotation handles");
108 if (!items) { // no items
109 _context.set(Inkscape::NORMAL_MESSAGE, _("No objects selected. Click, Shift+click, or drag around objects to select."));
110 } else {
111 SPItem *item = SP_ITEM(items->data);
112 SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
113 SPObject *root = selection->desktop()->currentRoot();
115 // Layer name
116 gchar *layer_name;
117 if (layer == root) {
118 layer_name = g_strdup(_("root"));
119 } else {
120 char const *layer_label;
121 bool is_label = false;
122 if (layer && layer->label()) {
123 layer_label = layer->label();
124 is_label = true;
125 } else {
126 layer_label = layer->defaultLabel();
127 }
128 char *quoted_layer_label = xml_quote_strdup(layer_label);
129 if (is_label) {
130 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
131 } else {
132 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
133 }
134 g_free(quoted_layer_label);
135 }
137 // Parent name
138 SPObject *parent = SP_OBJECT_PARENT (item);
139 gchar *parent_label = SP_OBJECT_ID(parent);
140 char *quoted_parent_label = xml_quote_strdup(parent_label);
141 gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
142 g_free(quoted_parent_label);
144 gchar *in_phrase;
145 guint num_layers = selection->numberOfLayers();
146 guint num_parents = selection->numberOfParents();
147 if (num_layers == 1) {
148 if (num_parents == 1) {
149 if (layer == parent)
150 in_phrase = g_strdup_printf(_(" in %s"), layer_name);
151 else
152 in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
153 } else {
154 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
155 }
156 } else {
157 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
158 }
159 g_free (layer_name);
160 g_free (parent_name);
162 if (!items->next) { // one item
163 char *item_desc = sp_item_description(item);
164 if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
165 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
166 item_desc, in_phrase,
167 _("Use <b>Shift+D</b> to look up original"), when_selected);
168 } else if (SP_IS_TEXT_TEXTPATH(item)) {
169 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
170 item_desc, in_phrase,
171 _("Use <b>Shift+D</b> to look up path"), when_selected);
172 } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
173 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
174 item_desc, in_phrase,
175 _("Use <b>Shift+D</b> to look up frame"), when_selected);
176 } else {
177 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
178 item_desc, in_phrase, when_selected);
179 }
180 g_free(item_desc);
181 } else { // multiple items
182 int object_count = g_slist_length((GSList *)items);
184 const gchar *objects_str = NULL;
185 GSList *terms = collect_terms ((GSList *)items);
186 int n_terms = g_slist_length(terms);
187 if (n_terms == 0) {
188 objects_str = g_strdup_printf (
189 // this is only used with 2 or more objects
190 ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count),
191 object_count);
192 } else if (n_terms == 1) {
193 objects_str = g_strdup_printf (
194 // this is only used with 2 or more objects
195 ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
196 object_count, (gchar *) terms->data);
197 } else if (n_terms == 2) {
198 objects_str = g_strdup_printf (
199 // this is only used with 2 or more objects
200 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),
201 object_count, (gchar *) terms->data, (gchar *) terms->next->data);
202 } else if (n_terms == 3) {
203 objects_str = g_strdup_printf (
204 // this is only used with 2 or more objects
205 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),
206 object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
207 } else {
208 objects_str = g_strdup_printf (
209 // this is only used with 2 or more objects
210 ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count),
211 object_count, n_terms);
212 }
213 g_slist_free (terms);
215 _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, when_selected);
217 if (objects_str)
218 g_free ((gchar *) objects_str);
219 }
221 g_free(in_phrase);
222 }
223 }
225 }
227 /*
228 Local Variables:
229 mode:c++
230 c-file-style:"stroustrup"
231 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
232 indent-tabs-mode:nil
233 fill-column:99
234 End:
235 */
236 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :