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., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | #include <string.h> |
---|
24 | #include <gdk/gdkkeysyms.h> |
---|
25 | #include <gtk/gtksignal.h> |
---|
26 | #include <gtk/gtkentry.h> |
---|
27 | |
---|
28 | #include "htmltextinput.h" |
---|
29 | #include "htmlform.h" |
---|
30 | |
---|
31 | |
---|
32 | HTMLTextInputClass html_text_input_class; |
---|
33 | static HTMLEmbeddedClass *parent_class = NULL; |
---|
34 | |
---|
35 | |
---|
36 | /* HTMLObject methods. */ |
---|
37 | |
---|
38 | static void |
---|
39 | destroy (HTMLObject *o) |
---|
40 | { |
---|
41 | HTMLTextInput *ti; |
---|
42 | |
---|
43 | ti = HTML_TEXTINPUT (o); |
---|
44 | |
---|
45 | if (ti->default_text) |
---|
46 | g_free (ti->default_text); |
---|
47 | |
---|
48 | HTML_OBJECT_CLASS (parent_class)->destroy (o); |
---|
49 | } |
---|
50 | |
---|
51 | static void |
---|
52 | copy (HTMLObject *self, |
---|
53 | HTMLObject *dest) |
---|
54 | { |
---|
55 | (* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest); |
---|
56 | |
---|
57 | HTML_TEXTINPUT (dest)->size = HTML_TEXTINPUT (self)->size; |
---|
58 | HTML_TEXTINPUT (dest)->maxlen = HTML_TEXTINPUT (self)->maxlen; |
---|
59 | HTML_TEXTINPUT (dest)->password = HTML_TEXTINPUT (self)->password; |
---|
60 | HTML_TEXTINPUT (dest)->default_text = g_strdup (HTML_TEXTINPUT (self)->default_text); |
---|
61 | |
---|
62 | /* g_warning ("HTMLTextInput::copy is not complete"); */ |
---|
63 | } |
---|
64 | |
---|
65 | static void |
---|
66 | reset (HTMLEmbedded *e) |
---|
67 | { |
---|
68 | gtk_entry_set_text (GTK_ENTRY (e->widget), HTML_TEXTINPUT (e)->default_text); |
---|
69 | } |
---|
70 | |
---|
71 | static gboolean |
---|
72 | html_text_input_key_pressed (GtkWidget *w, GdkEventKey *ev, gpointer p) |
---|
73 | { |
---|
74 | HTMLEmbedded *e = HTML_EMBEDDED(p); |
---|
75 | HTMLEmbedded *next = NULL; |
---|
76 | HTMLEmbedded *current = NULL; |
---|
77 | gboolean found = FALSE; |
---|
78 | GList *node = NULL; |
---|
79 | |
---|
80 | if (ev->keyval == GDK_Return) { |
---|
81 | for (node = e->form->elements; node; node = node->next) { |
---|
82 | current = HTML_EMBEDDED(node->data); |
---|
83 | |
---|
84 | /* focus on the next visible element */ |
---|
85 | if (current->widget && found |
---|
86 | && HTML_OBJECT_TYPE(current) != HTML_TYPE_BUTTON |
---|
87 | && HTML_OBJECT_TYPE(current) != HTML_TYPE_IMAGEINPUT) { |
---|
88 | next = current; |
---|
89 | break; |
---|
90 | } |
---|
91 | |
---|
92 | if (node->data == e) |
---|
93 | found = TRUE; |
---|
94 | } |
---|
95 | |
---|
96 | if (next) |
---|
97 | gtk_widget_grab_focus (next->widget); |
---|
98 | else if (found) |
---|
99 | html_form_submit (e->form); |
---|
100 | else |
---|
101 | g_warning ("Not in form's element list. Couldn't focus successor."); |
---|
102 | |
---|
103 | g_signal_stop_emission_by_name (w, "key_press_event"); |
---|
104 | return TRUE; |
---|
105 | } |
---|
106 | return FALSE; |
---|
107 | } |
---|
108 | |
---|
109 | /* HTMLEmbedded methods. */ |
---|
110 | |
---|
111 | static gchar * |
---|
112 | encode (HTMLEmbedded *e) |
---|
113 | { |
---|
114 | GString *encoding = g_string_new (""); |
---|
115 | gchar *ptr; |
---|
116 | |
---|
117 | if(strlen (e->name)) { |
---|
118 | ptr = html_embedded_encode_string (e->name); |
---|
119 | encoding = g_string_append (encoding, ptr); |
---|
120 | g_free (ptr); |
---|
121 | |
---|
122 | encoding = g_string_append_c (encoding, '='); |
---|
123 | |
---|
124 | ptr = html_embedded_encode_string (gtk_entry_get_text (GTK_ENTRY (e->widget))); |
---|
125 | encoding = g_string_append (encoding, ptr); |
---|
126 | g_free (ptr); |
---|
127 | } |
---|
128 | |
---|
129 | ptr = encoding->str; |
---|
130 | g_string_free(encoding, FALSE); |
---|
131 | |
---|
132 | return ptr; |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | void |
---|
137 | html_text_input_type_init (void) |
---|
138 | { |
---|
139 | html_text_input_class_init (&html_text_input_class, |
---|
140 | HTML_TYPE_TEXTINPUT, |
---|
141 | sizeof (HTMLTextInput)); |
---|
142 | } |
---|
143 | |
---|
144 | void |
---|
145 | html_text_input_class_init (HTMLTextInputClass *klass, |
---|
146 | HTMLType type, |
---|
147 | guint object_size) |
---|
148 | { |
---|
149 | HTMLEmbeddedClass *element_class; |
---|
150 | HTMLObjectClass *object_class; |
---|
151 | |
---|
152 | element_class = HTML_EMBEDDED_CLASS (klass); |
---|
153 | object_class = HTML_OBJECT_CLASS (klass); |
---|
154 | |
---|
155 | html_embedded_class_init (element_class, type, object_size); |
---|
156 | |
---|
157 | /* HTMLObject methods. */ |
---|
158 | object_class->destroy = destroy; |
---|
159 | object_class->copy = copy; |
---|
160 | |
---|
161 | /* HTMLEmbedded methods. */ |
---|
162 | element_class->reset = reset; |
---|
163 | element_class->encode = encode; |
---|
164 | |
---|
165 | parent_class = &html_embedded_class; |
---|
166 | } |
---|
167 | |
---|
168 | void |
---|
169 | html_text_input_init (HTMLTextInput *ti, |
---|
170 | HTMLTextInputClass *klass, |
---|
171 | GtkWidget *parent, |
---|
172 | gchar *name, |
---|
173 | gchar *value, |
---|
174 | gint size, |
---|
175 | gint maxlen, |
---|
176 | gboolean password) |
---|
177 | { |
---|
178 | HTMLEmbedded *element; |
---|
179 | HTMLObject *object; |
---|
180 | GtkWidget *entry; |
---|
181 | |
---|
182 | element = HTML_EMBEDDED (ti); |
---|
183 | object = HTML_OBJECT (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 * |
---|
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 | } |
---|