On Mon, Dec 27, 1999 at 10:06:31PM -0600, Jon Travis wrote:
> all I need is that the sort method be numeric in nature ... maybe instead of
> giving the full functionality, some reasonable defaults could be put into
> place, and we just select between a few sortfuncs?
I started on something like this. I thought I lost it, but I found it on a
backup. Basically I just took the default sort from gtk and modified it to
sort by integer value, by float value, and case insensitive and added these
directly (lots of redundancy!) to gtkmodule.c using the monkey see monkey do
method. That was for gnome-python 1.0.2. Here it is in unified diff format
(don't try to apply it! I edited it. Hope this is not too long.) I don't
think it would be too hard to make this standalone. I'll give it a shot
tomorrow.
diff -uNr pygtk.orig/GTK.py pygtk/GTK.py
--- pygtk.orig/GTK.py Mon Jan 11 05:30:34 1999
+++ pygtk/GTK.py Wed Apr 28 21:31:12 1999
+# GtkCList sort compare types
+COMPARE_DEFAULT = 0 #the default lexical ordering
+COMPARE_INSENSITIVE = 1 #case insensitive
+COMPARE_INT = 2 #compare as integer value; a string beginning
+ #with '0' is treated as octal, with '0x' as hex
+COMPARE_FLOAT = 3 #compare as floating point value
diff -uNr pygtk.orig/gtk.py pygtk/gtk.py
--- pygtk.orig/gtk.py Sat Apr 24 17:43:16 1999
+++ pygtk/gtk.py Wed Apr 28 20:32:16 1999
@@ -1268,6 +1268,10 @@
_gtk.gtk_clist_unselect_all(self._o)
def unselect_row(self, row, col):
_gtk.gtk_clist_unselect_row(self._o, row, col)
+ #Begin added by DMC
+ def set_compare_type(self, cmp_type):
+ _gtk.gtk_clist_set_compare_type(self._o, cmp_type)
+ #End added by DMC
class GtkCTree(GtkCList):
get_type = _gtk.gtk_ctree_get_type
diff -uNr pygtk.orig/gtkmodule.c pygtk/gtkmodule.c
--- pygtk.orig/gtkmodule.c Sat Apr 24 17:42:15 1999
+++ pygtk/gtkmodule.c Wed Apr 28 21:26:52 1999
@@ -4114,6 +4114,217 @@
}
}
+/* Begin gtk_clist_compare_as_int stuff */
+
+#define COMPARE_DEFAULT 0
+#define COMPARE_INSENSITIVE 1
+#define COMPARE_INT 2
+#define COMPARE_FLOAT 3
+
+/* clist_compare will be used to save the default compare func. */
+static GtkCListCompareFunc clist_compare = NULL;
+
+static int intcmp(char *s1, char *s2)
+{
+ int a, b;
+ char *tail;
+
+ /* Using strtol allows numbers like 0xFF (hex) and 081 (octal). */
+ a = strtol (s1, &tail, 0);
+ b = strtol (s2, &tail, 0);
+
+ return a-b;
+}
+
+static int doublecmp(char *s1, char *s2)
+{
+ double a, b;
+ char *tail;
+
+ a = g_strtod (s1, &tail);
+ b = g_strtod (s2, &tail);
+
+ if (a < b) return -1;
+ else if (a > b) return 1;
+
+ return 0;
+}
+
+
+/* Adapted from default_compare() in gtkclist.c */
+static gint
+cmp_int(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
+{
+ char *text1 = NULL;
+ char *text2 = NULL;
+
+ GtkCListRow *row1 = (GtkCListRow *) ptr1;
+ GtkCListRow *row2 = (GtkCListRow *) ptr2;
+
+ switch (row1->cell[clist->sort_column].type)
+ {
+ case GTK_CELL_TEXT:
+ text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
+ break;
+ case GTK_CELL_PIXTEXT:
+ text1 = GTK_CELL_PIXTEXT (row1->cell[clist->sort_column])->text;
+ break;
+ default:
+ break;
+ }
+
+ switch (row2->cell[clist->sort_column].type)
+ {
+ case GTK_CELL_TEXT:
+ text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
+ break;
+ case GTK_CELL_PIXTEXT:
+ text2 = GTK_CELL_PIXTEXT (row2->cell[clist->sort_column])->text;
+ break;
+ default:
+ break;
+ }
+
+ if (!text2)
+ return (text1 != NULL);
+
+ if (!text1)
+ return -1;
+
+ /* This is the only difference from the default compare which uses strcmp */
+ return intcmp (text1, text2);
+}
+
+static gint
+cmp_float(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
+{
+ char *text1 = NULL;
+ char *text2 = NULL;
+
+ GtkCListRow *row1 = (GtkCListRow *) ptr1;
+ GtkCListRow *row2 = (GtkCListRow *) ptr2;
+
+ switch (row1->cell[clist->sort_column].type)
+ {
+ case GTK_CELL_TEXT:
+ text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
+ break;
+ case GTK_CELL_PIXTEXT:
+ text1 = GTK_CELL_PIXTEXT (row1->cell[clist->sort_column])->text;
+ break;
+ default:
+ break;
+ }
+
+ switch (row2->cell[clist->sort_column].type)
+ {
+ case GTK_CELL_TEXT:
+ text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
+ break;
+ case GTK_CELL_PIXTEXT:
+ text2 = GTK_CELL_PIXTEXT (row2->cell[clist->sort_column])->text;
+ break;
+ default:
+ break;
+ }
+
+ if (!text2)
+ return (text1 != NULL);
+
+ if (!text1)
+ return -1;
+
+ /* This is the only difference from the default compare which uses strcmp */
+ return doublecmp (text1, text2);
+}
+
+static gint
+cmp_insensitive(GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2)
+{
+ char *text1 = NULL;
+ char *text2 = NULL;
+
+ GtkCListRow *row1 = (GtkCListRow *) ptr1;
+ GtkCListRow *row2 = (GtkCListRow *) ptr2;
+
+ switch (row1->cell[clist->sort_column].type)
+ {
+ case GTK_CELL_TEXT:
+ text1 = GTK_CELL_TEXT (row1->cell[clist->sort_column])->text;
+ break;
+ case GTK_CELL_PIXTEXT:
+ text1 = GTK_CELL_PIXTEXT (row1->cell[clist->sort_column])->text;
+ break;
+ default:
+ break;
+ }
+
+ switch (row2->cell[clist->sort_column].type)
+ {
+ case GTK_CELL_TEXT:
+ text2 = GTK_CELL_TEXT (row2->cell[clist->sort_column])->text;
+ break;
+ case GTK_CELL_PIXTEXT:
+ text2 = GTK_CELL_PIXTEXT (row2->cell[clist->sort_column])->text;
+ break;
+ default:
+ break;
+ }
+
+ if (!text2)
+ return (text1 != NULL);
+
+ if (!text1)
+ return -1;
+
+ /* This is the only difference from the default compare which uses strcmp */
+ return g_strcasecmp (text1, text2);
+}
+
+
+static void gtk_clist_set_compare_type(GtkCList *clist, gint type)
+{
+ /* Save default compare func */
+ if (clist_compare==NULL) clist_compare = clist->compare;
+ switch (type) {
+ case COMPARE_DEFAULT:
+ if (clist_compare != NULL) clist->compare = clist_compare;
+ break;
+ case COMPARE_INSENSITIVE:
+ gtk_clist_set_compare_func(clist, (GtkCListCompareFunc)
+ cmp_insensitive);
+ break;
+ case COMPARE_INT:
+ gtk_clist_set_compare_func(clist, (GtkCListCompareFunc)
+ cmp_int);
+ break;
+ case COMPARE_FLOAT:
+ gtk_clist_set_compare_func(clist, (GtkCListCompareFunc)
+ cmp_float);
+ break;
+ default:
+ if (clist_compare != NULL) clist->compare = clist_compare;
+ }
+}
+
+
+static PyObject *_wrap_gtk_clist_set_compare_type(PyObject *self,
+ PyObject *args) {
+ PyGtk_Object *obj;
+ gint type;
+
+
+ if (!PyArg_ParseTuple(args, "O!i:gtk_clist_set_compare_as_int",
+ &PyGtk_Type, &obj, &type))
+ return NULL;
+ gtk_clist_set_compare_type(GTK_CLIST(PyGtk_Get(obj)), type);
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+
+/* End gtk_clist_set_compare_as_int stuff */
+
Dave
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]