Updated Traditional Chinese translation(Hong Kong and Taiwan)
[evolution.git] / filter / e-filter-int.c
blobdc0fbdb5a84034889b4454b5ecb06b52fae7822b
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.
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.
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/>
16 * Authors:
17 * Jeffrey Stedfast <fejj@ximian.com>
19 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <stdlib.h>
28 #include <gtk/gtk.h>
30 #include <libedataserver/e-sexp.h>
32 #include "e-filter-int.h"
34 G_DEFINE_TYPE (
35 EFilterInt,
36 e_filter_int,
37 E_TYPE_FILTER_ELEMENT)
39 static void
40 filter_int_spin_changed (GtkSpinButton *spin_button,
41 EFilterElement *element)
43 EFilterInt *filter_int = E_FILTER_INT (element);
45 filter_int->val = gtk_spin_button_get_value_as_int (spin_button);
48 static void
49 filter_int_finalize (GObject *object)
51 EFilterInt *filter_int = E_FILTER_INT (object);
53 g_free (filter_int->type);
55 /* Chain up to parent's finalize() method. */
56 G_OBJECT_CLASS (e_filter_int_parent_class)->finalize (object);
59 static gint
60 filter_int_eq (EFilterElement *element_a,
61 EFilterElement *element_b)
63 EFilterInt *filter_int_a = E_FILTER_INT (element_a);
64 EFilterInt *filter_int_b = E_FILTER_INT (element_b);
66 /* Chain up to parent's eq() method. */
67 if (!E_FILTER_ELEMENT_CLASS (e_filter_int_parent_class)->
68 eq (element_a, element_b))
69 return FALSE;
71 return (filter_int_a->val == filter_int_b->val);
74 static EFilterElement *
75 filter_int_clone (EFilterElement *element)
77 EFilterInt *filter_int = E_FILTER_INT (element);
78 EFilterInt *clone;
80 clone = (EFilterInt *) e_filter_int_new_type (
81 filter_int->type, filter_int->min, filter_int->max);
82 clone->val = filter_int->val;
84 E_FILTER_ELEMENT (clone)->name = g_strdup (element->name);
86 return E_FILTER_ELEMENT (clone);
89 static xmlNodePtr
90 filter_int_xml_encode (EFilterElement *element)
92 EFilterInt *filter_int = E_FILTER_INT (element);
93 xmlNodePtr value;
94 gchar intval[32];
95 const gchar *type;
97 type = filter_int->type ? filter_int->type : "integer";
99 value = xmlNewNode (NULL, (xmlChar *)"value");
100 xmlSetProp (value, (xmlChar *) "name", (xmlChar *) element->name);
101 xmlSetProp (value, (xmlChar *) "type", (xmlChar *) type);
103 sprintf (intval, "%d", filter_int->val);
104 xmlSetProp (value, (xmlChar *)type, (xmlChar *)intval);
106 return value;
109 static gint
110 filter_int_xml_decode (EFilterElement *element,
111 xmlNodePtr node)
113 EFilterInt *filter_int = E_FILTER_INT (element);
114 gchar *name, *type;
115 gchar *intval;
117 name = (gchar *)xmlGetProp (node, (xmlChar *)"name");
118 xmlFree (element->name);
119 element->name = name;
121 type = (gchar *)xmlGetProp (node, (xmlChar *)"type");
122 g_free (filter_int->type);
123 filter_int->type = g_strdup (type);
124 xmlFree (type);
126 intval = (gchar *)xmlGetProp (
127 node, (xmlChar *) (filter_int->type ?
128 filter_int->type : "integer"));
129 if (intval) {
130 filter_int->val = atoi (intval);
131 xmlFree (intval);
132 } else {
133 filter_int->val = 0;
136 return 0;
139 static GtkWidget *
140 filter_int_get_widget (EFilterElement *element)
142 EFilterInt *filter_int = E_FILTER_INT (element);
143 GtkWidget *widget;
144 GtkAdjustment *adjustment;
146 adjustment = GTK_ADJUSTMENT (gtk_adjustment_new (
147 0.0, (gfloat) filter_int->min,
148 (gfloat) filter_int->max, 1.0, 1.0, 0));
149 widget = gtk_spin_button_new (
150 adjustment,
151 filter_int->max > filter_int->min + 1000 ? 5.0 : 1.0, 0);
152 gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (widget), TRUE);
154 if (filter_int->val)
155 gtk_spin_button_set_value (
156 GTK_SPIN_BUTTON (widget), (gfloat) filter_int->val);
158 g_signal_connect (
159 widget, "value-changed",
160 G_CALLBACK (filter_int_spin_changed), element);
162 return widget;
165 static void
166 filter_int_format_sexp (EFilterElement *element,
167 GString *out)
169 EFilterInt *filter_int = E_FILTER_INT (element);
171 if (filter_int->val < 0)
172 /* See #364731 #457523 C6*/
173 g_string_append_printf (out, "(- %d)", (filter_int->val * -1));
174 else
175 g_string_append_printf (out, "%d", filter_int->val);
178 static void
179 e_filter_int_class_init (EFilterIntClass *class)
181 GObjectClass *object_class;
182 EFilterElementClass *filter_element_class;
184 object_class = G_OBJECT_CLASS (class);
185 object_class->finalize = filter_int_finalize;
187 filter_element_class = E_FILTER_ELEMENT_CLASS (class);
188 filter_element_class->eq = filter_int_eq;
189 filter_element_class->clone = filter_int_clone;
190 filter_element_class->xml_encode = filter_int_xml_encode;
191 filter_element_class->xml_decode = filter_int_xml_decode;
192 filter_element_class->get_widget = filter_int_get_widget;
193 filter_element_class->format_sexp = filter_int_format_sexp;
196 static void
197 e_filter_int_init (EFilterInt *filter_int)
199 filter_int->min = 0;
200 filter_int->max = G_MAXINT;
203 EFilterElement *
204 e_filter_int_new (void)
206 return g_object_new (E_TYPE_FILTER_INT, NULL);
209 EFilterElement *
210 e_filter_int_new_type (const gchar *type,
211 gint min,
212 gint max)
214 EFilterInt *filter_int;
216 filter_int = g_object_new (E_TYPE_FILTER_INT, NULL);
218 filter_int->type = g_strdup (type);
219 filter_int->min = min;
220 filter_int->max = max;
222 return E_FILTER_ELEMENT (filter_int);
225 void
226 e_filter_int_set_value (EFilterInt *filter_int,
227 gint value)
229 g_return_if_fail (E_IS_FILTER_INT (filter_int));
231 filter_int->val = value;