| 12
 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
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 
 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
   Copyright (C) 2000 CodeFactory AB
   Copyright (C) 2000 Jonas Borgstr\366m <jonas@codefactory.se>
   Copyright (C) 2000 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 <glib.h>
#include <string.h>
#include "css/cssvalue.h"
#include "view/htmlview.h"
#include "layout/htmlbox.h"
#include "layout/htmlstyle.h"
#include "util/htmlglobalatoms.h"
static HtmlStyle *default_style = NULL;
static GtkStyle *
html_style_get_gtk_style (void)
{
  	GtkStyle *style;
	style = gtk_rc_get_style_by_paths (gtk_settings_get_default(),
                                           "GtkTextView",
                                           "TextView",
                                           HTML_TYPE_VIEW);
	if (!style) {
		style = gtk_rc_get_style_by_paths (gtk_settings_get_default(),
                                                   "GtkHtml",
                                                   "HtmlView",
                                                   GTK_TYPE_TEXT_VIEW);
	}
	if (!style) {
		style = gtk_style_new ();
	} else {
		g_object_ref (style);
        }
        return style;
}
static PangoFontDescription*
html_style_get_gtk_font_desc (GtkStyle *style)
{
	return style->font_desc;
}
static HtmlColor*
html_style_get_gtk_text_color (GtkStyle *style)
{
	GdkColor  text_color;
	GdkColor  base_color;
	gushort linkred;
	gushort linkgreen;
	guint red_green;
	base_color = style->base[GTK_STATE_NORMAL];
	/*
	 * Heuristic to pick more visible link color
	 */
	red_green = base_color.red + base_color.green;
	if (red_green < 65535) {
		linkred = 153;
		linkgreen = 204;
	} else {
		linkred = 0;
		linkgreen = 0;
	}
	html_color_set_linkblue (linkred, linkgreen);
	text_color = style->text[GTK_STATE_NORMAL];
	return html_color_new_from_rgb (text_color.red,
					text_color.green,
					text_color.blue);
}
void
html_length_set_value (HtmlLength *length, gint value, HtmlLengthType type)
{
	length->type = type;
	length->value = value;
}
gint
html_length_get_value (const HtmlLength *length, gint base)
{
	switch (length->type) {
	case HTML_LENGTH_FIXED:
		return length->value;
	case HTML_LENGTH_PERCENT:
		return (length->value * base) / 100;
	default:
	case HTML_LENGTH_AUTO:
		return 0;
	}
}
gboolean
html_length_from_css_value (HtmlFontSpecification *font_spec, CssValue *val, HtmlLength *length)
{
	/* FIXME: Should probably be xdpi and ydpi */
	static gdouble dpi = 0.0;
	if (dpi == 0.0)
		dpi = (((double) gdk_screen_width () * 25.4) / 
		       ((double) gdk_screen_width_mm ()));
	
	if (val->v.atom == HTML_ATOM_AUTO) {
		length->type = HTML_LENGTH_AUTO;
	}
	/* FIXME: This is not the correct way way to do this */
	else if (font_spec && 
		 (val->value_type == CSS_EMS ||
		  val->value_type == CSS_EXS)) {
		length->value = (gint) (val->v.d * font_spec->size);
		length->type = HTML_LENGTH_FIXED;
	}
	else if (val->value_type == CSS_PX ||
		 val->value_type == CSS_NUMBER) {
		length->value = (gint) val->v.d;
		length->type = HTML_LENGTH_FIXED;
	}
	else if (val->value_type == CSS_PERCENTAGE) {
		length->value = (gint) val->v.d;
		length->type = HTML_LENGTH_PERCENT;
	}
	else if (val->value_type == CSS_PT) {
		length->value = (gint) (val->v.d * dpi / 72);
		length->type = HTML_LENGTH_FIXED;
	}
	else if (val->value_type == CSS_PC) {
		length->value = (gint) (val->v.d * dpi * 12 / 72);
		length->type = HTML_LENGTH_FIXED;
	}
	else if (val->value_type == CSS_IN) {
		length->value = (gint) (val->v.d * dpi);
		length->type = HTML_LENGTH_FIXED;
	}
	else if (val->value_type == CSS_CM) {
		length->value = (gint) (val->v.d * dpi / 2.54);
		length->type = HTML_LENGTH_FIXED;
	}
	else if (val->value_type == CSS_MM) {
		length->value = (gint) (val->v.d * dpi / 25.4);
		length->type = HTML_LENGTH_FIXED;
	}
	else
		return FALSE;
	return TRUE;
}
gboolean
html_length_equals (const HtmlLength *length1, const HtmlLength *length2)
{
	if (length1->type == length2->type) {
		if (length1->type == HTML_LENGTH_AUTO || length1->value == length2->value)
			return TRUE;
	}
	return FALSE;
}
void
html_length_set (HtmlLength *length, const HtmlLength *length2)
{
	length->type = length2->type;
	length->value = length2->value;
}
HtmlStyle *
html_style_ref (HtmlStyle *style)
{
	style->refcount++;
	return style;
}
void
html_style_unref (HtmlStyle *style)
{
	if (style == NULL)
		return;
	style->refcount--;
	if (style->refcount <= 0) {
		/* FIXME: We do nothing here until I figure out the default style case /ac */
		html_style_box_unref (style->box);
		html_style_surround_unref (style->surround);
		html_style_inherited_unref (style->inherited);
		html_style_background_unref (style->background);
		html_style_outline_unref (style->outline);
		html_style_border_unref (style->border);
		/* FIXME, this should have a refcount */
		g_free (style->visual);
		if (style->content)
			g_free (style->content);
		g_free (style);
	}
}
HtmlStyle *
html_default_style_new (void)
{
	HtmlStyle *result = g_new0 (HtmlStyle, 1);
	HtmlStyleBox *box = html_style_box_new ();
	HtmlStyleSurround *surround = html_style_surround_new ();
	HtmlStyleInherited *inherited = html_style_inherited_new ();
	HtmlStyleBackground *background = html_style_background_new ();
	HtmlStyleBorder *border = html_style_border_new ();
	HtmlStyleOutline *outline = html_style_outline_new ();
	GtkStyle *gtk_style;
	gchar *font_name;
	GtkSettings *settings;
	PangoFontDescription *font_desc;
	const gchar *family;
	gfloat size;
	HtmlColor *color;
	/* Set refcount to 1 so it never will be deallocated */
	result->refcount = 1;
	
	html_style_set_style_box (result, box);
	html_style_set_style_inherited (result, inherited);
	html_style_set_style_surround (result, surround);
	html_style_set_style_background (result, background);
	html_style_set_style_border (result, border);
	html_style_set_style_outline (result, outline);
	html_style_set_border_top_width (result, HTML_BORDER_WIDTH_MEDIUM);
	html_style_set_border_bottom_width (result, HTML_BORDER_WIDTH_MEDIUM);
	html_style_set_border_left_width (result, HTML_BORDER_WIDTH_MEDIUM);
	html_style_set_border_right_width (result, HTML_BORDER_WIDTH_MEDIUM);
	
	html_style_set_outline_width (result, HTML_BORDER_WIDTH_MEDIUM);
	gtk_style = html_style_get_gtk_style ();
	font_desc = html_style_get_gtk_font_desc (gtk_style);
        
	family = pango_font_description_get_family (font_desc);
	size = pango_font_description_get_size (font_desc) / (gfloat)PANGO_SCALE;
        
	inherited->font_spec = 
                html_font_specification_new ((gchar *) family, 
                                             HTML_FONT_STYLE_NORMAL, 
                                             HTML_FONT_VARIANT_NORMAL, 
                                             HTML_FONT_WEIGHT_NORMAL, 
                                             HTML_FONT_STRETCH_NORMAL,
                                             HTML_FONT_DECORATION_NONE, 
                                             size);
	
	/*
	 * Set the default color to match the GTK+ theme  
	 * and free the HtmlColor
	 */
	color = html_style_get_gtk_text_color (gtk_style);
	html_style_set_color (result, color);
	html_color_unref (color);
	g_object_unref (gtk_style);
	
	return result;
}
static void
html_style_notify_settings (GObject *obj, GParamSpec *pspec)
{
	if (strcmp (pspec->name, "gtk-theme-name") == 0) {
		GtkStyle *gtk_style;
		HtmlColor *color;
		/*
		 * Update the default color
		 */
		gtk_style = html_style_get_gtk_style ();
		color =  html_style_get_gtk_text_color (gtk_style);
		g_object_unref (gtk_style);
		default_style->inherited->color->red =  color->red;
		default_style->inherited->color->green =  color->green;
		default_style->inherited->color->blue =  color->blue;
		g_free (color);
	}
}
                            
HtmlStyle *
html_style_new (HtmlStyle *parent)
{
	HtmlStyle *style = g_new0 (HtmlStyle, 1);
	if (!default_style) {
		default_style = html_default_style_new ();
		g_signal_connect (gtk_settings_get_default (),
				  "notify",
				  G_CALLBACK (html_style_notify_settings),
				  NULL);
	}
	/* FIXME: Eeeek, very ugly */
	
	/* FIXME: We should inherit these */
	style->visual = g_new0 (HtmlStyleVisual, 1);
	/*	style->box = g_new0 (HtmlStyleBox, 1);*/
	if (parent) {
		style->blink = parent->blink;
		html_style_set_style_inherited (style, parent->inherited);
	}
	else {
		html_style_set_style_inherited (style, default_style->inherited);
	}
	
	html_style_set_style_surround (style, default_style->surround);
	html_style_set_style_background (style, default_style->background);
	html_style_set_style_border (style, default_style->border);
	html_style_set_style_outline (style, default_style->outline);
	html_style_set_style_box (style, default_style->box);
	return style;
}
static void
html_debug_print_length (HtmlLength *length)
{
	if (length->type == HTML_LENGTH_AUTO)
		g_print ("auto");
	else
		g_print ("%d", length->value);
}
void
html_debug_print_style (HtmlStyle *style)
{
        g_print ("\n------------\n");
	g_print ("display: ");
	switch (style->display) {
	case HTML_DISPLAY_TABLE:
		g_print ("table;"); break;
	case HTML_DISPLAY_BLOCK:
		g_print ("block;"); break;
	case HTML_DISPLAY_INLINE:
		g_print ("inline;");break;
	case HTML_DISPLAY_NONE:
		g_print ("none;"); break;
	default:
		g_warning ("unhandled display property %d", style->display);
	}
	g_print ("\n");
	
	g_print ("visibility: ");
	switch (style->visibility) {
	case HTML_VISIBILITY_VISIBLE:
		g_print ("visible;");
		break;
	case HTML_VISIBILITY_HIDDEN:
		g_print ("hidden;");
		break;
	case HTML_VISIBILITY_COLLAPSE:
		g_print ("collapse;");
		break;
	}
	g_print ("\n");
	
	g_print ("width: ");
	html_debug_print_length (&style->box->width);
	g_print (";\n");
	g_print ("height: ");
	html_debug_print_length (&style->box->height);
	g_print (";\n");
	g_print ("max-width: ");
	html_debug_print_length (&style->box->max_width);
	g_print (";\n");
	g_print ("min-width: ");
	html_debug_print_length (&style->box->min_width);
	g_print (";\n");
	g_print ("max-height: ");
	html_debug_print_length (&style->box->max_height);
	g_print (";\n");
	g_print ("min-height: ");
	html_debug_print_length (&style->box->min_height);
	g_print (";\n");
}
#define COMPARE_RELAYOUT(x) if (s1->x != s2->x) return HTML_STYLE_CHANGE_RELAYOUT;
#define COMPARE_RELAYOUT_LENGTH(x) if (html_length_equals (&s1->x, &s2->x) == FALSE) return HTML_STYLE_CHANGE_RELAYOUT;
#define COMPARE_REPAINT_COLOR(x) if (html_color_equal (s1->x, s2->x) == FALSE) return HTML_STYLE_CHANGE_REPAINT;
#define COMPARE_REPAINT(x) if (s1->x != s2->x) return HTML_STYLE_CHANGE_REPAINT;
HtmlStyleChange
html_style_compare (const HtmlStyle *s1, const HtmlStyle *s2)
{
	/* RECREATE begin */
	if (s1->display != s1->display)
		return HTML_STYLE_CHANGE_RECREATE;
	/* RECREATE end */
	/* RELAYOUT begin */
	COMPARE_RELAYOUT (vertical_align);
	COMPARE_RELAYOUT (position);
	COMPARE_RELAYOUT (Float);
	COMPARE_RELAYOUT (overflow);
	COMPARE_RELAYOUT (text_transform);
	COMPARE_RELAYOUT (clear);
	COMPARE_RELAYOUT (unicode_bidi);
	COMPARE_RELAYOUT (table_layout);
	COMPARE_RELAYOUT (blink);
	COMPARE_RELAYOUT_LENGTH (box->width);
	COMPARE_RELAYOUT_LENGTH (box->min_width);
	COMPARE_RELAYOUT_LENGTH (box->max_width);
	COMPARE_RELAYOUT_LENGTH (box->height);
	COMPARE_RELAYOUT_LENGTH (box->min_height);
	COMPARE_RELAYOUT_LENGTH (box->max_height);
	COMPARE_RELAYOUT_LENGTH (visual->clip.top);
	COMPARE_RELAYOUT_LENGTH (visual->clip.left);
	COMPARE_RELAYOUT_LENGTH (visual->clip.right);
	COMPARE_RELAYOUT_LENGTH (visual->clip.bottom);
	COMPARE_RELAYOUT_LENGTH (surround->margin.top);
	COMPARE_RELAYOUT_LENGTH (surround->margin.left);
	COMPARE_RELAYOUT_LENGTH (surround->margin.right);
	COMPARE_RELAYOUT_LENGTH (surround->margin.bottom);
	COMPARE_RELAYOUT_LENGTH (surround->padding.top);
	COMPARE_RELAYOUT_LENGTH (surround->padding.left);
	COMPARE_RELAYOUT_LENGTH (surround->padding.right);
	COMPARE_RELAYOUT_LENGTH (surround->padding.bottom);
	COMPARE_RELAYOUT_LENGTH (surround->position.top);
	COMPARE_RELAYOUT_LENGTH (surround->position.left);
	COMPARE_RELAYOUT_LENGTH (surround->position.right);
	COMPARE_RELAYOUT_LENGTH (surround->position.bottom);
	COMPARE_RELAYOUT (border->top.width);
	COMPARE_RELAYOUT (border->left.width);
	COMPARE_RELAYOUT (border->right.width);
	COMPARE_RELAYOUT (border->bottom.width);
	COMPARE_RELAYOUT (border->top.border_style);
	COMPARE_RELAYOUT (border->left.border_style);
	COMPARE_RELAYOUT (border->right.border_style);
	COMPARE_RELAYOUT (border->bottom.border_style);
	COMPARE_RELAYOUT (inherited->line_height);
	COMPARE_RELAYOUT (inherited->word_spacing);
	COMPARE_RELAYOUT (inherited->letter_spacing);
	COMPARE_RELAYOUT (inherited->cursor);
	COMPARE_RELAYOUT (inherited->border_spacing_horiz);
	COMPARE_RELAYOUT (inherited->border_spacing_vert);
	COMPARE_RELAYOUT (inherited->direction);
	COMPARE_RELAYOUT (inherited->bidi_level);
	COMPARE_RELAYOUT (inherited->caption_side);
	COMPARE_RELAYOUT (inherited->white_space);
	COMPARE_RELAYOUT (inherited->list_style_type);
	COMPARE_RELAYOUT (inherited->font_spec->size);
	COMPARE_RELAYOUT (inherited->font_spec->weight);
	COMPARE_RELAYOUT (inherited->font_spec->style);
	COMPARE_RELAYOUT (inherited->font_spec->variant);
	COMPARE_RELAYOUT (inherited->font_spec->stretch);
	COMPARE_RELAYOUT_LENGTH (inherited->text_indent);
	if (strcmp (s1->inherited->font_spec->family, s2->inherited->font_spec->family) != 0)
		return HTML_STYLE_CHANGE_RELAYOUT;
#if 0
	if (html_font_description_equal (s1->inherited->font_spec, s2->inherited->font_spec) == FALSE)
		return HTML_STYLE_CHANGE_RELAYOUT;
#endif
	/* RELAYOUT end */
	/* REPAINT begin */
	if (html_color_equal (&s1->background->color, &s2->background->color) == FALSE)
		return HTML_STYLE_CHANGE_REPAINT;
#if 0
	COMPARE_REPAINT (background->image);
#endif
	COMPARE_REPAINT (background->repeat);
	COMPARE_REPAINT_COLOR (inherited->color);
	COMPARE_REPAINT_COLOR (border->top.color);
	COMPARE_REPAINT_COLOR (border->left.color);
	COMPARE_REPAINT_COLOR (border->right.color);
	COMPARE_REPAINT_COLOR (border->bottom.color);
	COMPARE_REPAINT (inherited->font_spec->decoration);
	COMPARE_REPAINT (outline->style);
	COMPARE_REPAINT (outline->width);
	COMPARE_REPAINT_COLOR (outline->color);
	/* REPAINT end */
	return HTML_STYLE_CHANGE_NONE;
}
 |