1 /**
2  * @file   gui-search.c
3  * @brief
4  *
5  * Copyright (C) 2009 Gummi Developers
6  * All Rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "gui-search.h"
31 
32 #include <glib.h>
33 #include <gtk/gtk.h>
34 
35 #include "editor.h"
36 #include "environment.h"
37 #include "utils.h"
38 #include "gui/gui-main.h"
39 
40 extern Gummi* gummi;
41 extern GummiGui* gui;
42 
searchgui_init(GtkBuilder * builder)43 GuSearchGui* searchgui_init (GtkBuilder* builder) {
44     g_return_val_if_fail (GTK_IS_BUILDER (builder), NULL);
45 
46     GuSearchGui* s = g_new0 (GuSearchGui, 1);
47     s->searchwindow =
48         GTK_WIDGET (gtk_builder_get_object (builder, "searchwindow"));
49     s->searchentry =
50         GTK_ENTRY (gtk_builder_get_object (builder, "searchentry"));
51     s->replaceentry =
52         GTK_ENTRY (gtk_builder_get_object (builder, "replaceentry"));
53     s->matchcase = FALSE;
54     s->backwards = FALSE;
55     s->wholeword = FALSE;
56     s->prev_search = NULL;
57     s->prev_replace = NULL;
58     g_signal_connect (s->searchentry, "changed",
59             G_CALLBACK (on_searchgui_text_changed), NULL);
60     return s;
61 }
62 
searchgui_main(GuSearchGui * gc)63 void searchgui_main (GuSearchGui* gc) {
64     gtk_entry_set_text (gc->searchentry, gc->prev_search? gc->prev_search:"");
65     gtk_entry_set_text (gc->replaceentry, gc->prev_replace? gc->prev_replace:"");
66     gtk_widget_grab_focus (GTK_WIDGET (gc->searchentry));
67     gtk_widget_show_all (GTK_WIDGET (gc->searchwindow));
68     slog_set_gui_parent (GTK_WINDOW (gc->searchwindow));
69 }
70 
71 G_MODULE_EXPORT
on_toggle_matchcase_toggled(GtkWidget * widget,void * user)72 void on_toggle_matchcase_toggled (GtkWidget *widget, void* user) {
73     gui->searchgui->matchcase =
74         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
75     g_active_editor->replace_activated = FALSE;
76 }
77 
78 G_MODULE_EXPORT
on_toggle_wholeword_toggled(GtkWidget * widget,void * user)79 void on_toggle_wholeword_toggled (GtkWidget *widget, void* user) {
80     gui->searchgui->wholeword =
81         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
82     g_active_editor->replace_activated = FALSE;
83 }
84 
85 G_MODULE_EXPORT
on_toggle_backwards_toggled(GtkWidget * widget,void * user)86 void on_toggle_backwards_toggled (GtkWidget *widget, void* user) {
87     gui->searchgui->backwards =
88         gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
89     g_active_editor->replace_activated = FALSE;
90 }
91 
on_searchgui_text_changed(GtkEditable * editable,void * user)92 void on_searchgui_text_changed (GtkEditable *editable, void* user) {
93     g_active_editor->replace_activated = FALSE;
94 }
95 
96 G_MODULE_EXPORT
on_button_searchwindow_close_clicked(GtkWidget * widget,void * user)97 gboolean on_button_searchwindow_close_clicked (GtkWidget* widget, void* user) {
98     g_free (gui->searchgui->prev_search);
99     g_free (gui->searchgui->prev_replace);
100     gui->searchgui->prev_search = g_strdup (gtk_entry_get_text (
101                 gui->searchgui->searchentry));
102     gui->searchgui->prev_replace = g_strdup (gtk_entry_get_text (
103                 gui->searchgui->replaceentry));
104     gtk_widget_hide (GTK_WIDGET (gui->searchgui->searchwindow));
105     slog_set_gui_parent (GTK_WINDOW (gui->mainwindow));
106     return TRUE;
107 }
108 
109 G_MODULE_EXPORT
on_button_searchwindow_find_clicked(GtkWidget * widget,void * user)110 void on_button_searchwindow_find_clicked (GtkWidget* widget, void* user) {
111     editor_start_search (g_active_editor,
112             gtk_entry_get_text (gui->searchgui->searchentry),
113             gui->searchgui->backwards,
114             gui->searchgui->wholeword,
115             gui->searchgui->matchcase
116             );
117 }
118 
119 G_MODULE_EXPORT
on_button_searchwindow_replace_next_clicked(GtkWidget * widget,void * user)120 void on_button_searchwindow_replace_next_clicked (GtkWidget* widget, void* user) {
121     editor_start_replace_next (g_active_editor,
122             gtk_entry_get_text (gui->searchgui->searchentry),
123             gtk_entry_get_text (gui->searchgui->replaceentry),
124             gui->searchgui->backwards,
125             gui->searchgui->wholeword,
126             gui->searchgui->matchcase
127             );
128 }
129 
130 G_MODULE_EXPORT
on_button_searchwindow_replace_all_clicked(GtkWidget * widget,void * user)131 void on_button_searchwindow_replace_all_clicked (GtkWidget* widget, void* user) {
132     editor_start_replace_all (g_active_editor,
133             gtk_entry_get_text (gui->searchgui->searchentry),
134             gtk_entry_get_text (gui->searchgui->replaceentry),
135             gui->searchgui->backwards,
136             gui->searchgui->wholeword,
137             gui->searchgui->matchcase
138             );
139 }
140