Code

Merge and cleanup of GSoC C++-ification project.
[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  *   Abhishek Sharma
8  *
9  * Copyright (C) 2004-2006 Authors
10  *
11  * Released under GNU GPL, read the file 'COPYING' for more information
12  */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include <glibmm/i18n.h>
19 #include "xml/quote.h"
20 #include "selection.h"
21 #include "selection-describer.h"
22 #include "desktop.h"
23 #include "sp-textpath.h"
24 #include "sp-offset.h"
25 #include "sp-flowtext.h"
26 #include "sp-use.h"
27 #include "sp-rect.h"
28 #include "box3d.h"
29 #include "sp-ellipse.h"
30 #include "sp-star.h"
31 #include "sp-anchor.h"
32 #include "sp-image.h"
33 #include "sp-path.h"
34 #include "sp-line.h"
35 #include "sp-use.h"
36 #include "sp-polyline.h"
37 #include "sp-spiral.h"
39 const gchar *
40 type2term(GType type)
41 {
42     if (type == SP_TYPE_ANCHOR)
43         //TRANSLATORS: "Link" means internet link (anchor)
44         { return C_("Web", "Link"); }
45     if (type == SP_TYPE_CIRCLE)
46         { return _("Circle"); }
47     if (type == SP_TYPE_ELLIPSE)
48         { return _("Ellipse"); }
49     if (type == SP_TYPE_FLOWTEXT)
50         { return _("Flowed text"); }
51     if (type == SP_TYPE_GROUP)
52         { return _("Group"); }
53     if (type == SP_TYPE_IMAGE)
54         { return _("Image"); }
55     if (type == SP_TYPE_LINE)
56         { return _("Line"); }
57     if (type == SP_TYPE_PATH)
58         { return _("Path"); }
59     if (type == SP_TYPE_POLYGON)
60         { return _("Polygon"); }
61     if (type == SP_TYPE_POLYLINE)
62         { return _("Polyline"); }
63     if (type == SP_TYPE_RECT)
64         { return _("Rectangle"); }
65     if (type == SP_TYPE_BOX3D)
66         { return _("3D Box"); }
67     if (type == SP_TYPE_TEXT)
68         { return _("Text"); }
69     if (type == SP_TYPE_USE)
70         // TRANSLATORS: "Clone" is a noun, type of object
71         { return C_("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, char *when_selected, char *when_nothing)
98     : _context(stack),
99       _when_selected (when_selected),
100       _when_nothing (when_nothing)
102     _selection_changed_connection = new sigc::connection (
103              selection->connectChanged(
104                  sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection)));
105     _selection_modified_connection = new sigc::connection (
106              selection->connectModified(
107                  sigc::mem_fun(*this, &SelectionDescriber::_selectionModified)));
108     _updateMessageFromSelection(selection);
111 SelectionDescriber::~SelectionDescriber()
113     _selection_changed_connection->disconnect();
114     _selection_modified_connection->disconnect();
115     delete _selection_changed_connection;
116     delete _selection_modified_connection;
119 void SelectionDescriber::_selectionModified(Inkscape::Selection *selection, guint /*flags*/)
121     _updateMessageFromSelection(selection);
124 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
125     GSList const *items = selection->itemList();
127     if (!items) { // no items
128         _context.set(Inkscape::NORMAL_MESSAGE, _when_nothing);
129     } else {
130         SPItem *item = SP_ITEM(items->data);
131         SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
132         SPObject *root = selection->desktop()->currentRoot();
134         // Layer name
135         gchar *layer_name;
136         if (layer == root) {
137             layer_name = g_strdup(_("root"));
138         } else {
139             char const *layer_label;
140             bool is_label = false;
141             if (layer && layer->label()) {
142                 layer_label = layer->label();
143                 is_label = true;
144             } else {
145                 layer_label = layer->defaultLabel();
146             }
147             char *quoted_layer_label = xml_quote_strdup(layer_label);
148             if (is_label) {
149                 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
150             } else {
151                 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
152             }
153             g_free(quoted_layer_label);
154         }
156         // Parent name
157         SPObject *parent = SP_OBJECT_PARENT (item);
158         gchar const *parent_label = parent->getId();
159         char *quoted_parent_label = xml_quote_strdup(parent_label);
160         gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
161         g_free(quoted_parent_label);
163         gchar *in_phrase;
164         guint num_layers = selection->numberOfLayers();
165         guint num_parents = selection->numberOfParents();
166         if (num_layers == 1) {
167             if (num_parents == 1) {
168                 if (layer == parent)
169                     in_phrase = g_strdup_printf(_(" in %s"), layer_name);
170                 else 
171                     in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
172             } else {
173                     in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
174             }
175         } else {
176             in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
177         }
178         g_free (layer_name);
179         g_free (parent_name);
181         if (!items->next) { // one item
182             char *item_desc = item->description();
183             if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
184                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
185                               item_desc, in_phrase,
186                               _("Use <b>Shift+D</b> to look up original"), _when_selected);
187             } else if (SP_IS_TEXT_TEXTPATH(item)) {
188                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
189                               item_desc, in_phrase,
190                               _("Use <b>Shift+D</b> to look up path"), _when_selected);
191             } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
192                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
193                               item_desc, in_phrase,
194                               _("Use <b>Shift+D</b> to look up frame"), _when_selected);
195             } else {
196                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
197                               item_desc, in_phrase, _when_selected);
198             }
199             g_free(item_desc);
200         } else { // multiple items
201             int object_count = g_slist_length((GSList *)items);
203             const gchar *objects_str = NULL;
204             GSList *terms = collect_terms ((GSList *)items);
205             int n_terms = g_slist_length(terms);
206             if (n_terms == 0) {
207                 objects_str = g_strdup_printf (
208                     // this is only used with 2 or more objects
209                     ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count), 
210                     object_count);
211             } else if (n_terms == 1) {
212                 objects_str = g_strdup_printf (
213                     // this is only used with 2 or more objects
214                     ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
215                     object_count, (gchar *) terms->data);
216             } else if (n_terms == 2) {
217                 objects_str = g_strdup_printf (
218                     // this is only used with 2 or more objects
219                     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), 
220                     object_count, (gchar *) terms->data, (gchar *) terms->next->data);
221             } else if (n_terms == 3) {
222                 objects_str = g_strdup_printf (
223                     // this is only used with 2 or more objects
224                     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), 
225                     object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
226             } else {
227                 objects_str = g_strdup_printf (
228                     // this is only used with 2 or more objects
229                     ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count), 
230                     object_count, n_terms);
231             }
232             g_slist_free (terms);
234             _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, _when_selected);
236             if (objects_str)
237                 g_free ((gchar *) objects_str);
238         }
240         g_free(in_phrase);
241     }
246 /*
247   Local Variables:
248   mode:c++
249   c-file-style:"stroustrup"
250   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
251   indent-tabs-mode:nil
252   fill-column:99
253   End:
254 */
255 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :