find.cpp revision 8d8dbe5c107a022acda514982d3b7e235cf9f65b
250N/A/*
250N/A * Authors:
250N/A * Bryce W. Harrington <bryce@bryceharrington.org>
250N/A * Johan Engelen <goejendaagh@zonnet.nl>
250N/A * Jon A. Cruz <jon@joncruz.org>
250N/A * Abhishek Sharma
250N/A *
250N/A * Copyright (C) 2004-2006 Authors
250N/A *
250N/A * Released under GNU GPL. Read the file 'COPYING' for more information.
250N/A */
250N/A
250N/A#ifdef HAVE_CONFIG_H
250N/A# include <config.h>
250N/A#endif
250N/A
250N/A#include "find.h"
250N/A#include <gtkmm/widget.h>
250N/A#include "verbs.h"
250N/A
250N/A#include "message-stack.h"
250N/A#include "helper/window.h"
250N/A#include "macros.h"
250N/A#include "inkscape.h"
250N/A#include "desktop.h"
250N/A#include "document.h"
250N/A#include "document-undo.h"
250N/A#include "selection.h"
250N/A#include "desktop-handles.h"
250N/A
250N/A#include "dialogs/dialog-events.h"
250N/A#include "verbs.h"
250N/A#include "interface.h"
250N/A#include "preferences.h"
250N/A#include "sp-text.h"
250N/A#include "sp-flowtext.h"
250N/A#include "text-editing.h"
250N/A#include "sp-tspan.h"
250N/A#include "sp-tref.h"
250N/A#include "selection-chemistry.h"
250N/A#include "sp-defs.h"
250N/A#include "sp-rect.h"
250N/A#include "sp-ellipse.h"
250N/A#include "sp-star.h"
250N/A#include "sp-spiral.h"
250N/A#include "sp-path.h"
250N/A#include "sp-line.h"
250N/A#include "sp-polyline.h"
250N/A#include "sp-item-group.h"
250N/A#include "sp-use.h"
250N/A#include "sp-image.h"
250N/A#include "sp-offset.h"
250N/A#include "sp-root.h"
250N/A#include "xml/repr.h"
250N/A#include "xml/node-iterators.h"
250N/A#include "xml/attribute-record.h"
250N/A
250N/A#include <glibmm/i18n.h>
250N/A
250N/Anamespace Inkscape {
250N/Anamespace UI {
250N/Anamespace Dialog {
250N/A
250N/AFind::Find()
250N/A : UI::Widget::Panel("", "/dialogs/find", SP_VERB_DIALOG_FIND),
250N/A
250N/A entry_find(_("F_ind:"), _("Find objects by their content or properties (exact or partial match)")),
250N/A entry_replace(_("Re_place:"), _("Replace match with this value")),
250N/A
250N/A check_scope_all(_("_All"), _("Search in all layers")),
250N/A check_scope_layer(_("Current _layer"), _("Limit search to the current layer")),
250N/A check_scope_selection(_("S_election"), _("Limit search to the current selection")),
250N/A check_searchin_text(_("Te_xt"), _("Search in text objects")),
250N/A check_searchin_property(_("_Properties"), _("Search in object properties, styles, attributes and IDs")),
250N/A vbox_searchin(0, false),
250N/A frame_searchin(_("Search in")),
250N/A frame_scope(_("Scope")),
250N/A
250N/A check_case_sensitive(_("Case sensiti_ve"), _("Match upper/lower case"), false),
250N/A check_exact_match(_("E_xact match"), _("Match whole objects only"), false),
250N/A check_include_hidden(_("Include _hidden"), _("Include hidden objects in search"), false),
250N/A check_include_locked(_("Include loc_ked"), _("Include locked objects in search"), false),
250N/A expander_options(_("Options")),
250N/A frame_options(_("General")),
250N/A
250N/A check_ids(_("_ID"), _("Search id name"), true),
250N/A check_attributename(_("Attribute _name"), _("Search attribute name"), false),
250N/A check_attributevalue(_("Attribute _value"), _("Search attribute value"), true),
250N/A check_style(_("_Style"), _("Search style"), true),
250N/A check_font(_("_Font"), _("Search fonts"), false),
250N/A frame_properties(_("Properties")),
250N/A
250N/A check_alltypes(_("All types"), _("Search all object types"), true),
250N/A check_rects(_("Rectangles"), _("Search rectangles"), false),
250N/A check_ellipses(_("Ellipses"), _("Search ellipses, arcs, circles"), false),
250N/A check_stars(_("Stars"), _("Search stars and polygons"), false),
250N/A check_spirals(_("Spirals"), _("Search spirals"), false),
250N/A check_paths(_("Paths"), _("Search paths, lines, polylines"), false),
250N/A check_texts(_("Texts"), _("Search text objects"), false),
250N/A check_groups(_("Groups"), _("Search groups"), false),
250N/A check_clones(
250N/A //TRANSLATORS: "Clones" is a noun indicating type of object to find
250N/A C_("Find dialog", "Clones"), _("Search clones"), false),
250N/A
250N/A check_images(_("Images"), _("Search images"), false),
250N/A check_offsets(_("Offsets"), _("Search offset objects"), false),
250N/A frame_types(_("Object types")),
250N/A
250N/A status(""),
250N/A button_find(_("_Find"), _("Select all objects matching the selection criteria")),
250N/A button_replace(_("_Replace All"), _("Replace all matches")),
250N/A _action_replace(false),
250N/A blocked(false),
250N/A desktop(NULL),
250N/A deskTrack()
250N/A
250N/A{
250N/A entry_find.getEntry()->set_width_chars(25);
250N/A entry_replace.getEntry()->set_width_chars(25);
250N/A
250N/A Gtk::RadioButtonGroup grp_searchin = check_searchin_text.get_group();
250N/A check_searchin_property.set_group(grp_searchin);
250N/A vbox_searchin.pack_start(check_searchin_text, false, false);
250N/A vbox_searchin.pack_start(check_searchin_property, false, false);
250N/A frame_searchin.add(vbox_searchin);
250N/A
250N/A Gtk::RadioButtonGroup grp_scope = check_scope_all.get_group();
250N/A check_scope_layer.set_group(grp_scope);
250N/A check_scope_selection.set_group(grp_scope);
250N/A vbox_scope.pack_start(check_scope_all, true, true);
250N/A vbox_scope.pack_start(check_scope_layer, true, true);
250N/A vbox_scope.pack_start(check_scope_selection, true, true);
250N/A frame_scope.add(vbox_scope);
250N/A
250N/A hbox_searchin.set_spacing(4);
250N/A hbox_searchin.pack_start(frame_searchin, true, true);
250N/A hbox_searchin.pack_start(frame_scope, true, true);
250N/A
250N/A vbox_options1.pack_start(check_case_sensitive, true, true);
250N/A vbox_options1.pack_start(check_include_hidden, true, true);
250N/A vbox_options2.pack_start(check_exact_match, true, true);
250N/A vbox_options2.pack_start(check_include_locked, true, true);
250N/A hbox_options.pack_start(vbox_options1, true, true, 4);
250N/A hbox_options.pack_start(vbox_options2, true, true, 4);
250N/A frame_options.add(hbox_options);
250N/A
250N/A hbox_properties1.set_homogeneous(false);
250N/A hbox_properties1.pack_start(check_ids, false, false, 4 );
250N/A hbox_properties1.pack_start(check_style, false, false, 8);
250N/A hbox_properties1.pack_start(check_font, false, false, 8);
250N/A hbox_properties2.set_homogeneous(false);
250N/A hbox_properties2.pack_start(check_attributevalue, false, false, 4);
250N/A hbox_properties2.pack_start(check_attributename, false, false, 4);
250N/A vbox_properties.pack_start(hbox_properties1, true, true, 0);
250N/A vbox_properties.pack_start(hbox_properties2, true, true, 2);
250N/A frame_properties.add(vbox_properties);
250N/A
250N/A vbox_types1.pack_start(check_alltypes, true, true);
250N/A vbox_types1.pack_start(check_paths, true, true);
250N/A vbox_types1.pack_start(check_texts, true, true);
250N/A vbox_types1.pack_start(check_groups, true, true);
250N/A vbox_types1.pack_start(check_clones, true, true);
250N/A vbox_types1.pack_start(check_images, true, true);
250N/A vbox_types2.pack_start(check_offsets, true, true);
250N/A vbox_types2.pack_start(check_rects, true, true);
250N/A vbox_types2.pack_start(check_ellipses, true, true);
250N/A vbox_types2.pack_start(check_stars, true, true);
250N/A vbox_types2.pack_start(check_spirals, true, true);
250N/A hbox_types.pack_start(vbox_types1, true, true, 4);
250N/A hbox_types.pack_start(vbox_types2, true, true, 4);
250N/A frame_types.add(hbox_types);
250N/A
250N/A vbox_expander.pack_start(frame_options, true, true, 4);
250N/A vbox_expander.pack_start(frame_properties, true, true, 4);
250N/A vbox_expander.pack_start(frame_types, true, true, 4);
250N/A
250N/A expander_options.set_use_underline();
250N/A expander_options.add(vbox_expander);
250N/A
250N/A box_buttons.set_layout(Gtk::BUTTONBOX_END);
250N/A box_buttons.set_spacing(4);
250N/A box_buttons.pack_start(button_find, true, true, 6);
250N/A box_buttons.pack_start(button_replace, true, true, 6);
250N/A hboxbutton_row.pack_start(status, true, true, 6);
250N/A hboxbutton_row.pack_end(box_buttons, true, true);
250N/A
250N/A Gtk::Box *contents = _getContents();
250N/A contents->set_spacing(6);
250N/A contents->pack_start(entry_find, false, false);
250N/A contents->pack_start(entry_replace, false, false);
250N/A contents->pack_start(hbox_searchin, false, false);
250N/A contents->pack_start(expander_options, false, false);
250N/A contents->pack_end(hboxbutton_row, false, false);
250N/A
250N/A checkProperties.push_back(&check_ids);
250N/A checkProperties.push_back(&check_style);
250N/A checkProperties.push_back(&check_font);
250N/A checkProperties.push_back(&check_attributevalue);
250N/A checkProperties.push_back(&check_attributename);
250N/A
250N/A checkTypes.push_back(&check_paths);
250N/A checkTypes.push_back(&check_texts);
250N/A checkTypes.push_back(&check_groups);
250N/A checkTypes.push_back(&check_clones);
250N/A checkTypes.push_back(&check_images);
250N/A checkTypes.push_back(&check_offsets);
250N/A checkTypes.push_back(&check_rects);
250N/A checkTypes.push_back(&check_ellipses);
250N/A checkTypes.push_back(&check_stars);
250N/A checkTypes.push_back(&check_spirals);
250N/A checkTypes.push_back(&check_offsets);
250N/A
250N/A // set signals to handle clicks
250N/A expander_options.property_expanded().signal_changed().connect(sigc::mem_fun(*this, &Find::onExpander));
250N/A button_find.signal_clicked().connect(sigc::mem_fun(*this, &Find::onFind));
250N/A button_replace.signal_clicked().connect(sigc::mem_fun(*this, &Find::onReplace));
250N/A check_searchin_text.signal_clicked().connect(sigc::mem_fun(*this, &Find::onSearchinText));
250N/A check_searchin_property.signal_clicked().connect(sigc::mem_fun(*this, &Find::onSearchinProperty));
250N/A check_alltypes.signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleAlltypes));
250N/A
250N/A for(size_t i = 0; i < checkProperties.size(); i++) {
250N/A checkProperties[i]->signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleCheck));
250N/A }
250N/A
250N/A for(size_t i = 0; i < checkTypes.size(); i++) {
250N/A checkTypes[i]->signal_clicked().connect(sigc::mem_fun(*this, &Find::onToggleCheck));
250N/A }
250N/A
250N/A onSearchinText();
250N/A onToggleAlltypes();
250N/A
250N/A desktopChangeConn = deskTrack.connectDesktopChanged( sigc::mem_fun(*this, &Find::setTargetDesktop) );
250N/A deskTrack.connect(GTK_WIDGET(gobj()));
250N/A
250N/A show_all_children();
250N/A
250N/A Inkscape::Selection *selection = sp_desktop_selection (SP_ACTIVE_DESKTOP);
250N/A SPItem *item = selection->singleItem();
250N/A if (item) {
250N/A if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
250N/A gchar *str;
250N/A str = sp_te_get_string_multiline (item);
250N/A entry_find.getEntry()->set_text(str);
250N/A }
250N/A }
250N/A
250N/A button_find.set_can_default();
250N/A //button_find.grab_default(); // activatable by Enter
250N/A entry_find.getEntry()->grab_focus();
250N/A}
250N/A
250N/AFind::~Find()
250N/A{
250N/A desktopChangeConn.disconnect();
250N/A selectChangedConn.disconnect();
250N/A deskTrack.disconnect();
250N/A}
250N/A
250N/Avoid Find::setDesktop(SPDesktop *desktop)
250N/A{
250N/A Panel::setDesktop(desktop);
250N/A deskTrack.setBase(desktop);
250N/A}
250N/A
250N/Avoid Find::setTargetDesktop(SPDesktop *desktop)
250N/A{
250N/A if (this->desktop != desktop) {
250N/A if (this->desktop) {
250N/A selectChangedConn.disconnect();
250N/A }
250N/A this->desktop = desktop;
250N/A if (desktop && desktop->selection) {
250N/A selectChangedConn = desktop->selection->connectChanged(sigc::hide(sigc::mem_fun(*this, &Find::onSelectionChange)));
250N/A }
250N/A }
250N/A}
250N/A
250N/Avoid Find::onSelectionChange(void)
250N/A{
250N/A if (!blocked) {
250N/A status.set_text("");
250N/A }
250N/A}
250N/A
250N/A/*########################################################################
250N/A# FIND helper functions
250N/A########################################################################*/
250N/A
250N/AGlib::ustring Find::find_replace(const gchar *str, const gchar *find, const gchar *replace, bool exact, bool casematch, bool replaceall)
250N/A{
250N/A Glib::ustring ustr = str;
250N/A Glib::ustring ufind = find;
250N/A if (!casematch) {
250N/A ufind = ufind.lowercase();
250N/A }
250N/A gsize n = find_strcmp_pos(ustr.c_str(), ufind.c_str(), exact, casematch);
250N/A while (n != std::string::npos) {
250N/A ustr.replace(n, ufind.length(), replace);
250N/A if (!replaceall) {
250N/A return ustr;
250N/A }
250N/A // Start the next search after the last replace character to avoid infinite loops (replace "a" with "aaa" etc)
250N/A n = find_strcmp_pos(ustr.c_str(), ufind.c_str(), exact, casematch, n + strlen(replace) + 1);
250N/A }
250N/A return ustr;
250N/A}
250N/A
250N/Agsize Find::find_strcmp_pos(const gchar *str, const gchar *find, bool exact, bool casematch, gsize start/*=0*/)
250N/A{
250N/A Glib::ustring ustr = str;
250N/A Glib::ustring ufind = find;
250N/A
250N/A if (!casematch) {
250N/A ustr = ustr.lowercase();
250N/A ufind = ufind.lowercase();
250N/A }
250N/A
250N/A gsize pos = std::string::npos;
250N/A if (exact) {
250N/A if (ustr == ufind) {
250N/A pos = 0;
250N/A }
250N/A } else {
250N/A pos = ustr.find(ufind, start);
250N/A }
250N/A
250N/A return pos;
250N/A}
250N/A
250N/A
250N/Abool Find::find_strcmp(const gchar *str, const gchar *find, bool exact, bool casematch)
250N/A{
250N/A return (std::string::npos != find_strcmp_pos(str, find, exact, casematch));
250N/A}
250N/A
250N/Abool
250N/AFind::item_text_match (SPItem *item, const gchar *find, bool exact, bool casematch, bool replace/*=false*/)
250N/A{
250N/A if (item->getRepr() == NULL) {
250N/A return false;
250N/A }
250N/A
250N/A if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
250N/A const gchar *item_text = sp_te_get_string_multiline (item);
250N/A if (item_text == NULL) {
250N/A return false;
250N/A }
250N/A bool found = find_strcmp(item_text, find, exact, casematch);
250N/A
250N/A if (found && replace) {
250N/A Glib::ustring ufind = find;
250N/A if (!casematch) {
250N/A ufind = ufind.lowercase();
250N/A }
250N/A
250N/A Inkscape::Text::Layout const *layout = te_get_layout (item);
250N/A if (!layout) {
250N/A return found;
250N/A }
250N/A
250N/A gchar* replace_text = g_strdup(entry_replace.getEntry()->get_text().c_str());
250N/A gsize n = find_strcmp_pos(item_text, ufind.c_str(), exact, casematch);
250N/A static Inkscape::Text::Layout::iterator _begin_w;
250N/A static Inkscape::Text::Layout::iterator _end_w;
250N/A while (n != std::string::npos) {
250N/A _begin_w = layout->charIndexToIterator(n);
250N/A _end_w = layout->charIndexToIterator(n + strlen(find));
250N/A sp_te_replace(item, _begin_w, _end_w, replace_text);
250N/A item_text = sp_te_get_string_multiline (item);
250N/A n = find_strcmp_pos(item_text, ufind.c_str(), exact, casematch, n + strlen(replace_text) + 1);
250N/A }
250N/A
250N/A g_free(replace_text);
250N/A }
250N/A
250N/A return found;
250N/A }
250N/A return false;
250N/A}
250N/A
250N/A
250N/Abool
250N/AFind::item_id_match (SPItem *item, const gchar *id, bool exact, bool casematch, bool replace/*=false*/)
250N/A{
250N/A if (item->getRepr() == NULL) {
250N/A return false;
250N/A }
250N/A
250N/A if (SP_IS_STRING(item)) { // SPStrings have "on demand" ids which are useless for searching
250N/A return false;
250N/A }
250N/A
250N/A const gchar *item_id = item->getRepr()->attribute("id");
250N/A if (item_id == NULL) {
250N/A return false;
250N/A }
250N/A
250N/A bool found = find_strcmp(item_id, id, exact, casematch);
250N/A
250N/A if (found && replace) {
250N/A gchar * replace_text = g_strdup(entry_replace.getEntry()->get_text().c_str());
250N/A Glib::ustring new_item_style = find_replace(item_id, id, replace_text , exact, casematch, true);
250N/A if (new_item_style != item_id) {
250N/A item->getRepr()->setAttribute("id", new_item_style.data());
250N/A }
250N/A g_free(replace_text);
250N/A }
250N/A
250N/A return found;
250N/A}
250N/A
250N/Abool
250N/AFind::item_style_match (SPItem *item, const gchar *text, bool exact, bool casematch, bool replace/*=false*/)
250N/A{
250N/A if (item->getRepr() == NULL) {
250N/A return false;
250N/A }
250N/A
250N/A gchar *item_style = g_strdup(item->getRepr()->attribute("style"));
250N/A if (item_style == NULL) {
250N/A return false;
250N/A }
250N/A
250N/A bool found = find_strcmp(item_style, text, exact, casematch);
250N/A
250N/A if (found && replace) {
250N/A gchar * replace_text = g_strdup(entry_replace.getEntry()->get_text().c_str());
250N/A Glib::ustring new_item_style = find_replace(item_style, text, replace_text , exact, casematch, true);
250N/A if (new_item_style != item_style) {
250N/A item->getRepr()->setAttribute("style", new_item_style.data());
250N/A }
250N/A g_free(replace_text);
250N/A }
250N/A
250N/A g_free(item_style);
250N/A return found;
250N/A}
250N/A
250N/Abool Find::item_attr_match(SPItem *item, const gchar *text, bool exact, bool /*casematch*/, bool replace/*=false*/)
250N/A{
250N/A bool found = false;
250N/A
250N/A if (item->getRepr() == NULL) {
250N/A return false;
250N/A }
250N/A
250N/A gchar *attr_value = g_strdup(item->getRepr()->attribute(text));
250N/A if (exact) {
250N/A found = (attr_value != NULL);
250N/A } else {
250N/A found = item->getRepr()->matchAttributeName(text);
250N/A }
250N/A g_free(attr_value);
250N/A
250N/A // TODO - Rename attribute name ?
250N/A if (found && replace) {
250N/A found = false;
250N/A }
2759N/A
2759N/A return found;
2759N/A}
2759N/A
2759N/Abool Find::item_attrvalue_match(SPItem *item, const gchar *text, bool exact, bool casematch, bool replace/*=false*/)
2759N/A{
2759N/A bool ret = false;
2759N/A
2759N/A if (item->getRepr() == NULL) {
2759N/A return false;
2759N/A }
2759N/A
2759N/A Inkscape::Util::List<Inkscape::XML::AttributeRecord const> iter = item->getRepr()->attributeList();
2759N/A for (; iter; ++iter) {
2759N/A const gchar* key = g_quark_to_string(iter->key);
2759N/A gchar *attr_value = g_strdup(item->getRepr()->attribute(key));
2759N/A bool found = find_strcmp(attr_value, text, exact, casematch);
2759N/A if (found) {
2759N/A ret = true;
2759N/A }
2759N/A
2759N/A if (found && replace) {
2759N/A gchar * replace_text = g_strdup(entry_replace.getEntry()->get_text().c_str());
2759N/A Glib::ustring new_item_style = find_replace(attr_value, text, replace_text , exact, casematch, true);
2759N/A if (new_item_style != attr_value) {
2759N/A item->getRepr()->setAttribute(key, new_item_style.data());
2759N/A }
2759N/A }
2759N/A
2759N/A g_free(attr_value);
2759N/A }
2759N/A
2759N/A return ret;
2759N/A}
2759N/A
2759N/A
2759N/Abool Find::item_font_match(SPItem *item, const gchar *text, bool exact, bool casematch, bool /*replace*/ /*=false*/)
2759N/A{
2759N/A bool ret = false;
2759N/A
2759N/A if (item->getRepr() == NULL) {
2759N/A return false;
2759N/A }
2759N/A
250N/A const gchar *item_style = item->getRepr()->attribute("style");
250N/A if (item_style == NULL) {
2759N/A return false;
}
std::vector<Glib::ustring> vFontTokenNames;
vFontTokenNames.push_back("font-family:");
vFontTokenNames.push_back("-inkscape-font-specification:");
std::vector<Glib::ustring> vStyleTokens = Glib::Regex::split_simple(";", item_style);
for(size_t i=0; i<vStyleTokens.size(); i++) {
Glib::ustring token = vStyleTokens[i];
for(size_t j=0; j<vFontTokenNames.size(); j++) {
if ( token.find(vFontTokenNames[j]) != std::string::npos) {
Glib::ustring font1 = Glib::ustring(vFontTokenNames[j]).append(text);
bool found = find_strcmp(token.c_str(), font1.c_str(), exact, casematch);
if (found) {
ret = true;
if (_action_replace) {
gchar *replace_text = g_strdup(entry_replace.getEntry()->get_text().c_str());
gchar *orig_str = g_strdup(token.c_str());
// Exact match fails since the "font-family:" is in the token, since the find was exact it still works with false below
Glib::ustring new_item_style = find_replace(orig_str, text, replace_text , false /*exact*/, casematch, true);
if (new_item_style != orig_str) {
vStyleTokens.at(i) = new_item_style;
}
g_free(orig_str);
g_free(replace_text);
}
}
}
}
}
if (ret && _action_replace) {
Glib::ustring new_item_style;
for(size_t i=0; i<vStyleTokens.size(); i++) {
new_item_style.append(vStyleTokens.at(i)).append(";");
}
new_item_style.erase(new_item_style.size()-1);
item->getRepr()->setAttribute("style", new_item_style.data());
}
return ret;
}
GSList *
Find::filter_fields (GSList *l, bool exact, bool casematch)
{
Glib::ustring tmp = entry_find.getEntry()->get_text();
if (tmp.empty()) {
return l;
}
gchar* text = g_strdup(tmp.c_str());
GSList *in = l;
GSList *out = NULL;
if (check_searchin_text.get_active()) {
for (GSList *i = in; i != NULL; i = i->next) {
if (item_text_match (SP_ITEM(i->data), text, exact, casematch)) {
if (!g_slist_find(out, i->data)) {
out = g_slist_prepend (out, i->data);
if (_action_replace) {
item_text_match (SP_ITEM(i->data), text, exact, casematch, _action_replace);
}
}
}
}
}
else if (check_searchin_property.get_active()) {
bool ids = check_ids.get_active();
bool style = check_style.get_active();
bool font = check_font.get_active();
bool attrname = check_attributename.get_active();
bool attrvalue = check_attributevalue.get_active();
if (ids) {
for (GSList *i = in; i != NULL; i = i->next) {
if (item_id_match (SP_ITEM(i->data), text, exact, casematch)) {
if (!g_slist_find(out, i->data)) {
out = g_slist_prepend (out, i->data);
if (_action_replace) {
item_id_match (SP_ITEM(i->data), text, exact, casematch, _action_replace);
}
}
}
}
}
if (style) {
for (GSList *i = in; i != NULL; i = i->next) {
if (item_style_match (SP_ITEM(i->data), text, exact, casematch)) {
if (!g_slist_find(out, i->data))
if (!g_slist_find(out, i->data)) {
out = g_slist_prepend (out, i->data);
if (_action_replace) {
item_style_match (SP_ITEM(i->data), text, exact, casematch, _action_replace);
}
}
}
}
}
if (attrname) {
for (GSList *i = in; i != NULL; i = i->next) {
if (item_attr_match (SP_ITEM(i->data), text, exact, casematch)) {
if (!g_slist_find(out, i->data)) {
out = g_slist_prepend (out, i->data);
if (_action_replace) {
item_attr_match (SP_ITEM(i->data), text, exact, casematch, _action_replace);
}
}
}
}
}
if (attrvalue) {
for (GSList *i = in; i != NULL; i = i->next) {
if (item_attrvalue_match (SP_ITEM(i->data), text, exact, casematch)) {
if (!g_slist_find(out, i->data)) {
out = g_slist_prepend (out, i->data);
if (_action_replace) {
item_attrvalue_match (SP_ITEM(i->data), text, exact, casematch, _action_replace);
}
}
}
}
}
if (font) {
for (GSList *i = in; i != NULL; i = i->next) {
if (item_font_match (SP_ITEM(i->data), text, exact, casematch)) {
if (!g_slist_find(out, i->data)) {
out = g_slist_prepend (out, i->data);
if (_action_replace) {
item_font_match (SP_ITEM(i->data), text, exact, casematch, _action_replace);
}
}
}
}
}
}
g_free(text);
return out;
}
bool
Find::item_type_match (SPItem *item)
{
bool all =check_alltypes.get_active();
if ( SP_IS_RECT(item)) {
return ( all ||check_rects.get_active());
} else if (SP_IS_GENERICELLIPSE(item) || SP_IS_ELLIPSE(item) || SP_IS_ARC(item) || SP_IS_CIRCLE(item)) {
return ( all || check_ellipses.get_active());
} else if (SP_IS_STAR(item) || SP_IS_POLYGON(item)) {
return ( all || check_stars.get_active());
} else if (SP_IS_SPIRAL(item)) {
return ( all || check_spirals.get_active());
} else if (SP_IS_PATH(item) || SP_IS_LINE(item) || SP_IS_POLYLINE(item)) {
return (all || check_paths.get_active());
} else if (SP_IS_TEXT(item) || SP_IS_TSPAN(item) || SP_IS_TREF(item) || SP_IS_STRING(item)) {
return (all || check_texts.get_active());
} else if (SP_IS_GROUP(item) && !desktop->isLayer(item) ) { // never select layers!
return (all || check_groups.get_active());
} else if (SP_IS_USE(item)) {
return (all || check_clones.get_active());
} else if (SP_IS_IMAGE(item)) {
return (all || check_images.get_active());
} else if (SP_IS_OFFSET(item)) {
return (all || check_offsets.get_active());
}
return false;
}
GSList *
Find::filter_types (GSList *l)
{
GSList *n = NULL;
for (GSList *i = l; i != NULL; i = i->next) {
if (item_type_match (SP_ITEM(i->data))) {
n = g_slist_prepend (n, i->data);
}
}
return n;
}
GSList *
Find::filter_list (GSList *l, bool exact, bool casematch)
{
l = filter_types (l);
l = filter_fields (l, exact, casematch);
return l;
}
GSList *
Find::all_items (SPObject *r, GSList *l, bool hidden, bool locked)
{
if (SP_IS_DEFS(r)) {
return l; // we're not interested in items in defs
}
if (!strcmp(r->getRepr()->name(), "svg:metadata")) {
return l; // we're not interested in metadata
}
for (SPObject *child = r->firstChild(); child; child = child->getNext()) {
if (SP_IS_ITEM(child) && !child->cloned && !desktop->isLayer(SP_ITEM(child))) {
SPItem *item = reinterpret_cast<SPItem *>(child);
if ((hidden || !desktop->itemIsHidden(item)) && (locked || !item->isLocked())) {
l = g_slist_prepend (l, child);
}
}
l = all_items (child, l, hidden, locked);
}
return l;
}
GSList *
Find::all_selection_items (Inkscape::Selection *s, GSList *l, SPObject *ancestor, bool hidden, bool locked)
{
for (GSList *i = (GSList *) s->itemList(); i != NULL; i = i->next) {
if (SP_IS_ITEM (i->data) && !reinterpret_cast<SPItem *>(i->data)->cloned && !desktop->isLayer(SP_ITEM(i->data))) {
SPItem *item = reinterpret_cast<SPItem *>(i->data);
if (!ancestor || ancestor->isAncestorOf(item)) {
if ((hidden || !desktop->itemIsHidden(item)) && (locked || !item->isLocked())) {
l = g_slist_prepend (l, i->data);
}
}
}
if (!ancestor || ancestor->isAncestorOf(SP_OBJECT (i->data))) {
l = all_items (SP_OBJECT (i->data), l, hidden, locked);
}
}
return l;
}
/*########################################################################
# BUTTON CLICK HANDLERS (callbacks)
########################################################################*/
void
Find::onFind()
{
_action_replace = false;
onAction();
// Return focus to the find entry
entry_find.getEntry()->grab_focus();
}
void
Find::onReplace()
{
if (entry_find.getEntry()->get_text().length() < 1) {
status.set_text(_("Nothing to replace"));
return;
}
_action_replace = true;
onAction();
// Return focus to the find entry
entry_find.getEntry()->grab_focus();
}
void
Find::onAction()
{
bool hidden = check_include_hidden.get_active();
bool locked = check_include_locked.get_active();
bool exact = check_exact_match.get_active();
bool casematch = check_case_sensitive.get_active();
blocked = true;
GSList *l = NULL;
if (check_scope_selection.get_active()) {
if (check_scope_layer.get_active()) {
l = all_selection_items (desktop->selection, l, desktop->currentLayer(), hidden, locked);
} else {
l = all_selection_items (desktop->selection, l, NULL, hidden, locked);
}
} else {
if (check_scope_layer.get_active()) {
l = all_items (desktop->currentLayer(), l, hidden, locked);
} else {
l = all_items(sp_desktop_document(desktop)->getRoot(), l, hidden, locked);
}
}
guint all = g_slist_length (l);
GSList *n = filter_list (l, exact, casematch);
if (n != NULL) {
int count = g_slist_length (n);
desktop->messageStack()->flashF(Inkscape::NORMAL_MESSAGE,
// TRANSLATORS: "%s" is replaced with "exact" or "partial" when this string is displayed
ngettext("<b>%d</b> object found (out of <b>%d</b>), %s match.",
"<b>%d</b> objects found (out of <b>%d</b>), %s match.",
count),
count, all, exact? _("exact") : _("partial"));
if (_action_replace){
// TRANSLATORS: "%1" is replaced with the number of matches
status.set_text(Glib::ustring::compose(ngettext("%1 match replaced","%1 matches replaced",count), count));
}
else {
// TRANSLATORS: "%1" is replaced with the number of matches
status.set_text(Glib::ustring::compose(ngettext("%1 object found","%1 objects found",count), count));
}
Inkscape::Selection *selection = sp_desktop_selection (desktop);
selection->clear();
selection->setList(n);
scroll_to_show_item (desktop, SP_ITEM(n->data));
if (_action_replace) {
DocumentUndo::done(sp_desktop_document(desktop), SP_VERB_CONTEXT_TEXT, _("Text Replace"));
}
} else {
status.set_text(_("Nothing found"));
if (!check_scope_selection.get_active()) {
Inkscape::Selection *selection = sp_desktop_selection (desktop);
selection->clear();
}
desktop->messageStack()->flash(Inkscape::WARNING_MESSAGE, _("No objects found"));
}
blocked = false;
}
void
Find::onToggleCheck ()
{
bool objectok = false;
status.set_text("");
if (check_alltypes.get_active()) {
objectok = true;
}
for(int i = 0; i < 11; i++) {
if (checkTypes[i]->get_active()) {
objectok = true;
}
}
if (!objectok) {
status.set_text(_("Select an object type"));
}
bool propertyok = false;
if (!check_searchin_property.get_active()) {
propertyok = true;
} else {
for(size_t i = 0; i < checkProperties.size(); i++) {
if (checkProperties[i]->get_active()) {
propertyok = true;
}
}
}
if (!propertyok) {
status.set_text(_("Select a property"));
}
// Can't replace attribute names
bool attributenameyok = !check_attributename.get_active();
button_find.set_sensitive(objectok && propertyok);
button_replace.set_sensitive(objectok && propertyok && attributenameyok);
}
void
Find::onToggleAlltypes ()
{
bool all =check_alltypes.get_active();
for(size_t i = 0; i < checkTypes.size(); i++) {
checkTypes[i]->set_sensitive(!all);
}
onToggleCheck();
}
void
Find::onSearchinText ()
{
searchinToggle(false);
onToggleCheck();
}
void
Find::onSearchinProperty ()
{
searchinToggle(true);
onToggleCheck();
}
void
Find::searchinToggle(bool on)
{
for(size_t i = 0; i < checkProperties.size(); i++) {
checkProperties[i]->set_sensitive(on);
}
}
void
Find::onExpander ()
{
if (!expander_options.get_expanded())
squeeze_window();
}
/*########################################################################
# UTILITY
########################################################################*/
void
Find::squeeze_window()
{
// TODO: resize dialog window when the expander is closed
// set_size_request(-1, -1);
}
} // namespace Dialog
} // namespace UI
} // namespace Inkscape
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :