1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3
* gedit-dialog-replace.c
4
* This file is part of gedit
6
* Copyright (C) 2001-2002 Paolo Maggi
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330,
21
* Boston, MA 02111-1307, USA.
25
* Modified by the gedit Team, 1998-2002. See the AUTHORS file for a
26
* list of people on the gedit Team.
27
* See the ChangeLog files for a list of changes.
36
#include <glade/glade-xml.h>
37
#include <libgnome/libgnome.h>
38
#include <libgnomeui/gnome-entry.h>
41
#include "gedit-mdi.h"
42
#include "gedit-utils.h"
43
#include "gedit-dialogs.h"
44
#include "gedit-document.h"
45
#include "gedit-mdi-child.h"
46
#include "gedit-view.h"
47
#include "gedit-debug.h"
48
#include "gedit-utils.h"
49
#include "gedit-menus.h"
51
#define GEDIT_RESPONSE_FIND 101
52
#define GEDIT_RESPONSE_REPLACE 102
53
#define GEDIT_RESPONSE_REPLACE_ALL 103
55
typedef struct _GeditDialogReplace GeditDialogReplace;
56
typedef struct _GeditDialogFind GeditDialogFind;
58
struct _GeditDialogReplace {
61
GtkWidget *search_entry;
62
GtkWidget *replace_entry;
63
GtkWidget *search_entry_list;
64
GtkWidget *replace_entry_list;
65
GtkWidget *match_case_checkbutton;
66
GtkWidget *entire_word_checkbutton;
67
GtkWidget *wrap_around_checkbutton;
68
GtkWidget *search_backwards_checkbutton;
71
struct _GeditDialogFind {
74
GtkWidget *search_entry;
75
GtkWidget *search_entry_list;
76
GtkWidget *case_sensitive;
77
GtkWidget *match_case_checkbutton;
78
GtkWidget *entire_word_checkbutton;
79
GtkWidget *wrap_around_checkbutton;
80
GtkWidget *search_backwards_checkbutton;
83
static void dialog_destroyed (GtkObject *obj, void **dialog_pointer);
85
static void dialog_find_response_handler (GtkDialog *dlg, gint res_id, GeditDialogFind *find_dialog);
87
static void find_dlg_find_button_pressed (GeditDialogFind * dialog);
89
static void replace_dlg_find_button_pressed (GeditDialogReplace * dialog);
90
static void replace_dlg_replace_button_pressed (GeditDialogReplace * dialog);
91
static void replace_dlg_replace_all_button_pressed (GeditDialogReplace * dialog);
93
static void insert_text_handler (GtkEditable *editable, const gchar *text, gint length, gint *position);
95
static GeditDialogReplace *dialog_replace_get_dialog (void);
96
static GeditDialogFind *dialog_find_get_dialog (void);
98
static GQuark was_wrap_around_id = 0;
99
static GQuark was_entire_word_id = 0;
100
static GQuark was_case_sensitive_id = 0;
101
static GQuark was_search_backwards_id = 0;
104
gedit_was_wrap_around_quark (void)
106
if (!was_wrap_around_id)
107
was_wrap_around_id = g_quark_from_static_string ("GeditWasWrapAround");
109
return was_wrap_around_id;
113
dialog_destroyed (GtkObject *obj, void **dialog_pointer)
115
gedit_debug (DEBUG_SEARCH, "");
117
if (dialog_pointer != NULL)
119
g_free (*dialog_pointer);
120
*dialog_pointer = NULL;
125
text_not_found_dialog (const gchar *text, GtkWindow *parent)
127
GtkWidget *message_dlg;
129
g_return_if_fail (text != NULL);
131
message_dlg = gtk_message_dialog_new (
133
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
136
_("The text \"%s\" was not found."), text);
138
gtk_dialog_set_default_response (GTK_DIALOG (message_dlg), GTK_RESPONSE_OK);
140
gtk_window_set_resizable (GTK_WINDOW (message_dlg), FALSE);
142
gtk_dialog_run (GTK_DIALOG (message_dlg));
143
gtk_widget_destroy (message_dlg);
147
update_menu_items_sensitivity (void)
149
BonoboWindow* active_window = NULL;
150
GeditDocument* doc = NULL;
151
BonoboUIComponent *ui_component;
154
gedit_debug (DEBUG_SEARCH, "");
156
active_window = gedit_get_active_window ();
157
g_return_if_fail (active_window != NULL);
159
ui_component = bonobo_mdi_get_ui_component_from_window (active_window);
160
g_return_if_fail (ui_component != NULL);
162
doc = gedit_get_active_document ();
163
g_return_if_fail (doc != NULL);
165
lst = gedit_document_get_last_searched_text (doc);
167
gedit_menus_set_verb_sensitive (ui_component, "/commands/SearchFindAgain", lst != NULL);
172
dialog_replace_response_handler (GtkDialog *dlg, gint res_id, GeditDialogReplace *replace_dialog)
174
gedit_debug (DEBUG_SEARCH, "");
177
case GEDIT_RESPONSE_FIND:
178
replace_dlg_find_button_pressed (replace_dialog);
181
case GEDIT_RESPONSE_REPLACE:
182
replace_dlg_replace_button_pressed (replace_dialog);
185
case GEDIT_RESPONSE_REPLACE_ALL:
186
replace_dlg_replace_all_button_pressed (replace_dialog);
189
gtk_widget_destroy (replace_dialog->dialog);
194
dialog_find_response_handler (GtkDialog *dlg, gint res_id, GeditDialogFind *find_dialog)
196
gedit_debug (DEBUG_SEARCH, "");
199
case GEDIT_RESPONSE_FIND:
200
find_dlg_find_button_pressed (find_dialog);
204
gtk_widget_destroy (find_dialog->dialog);
209
replace_search_entry_changed (GtkEditable *editable, GeditDialogReplace *dialog)
211
const gchar *search_string;
213
search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
214
g_return_if_fail (search_string != NULL);
216
if (strlen (search_string) <= 0)
218
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
219
GEDIT_RESPONSE_FIND, FALSE);
220
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
221
GEDIT_RESPONSE_REPLACE, FALSE);
222
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
223
GEDIT_RESPONSE_REPLACE_ALL, FALSE);
227
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
228
GEDIT_RESPONSE_FIND, TRUE);
229
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
230
GEDIT_RESPONSE_REPLACE_ALL, TRUE);
234
static GeditDialogReplace *
235
dialog_replace_get_dialog (void)
237
static GeditDialogReplace *dialog = NULL;
241
GtkWidget *replace_with_label;
243
gedit_debug (DEBUG_SEARCH, "");
245
window = GTK_WINDOW (gedit_get_active_window ());
249
gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
250
GTK_WINDOW (window));
251
gtk_window_present (GTK_WINDOW (dialog->dialog));
252
gtk_widget_grab_focus (dialog->dialog);
257
gui = glade_xml_new ( GEDIT_GLADEDIR "replace.glade2",
258
"replace_dialog_content", NULL);
261
gedit_warning (window,
263
GEDIT_GLADEDIR "replace.glade2");
267
dialog = g_new0 (GeditDialogReplace, 1);
269
dialog->dialog = gtk_dialog_new_with_buttons (_("Replace"),
271
GTK_DIALOG_DESTROY_WITH_PARENT,
276
g_return_val_if_fail (dialog->dialog != NULL, NULL);
278
gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE);
279
gtk_dialog_set_has_separator (GTK_DIALOG (dialog->dialog), FALSE);
281
gtk_dialog_add_button (GTK_DIALOG (dialog->dialog),
282
_("Replace _All"), GEDIT_RESPONSE_REPLACE_ALL);
284
gedit_dialog_add_button (GTK_DIALOG (dialog->dialog),
285
_("_Replace"), GTK_STOCK_FIND_AND_REPLACE,
286
GEDIT_RESPONSE_REPLACE);
288
gtk_dialog_add_button (GTK_DIALOG (dialog->dialog),
289
GTK_STOCK_FIND, GEDIT_RESPONSE_FIND);
291
content = glade_xml_get_widget (gui, "replace_dialog_content");
292
dialog->search_entry = glade_xml_get_widget (gui, "search_for_text_entry");
293
dialog->replace_entry = glade_xml_get_widget (gui, "replace_with_text_entry");
294
dialog->search_entry_list = glade_xml_get_widget (gui, "search_for_text_entry_list");
295
dialog->replace_entry_list = glade_xml_get_widget (gui, "replace_with_text_entry_list");
296
replace_with_label = glade_xml_get_widget (gui, "replace_with_label");
297
dialog->match_case_checkbutton = glade_xml_get_widget (gui, "match_case_checkbutton");
298
dialog->wrap_around_checkbutton = glade_xml_get_widget (gui, "wrap_around_checkbutton");
299
dialog->entire_word_checkbutton = glade_xml_get_widget (gui, "entire_word_checkbutton");
300
dialog->search_backwards_checkbutton = glade_xml_get_widget (gui, "search_backwards_checkbutton");
303
!dialog->search_entry ||
304
!dialog->replace_entry ||
305
!dialog->search_entry_list ||
306
!dialog->replace_entry_list ||
307
!replace_with_label ||
308
!dialog->match_case_checkbutton ||
309
!dialog->entire_word_checkbutton ||
310
!dialog->wrap_around_checkbutton ||
311
!dialog->search_backwards_checkbutton)
313
gedit_warning (window,
315
GEDIT_GLADEDIR "replace.glade2");
319
gtk_widget_show (replace_with_label);
320
gtk_widget_show (dialog->replace_entry);
321
gtk_widget_show (dialog->replace_entry_list);
323
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog->dialog)->vbox),
324
content, FALSE, FALSE, 0);
326
gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog),
327
GEDIT_RESPONSE_FIND);
329
g_signal_connect (G_OBJECT (dialog->search_entry_list), "changed",
330
G_CALLBACK (replace_search_entry_changed), dialog);
332
g_signal_connect (G_OBJECT (dialog->search_entry), "insert_text",
333
G_CALLBACK (insert_text_handler), NULL);
335
g_signal_connect (G_OBJECT (dialog->replace_entry), "insert_text",
336
G_CALLBACK (insert_text_handler), NULL);
338
g_signal_connect (G_OBJECT (dialog->dialog), "destroy",
339
G_CALLBACK (dialog_destroyed), &dialog);
341
g_signal_connect (G_OBJECT (dialog->dialog), "response",
342
G_CALLBACK (dialog_replace_response_handler), dialog);
344
g_object_unref (G_OBJECT (gui));
350
find_search_entry_changed (GtkEditable *editable, GeditDialogReplace *dialog)
352
const gchar *search_string;
354
search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
355
g_return_if_fail (search_string != NULL);
357
if (strlen (search_string) <= 0)
358
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
359
GEDIT_RESPONSE_FIND, FALSE);
361
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
362
GEDIT_RESPONSE_FIND, TRUE);
365
static GeditDialogFind *
366
dialog_find_get_dialog (void)
368
static GeditDialogFind *dialog = NULL;
372
GtkWidget *replace_with_label;
373
GtkWidget *replace_entry;
376
gedit_debug (DEBUG_SEARCH, "");
378
window = GTK_WINDOW (gedit_get_active_window ());
382
gtk_window_set_transient_for (GTK_WINDOW (dialog->dialog),
383
GTK_WINDOW (window));
384
gtk_window_present (GTK_WINDOW (dialog->dialog));
385
gtk_widget_grab_focus (dialog->dialog);
390
gui = glade_xml_new (GEDIT_GLADEDIR "replace.glade2",
391
"replace_dialog_content", NULL);
394
gedit_warning (window,
396
GEDIT_GLADEDIR "replace.glade2");
400
dialog = g_new0 (GeditDialogFind, 1);
402
dialog->dialog = gtk_dialog_new_with_buttons (_("Find"),
404
GTK_DIALOG_DESTROY_WITH_PARENT,
411
g_return_val_if_fail (dialog->dialog != NULL, NULL);
413
gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE);
414
gtk_dialog_set_has_separator (GTK_DIALOG (dialog->dialog), FALSE);
416
content = glade_xml_get_widget (gui, "replace_dialog_content");
418
dialog->search_entry = glade_xml_get_widget (gui, "search_for_text_entry");
419
dialog->search_entry_list = glade_xml_get_widget (gui, "search_for_text_entry_list");
420
replace_entry = glade_xml_get_widget (gui, "replace_with_text_entry_list");
422
replace_with_label = glade_xml_get_widget (gui, "replace_with_label");
424
dialog->match_case_checkbutton = glade_xml_get_widget (gui, "match_case_checkbutton");
425
dialog->wrap_around_checkbutton = glade_xml_get_widget (gui, "wrap_around_checkbutton");
426
dialog->entire_word_checkbutton = glade_xml_get_widget (gui, "entire_word_checkbutton");
427
dialog->search_backwards_checkbutton = glade_xml_get_widget (gui, "search_backwards_checkbutton");
429
table = glade_xml_get_widget (gui, "table");
433
!dialog->search_entry ||
434
!dialog->search_entry_list ||
436
!replace_with_label ||
437
!dialog->match_case_checkbutton ||
438
!dialog->entire_word_checkbutton ||
439
!dialog->wrap_around_checkbutton ||
440
!dialog->search_backwards_checkbutton)
442
gedit_warning (window,
444
GEDIT_GLADEDIR "replace.glade2");
448
gtk_widget_hide (replace_with_label);
449
gtk_widget_hide (replace_entry);
451
gtk_table_set_row_spacings (GTK_TABLE (table), 0);
453
gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog->dialog)->vbox),
454
content, FALSE, FALSE, 0);
456
gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog),
457
GEDIT_RESPONSE_FIND);
459
g_signal_connect (G_OBJECT (dialog->search_entry_list), "changed",
460
G_CALLBACK (find_search_entry_changed), dialog);
462
g_signal_connect (G_OBJECT (dialog->search_entry), "insert_text",
463
G_CALLBACK (insert_text_handler), NULL);
465
g_signal_connect(G_OBJECT (dialog->dialog), "destroy",
466
G_CALLBACK (dialog_destroyed), &dialog);
468
g_signal_connect(G_OBJECT (dialog->dialog), "response",
469
G_CALLBACK (dialog_find_response_handler), dialog);
471
g_object_unref (G_OBJECT (gui));
477
escape_search_text (const gchar* text)
484
g_return_val_if_fail (text != NULL, NULL);
486
gedit_debug (DEBUG_SEARCH, "Text: %s", text);
488
length = strlen (text);
490
str = g_string_new ("");
498
next = g_utf8_next_char (p);
503
g_string_append (str, "\\n");
506
g_string_append (str, "\\r");
509
g_string_append (str, "\\t");
512
g_string_append_len (str, p, next - p);
519
return g_string_free (str, FALSE);
523
insert_text_handler (GtkEditable *editable, const gchar *text, gint length, gint *position)
525
static gboolean insert_text = FALSE;
529
gedit_debug (DEBUG_SEARCH, "Text: %s", text);
531
/* To avoid recursive behavior */
535
escaped_text = escape_search_text (text);
537
gedit_debug (DEBUG_SEARCH, "Escaped Text: %s", escaped_text);
539
new_len = strlen (escaped_text);
541
if (new_len == length)
543
g_free (escaped_text);
549
g_signal_stop_emission_by_name (editable, "insert_text");
551
gtk_editable_insert_text (editable, escaped_text, new_len, position);
555
g_free (escaped_text);
559
gedit_dialog_find (void)
561
GeditDialogFind *dialog;
562
GeditMDIChild *active_child;
564
gchar* last_searched_text;
565
gint selection_start, selection_end;
566
gboolean selection_exists;
567
gboolean was_wrap_around;
568
gboolean was_entire_word;
569
gboolean was_case_sensitive;
570
gboolean was_search_backwards;
573
gedit_debug (DEBUG_SEARCH, "");
575
dialog = dialog_find_get_dialog ();
579
if (GTK_WIDGET_VISIBLE (dialog->dialog))
582
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
583
GEDIT_RESPONSE_FIND, FALSE);
585
active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
586
g_return_if_fail (active_child != NULL);
588
doc = active_child->document;
589
g_return_if_fail (doc != NULL);
591
selection_exists = gedit_document_get_selection (doc, &selection_start, &selection_end);
592
if (selection_exists && (selection_end - selection_start < 80))
594
gchar *selection_text;
596
selection_text = gedit_document_get_chars (doc, selection_start, selection_end);
598
gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), selection_text);
600
g_free (selection_text);
604
last_searched_text = gedit_document_get_last_searched_text (doc);
605
if (last_searched_text != NULL)
607
gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), last_searched_text);
608
g_free (last_searched_text);
612
if (!was_search_backwards_id)
613
was_search_backwards_id = g_quark_from_static_string ("GeditWasSearchBackwards");
615
if (!was_wrap_around_id)
616
was_wrap_around_id = gedit_was_wrap_around_quark ();
618
if (!was_entire_word_id)
619
was_entire_word_id = g_quark_from_static_string ("GeditWasEntireWord");
621
if (!was_case_sensitive_id)
622
was_case_sensitive_id = g_quark_from_static_string ("GeditWasCaseSensitive");
624
data = g_object_get_qdata (G_OBJECT (doc), was_wrap_around_id);
626
was_wrap_around = TRUE;
628
was_wrap_around = GPOINTER_TO_BOOLEAN (data);
630
data = g_object_get_qdata (G_OBJECT (doc), was_search_backwards_id);
632
was_search_backwards = FALSE;
634
was_search_backwards = GPOINTER_TO_BOOLEAN (data);
636
data = g_object_get_qdata (G_OBJECT (doc), was_entire_word_id);
638
was_entire_word = FALSE;
640
was_entire_word = GPOINTER_TO_BOOLEAN (data);
642
data = g_object_get_qdata (G_OBJECT (doc), was_case_sensitive_id);
644
was_case_sensitive = FALSE;
646
was_case_sensitive = GPOINTER_TO_BOOLEAN (data);
648
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton),
650
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton),
652
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton),
654
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton),
655
was_search_backwards);
657
gtk_widget_grab_focus (dialog->search_entry);
659
gtk_widget_show (dialog->dialog);
663
gedit_dialog_replace (void)
665
GeditDialogReplace *dialog;
666
GeditMDIChild *active_child;
668
gchar* last_searched_text;
669
gchar* last_replace_text;
670
gint selection_start, selection_end;
671
gboolean selection_exists;
672
gboolean was_wrap_around;
673
gboolean was_entire_word;
674
gboolean was_case_sensitive;
675
gboolean was_search_backwards;
678
gedit_debug (DEBUG_SEARCH, "");
680
dialog = dialog_replace_get_dialog ();
684
if (GTK_WIDGET_VISIBLE (dialog->dialog))
687
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
688
GEDIT_RESPONSE_FIND, FALSE);
689
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
690
GEDIT_RESPONSE_REPLACE, FALSE);
691
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
692
GEDIT_RESPONSE_REPLACE_ALL, FALSE);
694
active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
695
g_return_if_fail (active_child != NULL);
697
doc = active_child->document;
698
g_return_if_fail (doc != NULL);
700
selection_exists = gedit_document_get_selection (doc, &selection_start, &selection_end);
701
if (selection_exists && (selection_end - selection_start < 80))
703
gchar *selection_text;
705
selection_text = gedit_document_get_chars (doc, selection_start, selection_end);
707
gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), selection_text);
709
g_free (selection_text);
713
last_searched_text = gedit_document_get_last_searched_text (doc);
714
if (last_searched_text != NULL)
716
gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), last_searched_text);
717
g_free (last_searched_text);
721
last_replace_text = gedit_document_get_last_replace_text (doc);
722
if (last_replace_text != NULL)
724
gtk_entry_set_text (GTK_ENTRY (dialog->replace_entry), last_replace_text);
725
g_free (last_replace_text);
728
if (!was_search_backwards_id)
729
was_search_backwards_id = g_quark_from_static_string ("GeditWasSearchBackwards");
731
if (!was_wrap_around_id)
732
was_wrap_around_id = gedit_was_wrap_around_quark ();
734
if (!was_entire_word_id)
735
was_entire_word_id = g_quark_from_static_string ("GeditWasEntireWord");
737
if (!was_case_sensitive_id)
738
was_case_sensitive_id = g_quark_from_static_string ("GeditWasCaseSensitive");
740
data = g_object_get_qdata (G_OBJECT (doc), was_search_backwards_id);
742
was_search_backwards = FALSE;
744
was_search_backwards = GPOINTER_TO_BOOLEAN (data);
746
data = g_object_get_qdata (G_OBJECT (doc), was_wrap_around_id);
748
was_wrap_around = TRUE;
750
was_wrap_around = GPOINTER_TO_BOOLEAN (data);
752
data = g_object_get_qdata (G_OBJECT (doc), was_entire_word_id);
754
was_entire_word = FALSE;
756
was_entire_word = GPOINTER_TO_BOOLEAN (data);
758
data = g_object_get_qdata (G_OBJECT (doc), was_case_sensitive_id);
760
was_case_sensitive = FALSE;
762
was_case_sensitive = GPOINTER_TO_BOOLEAN (data);
764
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton),
766
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton),
768
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton),
770
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton),
771
was_search_backwards);
773
gtk_widget_grab_focus (dialog->search_entry);
775
gtk_widget_show (dialog->dialog);
779
find_dlg_find_button_pressed (GeditDialogFind *dialog)
781
GeditMDIChild *active_child;
782
GeditView* active_view;
784
const gchar* search_string = NULL;
787
gboolean case_sensitive;
788
gboolean entire_word;
789
gboolean wrap_around;
790
gboolean search_backwards;
793
gedit_debug (DEBUG_SEARCH, "");
795
if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
798
active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
799
g_return_if_fail (active_child != NULL);
801
active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
802
g_return_if_fail (active_view != NULL);
804
doc = active_child->document;
805
g_return_if_fail (doc != NULL);
807
search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
808
g_return_if_fail (search_string != NULL);
810
if (strlen (search_string) <= 0)
813
gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
815
/* retrieve search settings from the dialog */
816
case_sensitive = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton));
817
entire_word = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton));
818
wrap_around = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton));
819
search_backwards = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton));
821
/* setup quarks for next invocation */
822
g_object_set_qdata (G_OBJECT (doc), was_search_backwards_id, GBOOLEAN_TO_POINTER (search_backwards));
823
g_object_set_qdata (G_OBJECT (doc), was_wrap_around_id, GBOOLEAN_TO_POINTER (wrap_around));
824
g_object_set_qdata (G_OBJECT (doc), was_entire_word_id, GBOOLEAN_TO_POINTER (entire_word));
825
g_object_set_qdata (G_OBJECT (doc), was_case_sensitive_id, GBOOLEAN_TO_POINTER (case_sensitive));
827
/* setup search parameter bitfield */
828
GEDIT_SEARCH_SET_FROM_CURSOR (flags, TRUE);
829
GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, case_sensitive);
830
GEDIT_SEARCH_SET_BACKWARDS (flags, search_backwards);
831
GEDIT_SEARCH_SET_ENTIRE_WORD (flags, entire_word);
834
found = gedit_document_find (doc, search_string, flags);
836
/* if we're able to wrap, don't use the cursor position */
837
if (!found && wrap_around)
839
GEDIT_SEARCH_SET_FROM_CURSOR (flags, FALSE);
840
found = gedit_document_find (doc, search_string, flags);
844
gedit_view_scroll_to_cursor (active_view);
846
text_not_found_dialog (search_string, (GTK_WINDOW (dialog->dialog)));
848
update_menu_items_sensitivity ();
853
replace_dlg_find_button_pressed (GeditDialogReplace *dialog)
855
/* This is basically the same as find_dlg_find_button_pressed */
857
GeditMDIChild *active_child;
858
GeditView* active_view;
860
const gchar* search_string = NULL;
863
gboolean case_sensitive;
864
gboolean entire_word;
865
gboolean wrap_around;
866
gboolean search_backwards;
869
gedit_debug (DEBUG_SEARCH, "");
871
if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
874
active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
875
g_return_if_fail (active_child != NULL);
877
active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
878
g_return_if_fail (active_view != NULL);
880
doc = active_child->document;
881
g_return_if_fail (doc != NULL);
883
search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
884
g_return_if_fail (search_string != NULL);
886
if (strlen (search_string) <= 0)
889
gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
891
/* retrieve search settings from the dialog */
892
case_sensitive = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton));
893
entire_word = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton));
894
wrap_around = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton));
895
search_backwards = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton));
897
/* setup quarks for next invocation */
898
g_object_set_qdata (G_OBJECT (doc), was_search_backwards_id, GBOOLEAN_TO_POINTER (search_backwards));
899
g_object_set_qdata (G_OBJECT (doc), was_wrap_around_id, GBOOLEAN_TO_POINTER (wrap_around));
900
g_object_set_qdata (G_OBJECT (doc), was_entire_word_id, GBOOLEAN_TO_POINTER (entire_word));
901
g_object_set_qdata (G_OBJECT (doc), was_case_sensitive_id, GBOOLEAN_TO_POINTER (case_sensitive));
903
/* setup search parameter bitfield */
904
GEDIT_SEARCH_SET_FROM_CURSOR (flags, TRUE);
905
GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, case_sensitive);
906
GEDIT_SEARCH_SET_BACKWARDS (flags, search_backwards);
907
GEDIT_SEARCH_SET_ENTIRE_WORD (flags, entire_word);
910
found = gedit_document_find (doc, search_string, flags);
912
/* if we're able to wrap, don't use the cursor position */
913
if (!found && wrap_around)
915
GEDIT_SEARCH_SET_FROM_CURSOR (flags, FALSE);
916
found = gedit_document_find (doc, search_string, flags);
920
gedit_view_scroll_to_cursor (active_view);
922
text_not_found_dialog (search_string, (GTK_WINDOW (dialog->dialog)));
924
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
925
GEDIT_RESPONSE_REPLACE, found);
927
update_menu_items_sensitivity ();
931
replace_dlg_replace_button_pressed (GeditDialogReplace *dialog)
933
GeditMDIChild *active_child;
934
GeditView* active_view;
936
const gchar* search_string = NULL;
937
const gchar* replace_string = NULL;
938
gchar* selected_text = NULL;
939
gchar *converted_search_string = NULL;
943
gboolean case_sensitive;
944
gboolean entire_word;
945
gboolean wrap_around;
946
gboolean search_backwards;
949
gedit_debug (DEBUG_SEARCH, "");
951
g_return_if_fail (dialog != NULL);
953
if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
955
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
956
GEDIT_RESPONSE_REPLACE, FALSE);
960
active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
961
g_return_if_fail (active_child != NULL);
963
active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
964
g_return_if_fail (active_view != NULL);
966
doc = active_child->document;
967
g_return_if_fail (doc != NULL);
969
search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
970
replace_string = gtk_entry_get_text (GTK_ENTRY (dialog->replace_entry));
972
g_return_if_fail (search_string);
973
g_return_if_fail (replace_string);
975
if (strlen (search_string) <= 0)
978
gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
980
if (strlen (replace_string) > 0)
981
gnome_entry_prepend_history (GNOME_ENTRY (dialog->replace_entry_list),
985
if (gedit_document_get_selection (doc, &start, &end))
986
selected_text = gedit_document_get_chars (doc, start, end);
988
gedit_debug (DEBUG_SEARCH, "Sel text: %s", selected_text ? selected_text : "NULL");
989
gedit_debug (DEBUG_SEARCH, "Search string: %s", search_string ? search_string : "NULL");
991
/* retrieve search settings from the dialog */
992
case_sensitive = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton));
993
entire_word = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton));
994
wrap_around = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton));
995
search_backwards = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton));
997
converted_search_string = gedit_utils_convert_search_text (search_string);
999
if ((selected_text == NULL) ||
1000
(case_sensitive && (strcmp (selected_text, converted_search_string) != 0)) ||
1001
(!case_sensitive && !g_utf8_caselessnmatch (selected_text, search_string,
1002
strlen (selected_text),
1003
strlen (search_string)) != 0))
1005
gedit_debug (DEBUG_SEARCH, "selected_text (%s) != search_string (%s)",
1006
selected_text ? selected_text : "NULL",
1007
search_string ? search_string : "NULL");
1009
if (selected_text != NULL)
1010
g_free (selected_text);
1012
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
1013
GEDIT_RESPONSE_REPLACE, FALSE);
1015
replace_dlg_find_button_pressed (dialog);
1020
g_free (selected_text);
1021
g_free (converted_search_string);
1023
gedit_debug (DEBUG_SEARCH, "Replace string: %s", replace_string ? replace_string : "NULL");
1025
gedit_document_replace_selected_text (doc, replace_string);
1027
gedit_debug (DEBUG_SEARCH, "Replaced");
1029
/* setup search parameter bitfield */
1030
GEDIT_SEARCH_SET_FROM_CURSOR (flags, TRUE);
1031
GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, case_sensitive);
1032
GEDIT_SEARCH_SET_BACKWARDS (flags, search_backwards);
1033
GEDIT_SEARCH_SET_ENTIRE_WORD (flags, entire_word);
1036
found = gedit_document_find (doc, search_string, flags);
1038
gedit_debug (DEBUG_SEARCH, "Found 1: %s", found ? "TRUE" : "FALSE");
1040
/* if we're able to wrap, don't use the cursor position */
1041
if (!found && wrap_around)
1043
GEDIT_SEARCH_SET_FROM_CURSOR (flags, FALSE);
1044
found = gedit_document_find (doc, search_string, flags);
1046
gedit_debug (DEBUG_SEARCH, "Found 2: %s", found ? "TRUE" : "FALSE");
1050
gedit_view_scroll_to_cursor (active_view);
1052
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog),
1053
GEDIT_RESPONSE_REPLACE,
1056
update_menu_items_sensitivity ();
1058
gedit_debug (DEBUG_SEARCH, "END");
1062
replace_dlg_replace_all_button_pressed (GeditDialogReplace *dialog)
1064
GeditMDIChild *active_child;
1065
GeditView* active_view;
1067
const gchar* search_string = NULL;
1068
const gchar* replace_string = NULL;
1069
gint replaced_items;
1070
GtkWidget *message_dlg;
1072
gboolean case_sensitive;
1073
gboolean entire_word;
1076
gedit_debug (DEBUG_SEARCH, "");
1078
if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
1081
active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
1082
g_return_if_fail (active_child != NULL);
1084
active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
1085
g_return_if_fail (active_view != NULL);
1087
doc = active_child->document;
1088
g_return_if_fail (doc != NULL);
1090
search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
1091
replace_string = gtk_entry_get_text (GTK_ENTRY (dialog->replace_entry));
1093
g_return_if_fail (search_string);
1094
g_return_if_fail (replace_string);
1096
if (strlen (search_string) <= 0)
1099
gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
1101
if (strlen (replace_string) > 0)
1102
gnome_entry_prepend_history (GNOME_ENTRY (dialog->replace_entry_list),
1106
/* retrieve search settings from the dialog */
1107
case_sensitive = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton));
1108
entire_word = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton));
1110
/* setup search parameter bitfield */
1111
GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, case_sensitive);
1112
GEDIT_SEARCH_SET_ENTIRE_WORD (flags, entire_word);
1114
replaced_items = gedit_document_replace_all (doc, search_string, replace_string, flags);
1116
update_menu_items_sensitivity ();
1118
if (replaced_items <= 0)
1120
message_dlg = gtk_message_dialog_new (
1121
GTK_WINDOW (dialog->dialog),
1122
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1125
_("The text \"%s\" was not found."), search_string);
1129
message_dlg = gtk_message_dialog_new (
1130
GTK_WINDOW (dialog->dialog),
1131
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
1134
ngettext("Found and replaced %d occurrence.",
1135
"Found and replaced %d occurrences.",
1136
replaced_items), replaced_items);
1139
gtk_dialog_set_default_response (GTK_DIALOG (message_dlg), GTK_RESPONSE_OK);
1141
gtk_window_set_resizable (GTK_WINDOW (message_dlg), FALSE);
1143
gtk_dialog_run (GTK_DIALOG (message_dlg));
1144
gtk_widget_destroy (message_dlg);