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 (C) 2000 Jonas Borgstr�m <jonas_b@bitsmart.com>.
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Library General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Library General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Library General Public License
17  *  along with this library; see the file COPYING.LIB.  If not, write to
18  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  *  Boston, MA 02110-1301, USA.
20 */
21 
22 #include <config.h>
23 #include <string.h>
24 #include <gtk/gtk.h>
25 #include <gdk/gdkkeysyms.h>
26 
27 #include "htmltextinput.h"
28 #include "htmlform.h"
29 
30 
31 HTMLTextInputClass html_text_input_class;
32 static HTMLEmbeddedClass *parent_class = NULL;
33 
34 
35 /* HTMLObject methods.  */
36 
37 static void
destroy(HTMLObject * o)38 destroy (HTMLObject *o)
39 {
40 	HTMLTextInput *ti;
41 
42 	ti = HTML_TEXTINPUT (o);
43 
44 	if (ti->default_text)
45 		g_free (ti->default_text);
46 
47 	HTML_OBJECT_CLASS (parent_class)->destroy (o);
48 }
49 
50 static void
copy(HTMLObject * self,HTMLObject * dest)51 copy (HTMLObject *self,
52       HTMLObject *dest)
53 {
54 	(* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest);
55 
56 	HTML_TEXTINPUT (dest)->size = HTML_TEXTINPUT (self)->size;
57 	HTML_TEXTINPUT (dest)->maxlen = HTML_TEXTINPUT (self)->maxlen;
58 	HTML_TEXTINPUT (dest)->password = HTML_TEXTINPUT (self)->password;
59 	HTML_TEXTINPUT (dest)->default_text = g_strdup (HTML_TEXTINPUT (self)->default_text);
60 
61 	/* g_warning ("HTMLTextInput::copy is not complete"); */
62 }
63 
64 static void
reset(HTMLEmbedded * e)65 reset (HTMLEmbedded *e)
66 {
67 	gtk_entry_set_text (GTK_ENTRY (e->widget), HTML_TEXTINPUT (e)->default_text);
68 }
69 
70 static gboolean
html_text_input_key_pressed(GtkWidget * w,GdkEventKey * ev,gpointer p)71 html_text_input_key_pressed (GtkWidget *w,
72                              GdkEventKey *ev,
73                              gpointer p)
74 {
75 	HTMLEmbedded *e = HTML_EMBEDDED (p);
76 	HTMLEmbedded *next = NULL;
77 	HTMLEmbedded *current = NULL;
78 	gboolean found = FALSE;
79 	GList *node = NULL;
80 
81 	if (ev->keyval == GDK_KEY_Return) {
82 		for (node = e->form->elements; node; node = node->next) {
83 			current = HTML_EMBEDDED (node->data);
84 
85 			/* focus on the next visible element */
86 			if (current->widget && found
87 			    && HTML_OBJECT_TYPE (current) != HTML_TYPE_BUTTON
88 			    && HTML_OBJECT_TYPE (current) != HTML_TYPE_IMAGEINPUT) {
89 				next = current;
90 				break;
91 			}
92 
93 			if (node->data == e)
94 				found = TRUE;
95 		}
96 
97 		if (next)
98 			gtk_widget_grab_focus (next->widget);
99 		else if (found)
100 			html_form_submit (e->form);
101 		else
102 			g_warning ("Not in form's element list.  Couldn't focus successor.");
103 
104 		g_signal_stop_emission_by_name (w, "key_press_event");
105 		return TRUE;
106 	}
107 	return FALSE;
108 }
109 
110 /* HTMLEmbedded methods.  */
111 
112 static gchar *
encode(HTMLEmbedded * e,const gchar * codepage)113 encode (HTMLEmbedded *e,
114         const gchar *codepage)
115 {
116 	GString *encoding = g_string_new ("");
117 	gchar *ptr;
118 
119 	if (strlen (e->name)) {
120 		ptr = html_embedded_encode_string (e->name, codepage);
121 		encoding = g_string_append (encoding, ptr);
122 		g_free (ptr);
123 
124 		encoding = g_string_append_c (encoding, '=');
125 
126 		ptr = html_embedded_encode_string (gtk_entry_get_text (GTK_ENTRY (e->widget)), codepage);
127 		encoding = g_string_append (encoding, ptr);
128 		g_free (ptr);
129 	}
130 
131 	ptr = encoding->str;
132 	g_string_free (encoding, FALSE);
133 
134 	return ptr;
135 }
136 
137 
138 void
html_text_input_type_init(void)139 html_text_input_type_init (void)
140 {
141 	html_text_input_class_init (&html_text_input_class,
142 				    HTML_TYPE_TEXTINPUT,
143 				    sizeof (HTMLTextInput));
144 }
145 
146 void
html_text_input_class_init(HTMLTextInputClass * klass,HTMLType type,guint object_size)147 html_text_input_class_init (HTMLTextInputClass *klass,
148                             HTMLType type,
149                             guint object_size)
150 {
151 	HTMLEmbeddedClass *element_class;
152 	HTMLObjectClass *object_class;
153 
154 	element_class = HTML_EMBEDDED_CLASS (klass);
155 	object_class = HTML_OBJECT_CLASS (klass);
156 
157 	html_embedded_class_init (element_class, type, object_size);
158 
159 	/* HTMLObject methods.   */
160 	object_class->destroy = destroy;
161 	object_class->copy = copy;
162 
163 	/* HTMLEmbedded methods.   */
164 	element_class->reset = reset;
165 	element_class->encode = encode;
166 
167 	parent_class = &html_embedded_class;
168 }
169 
170 void
html_text_input_init(HTMLTextInput * ti,HTMLTextInputClass * klass,GtkWidget * parent,gchar * name,gchar * value,gint size,gint maxlen,gboolean password)171 html_text_input_init (HTMLTextInput *ti,
172                       HTMLTextInputClass *klass,
173                       GtkWidget *parent,
174                       gchar *name,
175                       gchar *value,
176                       gint size,
177                       gint maxlen,
178                       gboolean password)
179 {
180 	HTMLEmbedded *element;
181 	GtkWidget *entry;
182 
183 	element = HTML_EMBEDDED (ti);
184 
185 	html_embedded_init (element, HTML_EMBEDDED_CLASS (klass),
186 			   parent, name, value);
187 
188 	entry = gtk_entry_new ();
189 	html_embedded_set_widget (element, entry);
190 	g_signal_connect_after (entry, "key_press_event", G_CALLBACK (html_text_input_key_pressed), element);
191 
192 	if (strlen (element->value))
193 		gtk_entry_set_text (GTK_ENTRY (element->widget), element->value);
194 
195 	ti->default_text = g_strdup (element->value);
196 
197 	if (maxlen != -1)
198 		gtk_entry_set_max_length (GTK_ENTRY (element->widget), maxlen);
199 
200 	gtk_entry_set_visibility (GTK_ENTRY (element->widget), !password);
201 
202 	gtk_entry_set_width_chars (GTK_ENTRY (element->widget), size);
203 
204 	ti->size = size;
205 	ti->maxlen = maxlen;
206 }
207 
208 HTMLObject *
html_text_input_new(GtkWidget * parent,gchar * name,gchar * value,gint size,gint maxlen,gboolean password)209 html_text_input_new (GtkWidget *parent,
210                      gchar *name,
211                      gchar *value,
212                      gint size,
213                      gint maxlen,
214                      gboolean password)
215 {
216 	HTMLTextInput *ti;
217 
218 	ti = g_new0 (HTMLTextInput, 1);
219 	html_text_input_init (ti, &html_text_input_class,
220 			      parent, name, value, size,
221 			      maxlen, password);
222 
223 	return HTML_OBJECT (ti);
224 }
225