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 <gtk/gtkeditable.h> |
---|
25 | #include <gtk/gtksignal.h> |
---|
26 | #include <gtk/gtkscrolledwindow.h> |
---|
27 | #include <gtk/gtktextview.h> |
---|
28 | #include "htmltextarea.h" |
---|
29 | |
---|
30 | |
---|
31 | HTMLTextAreaClass html_textarea_class; |
---|
32 | static HTMLEmbeddedClass *parent_class = NULL; |
---|
33 | |
---|
34 | |
---|
35 | static void |
---|
36 | destroy (HTMLObject *o) |
---|
37 | { |
---|
38 | HTMLTextArea *ta; |
---|
39 | |
---|
40 | ta = HTML_TEXTAREA (o); |
---|
41 | |
---|
42 | if (ta->default_text) |
---|
43 | g_free (ta->default_text); |
---|
44 | |
---|
45 | HTML_OBJECT_CLASS (parent_class)->destroy (o); |
---|
46 | } |
---|
47 | |
---|
48 | static void |
---|
49 | copy (HTMLObject *self, |
---|
50 | HTMLObject *dest) |
---|
51 | { |
---|
52 | (* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest); |
---|
53 | |
---|
54 | /* FIXME TODO this is not going to work. */ |
---|
55 | |
---|
56 | HTML_TEXTAREA (dest)->text = NULL; |
---|
57 | HTML_TEXTAREA (dest)->default_text = g_strdup (HTML_TEXTAREA (self)->default_text); |
---|
58 | |
---|
59 | /* FIXME g_warning ("HTMLTextArea::copy is not complete."); */ |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | static void |
---|
64 | reset (HTMLEmbedded *e) |
---|
65 | { |
---|
66 | html_textarea_set_text ( HTML_TEXTAREA (e), HTML_TEXTAREA (e)->default_text); |
---|
67 | } |
---|
68 | |
---|
69 | static gchar * |
---|
70 | encode (HTMLEmbedded *e) |
---|
71 | { |
---|
72 | GString *encoding = g_string_new (""); |
---|
73 | gchar *encoded_str, *utf8_str, *gtk_text; |
---|
74 | |
---|
75 | if(strlen (e->name)) { |
---|
76 | GtkTextIter first, last; |
---|
77 | |
---|
78 | utf8_str = html_embedded_encode_string (e->name); |
---|
79 | encoding = g_string_append (encoding, utf8_str); |
---|
80 | g_free (utf8_str); |
---|
81 | |
---|
82 | encoding = g_string_append_c (encoding, '='); |
---|
83 | |
---|
84 | gtk_text_buffer_get_bounds (HTML_TEXTAREA (e)->buffer, &first, &last); |
---|
85 | gtk_text = gtk_text_buffer_get_text (HTML_TEXTAREA (e)->buffer, &first, &last, FALSE); |
---|
86 | |
---|
87 | encoded_str = html_embedded_encode_string (gtk_text); |
---|
88 | encoding = g_string_append (encoding, encoded_str); |
---|
89 | |
---|
90 | g_free (encoded_str); |
---|
91 | g_free (gtk_text); |
---|
92 | } |
---|
93 | |
---|
94 | utf8_str = encoding->str; |
---|
95 | g_string_free(encoding, FALSE); |
---|
96 | |
---|
97 | return utf8_str; |
---|
98 | } |
---|
99 | |
---|
100 | void |
---|
101 | html_textarea_type_init (void) |
---|
102 | { |
---|
103 | html_textarea_class_init (&html_textarea_class, HTML_TYPE_TEXTAREA, sizeof (HTMLTextArea)); |
---|
104 | } |
---|
105 | |
---|
106 | void |
---|
107 | html_textarea_class_init (HTMLTextAreaClass *klass, |
---|
108 | HTMLType type, |
---|
109 | guint object_size) |
---|
110 | { |
---|
111 | HTMLEmbeddedClass *element_class; |
---|
112 | HTMLObjectClass *object_class; |
---|
113 | |
---|
114 | element_class = HTML_EMBEDDED_CLASS (klass); |
---|
115 | object_class = HTML_OBJECT_CLASS (klass); |
---|
116 | |
---|
117 | html_embedded_class_init (element_class, type, object_size); |
---|
118 | |
---|
119 | /* HTMLEmbedded methods. */ |
---|
120 | element_class->reset = reset; |
---|
121 | element_class->encode = encode; |
---|
122 | |
---|
123 | /* HTMLObject methods. */ |
---|
124 | object_class->destroy = destroy; |
---|
125 | object_class->copy = copy; |
---|
126 | |
---|
127 | parent_class = &html_embedded_class; |
---|
128 | } |
---|
129 | |
---|
130 | void |
---|
131 | html_textarea_init (HTMLTextArea *ta, |
---|
132 | HTMLTextAreaClass *klass, |
---|
133 | GtkWidget *parent, |
---|
134 | gchar *name, |
---|
135 | gint row, |
---|
136 | gint col) |
---|
137 | { |
---|
138 | GtkWidget *sw; |
---|
139 | HTMLEmbedded *element; |
---|
140 | HTMLObject *object; |
---|
141 | PangoLayout *layout; |
---|
142 | gint width, height; |
---|
143 | |
---|
144 | element = HTML_EMBEDDED (ta); |
---|
145 | object = HTML_OBJECT (ta); |
---|
146 | |
---|
147 | html_embedded_init (element, HTML_EMBEDDED_CLASS (klass), |
---|
148 | parent, name, NULL); |
---|
149 | |
---|
150 | ta->buffer = gtk_text_buffer_new (NULL); |
---|
151 | ta->text = gtk_text_view_new_with_buffer (ta->buffer); |
---|
152 | gtk_text_view_set_editable (GTK_TEXT_VIEW (ta->text), TRUE); |
---|
153 | |
---|
154 | gtk_widget_set_events (ta->text, GDK_BUTTON_PRESS_MASK); |
---|
155 | |
---|
156 | sw = gtk_scrolled_window_new (NULL, NULL); |
---|
157 | gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw), GTK_SHADOW_IN); |
---|
158 | gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); |
---|
159 | gtk_container_add (GTK_CONTAINER (sw), ta->text); |
---|
160 | gtk_widget_show_all (sw); |
---|
161 | html_embedded_set_widget (element, sw); |
---|
162 | |
---|
163 | layout = pango_layout_new (gtk_widget_get_pango_context (ta->text)); |
---|
164 | pango_layout_set_font_description (layout, ta->text->style->font_desc); |
---|
165 | pango_layout_set_text (layout, "0", 1); |
---|
166 | pango_layout_get_size (layout, &width, &height); |
---|
167 | g_object_unref (layout); |
---|
168 | |
---|
169 | gtk_widget_set_size_request (ta->text, (width / PANGO_SCALE) * col + 8, (height / PANGO_SCALE) * row + 4); |
---|
170 | |
---|
171 | ta->default_text = NULL; |
---|
172 | } |
---|
173 | |
---|
174 | HTMLObject * |
---|
175 | html_textarea_new (GtkWidget *parent, |
---|
176 | gchar *name, |
---|
177 | gint row, |
---|
178 | gint col) |
---|
179 | { |
---|
180 | HTMLTextArea *ta; |
---|
181 | |
---|
182 | ta = g_new0 (HTMLTextArea, 1); |
---|
183 | html_textarea_init (ta, &html_textarea_class, |
---|
184 | parent, name, row, col); |
---|
185 | |
---|
186 | return HTML_OBJECT (ta); |
---|
187 | } |
---|
188 | |
---|
189 | void html_textarea_set_text (HTMLTextArea *ta, gchar *text) |
---|
190 | { |
---|
191 | GtkTextIter begin, end; |
---|
192 | |
---|
193 | if (!ta->default_text) |
---|
194 | ta->default_text = g_strdup (text); |
---|
195 | |
---|
196 | gtk_text_buffer_get_bounds (ta->buffer, &begin, &end); |
---|
197 | gtk_text_buffer_delete (ta->buffer, &begin, &end); |
---|
198 | gtk_text_buffer_get_bounds (ta->buffer, &begin, &end); |
---|
199 | gtk_text_buffer_insert (ta->buffer, &begin, text, strlen (text)); |
---|
200 | } |
---|