@@ -84,6 +84,8 @@ ControlFontStyle_Convert(v, itself)
8484/* TrackControl and HandleControlClick callback support */
8585static PyObject * tracker ;
8686static ControlActionUPP mytracker_upp ;
87+ static ControlUserPaneDrawUPP mydrawproc_upp ;
88+ static ControlUserPaneIdleUPP myidleproc_upp ;
8789
8890extern int settrackfunc (PyObject * ); /* forward */
8991extern void clrtrackfunc (void ); /* forward */
@@ -99,6 +101,7 @@ PyTypeObject Control_Type;
99101typedef struct ControlObject {
100102 PyObject_HEAD
101103 ControlHandle ob_itself ;
104+ PyObject * ob_callbackdict ;
102105} ControlObject ;
103106
104107PyObject * CtlObj_New (itself )
@@ -110,6 +113,7 @@ PyObject *CtlObj_New(itself)
110113 if (it == NULL ) return NULL ;
111114 it -> ob_itself = itself ;
112115 SetControlReference (itself , (long )it );
116+ it -> ob_callbackdict = NULL ;
113117 return (PyObject * )it ;
114118}
115119CtlObj_Convert (v , p_itself )
@@ -128,6 +132,7 @@ CtlObj_Convert(v, p_itself)
128132static void CtlObj_dealloc (self )
129133 ControlObject * self ;
130134{
135+ Py_XDECREF (self -> ob_callbackdict );
131136 if (self -> ob_itself )SetControlReference (self -> ob_itself , (long )0 ); /* Make it forget about us */
132137 PyMem_DEL (self );
133138}
@@ -722,19 +727,18 @@ static PyObject *CtlObj_RemoveControlProperty(_self, _args)
722727 PyObject * _args ;
723728{
724729 PyObject * _res = NULL ;
725- OSStatus _err ;
730+ OSStatus _rv ;
726731 OSType propertyCreator ;
727732 OSType propertyTag ;
728733 if (!PyArg_ParseTuple (_args , "O&O&" ,
729734 PyMac_GetOSType , & propertyCreator ,
730735 PyMac_GetOSType , & propertyTag ))
731736 return NULL ;
732- _err = RemoveControlProperty (_self -> ob_itself ,
733- propertyCreator ,
734- propertyTag );
735- if (_err != noErr ) return PyMac_Error (_err );
736- Py_INCREF (Py_None );
737- _res = Py_None ;
737+ _rv = RemoveControlProperty (_self -> ob_itself ,
738+ propertyCreator ,
739+ propertyTag );
740+ _res = Py_BuildValue ("l" ,
741+ _rv );
738742 return _res ;
739743}
740744
@@ -743,19 +747,18 @@ static PyObject *CtlObj_GetControlRegion(_self, _args)
743747 PyObject * _args ;
744748{
745749 PyObject * _res = NULL ;
746- OSStatus _err ;
750+ OSStatus _rv ;
747751 ControlPartCode inPart ;
748752 RgnHandle outRegion ;
749753 if (!PyArg_ParseTuple (_args , "hO&" ,
750754 & inPart ,
751755 ResObj_Convert , & outRegion ))
752756 return NULL ;
753- _err = GetControlRegion (_self -> ob_itself ,
754- inPart ,
755- outRegion );
756- if (_err != noErr ) return PyMac_Error (_err );
757- Py_INCREF (Py_None );
758- _res = Py_None ;
757+ _rv = GetControlRegion (_self -> ob_itself ,
758+ inPart ,
759+ outRegion );
760+ _res = Py_BuildValue ("l" ,
761+ _rv );
759762 return _res ;
760763}
761764
@@ -1255,6 +1258,39 @@ static PyObject *CtlObj_GetControlDataHandle(_self, _args)
12551258
12561259}
12571260
1261+ static PyObject * CtlObj_SetControlDataCallback (_self , _args )
1262+ ControlObject * _self ;
1263+ PyObject * _args ;
1264+ {
1265+ PyObject * _res = NULL ;
1266+
1267+ OSErr _err ;
1268+ ControlPartCode inPart ;
1269+ ResType inTagName ;
1270+ PyObject * callback ;
1271+ UniversalProcPtr * c_callback ;
1272+
1273+ if (!PyArg_ParseTuple (_args , "hO&O" ,
1274+ & inPart ,
1275+ PyMac_GetOSType , & inTagName ,
1276+ & callback ))
1277+ return NULL ;
1278+
1279+ if ( setcallback (_self , inTagName , callback , & c_callback ) < 0 )
1280+ return NULL ;
1281+ _err = SetControlData (_self -> ob_itself ,
1282+ inPart ,
1283+ inTagName ,
1284+ sizeof (c_callback ),
1285+ (Ptr )& c_callback );
1286+
1287+ if (_err != noErr )
1288+ return PyMac_Error (_err );
1289+ _res = Py_None ;
1290+ return _res ;
1291+
1292+ }
1293+
12581294static PyObject * CtlObj_GetPopupData (_self , _args )
12591295 ControlObject * _self ;
12601296 PyObject * _args ;
@@ -1373,9 +1409,9 @@ static PyMethodDef CtlObj_methods[] = {
13731409 {"IsValidControlHandle" , (PyCFunction )CtlObj_IsValidControlHandle , 1 ,
13741410 "() -> (Boolean _rv)" },
13751411 {"RemoveControlProperty" , (PyCFunction )CtlObj_RemoveControlProperty , 1 ,
1376- "(OSType propertyCreator, OSType propertyTag) -> None " },
1412+ "(OSType propertyCreator, OSType propertyTag) -> (OSStatus _rv) " },
13771413 {"GetControlRegion" , (PyCFunction )CtlObj_GetControlRegion , 1 ,
1378- "(ControlPartCode inPart, RgnHandle outRegion) -> None " },
1414+ "(ControlPartCode inPart, RgnHandle outRegion) -> (OSStatus _rv) " },
13791415 {"GetControlVariant" , (PyCFunction )CtlObj_GetControlVariant , 1 ,
13801416 "() -> (ControlVariant _rv)" },
13811417 {"SetControlReference" , (PyCFunction )CtlObj_SetControlReference , 1 ,
@@ -1420,6 +1456,8 @@ static PyMethodDef CtlObj_methods[] = {
14201456 "(ResObj) -> None" },
14211457 {"GetControlDataHandle" , (PyCFunction )CtlObj_GetControlDataHandle , 1 ,
14221458 "(part, type) -> ResObj" },
1459+ {"SetControlDataCallback" , (PyCFunction )CtlObj_SetControlDataCallback , 1 ,
1460+ "(callbackfunc) -> None" },
14231461 {"GetPopupData" , (PyCFunction )CtlObj_GetPopupData , 1 ,
14241462 NULL },
14251463 {"SetPopupData" , (PyCFunction )CtlObj_SetPopupData , 1 ,
@@ -1912,6 +1950,83 @@ mytracker(ctl, part)
19121950 PySys_WriteStderr ("TrackControl or HandleControlClick: exception in tracker function\n" );
19131951}
19141952
1953+ static int
1954+ setcallback (self , which , callback , uppp )
1955+ ControlObject * self ;
1956+ OSType which ;
1957+ PyObject * callback ;
1958+ UniversalProcPtr * uppp ;
1959+ {
1960+ char keybuf [9 ];
1961+
1962+ if ( which == kControlUserPaneDrawProcTag )
1963+ * uppp = mydrawproc_upp ;
1964+ else if ( which == kControlUserPaneIdleProcTag )
1965+ * uppp = myidleproc_upp ;
1966+ else
1967+ return -1 ;
1968+ /* Only now do we test for clearing of the callback: */
1969+ if ( callback == Py_None )
1970+ * uppp = NULL ;
1971+ /* Create the dict if it doesn't exist yet (so we don't get such a dict for every control) */
1972+ if ( self -> ob_callbackdict == NULL )
1973+ if ( (self -> ob_callbackdict = PyDict_New ()) == NULL )
1974+ return -1 ;
1975+ /* And store the Python callback */
1976+ sprintf (keybuf , "%x" , which );
1977+ if (PyDict_SetItemString (self -> ob_callbackdict , keybuf , callback ) < 0 )
1978+ return -1 ;
1979+ return 0 ;
1980+ }
1981+
1982+ static PyObject *
1983+ callcallback (self , which , arglist )
1984+ ControlObject * self ;
1985+ OSType which ;
1986+ PyObject * arglist ;
1987+ {
1988+ char keybuf [9 ];
1989+ PyObject * func , * rv ;
1990+
1991+ sprintf (keybuf , "%x" , which );
1992+ if ( self -> ob_callbackdict == NULL ||
1993+ (func = PyDict_GetItemString (self -> ob_callbackdict , keybuf )) == NULL ) {
1994+ PySys_WriteStderr ("Control callback without callback object\n" );
1995+ return NULL ;
1996+ }
1997+ rv = PyEval_CallObject (func , arglist );
1998+ if ( rv == NULL )
1999+ PySys_WriteStderr ("Exception in control callback handler\n" );
2000+ return rv ;
2001+ }
2002+
2003+ static pascal void
2004+ mydrawproc (ControlHandle control , SInt16 part )
2005+ {
2006+ ControlObject * ctl_obj ;
2007+ PyObject * arglist , * rv ;
2008+
2009+ ctl_obj = (ControlObject * )CtlObj_WhichControl (control );
2010+ arglist = Py_BuildValue ("Oh" , ctl_obj , part );
2011+ rv = callcallback (ctl_obj , kControlUserPaneDrawProcTag , arglist );
2012+ Py_XDECREF (arglist );
2013+ Py_XDECREF (rv );
2014+ }
2015+
2016+ static pascal void
2017+ myidleproc (ControlHandle control )
2018+ {
2019+ ControlObject * ctl_obj ;
2020+ PyObject * arglist , * rv ;
2021+
2022+ ctl_obj = (ControlObject * )CtlObj_WhichControl (control );
2023+ arglist = Py_BuildValue ("O" , ctl_obj );
2024+ rv = callcallback (ctl_obj , kControlUserPaneIdleProcTag , arglist );
2025+ Py_XDECREF (arglist );
2026+ Py_XDECREF (rv );
2027+ }
2028+
2029+
19152030
19162031void initCtl ()
19172032{
@@ -1921,6 +2036,8 @@ void initCtl()
19212036
19222037
19232038 mytracker_upp = NewControlActionProc (mytracker );
2039+ mydrawproc_upp = NewControlUserPaneDrawProc (mydrawproc );
2040+ myidleproc_upp = NewControlUserPaneDrawProc (myidleproc );
19242041
19252042
19262043 m = Py_InitModule ("Ctl" , Ctl_methods );
0 commit comments