1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
Copyright (C) 2000,2001 CodeFactory AB
Copyright (C) 2000,2001 Jonas Borgstrm <jonas@codefactory.se>
Copyright (C) 2000,2001 Anders Carlsson <andersca@codefactory.se>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <string.h>
#include "htmlboxtext.h"
#include "htmlboxlistitem.h"
static HtmlBoxClass *parent_class = NULL;
static char *
convert_to_roman (long decimal)
{
char *roman[] = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
static char *result=NULL;
if (!result)
result = (char *) g_malloc(50);
memset (result, 0, 50);
if (decimal >= 4900 || decimal < 1)
{
printf ("Decimal value exceeds 4900 or less than 1\n");
g_strlcat (result, "\0", sizeof(result));
return result;
}
while (decimal >= 1000)
{
decimal -= 1000;
g_strlcat (result, "M", sizeof(result));
}
if (decimal >= 900)
{
decimal -= 900;
g_strlcat (result, "CM", sizeof(result));
}
if (decimal >= 500)
{
decimal -= 500;
g_strlcat (result, "D", sizeof(result));
}
if (decimal >= 400)
{
decimal -= 400;
g_strlcat (result, "CD", sizeof(result));
}
while (decimal >= 100)
{
decimal -= 100;
g_strlcat (result, "C", sizeof(result));
}
if (decimal >= 90)
{
decimal -= 90;
g_strlcat (result , "XC", sizeof(result));
}
if (decimal >= 50)
{
decimal -= 50;
g_strlcat (result, "L", sizeof(result));
}
if (decimal >= 40)
{
decimal -= 40;
g_strlcat (result, "XL", sizeof(result));
}
while (decimal >= 10)
{
decimal -= 10;
g_strlcat (result, "X", sizeof(result));
}
if (decimal > 0 && decimal < 10)
g_strlcat (result, *(roman + decimal - 1), sizeof(result));
return result;
}
static gint
html_box_list_item_left_mbp_sum (HtmlBox *self, gint width)
{
HtmlStyle *style = HTML_BOX_GET_STYLE (self);
if (style->inherited->direction != HTML_DIRECTION_LTR ||
style->surround->margin.left.value != 0)
return parent_class->left_mbp_sum (self, width);
else
return parent_class->left_mbp_sum (self, width) +
2 * style->inherited->font_spec->size;
}
static gint
html_box_list_item_right_mbp_sum (HtmlBox *self, gint width)
{
HtmlStyle *style = HTML_BOX_GET_STYLE (self);
if (style->inherited->direction != HTML_DIRECTION_RTL ||
style->surround->margin.right.value != 0)
return parent_class->right_mbp_sum (self, width);
else
return parent_class->right_mbp_sum (self, width) +
2 * style->inherited->font_spec->size;
}
static void
html_box_list_item_init_counter (HtmlBox *self, HtmlRelayout *relayout)
{
HtmlStyle *style = HTML_BOX_GET_STYLE (self);
HtmlBoxListItem *item = HTML_BOX_LIST_ITEM (self);
HtmlBox *box = self->prev;
char lower_alpha='a', upper_alpha='A';
char *roman_str;
if (item->counter != 0)
return;
while (box && !HTML_IS_BOX_LIST_ITEM (box)) {
box = box->prev;
}
if (box)
item->counter = HTML_BOX_LIST_ITEM(box)->counter + 1;
else
item->counter = 1;
/* FIXME: add more types here */
switch (style->inherited->list_style_type) {
case HTML_LIST_STYLE_TYPE_DECIMAL:
item->str = g_strdup_printf ("%d. ", item->counter);
break;
case HTML_LIST_STYLE_TYPE_DECIMAL_LEADING_ZERO:
item->str = g_strdup_printf ("%02d. ", item->counter);
break;
case HTML_LIST_STYLE_TYPE_LOWER_ALPHA:
item->str = g_strdup_printf ("%c. ", (lower_alpha + item->counter - 1));
break;
case HTML_LIST_STYLE_TYPE_UPPER_ALPHA:
item->str = g_strdup_printf ("%c. ", (upper_alpha + item->counter - 1));
break;
case HTML_LIST_STYLE_TYPE_LOWER_ROMAN:
roman_str = convert_to_roman (item->counter);
item->str = g_strdup_printf ("%s. ", g_ascii_strdown (roman_str, strlen(roman_str)));
break;
case HTML_LIST_STYLE_TYPE_UPPER_ROMAN:
item->str = g_strdup_printf ("%s. ", convert_to_roman (item->counter));
break;
default:
break;
}
if (item->str) {
item->label = html_box_text_new (TRUE);
html_box_text_set_text (HTML_BOX_TEXT (item->label), item->str);
html_box_set_style (item->label, style);
item->label->parent = self;
html_box_relayout (item->label, relayout);
}
}
static void
html_box_list_item_relayout (HtmlBox *self, HtmlRelayout *relayout)
{
parent_class->relayout (self, relayout);
html_box_list_item_init_counter (self, relayout);
}
static void
html_box_list_item_paint (HtmlBox *self, HtmlPainter *painter, GdkRectangle *area, gint tx, gint ty)
{
HtmlStyle *style = HTML_BOX_GET_STYLE (self);
HtmlBoxListItem *item = HTML_BOX_LIST_ITEM(self);
gint offset = 0;
parent_class->paint (self, painter, area, tx, ty);
if (item->label) {
switch (HTML_BOX_GET_STYLE (self)->inherited->direction) {
case HTML_DIRECTION_LTR:
offset = (style->surround->margin.left.value ?
style->surround->margin.left.value :
2 * style->inherited->font_spec->size) - item->label->width;
break;
case HTML_DIRECTION_RTL:
offset = self->width -
(style->surround->margin.right.value ?
style->surround->margin.right.value :
2 * style->inherited->font_spec->size) + item->label->width;
break;
};
html_box_paint (item->label, painter, area, tx + self->x + offset, ty + self->y);
}
else {
gint square_size = style->inherited->font_spec->size / 3;
switch (style->inherited->direction) {
case HTML_DIRECTION_LTR:
offset = (style->surround->margin.left.value ?
style->surround->margin.left.value :
2 * style->inherited->font_spec->size) -
style->inherited->font_spec->size / 2 - square_size;
break;
case HTML_DIRECTION_RTL:
offset = self->width -
(style->surround->margin.right.value ?
style->surround->margin.right.value :
2 * style->inherited->font_spec->size) + style->inherited->font_spec->size / 2;
break;
};
html_painter_set_foreground_color (painter, style->inherited->color);
if (style->inherited->list_style_type == HTML_LIST_STYLE_TYPE_DISC)
html_painter_draw_arc (painter, area, tx + self->x + offset + 1, ty + self->y + square_size + 1, square_size + 1, square_size + 1, 0, 64 * 360, TRUE);
else if (style->inherited->list_style_type == HTML_LIST_STYLE_TYPE_CIRCLE)
html_painter_draw_arc (painter, area, tx + self->x + offset + 1, ty + self->y + square_size + 1, square_size + 1, square_size + 1, 0, 64 * 360, FALSE);
else
html_painter_fill_rectangle (painter, area, tx + self->x + offset + 2, ty + self->y + square_size + 1, square_size, square_size);
}
}
static void
html_box_list_item_finalize (GObject *object)
{
HtmlBoxListItem *item = HTML_BOX_LIST_ITEM (object);
if (item->str)
g_free (item->str);
if (item->label)
g_object_unref (G_OBJECT(item->label));
G_OBJECT_CLASS(parent_class)->finalize (object);
}
static void
html_box_list_item_class_init (HtmlBoxListItemClass *klass)
{
HtmlBoxClass *box_class = (HtmlBoxClass *) klass;
GObjectClass *object_class = (GObjectClass *) klass;
box_class->paint = html_box_list_item_paint;
box_class->relayout = html_box_list_item_relayout;
box_class->left_mbp_sum = html_box_list_item_left_mbp_sum;
box_class->right_mbp_sum = html_box_list_item_right_mbp_sum;
object_class->finalize = html_box_list_item_finalize;
parent_class = g_type_class_peek_parent (klass);
}
static void
html_box_list_item_init (HtmlBoxListItem *box)
{
}
GType
html_box_list_item_get_type (void)
{
static GType html_type = 0;
if (!html_type) {
static GTypeInfo type_info = {
sizeof (HtmlBoxListItemClass),
NULL,
NULL,
(GClassInitFunc) html_box_list_item_class_init,
NULL,
NULL,
sizeof (HtmlBoxListItem),
16,
(GInstanceInitFunc) html_box_list_item_init
};
html_type = g_type_register_static (HTML_TYPE_BOX_BLOCK, "HtmlBoxListItem", &type_info, 0);
}
return html_type;
}
HtmlBox *
html_box_list_item_new (void)
{
return g_object_new (HTML_TYPE_BOX_LIST_ITEM, NULL);
}
|