Commit 5132f572 authored by Sébastien Wilmet's avatar Sébastien Wilmet

Create the SearchContext in GeditReplaceDialog

parent a5018856
......@@ -490,113 +490,13 @@ do_replace_all (GeditReplaceDialog *dialog,
FALSE);
}
static void
update_regex_error (GeditReplaceDialog *dialog,
GtkSourceSearchContext *search_context)
{
GError *regex_error = gtk_source_search_context_get_regex_error (search_context);
GtkSourceRegexSearchState regex_state = gtk_source_search_context_get_regex_state (search_context);
gedit_replace_dialog_set_search_error (dialog, NULL);
gedit_replace_dialog_set_replace_error (dialog, NULL);
if (regex_error == NULL)
{
return;
}
switch (regex_state)
{
case GTK_SOURCE_REGEX_SEARCH_COMPILATION_ERROR:
case GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR:
gedit_replace_dialog_set_search_error (dialog, regex_error->message);
break;
case GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR:
gedit_replace_dialog_set_replace_error (dialog, regex_error->message);
break;
default:
g_return_if_reached ();
}
g_error_free (regex_error);
}
static void
regex_error_notify_cb (GtkSourceSearchContext *search_context,
GParamSpec *pspec,
GeditReplaceDialog *dialog)
{
GeditDocument *doc;
GtkSourceSearchContext *doc_search_context;
doc = GEDIT_DOCUMENT (gtk_source_search_context_get_buffer (search_context));
doc_search_context = _gedit_document_get_search_context (doc);
if (search_context == doc_search_context)
{
update_regex_error (dialog, search_context);
}
}
static void
search_context_finalized_cb (GeditReplaceDialog *dialog,
GObject *where_the_object_was)
{
gedit_replace_dialog_set_search_error (dialog, NULL);
gedit_replace_dialog_set_replace_error (dialog, NULL);
}
static void
create_search_context (GeditReplaceDialog *dialog,
GeditDocument *doc)
{
GtkSourceSearchContext *search_context;
GtkSourceSearchSettings *search_settings;
search_settings = gedit_replace_dialog_get_search_settings (dialog);
search_context = _gedit_document_get_search_context (doc);
if (search_context != NULL &&
search_settings == gtk_source_search_context_get_settings (search_context))
{
return;
}
search_context = gtk_source_search_context_new (GTK_SOURCE_BUFFER (doc),
search_settings);
g_signal_connect_object (search_context,
"notify::regex-error",
G_CALLBACK (regex_error_notify_cb),
dialog,
0);
g_object_weak_ref (G_OBJECT (search_context),
(GWeakNotify)search_context_finalized_cb,
dialog);
update_regex_error (dialog, search_context);
_gedit_document_set_search_context (doc, search_context);
g_object_unref (search_context);
}
static void
replace_dialog_response_cb (GeditReplaceDialog *dialog,
gint response_id,
GeditWindow *window)
{
GeditDocument *doc;
gedit_debug (DEBUG_COMMANDS);
doc = gedit_window_get_active_document (window);
create_search_context (dialog, doc);
switch (response_id)
{
case GEDIT_REPLACE_DIALOG_FIND_RESPONSE:
......@@ -631,18 +531,6 @@ replace_dialog_destroyed (GeditWindow *window,
NULL);
}
static void
active_tab_changed_cb (GeditWindow *window,
GeditTab *tab,
GeditReplaceDialog *replace_dialog)
{
if (tab != NULL)
{
GeditDocument *doc = gedit_tab_get_document (tab);
create_search_context (replace_dialog, doc);
}
}
static GtkWidget *
create_dialog (GeditWindow *window)
{
......@@ -661,12 +549,6 @@ create_dialog (GeditWindow *window)
(GWeakNotify) replace_dialog_destroyed,
window);
g_signal_connect_object (window,
"active-tab-changed",
G_CALLBACK (active_tab_changed_cb),
dialog,
0);
return dialog;
}
......@@ -696,7 +578,6 @@ _gedit_cmd_search_replace (GtkAction *action,
{
gpointer data;
GtkWidget *replace_dialog;
GeditDocument *doc;
gedit_debug (DEBUG_COMMANDS);
......@@ -713,11 +594,6 @@ _gedit_cmd_search_replace (GtkAction *action,
replace_dialog = GTK_WIDGET (data);
}
doc = gedit_window_get_active_document (window);
g_return_if_fail (doc != NULL);
create_search_context (GEDIT_REPLACE_DIALOG (replace_dialog), doc);
gtk_widget_show (replace_dialog);
last_search_data_restore_position (GEDIT_REPLACE_DIALOG (replace_dialog));
gedit_replace_dialog_present_with_time (GEDIT_REPLACE_DIALOG (replace_dialog),
......
......@@ -31,9 +31,7 @@
#include "gedit-replace-dialog.h"
#include "gedit-history-entry.h"
#include "gedit-utils.h"
#include "gedit-marshal.h"
#include "gedit-dirs.h"
#include "gedit-document.h"
struct _GeditReplaceDialogPrivate
{
......@@ -68,6 +66,43 @@ get_gedit_window (GeditReplaceDialog *dialog)
return NULL;
}
static GeditDocument *
get_active_document (GeditReplaceDialog *dialog)
{
GeditWindow *window = get_gedit_window (dialog);
if (window != NULL)
{
return gedit_window_get_active_document (window);
}
return NULL;
}
static GtkSourceSearchContext *
get_active_search_context (GeditReplaceDialog *dialog)
{
GeditDocument *doc;
GtkSourceSearchContext *search_context;
doc = get_active_document (dialog);
if (doc == NULL)
{
return NULL;
}
search_context = _gedit_document_get_search_context (doc);
if (search_context != NULL &&
dialog->priv->search_settings == gtk_source_search_context_get_settings (search_context))
{
return search_context;
}
return NULL;
}
void
gedit_replace_dialog_present_with_time (GeditReplaceDialog *dialog,
guint32 timestamp)
......@@ -87,6 +122,111 @@ gedit_replace_dialog_delete_event (GtkWidget *widget,
return TRUE;
}
static void
set_error (GtkEntry *entry,
const gchar *error_msg)
{
if (error_msg == NULL || error_msg[0] == '\0')
{
gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
}
else
{
GIcon *icon = g_themed_icon_new_with_default_fallbacks ("dialog-error-symbolic");
gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, icon);
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, error_msg);
g_object_unref (icon);
}
}
static void
set_search_error (GeditReplaceDialog *dialog,
const gchar *error_msg)
{
set_error (GTK_ENTRY (dialog->priv->search_text_entry), error_msg);
}
static void
set_replace_error (GeditReplaceDialog *dialog,
const gchar *error_msg)
{
set_error (GTK_ENTRY (dialog->priv->replace_text_entry), error_msg);
}
static void
update_regex_error (GeditReplaceDialog *dialog)
{
GtkSourceSearchContext *search_context;
GError *regex_error;
GtkSourceRegexSearchState regex_state;
set_search_error (dialog, NULL);
set_replace_error (dialog, NULL);
search_context = get_active_search_context (dialog);
if (search_context == NULL)
{
return;
}
regex_error = gtk_source_search_context_get_regex_error (search_context);
regex_state = gtk_source_search_context_get_regex_state (search_context);
if (regex_error == NULL)
{
return;
}
switch (regex_state)
{
case GTK_SOURCE_REGEX_SEARCH_COMPILATION_ERROR:
case GTK_SOURCE_REGEX_SEARCH_MATCHING_ERROR:
set_search_error (dialog, regex_error->message);
break;
case GTK_SOURCE_REGEX_SEARCH_REPLACE_ERROR:
set_replace_error (dialog, regex_error->message);
break;
default:
g_return_if_reached ();
}
g_error_free (regex_error);
}
static void
create_search_context (GeditReplaceDialog *dialog)
{
GtkSourceSearchContext *search_context = get_active_search_context (dialog);
GeditDocument *doc;
if (search_context != NULL)
{
return;
}
doc = get_active_document (dialog);
search_context = gtk_source_search_context_new (GTK_SOURCE_BUFFER (doc),
dialog->priv->search_settings);
g_signal_connect_object (search_context,
"notify::regex-error",
G_CALLBACK (update_regex_error),
dialog,
G_CONNECT_SWAPPED);
update_regex_error (dialog);
_gedit_document_set_search_context (doc, search_context);
g_object_unref (search_context);
}
static void
gedit_replace_dialog_response (GtkDialog *dialog,
gint response_id)
......@@ -94,6 +234,8 @@ gedit_replace_dialog_response (GtkDialog *dialog,
GeditReplaceDialog *dlg = GEDIT_REPLACE_DIALOG (dialog);
const gchar *str;
create_search_context (GEDIT_REPLACE_DIALOG (dialog));
switch (response_id)
{
case GEDIT_REPLACE_DIALOG_REPLACE_RESPONSE:
......@@ -225,6 +367,13 @@ get_selected_text (GtkTextBuffer *doc,
return TRUE;
}
static void
active_tab_changed_cb (GeditReplaceDialog *dialog)
{
create_search_context (dialog);
update_regex_error (dialog);
}
static void
show_cb (GeditReplaceDialog *dialog)
{
......@@ -241,7 +390,13 @@ show_cb (GeditReplaceDialog *dialog)
return;
}
doc = gedit_window_get_active_document (window);
g_signal_connect_object (window,
"active-tab-changed",
G_CALLBACK (active_tab_changed_cb),
dialog,
G_CONNECT_SWAPPED);
doc = get_active_document (dialog);
if (doc == NULL)
{
......@@ -262,9 +417,19 @@ show_cb (GeditReplaceDialog *dialog)
g_free (escaped_selection);
}
create_search_context (dialog);
g_free (selection);
}
static void
hide_cb (GeditReplaceDialog *dialog)
{
GeditWindow *window = get_gedit_window (dialog);
g_signal_handlers_disconnect_by_func (window, active_tab_changed_cb, dialog);
}
static void
gedit_replace_dialog_init (GeditReplaceDialog *dlg)
{
......@@ -339,6 +504,11 @@ gedit_replace_dialog_init (GeditReplaceDialog *dlg)
"show",
G_CALLBACK (show_cb),
NULL);
g_signal_connect (dlg,
"hide",
G_CALLBACK (hide_cb),
NULL);
}
GtkWidget *
......@@ -372,14 +542,6 @@ gedit_replace_dialog_get_backwards (GeditReplaceDialog *dialog)
return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->backwards_checkbutton));
}
GtkSourceSearchSettings *
gedit_replace_dialog_get_search_settings (GeditReplaceDialog *dialog)
{
g_return_val_if_fail (GEDIT_IS_REPLACE_DIALOG (dialog), NULL);
return dialog->priv->search_settings;
}
/* This function returns the original search text. The search text from the
* search settings has been unescaped, and the escape function is not
* reciprocal. So to avoid bugs, we have to deal with the original search text.
......@@ -392,38 +554,4 @@ gedit_replace_dialog_get_search_text (GeditReplaceDialog *dialog)
return gtk_entry_get_text (GTK_ENTRY (dialog->priv->search_text_entry));
}
static void
set_error (GtkEntry *entry,
const gchar *error_msg)
{
if (error_msg == NULL || error_msg[0] == '\0')
{
gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, NULL);
}
else
{
GIcon *icon = g_themed_icon_new_with_default_fallbacks ("dialog-error-symbolic");
gtk_entry_set_icon_from_gicon (entry, GTK_ENTRY_ICON_SECONDARY, icon);
gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, error_msg);
g_object_unref (icon);
}
}
void
gedit_replace_dialog_set_search_error (GeditReplaceDialog *dialog,
const gchar *error_msg)
{
set_error (GTK_ENTRY (dialog->priv->search_text_entry), error_msg);
}
void
gedit_replace_dialog_set_replace_error (GeditReplaceDialog *dialog,
const gchar *error_msg)
{
set_error (GTK_ENTRY (dialog->priv->replace_text_entry), error_msg);
}
/* ex:set ts=8 noet: */
......@@ -97,14 +97,6 @@ const gchar *gedit_replace_dialog_get_replace_text (GeditReplaceDialog *dialog
gboolean gedit_replace_dialog_get_backwards (GeditReplaceDialog *dialog);
GtkSourceSearchSettings *gedit_replace_dialog_get_search_settings (GeditReplaceDialog *dialog);
void gedit_replace_dialog_set_search_error (GeditReplaceDialog *dialog,
const gchar *error_msg);
void gedit_replace_dialog_set_replace_error (GeditReplaceDialog *dialog,
const gchar *error_msg);
G_END_DECLS
#endif /* __GEDIT_REPLACE_DIALOG_H__ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment