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 "string" in "context|string".
43 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
44 // "Link" means internet link (anchor)
45 { return Q_("web|Link"); }
46 if (type == SP_TYPE_CIRCLE)
47 { return _("Circle"); }
48 if (type == SP_TYPE_ELLIPSE)
49 { return _("Ellipse"); }
50 if (type == SP_TYPE_FLOWTEXT)
51 { return _("Flowed text"); }
52 if (type == SP_TYPE_GROUP)
53 { return _("Group"); }
54 if (type == SP_TYPE_IMAGE)
55 { return _("Image"); }
56 if (type == SP_TYPE_LINE)
57 { return _("Line"); }
58 if (type == SP_TYPE_PATH)
59 { return _("Path"); }
60 if (type == SP_TYPE_POLYGON)
61 { return _("Polygon"); }
62 if (type == SP_TYPE_POLYLINE)
63 { return _("Polyline"); }
64 if (type == SP_TYPE_RECT)
65 { return _("Rectangle"); }
66 if (type == SP_TYPE_BOX3D)
67 { return _("3D Box"); }
68 if (type == SP_TYPE_TEXT)
69 { return _("Text"); }
70 if (type == SP_TYPE_USE)
71 // TRANSLATORS: only translate "string" in "context|string".
72 // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
73 // "Clone" is a noun, type of object
74 { return Q_("object|Clone"); }
75 if (type == SP_TYPE_ARC)
76 { return _("Ellipse"); }
77 if (type == SP_TYPE_OFFSET)
78 { return _("Offset path"); }
79 if (type == SP_TYPE_SPIRAL)
80 { return _("Spiral"); }
81 if (type == SP_TYPE_STAR)
82 { return _("Star"); }
83 return NULL;
84 }
86 GSList *collect_terms (GSList *items)
87 {
88 GSList *r = NULL;
89 for (GSList *i = items; i != NULL; i = i->next) {
90 const gchar *term = type2term (G_OBJECT_TYPE(i->data));
91 if (term != NULL && g_slist_find (r, term) == NULL)
92 r = g_slist_prepend (r, (void *) term);
93 }
94 return r;
95 }
98 namespace Inkscape {
100 SelectionDescriber::SelectionDescriber(Inkscape::Selection *selection, MessageStack *stack)
101 : _context(stack)
102 {
103 _selection_changed_connection = new sigc::connection (
104 selection->connectChanged(
105 sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection)));
106 _selection_modified_connection = new sigc::connection (
107 selection->connectModified(
108 sigc::mem_fun(*this, &SelectionDescriber::_selectionModified)));
109 _updateMessageFromSelection(selection);
110 }
112 SelectionDescriber::~SelectionDescriber()
113 {
114 _selection_changed_connection->disconnect();
115 _selection_modified_connection->disconnect();
116 delete _selection_changed_connection;
117 delete _selection_modified_connection;
118 }
120 void SelectionDescriber::_selectionModified(Inkscape::Selection *selection, guint /*flags*/)
121 {
122 _updateMessageFromSelection(selection);
123 }
125 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
126 GSList const *items = selection->itemList();
128 char const *when_selected = _("Click selection to toggle scale/rotation handles");
129 if (!items) { // no items
130 _context.set(Inkscape::NORMAL_MESSAGE, _("No objects selected. Click, Shift+click, or drag around objects to select."));
131 } else {
132 SPItem *item = SP_ITEM(items->data);
133 SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
134 SPObject *root = selection->desktop()->currentRoot();
136 // Layer name
137 gchar *layer_name;
138 if (layer == root) {
139 layer_name = g_strdup(_("root"));
140 } else {
141 char const *layer_label;
142 bool is_label = false;
143 if (layer && layer->label()) {
144 layer_label = layer->label();
145 is_label = true;
146 } else {
147 layer_label = layer->defaultLabel();
148 }
149 char *quoted_layer_label = xml_quote_strdup(layer_label);
150 if (is_label) {
151 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
152 } else {
153 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
154 }
155 g_free(quoted_layer_label);
156 }
158 // Parent name
159 SPObject *parent = SP_OBJECT_PARENT (item);
160 gchar *parent_label = SP_OBJECT_ID(parent);
161 char *quoted_parent_label = xml_quote_strdup(parent_label);
162 gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
163 g_free(quoted_parent_label);
165 gchar *in_phrase;
166 guint num_layers = selection->numberOfLayers();
167 guint num_parents = selection->numberOfParents();
168 if (num_layers == 1) {
169 if (num_parents == 1) {
170 if (layer == parent)
171 in_phrase = g_strdup_printf(_(" in %s"), layer_name);
172 else
173 in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
174 } else {
175 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
176 }
177 } else {
178 in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
179 }
180 g_free (layer_name);
181 g_free (parent_name);
183 if (!items->next) { // one item
184 char *item_desc = sp_item_description(item);
185 if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
186 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
187 item_desc, in_phrase,
188 _("Use <b>Shift+D</b> to look up original"), when_selected);
189 } else if (SP_IS_TEXT_TEXTPATH(item)) {
190 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
191 item_desc, in_phrase,
192 _("Use <b>Shift+D</b> to look up path"), when_selected);
193 } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
194 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
195 item_desc, in_phrase,
196 _("Use <b>Shift+D</b> to look up frame"), when_selected);
197 } else {
198 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
199 item_desc, in_phrase, when_selected);
200 }
201 g_free(item_desc);
202 } else { // multiple items
203 int object_count = g_slist_length((GSList *)items);
205 const gchar *objects_str = NULL;
206 GSList *terms = collect_terms ((GSList *)items);
207 int n_terms = g_slist_length(terms);
208 if (n_terms == 0) {
209 objects_str = g_strdup_printf (
210 // this is only used with 2 or more objects
211 ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count),
212 object_count);
213 } else if (n_terms == 1) {
214 objects_str = g_strdup_printf (
215 // this is only used with 2 or more objects
216 ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
217 object_count, (gchar *) terms->data);
218 } else if (n_terms == 2) {
219 objects_str = g_strdup_printf (
220 // this is only used with 2 or more objects
221 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),
222 object_count, (gchar *) terms->data, (gchar *) terms->next->data);
223 } else if (n_terms == 3) {
224 objects_str = g_strdup_printf (
225 // this is only used with 2 or more objects
226 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),
227 object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
228 } else {
229 objects_str = g_strdup_printf (
230 // this is only used with 2 or more objects
231 ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count),
232 object_count, n_terms);
233 }
234 g_slist_free (terms);
236 _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, when_selected);
238 if (objects_str)
239 g_free ((gchar *) objects_str);
240 }
242 g_free(in_phrase);
243 }
244 }
246 }
248 /*
249 Local Variables:
250 mode:c++
251 c-file-style:"stroustrup"
252 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
253 indent-tabs-mode:nil
254 fill-column:99
255 End:
256 */
257 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :