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

Revision 19539, 5.5 KB checked in by ghudson, 21 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r19538, 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 <gtk/gtkobject.h>
29#include "gtkhtml.h"
30#include "gtkhtml-private.h"
31#include "gtkhtml-search.h"
32#include "htmlengine-search.h"
33#include "htmlsearch.h"
34#include "htmlselection.h"
35
36struct _GtkHTMLISearch {
37        GtkHTML  *html;
38        gboolean forward;
39        gboolean changed;
40        guint focus_out;
41        gchar *last_text;
42};
43typedef struct _GtkHTMLISearch GtkHTMLISearch;
44
45static void
46changed (GtkEntry *entry, GtkHTMLISearch *data)
47{
48        /* printf ("isearch changed to '%s'\n", gtk_entry_get_text (entry)); */
49        if (*gtk_entry_get_text (GTK_ENTRY (data->html->priv->search_input_line))) {
50                html_engine_search_incremental (data->html->engine,
51                                                gtk_entry_get_text (GTK_ENTRY (data->html->priv->search_input_line)),
52                                                data->forward);
53        } else
54                html_engine_unselect_all (data->html->engine);
55        data->changed = TRUE;
56}
57
58static void
59continue_search (GtkHTMLISearch *data, gboolean forward)
60{
61        HTMLEngine *e = data->html->engine;
62
63        if (!data->changed && data->last_text && *data->last_text) {
64                gtk_entry_set_text (GTK_ENTRY (data->html->priv->search_input_line), data->last_text);
65                html_engine_search_incremental (data->html->engine, data->last_text, forward);
66                data->changed = TRUE;
67        } else if (*gtk_entry_get_text (GTK_ENTRY (data->html->priv->search_input_line))) {
68                if (e->search_info)
69                        html_search_set_forward (e->search_info, forward);
70                html_engine_search_next (e);
71        }
72        data->forward = forward;
73}
74
75static gboolean
76hide (GtkHTMLISearch *data)
77{
78        g_signal_handler_disconnect (data->html->priv->search_input_line, data->focus_out);
79        gtk_grab_remove (GTK_WIDGET (data->html->priv->search_input_line));
80        gtk_widget_grab_focus (GTK_WIDGET (data->html));
81        gtk_widget_hide (GTK_WIDGET (data->html->priv->search_input_line));
82
83        return FALSE;
84}
85
86static void
87data_destroy (GtkHTMLISearch *data)
88{
89        g_free (data->last_text);
90        g_free (data);
91}
92
93static gint
94key_press (GtkWidget *widget, GdkEventKey *event, GtkHTMLISearch *data)
95{
96        gint rv = TRUE;
97
98        if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_s) {
99                continue_search (data, TRUE);
100        } else if (event->state & GDK_CONTROL_MASK && event->keyval == GDK_r) {
101                continue_search (data, FALSE);
102        } else if (event->keyval == GDK_Escape) {
103                hide (data);
104        } else
105                rv = FALSE;
106
107        return rv;
108}
109
110static gint
111focus_out_event (GtkWidget *widget, GdkEventFocus *event, GtkHTMLISearch *data)
112{
113        hide (data);
114
115        return FALSE;
116}
117
118static void
119destroy (GtkWidget *w, GtkHTMLISearch *data)
120{
121        data_destroy (data);
122}
123
124void
125gtk_html_isearch (GtkHTML *html, gboolean forward)
126{
127        GtkHTMLISearch *data;
128
129        if (!html->editor_api->create_input_line)
130                return;
131
132        if (!html->priv->search_input_line) {
133                html->priv->search_input_line  = (*html->editor_api->create_input_line) (html, html->editor_data);
134                if (!html->priv->search_input_line)
135                        return;
136                gtk_widget_ref (GTK_WIDGET (html->priv->search_input_line));
137
138                data = g_new (GtkHTMLISearch, 1);
139                g_object_set_data (G_OBJECT (html->priv->search_input_line), "search_data", data);
140
141                data->html      = html;
142
143                g_signal_connect (html->priv->search_input_line, "key_press_event", G_CALLBACK (key_press), data);
144                g_signal_connect (html->priv->search_input_line, "changed", G_CALLBACK (changed), data);
145                g_signal_connect (html->priv->search_input_line, "destroy", G_CALLBACK (destroy), data);
146        } else {
147                gtk_widget_show (GTK_WIDGET (html->priv->search_input_line));
148                data = (GtkHTMLISearch *) g_object_get_data (G_OBJECT (html->priv->search_input_line), "search_data");
149        }
150
151        data->forward   = forward;
152        data->changed   = FALSE;
153        data->last_text = NULL;
154
155        if (html->engine->search_info) {
156                data->last_text = g_strdup (html->engine->search_info->text);
157                html_search_set_text (html->engine->search_info, "");
158        }
159
160        gtk_widget_grab_focus (GTK_WIDGET (html->priv->search_input_line));
161        data->focus_out = g_signal_connect (html->priv->search_input_line, "focus_out_event",
162                                            G_CALLBACK (focus_out_event), data);
163}
164
165gboolean
166gtk_html_engine_search (GtkHTML *html, const gchar *text, gboolean case_sensitive, gboolean forward, gboolean regular)
167{
168        return html_engine_search (html->engine, text, case_sensitive, forward, regular);
169}
170
171void
172gtk_html_engine_search_set_forward (GtkHTML *html, gboolean forward)
173{
174        html_engine_search_set_forward (html->engine, forward);
175}
176
177gboolean
178gtk_html_engine_search_next (GtkHTML *html)
179{
180        return html_engine_search_next (html->engine);
181}
182
183gboolean
184gtk_html_engine_search_incremental (GtkHTML *html, const gchar *text, gboolean forward)
185{
186        return html_engine_search_incremental (html->engine, text, forward);
187}
Note: See TracBrowser for help on using the repository browser.