Code

Some consistency cleanup for descriptions.
[inkscape.git] / src / selection-describer.cpp
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_changed_connection = new sigc::connection (
101              selection->connectChanged(
102                  sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection)));
103     _updateMessageFromSelection(selection);
106 SelectionDescriber::~SelectionDescriber()
108     _selection_changed_connection->disconnect();
109     delete _selection_changed_connection;
112 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
113     GSList const *items = selection->itemList();
115     char const *when_selected = _("Click selection to toggle scale/rotation handles");
116     if (!items) { // no items
117         _context.set(Inkscape::NORMAL_MESSAGE, _("No objects selected. Click, Shift+click, or drag around objects to select."));
118     } else {
119         SPItem *item = SP_ITEM(items->data);
120         SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
121         SPObject *root = selection->desktop()->currentRoot();
123         // Layer name
124         gchar *layer_name;
125         if (layer == root) {
126             layer_name = g_strdup(_("root"));
127         } else {
128             char const *layer_label;
129             bool is_label = false;
130             if (layer && layer->label()) {
131                 layer_label = layer->label();
132                 is_label = true;
133             } else {
134                 layer_label = layer->defaultLabel();
135             }
136             char *quoted_layer_label = xml_quote_strdup(layer_label);
137             if (is_label) {
138                 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
139             } else {
140                 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
141             }
142             g_free(quoted_layer_label);
143         }
145         // Parent name
146         SPObject *parent = SP_OBJECT_PARENT (item);
147         gchar *parent_label = SP_OBJECT_ID(parent);
148         char *quoted_parent_label = xml_quote_strdup(parent_label);
149         gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
150         g_free(quoted_parent_label);
152         gchar *in_phrase;
153         guint num_layers = selection->numberOfLayers();
154         guint num_parents = selection->numberOfParents();
155         if (num_layers == 1) {
156             if (num_parents == 1) {
157                 if (layer == parent)
158                     in_phrase = g_strdup_printf(_(" in %s"), layer_name);
159                 else 
160                     in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
161             } else {
162                     in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
163             }
164         } else {
165             in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
166         }
167         g_free (layer_name);
168         g_free (parent_name);
170         if (!items->next) { // one item
171             char *item_desc = sp_item_description(item);
172             if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
173                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
174                               item_desc, in_phrase,
175                               _("Use <b>Shift+D</b> to look up original"), when_selected);
176             } else if (SP_IS_TEXT_TEXTPATH(item)) {
177                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
178                               item_desc, in_phrase,
179                               _("Use <b>Shift+D</b> to look up path"), when_selected);
180             } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
181                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
182                               item_desc, in_phrase,
183                               _("Use <b>Shift+D</b> to look up frame"), when_selected);
184             } else {
185                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
186                               item_desc, in_phrase, when_selected);
187             }
188             g_free(item_desc);
189         } else { // multiple items
190             int object_count = g_slist_length((GSList *)items);
192             const gchar *objects_str = NULL;
193             GSList *terms = collect_terms ((GSList *)items);
194             int n_terms = g_slist_length(terms);
195             if (n_terms == 0) {
196                 objects_str = g_strdup_printf (
197                     // this is only used with 2 or more objects
198                     ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count), 
199                     object_count);
200             } else if (n_terms == 1) {
201                 objects_str = g_strdup_printf (
202                     // this is only used with 2 or more objects
203                     ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
204                     object_count, (gchar *) terms->data);
205             } else if (n_terms == 2) {
206                 objects_str = g_strdup_printf (
207                     // this is only used with 2 or more objects
208                     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), 
209                     object_count, (gchar *) terms->data, (gchar *) terms->next->data);
210             } else if (n_terms == 3) {
211                 objects_str = g_strdup_printf (
212                     // this is only used with 2 or more objects
213                     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), 
214                     object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
215             } else {
216                 objects_str = g_strdup_printf (
217                     // this is only used with 2 or more objects
218                     ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count), 
219                     object_count, n_terms);
220             }
221             g_slist_free (terms);
223             _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, when_selected);
225             if (objects_str)
226                 g_free ((gchar *) objects_str);
227         }
229         g_free(in_phrase);
230     }
235 /*
236   Local Variables:
237   mode:c++
238   c-file-style:"stroustrup"
239   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
240   indent-tabs-mode:nil
241   fill-column:99
242   End:
243 */
244 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :