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 Helix Code, Inc. |
---|
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., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | #include <gtk/gtkwidget.h> |
---|
24 | #include "htmlcolor.h" |
---|
25 | #include "htmlcolorset.h" |
---|
26 | #include "htmlpainter.h" |
---|
27 | |
---|
28 | |
---|
29 | |
---|
30 | HTMLColorSet * |
---|
31 | html_colorset_new (GtkWidget *w) |
---|
32 | { |
---|
33 | HTMLColorSet *s; |
---|
34 | |
---|
35 | s = g_new0 (HTMLColorSet, 1); |
---|
36 | |
---|
37 | /* these are default color settings */ |
---|
38 | |
---|
39 | if (w && gtk_widget_get_style (w)) { |
---|
40 | html_colorset_set_style (s, w); |
---|
41 | } else { |
---|
42 | s->color [HTMLLinkColor] = html_color_new_from_rgb (0, 0, 0xffff); |
---|
43 | s->color [HTMLALinkColor] = html_color_new_from_rgb (0, 0, 0xffff); |
---|
44 | s->color [HTMLVLinkColor] = html_color_new_from_rgb (0, 0, 0xffff); |
---|
45 | s->color [HTMLSpellErrorColor] = html_color_new_from_rgb (0xffff, 0, 0); |
---|
46 | s->color [HTMLBgColor] = html_color_new_from_rgb (0xffff, 0xffff, 0xffff); |
---|
47 | s->color [HTMLHighlightColor] = html_color_new_from_rgb (0x7fff, 0x7fff, 0xffff); |
---|
48 | s->color [HTMLHighlightTextColor] = html_color_new (); |
---|
49 | s->color [HTMLHighlightNFColor] = html_color_new (); |
---|
50 | s->color [HTMLHighlightTextNFColor] = html_color_new (); |
---|
51 | s->color [HTMLTextColor] = html_color_new (); |
---|
52 | s->color [HTMLCiteColor] = html_color_new (); |
---|
53 | } |
---|
54 | |
---|
55 | return s; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | void |
---|
60 | html_colorset_destroy (HTMLColorSet *set) |
---|
61 | { |
---|
62 | int i; |
---|
63 | |
---|
64 | g_return_if_fail (set != NULL); |
---|
65 | |
---|
66 | for (i = 0; i < HTMLColors; i++) { |
---|
67 | if (set->color[i] != NULL) |
---|
68 | html_color_unref (set->color[i]); |
---|
69 | } |
---|
70 | |
---|
71 | if (set->slaves) |
---|
72 | g_slist_free (set->slaves); |
---|
73 | |
---|
74 | g_free (set); |
---|
75 | } |
---|
76 | |
---|
77 | void |
---|
78 | html_colorset_add_slave (HTMLColorSet *set, HTMLColorSet *slave) |
---|
79 | { |
---|
80 | set->slaves = g_slist_prepend (set->slaves, slave); |
---|
81 | } |
---|
82 | |
---|
83 | void |
---|
84 | html_colorset_set_color (HTMLColorSet *s, GdkColor *color, HTMLColorId idx) |
---|
85 | { |
---|
86 | GSList *cur; |
---|
87 | HTMLColorSet *cs; |
---|
88 | |
---|
89 | html_color_set (s->color [idx], color); |
---|
90 | s->changed [idx] = TRUE; |
---|
91 | |
---|
92 | /* forward change to slaves */ |
---|
93 | cur = s->slaves; |
---|
94 | while (cur) { |
---|
95 | cs = (HTMLColorSet *) cur->data; |
---|
96 | html_colorset_set_color (cs, color, idx); |
---|
97 | cur = cur->next; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | HTMLColor * |
---|
102 | html_colorset_get_color (HTMLColorSet *s, HTMLColorId idx) |
---|
103 | { |
---|
104 | return s->color [idx]; |
---|
105 | } |
---|
106 | |
---|
107 | HTMLColor * |
---|
108 | html_colorset_get_color_allocated (HTMLColorSet *s, HTMLPainter *painter, HTMLColorId idx) |
---|
109 | { |
---|
110 | html_color_alloc (s->color [idx], painter); |
---|
111 | return s->color [idx]; |
---|
112 | } |
---|
113 | |
---|
114 | void |
---|
115 | html_colorset_set_by (HTMLColorSet *s, HTMLColorSet *o) |
---|
116 | { |
---|
117 | HTMLColorId i; |
---|
118 | |
---|
119 | for (i=0; i < HTMLColors; i++) { |
---|
120 | html_colorset_set_color (s, &o->color [i]->color, i); |
---|
121 | /* unset the changed flag */ |
---|
122 | s->changed [i] = FALSE; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | void |
---|
127 | html_colorset_set_unchanged (HTMLColorSet *s, HTMLColorSet *o) |
---|
128 | { |
---|
129 | HTMLColorId i; |
---|
130 | |
---|
131 | for (i=0; i < HTMLColors; i++) { |
---|
132 | if (!s->changed[i]) { |
---|
133 | html_colorset_set_color (s, &o->color [i]->color, i); |
---|
134 | s->changed [i] = FALSE; |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | static GdkColor * |
---|
140 | get_prop_color (GtkWidget *w, char *name, char *dv, GdkColor *gdk_color) |
---|
141 | { |
---|
142 | GdkColor c; |
---|
143 | GdkColor *color = NULL; |
---|
144 | |
---|
145 | gtk_widget_style_get (w, name, &color, NULL); |
---|
146 | |
---|
147 | if (color) |
---|
148 | return color; |
---|
149 | |
---|
150 | if (dv && gdk_color_parse (dv, &c)) |
---|
151 | return gdk_color_copy (&c); |
---|
152 | |
---|
153 | if (gdk_color) |
---|
154 | return gdk_color_copy (gdk_color); |
---|
155 | |
---|
156 | g_warning ("falling back to text color"); |
---|
157 | return (gdk_color_copy (&w->style->text [GTK_STATE_NORMAL])); |
---|
158 | } |
---|
159 | |
---|
160 | #define SET_GCOLOR(t,c) \ |
---|
161 | if (!s->changed [HTML ## t ## Color]) { \ |
---|
162 | if (s->color [HTML ## t ## Color]) html_color_unref (s->color [HTML ## t ## Color]); \ |
---|
163 | s->color [HTML ## t ## Color] = html_color_new_from_gdk_color (&c); \ |
---|
164 | } |
---|
165 | |
---|
166 | void |
---|
167 | html_colorset_set_style (HTMLColorSet *s, GtkWidget *w) |
---|
168 | { |
---|
169 | GdkColor *color = NULL; |
---|
170 | GtkStyle *style = gtk_widget_get_style (w); |
---|
171 | |
---|
172 | SET_GCOLOR (Bg, style->base [GTK_STATE_NORMAL]); |
---|
173 | SET_GCOLOR (Text, style->text [GTK_STATE_NORMAL]); |
---|
174 | SET_GCOLOR (Highlight, style->base [GTK_STATE_SELECTED]); |
---|
175 | SET_GCOLOR (HighlightText, style->text [GTK_STATE_SELECTED]); |
---|
176 | SET_GCOLOR (HighlightNF, style->base [GTK_STATE_ACTIVE]); |
---|
177 | SET_GCOLOR (HighlightTextNF, style->text [GTK_STATE_ACTIVE]); |
---|
178 | color = get_prop_color (w, "link_color", "#0000ff", NULL); |
---|
179 | SET_GCOLOR (Link, *color); |
---|
180 | gdk_color_free (color); |
---|
181 | color = get_prop_color (w, "alink_color", "#0000ff", NULL); |
---|
182 | SET_GCOLOR (ALink, *color); |
---|
183 | gdk_color_free (color); |
---|
184 | color = get_prop_color (w, "vlink_color", "#0000ff", NULL); |
---|
185 | SET_GCOLOR (VLink, *color); |
---|
186 | gdk_color_free (color); |
---|
187 | color = get_prop_color (w, "spell_error_color", "#ff0000", NULL); |
---|
188 | SET_GCOLOR (SpellError, *color); |
---|
189 | gdk_color_free (color); |
---|
190 | color = get_prop_color (w, "cite_color", NULL, &style->text [GTK_STATE_NORMAL]); |
---|
191 | SET_GCOLOR (Cite, *color); |
---|
192 | gdk_color_free (color); |
---|
193 | } |
---|