[ekg2-commit] ekg2: userlistgui.c (HEAD) [darkjames]

CVS commit cvs w toxygen.net
Czw, 22 Lis 2007, 15:05:41 CET


Module name:	ekg2
Changes by:	darkjames	2007-11-22 15:05:39

Modified files:
	userlistgui.c

Log message:
use GtkTreeSortable in userlist.
and change some magic constants into enums.
Currently sort after address of pixmap ;)
	[first ffc -> avail -> away -> dnd -> xa -> invisible -> notavail -> error -> unknown last]
It gives sample how do what we have got in ncurses in much faster way.

Index: userlistgui.c
===================================================================
RCS file: /home/cvs/ekg2/plugins/gtk/userlistgui.c,v
diff -d -u -r1.8 -r1.9
--- userlistgui.c	30 Aug 2007 22:43:45 -0000	1.8
+++ userlistgui.c	22 Nov 2007 14:05:39 -0000	1.9
@@ -42,6 +42,17 @@
 #include "main.h"
 #include "palette.h"
 
+enum {
+	USERLIST_PIXMAP = 0,
+	USERLIST_NICKNAME,
+	USERLIST_DESCRIPTION,
+	USERLIST_USER,
+	USERLIST_COLOR,
+
+	USERLIST_COLS
+};
+
+
 #define show_descr_in_userlist_config 1
 
 #if 0
@@ -282,7 +293,29 @@
 
 #warning "fe_userlist_rehash() dobre do zmian stanow"
 
-/* void fe_userlist_insert(window_t *sess, struct User *newuser, int row, int sel)  */
+static gint gtk_userlist_sort_func(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata) {
+	GdkPixbuf *a1, *b1;
+
+	gint sortcol = GPOINTER_TO_INT(userdata);
+
+	if (sortcol != USERLIST_PIXMAP) {
+		printf("gtk_userlist_sort_func() IE\n");
+		return;
+	}
+
+/* XXX, sequence should match sequence in contacts_options */
+	gtk_tree_model_get(model, a, USERLIST_PIXMAP, &a1, -1);
+	gtk_tree_model_get(model, b, USERLIST_PIXMAP, &b1, -1);
+
+/* yeah, i know, i'm lazy */
+	if (a1 < b1)
+		return -1;
+	else if (a1 > b1)
+		return 1;
+	else	return 0;
+}
+
+
 void fe_userlist_insert(window_t *sess, userlist_t *u, GdkPixbuf **pixmaps)
 {
 	GtkTreeModel *model = gtk_private(sess)->user_model;
@@ -330,14 +363,13 @@
 		do_away = FALSE;
 
 #endif
-	gtk_list_store_insert_with_values(GTK_LIST_STORE(model), &iter, 0, /* row, */
-					  0, pix,
-					  1, u->nickname,
+	gtk_list_store_insert_with_values(GTK_LIST_STORE(model), &iter, -1,
+					  USERLIST_PIXMAP, pix,
+					  USERLIST_NICKNAME, u->nickname,
 					  /* XXX, u->uid */
-					  2, u->descr,
-//					  3, /* newuser, */ NULL,
-//					  4, /* (do_away) */ FALSE,
-//					  /* ? (newuser->away ? &colors[COL_AWAY] : NULL) : */ (NULL),
+					  USERLIST_DESCRIPTION, u->descr,
+					  USERLIST_USER, u,
+//					  USERLIST_COLOR, /* (do_away) */ FALSE,  ? (newuser->away ? &colors[COL_AWAY] : NULL) : */ (NULL),
 					  -1);
 
 #if DARK
@@ -375,8 +407,20 @@
 
 void *userlist_create_model(void)
 {
-	return gtk_list_store_new(5, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING,
-				  G_TYPE_POINTER, GDK_TYPE_COLOR);
+	GtkTreeSortable *sortable;
+
+	void *liststore;
+
+	liststore = gtk_list_store_new(USERLIST_COLS, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, GDK_TYPE_COLOR);
+
+	sortable = GTK_TREE_SORTABLE(liststore);
+	
+	gtk_tree_sortable_set_sort_func(sortable, USERLIST_PIXMAP, gtk_userlist_sort_func, GINT_TO_POINTER(USERLIST_PIXMAP), NULL);
+
+/* initial sort */
+	gtk_tree_sortable_set_sort_column_id(sortable, USERLIST_PIXMAP, GTK_SORT_ASCENDING);
+
+	return liststore;
 }
 
 static void userlist_add_columns(GtkTreeView * treeview)
@@ -385,22 +429,18 @@
 
 	/* icon column */
 	renderer = gtk_cell_renderer_pixbuf_new();
-	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview),
-						    -1, NULL, renderer, "pixbuf", 0, NULL);
+	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, NULL, renderer, "pixbuf", USERLIST_PIXMAP, NULL);
 
 	/* nick column */
 	renderer = gtk_cell_renderer_text_new();
 	gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer), 1);
-	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview),
-						    -1, NULL, renderer,
-						    "text", 1, "foreground-gdk", 4, NULL);
+	gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, NULL, renderer, "text", USERLIST_NICKNAME, "foreground-gdk", USERLIST_COLOR, NULL);
+
+	/* description column (?) */
 	if (show_descr_in_userlist_config) {
-		/* hostname column */
 		renderer = gtk_cell_renderer_text_new();
-		gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer),
-								  1);
-		gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, NULL,
-							    renderer, "text", 2, NULL);
+		gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer), 1);
+		gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeview), -1, NULL, renderer, "text", 2, NULL);
 	}
 }
 


Więcej informacji o liście dyskusyjnej ekg2-commit