Code

A simple layout document as to what, why and how is cppification.
[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 "string" in "context|string".
43         // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
44         // "Link" means internet link (anchor)
45         { return Q_("web|Link"); }
46     if (type == SP_TYPE_CIRCLE)
47         { return _("Circle"); }
48     if (type == SP_TYPE_ELLIPSE)
49         { return _("Ellipse"); }
50     if (type == SP_TYPE_FLOWTEXT)
51         { return _("Flowed text"); }
52     if (type == SP_TYPE_GROUP)
53         { return _("Group"); }
54     if (type == SP_TYPE_IMAGE)
55         { return _("Image"); }
56     if (type == SP_TYPE_LINE)
57         { return _("Line"); }
58     if (type == SP_TYPE_PATH)
59         { return _("Path"); }
60     if (type == SP_TYPE_POLYGON)
61         { return _("Polygon"); }
62     if (type == SP_TYPE_POLYLINE)
63         { return _("Polyline"); }
64     if (type == SP_TYPE_RECT)
65         { return _("Rectangle"); }
66     if (type == SP_TYPE_BOX3D)
67         { return _("3D Box"); }
68     if (type == SP_TYPE_TEXT)
69         { return _("Text"); }
70     if (type == SP_TYPE_USE)
71         // TRANSLATORS: only translate "string" in "context|string".
72         // For more details, see http://developer.gnome.org/doc/API/2.0/glib/glib-I18N.html#Q-:CAPS
73         // "Clone" is a noun, type of object
74         { return Q_("object|Clone"); }
75     if (type == SP_TYPE_ARC)
76         { return _("Ellipse"); }
77     if (type == SP_TYPE_OFFSET)
78         { return _("Offset path"); }
79     if (type == SP_TYPE_SPIRAL)
80         { return _("Spiral"); }
81     if (type == SP_TYPE_STAR)
82         { return _("Star"); }
83     return NULL;
84 }
86 GSList *collect_terms (GSList *items)
87 {
88     GSList *r = NULL;
89     for (GSList *i = items; i != NULL; i = i->next) {
90         const gchar *term = type2term (G_OBJECT_TYPE(i->data));
91         if (term != NULL && g_slist_find (r, term) == NULL)
92             r = g_slist_prepend (r, (void *) term);
93     }
94     return r;
95 }
98 namespace Inkscape {
100 SelectionDescriber::SelectionDescriber(Inkscape::Selection *selection, MessageStack *stack, char *when_selected, char *when_nothing)
101     : _context(stack),
102       _when_selected (when_selected),
103       _when_nothing (when_nothing)
105     _selection_changed_connection = new sigc::connection (
106              selection->connectChanged(
107                  sigc::mem_fun(*this, &SelectionDescriber::_updateMessageFromSelection)));
108     _selection_modified_connection = new sigc::connection (
109              selection->connectModified(
110                  sigc::mem_fun(*this, &SelectionDescriber::_selectionModified)));
111     _updateMessageFromSelection(selection);
114 SelectionDescriber::~SelectionDescriber()
116     _selection_changed_connection->disconnect();
117     _selection_modified_connection->disconnect();
118     delete _selection_changed_connection;
119     delete _selection_modified_connection;
122 void SelectionDescriber::_selectionModified(Inkscape::Selection *selection, guint /*flags*/)
124     _updateMessageFromSelection(selection);
127 void SelectionDescriber::_updateMessageFromSelection(Inkscape::Selection *selection) {
128     GSList const *items = selection->itemList();
130     if (!items) { // no items
131         _context.set(Inkscape::NORMAL_MESSAGE, _when_nothing);
132     } else {
133         SPItem *item = SP_ITEM(items->data);
134         SPObject *layer = selection->desktop()->layerForObject (SP_OBJECT (item));
135         SPObject *root = selection->desktop()->currentRoot();
137         // Layer name
138         gchar *layer_name;
139         if (layer == root) {
140             layer_name = g_strdup(_("root"));
141         } else {
142             char const *layer_label;
143             bool is_label = false;
144             if (layer && layer->label()) {
145                 layer_label = layer->label();
146                 is_label = true;
147             } else {
148                 layer_label = layer->defaultLabel();
149             }
150             char *quoted_layer_label = xml_quote_strdup(layer_label);
151             if (is_label) {
152                 layer_name = g_strdup_printf(_("layer <b>%s</b>"), quoted_layer_label);
153             } else {
154                 layer_name = g_strdup_printf(_("layer <b><i>%s</i></b>"), quoted_layer_label);
155             }
156             g_free(quoted_layer_label);
157         }
159         // Parent name
160         SPObject *parent = SP_OBJECT_PARENT (item);
161         gchar const *parent_label = parent->getId();
162         char *quoted_parent_label = xml_quote_strdup(parent_label);
163         gchar *parent_name = g_strdup_printf(_("<i>%s</i>"), quoted_parent_label);
164         g_free(quoted_parent_label);
166         gchar *in_phrase;
167         guint num_layers = selection->numberOfLayers();
168         guint num_parents = selection->numberOfParents();
169         if (num_layers == 1) {
170             if (num_parents == 1) {
171                 if (layer == parent)
172                     in_phrase = g_strdup_printf(_(" in %s"), layer_name);
173                 else 
174                     in_phrase = g_strdup_printf(_(" in group %s (%s)"), parent_name, layer_name);
175             } else {
176                     in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> parents (%s)", " in <b>%i</b> parents (%s)", num_parents), num_parents, layer_name);
177             }
178         } else {
179             in_phrase = g_strdup_printf(ngettext(" in <b>%i</b> layers", " in <b>%i</b> layers", num_layers), num_layers);
180         }
181         g_free (layer_name);
182         g_free (parent_name);
184         if (!items->next) { // one item
185             char *item_desc = item->description();
186             if (SP_IS_USE(item) || (SP_IS_OFFSET(item) && SP_OFFSET (item)->sourceHref)) {
187                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
188                               item_desc, in_phrase,
189                               _("Use <b>Shift+D</b> to look up original"), _when_selected);
190             } else if (SP_IS_TEXT_TEXTPATH(item)) {
191                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
192                               item_desc, in_phrase,
193                               _("Use <b>Shift+D</b> to look up path"), _when_selected);
194             } else if (SP_IS_FLOWTEXT(item) && !SP_FLOWTEXT(item)->has_internal_frame()) {
195                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s. %s.",
196                               item_desc, in_phrase,
197                               _("Use <b>Shift+D</b> to look up frame"), _when_selected);
198             } else {
199                 _context.setF(Inkscape::NORMAL_MESSAGE, "%s%s. %s.",
200                               item_desc, in_phrase, _when_selected);
201             }
202             g_free(item_desc);
203         } else { // multiple items
204             int object_count = g_slist_length((GSList *)items);
206             const gchar *objects_str = NULL;
207             GSList *terms = collect_terms ((GSList *)items);
208             int n_terms = g_slist_length(terms);
209             if (n_terms == 0) {
210                 objects_str = g_strdup_printf (
211                     // this is only used with 2 or more objects
212                     ngettext("<b>%i</b> object selected", "<b>%i</b> objects selected", object_count), 
213                     object_count);
214             } else if (n_terms == 1) {
215                 objects_str = g_strdup_printf (
216                     // this is only used with 2 or more objects
217                     ngettext("<b>%i</b> object of type <b>%s</b>", "<b>%i</b> objects of type <b>%s</b>", object_count),
218                     object_count, (gchar *) terms->data);
219             } else if (n_terms == 2) {
220                 objects_str = g_strdup_printf (
221                     // this is only used with 2 or more objects
222                     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), 
223                     object_count, (gchar *) terms->data, (gchar *) terms->next->data);
224             } else if (n_terms == 3) {
225                 objects_str = g_strdup_printf (
226                     // this is only used with 2 or more objects
227                     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), 
228                     object_count, (gchar *) terms->data, (gchar *) terms->next->data, (gchar *) terms->next->next->data);
229             } else {
230                 objects_str = g_strdup_printf (
231                     // this is only used with 2 or more objects
232                     ngettext("<b>%i</b> object of <b>%i</b> types", "<b>%i</b> objects of <b>%i</b> types", object_count), 
233                     object_count, n_terms);
234             }
235             g_slist_free (terms);
237             _context.setF(Inkscape::NORMAL_MESSAGE, _("%s%s. %s."), objects_str, in_phrase, _when_selected);
239             if (objects_str)
240                 g_free ((gchar *) objects_str);
241         }
243         g_free(in_phrase);
244     }
249 /*
250   Local Variables:
251   mode:c++
252   c-file-style:"stroustrup"
253   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
254   indent-tabs-mode:nil
255   fill-column:99
256   End:
257 */
258 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :