~ubuntu-branches/debian/squeeze/galeon/squeeze

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 *  Copyright (C) 2003  Tommi Komulainen
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation. 
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */ 

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gul-gui-option.h"

#include <glib/gi18n.h>
#include <gtk/gtkcelllayout.h>
#include <gtk/gtkcellrenderertext.h>
#include <gtk/gtkliststore.h>
#include <gtk/gtktreemodelsort.h>

#include <string.h>

/**************************************************************************
 * GulGuiOption
 *
 * GulGuiOption is an option item that has a explicitly defined value and a
 * (translated) title for showing to the user.  By linking the title and the
 * value together, instead of relying on the order of the items, we can sort
 * the user visible titles according to user's locale.  The end result is
 * likely to look more sane than just simply translating the titles in place,
 * which can make the order of items look random.
 */
/**
 * gul_gui_option_new:
 * @title: localized title for the option
 * @value: the value
 *
 * Returns: a newly allocated #GulGuiOption item.
 */
GulGuiOption *
gul_gui_option_new (const char *title, const char *value)
{
	GulGuiOption *item;
	char          *tmp;

	item = g_new(GulGuiOption, 1);
	item->title = g_strdup (title);
	item->value = g_strdup (value);

	tmp = g_utf8_casefold (item->title, -1);
	item->key = g_utf8_collate_key (tmp, -1);
	g_free (tmp);

	return item;
}

void
gul_gui_option_combobox_populate(GtkComboBox *combobox,
				 const GulGuiOption *options,
				 guint n_options,
				 gboolean sortable)
{
        guint i;
	GtkTreeModel *model = NULL;
	GtkListStore *store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);

        for (i = 0; i < n_options; i++)
	{
		GtkTreeIter iter;
		gtk_list_store_append(store, &iter);
		gtk_list_store_set(store, &iter,
				   OPTION_COL_TITLE, _(options[i].title),
				   OPTION_COL_VALUE, options[i].value,
				   -1);
	}

	if (sortable)
	{
		GtkTreeModel *sorter =
			gtk_tree_model_sort_new_with_model(GTK_TREE_MODEL(store));
		gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(sorter),
						     OPTION_COL_TITLE,
						     GTK_SORT_ASCENDING);
		model = sorter;
	}
	else
	{
		model = GTK_TREE_MODEL(store);
	}

        gtk_combo_box_set_model(GTK_COMBO_BOX(combobox), model);

	gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), 0);
}


gchar *gul_gui_option_combobox_get_value(GtkComboBox *combobox)
{
	gchar *value = NULL;
	GtkTreeIter iter;
	GtkTreeModel *model = gtk_combo_box_get_model(combobox);

	if (gtk_combo_box_get_active_iter(combobox, &iter))
	{
		gtk_tree_model_get(model, &iter,
				   OPTION_COL_VALUE, &value,
				   -1);
	}

	return value;
}

void
gul_gui_option_combobox_set_value(GtkComboBox *combobox,
				  const gchar *value)
{
	gboolean valid;
	GtkTreeIter iter;

	GtkTreeModel *model = gtk_combo_box_get_model(combobox); 

	valid = gtk_tree_model_get_iter_first(model, &iter);

	while (valid)
	{
		gchar *iter_value;
		gtk_tree_model_get(model, &iter,
				   OPTION_COL_VALUE, &iter_value,
				   -1);
		if (!iter_value)
		{
			valid = gtk_tree_model_iter_next(model, &iter);
			continue;
		}

		if (strcmp(value, iter_value) == 0)
		{
			gtk_combo_box_set_active_iter(combobox, &iter);
			g_free(iter_value);
			break;
		}
		g_free(iter_value);

		valid = gtk_tree_model_iter_next(model, &iter);
	}
}