Code

fix compile
[inkscape.git] / src / ui / dialog / find.cpp
1 /**
2  * \brief Find dialog
3  *
4  * Authors:
5  *   Bryce W. Harrington <bryce@bryceharrington.org>
6  *   Johan Engelen <goejendaagh@zonnet.nl>
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 <gtkmm/widget.h>
18 #include "find.h"
19 #include "verbs.h"
21 #include "message-stack.h"
22 #include "helper/window.h"
23 #include "macros.h"
24 #include "inkscape.h"
25 #include "document.h"
26 #include "desktop.h"
27 #include "selection.h"
28 #include "desktop-handles.h"
30 #include "dialogs/dialog-events.h"
31 #include "prefs-utils.h"
32 #include "verbs.h"
33 #include "interface.h"
34 #include "sp-text.h"
35 #include "sp-flowtext.h"
36 #include "text-editing.h"
37 #include "sp-tspan.h"
38 #include "sp-tref.h"
39 #include "selection-chemistry.h"
40 #include "sp-defs.h"
41 #include "sp-rect.h"
42 #include "sp-ellipse.h"
43 #include "sp-star.h"
44 #include "sp-spiral.h"
45 #include "sp-path.h"
46 #include "sp-line.h"
47 #include "sp-polyline.h"
48 #include "sp-item-group.h"
49 #include "sp-use.h"
50 #include "sp-image.h"
51 #include "sp-offset.h"
52 #include <xml/repr.h>
55 namespace Inkscape {
56 namespace UI {
57 namespace Dialog {
59 Find::Find()
60     : UI::Widget::Panel("", "dialogs.find", SP_VERB_DIALOG_FIND),
61       _entry_text(_("_Text: "), _("Find objects by their text content (exact or partial match)")),
62       _entry_id(_("_ID: "), _("Find objects by the value of the id attribute (exact or partial match)")),
63       _entry_style(_("_Style: "), _("Find objects by the value of the style attribute (exact or partial match)")),
64       _entry_attribute(_("_Attribute: "), _("Find objects by the name of an attribute (exact or partial match)")),
65       _check_search_selection(_("Search in s_election"), _("Limit search to the current selection")),
66       _check_search_layer(_("Search in current _layer"), _("Limit search to the current layer")),
67       _check_include_hidden(_("Include _hidden"), _("Include hidden objects in search")),
68       _check_include_locked(_("Include l_ocked"), _("Include locked objects in search")),
70       _check_all(_("All types"), _("Search in all object types")),
71       _check_all_shapes(_("All shapes"), _("Search all shapes")),
72       _check_rects(_("Rectangles"), _("Search rectangles")),
73       _check_ellipses(_("Ellipses"), _("Search ellipses, arcs, circles")),
74       _check_stars(_("Stars"), _("Search stars and polygons")),
75       _check_spirals(_("Spirals"), _("Search spirals")),
76       _check_paths(_("Paths"), _("Search paths, lines, polylines")),
77       _check_texts(_("Texts"), _("Search text objects")),
78       _check_groups(_("Groups"), _("Search groups")),
79       _check_clones(
80                   //TRANSLATORS: Translate the word "Clones" only. A noun indicating type of object to find
81                   Q_("find|Clones"), _("Search clones")),
82       _check_images(_("Images"), _("Search images")),
83       _check_offsets(_("Offsets"), _("Search offset objects")),
84     
85       _button_clear(_("_Clear"), _("Clear values")),
86       _button_find(_("_Find"), _("Select objects matching all of the fields you filled in"))
87 {
88     Gtk::Box *contents = _getContents();
89     contents->set_spacing(4);
90     
91     contents->pack_start(_entry_text, true, true);
92     contents->pack_start(_entry_id, true, true);
93     contents->pack_start(_entry_style, true, true);
94     contents->pack_start(_entry_attribute, true, true);
96     contents->pack_start(_check_all, true, true);
97     contents->pack_start(_check_all_shapes, true, true);
98     contents->pack_start(_check_rects, true, true);
99     contents->pack_start(_check_ellipses, true, true);
100     contents->pack_start(_check_stars, true, true);
101     contents->pack_start(_check_spirals, true, true);
102     contents->pack_start(_check_paths, true, true);
103     contents->pack_start(_check_texts, true, true);
104     contents->pack_start(_check_groups, true, true);
105     contents->pack_start(_check_clones, true, true);
106     contents->pack_start(_check_images, true, true);
107     contents->pack_start(_check_offsets, true, true);
109     contents->pack_start(_check_search_selection, true, true);
110     contents->pack_start(_check_search_layer, true, true);
111     contents->pack_start(_check_include_hidden, true, true);
112     contents->pack_start(_check_include_locked, true, true);
114     contents->pack_start(_button_clear, true, true);
115     contents->pack_start(_button_find, true, true);
117     // set signals to handle clicks
118     _check_all.signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleAlltypes));
119     _check_all_shapes.signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleShapes));
120     _button_clear.signal_clicked().connect(sigc::mem_fun(*this, &Find::onClear));
121     _button_find.signal_clicked().connect(sigc::mem_fun(*this, &Find::onFind));
123     _button_find.set_flags(Gtk::CAN_DEFAULT);
124     // set_default (_button_find); // activatable by Enter
125     _entry_text.getEntry()->grab_focus();
127     show_all_children();
128     onClear();
131 Find::~Find() 
136 /*########################################################################
137 # FIND helper functions
138 ########################################################################*/
140                    
141 bool
142 Find::item_id_match (SPItem *item, const gchar *id, bool exact)
144     if (SP_OBJECT_REPR (item) == NULL)
145         return false;
147     if (SP_IS_STRING(item)) // SPStrings have "on demand" ids which are useless for searching
148         return false;
150     const gchar *item_id = (SP_OBJECT_REPR (item))->attribute("id");
151     if (item_id == NULL)
152         return false;
154     if (exact) {
155         return ((bool) !strcmp(item_id, id));
156     } else {
157 //        g_print ("strstr: %s %s: %s\n", item_id, id, strstr(item_id, id) != NULL? "yes":"no");
158         return ((bool) (strstr(item_id, id) != NULL));
159     }
162 bool
163 Find::item_text_match (SPItem *item, const gchar *text, bool exact)
165     if (SP_OBJECT_REPR (item) == NULL)
166         return false;
168     if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
169         const gchar *item_text = sp_te_get_string_multiline (item);
170         if (item_text == NULL)
171             return false;
172         bool ret;
173         if (exact) {
174             ret = ((bool) !strcasecmp(item_text, text));
175         } else {
176             //FIXME: strcasestr
177             ret = ((bool) (strstr(item_text, text) != NULL));
178         }
179         g_free ((void*) item_text);
180         return ret;
181     }
182     return false;
185 bool
186 Find::item_style_match (SPItem *item, const gchar *text, bool exact)
188     if (SP_OBJECT_REPR (item) == NULL)
189         return false;
191     const gchar *item_text = (SP_OBJECT_REPR (item))->attribute("style");
192     if (item_text == NULL)
193         return false;
195     if (exact) {
196         return ((bool) !strcmp(item_text, text));
197     } else {
198         return ((bool) (strstr(item_text, text) != NULL));
199     }
202 bool
203 Find::item_attr_match (SPItem *item, const gchar *name, bool exact)
205     if (SP_OBJECT_REPR (item) == NULL)
206         return false;
208     if (exact) {
209         const gchar *attr_value = (SP_OBJECT_REPR (item))->attribute(name);
210         return ((bool) (attr_value != NULL));
211     } else {
212         return SP_OBJECT_REPR (item)->matchAttributeName(name);
213     }
217 GSList *
218 Find::filter_fields (GSList *l, bool exact)
220     const gchar* text = _entry_text.getEntry()->get_text().c_str();
221     const gchar* id = _entry_id.getEntry()->get_text().c_str();
222     const gchar* style = _entry_style.getEntry()->get_text().c_str();
223     const gchar* attr = _entry_attribute.getEntry()->get_text().c_str();
224     
225     GSList *in = l;
226     GSList *out = NULL;
227     if (strlen (text) != 0) {
228         for (GSList *i = in; i != NULL; i = i->next) {
229             if (item_text_match (SP_ITEM(i->data), text, exact)) {
230                 out = g_slist_prepend (out, i->data);
231             }
232         }
233     } else {
234         out = in;
235     }
236     
237     in = out;
238     out = NULL;
239     if (strlen (id) != 0) {
240         for (GSList *i = in; i != NULL; i = i->next) {
241             if (item_id_match (SP_ITEM(i->data), id, exact)) {
242                 out = g_slist_prepend (out, i->data);
243             }
244         }
245     } else {
246         out = in;
247     }
249     in = out;
250     out = NULL;
251     if (strlen (style) != 0) {
252         for (GSList *i = in; i != NULL; i = i->next) {
253             if (item_style_match (SP_ITEM(i->data), style, exact)) {
254                 out = g_slist_prepend (out, i->data);
255             }
256         }
257     } else {
258         out = in;
259     }
261     in = out;
262     out = NULL;
263     if (strlen (attr) != 0) {
264         for (GSList *i = in; i != NULL; i = i->next) {
265             if (item_attr_match (SP_ITEM(i->data), attr, exact)) {
266                 out = g_slist_prepend (out, i->data);
267             }
268         }
269     } else {
270         out = in;
271     }
273     return out;
277 bool
278 Find::item_type_match (SPItem *item)
280     SPDesktop *desktop = getDesktop();
282     if (SP_IS_RECT(item)) {
283         return (_check_all_shapes.get_active() || _check_rects.get_active());
285     } else if (SP_IS_GENERICELLIPSE(item) || SP_IS_ELLIPSE(item) || SP_IS_ARC(item) || SP_IS_CIRCLE(item)) {
286         return (_check_all_shapes.get_active() || _check_ellipses.get_active());
288     } else if (SP_IS_STAR(item) || SP_IS_POLYGON(item)) {
289         return (_check_all_shapes.get_active() || _check_stars.get_active());
291     } else if (SP_IS_SPIRAL(item)) {
292         return (_check_all_shapes.get_active() || _check_spirals.get_active());
294     } else if (SP_IS_PATH(item) || SP_IS_LINE(item) || SP_IS_POLYLINE(item)) {
295         return (_check_paths.get_active());
297     } else if (SP_IS_TEXT(item) || SP_IS_TSPAN(item) || SP_IS_TREF(item) || SP_IS_STRING(item)) {
298         return (_check_texts.get_active());
300     } else if (SP_IS_GROUP(item) && !desktop->isLayer(item) ) { // never select layers!
301         return (_check_groups.get_active());
303     } else if (SP_IS_USE(item)) {
304         return (_check_clones.get_active());
306     } else if (SP_IS_IMAGE(item)) {
307         return (_check_images.get_active());
309     } else if (SP_IS_OFFSET(item)) {
310         return (_check_offsets.get_active());
311     }
313     return false;
316 GSList *
317 Find::filter_types (GSList *l)
319     if (_check_all.get_active()) return l;
321     GSList *n = NULL;
322     for (GSList *i = l; i != NULL; i = i->next) {
323         if (item_type_match (SP_ITEM(i->data))) {
324             n = g_slist_prepend (n, i->data);
325         }
326     }
327     return n;
331 GSList *
332 Find::filter_list (GSList *l, bool exact)
334     l = filter_fields (l, exact);
335     l = filter_types (l);
336     return l;
339 GSList *
340 Find::all_items (SPObject *r, GSList *l, bool hidden, bool locked)
342     SPDesktop *desktop = getDesktop();
344     if (SP_IS_DEFS(r))
345         return l; // we're not interested in items in defs
347     if (!strcmp (SP_OBJECT_REPR (r)->name(), "svg:metadata"))
348         return l; // we're not interested in metadata
350     for (SPObject *child = sp_object_first_child(r); child; child = SP_OBJECT_NEXT (child)) {
351         if (SP_IS_ITEM (child) && !SP_OBJECT_IS_CLONED (child) && !desktop->isLayer(SP_ITEM(child))) {
352                 if ((hidden || !desktop->itemIsHidden(SP_ITEM(child))) && (locked || !SP_ITEM(child)->isLocked())) {
353                     l = g_slist_prepend (l, child);
354                 }
355         }
356         l = all_items (child, l, hidden, locked);
357     }
358     return l;
361 GSList *
362 Find::all_selection_items (Inkscape::Selection *s, GSList *l, SPObject *ancestor, bool hidden, bool locked)
364     SPDesktop *desktop = getDesktop();
366     for (GSList *i = (GSList *) s->itemList(); i != NULL; i = i->next) {
367         if (SP_IS_ITEM (i->data) && !SP_OBJECT_IS_CLONED (i->data) && !desktop->isLayer(SP_ITEM(i->data))) {
368             if (!ancestor || ancestor->isAncestorOf(SP_OBJECT (i->data))) {
369                 if ((hidden || !desktop->itemIsHidden(SP_ITEM(i->data))) && (locked || !SP_ITEM(i->data)->isLocked())) {
370                     l = g_slist_prepend (l, i->data);
371                 }
372             }
373         }
374         if (!ancestor || ancestor->isAncestorOf(SP_OBJECT (i->data))) {
375             l = all_items (SP_OBJECT (i->data), l, hidden, locked);
376         }
377     }
378     return l;
380                    
381                    
383 /*########################################################################
384 # BUTTON CLICK HANDLERS    (callbacks)
385 ########################################################################*/
387 void
388 Find::onClear()
389 {         
390     _entry_text.getEntry()->set_text(Glib::ustring(""));
391     _entry_id.getEntry()->set_text(Glib::ustring(""));
392     _entry_style.getEntry()->set_text(Glib::ustring(""));
393     _entry_attribute.getEntry()->set_text(Glib::ustring(""));
395     _check_all.set_active();
397     
398     
400 void
401 Find::onFind()
402 {   
403     SPDesktop *desktop = getDesktop();
405     bool hidden = _check_include_hidden.get_active();
406     bool locked = _check_include_locked.get_active();
408     GSList *l = NULL;
409     if (_check_search_selection.get_active()) {
410         if (_check_search_layer.get_active()) {
411             l = all_selection_items (desktop->selection, l, desktop->currentLayer(), hidden, locked);
412         } else {
413             l = all_selection_items (desktop->selection, l, NULL, hidden, locked);
414         }
415     } else {
416         if (_check_search_layer.get_active()) {
417             l = all_items (desktop->currentLayer(), l, hidden, locked);
418         } else {
419             l = all_items (SP_DOCUMENT_ROOT (sp_desktop_document (desktop)), l, hidden, locked);
420         }
421     }
422     guint all = g_slist_length (l);
424     bool exact = true;
425     GSList *n = NULL;
426     n = filter_list (l, exact);
427     if (n == NULL) {
428         exact = false;
429         n = filter_list (l, exact);
430     }
432     if (n != NULL) {
433         int count = g_slist_length (n);
434         desktop->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
435                                         // TRANSLATORS: "%s" is replaced with "exact" or "partial" when this string is displayed
436                                         ngettext("<b>%d</b> object found (out of <b>%d</b>), %s match.",
437                                                  "<b>%d</b> objects found (out of <b>%d</b>), %s match.",
438                                                  count),
439                                         count, all, exact? _("exact") : _("partial"));
441         Inkscape::Selection *selection = sp_desktop_selection (desktop);
442         selection->clear();
443         selection->setList(n);
444         scroll_to_show_item (desktop, SP_ITEM(n->data));
445     } else {
446         desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No objects found"));
447     }
450 void
451 Find::onToggleAlltypes ()
453     if (_check_all.get_active()) {
454         // explicit toggle to make sure its handler gets called, no matter what was the original state
455         _check_all_shapes.toggled();
456         _check_all_shapes.set_active();
457         _check_all_shapes.hide();
458         _check_paths.hide();
459         _check_texts.hide();
460         _check_groups.hide();
461         _check_clones.hide();
462         _check_images.hide();
463         _check_offsets.hide();
464     } else {
465         // explicit toggle to make sure its handler gets called, no matter what was the original state
466         _check_all_shapes.toggled();
467         _check_all_shapes.set_active();
468         _check_all_shapes.show();
470         _check_paths.set_active();
471         _check_paths.show();
472         _check_texts.set_active();
473         _check_texts.show();
474         _check_groups.set_active();
475         _check_groups.show();
476         _check_clones.set_active();
477         _check_clones.show();
478         _check_images.set_active();
479         _check_images.show();
480         _check_offsets.set_active();
481         _check_offsets.show();
482     }
483     squeeze_window();
486 void
487 Find::onToggleShapes ()
489     if (_check_all_shapes.get_active()) {
490         _check_rects.hide();
491         _check_ellipses.hide();
492         _check_stars.hide();
493         _check_spirals.hide();
494     } else {
495         _check_rects.set_active();
496         _check_rects.show();
497         _check_ellipses.set_active();
498         _check_ellipses.show();
499         _check_stars.set_active();
500         _check_stars.show();
501         _check_spirals.set_active();
502         _check_spirals.show();
503     }
504     squeeze_window();
508 /*########################################################################
509 # UTILITY
510 ########################################################################*/
514 void
515 Find::squeeze_window()
517     // TO DO: make window as small as possible
522 } // namespace Dialog
523 } // namespace UI
524 } // namespace Inkscape