evolution-3.6.4/widgets/table/e-cell-percent.c

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  *		Damon Chaplin <damon@ximian.com>
 18  *
 19  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 20  *
 21  */
 22 
 23 /*
 24  * ECellPercent - a subclass of ECellText used to show an integer percentage
 25  * in an ETable.
 26  */
 27 
 28 #ifdef HAVE_CONFIG_H
 29 #include <config.h>
 30 #endif
 31 
 32 #include <ctype.h>
 33 
 34 #include <sys/time.h>
 35 #include <unistd.h>
 36 #include <stdio.h>
 37 #include <glib/gi18n.h>
 38 
 39 #include "e-cell-percent.h"
 40 
 41 G_DEFINE_TYPE (ECellPercent, e_cell_percent, E_TYPE_CELL_TEXT)
 42 
 43 static gchar *
 44 ecp_get_text (ECellText *cell,
 45               ETableModel *model,
 46               gint col,
 47               gint row)
 48 {
 49 	gint percent;
 50 	static gchar buffer[8];
 51 
 52 	percent = GPOINTER_TO_INT (e_table_model_value_at (model, col, row));
 53 
 54 	/* A -ve value means the property is not set. */
 55 	if (percent < 0) {
 56 		buffer[0] = '\0';
 57 	} else {
 58 		g_snprintf (buffer, sizeof (buffer), "%i%%", percent);
 59 	}
 60 
 61 	return buffer;
 62 }
 63 
 64 static void
 65 ecp_free_text (ECellText *cell,
 66                gchar *text)
 67 {
 68 	/* Do Nothing. */
 69 }
 70 
 71 /* FIXME: We need to set the "transient_for" property for the dialog. */
 72 static void
 73 show_percent_warning (void)
 74 {
 75 	GtkWidget *dialog;
 76 
 77 	dialog = gtk_message_dialog_new (
 78 		NULL, 0,
 79 		GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
 80 		"%s", _("The percent value must be between 0 and 100, inclusive"));
 81 	gtk_dialog_run (GTK_DIALOG (dialog));
 82 	gtk_widget_destroy (dialog);
 83 }
 84 
 85 static void
 86 ecp_set_value (ECellText *cell,
 87                ETableModel *model,
 88                gint col,
 89                gint row,
 90                const gchar *text)
 91 {
 92 	gint matched, percent;
 93 	gboolean empty = TRUE;
 94 	const gchar *p;
 95 
 96 	if (text) {
 97 		p = text;
 98 		while (*p) {
 99 			if (!isspace ((guchar) *p)) {
100 				empty = FALSE;
101 				break;
102 			}
103 			p++;
104 		}
105 	}
106 
107 	if (empty) {
108 		percent = -1;
109 	} else {
110 		matched = sscanf (text, "%i", &percent);
111 
112 		if (matched != 1 || percent < 0 || percent > 100) {
113 			show_percent_warning ();
114 			return;
115 		}
116 	}
117 
118 	e_table_model_set_value_at (
119 		model, col, row,
120 		GINT_TO_POINTER (percent));
121 }
122 
123 static void
124 e_cell_percent_class_init (ECellPercentClass *ecpc)
125 {
126 	ECellTextClass *ectc = (ECellTextClass *) ecpc;
127 
128 	ectc->get_text  = ecp_get_text;
129 	ectc->free_text = ecp_free_text;
130 	ectc->set_value = ecp_set_value;
131 }
132 
133 static void
134 e_cell_percent_init (ECellPercent *ecp)
135 {
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 *
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 }