"J.W. Bizzaro" <[EMAIL PROTECTED]> writes:
> Bernhard,
>
> Do you ever figure out how to do it? Inquiring minds want to know :-)
Well, to quickly get a working solution, I just wrote a C-function for
my _sketch module, which already had some other functions that access
GTK from C:
#define OFF(x) offsetof(GtkAdjustment, x)
static struct memberlist adjustment_memberlist[] = {
{"lower", T_FLOAT, OFF(lower)},
{"upper", T_FLOAT, OFF(upper)},
{"value", T_FLOAT, OFF(value)},
{"step_increment", T_FLOAT, OFF(step_increment)},
{"page_increment", T_FLOAT, OFF(page_increment)},
{"page_size", T_FLOAT, OFF(page_size)},
{NULL}
};
PyObject *
skgtk_set_adjustment(PyObject * self, PyObject * args)
{
PyObject *adjustment, *dict, *key, *value;
int pos;
if (!PyArg_ParseTuple(args, "O!O!", PyGtk_Type, &adjustment,
&PyDict_Type, &dict))
return NULL;
pos = 0;
while (PyDict_Next(dict, &pos, &key, &value))
{
if (PyString_Check(key) && PyNumber_Check(value))
{
if (PyMember_Set((char*)(GTK_ADJUSTMENT(PyGtk_Get(adjustment))),
adjustment_memberlist, PyString_AsString(key),
value) == -1)
return NULL;
}
else
{
PyErr_SetString(PyExc_TypeError,
"dict with string keys and number values required");
return NULL;
}
}
Py_INCREF(Py_None);
return Py_None;
}
and a small python wrapper:
def modify_adjustment(adjustment, **kw):
_sketch.set_adjustment(adjustment._o, kw)
adjustment.changed()
I haven't bothered yet to add this functionality to pygtk and I'm not
sure what the best way to do this would actually be. I see several ways:
1) Extend generate.py to generate *_set_* functions in addition to the
*_get_* functions. This wouldn't be too difficult, I think. Just add a
set_attr_func method to FunctionDefsParser and modify its define_object
accordingly. There would have to be a method to define which of
attributes are actually writable, though.
2) Use the function above, or perhaps have a adjustment specific
setattr-function used like this: adjustment_setattr('lower', -100.0)
3) Define a C-level adjustment object and implement its setattr and
getattr methods.
I have to admit that I don't like 1). With that scheme there are lots of
little functions, one for each attribute. 3) wouldn't be very good
either, especially if that method had to be applied to other GTK objects
as well.
I'm in favor of 2). The adjustment_setattr variant would make the python
wrapper in gtk.py very simple. E.g. use the __setitem__ method.
Alternatively, GtkAdjustment could extend the set() method.
It's up to James to decide which way to go. Needless to say, I'd be
willing to implement this.
On a related issue:
The implementation of the __getattr__ methods is a bit inefficient,
don't you think:
def __getattr__(self, attr):
attrs = {
'lower': _gtk.gtk_adjustment_get_lower,
'upper': _gtk.gtk_adjustment_get_upper,
'value': _gtk.gtk_adjustment_get_value,
'step_increment': _gtk.gtk_adjustment_get_step_increment,
'page_increment': _gtk.gtk_adjustment_get_page_increment,
'page_size': _gtk.gtk_adjustment_get_page_size
}
if attr in attrs.keys():
return attrs[attr](self._o)
return GtkData.__getattr__(self, attr)
Constructing the dictionary *every time* kills most of the advantage
using a dict gives you in the first place, IMHO. Especially if you use
'attr in attrs.keys()'... (the other __getattr__ methods use has_key for
this, though)
I'd move the dict to the class level and implement a generic __getattr__
in GtkObject.
I hope that wasn't too harsh. If James agrees, I'll make the necessary
changes.
Bernhard
--
Bernhard Herzog | Sketch, a python based drawing program
[EMAIL PROTECTED] | http://www.online.de/home/sketch/
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]