~ubuntu-branches/ubuntu/hoary/gedit/hoary

« back to all changes in this revision

Viewing changes to gedit/dialogs/gedit-dialog-replace.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2004-10-10 21:24:57 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041010212457-f7q85htgynmslwf4
Tags: 2.8.1-0ubuntu1
* New upstream release:
  - do not try to open non regular files.
  - allow opening file by drag and drop on read only documents.
  - open dropped files in the right target gedit window.
  - do not change active document when closing an unmodified tab.
  - fix crash when trying to open an invalid sftp uri.
* debian/patches/01_relibtoolise.patch:
  - removed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
/*
 
3
 * gedit-dialog-replace.c
 
4
 * This file is part of gedit
 
5
 *
 
6
 * Copyright (C) 2001-2002 Paolo Maggi 
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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. 
 
22
 */
 
23
 
 
24
/*
 
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. 
 
28
 */
 
29
 
 
30
#ifdef HAVE_CONFIG_H
 
31
#include <config.h>
 
32
#endif
 
33
 
 
34
#include <string.h>
 
35
 
 
36
#include <glade/glade-xml.h>
 
37
#include <libgnome/libgnome.h>
 
38
#include <libgnomeui/gnome-entry.h>
 
39
 
 
40
#include "gedit2.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"
 
50
 
 
51
#define GEDIT_RESPONSE_FIND             101
 
52
#define GEDIT_RESPONSE_REPLACE          102
 
53
#define GEDIT_RESPONSE_REPLACE_ALL      103
 
54
 
 
55
typedef struct _GeditDialogReplace GeditDialogReplace;
 
56
typedef struct _GeditDialogFind GeditDialogFind;
 
57
 
 
58
struct _GeditDialogReplace {
 
59
        GtkWidget *dialog;
 
60
 
 
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;
 
69
};
 
70
 
 
71
struct _GeditDialogFind {
 
72
        GtkWidget *dialog;
 
73
 
 
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;
 
81
};
 
82
 
 
83
static void dialog_destroyed (GtkObject *obj,  void **dialog_pointer);
 
84
 
 
85
static void dialog_find_response_handler (GtkDialog *dlg, gint res_id,  GeditDialogFind *find_dialog);
 
86
 
 
87
static void find_dlg_find_button_pressed (GeditDialogFind * dialog);
 
88
 
 
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);
 
92
 
 
93
static void insert_text_handler (GtkEditable *editable, const gchar *text, gint length, gint *position);
 
94
 
 
95
static GeditDialogReplace *dialog_replace_get_dialog    (void);
 
96
static GeditDialogFind    *dialog_find_get_dialog       (void);
 
97
 
 
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;
 
102
 
 
103
GQuark 
 
104
gedit_was_wrap_around_quark (void)
 
105
{
 
106
        if (!was_wrap_around_id)
 
107
                was_wrap_around_id = g_quark_from_static_string ("GeditWasWrapAround");
 
108
 
 
109
        return was_wrap_around_id;
 
110
}
 
111
 
 
112
static void
 
113
dialog_destroyed (GtkObject *obj,  void **dialog_pointer)
 
114
{
 
115
        gedit_debug (DEBUG_SEARCH, "");
 
116
 
 
117
        if (dialog_pointer != NULL)
 
118
        {
 
119
                g_free (*dialog_pointer);
 
120
                *dialog_pointer = NULL;
 
121
        }       
 
122
}
 
123
 
 
124
static void 
 
125
text_not_found_dialog (const gchar *text, GtkWindow *parent)
 
126
{
 
127
        GtkWidget *message_dlg;
 
128
 
 
129
        g_return_if_fail (text != NULL);
 
130
        
 
131
        message_dlg = gtk_message_dialog_new (
 
132
                        parent,
 
133
                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 
134
                        GTK_MESSAGE_INFO,
 
135
                        GTK_BUTTONS_OK,
 
136
                        _("The text \"%s\" was not found."), text);
 
137
                
 
138
        gtk_dialog_set_default_response (GTK_DIALOG (message_dlg), GTK_RESPONSE_OK);
 
139
 
 
140
        gtk_window_set_resizable (GTK_WINDOW (message_dlg), FALSE);
 
141
        
 
142
        gtk_dialog_run (GTK_DIALOG (message_dlg));
 
143
        gtk_widget_destroy (message_dlg);
 
144
}
 
145
 
 
146
static void
 
147
update_menu_items_sensitivity (void)
 
148
{
 
149
        BonoboWindow* active_window = NULL;
 
150
        GeditDocument* doc = NULL;
 
151
        BonoboUIComponent *ui_component;
 
152
        gchar *lst;
 
153
        
 
154
        gedit_debug (DEBUG_SEARCH, "");
 
155
        
 
156
        active_window = gedit_get_active_window ();
 
157
        g_return_if_fail (active_window != NULL);
 
158
        
 
159
        ui_component = bonobo_mdi_get_ui_component_from_window (active_window);
 
160
        g_return_if_fail (ui_component != NULL);
 
161
 
 
162
        doc = gedit_get_active_document ();
 
163
        g_return_if_fail (doc != NULL);
 
164
 
 
165
        lst = gedit_document_get_last_searched_text (doc);
 
166
 
 
167
        gedit_menus_set_verb_sensitive (ui_component, "/commands/SearchFindAgain", lst != NULL);        
 
168
        g_free (lst);
 
169
}
 
170
 
 
171
static void
 
172
dialog_replace_response_handler (GtkDialog *dlg, gint res_id,  GeditDialogReplace *replace_dialog)
 
173
{
 
174
        gedit_debug (DEBUG_SEARCH, "");
 
175
 
 
176
        switch (res_id) {
 
177
                case GEDIT_RESPONSE_FIND:
 
178
                        replace_dlg_find_button_pressed (replace_dialog);
 
179
                        break;
 
180
                        
 
181
                case GEDIT_RESPONSE_REPLACE:
 
182
                        replace_dlg_replace_button_pressed (replace_dialog);
 
183
                        break;
 
184
                        
 
185
                case GEDIT_RESPONSE_REPLACE_ALL:
 
186
                        replace_dlg_replace_all_button_pressed (replace_dialog);
 
187
                        break;
 
188
                default:
 
189
                        gtk_widget_destroy (replace_dialog->dialog);
 
190
        }
 
191
}
 
192
 
 
193
static void
 
194
dialog_find_response_handler (GtkDialog *dlg, gint res_id,  GeditDialogFind *find_dialog)
 
195
{
 
196
        gedit_debug (DEBUG_SEARCH, "");
 
197
 
 
198
        switch (res_id) {
 
199
                case GEDIT_RESPONSE_FIND:
 
200
                        find_dlg_find_button_pressed (find_dialog);
 
201
                        break;
 
202
 
 
203
                default:
 
204
                        gtk_widget_destroy (find_dialog->dialog);
 
205
        }
 
206
}
 
207
 
 
208
static void 
 
209
replace_search_entry_changed (GtkEditable *editable, GeditDialogReplace *dialog)
 
210
{
 
211
        const gchar *search_string;
 
212
        
 
213
        search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));          
 
214
        g_return_if_fail (search_string != NULL);
 
215
 
 
216
        if (strlen (search_string) <= 0)
 
217
        {
 
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);
 
224
        }
 
225
        else
 
226
        {
 
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);
 
231
        }
 
232
}
 
233
 
 
234
static GeditDialogReplace *
 
235
dialog_replace_get_dialog (void)
 
236
{
 
237
        static GeditDialogReplace *dialog = NULL;
 
238
        GladeXML *gui;
 
239
        GtkWindow *window;
 
240
        GtkWidget *content;
 
241
        GtkWidget *replace_with_label;
 
242
        
 
243
        gedit_debug (DEBUG_SEARCH, "");
 
244
        
 
245
        window = GTK_WINDOW (gedit_get_active_window ());
 
246
 
 
247
        if (dialog != NULL)
 
248
        {
 
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);
 
253
 
 
254
                return dialog;
 
255
        }
 
256
 
 
257
        gui = glade_xml_new ( GEDIT_GLADEDIR "replace.glade2",
 
258
                             "replace_dialog_content", NULL);
 
259
        if (!gui)
 
260
        {
 
261
                gedit_warning (window,
 
262
                               MISSING_FILE,
 
263
                               GEDIT_GLADEDIR "replace.glade2");
 
264
                return NULL;
 
265
        }
 
266
 
 
267
        dialog = g_new0 (GeditDialogReplace, 1);
 
268
 
 
269
        dialog->dialog = gtk_dialog_new_with_buttons (_("Replace"),
 
270
                                                      window,                                                 
 
271
                                                      GTK_DIALOG_DESTROY_WITH_PARENT,
 
272
                                                      GTK_STOCK_CLOSE,
 
273
                                                      GTK_RESPONSE_CLOSE,
 
274
                                                      NULL);
 
275
 
 
276
        g_return_val_if_fail (dialog->dialog != NULL, NULL);
 
277
 
 
278
        gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE);
 
279
        gtk_dialog_set_has_separator (GTK_DIALOG (dialog->dialog), FALSE);
 
280
 
 
281
        gtk_dialog_add_button (GTK_DIALOG (dialog->dialog), 
 
282
                               _("Replace _All"), GEDIT_RESPONSE_REPLACE_ALL);
 
283
 
 
284
        gedit_dialog_add_button (GTK_DIALOG (dialog->dialog),
 
285
                                 _("_Replace"), GTK_STOCK_FIND_AND_REPLACE,
 
286
                                 GEDIT_RESPONSE_REPLACE);
 
287
 
 
288
        gtk_dialog_add_button (GTK_DIALOG (dialog->dialog), 
 
289
                               GTK_STOCK_FIND, GEDIT_RESPONSE_FIND);
 
290
 
 
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");
 
301
 
 
302
        if (!content                            ||
 
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)
 
312
        {
 
313
                gedit_warning (window,
 
314
                               MISSING_WIDGETS,
 
315
                               GEDIT_GLADEDIR "replace.glade2");
 
316
                return NULL;
 
317
        }
 
318
 
 
319
        gtk_widget_show (replace_with_label);
 
320
        gtk_widget_show (dialog->replace_entry);
 
321
        gtk_widget_show (dialog->replace_entry_list);
 
322
        
 
323
        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog->dialog)->vbox),
 
324
                            content, FALSE, FALSE, 0);
 
325
 
 
326
        gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog),
 
327
                                         GEDIT_RESPONSE_FIND);
 
328
 
 
329
        g_signal_connect (G_OBJECT (dialog->search_entry_list), "changed",
 
330
                          G_CALLBACK (replace_search_entry_changed), dialog);
 
331
        
 
332
        g_signal_connect (G_OBJECT (dialog->search_entry), "insert_text",
 
333
                          G_CALLBACK (insert_text_handler), NULL);
 
334
 
 
335
        g_signal_connect (G_OBJECT (dialog->replace_entry), "insert_text",
 
336
                          G_CALLBACK (insert_text_handler), NULL);
 
337
 
 
338
        g_signal_connect (G_OBJECT (dialog->dialog), "destroy",
 
339
                          G_CALLBACK (dialog_destroyed), &dialog);
 
340
 
 
341
        g_signal_connect (G_OBJECT (dialog->dialog), "response",
 
342
                          G_CALLBACK (dialog_replace_response_handler), dialog);
 
343
 
 
344
        g_object_unref (G_OBJECT (gui));
 
345
 
 
346
        return dialog;
 
347
}
 
348
 
 
349
static void 
 
350
find_search_entry_changed (GtkEditable *editable, GeditDialogReplace *dialog)
 
351
{
 
352
        const gchar *search_string;
 
353
        
 
354
        search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));          
 
355
        g_return_if_fail (search_string != NULL);
 
356
 
 
357
        if (strlen (search_string) <= 0)
 
358
                gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
359
                        GEDIT_RESPONSE_FIND, FALSE);
 
360
        else
 
361
                gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
362
                        GEDIT_RESPONSE_FIND, TRUE);
 
363
}
 
364
 
 
365
static GeditDialogFind *
 
366
dialog_find_get_dialog (void)
 
367
{
 
368
        static GeditDialogFind *dialog = NULL;
 
369
        GladeXML *gui;
 
370
        GtkWindow *window;
 
371
        GtkWidget *content;
 
372
        GtkWidget *replace_with_label;
 
373
        GtkWidget *replace_entry;
 
374
        GtkWidget *table;
 
375
        
 
376
        gedit_debug (DEBUG_SEARCH, "");
 
377
 
 
378
        window = GTK_WINDOW (gedit_get_active_window ());
 
379
 
 
380
        if (dialog != NULL)
 
381
        {
 
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);
 
386
 
 
387
                return dialog;
 
388
        }
 
389
 
 
390
        gui = glade_xml_new (GEDIT_GLADEDIR "replace.glade2",
 
391
                             "replace_dialog_content", NULL);
 
392
        if (!gui)
 
393
        {
 
394
                gedit_warning (window,
 
395
                               MISSING_FILE,
 
396
                               GEDIT_GLADEDIR "replace.glade2");
 
397
                return NULL;
 
398
        }
 
399
 
 
400
        dialog = g_new0 (GeditDialogFind, 1);
 
401
 
 
402
        dialog->dialog = gtk_dialog_new_with_buttons (_("Find"),
 
403
                                                      window,
 
404
                                                      GTK_DIALOG_DESTROY_WITH_PARENT,
 
405
                                                      GTK_STOCK_CLOSE,
 
406
                                                      GTK_RESPONSE_CLOSE,
 
407
                                                      GTK_STOCK_FIND,
 
408
                                                      GEDIT_RESPONSE_FIND,
 
409
                                                      NULL);
 
410
 
 
411
        g_return_val_if_fail (dialog->dialog != NULL, NULL);
 
412
 
 
413
        gtk_window_set_resizable (GTK_WINDOW (dialog->dialog), FALSE);
 
414
        gtk_dialog_set_has_separator (GTK_DIALOG (dialog->dialog), FALSE);
 
415
 
 
416
        content = glade_xml_get_widget (gui, "replace_dialog_content");
 
417
 
 
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");
 
421
 
 
422
        replace_with_label         = glade_xml_get_widget (gui, "replace_with_label");
 
423
        
 
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");
 
428
 
 
429
        table                      = glade_xml_get_widget (gui, "table");
 
430
 
 
431
        if (!content                            ||
 
432
            !table                              ||
 
433
            !dialog->search_entry               ||
 
434
            !dialog->search_entry_list          ||
 
435
            !replace_entry                      ||
 
436
            !replace_with_label                 ||
 
437
            !dialog->match_case_checkbutton     ||
 
438
            !dialog->entire_word_checkbutton    ||
 
439
            !dialog->wrap_around_checkbutton    ||
 
440
            !dialog->search_backwards_checkbutton)
 
441
        {
 
442
                gedit_warning (window,
 
443
                               MISSING_WIDGETS,
 
444
                               GEDIT_GLADEDIR "replace.glade2");
 
445
                return NULL;
 
446
        }
 
447
 
 
448
        gtk_widget_hide (replace_with_label);
 
449
        gtk_widget_hide (replace_entry);
 
450
 
 
451
        gtk_table_set_row_spacings (GTK_TABLE (table), 0);
 
452
 
 
453
        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog->dialog)->vbox),
 
454
                            content, FALSE, FALSE, 0);
 
455
 
 
456
        gtk_dialog_set_default_response (GTK_DIALOG (dialog->dialog),
 
457
                                         GEDIT_RESPONSE_FIND);
 
458
 
 
459
        g_signal_connect (G_OBJECT (dialog->search_entry_list), "changed",
 
460
                          G_CALLBACK (find_search_entry_changed), dialog);
 
461
 
 
462
        g_signal_connect (G_OBJECT (dialog->search_entry), "insert_text",
 
463
                          G_CALLBACK (insert_text_handler), NULL);
 
464
 
 
465
        g_signal_connect(G_OBJECT (dialog->dialog), "destroy",
 
466
                         G_CALLBACK (dialog_destroyed), &dialog);
 
467
 
 
468
        g_signal_connect(G_OBJECT (dialog->dialog), "response",
 
469
                         G_CALLBACK (dialog_find_response_handler), dialog);
 
470
 
 
471
        g_object_unref (G_OBJECT (gui));
 
472
 
 
473
        return dialog;
 
474
}
 
475
 
 
476
static gchar* 
 
477
escape_search_text (const gchar* text)
 
478
{
 
479
        GString *str;
 
480
        gint length;
 
481
        const gchar *p;
 
482
        const gchar *end;
 
483
 
 
484
        g_return_val_if_fail (text != NULL, NULL);
 
485
 
 
486
        gedit_debug (DEBUG_SEARCH, "Text: %s", text);
 
487
 
 
488
        length = strlen (text);
 
489
 
 
490
        str = g_string_new ("");
 
491
 
 
492
        p = text;
 
493
        end = text + length;
 
494
 
 
495
        while (p != end)
 
496
        {
 
497
                const gchar *next;
 
498
                next = g_utf8_next_char (p);
 
499
 
 
500
                switch (*p)
 
501
                {
 
502
                        case '\n':
 
503
                                g_string_append (str, "\\n");
 
504
                                break;
 
505
                        case '\r':
 
506
                                g_string_append (str, "\\r");
 
507
                                break;
 
508
                        case '\t':
 
509
                                g_string_append (str, "\\t");
 
510
                                break;
 
511
                        default:
 
512
                                g_string_append_len (str, p, next - p);
 
513
                                break;
 
514
                }
 
515
 
 
516
                p = next;
 
517
        }
 
518
 
 
519
        return g_string_free (str, FALSE);
 
520
}
 
521
 
 
522
static void
 
523
insert_text_handler (GtkEditable *editable, const gchar *text, gint length, gint *position)
 
524
{
 
525
        static gboolean insert_text = FALSE;
 
526
        gchar *escaped_text;
 
527
        gint new_len;
 
528
 
 
529
        gedit_debug (DEBUG_SEARCH, "Text: %s", text);
 
530
 
 
531
        /* To avoid recursive behavior */
 
532
        if (insert_text)
 
533
                return;
 
534
 
 
535
        escaped_text = escape_search_text (text);
 
536
 
 
537
        gedit_debug (DEBUG_SEARCH, "Escaped Text: %s", escaped_text);
 
538
 
 
539
        new_len = strlen (escaped_text);
 
540
 
 
541
        if (new_len == length)
 
542
        {
 
543
                g_free (escaped_text);
 
544
                return;
 
545
        }
 
546
 
 
547
        insert_text = TRUE;
 
548
 
 
549
        g_signal_stop_emission_by_name (editable, "insert_text");
 
550
        
 
551
        gtk_editable_insert_text (editable, escaped_text, new_len, position);
 
552
 
 
553
        insert_text = FALSE;
 
554
 
 
555
        g_free (escaped_text);
 
556
}
 
557
 
 
558
void
 
559
gedit_dialog_find (void)
 
560
{
 
561
        GeditDialogFind *dialog;
 
562
        GeditMDIChild *active_child;
 
563
        GeditDocument *doc;
 
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;
 
571
        gpointer data;
 
572
        
 
573
        gedit_debug (DEBUG_SEARCH, "");
 
574
 
 
575
        dialog = dialog_find_get_dialog ();
 
576
        if (!dialog)
 
577
                return;
 
578
 
 
579
        if (GTK_WIDGET_VISIBLE (dialog->dialog))
 
580
                return;
 
581
 
 
582
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
583
                                           GEDIT_RESPONSE_FIND, FALSE);
 
584
 
 
585
        active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
 
586
        g_return_if_fail (active_child != NULL);
 
587
 
 
588
        doc = active_child->document;
 
589
        g_return_if_fail (doc != NULL);
 
590
 
 
591
        selection_exists = gedit_document_get_selection (doc, &selection_start, &selection_end);
 
592
        if (selection_exists && (selection_end - selection_start < 80)) 
 
593
        {
 
594
                gchar *selection_text;
 
595
                
 
596
                selection_text = gedit_document_get_chars (doc, selection_start, selection_end);
 
597
                
 
598
                gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), selection_text);
 
599
                
 
600
                g_free (selection_text);
 
601
        } 
 
602
        else 
 
603
        {
 
604
                last_searched_text = gedit_document_get_last_searched_text (doc);
 
605
                if (last_searched_text != NULL)
 
606
                {
 
607
                        gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), last_searched_text);
 
608
                        g_free (last_searched_text);    
 
609
                }
 
610
        }
 
611
 
 
612
        if (!was_search_backwards_id)
 
613
                was_search_backwards_id = g_quark_from_static_string ("GeditWasSearchBackwards");
 
614
 
 
615
        if (!was_wrap_around_id)
 
616
                was_wrap_around_id = gedit_was_wrap_around_quark ();
 
617
 
 
618
        if (!was_entire_word_id)
 
619
                was_entire_word_id = g_quark_from_static_string ("GeditWasEntireWord");
 
620
 
 
621
        if (!was_case_sensitive_id)
 
622
                was_case_sensitive_id = g_quark_from_static_string ("GeditWasCaseSensitive");
 
623
 
 
624
        data = g_object_get_qdata (G_OBJECT (doc), was_wrap_around_id);
 
625
        if (data == NULL)
 
626
                was_wrap_around = TRUE;
 
627
        else
 
628
                was_wrap_around = GPOINTER_TO_BOOLEAN (data);
 
629
 
 
630
        data = g_object_get_qdata (G_OBJECT (doc), was_search_backwards_id);
 
631
        if (data == NULL)
 
632
                was_search_backwards = FALSE;
 
633
        else
 
634
                was_search_backwards = GPOINTER_TO_BOOLEAN (data);
 
635
 
 
636
        data = g_object_get_qdata (G_OBJECT (doc), was_entire_word_id);
 
637
        if (data == NULL)
 
638
                was_entire_word = FALSE;
 
639
        else
 
640
                was_entire_word = GPOINTER_TO_BOOLEAN (data);
 
641
 
 
642
        data = g_object_get_qdata (G_OBJECT (doc), was_case_sensitive_id);
 
643
        if (data == NULL)
 
644
                was_case_sensitive = FALSE;
 
645
        else
 
646
                was_case_sensitive = GPOINTER_TO_BOOLEAN (data);
 
647
        
 
648
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton), 
 
649
                                      was_case_sensitive);
 
650
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton),
 
651
                                      was_entire_word);
 
652
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton),
 
653
                                      was_wrap_around);
 
654
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton),
 
655
                                      was_search_backwards);
 
656
 
 
657
        gtk_widget_grab_focus (dialog->search_entry);
 
658
        
 
659
        gtk_widget_show (dialog->dialog);
 
660
}
 
661
 
 
662
void
 
663
gedit_dialog_replace (void)
 
664
{
 
665
        GeditDialogReplace *dialog;
 
666
        GeditMDIChild *active_child;
 
667
        GeditDocument *doc;
 
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;
 
676
        gpointer data;
 
677
 
 
678
        gedit_debug (DEBUG_SEARCH, "");
 
679
 
 
680
        dialog = dialog_replace_get_dialog ();
 
681
        if (!dialog)
 
682
                return;
 
683
 
 
684
        if (GTK_WIDGET_VISIBLE (dialog->dialog))
 
685
                return;
 
686
 
 
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);
 
693
 
 
694
        active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
 
695
        g_return_if_fail (active_child != NULL);
 
696
 
 
697
        doc = active_child->document;
 
698
        g_return_if_fail (doc != NULL);
 
699
 
 
700
        selection_exists = gedit_document_get_selection (doc, &selection_start, &selection_end);
 
701
        if (selection_exists && (selection_end - selection_start < 80)) 
 
702
        {
 
703
                gchar *selection_text;
 
704
                
 
705
                selection_text = gedit_document_get_chars (doc, selection_start, selection_end);
 
706
                
 
707
                gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), selection_text);
 
708
                
 
709
                g_free (selection_text);
 
710
        } 
 
711
        else 
 
712
        {
 
713
                last_searched_text = gedit_document_get_last_searched_text (doc);
 
714
                if (last_searched_text != NULL)
 
715
                {
 
716
                        gtk_entry_set_text (GTK_ENTRY (dialog->search_entry), last_searched_text);
 
717
                        g_free (last_searched_text);    
 
718
                }
 
719
        }
 
720
        
 
721
        last_replace_text = gedit_document_get_last_replace_text (doc);
 
722
        if (last_replace_text != NULL)
 
723
        {
 
724
                gtk_entry_set_text (GTK_ENTRY (dialog->replace_entry), last_replace_text);
 
725
                g_free (last_replace_text);     
 
726
        }
 
727
 
 
728
        if (!was_search_backwards_id)
 
729
                was_search_backwards_id = g_quark_from_static_string ("GeditWasSearchBackwards");
 
730
 
 
731
        if (!was_wrap_around_id)
 
732
                was_wrap_around_id = gedit_was_wrap_around_quark ();
 
733
 
 
734
        if (!was_entire_word_id)
 
735
                was_entire_word_id = g_quark_from_static_string ("GeditWasEntireWord");
 
736
 
 
737
        if (!was_case_sensitive_id)
 
738
                was_case_sensitive_id = g_quark_from_static_string ("GeditWasCaseSensitive");
 
739
 
 
740
        data = g_object_get_qdata (G_OBJECT (doc), was_search_backwards_id);
 
741
        if (data == NULL)
 
742
                was_search_backwards = FALSE;
 
743
        else
 
744
                was_search_backwards = GPOINTER_TO_BOOLEAN (data);
 
745
 
 
746
        data = g_object_get_qdata (G_OBJECT (doc), was_wrap_around_id);
 
747
        if (data == NULL)
 
748
                was_wrap_around = TRUE;
 
749
        else
 
750
                was_wrap_around = GPOINTER_TO_BOOLEAN (data);
 
751
 
 
752
        data = g_object_get_qdata (G_OBJECT (doc), was_entire_word_id);
 
753
        if (data == NULL)
 
754
                was_entire_word = FALSE;
 
755
        else
 
756
                was_entire_word = GPOINTER_TO_BOOLEAN (data);
 
757
 
 
758
        data = g_object_get_qdata (G_OBJECT (doc), was_case_sensitive_id);
 
759
        if (data == NULL)
 
760
                was_case_sensitive = FALSE;
 
761
        else
 
762
                was_case_sensitive = GPOINTER_TO_BOOLEAN (data);
 
763
        
 
764
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->match_case_checkbutton), 
 
765
                                      was_case_sensitive);
 
766
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->entire_word_checkbutton),
 
767
                                      was_entire_word);
 
768
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->wrap_around_checkbutton),
 
769
                                      was_wrap_around);
 
770
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->search_backwards_checkbutton),
 
771
                                      was_search_backwards);
 
772
 
 
773
        gtk_widget_grab_focus (dialog->search_entry);
 
774
 
 
775
        gtk_widget_show (dialog->dialog);
 
776
}
 
777
 
 
778
static void
 
779
find_dlg_find_button_pressed (GeditDialogFind *dialog)
 
780
{
 
781
        GeditMDIChild *active_child;
 
782
        GeditView* active_view;
 
783
        GeditDocument *doc;
 
784
        const gchar* search_string = NULL;
 
785
        gboolean found;
 
786
 
 
787
        gboolean case_sensitive;
 
788
        gboolean entire_word;
 
789
        gboolean wrap_around;
 
790
        gboolean search_backwards;
 
791
        gint flags = 0;
 
792
 
 
793
        gedit_debug (DEBUG_SEARCH, "");
 
794
 
 
795
        if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
 
796
                return;
 
797
        
 
798
        active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
 
799
        g_return_if_fail (active_child != NULL);
 
800
 
 
801
        active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
 
802
        g_return_if_fail (active_view != NULL);
 
803
        
 
804
        doc = active_child->document;
 
805
        g_return_if_fail (doc != NULL);
 
806
                        
 
807
        search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));          
 
808
        g_return_if_fail (search_string != NULL);
 
809
 
 
810
        if (strlen (search_string) <= 0)
 
811
                return;
 
812
 
 
813
        gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
 
814
                
 
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));
 
820
 
 
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));
 
826
 
 
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);
 
832
 
 
833
        /* run search */
 
834
        found = gedit_document_find (doc, search_string, flags);
 
835
 
 
836
        /* if we're able to wrap, don't use the cursor position */
 
837
        if (!found && wrap_around)
 
838
        {
 
839
                GEDIT_SEARCH_SET_FROM_CURSOR (flags, FALSE);
 
840
                found = gedit_document_find (doc, search_string, flags);
 
841
        }
 
842
 
 
843
        if (found)
 
844
                gedit_view_scroll_to_cursor (active_view);
 
845
        else
 
846
                text_not_found_dialog (search_string, (GTK_WINDOW (dialog->dialog)));
 
847
 
 
848
        update_menu_items_sensitivity ();
 
849
}
 
850
 
 
851
 
 
852
static void
 
853
replace_dlg_find_button_pressed (GeditDialogReplace *dialog)
 
854
{
 
855
        /* This is basically the same as find_dlg_find_button_pressed */
 
856
 
 
857
        GeditMDIChild *active_child;
 
858
        GeditView* active_view;
 
859
        GeditDocument *doc;
 
860
        const gchar* search_string = NULL;
 
861
        gboolean found;
 
862
 
 
863
        gboolean case_sensitive;
 
864
        gboolean entire_word;
 
865
        gboolean wrap_around;
 
866
        gboolean search_backwards;
 
867
        gint flags = 0;
 
868
 
 
869
        gedit_debug (DEBUG_SEARCH, "");
 
870
 
 
871
        if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
 
872
                return;
 
873
        
 
874
        active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
 
875
        g_return_if_fail (active_child != NULL);
 
876
 
 
877
        active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
 
878
        g_return_if_fail (active_view != NULL);
 
879
        
 
880
        doc = active_child->document;
 
881
        g_return_if_fail (doc != NULL);
 
882
                        
 
883
        search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));          
 
884
        g_return_if_fail (search_string != NULL);
 
885
 
 
886
        if (strlen (search_string) <= 0)
 
887
                return;
 
888
        
 
889
        gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
 
890
                
 
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));
 
896
 
 
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));
 
902
 
 
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);
 
908
 
 
909
        /* run search */
 
910
        found = gedit_document_find (doc, search_string, flags);
 
911
 
 
912
        /* if we're able to wrap, don't use the cursor position */
 
913
        if (!found && wrap_around)
 
914
        {
 
915
                GEDIT_SEARCH_SET_FROM_CURSOR (flags, FALSE);
 
916
                found = gedit_document_find (doc, search_string, flags);
 
917
        }
 
918
 
 
919
        if (found)
 
920
                gedit_view_scroll_to_cursor (active_view);
 
921
        else
 
922
                text_not_found_dialog (search_string, (GTK_WINDOW (dialog->dialog)));
 
923
 
 
924
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
925
                                                        GEDIT_RESPONSE_REPLACE, found);
 
926
 
 
927
        update_menu_items_sensitivity ();
 
928
}
 
929
 
 
930
static void
 
931
replace_dlg_replace_button_pressed (GeditDialogReplace *dialog)
 
932
{
 
933
        GeditMDIChild *active_child;
 
934
        GeditView* active_view;
 
935
        GeditDocument *doc;
 
936
        const gchar* search_string = NULL;
 
937
        const gchar* replace_string = NULL;
 
938
        gchar* selected_text = NULL;
 
939
        gchar *converted_search_string = NULL;
 
940
        gint start, end;
 
941
        gboolean found;
 
942
 
 
943
        gboolean case_sensitive;
 
944
        gboolean entire_word;
 
945
        gboolean wrap_around;
 
946
        gboolean search_backwards;
 
947
        gint flags = 0;
 
948
 
 
949
        gedit_debug (DEBUG_SEARCH, "");
 
950
 
 
951
        g_return_if_fail (dialog != NULL);
 
952
        
 
953
        if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
 
954
        {
 
955
                gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
956
                                                        GEDIT_RESPONSE_REPLACE, FALSE);
 
957
                return;
 
958
        }
 
959
 
 
960
        active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
 
961
        g_return_if_fail (active_child != NULL);
 
962
 
 
963
        active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
 
964
        g_return_if_fail (active_view != NULL);
 
965
        
 
966
        doc = active_child->document;
 
967
        g_return_if_fail (doc != NULL);
 
968
 
 
969
        search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));
 
970
        replace_string = gtk_entry_get_text (GTK_ENTRY (dialog->replace_entry));
 
971
 
 
972
        g_return_if_fail (search_string);
 
973
        g_return_if_fail (replace_string);
 
974
 
 
975
        if (strlen (search_string) <= 0)
 
976
                return;
 
977
        
 
978
        gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
 
979
 
 
980
        if (strlen (replace_string) > 0)
 
981
                gnome_entry_prepend_history (GNOME_ENTRY (dialog->replace_entry_list), 
 
982
                                             TRUE, 
 
983
                                             replace_string);
 
984
 
 
985
        if (gedit_document_get_selection (doc, &start, &end))
 
986
                selected_text = gedit_document_get_chars (doc, start, end);
 
987
        
 
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");
 
990
 
 
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));
 
996
 
 
997
        converted_search_string = gedit_utils_convert_search_text (search_string);
 
998
 
 
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))
 
1004
        {
 
1005
                gedit_debug (DEBUG_SEARCH, "selected_text (%s) != search_string (%s)", 
 
1006
                             selected_text ? selected_text : "NULL",
 
1007
                             search_string ? search_string : "NULL");
 
1008
 
 
1009
                if (selected_text != NULL)
 
1010
                        g_free (selected_text);
 
1011
 
 
1012
                gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
1013
                        GEDIT_RESPONSE_REPLACE, FALSE);
 
1014
                
 
1015
                replace_dlg_find_button_pressed (dialog);
 
1016
 
 
1017
                return;
 
1018
        }
 
1019
                                
 
1020
        g_free (selected_text);
 
1021
        g_free (converted_search_string);
 
1022
                
 
1023
        gedit_debug (DEBUG_SEARCH, "Replace string: %s", replace_string ? replace_string : "NULL");
 
1024
 
 
1025
        gedit_document_replace_selected_text (doc, replace_string);
 
1026
 
 
1027
        gedit_debug (DEBUG_SEARCH, "Replaced");
 
1028
 
 
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);
 
1034
 
 
1035
        /* run search */
 
1036
        found = gedit_document_find (doc, search_string, flags);
 
1037
 
 
1038
        gedit_debug (DEBUG_SEARCH, "Found 1: %s", found ? "TRUE" : "FALSE");
 
1039
 
 
1040
        /* if we're able to wrap, don't use the cursor position */
 
1041
        if (!found && wrap_around)
 
1042
        {
 
1043
                GEDIT_SEARCH_SET_FROM_CURSOR (flags, FALSE);
 
1044
                found = gedit_document_find (doc, search_string, flags);
 
1045
 
 
1046
                gedit_debug (DEBUG_SEARCH, "Found 2: %s", found ? "TRUE" : "FALSE");
 
1047
        }
 
1048
 
 
1049
        if (found)
 
1050
                gedit_view_scroll_to_cursor (active_view);
 
1051
        
 
1052
        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog->dialog), 
 
1053
                                           GEDIT_RESPONSE_REPLACE, 
 
1054
                                           found);      
 
1055
 
 
1056
        update_menu_items_sensitivity ();
 
1057
 
 
1058
        gedit_debug (DEBUG_SEARCH, "END");
 
1059
}
 
1060
 
 
1061
static void
 
1062
replace_dlg_replace_all_button_pressed (GeditDialogReplace *dialog)
 
1063
{
 
1064
        GeditMDIChild *active_child;
 
1065
        GeditView* active_view;
 
1066
        GeditDocument *doc;
 
1067
        const gchar* search_string = NULL;
 
1068
        const gchar* replace_string = NULL;
 
1069
        gint replaced_items;
 
1070
        GtkWidget *message_dlg;
 
1071
        
 
1072
        gboolean case_sensitive;
 
1073
        gboolean entire_word;
 
1074
        gint flags = 0;
 
1075
        
 
1076
        gedit_debug (DEBUG_SEARCH, "");
 
1077
 
 
1078
        if (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)) == NULL)
 
1079
                return;
 
1080
 
 
1081
        active_child = GEDIT_MDI_CHILD (bonobo_mdi_get_active_child (BONOBO_MDI (gedit_mdi)));
 
1082
        g_return_if_fail (active_child != NULL);
 
1083
 
 
1084
        active_view = GEDIT_VIEW (bonobo_mdi_get_active_view (BONOBO_MDI (gedit_mdi)));
 
1085
        g_return_if_fail (active_view != NULL);
 
1086
        
 
1087
        doc = active_child->document;
 
1088
        g_return_if_fail (doc != NULL);
 
1089
 
 
1090
        search_string = gtk_entry_get_text (GTK_ENTRY (dialog->search_entry));          
 
1091
        replace_string = gtk_entry_get_text (GTK_ENTRY (dialog->replace_entry));                
 
1092
 
 
1093
        g_return_if_fail (search_string);
 
1094
        g_return_if_fail (replace_string);
 
1095
 
 
1096
        if (strlen (search_string) <= 0)
 
1097
                return;
 
1098
        
 
1099
        gnome_entry_prepend_history (GNOME_ENTRY (dialog->search_entry_list), TRUE, search_string);
 
1100
 
 
1101
        if (strlen (replace_string) > 0)
 
1102
                gnome_entry_prepend_history (GNOME_ENTRY (dialog->replace_entry_list), 
 
1103
                                             TRUE, 
 
1104
                                             replace_string);
 
1105
 
 
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));
 
1109
 
 
1110
        /* setup search parameter bitfield */
 
1111
        GEDIT_SEARCH_SET_CASE_SENSITIVE (flags, case_sensitive);
 
1112
        GEDIT_SEARCH_SET_ENTIRE_WORD (flags, entire_word);
 
1113
 
 
1114
        replaced_items = gedit_document_replace_all (doc, search_string, replace_string, flags);
 
1115
 
 
1116
        update_menu_items_sensitivity ();
 
1117
 
 
1118
        if (replaced_items <= 0)
 
1119
        {
 
1120
                message_dlg = gtk_message_dialog_new (
 
1121
                        GTK_WINDOW (dialog->dialog),
 
1122
                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 
1123
                        GTK_MESSAGE_INFO,
 
1124
                        GTK_BUTTONS_OK,
 
1125
                        _("The text \"%s\" was not found."), search_string);
 
1126
        }
 
1127
        else
 
1128
        {       
 
1129
                message_dlg = gtk_message_dialog_new (
 
1130
                        GTK_WINDOW (dialog->dialog),
 
1131
                        GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
 
1132
                        GTK_MESSAGE_INFO,
 
1133
                        GTK_BUTTONS_OK,
 
1134
                        ngettext("Found and replaced %d occurrence.",
 
1135
                                 "Found and replaced %d occurrences.",
 
1136
                                 replaced_items), replaced_items);
 
1137
        }
 
1138
        
 
1139
        gtk_dialog_set_default_response (GTK_DIALOG (message_dlg), GTK_RESPONSE_OK);
 
1140
 
 
1141
        gtk_window_set_resizable (GTK_WINDOW (message_dlg), FALSE);
 
1142
 
 
1143
        gtk_dialog_run (GTK_DIALOG (message_dlg));
 
1144
        gtk_widget_destroy (message_dlg);
 
1145
}