Code

fixing crash in situations where locale language has many plural forms (example ...
[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 "sp-ellipse.h"
28 #include "sp-star.h"
29 #include "sp-anchor.h"
30 #include "sp-image.h"
31 #include "sp-path.h"
32 #include "sp-line.h"
33 #include "sp-use.h"
34 #include "sp-polyline.h"
35 #include "sp-spiral.h"
37 const gchar *
38 type2term(GType type)
39 {
40     if (type == SP_TYPE_ANCHOR)
41         { return _("Link"); }
42     if (type == SP_TYPE_CIRCLE)
43         { return _("Circle"); }
44     if (type == SP_TYPE_ELLIPSE)
45         { return _("Ellipse"); }
46     if (type == SP_TYPE_FLOWTEXT)
47         { return _("Flowed text"); }
48     if (type == SP_TYPE_GROUP)
49         { return _("Group"); }
50     if (type == SP_TYPE_IMAGE)
51         { return _("Image"); }
52     if (type == SP_TYPE_LINE)
53         { return _("Line"); }
54     if (type == SP_TYPE_PATH)
55         { return _("Path"); }
56     if (type == SP_TYPE_POLYGON)
57         { return _("Polygon"); }
58     if (type == SP_TYPE_POLYLINE)
59         { return _("Polyline"); }
60     if (type == SP_TYPE_RECT)
61         { return _("Rectangle"); }
62     if (type == SP_TYPE_TEXT)
63         { return _("Text"); }
64     if (type == SP_TYPE_USE)
65         { return _("Clone"); }
66     if (type == SP_TYPE_ARC)
67         { return _("Ellipse"); }
68     if (type == SP_TYPE_OFFSET)
69         { return _("Offset path"); }
70     if (type == SP_TYPE_SPIRAL)
71         { return _("Spiral"); }
72     if (type == SP_TYPE_STAR)
73         { return _("Star"); }
74     return NULL;
75 }
77 GSList *collect_terms (GSList *items)
78 {
79     GSList *r = NULL;
80     for (GSList *i = items; i != NULL; i = i->next) {
81         const gchar *term = type2term (G_OBJECT_TYPE(i->data));
82         if (term != NULL && g_slist_find (r, term) == NULL)
83             r = g_slist_prepend (r, (void *) term);
84     }
85     return r;
86 }
89 namespace Inkscape {
91 SelectionDescriber::SelectionDescriber(Inkscape::Selection *selection, MessageStack *stack)
92 : _context(stack)
93 {
94     selection->connectChanged(sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection));
95     _updateMessageFromSelection(selection);
96 }
98 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
99     GSList const *items = selection->itemList();
101     char const *when_selected = _("Click selection to toggle scale/rotation handles");
102     if (!items) { // no items
103         _context.set(Inkscape::NORMAL_MESSAGE, _("No objects selected. Click, Shift+click, or drag around objects to select."));
104     } else {
105         SPItem *item = SP_ITEM(items->data);
106         SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
107         SPObject *root = selection->desktop()->currentRoot();
109         // Layer name
110         gchar *layer_name;
111         if (layer == root) {
112             layer_name = g_strdup(_("root"));
113         } else {
114             char const *layer_label;
115             bool is_label = false;
116             if (layer && layer->label()) {
117                 layer_label = layer->label();
118                 is_label = true;
119             } else {
120                 layer_label = layer->defaultLabel();
121             }
122             char *quoted_layer_label = xml_quote_strdup(layer_label);
123             if (is_label) {
124                 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
125             } else {
126                 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
127             }
128             g_free(quoted_layer_label);
129         }
131         // Parent name
132         SPObject *parent = SP_OBJECT_PARENT (item);
133         gchar *parent_label = SP_OBJECT_ID(parent);
134         char *quoted_parent_label = xml_quote_strdup(parent_label);
135         gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
136         g_free(quoted_parent_label);
138         gchar *in_phrase;
139         guint num_layers = selection->numberOfLayers();
140         guint num_parents = selection->numberOfParents();
141         if (num_layers == 1) {
142             if (num_parents == 1) {
143                 if (layer == parent)
144                     in_phrase = g_strdup_printf(_(" in %s"), layer_name);
145                 else 
146                     in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
147             } else {
148                     in_phrase = g_strdup_printf(_(" in <b>%i</b> parents (%s)"), num_parents, layer_name);
149             }
150         } else {
151             in_phrase = g_strdup_printf(_(" in <b>%i</b> layers"), num_layers);
152         }
153         g_free (layer_name);
154         g_free (parent_name);
156         if (!items->next) { // one item
157             char *item_desc = sp_item_description(item);
158             if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
159                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
160                               item_desc, in_phrase,
161                               _("Use <b>Shift+D</b> to look up original"), when_selected);
162             } else if (SP_IS_TEXT_TEXTPATH(item)) {
163                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
164                               item_desc, in_phrase,
165                               _("Use <b>Shift+D</b> to look up path"), when_selected);
166             } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
167                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
168                               item_desc, in_phrase,
169                               _("Use <b>Shift+D</b> to look up frame"), when_selected);
170             } else {
171                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
172                               item_desc, in_phrase, when_selected);
173             }
174             g_free(item_desc);
175         } else { // multiple items
176             int object_count = g_slist_length((GSList *)items);
178             const gchar *objects_str = NULL;
179             GSList *terms = collect_terms ((GSList *)items);
180             int n_terms = g_slist_length(terms);
181             if (n_terms == 0) {
182                 objects_str = g_strdup_printf (
183                     // this is only used with 2 or more objects
184                     ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count), 
185                     object_count);
186             } else if (n_terms == 1) {
187                 objects_str = g_strdup_printf (
188                     // this is only used with 2 or more objects
189                     ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
190                     object_count, (gchar *) terms->data);
191             } else if (n_terms == 2) {
192                 objects_str = g_strdup_printf (
193                     // this is only used with 2 or more objects
194                     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), 
195                     object_count, (gchar *) terms->data, (gchar *) terms->next->data);
196             } else if (n_terms == 3) {
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>%s</b>", "<b>%i</b> objects of types <b>%s</b>, <b>%s</b>, <b>%s</b>", object_count), 
200                     object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
201             } else {
202                 objects_str = g_strdup_printf (
203                     // this is only used with 2 or more objects
204                     ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count), 
205                     object_count, n_terms);
206             }
207             g_slist_free (terms);
209             _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, when_selected);
211             if (objects_str)
212                 g_free ((gchar *) objects_str);
213         }
215         g_free(in_phrase);
216     }
221 /*
222   Local Variables:
223   mode:c++
224   c-file-style:"stroustrup"
225   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
226   indent-tabs-mode:nil
227   fill-column:99
228   End:
229 */
230 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :