source: trunk/third/gtkhtml3/src/htmlrule.c @ 21460

Revision 21460, 7.5 KB checked in by ghudson, 20 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r21459, 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#include "htmlsettings.h"
31#include "gtkhtml.h"
32
33
34HTMLRuleClass html_rule_class;
35static HTMLObjectClass *parent_class = NULL;
36
37
38/* HTMLObject methods.  */
39
40#define HTML_RULE_MIN_SIZE 12
41
42static void
43copy (HTMLObject *self,
44      HTMLObject *dest)
45{
46        (* HTML_OBJECT_CLASS (parent_class)->copy) (self, dest);
47
48        HTML_RULE (dest)->length = HTML_RULE (self)->length;
49        HTML_RULE (dest)->size   = HTML_RULE (self)->size;
50        HTML_RULE (dest)->shade  = HTML_RULE (self)->shade;
51        HTML_RULE (dest)->halign = HTML_RULE (self)->halign;
52}
53
54static void
55set_max_width (HTMLObject *o, HTMLPainter *painter, gint max_width)
56{
57        o->max_width = max_width;
58}
59
60static gint
61calc_min_width (HTMLObject *o,
62                HTMLPainter *painter)
63{
64        gint pixel_size;
65
66        pixel_size = html_painter_get_pixel_size (painter);
67
68        if (HTML_RULE (o)->length > 0)
69                return HTML_RULE (o)->length * pixel_size;
70
71        return pixel_size;
72}
73
74static HTMLFitType
75fit_line (HTMLObject *o,
76          HTMLPainter *painter,
77          gboolean start_of_line,
78          gboolean first_run,
79          gboolean next_to_floating,
80          gint width_left)
81{
82        if (!start_of_line)
83                return HTML_FIT_NONE;
84        o->width = MIN (width_left, o->max_width);
85
86        if (o->percent == 0) {
87                gint pixel_size = html_painter_get_pixel_size (painter);
88                if (HTML_RULE (o)->length * pixel_size > width_left) {
89                        o->width = HTML_RULE (o)->length * pixel_size;
90                }
91        }
92
93        return (next_to_floating && width_left <= 0) ? HTML_FIT_NONE : HTML_FIT_COMPLETE;
94}
95
96static gboolean
97html_rule_real_calc_size (HTMLObject *self, HTMLPainter *painter, GList **changed_objs)
98{
99        HTMLRule *rule;
100        gint ascent, descent;
101        gint pixel_size;
102        gint height;
103        gboolean changed;
104
105        rule = HTML_RULE (self);
106        pixel_size = html_painter_get_pixel_size (painter);
107
108        if (rule->size >= HTML_RULE_MIN_SIZE) {
109                height = rule->size;
110        } else {
111                height = HTML_RULE_MIN_SIZE;
112        }
113
114        ascent = (height / 2 + height % 2 + 1) * pixel_size;
115        descent = (height / 2 + 1) * pixel_size;
116
117        changed = FALSE;
118
119        if (self->width > self->max_width) {
120                self->width = self->max_width;
121                changed = TRUE;
122        }
123
124        if (ascent != self->ascent) {
125                self->ascent = ascent;
126                changed = TRUE;
127        }
128
129        if (descent != self->descent) {
130                self->descent = descent;
131                changed = TRUE;
132        }
133
134        return changed;
135}
136
137static void
138html_rule_draw (HTMLObject *o,
139      HTMLPainter *p,
140      gint x, gint y,
141      gint width, gint height,
142      gint tx, gint ty)
143{
144        HTMLRule *rule;
145        guint w, h;
146        gint xp, yp;
147        gint pixel_size = html_painter_get_pixel_size (p);
148        HTMLEngine *e;
149
150        if (p->widget && GTK_IS_HTML (p->widget))
151                e = GTK_HTML (p->widget)->engine;
152        else
153                return;
154
155        rule = HTML_RULE (o);
156       
157        if (y + height < o->y - o->ascent || y > o->y + o->descent)
158                return;
159
160        h = rule->size * pixel_size;
161        xp = o->x + tx;
162        yp = o->y + ty - (rule->size / 2 + rule->size % 2) * pixel_size;
163
164        if (o->percent == 0)
165                w = o->width;
166        else
167                /* The cast to `gdouble' is to avoid overflow (eg. when
168                   printing).  */
169                w = ((gdouble) o->width * o->percent) / 100;
170
171        switch (rule->halign) {
172        case HTML_HALIGN_LEFT:
173                break;
174        case HTML_HALIGN_CENTER:
175        case HTML_HALIGN_NONE:
176                /* Default is `align=center' according to the specs.  */
177                xp += (o->width - w) / 2;
178                break;
179        case HTML_HALIGN_RIGHT:
180                xp += o->width - w;
181                break;
182        default:
183                g_warning ("Unknown HTMLRule alignment %d.", rule->halign);
184        }
185
186        if (rule->shade)
187                html_painter_draw_panel (p,
188                                         &((html_colorset_get_color (e->settings->color_set, HTMLBgColor))->color),
189                                         xp, yp, w, h, GTK_HTML_ETCH_IN, 1);
190        else {
191                html_painter_set_pen (p, &html_colorset_get_color_allocated (e->settings->color_set, p,
192                                                                             HTMLTextColor)->color);
193                html_painter_fill_rect (p, xp, yp, w, h);
194        }
195}
196
197static gboolean
198accepts_cursor (HTMLObject *self)
199{
200        return TRUE;
201}
202
203static gboolean
204save (HTMLObject *self,
205      HTMLEngineSaveState *state)
206{
207        gchar *size, *shade, *length;
208        gboolean rv;
209
210        size   = HTML_RULE (self)->size == 2 ? "" : g_strdup_printf (" SIZE=\"%d\"", HTML_RULE (self)->size);
211        shade  = HTML_RULE (self)->shade ? "" : " NOSHADE";
212        length = HTML_RULE (self)->length
213                ? g_strdup_printf (" LENGTH=\"%d\"", HTML_RULE (self)->length)
214                : (self->percent > 0 && self->percent != 100 ? g_strdup_printf (" LENGTH=\"%d%%\"", self->percent) : "");
215
216        rv = html_engine_save_output_string (state, "\n<HR%s%s%s>\n", shade, size, length);
217
218        if (*size)
219                g_free (size);
220        if (*length)
221                g_free (length);
222
223        return rv;
224}
225
226static gboolean
227save_plain (HTMLObject *self,
228            HTMLEngineSaveState *state,
229            gint requested_width)
230{
231        int i;
232       
233        if (!html_engine_save_output_string (state, "\n"))
234                return FALSE;
235
236        /* Fixme no alignment or percent */
237        for (i = 0; i < requested_width; i++)
238                if (!html_engine_save_output_string (state, "_"))
239                        return FALSE;
240       
241        if (!html_engine_save_output_string (state, "\n"))
242                return FALSE;
243
244        return TRUE;
245}
246
247void
248html_rule_type_init (void)
249{
250        html_rule_class_init (&html_rule_class, HTML_TYPE_RULE, sizeof (HTMLRule));
251}
252
253void
254html_rule_class_init (HTMLRuleClass *klass,
255                      HTMLType type,
256                      guint object_size)
257{
258        HTMLObjectClass *object_class;
259
260        object_class = HTML_OBJECT_CLASS (klass);
261
262        html_object_class_init (object_class, type, object_size);
263
264        object_class->copy = copy;
265        object_class->draw = html_rule_draw;
266        object_class->set_max_width = set_max_width;
267        object_class->calc_min_width = calc_min_width;
268        object_class->fit_line = fit_line;
269        object_class->calc_size = html_rule_real_calc_size;
270        object_class->accepts_cursor = accepts_cursor;
271        object_class->save = save;
272        object_class->save_plain = save_plain;
273
274        parent_class = &html_object_class;
275}
276
277void
278html_rule_init (HTMLRule *rule,
279                HTMLRuleClass *klass,
280                gint length,
281                gint percent,
282                gint size,
283                gboolean shade,
284                HTMLHAlignType halign)
285{
286        HTMLObject *object;
287
288        object = HTML_OBJECT (rule);
289
290        html_object_init (object, HTML_OBJECT_CLASS (klass));
291
292        if (size < 1)
293                size = 1;       /* FIXME why? */
294        rule->size = size;
295
296        object->percent = percent;
297
298        rule->length = length;
299        rule->shade  = shade;
300        rule->halign = halign;
301
302        if (percent > 0) {
303                object->flags &= ~ HTML_OBJECT_FLAG_FIXEDWIDTH;
304                rule->length = 0;
305        } else if (rule->length > 0) {
306                object->flags |= HTML_OBJECT_FLAG_FIXEDWIDTH;
307        } else {
308                object->flags &= ~ HTML_OBJECT_FLAG_FIXEDWIDTH;
309        }
310}
311
312HTMLObject *
313html_rule_new (gint length,
314               gint percent,
315               gint size,
316               gboolean shade,
317               HTMLHAlignType halign)
318{
319        HTMLRule *rule;
320
321        rule = g_new (HTMLRule, 1);
322        html_rule_init (rule, &html_rule_class, length, percent,
323                        size, shade, halign);
324
325        return HTML_OBJECT (rule);
326}
327
328
329
330
331
332
333
334
335
336
337
Note: See TracBrowser for help on using the repository browser.