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 "htmlimageinput.h"
24 #include "htmlform.h"
25 #include <string.h>
26
27
28 HTMLImageInputClass html_imageinput_class;
29 static HTMLEmbeddedClass *parent_class;
30
31
32 static void
destroy(HTMLObject * o)33 destroy (HTMLObject *o)
34 {
35 html_object_destroy (HTML_OBJECT (HTML_IMAGEINPUT (o)->image));
36
37 HTML_OBJECT_CLASS (parent_class)->destroy (o);
38 }
39
40 static void
copy(HTMLObject * self,HTMLObject * dest)41 copy (HTMLObject *self,
42 HTMLObject *dest)
43 {
44 HTMLObject *duplicate_image;
45
46 (* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest);
47
48 HTML_IMAGEINPUT (dest)->m_x = HTML_IMAGEINPUT (self)->m_x;
49 HTML_IMAGEINPUT (dest)->m_y = HTML_IMAGEINPUT (self)->m_y;
50
51 duplicate_image = html_object_dup (HTML_OBJECT (HTML_IMAGEINPUT (self)->image));
52 HTML_IMAGEINPUT (dest)->image = HTML_IMAGE (duplicate_image);
53 }
54
55 static void
draw(HTMLObject * o,HTMLPainter * p,gint x,gint y,gint width,gint height,gint tx,gint ty)56 draw (HTMLObject *o,
57 HTMLPainter *p,
58 gint x,
59 gint y,
60 gint width,
61 gint height,
62 gint tx,
63 gint ty)
64 {
65 HTML_OBJECT (HTML_IMAGEINPUT (o)->image)->x = o->x;
66 HTML_OBJECT (HTML_IMAGEINPUT (o)->image)->y = o->y;
67
68 html_object_draw (HTML_OBJECT (HTML_IMAGEINPUT (o)->image),
69 p,
70 x, y,
71 width, height,
72 tx, ty);
73 }
74
75
76 /* Even if it's an HTMLEmbeddable, HTMLImageInput does not use a
77 * widget, so we need to implement these methods ourselves instead of
78 * using the HTMLEmbeddable default implementations. */
79
80 static gint
calc_min_width(HTMLObject * self,HTMLPainter * painter)81 calc_min_width (HTMLObject *self,
82 HTMLPainter *painter)
83 {
84 HTMLImageInput *image_input;
85
86 image_input = HTML_IMAGEINPUT (self);
87
88 return html_object_calc_min_width (HTML_OBJECT (image_input->image),
89 painter);
90 }
91
92 static gboolean
html_image_input_real_calc_size(HTMLObject * self,HTMLPainter * painter,GList ** changed_objs)93 html_image_input_real_calc_size (HTMLObject *self,
94 HTMLPainter *painter,
95 GList **changed_objs)
96 {
97 HTMLImageInput *image_input;
98 HTMLObject *image_object;
99 gboolean retval;
100
101 image_input = HTML_IMAGEINPUT (self);
102 image_object = HTML_OBJECT (image_input->image);
103
104 retval = html_object_calc_size (image_object, painter, changed_objs);
105
106 self->width = image_object->width;
107 self->ascent = image_object->ascent;
108 self->descent = image_object->descent;
109
110 return retval;
111 }
112
113
114 static gchar *
encode(HTMLEmbedded * e,const gchar * codepage)115 encode (HTMLEmbedded *e,
116 const gchar *codepage)
117 {
118 GString *encoding = g_string_new ("");
119 gchar *ptr;
120
121 if (strlen (e->name)) {
122 ptr = html_embedded_encode_string (e->name, codepage);
123 encoding = g_string_assign (encoding, ptr);
124 g_free (ptr);
125
126 ptr = g_strdup_printf (".x=%d&", HTML_IMAGEINPUT (e)->m_x);
127 encoding = g_string_append (encoding, ptr);
128 g_free (ptr);
129
130 ptr = html_embedded_encode_string (e->name, codepage);
131 encoding = g_string_append (encoding, ptr);
132 g_free (ptr);
133
134 ptr = g_strdup_printf (".y=%d", HTML_IMAGEINPUT (e)->m_y);
135 encoding = g_string_append (encoding, ptr);
136 g_free (ptr);
137 }
138
139 ptr = encoding->str;
140 g_string_free (encoding, FALSE);
141
142 return ptr;
143 }
144
145
146 void
html_imageinput_type_init(void)147 html_imageinput_type_init (void)
148 {
149 html_imageinput_class_init (&html_imageinput_class, HTML_TYPE_IMAGEINPUT, sizeof (HTMLImageInput));
150 }
151
152 void
html_imageinput_class_init(HTMLImageInputClass * klass,HTMLType type,guint size)153 html_imageinput_class_init (HTMLImageInputClass *klass,
154 HTMLType type,
155 guint size)
156 {
157 HTMLEmbeddedClass *element_class;
158 HTMLObjectClass *object_class;
159
160 element_class = HTML_EMBEDDED_CLASS (klass);
161 object_class = HTML_OBJECT_CLASS (klass);
162
163 html_embedded_class_init (element_class, type, size);
164
165 /* HTMLEmbedded methods. */
166 element_class->encode = encode;
167
168 /* HTMLObject methods. */
169 object_class->destroy = destroy;
170 object_class->copy = copy;
171 object_class->draw = draw;
172 object_class->calc_min_width = calc_min_width;
173 object_class->calc_size = html_image_input_real_calc_size;
174
175 parent_class = &html_embedded_class;
176 }
177
178 void
html_imageinput_init(HTMLImageInput * img,HTMLImageInputClass * klass,HTMLImageFactory * imf,gchar * name,gchar * url)179 html_imageinput_init (HTMLImageInput *img,
180 HTMLImageInputClass *klass,
181 HTMLImageFactory *imf,
182 gchar *name,
183 gchar *url)
184 {
185 HTMLEmbedded *element;
186 HTMLObject *object;
187
188 element = HTML_EMBEDDED (img);
189 object = HTML_OBJECT (img);
190
191 html_embedded_init (element, HTML_EMBEDDED_CLASS (klass), NULL, name, NULL);
192
193 object->width = object->ascent = 32;
194
195 img->image = HTML_IMAGE (html_image_new (imf,
196 url, NULL, NULL,
197 -1, -1, FALSE, FALSE, 0,
198 NULL,
199 HTML_VALIGN_BOTTOM, FALSE));
200
201 object->ascent = 32;
202 object->width = 0;
203 object->descent = 0;
204 }
205
206 HTMLObject *
html_imageinput_new(HTMLImageFactory * imf,gchar * name,gchar * url)207 html_imageinput_new (HTMLImageFactory *imf,
208 gchar *name,
209 gchar *url)
210 {
211 HTMLImageInput *img;
212
213 img = g_new0 (HTMLImageInput, 1);
214 html_imageinput_init (img, &html_imageinput_class, imf, name, url);
215
216 return HTML_OBJECT (img);
217 }
218