Home | History | Annotate | Download | only in res
      1 ##resource_body = """
      2 ##char *buf;
      3 ##int len;
      4 ##Handle h;
      5 ##
      6 ##if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
      7 ##      return NULL;
      8 ##h = NewHandle(len);
      9 ##if ( h == NULL ) {
     10 ##      PyErr_NoMemory();
     11 ##      return NULL;
     12 ##}
     13 ##HLock(h);
     14 ##memcpy(*h, buf, len);
     15 ##HUnlock(h);
     16 ##_res = ResObj_New(h);
     17 ##return _res;
     18 ##"""
     19 ##
     20 ##f = ManualGenerator("Resource", resource_body)
     21 ##f.docstring = lambda: """Convert a string to a resource object.
     22 ##
     23 ##The created resource object is actually just a handle,
     24 ##apply AddResource() to write it to a resource file.
     25 ##See also the Handle() docstring.
     26 ##"""
     27 ##functions.append(f)
     28 
     29 handle_body = """
     30 char *buf;
     31 int len;
     32 Handle h;
     33 ResourceObject *rv;
     34 
     35 if (!PyArg_ParseTuple(_args, "s#", &buf, &len))
     36         return NULL;
     37 h = NewHandle(len);
     38 if ( h == NULL ) {
     39         PyErr_NoMemory();
     40         return NULL;
     41 }
     42 HLock(h);
     43 memcpy(*h, buf, len);
     44 HUnlock(h);
     45 rv = (ResourceObject *)ResObj_New(h);
     46 rv->ob_freeit = PyMac_AutoDisposeHandle;
     47 _res = (PyObject *)rv;
     48 return _res;
     49 """
     50 
     51 f = ManualGenerator("Handle", handle_body)
     52 f.docstring = lambda: """Convert a string to a Handle object.
     53 
     54 Resource() and Handle() are very similar, but objects created with Handle() are
     55 by default automatically DisposeHandle()d upon object cleanup. Use AutoDispose()
     56 to change this.
     57 """
     58 functions.append(f)
     59 
     60 # Convert resources to other things.
     61 
     62 as_xxx_body = """
     63 _res = %sObj_New((%sHandle)_self->ob_itself);
     64 return _res;
     65 """
     66 
     67 def genresconverter(longname, shortname):
     68 
     69     f = ManualGenerator("as_%s"%longname, as_xxx_body%(shortname, longname))
     70     docstring =  "Return this resource/handle as a %s"%longname
     71     f.docstring = lambda docstring=docstring: docstring
     72     return f
     73 
     74 resmethods.append(genresconverter("Control", "Ctl"))
     75 resmethods.append(genresconverter("Menu", "Menu"))
     76 
     77 # The definition of this one is MacLoadResource, so we do it by hand...
     78 
     79 f = ResMethod(void, 'LoadResource',
     80     (Handle, 'theResource', InMode),
     81 )
     82 resmethods.append(f)
     83 
     84 #
     85 # A method to set the auto-dispose flag
     86 #
     87 AutoDispose_body = """
     88 int onoff, old = 0;
     89 if (!PyArg_ParseTuple(_args, "i", &onoff))
     90         return NULL;
     91 if ( _self->ob_freeit )
     92         old = 1;
     93 if ( onoff )
     94         _self->ob_freeit = PyMac_AutoDisposeHandle;
     95 else
     96         _self->ob_freeit = NULL;
     97 _res = Py_BuildValue("i", old);
     98 return _res;
     99 """
    100 f = ManualGenerator("AutoDispose", AutoDispose_body)
    101 f.docstring = lambda: "(int)->int. Automatically DisposeHandle the object on Python object cleanup"
    102 resmethods.append(f)
    103