source: trunk/third/gtkhtml/src/gtkhtml-search.c @ 16767

Revision 16767, 5.6 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r16766, which included commits to RCS files with non-trunk default branches.
Line 
1/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2/*  This file is part of the GtkHTML library.
3
4    Copyright 1999, 2000 Helix Code, Inc.
5    Authors:             Radek Doulik (rodo@helixcode.com)
6   
7    This library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public
9    License as published by the Free Software Foundation; either
10    version 2 of the License, or (at your option) any later version.
11
12    This library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public License
18    along with this library; see the file COPYING.LIB.  If not, write to
19    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21*/
22
23#include <config.h>
24#include <gdk/gdkkeysyms.h>
25#include <gtk/gtkentry.h>
26#include <gtk/gtkmain.h>
27#include <gtk/gtksignal.h>
28#include "gtkhtml.h"
29#include "gtkhtml-private.h"
30#include "gtkhtml-search.h"
31#include "htmlengine-search.h"
32#include "htmlsearch.h"
33#include "htmlselection.h"
34
35struct _GtkHTMLISearch {
36        GtkHTML  *html;
37        gboolean forward;
38        gboolean changed;
39        guint focus_out;
40        gchar *last_text;
41};
42typedef struct _GtkHTMLISearch GtkHTMLISearch;
43
44static void
45changed (GtkEntry *entry, GtkHTMLISearch *data)
46{
47        /* printf ("isearch changed to '%s'\n", gtk_entry_get_text (entry)); */
48        if (*gtk_entry_get_text (GTK_ENTRY (data->html->priv->search_input_line))) {
49                html_engine_search_incremental (data->html->engine,
50                                                gtk_entry_get_text (GTK_ENTRY (data->html->priv->search_input_line)),
51                                                data->forward);
52        } else
53                html_engine_unselect_all (data->html->engine);
54        data->changed = TRUE;
55}
56
57static void
58continue_search (GtkHTMLISearch *data, gboolean forward)
59{
60        HTMLEngine *e = data->html->engine;
61
62        if (!data->changed && data->last_text && *data->last_text) {
63                gtk_entry_set_text (GTK_ENTRY (data->html->priv->search_input_line), data->last_text);
64                html_engine_search_incremental (data->html->engine, data->last_text, forward);
65                data->changed = TRUE;
66        } else if (*gtk_entry_get_text (GTK_ENTRY (data->html->priv->search_input_line))) {
67                if (e->search_info)
68                        html_search_set_forward (e->search_info, forward);
69                html_engine_search_next (e);
70        }
71        data->forward = forward;
72}
73
74static gboolean
75hide (GtkHTMLISearch *data)
76{
77        gtk_signal_disconnect (GTK_OBJECT (data->html->priv->search_input_line), data->focus_out);
78        gtk_grab_remove (GTK_WIDGET (data->html->priv->search_input_line));
79        gtk_widget_grab_focus (GTK_WIDGET (data->html));
80        gtk_widget_hide (GTK_WIDGET (data->html->priv->search_input_line));
81
82        return FALSE;
83}
84
85static void
86data_destroy (GtkHTMLISearch *data)
87{
88        g_free (data->last_text);
89        g_free (data);
90}
91
92static gint
93key_press (GtkWidget *widget, GdkEventKey *event, GtkHTMLISearch *data)
94{
95        gint rv = TRUE;
96
97        if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_s) {
98                continue_search (data, TRUE);
99        } else if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_r) {
100                continue_search (data, FALSE);
101        } else if (event->keyval == GDK_Escape) {
102                hide (data);
103        } else
104                rv = FALSE;
105
106        return rv;
107}
108
109static gint
110focus_out_event (GtkWidget *widget, GdkEventFocus *event, GtkHTMLISearch *data)
111{
112        hide (data);
113
114        return FALSE;
115}
116
117static void
118destroy (GtkWidget *w, GtkHTMLISearch *data)
119{
120        data_destroy (data);
121}
122
123void
124gtk_html_isearch (GtkHTML *html, gboolean forward)
125{
126        GtkHTMLISearch *data;
127
128        if (!html->editor_api->create_input_line)
129                return;
130
131        if (!html->priv->search_input_line) {
132                html->priv->search_input_line  = (*html->editor_api->create_input_line) (html, html->editor_data);
133                if (!html->priv->search_input_line)
134                        return;
135                gtk_widget_ref (GTK_WIDGET (html->priv->search_input_line));
136
137                data = g_new (GtkHTMLISearch, 1);
138                gtk_object_set_data (GTK_OBJECT (html->priv->search_input_line), "search_data", data);
139
140                data->html      = html;
141
142                gtk_signal_connect (GTK_OBJECT (html->priv->search_input_line), "key_press_event",
143                                    GTK_SIGNAL_FUNC (key_press), data);
144                gtk_signal_connect (GTK_OBJECT (html->priv->search_input_line), "changed",
145                                    GTK_SIGNAL_FUNC (changed), data);
146                gtk_signal_connect (GTK_OBJECT (html->priv->search_input_line), "destroy",
147                                    GTK_SIGNAL_FUNC (destroy), data);
148        } else {
149                gtk_widget_show (GTK_WIDGET (html->priv->search_input_line));
150                data = gtk_object_get_data (GTK_OBJECT (html->priv->search_input_line), "search_data");
151        }
152
153        data->forward   = forward;
154        data->changed   = FALSE;
155        data->last_text = NULL;
156
157        if (html->engine->search_info) {
158                data->last_text = g_strdup (html->engine->search_info->text);
159                html_search_set_text (html->engine->search_info, "");
160        }
161
162        gtk_widget_grab_focus (GTK_WIDGET (html->priv->search_input_line));
163        data->focus_out = gtk_signal_connect (GTK_OBJECT (html->priv->search_input_line), "focus_out_event",
164                                              GTK_SIGNAL_FUNC (focus_out_event), data);
165}
166
167gboolean
168gtk_html_engine_search (GtkHTML *html, const gchar *text, gboolean case_sensitive, gboolean forward, gboolean regular)
169{
170        return html_engine_search (html->engine, text, case_sensitive, forward, regular);
171}
172
173void
174gtk_html_engine_search_set_forward (GtkHTML *html, gboolean forward)
175{
176        html_engine_search_set_forward (html->engine, forward);
177}
178
179gboolean
180gtk_html_engine_search_next (GtkHTML *html)
181{
182        return html_engine_search_next (html->engine);
183}
184
185gboolean
186gtk_html_engine_search_incremental (GtkHTML *html, const gchar *text, gboolean forward)
187{
188        return html_engine_search_incremental (html->engine, text, forward);
189}
Note: See TracBrowser for help on using the repository browser.