1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published by
4  * the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful, but
7  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
8  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9  * for more details.
10  *
11  * You should have received a copy of the GNU Lesser General Public License
12  * along with this program; if not, see <http://www.gnu.org/licenses/>.
13  *
14  *
15  * Authors:
16  *		Damon Chaplin <damon@ximian.com>
17  *
18  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
19  *
20  */
21 
22 /*
23  * ECellPercent - a subclass of ECellText used to show an integer percentage
24  * in an ETable.
25  */
26 
27 #include "evolution-config.h"
28 
29 #include <ctype.h>
30 
31 #include <sys/time.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <glib/gi18n.h>
35 
36 #include "e-cell-percent.h"
37 
G_DEFINE_TYPE(ECellPercent,e_cell_percent,E_TYPE_CELL_TEXT)38 G_DEFINE_TYPE (ECellPercent, e_cell_percent, E_TYPE_CELL_TEXT)
39 
40 static gchar *
41 ecp_get_text (ECellText *cell,
42               ETableModel *model,
43               gint col,
44               gint row)
45 {
46 	gint percent;
47 	static gchar buffer[8];
48 
49 	percent = GPOINTER_TO_INT (e_table_model_value_at (model, col, row));
50 
51 	/* A -ve value means the property is not set. */
52 	if (percent < 0) {
53 		buffer[0] = '\0';
54 	} else {
55 		g_snprintf (buffer, sizeof (buffer), "%i%%", percent);
56 	}
57 
58 	return buffer;
59 }
60 
61 static void
ecp_free_text(ECellText * cell,ETableModel * model,gint col,gchar * text)62 ecp_free_text (ECellText *cell,
63 	       ETableModel *model,
64 	       gint col,
65                gchar *text)
66 {
67 	/* Do Nothing. */
68 }
69 
70 /* FIXME: We need to set the "transient_for" property for the dialog. */
71 static void
show_percent_warning(void)72 show_percent_warning (void)
73 {
74 	GtkWidget *dialog;
75 
76 	dialog = gtk_message_dialog_new (
77 		NULL, 0,
78 		GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
79 		"%s", _("The percent value must be between 0 and 100, inclusive"));
80 	gtk_dialog_run (GTK_DIALOG (dialog));
81 	gtk_widget_destroy (dialog);
82 }
83 
84 static void
ecp_set_value(ECellText * cell,ETableModel * model,gint col,gint row,const gchar * text)85 ecp_set_value (ECellText *cell,
86                ETableModel *model,
87                gint col,
88                gint row,
89                const gchar *text)
90 {
91 	gint matched, percent;
92 	gboolean empty = TRUE;
93 	const gchar *p;
94 
95 	if (text) {
96 		p = text;
97 		while (*p) {
98 			if (!isspace ((guchar) *p)) {
99 				empty = FALSE;
100 				break;
101 			}
102 			p++;
103 		}
104 	}
105 
106 	if (empty) {
107 		percent = -1;
108 	} else {
109 		matched = sscanf (text, "%i", &percent);
110 
111 		if (matched != 1 || percent < 0 || percent > 100) {
112 			show_percent_warning ();
113 			return;
114 		}
115 	}
116 
117 	e_table_model_set_value_at (
118 		model, col, row,
119 		GINT_TO_POINTER (percent));
120 }
121 
122 static void
e_cell_percent_class_init(ECellPercentClass * ecpc)123 e_cell_percent_class_init (ECellPercentClass *ecpc)
124 {
125 	ECellTextClass *ectc = (ECellTextClass *) ecpc;
126 
127 	ectc->get_text = ecp_get_text;
128 	ectc->free_text = ecp_free_text;
129 	ectc->set_value = ecp_set_value;
130 }
131 
132 static void
e_cell_percent_init(ECellPercent * ecp)133 e_cell_percent_init (ECellPercent *ecp)
134 {
135 	g_object_set (ecp, "use-tabular-numbers", TRUE, NULL);
136 }
137 
138 /**
139  * e_cell_percent_new:
140  * @fontname: font to be used to render on the screen
141  * @justify: Justification of the string in the cell.
142  *
143  * Creates a new ECell renderer that can be used to render an integer
144  * percentage that comes from the model.  The value returned from the model is
145  * interpreted as being an int.
146  *
147  * See ECellText for other features.
148  *
149  * Returns: an ECell object that can be used to render numbers.
150  */
151 ECell *
e_cell_percent_new(const gchar * fontname,GtkJustification justify)152 e_cell_percent_new (const gchar *fontname,
153                     GtkJustification justify)
154 {
155 	ECellPercent *ecn = g_object_new (E_TYPE_CELL_PERCENT, NULL);
156 
157 	e_cell_text_construct (E_CELL_TEXT (ecn), fontname, justify);
158 
159 	return (ECell *) ecn;
160 }
161