evolution-3.6.4/e-util/e-dialog-widgets.c

No issues found

  1 /*
  2  * Evolution internal utilities - Glade dialog widget utilities
  3  *
  4  * This program is free software; you can redistribute it and/or
  5  * modify it under the terms of the GNU Lesser General Public
  6  * License as published by the Free Software Foundation; either
  7  * version 2 of the License, or (at your option) version 3.
  8  *
  9  * This program is distributed in the hope that it will be useful,
 10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 12  * Lesser General Public License for more details.
 13  *
 14  * You should have received a copy of the GNU Lesser General Public
 15  * License along with the program; if not, see <http://www.gnu.org/licenses/>
 16  *
 17  *
 18  * Authors:
 19  *		Federico Mena-Quintero <federico@ximian.com>
 20  *
 21  * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 22  *
 23  */
 24 
 25 #ifdef HAVE_CONFIG_H
 26 #include <config.h>
 27 #endif
 28 
 29 #include <math.h>
 30 #include <string.h>
 31 #include <time.h>
 32 #include <gtk/gtk.h>
 33 
 34 #include "e-dialog-widgets.h"
 35 
 36 /* Converts an mapped value to the appropriate index in an item group.  The
 37  * values for the items are provided as a -1-terminated array.
 38  */
 39 static gint
 40 value_to_index (const gint *value_map,
 41                 gint value)
 42 {
 43 	gint i;
 44 
 45 	for (i = 0; value_map[i] != -1; i++)
 46 		if (value_map[i] == value)
 47 			return i;
 48 
 49 	return -1;
 50 }
 51 
 52 /* Converts an index in an item group to the appropriate mapped value.  See the
 53  * function above.
 54  */
 55 static gint
 56 index_to_value (const gint *value_map,
 57                 gint index)
 58 {
 59 	gint i;
 60 
 61 	/* We do this the hard way, i.e. not as a simple array reference, to
 62 	 * check for correctness.
 63 	 */
 64 
 65 	for (i = 0; value_map[i] != -1; i++)
 66 		if (i == index)
 67 			return value_map[i];
 68 
 69 	return -1;
 70 }
 71 
 72 /**
 73  * e_dialog_combo_box_set:
 74  * @widget: A #GtkComboBox.
 75  * @value: Enumerated value.
 76  * @value_map: Map from enumeration values to array indices.
 77  *
 78  * Sets the selected item in a #GtkComboBox.  Please read the description of
 79  * e_dialog_radio_set() to see how @value_map maps enumeration values to item
 80  * indices.
 81  **/
 82 void
 83 e_dialog_combo_box_set (GtkWidget *widget,
 84                         gint value,
 85                         const gint *value_map)
 86 {
 87 	gint i;
 88 
 89 	g_return_if_fail (GTK_IS_COMBO_BOX (widget));
 90 	g_return_if_fail (value_map != NULL);
 91 
 92 	i = value_to_index (value_map, value);
 93 
 94 	if (i != -1)
 95 		gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
 96 	else
 97 		g_message (
 98 			"e_dialog_combo_box_set(): could not "
 99 			"find value %d in value map!", value);
100 }
101 
102 /**
103  * e_dialog_combo_box_get:
104  * @widget: A #GtkComboBox.
105  * @value_map: Map from enumeration values to array indices.
106  *
107  * Queries the selected item in a #GtkComboBox.  Please read the description
108  * of e_dialog_radio_set() to see how @value_map maps enumeration values to item
109  * indices.
110  *
111  * Return value: Enumeration value which corresponds to the selected item in the
112  * combo box.
113  **/
114 gint
115 e_dialog_combo_box_get (GtkWidget *widget,
116                         const gint *value_map)
117 {
118 	gint active;
119 	gint i;
120 
121 	g_return_val_if_fail (GTK_IS_COMBO_BOX (widget), -1);
122 	g_return_val_if_fail (value_map != NULL, -1);
123 
124 	active = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
125 	i = index_to_value (value_map, active);
126 
127 	if (i == -1) {
128 		g_message (
129 			"e_dialog_combo_box_get(): could not "
130 			"find index %d in value map!", i);
131 		return -1;
132 	}
133 
134 	return i;
135 }