No issues found
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU Lesser General Public
4 * License as published by the Free Software Foundation; either
5 * version 2 of the License, or (at your option) version 3.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Lesser General Public License for more details.
11 *
12 * You should have received a copy of the GNU Lesser General Public
13 * License along with the program; if not, see <http://www.gnu.org/licenses/>
14 *
15 *
16 * Authors:
17 * Not Zed <notzed@lostzed.mmc.com.au>
18 * Jeffrey Stedfast <fejj@ximian.com>
19 *
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
21 *
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <gtk/gtk.h>
29
30 #include "e-filter-color.h"
31
32 G_DEFINE_TYPE (
33 EFilterColor,
34 e_filter_color,
35 E_TYPE_FILTER_ELEMENT)
36
37 static void
38 set_color (GtkColorButton *color_button,
39 EFilterColor *fc)
40 {
41 gtk_color_button_get_color (color_button, &fc->color);
42 }
43
44 static gint
45 filter_color_eq (EFilterElement *element_a,
46 EFilterElement *element_b)
47 {
48 EFilterColor *color_a = E_FILTER_COLOR (element_a);
49 EFilterColor *color_b = E_FILTER_COLOR (element_b);
50
51 return E_FILTER_ELEMENT_CLASS (e_filter_color_parent_class)->
52 eq (element_a, element_b) &&
53 gdk_color_equal (&color_a->color, &color_b->color);
54 }
55
56 static xmlNodePtr
57 filter_color_xml_encode (EFilterElement *element)
58 {
59 EFilterColor *fc = E_FILTER_COLOR (element);
60 xmlNodePtr value;
61 gchar spec[16];
62
63 g_snprintf (
64 spec, sizeof (spec), "#%04x%04x%04x",
65 fc->color.red, fc->color.green, fc->color.blue);
66
67 value = xmlNewNode (NULL, (xmlChar *)"value");
68 xmlSetProp (value, (xmlChar *)"type", (xmlChar *)"colour");
69 xmlSetProp (value, (xmlChar *)"name", (xmlChar *) element->name);
70 xmlSetProp (value, (xmlChar *)"spec", (xmlChar *) spec);
71
72 return value;
73 }
74
75 static gint
76 filter_color_xml_decode (EFilterElement *element,
77 xmlNodePtr node)
78 {
79 EFilterColor *fc = E_FILTER_COLOR (element);
80 xmlChar *prop;
81
82 xmlFree (element->name);
83 element->name = (gchar *) xmlGetProp (node, (xmlChar *)"name");
84
85 prop = xmlGetProp (node, (xmlChar *)"spec");
86 if (prop != NULL) {
87 gdk_color_parse ((gchar *) prop, &fc->color);
88 xmlFree (prop);
89 } else {
90 /* try reading the old RGB properties */
91 prop = xmlGetProp (node, (xmlChar *)"red");
92 sscanf ((gchar *) prop, "%" G_GINT16_MODIFIER "x", &fc->color.red);
93 xmlFree (prop);
94 prop = xmlGetProp (node, (xmlChar *)"green");
95 sscanf ((gchar *) prop, "%" G_GINT16_MODIFIER "x", &fc->color.green);
96 xmlFree (prop);
97 prop = xmlGetProp (node, (xmlChar *)"blue");
98 sscanf ((gchar *) prop, "%" G_GINT16_MODIFIER "x", &fc->color.blue);
99 xmlFree (prop);
100 }
101
102 return 0;
103 }
104
105 static GtkWidget *
106 filter_color_get_widget (EFilterElement *element)
107 {
108 EFilterColor *fc = E_FILTER_COLOR (element);
109 GtkWidget *color_button;
110
111 color_button = gtk_color_button_new_with_color (&fc->color);
112 gtk_widget_show (color_button);
113
114 g_signal_connect (
115 color_button, "color_set",
116 G_CALLBACK (set_color), element);
117
118 return color_button;
119 }
120
121 static void
122 filter_color_format_sexp (EFilterElement *element,
123 GString *out)
124 {
125 EFilterColor *fc = E_FILTER_COLOR (element);
126 gchar spec[16];
127
128 g_snprintf (
129 spec, sizeof (spec), "#%04x%04x%04x",
130 fc->color.red, fc->color.green, fc->color.blue);
131 camel_sexp_encode_string (out, spec);
132 }
133
134 static void
135 e_filter_color_class_init (EFilterColorClass *class)
136 {
137 EFilterElementClass *filter_element_class;
138
139 filter_element_class = E_FILTER_ELEMENT_CLASS (class);
140 filter_element_class->eq = filter_color_eq;
141 filter_element_class->xml_encode = filter_color_xml_encode;
142 filter_element_class->xml_decode = filter_color_xml_decode;
143 filter_element_class->get_widget = filter_color_get_widget;
144 filter_element_class->format_sexp = filter_color_format_sexp;
145 }
146
147 static void
148 e_filter_color_init (EFilterColor *filter)
149 {
150 }
151
152 /**
153 * filter_color_new:
154 *
155 * Create a new EFilterColor object.
156 *
157 * Return value: A new #EFilterColor object.
158 **/
159 EFilterColor *
160 e_filter_color_new (void)
161 {
162 return g_object_new (E_TYPE_FILTER_COLOR, NULL);
163 }