Code

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