Code

better description for multiple selections
[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 MenTaLguY
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();
108         gchar *layer_phrase;
109         if (layer == root) {
110             layer_phrase = g_strdup("");  // for simplicity
111         } else {
112             char const *name, *fmt;
113             if (layer && layer->label()) {
114                 name = layer->label();
115                 fmt = _(" in layer <b>%s</b>");
116             } else {
117                 name = layer->defaultLabel();
118                 fmt = _(" in layer <b><i>%s</i></b>");
119             }
120             char *quoted_name = xml_quote_strdup(name);
121             layer_phrase = g_strdup_printf(fmt, quoted_name);
122             g_free(quoted_name);
123         }
125         if (!items->next) { // one item
126             char *item_desc = sp_item_description(item);
127             if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
128                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
129                               item_desc, layer_phrase,
130                               _("Use <b>Shift+D</b> to look up original"), when_selected);
131             } else if (SP_IS_TEXT_TEXTPATH(item)) {
132                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
133                               item_desc, layer_phrase,
134                               _("Use <b>Shift+D</b> to look up path"), when_selected);
135             } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
136                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
137                               item_desc, layer_phrase,
138                               _("Use <b>Shift+D</b> to look up frame"), when_selected);
139             } else {
140                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
141                               item_desc, layer_phrase, when_selected);
142             }
143             g_free(item_desc);
144         } else { // multiple items
145             int object_count = g_slist_length((GSList *)items);
147             const gchar *objects_str = NULL;
148             GSList *terms = collect_terms ((GSList *)items);
149             int n_terms = g_slist_length(terms);
150             if (n_terms == 0) {
151                 objects_str = g_strdup_printf (_("<b>%i</b> objects selected"), object_count);
152             } else if (n_terms == 1) {
153                 objects_str = g_strdup_printf (_("<b>%i</b> objects of type <b>%s</b>"), object_count, (gchar *) terms->data);
154             } else if (n_terms == 2) {
155                 objects_str = g_strdup_printf (_("<b>%i</b> objects of types <b>%s</b>, <b>%s</b>"), object_count, (gchar *) terms->data, (gchar *) terms->next->data);
156             } else if (n_terms == 3) {
157                 objects_str = g_strdup_printf (_("<b>%i</b> objects of types <b>%s</b>, <b>%s</b>, <b>%s</b>"), object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
158             } else {
159                 objects_str = g_strdup_printf (_("<b>%i</b> objects of <b>%i</b> types"), object_count, n_terms);
160             }
161             g_slist_free (terms);
163             if (selection->numberOfLayers() == 1) {
164                 _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."),
165                               objects_str, layer_phrase, when_selected);
166             } else {
167                 _context.setF(Inkscape::NORMAL_MESSAGE,
168                               ngettext("%s in <b>%i</b> layer. %s.",
169                                        "%s in <b>%i</b> layers. %s.",
170                                        selection->numberOfLayers()),
171                               objects_str, selection->numberOfLayers(), when_selected);
172             }
174             if (objects_str)
175                 g_free ((gchar *) objects_str);
176         }
178         g_free(layer_phrase);
179     }
184 /*
185   Local Variables:
186   mode:c++
187   c-file-style:"stroustrup"
188   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
189   indent-tabs-mode:nil
190   fill-column:99
191   End:
192 */
193 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :