changeset 7909:5be22af4ebfc

[gaim-migrate @ 8569] Reworked the list field API a bit. Items now have data associated with them (which is not automatically freed by the API). committer: Tailor Script <tailor@pidgin.im>
author Christian Hammond <chipx86@chipx86.com>
date Sun, 21 Dec 2003 20:51:05 +0000
parents ad4841411ada
children a63c93e39ea5
files src/gtkrequest.c src/request.c src/request.h
diffstat 3 files changed, 72 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/src/gtkrequest.c	Sun Dec 21 19:58:11 2003 +0000
+++ b/src/gtkrequest.c	Sun Dec 21 20:51:05 2003 +0000
@@ -621,7 +621,7 @@
 	GaimRequestField *field = (GaimRequestField *)data;
 	const char *text;
 
-	gtk_tree_model_get(model, iter, 0, &text, -1);
+	gtk_tree_model_get(model, iter, 1, &text, -1);
 
 	gaim_request_field_list_add_selected(field, text);
 }
@@ -643,6 +643,8 @@
 	GtkCellRenderer *renderer;
 	GtkTreeSelection *sel;
 	GtkTreeViewColumn *column;
+	GtkTreeIter iter;
+	const GList *l;
 
 	/* Create the scrolled window */
 	sw = gtk_scrolled_window_new(NULL, NULL);
@@ -654,7 +656,7 @@
 	gtk_widget_show(sw);
 
 	/* Create the list store */
-	store = gtk_list_store_new(1, G_TYPE_STRING);
+	store = gtk_list_store_new(2, G_TYPE_POINTER, G_TYPE_STRING);
 
 	/* Create the tree view */
 	treeview = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
@@ -674,7 +676,19 @@
 
 	renderer = gtk_cell_renderer_text_new();
 	gtk_tree_view_column_pack_start(column, renderer, TRUE);
-	gtk_tree_view_column_add_attribute(column, renderer, "text", 0);
+	gtk_tree_view_column_add_attribute(column, renderer, "text", 1);
+
+	for (l = gaim_request_field_list_get_items(field); l != NULL; l = l->next)
+	{
+		const char *text = (const char *)l->data;
+
+		gtk_list_store_append(store, &iter);
+
+		gtk_list_store_set(store, &iter,
+						   0, gaim_request_field_list_get_data(field, text),
+						   1, text,
+						   -1);
+	}
 
 	gtk_container_add(GTK_CONTAINER(sw), treeview);
 	gtk_widget_show(treeview);
--- a/src/request.c	Sun Dec 21 19:58:11 2003 +0000
+++ b/src/request.c	Sun Dec 21 20:51:05 2003 +0000
@@ -299,6 +299,8 @@
 			g_list_foreach(field->u.list.selected, (GFunc)g_free, NULL);
 			g_list_free(field->u.list.selected);
 		}
+
+		g_hash_table_destroy(field->u.list.item_data);
 	}
 
 	g_free(field);
@@ -649,37 +651,22 @@
 }
 
 GaimRequestField *
-gaim_request_field_list_new(const char *id, const char *text, GList *items)
+gaim_request_field_list_new(const char *id, const char *text)
 {
 	GaimRequestField *field;
 
-	g_return_val_if_fail(id    != NULL, NULL);
-	g_return_val_if_fail(items != NULL, NULL);
+	g_return_val_if_fail(id   != NULL, NULL);
+	g_return_val_if_fail(text != NULL, NULL);
 
 	field = gaim_request_field_new(id, text, GAIM_REQUEST_FIELD_LIST);
 
-	gaim_request_field_list_set_items(field, items);
+	field->u.list.item_data = g_hash_table_new_full(g_str_hash, g_str_equal,
+													g_free, NULL);
 
 	return field;
 }
 
 void
-gaim_request_field_list_set_items(GaimRequestField *field, GList *items)
-{
-	g_return_if_fail(field != NULL);
-	g_return_if_fail(items != NULL);
-	g_return_if_fail(field->type == GAIM_REQUEST_FIELD_LIST);
-
-	if (field->u.list.items != NULL)
-	{
-		g_list_foreach(field->u.list.items, (GFunc)g_free, NULL);
-		g_list_free(field->u.list.items);
-	}
-
-	field->u.list.items = items;
-}
-
-void
 gaim_request_field_list_set_multi_select(GaimRequestField *field,
 										 gboolean multi_select)
 {
@@ -698,14 +685,29 @@
 	return field->u.list.multiple_selection;
 }
 
+void *
+gaim_request_field_list_get_data(const GaimRequestField *field,
+								 const char *text)
+{
+	g_return_val_if_fail(field != NULL, NULL);
+	g_return_val_if_fail(text  != NULL, NULL);
+	g_return_val_if_fail(field->type == GAIM_REQUEST_FIELD_LIST, NULL);
+
+	return g_hash_table_lookup(field->u.list.item_data, text);
+}
+
 void
-gaim_request_field_list_add(GaimRequestField *field, const char *item)
+gaim_request_field_list_add(GaimRequestField *field, const char *item,
+							void *data)
 {
 	g_return_if_fail(field != NULL);
 	g_return_if_fail(item  != NULL);
+	g_return_if_fail(data  != NULL);
 	g_return_if_fail(field->type == GAIM_REQUEST_FIELD_LIST);
 
 	field->u.list.items = g_list_append(field->u.list.items, g_strdup(item));
+
+	g_hash_table_insert(field->u.list.item_data, g_strdup(item), data);
 }
 
 void
@@ -715,8 +717,8 @@
 	g_return_if_fail(item  != NULL);
 	g_return_if_fail(field->type == GAIM_REQUEST_FIELD_LIST);
 
-	field->u.list.selected =
-		g_list_append(field->u.list.selected, g_strdup(item));
+	field->u.list.selected = g_list_append(field->u.list.selected,
+										   g_strdup(item));
 }
 
 void
@@ -725,7 +727,17 @@
 	g_return_if_fail(field != NULL);
 	g_return_if_fail(field->type == GAIM_REQUEST_FIELD_LIST);
 
-	gaim_request_field_list_set_selected(field, NULL);
+	if (field->u.list.items != NULL)
+	{
+		g_list_foreach(field->u.list.items, (GFunc)g_free, NULL);
+		g_list_free(field->u.list.items);
+		field->u.list.items = NULL;
+	}
+
+	g_hash_table_destroy(field->u.list.item_data);
+
+	field->u.list.item_data = g_hash_table_new_full(g_str_hash, g_str_equal,
+													g_free, NULL);
 }
 
 void
@@ -739,6 +751,7 @@
 	{
 		g_list_foreach(field->u.list.selected, (GFunc)g_free, NULL);
 		g_list_free(field->u.list.selected);
+		field->u.list.selected = NULL;
 	}
 
 	field->u.list.selected = items;
--- a/src/request.h	Sun Dec 21 19:58:11 2003 +0000
+++ b/src/request.h	Sun Dec 21 20:51:05 2003 +0000
@@ -104,6 +104,7 @@
 		struct
 		{
 			GList *items;
+			GHashTable *item_data;
 			GList *selected;
 
 			gboolean multiple_selection;
@@ -705,24 +706,12 @@
 /**
  * Creates a multiple list item field.
  *
- * @param id    The field ID.
- * @param text  The optional label of the field.
- * @param items The list of items.
+ * @param id   The field ID.
+ * @param text The optional label of the field.
  *
  * @return The new field.
  */
-GaimRequestField *gaim_request_field_list_new(const char *id, const char *text,
-											  GList *items);
-
-/**
- * Sets the list of items in a list field.
- *
- * The items are not copied. The actual list passed is assigned to the field.
- *
- * @param field The list field.
- * @param items The items.
- */
-void gaim_request_field_list_set_items(GaimRequestField *field, GList *items);
+GaimRequestField *gaim_request_field_list_new(const char *id, const char *text);
 
 /**
  * Sets whether or not a list field allows multiple selection.
@@ -745,12 +734,25 @@
 	const GaimRequestField *field);
 
 /**
+ * Returns the data for a particular item.
+ *
+ * @param field The list field.
+ * @param item  The item text.
+ *
+ * @return The data associated with the item.
+ */
+void *gaim_request_field_list_get_data(const GaimRequestField *field,
+									   const char *text);
+
+/**
  * Adds an item to a list field.
  *
  * @param field The list field.
  * @param item  The list item.
+ * @param data  The associated data.
  */
-void gaim_request_field_list_add(GaimRequestField *field, const char *item);
+void gaim_request_field_list_add(GaimRequestField *field,
+								 const char *item, void *data);
 
 /**
  * Adds a selected item to the list field.