source: trunk/third/gtkhtml/src/htmlrule.c @ 18136

Revision 18136, 7.2 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18135, 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 (C) 1997 Martin Jones (mjones@kde.org)
5   Copyright (C) 1997 Torben Weis (weis@kde.org)
6   Copyright (C) 2000 Helix Code, Inc.
7   
8   This library is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Library General Public
10   License as published by the Free Software Foundation; either
11   version 2 of the License, or (at your option) any later version.
12   
13   This library is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16   Library General Public License for more details.
17   
18   You should have received a copy of the GNU Library General Public License
19   along with this library; see the file COPYING.LIB.  If not, write to
20   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21   Boston, MA 02111-1307, USA.
22*/
23
24#include <config.h>
25#include "htmlcolor.h"
26#include "htmlcolorset.h"
27#include "htmlengine-save.h"
28#include "htmlrule.h"
29#include "htmlpainter.h"
30
31
32HTMLRuleClass html_rule_class;
33static HTMLObjectClass *parent_class = NULL;
34
35
36/* HTMLObject methods.  */
37
38#define HTML_RULE_MIN_SIZE 12
39
40static void
41copy (HTMLObject *self,
42      HTMLObject *dest)
43{
44        (* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest);
45
46        HTML_RULE (dest)->length = HTML_RULE (self)->length;
47        HTML_RULE (dest)->size   = HTML_RULE (self)->size;
48        HTML_RULE (dest)->shade  = HTML_RULE (self)->shade;
49        HTML_RULE (dest)->halign = HTML_RULE (self)->halign;
50}
51
52static void
53set_max_width (HTMLObject *o, HTMLPainter *painter, gint max_width)
54{
55        o->max_width = max_width;
56}
57
58static gint
59calc_min_width (HTMLObject *o,
60                HTMLPainter *painter)
61{
62        gint pixel_size;
63
64        pixel_size = html_painter_get_pixel_size (painter);
65
66        if (HTML_RULE (o)->length > 0)
67                return HTML_RULE (o)->length * pixel_size;
68
69        return pixel_size;
70}
71
72static HTMLFitType
73fit_line (HTMLObject *o,
74          HTMLPainter *painter,
75          gboolean start_of_line,
76          gboolean first_run,
77          gboolean next_to_floating,
78          gint width_left)
79{
80        if (!start_of_line)
81                return HTML_FIT_NONE;
82        o->width = width_left;
83
84        if (o->percent == 0) {
85                gint pixel_size = html_painter_get_pixel_size (painter);
86                if (HTML_RULE (o)->length * pixel_size > width_left) {
87                        o->width = HTML_RULE (o)->length * pixel_size;
88                }
89        }
90
91        return (next_to_floating && width_left <= 0) ? HTML_FIT_NONE : HTML_FIT_COMPLETE;
92}
93
94static gboolean
95calc_size (HTMLObject *self, HTMLPainter *painter, GList **changed_objs)
96{
97        HTMLRule *rule;
98        gint ascent, descent;
99        gint pixel_size;
100        gint height;
101        gboolean changed;
102
103        rule = HTML_RULE (self);
104        pixel_size = html_painter_get_pixel_size (painter);
105
106        if (rule->size >= HTML_RULE_MIN_SIZE) {
107                height = rule->size;
108        } else {
109                height = HTML_RULE_MIN_SIZE;
110        }
111
112        ascent = (height / 2 + height % 2 + 1) * pixel_size;
113        descent = (height / 2 + 1) * pixel_size;
114
115        changed = FALSE;
116
117        if (self->width > self->max_width) {
118                self->width = self->max_width;
119                changed = TRUE;
120        }
121
122        if (ascent != self->ascent) {
123                self->ascent = ascent;
124                changed = TRUE;
125        }
126
127        if (descent != self->descent) {
128                self->descent = descent;
129                changed = TRUE;
130        }
131
132        return changed;
133}
134
135static void
136draw (HTMLObject *o,
137      HTMLPainter *p,
138      gint x, gint y,
139      gint width, gint height,
140      gint tx, gint ty)
141{
142        HTMLRule *rule;
143        guint w, h;
144        gint xp, yp;
145        gint pixel_size = html_painter_get_pixel_size (p);
146
147        rule = HTML_RULE (o);
148       
149        if (y + height < o->y - o->ascent || y > o->y + o->descent)
150                return;
151
152        h = rule->size * pixel_size;
153        xp = o->x + tx;
154        yp = o->y + ty - (rule->size / 2 + rule->size % 2) * pixel_size;
155
156        if (o->percent == 0)
157                w = o->width;
158        else
159                /* The cast to `gdouble' is to avoid overflow (eg. when
160                   printing).  */
161                w = ((gdouble) o->width * o->percent) / 100;
162
163        switch (rule->halign) {
164        case HTML_HALIGN_LEFT:
165                break;
166        case HTML_HALIGN_CENTER:
167        case HTML_HALIGN_NONE:
168                /* Default is `align=center' according to the specs.  */
169                xp += (o->width - w) / 2;
170                break;
171        case HTML_HALIGN_RIGHT:
172                xp += o->width - w;
173                break;
174        default:
175                g_warning ("Unknown HTMLRule alignment %d.", rule->halign);
176        }
177
178        if (rule->shade)
179                html_painter_draw_panel (p, &((html_colorset_get_color (p->color_set, HTMLBgColor))->color),
180                                         xp, yp, w, h, GTK_HTML_ETCH_IN, 1);
181        else {
182                html_painter_set_pen (p, &html_colorset_get_color_allocated (p, HTMLTextColor)->color);
183                html_painter_fill_rect (p, xp, yp, w, h);
184        }
185}
186
187static gboolean
188accepts_cursor (HTMLObject *self)
189{
190        return TRUE;
191}
192
193static gboolean
194save (HTMLObject *self,
195      HTMLEngineSaveState *state)
196{
197        gchar *size, *shade, *length;
198        gboolean rv;
199
200        size   = HTML_RULE (self)->size == 2 ? "" : g_strdup_printf (" SIZE=\"%d\"", HTML_RULE (self)->size);
201        shade  = HTML_RULE (self)->shade ? "" : " NOSHADE";
202        length = HTML_RULE (self)->length
203                ? g_strdup_printf (" LENGTH=\"%d\"", HTML_RULE (self)->length)
204                : (self->percent > 0 && self->percent != 100 ? g_strdup_printf (" LENGTH=\"%d%%\"", self->percent) : "");
205
206        rv = html_engine_save_output_string (state, "\n<HR%s%s%s>\n", shade, size, length);
207
208        if (*size)
209                g_free (size);
210        if (*length)
211                g_free (length);
212
213        return rv;
214}
215
216static gboolean
217save_plain (HTMLObject *self,
218            HTMLEngineSaveState *state,
219            gint requested_width)
220{
221        int i;
222       
223        if (!html_engine_save_output_string (state, "\n"))
224                return FALSE;
225
226        /* Fixme no alignment or percent */
227        for (i = 0; i < requested_width; i++)
228                if (!html_engine_save_output_string (state, "_"))
229                        return FALSE;
230       
231        if (!html_engine_save_output_string (state, "\n"))
232                return FALSE;
233
234        return TRUE;
235}
236
237void
238html_rule_type_init (void)
239{
240        html_rule_class_init (&html_rule_class, HTML_TYPE_RULE, sizeof (HTMLRule));
241}
242
243void
244html_rule_class_init (HTMLRuleClass *klass,
245                      HTMLType type,
246                      guint object_size)
247{
248        HTMLObjectClass *object_class;
249
250        object_class = HTML_OBJECT_CLASS (klass);
251
252        html_object_class_init (object_class, type, object_size);
253
254        object_class->copy = copy;
255        object_class->draw = draw;
256        object_class->set_max_width = set_max_width;
257        object_class->calc_min_width = calc_min_width;
258        object_class->fit_line = fit_line;
259        object_class->calc_size = calc_size;
260        object_class->accepts_cursor = accepts_cursor;
261        object_class->save = save;
262        object_class->save_plain = save_plain;
263
264        parent_class = &html_object_class;
265}
266
267void
268html_rule_init (HTMLRule *rule,
269                HTMLRuleClass *klass,
270                gint length,
271                gint percent,
272                gint size,
273                gboolean shade,
274                HTMLHAlignType halign)
275{
276        HTMLObject *object;
277
278        object = HTML_OBJECT (rule);
279
280        html_object_init (object, HTML_OBJECT_CLASS (klass));
281
282        if (size < 1)
283                size = 1;       /* FIXME why? */
284        rule->size = size;
285
286        object->percent = percent;
287
288        rule->length = length;
289        rule->shade  = shade;
290        rule->halign = halign;
291
292        if (percent > 0) {
293                object->flags &= ~ HTML_OBJECT_FLAG_FIXEDWIDTH;
294                rule->length = 0;
295        } else if (rule->length > 0) {
296                object->flags |= HTML_OBJECT_FLAG_FIXEDWIDTH;
297        } else {
298                object->flags &= ~ HTML_OBJECT_FLAG_FIXEDWIDTH;
299        }
300}
301
302HTMLObject *
303html_rule_new (gint length,
304               gint percent,
305               gint size,
306               gboolean shade,
307               HTMLHAlignType halign)
308{
309        HTMLRule *rule;
310
311        rule = g_new (HTMLRule, 1);
312        html_rule_init (rule, &html_rule_class, length, percent,
313                        size, shade, halign);
314
315        return HTML_OBJECT (rule);
316}
317
318
319
320
321
322
323
324
325
326
327
Note: See TracBrowser for help on using the repository browser.