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(_("Clones"), _("Search clones")),
80 _check_images(_("Images"), _("Search images")),
81 _check_offsets(_("Offsets"), _("Search offset objects")),
83 _button_clear(_("_Clear"), _("Clear values")),
84 _button_find(_("_Find"), _("Select objects matching all of the fields you filled in"))
85 {
86 Gtk::Box *contents = _getContents();
87 contents->set_spacing(4);
89 contents->pack_start(_entry_text, true, true);
90 contents->pack_start(_entry_id, true, true);
91 contents->pack_start(_entry_style, true, true);
92 contents->pack_start(_entry_attribute, true, true);
94 contents->pack_start(_check_all, true, true);
95 contents->pack_start(_check_all_shapes, true, true);
96 contents->pack_start(_check_rects, true, true);
97 contents->pack_start(_check_ellipses, true, true);
98 contents->pack_start(_check_stars, true, true);
99 contents->pack_start(_check_spirals, true, true);
100 contents->pack_start(_check_paths, true, true);
101 contents->pack_start(_check_texts, true, true);
102 contents->pack_start(_check_groups, true, true);
103 contents->pack_start(_check_clones, true, true);
104 contents->pack_start(_check_images, true, true);
105 contents->pack_start(_check_offsets, true, true);
107 contents->pack_start(_check_search_selection, true, true);
108 contents->pack_start(_check_search_layer, true, true);
109 contents->pack_start(_check_include_hidden, true, true);
110 contents->pack_start(_check_include_locked, true, true);
112 contents->pack_start(_button_clear, true, true);
113 contents->pack_start(_button_find, true, true);
115 // set signals to handle clicks
116 _check_all.signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleAlltypes));
117 _check_all_shapes.signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleShapes));
118 _button_clear.signal_clicked().connect(sigc::mem_fun(*this, &Find::onClear));
119 _button_find.signal_clicked().connect(sigc::mem_fun(*this, &Find::onFind));
121 _button_find.set_flags(Gtk::CAN_DEFAULT);
122 // set_default (_button_find); // activatable by Enter
123 _entry_text.getEntry()->grab_focus();
125 show_all_children();
126 onClear();
127 }
129 Find::~Find()
130 {
131 }
134 /*########################################################################
135 # FIND helper functions
136 ########################################################################*/
139 bool
140 Find::item_id_match (SPItem *item, const gchar *id, bool exact)
141 {
142 if (SP_OBJECT_REPR (item) == NULL)
143 return false;
145 if (SP_IS_STRING(item)) // SPStrings have "on demand" ids which are useless for searching
146 return false;
148 const gchar *item_id = (SP_OBJECT_REPR (item))->attribute("id");
149 if (item_id == NULL)
150 return false;
152 if (exact) {
153 return ((bool) !strcmp(item_id, id));
154 } else {
155 // g_print ("strstr: %s %s: %s\n", item_id, id, strstr(item_id, id) != NULL? "yes":"no");
156 return ((bool) (strstr(item_id, id) != NULL));
157 }
158 }
160 bool
161 Find::item_text_match (SPItem *item, const gchar *text, bool exact)
162 {
163 if (SP_OBJECT_REPR (item) == NULL)
164 return false;
166 if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
167 const gchar *item_text = sp_te_get_string_multiline (item);
168 if (item_text == NULL)
169 return false;
170 bool ret;
171 if (exact) {
172 ret = ((bool) !strcasecmp(item_text, text));
173 } else {
174 //FIXME: strcasestr
175 ret = ((bool) (strstr(item_text, text) != NULL));
176 }
177 g_free ((void*) item_text);
178 return ret;
179 }
180 return false;
181 }
183 bool
184 Find::item_style_match (SPItem *item, const gchar *text, bool exact)
185 {
186 if (SP_OBJECT_REPR (item) == NULL)
187 return false;
189 const gchar *item_text = (SP_OBJECT_REPR (item))->attribute("style");
190 if (item_text == NULL)
191 return false;
193 if (exact) {
194 return ((bool) !strcmp(item_text, text));
195 } else {
196 return ((bool) (strstr(item_text, text) != NULL));
197 }
198 }
200 bool
201 Find::item_attr_match (SPItem *item, const gchar *name, bool exact)
202 {
203 if (SP_OBJECT_REPR (item) == NULL)
204 return false;
206 if (exact) {
207 const gchar *attr_value = (SP_OBJECT_REPR (item))->attribute(name);
208 return ((bool) (attr_value != NULL));
209 } else {
210 return SP_OBJECT_REPR (item)->matchAttributeName(name);
211 }
212 }
215 GSList *
216 Find::filter_fields (GSList *l, bool exact)
217 {
218 const gchar* text = _entry_text.getEntry()->get_text().c_str();
219 const gchar* id = _entry_id.getEntry()->get_text().c_str();
220 const gchar* style = _entry_style.getEntry()->get_text().c_str();
221 const gchar* attr = _entry_attribute.getEntry()->get_text().c_str();
223 GSList *in = l;
224 GSList *out = NULL;
225 if (strlen (text) != 0) {
226 for (GSList *i = in; i != NULL; i = i->next) {
227 if (item_text_match (SP_ITEM(i->data), text, exact)) {
228 out = g_slist_prepend (out, i->data);
229 }
230 }
231 } else {
232 out = in;
233 }
235 in = out;
236 out = NULL;
237 if (strlen (id) != 0) {
238 for (GSList *i = in; i != NULL; i = i->next) {
239 if (item_id_match (SP_ITEM(i->data), id, exact)) {
240 out = g_slist_prepend (out, i->data);
241 }
242 }
243 } else {
244 out = in;
245 }
247 in = out;
248 out = NULL;
249 if (strlen (style) != 0) {
250 for (GSList *i = in; i != NULL; i = i->next) {
251 if (item_style_match (SP_ITEM(i->data), style, exact)) {
252 out = g_slist_prepend (out, i->data);
253 }
254 }
255 } else {
256 out = in;
257 }
259 in = out;
260 out = NULL;
261 if (strlen (attr) != 0) {
262 for (GSList *i = in; i != NULL; i = i->next) {
263 if (item_attr_match (SP_ITEM(i->data), attr, exact)) {
264 out = g_slist_prepend (out, i->data);
265 }
266 }
267 } else {
268 out = in;
269 }
271 return out;
272 }
275 bool
276 Find::item_type_match (SPItem *item)
277 {
278 SPDesktop *desktop = getDesktop();
280 if (SP_IS_RECT(item)) {
281 return (_check_all_shapes.get_active() || _check_rects.get_active());
283 } else if (SP_IS_GENERICELLIPSE(item) || SP_IS_ELLIPSE(item) || SP_IS_ARC(item) || SP_IS_CIRCLE(item)) {
284 return (_check_all_shapes.get_active() || _check_ellipses.get_active());
286 } else if (SP_IS_STAR(item) || SP_IS_POLYGON(item)) {
287 return (_check_all_shapes.get_active() || _check_stars.get_active());
289 } else if (SP_IS_SPIRAL(item)) {
290 return (_check_all_shapes.get_active() || _check_spirals.get_active());
292 } else if (SP_IS_PATH(item) || SP_IS_LINE(item) || SP_IS_POLYLINE(item)) {
293 return (_check_paths.get_active());
295 } else if (SP_IS_TEXT(item) || SP_IS_TSPAN(item) || SP_IS_TREF(item) || SP_IS_STRING(item)) {
296 return (_check_texts.get_active());
298 } else if (SP_IS_GROUP(item) && !desktop->isLayer(item) ) { // never select layers!
299 return (_check_groups.get_active());
301 } else if (SP_IS_USE(item)) {
302 return (_check_clones.get_active());
304 } else if (SP_IS_IMAGE(item)) {
305 return (_check_images.get_active());
307 } else if (SP_IS_OFFSET(item)) {
308 return (_check_offsets.get_active());
309 }
311 return false;
312 }
314 GSList *
315 Find::filter_types (GSList *l)
316 {
317 if (_check_all.get_active()) return l;
319 GSList *n = NULL;
320 for (GSList *i = l; i != NULL; i = i->next) {
321 if (item_type_match (SP_ITEM(i->data))) {
322 n = g_slist_prepend (n, i->data);
323 }
324 }
325 return n;
326 }
329 GSList *
330 Find::filter_list (GSList *l, bool exact)
331 {
332 l = filter_fields (l, exact);
333 l = filter_types (l);
334 return l;
335 }
337 GSList *
338 Find::all_items (SPObject *r, GSList *l, bool hidden, bool locked)
339 {
340 SPDesktop *desktop = getDesktop();
342 if (SP_IS_DEFS(r))
343 return l; // we're not interested in items in defs
345 if (!strcmp (SP_OBJECT_REPR (r)->name(), "svg:metadata"))
346 return l; // we're not interested in metadata
348 for (SPObject *child = sp_object_first_child(r); child; child = SP_OBJECT_NEXT (child)) {
349 if (SP_IS_ITEM (child) && !SP_OBJECT_IS_CLONED (child) && !desktop->isLayer(SP_ITEM(child))) {
350 if ((hidden || !desktop->itemIsHidden(SP_ITEM(child))) && (locked || !SP_ITEM(child)->isLocked())) {
351 l = g_slist_prepend (l, child);
352 }
353 }
354 l = all_items (child, l, hidden, locked);
355 }
356 return l;
357 }
359 GSList *
360 Find::all_selection_items (Inkscape::Selection *s, GSList *l, SPObject *ancestor, bool hidden, bool locked)
361 {
362 SPDesktop *desktop = getDesktop();
364 for (GSList *i = (GSList *) s->itemList(); i != NULL; i = i->next) {
365 if (SP_IS_ITEM (i->data) && !SP_OBJECT_IS_CLONED (i->data) && !desktop->isLayer(SP_ITEM(i->data))) {
366 if (!ancestor || ancestor->isAncestorOf(SP_OBJECT (i->data))) {
367 if ((hidden || !desktop->itemIsHidden(SP_ITEM(i->data))) && (locked || !SP_ITEM(i->data)->isLocked())) {
368 l = g_slist_prepend (l, i->data);
369 }
370 }
371 }
372 if (!ancestor || ancestor->isAncestorOf(SP_OBJECT (i->data))) {
373 l = all_items (SP_OBJECT (i->data), l, hidden, locked);
374 }
375 }
376 return l;
377 }
381 /*########################################################################
382 # BUTTON CLICK HANDLERS (callbacks)
383 ########################################################################*/
385 void
386 Find::onClear()
387 {
388 _entry_text.getEntry()->set_text(Glib::ustring(""));
389 _entry_id.getEntry()->set_text(Glib::ustring(""));
390 _entry_style.getEntry()->set_text(Glib::ustring(""));
391 _entry_attribute.getEntry()->set_text(Glib::ustring(""));
393 _check_all.set_active();
394 }
398 void
399 Find::onFind()
400 {
401 SPDesktop *desktop = getDesktop();
403 bool hidden = _check_include_hidden.get_active();
404 bool locked = _check_include_locked.get_active();
406 GSList *l = NULL;
407 if (_check_search_selection.get_active()) {
408 if (_check_search_layer.get_active()) {
409 l = all_selection_items (desktop->selection, l, desktop->currentLayer(), hidden, locked);
410 } else {
411 l = all_selection_items (desktop->selection, l, NULL, hidden, locked);
412 }
413 } else {
414 if (_check_search_layer.get_active()) {
415 l = all_items (desktop->currentLayer(), l, hidden, locked);
416 } else {
417 l = all_items (SP_DOCUMENT_ROOT (sp_desktop_document (desktop)), l, hidden, locked);
418 }
419 }
420 guint all = g_slist_length (l);
422 bool exact = true;
423 GSList *n = NULL;
424 n = filter_list (l, exact);
425 if (n == NULL) {
426 exact = false;
427 n = filter_list (l, exact);
428 }
430 if (n != NULL) {
431 int count = g_slist_length (n);
432 desktop->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
433 // TRANSLATORS: "%s" is replaced with "exact" or "partial" when this string is displayed
434 ngettext("<b>%d</b> object found (out of <b>%d</b>), %s match.",
435 "<b>%d</b> objects found (out of <b>%d</b>), %s match.",
436 count),
437 count, all, exact? _("exact") : _("partial"));
439 Inkscape::Selection *selection = sp_desktop_selection (desktop);
440 selection->clear();
441 selection->setList(n);
442 scroll_to_show_item (desktop, SP_ITEM(n->data));
443 } else {
444 desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No objects found"));
445 }
446 }
448 void
449 Find::onToggleAlltypes ()
450 {
451 if (_check_all.get_active()) {
452 // explicit toggle to make sure its handler gets called, no matter what was the original state
453 _check_all_shapes.toggled();
454 _check_all_shapes.set_active();
455 _check_all_shapes.hide();
456 _check_paths.hide();
457 _check_texts.hide();
458 _check_groups.hide();
459 _check_clones.hide();
460 _check_images.hide();
461 _check_offsets.hide();
462 } else {
463 // explicit toggle to make sure its handler gets called, no matter what was the original state
464 _check_all_shapes.toggled();
465 _check_all_shapes.set_active();
466 _check_all_shapes.show();
468 _check_paths.set_active();
469 _check_paths.show();
470 _check_texts.set_active();
471 _check_texts.show();
472 _check_groups.set_active();
473 _check_groups.show();
474 _check_clones.set_active();
475 _check_clones.show();
476 _check_images.set_active();
477 _check_images.show();
478 _check_offsets.set_active();
479 _check_offsets.show();
480 }
481 squeeze_window();
482 }
484 void
485 Find::onToggleShapes ()
486 {
487 if (_check_all_shapes.get_active()) {
488 _check_rects.hide();
489 _check_ellipses.hide();
490 _check_stars.hide();
491 _check_spirals.hide();
492 } else {
493 _check_rects.set_active();
494 _check_rects.show();
495 _check_ellipses.set_active();
496 _check_ellipses.show();
497 _check_stars.set_active();
498 _check_stars.show();
499 _check_spirals.set_active();
500 _check_spirals.show();
501 }
502 squeeze_window();
503 }
506 /*########################################################################
507 # UTILITY
508 ########################################################################*/
512 void
513 Find::squeeze_window()
514 {
515 // TO DO: make window as small as possible
516 }
520 } // namespace Dialog
521 } // namespace UI
522 } // namespace Inkscape