I had some time on my hands, so I hacked gtkmodule.c and gtk.py to add two
methods, GtkCList.set_compare_as_int() and GtkCList.reset_compare_func()
since it seemed to me that a common reason (at least my reason) to change
the compare func it's because you want to sort in numeric order rather than
lexical order.
Now that I think of it, one method that takes arguments like INT, FLOAT and
DEFAULT would probably be better. Maybe CASE_INSENSITIVE, too.
Is there any interest in adding this or is it an abomination? If it's OK,
I'll fix it up to make it more general. A diff from gnome-python-1.0.2
follows.
Dave Cook
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 18:24:20 1999
@@ -1268,6 +1268,12 @@
_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_as_int(self):
+ _gtk.gtk_clist_set_compare_as_int(self._o)
+ def reset_compare_func(self):
+ _gtk.gtk_clist_reset_compare_func(self._o)
+ #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 18:23:27 1999
@@ -4114,6 +4114,111 @@
}
}
+/* Begin gtk_clist_compare_as_int stuff */
+
+/* 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);
+
+ if (a < b) return -1;
+ else if (a==b) return 0;
+
+ return 1;
+}
+
+
+/* Adapted from default_compare() in gtkclist.c */
+static gint
+cmp_as_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 void gtk_clist_set_compare_as_int(GtkCList *clist)
+{
+ /* Save default compare func */
+ if (clist_compare==NULL) clist_compare = clist->compare;
+ gtk_clist_set_compare_func(clist, (GtkCListCompareFunc) cmp_as_int);
+}
+
+/* Reset the compare func to the default. */
+static void gtk_clist_reset_compare_func(GtkCList *clist)
+{
+ if (clist_compare != NULL) clist->compare = clist_compare;
+}
+
+static PyObject *_wrap_gtk_clist_set_compare_as_int(PyObject *self,
+ PyObject *args) {
+ PyGtk_Object *obj;
+
+ if (!PyArg_ParseTuple(args, "O!:gtk_clist_set_compare_as_int",
+ &PyGtk_Type, &obj))
+ return NULL;
+ gtk_clist_set_compare_as_int(GTK_CLIST(PyGtk_Get(obj)));
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+static PyObject *_wrap_gtk_clist_reset_compare_func(PyObject *self,
+ PyObject *args) {
+ PyGtk_Object *obj;
+
+ if (!PyArg_ParseTuple(args, "O!:gtk_clist_reset_compare_func",
+ &PyGtk_Type, &obj))
+ return NULL;
+ gtk_clist_reset_compare_func(GTK_CLIST(PyGtk_Get(obj)));
+ Py_INCREF(Py_None);
+ return Py_None;
+}
+
+/* End gtk_clist_set_compare_as_int stuff */
+
static PyObject *
_wrap_gtk_combo_set_popdown_strings(PyObject *self, PyObject *args) {
PyObject *obj, *list, *item;
@@ -5630,6 +5735,8 @@
{ "gtk_clist_get_row_data", _wrap_gtk_clist_get_row_data, 1 },
{ "gtk_clist_find_row_from_data", _wrap_gtk_clist_find_row_from_data, 1 },
{ "gtk_clist_get_selection_info", _wrap_gtk_clist_get_selection_info, 1 },
+ { "gtk_clist_set_compare_as_int", _wrap_gtk_clist_set_compare_as_int, 1 },
+ { "gtk_clist_reset_compare_func", _wrap_gtk_clist_reset_compare_func, 1 },
{ "gtk_combo_set_popdown_strings", _wrap_gtk_combo_set_popdown_strings,1 },
{ "gtk_curve_get_vector", _wrap_gtk_curve_get_vector, 1 },
{ "gtk_curve_set_vector", _wrap_gtk_curve_set_vector, 1 },
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]