IUP

Check-in [41857c1bf7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Nicer Python property handling
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | python
Files: files | file ages | folders
SHA1: 41857c1bf784ce9d24d6e2fb7ebd7de6700dcdee
User & Date: murphy 2011-04-13 00:13:56
Context
2011-04-13
00:13
Nicer Python property handling Leaf check-in: 41857c1bf7 user: murphy tags: python
2011-04-12
20:05
Started Python version of the binding code check-in: cb38b0fdf0 user: murphy tags: python
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to python/iup.pyx.

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
130
131
132
133
134
135
136

137
138
139
140
141
142
143
144
145
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
...
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
...
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
...
332
333
334
335
336
337
338
339
340
341


342
343
344
345
346
347
348
...
350
351
352
353
354
355
356

357
358
359
360
361
362
363
364

365


366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    cdef char *status
    if isinstance(data, str): data = data.encode()
    status = IupLoadBuffer(data)
    if status: raise IupError(ERROR, status.strip().decode())


cdef void __frame_callback(void *data, va_alist frame) with gil:
    (<Callback>data).__call_with_frame__(frame)

cdef class Callback:
    '''Callback invocation wrapper.'''
    
    cdef void *_cb
    cdef readonly object format
    cdef public object target
    
    def __cinit__(self):
................................................................................
    def __init__(self, format):
        if not format:
            format = b'h=i'
        elif len(format) < 2 or format[-2] != ord(b'='):
            format += b'=i'
        self.format = format
    

    def __address__(self):
        '''Returns the memory address of the C callback entry point.'''
        return <unsigned long>self._cb
    
    def __call__(self, *args):
        '''Performs a Python call to the callback.'''
        return self.target(*args)
    
    cdef __call_with_frame__(self, va_alist frame):
................................................................................
        elif ret == ord(b'f'):
            va_return_float(frame, result)
        elif ret == ord(b'd'):
            va_return_double(frame, result)
        elif ret == ord(b'v'):
            va_return_voidp(frame, <void *><unsigned long?>result)
        elif ret == ord(b'h'):
            va_return_voidp(frame, <void *><unsigned long?>result.__address__())


cdef class BaseHandle:
    '''Base class of IUP attribute containers.'''
    
    cdef Ihandle *_ih
    cdef object _registry
    
    def __cinit__(self):
        self._ih = NULL
................................................................................
        cdef unsigned long address
        if isinstance(value, str):
            value = value.encode()
            IupStoreAttribute(self._ih, name, value)
        elif isinstance(value, bytes):
            IupStoreAttribute(self._ih, name, value)
        elif isinstance(value, Handle):
            address = value.__address__()
            IupSetAttributeHandle(self._ih, name, <Ihandle *>address)
        elif isinstance(value, bool) or value is None:
            if value:
                value = b'YES'
            else:
                value = b'NO'
            IupStoreAttribute(self._ih, name, value)
        else:
            value = str(value).encode()
            IupStoreAttribute(self._ih, name, value)
    
    cdef object _get_callback(self, char *name, object table):
        cdef object callback = None
        cdef unsigned long address
        if table:
            address = <unsigned long>IupGetCallback(self._ih, name)
            if address in table: callback = table[address]
        return callback
    
    cdef void _set_callback(self, char *name, object table, object target):
        cdef Callback callback
        cdef unsigned long address = <unsigned long>IupGetCallback(self._ih, name)
        if address in table:
            callback = table[address]
        else:
            callback = Callback(iupClassCallbackGetFormat(self._ih.iclass, name))
            address = callback.__address__()
            table[address] = callback
            IupSetCallback(self._ih, name, <void *>address)
        callback.target = target


@cython.final
cdef class GlobalHandle(BaseHandle):
    '''Class of the global attribute container.'''
    
    def __getattr__(self, name):
        '''Retrieves a callback or attribute.'''
        if isinstance(name, str): name = name.encode()
        return \
            self._get_callback(name, self._registry) or \
................................................................................
        '''Sets a callback or attribute.'''
        if isinstance(name, str): name = name.encode()
        if callable(value):
            self._set_callback(name, self._registry, value)
        else:
            self._set_attribute(name, value)

IUP_GLOBAL = GlobalHandle()


CENTER       = 0xFFFF
LEFT         = 0xFFFE
RIGHT        = 0xFFFD
MOUSEPOS     = 0xFFFC
CURRENT      = 0xFFFB
CENTERPARENT = 0xFFFA
TOP          = LEFT
BOTTOM       = RIGHT

cdef class Handle(BaseHandle):
    '''Class of IUP handles.'''
    
    def __init__(self, unsigned long address):
        '''Wraps a memory address into a new handle.'''
        if not address: raise ValueError('Handle may not be NULL')
        self._ih = <Ihandle *>address
        Py_INCREF(self)
................................................................................
    def create(Self, name, **attrs):
        '''Wraps a new instance of an IUP object into a handle.'''
        cdef unsigned long address
        cdef Handle self
        if isinstance(name, str): name = name.encode()
        
        address = <unsigned long>IupCreate(name)
        if not address:
            raise IupError(ERROR, 'Failed to create {}'.format(name))
        self = Self(address)


        
        try:
            for name, value in attrs.items(): self.__setattr__(name, value)
        except:
            self.destroy()
            raise
        
................................................................................
    
    @classmethod
    def by_name(Self, name):
        '''Retrieves an IUP object instance by name.'''
        if isinstance(name, str): name = name.encode()
        return Self.wrap(<unsigned long>IupGetHandle(name))
    

    def __address__(self):
        '''Returns the memory address of the underlying object.'''
        return <unsigned long>self._ih

    def _check_destroyed(self):
        '''Ensures that the underlying object is still alive.'''
        if not self._ih: raise ReferenceError('Handle is destroyed')
    

    cdef object _get_name(self):


        cdef char *name = IupGetName(self._ih)
        if name:
            return name.decode()
        else:
            return None
    
    cdef _set_name(self, object name):
        cdef char *ptr
        if isinstance(name, str):
            name = name.encode()
            ptr = name
        elif isinstance(name, bytes):
            ptr = name
        else:
            name = str(name).encode()
            ptr = name
        IupSetHandle(ptr, self._ih)
    
    def _class_name(self):
        return IupGetClassName(self._ih).decode()
    
    def _class_type(self):
        return IupGetClassType(self._ih).decode()
    
    def __getattr__(self, name):
        '''Retrieves a name, callback or attribute.'''
        self._check_destroyed()
        if isinstance(name, str): name = name.encode()
        if name == b'name':
            return self._get_name()
        elif name == b'class_name':
            return self._class_name()
        elif name == b'class_type':
            return self._class_type()
        else:
            return \
                self._get_callback(name, self._registry) or \
                self._get_attribute(name)
    
    def __setattr__(self, name, value):
        '''Sets a name, callback or attribute.'''
        self._check_destroyed()
        if isinstance(name, str): name = name.encode()
        if name == b'name':
            self._set_name(value)
        else:
            if callable(value):
                self._set_callback(name, self._registry, value)
            else:
                self._set_attribute(name, value)
    
    def save_attributes(self):
        '''Ensures that attributes stored in native resources are mirrorred.'''
        self._check_destroyed()
        IupSaveClassAttributes(self._ih)
    
    def show(self, modal = False, x = CURRENT, y = CURRENT):







|

|







 







>

|







 







|


|







 







|












|



|
|


|




|
|






|







 







|











|







 







|
<
|
>
>







 







>

|





|
>
|
>
>






|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|



|


<
<
<
<
<
<
<
|
|
|







<
|
|
|
|







106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
...
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
...
333
334
335
336
337
338
339
340

341
342
343
344
345
346
347
348
349
350
...
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399







400
401
402
403
404
405
406
407
408
409

410
411
412
413
414
415
416
417
418
419
420
    cdef char *status
    if isinstance(data, str): data = data.encode()
    status = IupLoadBuffer(data)
    if status: raise IupError(ERROR, status.strip().decode())


cdef void __frame_callback(void *data, va_alist frame) with gil:
    (<_Callback>data).__call_with_frame__(frame)

cdef class _Callback:
    '''Callback invocation wrapper.'''
    
    cdef void *_cb
    cdef readonly object format
    cdef public object target
    
    def __cinit__(self):
................................................................................
    def __init__(self, format):
        if not format:
            format = b'h=i'
        elif len(format) < 2 or format[-2] != ord(b'='):
            format += b'=i'
        self.format = format
    
    @property
    def __address__(self):
        '''The memory address of the C callback entry point.'''
        return <unsigned long>self._cb
    
    def __call__(self, *args):
        '''Performs a Python call to the callback.'''
        return self.target(*args)
    
    cdef __call_with_frame__(self, va_alist frame):
................................................................................
        elif ret == ord(b'f'):
            va_return_float(frame, result)
        elif ret == ord(b'd'):
            va_return_double(frame, result)
        elif ret == ord(b'v'):
            va_return_voidp(frame, <void *><unsigned long?>result)
        elif ret == ord(b'h'):
            va_return_voidp(frame, <void *><unsigned long?>result.__address__)


cdef class _BaseHandle:
    '''Base class of IUP attribute containers.'''
    
    cdef Ihandle *_ih
    cdef object _registry
    
    def __cinit__(self):
        self._ih = NULL
................................................................................
        cdef unsigned long address
        if isinstance(value, str):
            value = value.encode()
            IupStoreAttribute(self._ih, name, value)
        elif isinstance(value, bytes):
            IupStoreAttribute(self._ih, name, value)
        elif isinstance(value, Handle):
            address = value.__address__
            IupSetAttributeHandle(self._ih, name, <Ihandle *>address)
        elif isinstance(value, bool) or value is None:
            if value:
                value = b'YES'
            else:
                value = b'NO'
            IupStoreAttribute(self._ih, name, value)
        else:
            value = str(value).encode()
            IupStoreAttribute(self._ih, name, value)
    
    cdef object _get_callback(self, char *name, object table):
        cdef object target = None
        cdef unsigned long address
        if table:
            address = <unsigned long>IupGetCallback(self._ih, name)
            if address in table: target = table[address].target
        return target
    
    cdef void _set_callback(self, char *name, object table, object target):
        cdef _Callback callback
        cdef unsigned long address = <unsigned long>IupGetCallback(self._ih, name)
        if address in table:
            callback = table[address]
        else:
            callback = _Callback(iupClassCallbackGetFormat(self._ih.iclass, name))
            address = callback.__address__
            table[address] = callback
            IupSetCallback(self._ih, name, <void *>address)
        callback.target = target


@cython.final
cdef class _GlobalHandle(_BaseHandle):
    '''Class of the global attribute container.'''
    
    def __getattr__(self, name):
        '''Retrieves a callback or attribute.'''
        if isinstance(name, str): name = name.encode()
        return \
            self._get_callback(name, self._registry) or \
................................................................................
        '''Sets a callback or attribute.'''
        if isinstance(name, str): name = name.encode()
        if callable(value):
            self._set_callback(name, self._registry, value)
        else:
            self._set_attribute(name, value)

IUP_GLOBAL = _GlobalHandle()


CENTER       = 0xFFFF
LEFT         = 0xFFFE
RIGHT        = 0xFFFD
MOUSEPOS     = 0xFFFC
CURRENT      = 0xFFFB
CENTERPARENT = 0xFFFA
TOP          = LEFT
BOTTOM       = RIGHT

cdef class Handle(_BaseHandle):
    '''Class of IUP handles.'''
    
    def __init__(self, unsigned long address):
        '''Wraps a memory address into a new handle.'''
        if not address: raise ValueError('Handle may not be NULL')
        self._ih = <Ihandle *>address
        Py_INCREF(self)
................................................................................
    def create(Self, name, **attrs):
        '''Wraps a new instance of an IUP object into a handle.'''
        cdef unsigned long address
        cdef Handle self
        if isinstance(name, str): name = name.encode()
        
        address = <unsigned long>IupCreate(name)
        if address:

            self = Self(address)
        else:
            raise IupError(ERROR, 'Failed to create {}'.format(name))
        
        try:
            for name, value in attrs.items(): self.__setattr__(name, value)
        except:
            self.destroy()
            raise
        
................................................................................
    
    @classmethod
    def by_name(Self, name):
        '''Retrieves an IUP object instance by name.'''
        if isinstance(name, str): name = name.encode()
        return Self.wrap(<unsigned long>IupGetHandle(name))
    
    @property
    def __address__(self):
        '''The memory address of the underlying object.'''
        return <unsigned long>self._ih

    def _check_destroyed(self):
        '''Ensures that the underlying object is still alive.'''
        if not self._ih: raise ReferenceError('Handle is destroyed')

    @property
    def name(self):
        '''Global name for the handle.'''
        self._check_destroyed()
        cdef char *name = IupGetName(self._ih)
        if name:
            return name.decode()
        else:
            return None
    
    cdef void _set_name(self, object value):
        cdef char *name = IupGetName(self._ih)
        if isinstance(value, str): value = value.encode()
        if name: IupSetHandle(name, NULL)
        IupSetHandle(value, self._ih)
    
    @property
    def class_name(self):
        '''Name of the handle's IUP object class.'''
        self._check_destroyed()
        return IupGetClassName(self._ih).decode()
    
    @property
    def class_type(self):
        '''Name of the handle's IUP native resource type.'''
        self._check_destroyed()
        return IupGetClassType(self._ih).decode()
    
    def __getattr__(self, name):
        '''Retrieves a callback or attribute.'''
        self._check_destroyed()
        if isinstance(name, str): name = name.encode()







        return \
            self._get_callback(name, self._registry) or \
            self._get_attribute(name)
    
    def __setattr__(self, name, value):
        '''Sets a name, callback or attribute.'''
        self._check_destroyed()
        if isinstance(name, str): name = name.encode()
        if name == b'name':
            self._set_name(value)

        elif callable(value):
            self._set_callback(name, self._registry, value)
        else:
            self._set_attribute(name, value)
    
    def save_attributes(self):
        '''Ensures that attributes stored in native resources are mirrorred.'''
        self._check_destroyed()
        IupSaveClassAttributes(self._ih)
    
    def show(self, modal = False, x = CURRENT, y = CURRENT):