symbian-qemu-0.9.1-12/python-2.6.1/Mac/Modules/ctl/ctlsupport.py
changeset 1 2fb8b9db1c86
equal deleted inserted replaced
0:ffa851df0825 1:2fb8b9db1c86
       
     1 # This script generates a Python interface for an Apple Macintosh Manager.
       
     2 # It uses the "bgen" package to generate C code.
       
     3 # The function specifications are generated by scanning the mamager's header file,
       
     4 # using the "scantools" package (customized for this particular manager).
       
     5 
       
     6 import string
       
     7 
       
     8 # Declarations that change for each manager
       
     9 MACHEADERFILE = 'Controls.h'            # The Apple header file
       
    10 MODNAME = '_Ctl'                                # The name of the module
       
    11 OBJECTNAME = 'Control'                  # The basic name of the objects used here
       
    12 
       
    13 # The following is *usually* unchanged but may still require tuning
       
    14 MODPREFIX = 'Ctl'                       # The prefix for module-wide routines
       
    15 OBJECTTYPE = OBJECTNAME + 'Handle'      # The C type used to represent them
       
    16 OBJECTPREFIX = MODPREFIX + 'Obj'        # The prefix for object methods
       
    17 INPUTFILE = string.lower(MODPREFIX) + 'gen.py' # The file generated by the scanner
       
    18 OUTPUTFILE = MODNAME + "module.c"       # The file generated by this program
       
    19 
       
    20 from macsupport import *
       
    21 
       
    22 # Create the type objects
       
    23 
       
    24 ControlHandle = OpaqueByValueType(OBJECTTYPE, OBJECTPREFIX)
       
    25 ControlRef = ControlHandle
       
    26 ExistingControlHandle = OpaqueByValueType(OBJECTTYPE, "CtlObj_WhichControl", "BUG")
       
    27 
       
    28 RgnHandle = OpaqueByValueType("RgnHandle", "ResObj")
       
    29 CCTabHandle = OpaqueByValueType("CCTabHandle", "ResObj")
       
    30 AuxCtlHandle = OpaqueByValueType("AuxCtlHandle", "ResObj")
       
    31 ControlPartCode = Type("ControlPartCode", "h")
       
    32 DragConstraint = Type("DragConstraint", "H")
       
    33 ControlVariant = Type("ControlVariant", "h")
       
    34 IconTransformType = Type("IconTransformType", "h")
       
    35 EventModifiers = Type("EventModifiers", "H")
       
    36 ClickActivationResult = Type("ClickActivationResult", "l")
       
    37 ControlButtonGraphicAlignment = Type("ControlButtonGraphicAlignment", "h")
       
    38 ControlButtonTextAlignment = Type("ControlButtonTextAlignment", "h")
       
    39 ControlButtonTextPlacement = Type("ControlButtonTextPlacement", "h")
       
    40 ControlContentType = Type("ControlContentType", "h")
       
    41 ControlFocusPart = Type("ControlFocusPart", "h")
       
    42 
       
    43 ControlFontStyleRec = OpaqueType('ControlFontStyleRec', 'ControlFontStyle')
       
    44 ControlFontStyleRec_ptr = ControlFontStyleRec
       
    45 ControlID = OpaqueType('ControlID', 'PyControlID')
       
    46 ControlID_ptr = ControlID
       
    47 
       
    48 DragTrackingMessage = Type("DragTrackingMessage", "h")
       
    49 DragReference = OpaqueByValueType("DragReference", "DragObj")
       
    50 
       
    51 CFStringRef = OpaqueByValueType("CFStringRef", "CFStringRefObj")
       
    52 CFMutableStringRef = OpaqueByValueType("CFMutableStringRef", "CFMutableStringRefObj")
       
    53 CFDataRef = OpaqueByValueType("CFDataRef", "CFDataRefObj")
       
    54 
       
    55 ControlTabSize = UInt16
       
    56 ControlTabDirection = UInt16
       
    57 ControlPopupArrowOrientation = UInt16
       
    58 ControlPopupArrowSize = UInt16
       
    59 ControlClockType = UInt16
       
    60 ControlClockFlags = UInt32
       
    61 ControlRoundButtonSize = SInt16
       
    62 DataBrowserViewStyle = OSType
       
    63 DataBrowserItemID = UInt32
       
    64 DataBrowserEditCommand = UInt32
       
    65 DataBrowserSelectionAnchorDirection = UInt32
       
    66 DataBrowserItemState = UInt32
       
    67 DataBrowserPropertyID = UInt32
       
    68 DataBrowserRevealOptions = UInt8
       
    69 DataBrowserSortOrder = UInt16
       
    70 DataBrowserSelectionFlags = UInt32
       
    71 DataBrowserPropertyFlags = UInt32
       
    72 DataBrowserPropertyPart = OSType
       
    73 DataBrowserTableViewColumnID = DataBrowserPropertyID
       
    74 #DataBrowserTableViewColumnDesc = DataBrowserPropertyDesc
       
    75 DataBrowserTableViewHiliteStyle = UInt32
       
    76 DataBrowserTableViewRowIndex = UInt32
       
    77 DataBrowserTableViewColumnIndex = UInt32
       
    78 DataBrowserPropertyType = OSType
       
    79 ControlDisclosureTriangleOrientation = UInt16
       
    80 
       
    81 DataBrowserTableViewColumnDesc = OpaqueType("DataBrowserTableViewColumnDesc",
       
    82                 "DataBrowserTableViewColumnDesc")
       
    83 DataBrowserListViewColumnDesc = OpaqueType("DataBrowserListViewColumnDesc",
       
    84                 "DataBrowserListViewColumnDesc")
       
    85 ControlButtonContentInfo = OpaqueType("ControlButtonContentInfo",
       
    86                 "ControlButtonContentInfo")
       
    87 ControlButtonContentInfoPtr = ControlButtonContentInfo_ptr = ControlButtonContentInfo
       
    88 
       
    89 ControlTabEntry_ptr = OpaqueType("ControlTabEntry", "ControlTabEntry")
       
    90 
       
    91 ControlBevelThickness = UInt16
       
    92 ControlBevelButtonBehavior = UInt16
       
    93 ControlBevelButtonMenuBehavior = UInt16
       
    94 ControlBevelButtonMenuPlacement = UInt16
       
    95 ControlPushButtonIconAlignment = UInt16
       
    96 
       
    97 class ControlActionDefinition(Type):
       
    98     def declare(self, name):
       
    99         Output("%s %s;", self.typeName, name)
       
   100         Output("UniversalProcPtr c_callback;")
       
   101     def passInput(self, name):
       
   102         return "myactionproc_upp"
       
   103     def cleanup(self, name):
       
   104         Output("setcallback((PyObject*)_self, kMyControlActionProcTag, actionProc, &c_callback);")
       
   105 
       
   106 class ControlActionDefinitionNewControl(ControlActionDefinition):
       
   107     def cleanup(self, name):
       
   108         Output("setcallback(_res, kMyControlActionProcTag, liveTrackingProc, &c_callback);")
       
   109 
       
   110 ControlActionUPP = ControlActionDefinition("PyObject*", "O")
       
   111 ControlActionUPPNewControl = ControlActionDefinitionNewControl("PyObject*", "O")
       
   112 ControlSliderOrientation = UInt16
       
   113 
       
   114 
       
   115 includestuff = includestuff + """
       
   116 #include <Carbon/Carbon.h>
       
   117 
       
   118 #ifdef USE_TOOLBOX_OBJECT_GLUE
       
   119 extern PyObject *_CtlObj_New(ControlHandle);
       
   120 extern int _CtlObj_Convert(PyObject *, ControlHandle *);
       
   121 
       
   122 #define CtlObj_New _CtlObj_New
       
   123 #define CtlObj_Convert _CtlObj_Convert
       
   124 #endif
       
   125 
       
   126 static PyObject *CtlObj_WhichControl(ControlHandle);
       
   127 
       
   128 #define as_Control(h) ((ControlHandle)h)
       
   129 #define as_Resource(ctl) ((Handle)ctl)
       
   130 #define GetControlRect(ctl, rectp) GetControlBounds(ctl, rectp)
       
   131 
       
   132 #define MAXTABS 32  /* maximum number of tabs that we support in a tabs control */
       
   133 /*
       
   134 ** Parse/generate ControlFontStyleRec records
       
   135 */
       
   136 #if 0 /* Not needed */
       
   137 static PyObject *
       
   138 ControlFontStyle_New(ControlFontStyleRec *itself)
       
   139 {
       
   140 
       
   141         return Py_BuildValue("hhhhhhO&O&", itself->flags, itself->font,
       
   142                 itself->size, itself->style, itself->mode, itself->just,
       
   143                 QdRGB_New, &itself->foreColor, QdRGB_New, &itself->backColor);
       
   144 }
       
   145 #endif
       
   146 
       
   147 static int
       
   148 ControlFontStyle_Convert(PyObject *v, ControlFontStyleRec *itself)
       
   149 {
       
   150         return PyArg_Parse(v, "(hhhhhhO&O&)", &itself->flags,
       
   151                 &itself->font, &itself->size, &itself->style, &itself->mode,
       
   152                 &itself->just, QdRGB_Convert, &itself->foreColor,
       
   153                 QdRGB_Convert, &itself->backColor);
       
   154 }
       
   155 
       
   156 /*
       
   157 ** Parse/generate ControlID records
       
   158 */
       
   159 static PyObject *
       
   160 PyControlID_New(ControlID *itself)
       
   161 {
       
   162 
       
   163         return Py_BuildValue("O&l", PyMac_BuildOSType, itself->signature, itself->id);
       
   164 }
       
   165 
       
   166 static int
       
   167 PyControlID_Convert(PyObject *v, ControlID *itself)
       
   168 {
       
   169         return PyArg_Parse(v, "(O&l)", PyMac_GetOSType, &itself->signature, &itself->id);
       
   170 }
       
   171 
       
   172 /*
       
   173 ** generate DataBrowserListViewColumnDesc records
       
   174 */
       
   175 static int
       
   176 DataBrowserTableViewColumnDesc_Convert(PyObject *v, DataBrowserTableViewColumnDesc *itself)
       
   177 {
       
   178         return PyArg_Parse(v, "(lO&l)",
       
   179                            &itself->propertyID,
       
   180                            PyMac_GetOSType, &itself->propertyType,
       
   181                            &itself->propertyFlags);
       
   182 }
       
   183 
       
   184 static int
       
   185 ControlButtonContentInfo_Convert(PyObject *v, ControlButtonContentInfo *itself)
       
   186 {
       
   187         return PyArg_Parse(v, "(hO&)",
       
   188                            &itself->contentType,
       
   189                            OptResObj_Convert, &itself->u.iconSuite);
       
   190 }
       
   191 
       
   192 static int
       
   193 DataBrowserListViewHeaderDesc_Convert(PyObject *v, DataBrowserListViewHeaderDesc *itself)
       
   194 {
       
   195         itself->version = kDataBrowserListViewLatestHeaderDesc;
       
   196         return PyArg_Parse(v, "(HHhO&HO&O&)",
       
   197                            &itself->minimumWidth,
       
   198                            &itself->maximumWidth,
       
   199                            &itself->titleOffset,
       
   200                            CFStringRefObj_Convert, &itself->titleString,
       
   201                            &itself->initialOrder,
       
   202                            ControlFontStyle_Convert, &itself->btnFontStyle,
       
   203                            ControlButtonContentInfo_Convert, &itself->btnContentInfo);
       
   204 }
       
   205 
       
   206 static int
       
   207 DataBrowserListViewColumnDesc_Convert(PyObject *v, DataBrowserListViewColumnDesc *itself)
       
   208 {
       
   209         return PyArg_Parse(v, "(O&O&)",
       
   210                            DataBrowserTableViewColumnDesc_Convert, &itself->propertyDesc,
       
   211                            DataBrowserListViewHeaderDesc_Convert, &itself->headerBtnDesc);
       
   212 }
       
   213 
       
   214 /* TrackControl and HandleControlClick callback support */
       
   215 #define kMyControlActionProcTag 'ACTN'  /* not an official tag, only for internal use */
       
   216 static PyObject *tracker;
       
   217 static ControlActionUPP mytracker_upp;
       
   218 static ControlActionUPP myactionproc_upp;
       
   219 static ControlUserPaneKeyDownUPP mykeydownproc_upp;
       
   220 static ControlUserPaneFocusUPP myfocusproc_upp;
       
   221 static ControlUserPaneDrawUPP mydrawproc_upp;
       
   222 static ControlUserPaneIdleUPP myidleproc_upp;
       
   223 static ControlUserPaneHitTestUPP myhittestproc_upp;
       
   224 static ControlUserPaneTrackingUPP mytrackingproc_upp;
       
   225 
       
   226 static int settrackfunc(PyObject *);    /* forward */
       
   227 static void clrtrackfunc(void); /* forward */
       
   228 static int setcallback(PyObject *, OSType, PyObject *, UniversalProcPtr *);
       
   229 """
       
   230 
       
   231 finalstuff = finalstuff + """
       
   232 static PyObject *
       
   233 CtlObj_NewUnmanaged(ControlHandle itself)
       
   234 {
       
   235         ControlObject *it;
       
   236         if (itself == NULL) return PyMac_Error(resNotFound);
       
   237         it = PyObject_NEW(ControlObject, &Control_Type);
       
   238         if (it == NULL) return NULL;
       
   239         it->ob_itself = itself;
       
   240         it->ob_callbackdict = NULL;
       
   241         return (PyObject *)it;
       
   242 }
       
   243 
       
   244 static PyObject *
       
   245 CtlObj_WhichControl(ControlHandle c)
       
   246 {
       
   247         PyObject *it;
       
   248 
       
   249         if (c == NULL)
       
   250                 it = Py_None;
       
   251         else {
       
   252                 it = (PyObject *) GetControlReference(c);
       
   253                 /*
       
   254                 ** If the refcon is zero or doesn't point back to the Python object
       
   255                 ** the control is not ours. Return a temporary object.
       
   256                 */
       
   257                 if (it == NULL || ((ControlObject *)it)->ob_itself != c)
       
   258                         return CtlObj_NewUnmanaged(c);
       
   259         }
       
   260         Py_INCREF(it);
       
   261         return it;
       
   262 }
       
   263 
       
   264 static int
       
   265 settrackfunc(PyObject *obj)
       
   266 {
       
   267         if (tracker) {
       
   268                 PyErr_SetString(Ctl_Error, "Tracker function in use");
       
   269                 return 0;
       
   270         }
       
   271         tracker = obj;
       
   272         Py_INCREF(tracker);
       
   273         return 1;
       
   274 }
       
   275 
       
   276 static void
       
   277 clrtrackfunc(void)
       
   278 {
       
   279         Py_XDECREF(tracker);
       
   280         tracker = 0;
       
   281 }
       
   282 
       
   283 static pascal void
       
   284 mytracker(ControlHandle ctl, short part)
       
   285 {
       
   286         PyObject *args, *rv=0;
       
   287 
       
   288         args = Py_BuildValue("(O&i)", CtlObj_WhichControl, ctl, (int)part);
       
   289         if (args && tracker) {
       
   290                 rv = PyEval_CallObject(tracker, args);
       
   291                 Py_DECREF(args);
       
   292         }
       
   293         if (rv)
       
   294                 Py_DECREF(rv);
       
   295         else {
       
   296                 PySys_WriteStderr("TrackControl or HandleControlClick: exception in tracker function\\n");
       
   297                 PyErr_Print();
       
   298         }
       
   299 }
       
   300 
       
   301 static int
       
   302 setcallback(PyObject *myself, OSType which, PyObject *callback, UniversalProcPtr *uppp)
       
   303 {
       
   304         ControlObject *self = (ControlObject *)myself;
       
   305         char keybuf[9];
       
   306 
       
   307         if ( which == kMyControlActionProcTag )
       
   308                 *uppp = (UniversalProcPtr)myactionproc_upp;
       
   309         else if ( which == kControlUserPaneKeyDownProcTag )
       
   310                 *uppp = (UniversalProcPtr)mykeydownproc_upp;
       
   311         else if ( which == kControlUserPaneFocusProcTag )
       
   312                 *uppp = (UniversalProcPtr)myfocusproc_upp;
       
   313         else if ( which == kControlUserPaneDrawProcTag )
       
   314                 *uppp = (UniversalProcPtr)mydrawproc_upp;
       
   315         else if ( which == kControlUserPaneIdleProcTag )
       
   316                 *uppp = (UniversalProcPtr)myidleproc_upp;
       
   317         else if ( which == kControlUserPaneHitTestProcTag )
       
   318                 *uppp = (UniversalProcPtr)myhittestproc_upp;
       
   319         else if ( which == kControlUserPaneTrackingProcTag )
       
   320                 *uppp = (UniversalProcPtr)mytrackingproc_upp;
       
   321         else
       
   322                 return -1;
       
   323         /* Only now do we test for clearing of the callback: */
       
   324         if ( callback == Py_None )
       
   325                 *uppp = NULL;
       
   326         /* Create the dict if it doesn't exist yet (so we don't get such a dict for every control) */
       
   327         if ( self->ob_callbackdict == NULL )
       
   328                 if ( (self->ob_callbackdict = PyDict_New()) == NULL )
       
   329                         return -1;
       
   330         /* And store the Python callback */
       
   331         sprintf(keybuf, "%x", (unsigned)which);
       
   332         if (PyDict_SetItemString(self->ob_callbackdict, keybuf, callback) < 0)
       
   333                 return -1;
       
   334         return 0;
       
   335 }
       
   336 
       
   337 static PyObject *
       
   338 callcallback(ControlObject *self, OSType which, PyObject *arglist)
       
   339 {
       
   340         char keybuf[9];
       
   341         PyObject *func, *rv;
       
   342 
       
   343         sprintf(keybuf, "%x", (unsigned)which);
       
   344         if ( self->ob_callbackdict == NULL ||
       
   345                         (func = PyDict_GetItemString(self->ob_callbackdict, keybuf)) == NULL ) {
       
   346                 PySys_WriteStderr("Control callback %x without callback object\\n", (unsigned)which);
       
   347                 return NULL;
       
   348         }
       
   349         rv = PyEval_CallObject(func, arglist);
       
   350         if ( rv == NULL ) {
       
   351                 PySys_WriteStderr("Exception in control callback %x handler\\n", (unsigned)which);
       
   352                 PyErr_Print();
       
   353         }
       
   354         return rv;
       
   355 }
       
   356 
       
   357 static pascal void
       
   358 myactionproc(ControlHandle control, SInt16 part)
       
   359 {
       
   360         ControlObject *ctl_obj;
       
   361         PyObject *arglist, *rv;
       
   362 
       
   363         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   364         arglist = Py_BuildValue("Oh", ctl_obj, part);
       
   365         rv = callcallback(ctl_obj, kMyControlActionProcTag, arglist);
       
   366         Py_XDECREF(arglist);
       
   367         Py_XDECREF(rv);
       
   368 }
       
   369 
       
   370 static pascal ControlPartCode
       
   371 mykeydownproc(ControlHandle control, SInt16 keyCode, SInt16 charCode, SInt16 modifiers)
       
   372 {
       
   373         ControlObject *ctl_obj;
       
   374         PyObject *arglist, *rv;
       
   375         short c_rv = 0;
       
   376 
       
   377         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   378         arglist = Py_BuildValue("Ohhh", ctl_obj, keyCode, charCode, modifiers);
       
   379         rv = callcallback(ctl_obj, kControlUserPaneKeyDownProcTag, arglist);
       
   380         Py_XDECREF(arglist);
       
   381         if ( rv )
       
   382                 if (!PyArg_Parse(rv, "h", &c_rv))
       
   383                         PyErr_Clear();
       
   384         Py_XDECREF(rv);
       
   385         return (ControlPartCode)c_rv;
       
   386 }
       
   387 
       
   388 static pascal ControlPartCode
       
   389 myfocusproc(ControlHandle control, ControlPartCode part)
       
   390 {
       
   391         ControlObject *ctl_obj;
       
   392         PyObject *arglist, *rv;
       
   393         short c_rv = kControlFocusNoPart;
       
   394 
       
   395         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   396         arglist = Py_BuildValue("Oh", ctl_obj, part);
       
   397         rv = callcallback(ctl_obj, kControlUserPaneFocusProcTag, arglist);
       
   398         Py_XDECREF(arglist);
       
   399         if ( rv )
       
   400                 if (!PyArg_Parse(rv, "h", &c_rv))
       
   401                         PyErr_Clear();
       
   402         Py_XDECREF(rv);
       
   403         return (ControlPartCode)c_rv;
       
   404 }
       
   405 
       
   406 static pascal void
       
   407 mydrawproc(ControlHandle control, SInt16 part)
       
   408 {
       
   409         ControlObject *ctl_obj;
       
   410         PyObject *arglist, *rv;
       
   411 
       
   412         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   413         arglist = Py_BuildValue("Oh", ctl_obj, part);
       
   414         rv = callcallback(ctl_obj, kControlUserPaneDrawProcTag, arglist);
       
   415         Py_XDECREF(arglist);
       
   416         Py_XDECREF(rv);
       
   417 }
       
   418 
       
   419 static pascal void
       
   420 myidleproc(ControlHandle control)
       
   421 {
       
   422         ControlObject *ctl_obj;
       
   423         PyObject *arglist, *rv;
       
   424 
       
   425         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   426         arglist = Py_BuildValue("O", ctl_obj);
       
   427         rv = callcallback(ctl_obj, kControlUserPaneIdleProcTag, arglist);
       
   428         Py_XDECREF(arglist);
       
   429         Py_XDECREF(rv);
       
   430 }
       
   431 
       
   432 static pascal ControlPartCode
       
   433 myhittestproc(ControlHandle control, Point where)
       
   434 {
       
   435         ControlObject *ctl_obj;
       
   436         PyObject *arglist, *rv;
       
   437         short c_rv = -1;
       
   438 
       
   439         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   440         arglist = Py_BuildValue("OO&", ctl_obj, PyMac_BuildPoint, where);
       
   441         rv = callcallback(ctl_obj, kControlUserPaneHitTestProcTag, arglist);
       
   442         Py_XDECREF(arglist);
       
   443         /* Ignore errors, nothing we can do about them */
       
   444         if ( rv )
       
   445                 if (!PyArg_Parse(rv, "h", &c_rv))
       
   446                         PyErr_Clear();
       
   447         Py_XDECREF(rv);
       
   448         return (ControlPartCode)c_rv;
       
   449 }
       
   450 
       
   451 static pascal ControlPartCode
       
   452 mytrackingproc(ControlHandle control, Point startPt, ControlActionUPP actionProc)
       
   453 {
       
   454         ControlObject *ctl_obj;
       
   455         PyObject *arglist, *rv;
       
   456         short c_rv = -1;
       
   457 
       
   458         ctl_obj = (ControlObject *)CtlObj_WhichControl(control);
       
   459         /* We cannot pass the actionProc without lots of work */
       
   460         arglist = Py_BuildValue("OO&", ctl_obj, PyMac_BuildPoint, startPt);
       
   461         rv = callcallback(ctl_obj, kControlUserPaneTrackingProcTag, arglist);
       
   462         Py_XDECREF(arglist);
       
   463         if ( rv )
       
   464                 if (!PyArg_Parse(rv, "h", &c_rv))
       
   465                         PyErr_Clear();
       
   466         Py_XDECREF(rv);
       
   467         return (ControlPartCode)c_rv;
       
   468 }
       
   469 """
       
   470 
       
   471 initstuff = initstuff + """
       
   472 mytracker_upp = NewControlActionUPP(mytracker);
       
   473 myactionproc_upp = NewControlActionUPP(myactionproc);
       
   474 mykeydownproc_upp = NewControlUserPaneKeyDownUPP(mykeydownproc);
       
   475 myfocusproc_upp = NewControlUserPaneFocusUPP(myfocusproc);
       
   476 mydrawproc_upp = NewControlUserPaneDrawUPP(mydrawproc);
       
   477 myidleproc_upp = NewControlUserPaneIdleUPP(myidleproc);
       
   478 myhittestproc_upp = NewControlUserPaneHitTestUPP(myhittestproc);
       
   479 mytrackingproc_upp = NewControlUserPaneTrackingUPP(mytrackingproc);
       
   480 PyMac_INIT_TOOLBOX_OBJECT_NEW(ControlHandle, CtlObj_New);
       
   481 PyMac_INIT_TOOLBOX_OBJECT_CONVERT(ControlHandle, CtlObj_Convert);
       
   482 """
       
   483 
       
   484 class MyObjectDefinition(PEP253Mixin, ObjectIdentityMixin, GlobalObjectDefinition):
       
   485     def outputStructMembers(self):
       
   486         GlobalObjectDefinition.outputStructMembers(self)
       
   487         Output("PyObject *ob_callbackdict;")
       
   488     def outputCheckNewArg(self):
       
   489         Output("if (itself == NULL) return PyMac_Error(resNotFound);")
       
   490     def outputInitStructMembers(self):
       
   491         GlobalObjectDefinition.outputInitStructMembers(self)
       
   492         Output("SetControlReference(itself, (long)it);")
       
   493         Output("it->ob_callbackdict = NULL;")
       
   494     def outputCleanupStructMembers(self):
       
   495         Output("Py_XDECREF(self->ob_callbackdict);")
       
   496         Output("if (self->ob_itself)SetControlReference(self->ob_itself, (long)0); /* Make it forget about us */")
       
   497 
       
   498 # Create the generator groups and link them
       
   499 module = MacModule(MODNAME, MODPREFIX, includestuff, finalstuff, initstuff)
       
   500 object = MyObjectDefinition(OBJECTNAME, OBJECTPREFIX, OBJECTTYPE)
       
   501 module.addobject(object)
       
   502 
       
   503 # Create the generator classes used to populate the lists
       
   504 Function = OSErrWeakLinkFunctionGenerator
       
   505 Method = OSErrWeakLinkMethodGenerator
       
   506 
       
   507 # Create and populate the lists
       
   508 functions = []
       
   509 methods = []
       
   510 execfile(INPUTFILE)
       
   511 execfile('ctledit.py')
       
   512 
       
   513 # add the populated lists to the generator groups
       
   514 for f in functions: module.add(f)
       
   515 for f in methods: object.add(f)
       
   516 
       
   517 # Manual generator for TrackControl, due to callback ideosyncracies
       
   518 trackcontrol_body = """
       
   519 ControlPartCode _rv;
       
   520 Point startPoint;
       
   521 ControlActionUPP upp = 0;
       
   522 PyObject *callback = 0;
       
   523 
       
   524 if (!PyArg_ParseTuple(_args, "O&|O",
       
   525                       PyMac_GetPoint, &startPoint, &callback))
       
   526         return NULL;
       
   527 if (callback && callback != Py_None) {
       
   528         if (PyInt_Check(callback) && PyInt_AS_LONG(callback) == -1)
       
   529                 upp = (ControlActionUPP)-1;
       
   530         else {
       
   531                 settrackfunc(callback);
       
   532                 upp = mytracker_upp;
       
   533         }
       
   534 }
       
   535 _rv = TrackControl(_self->ob_itself,
       
   536                    startPoint,
       
   537                    upp);
       
   538 clrtrackfunc();
       
   539 _res = Py_BuildValue("h",
       
   540                      _rv);
       
   541 return _res;
       
   542 """
       
   543 
       
   544 f = ManualGenerator("TrackControl", trackcontrol_body);
       
   545 f.docstring = lambda: "(Point startPoint [,trackercallback]) -> (ControlPartCode _rv)"
       
   546 object.add(f)
       
   547 
       
   548 # CJW - added 5/12/99
       
   549 # Manual generator for HandleControlClick, as for TrackControl
       
   550 handlecontrolclick_body = """
       
   551 ControlPartCode _rv;
       
   552 Point startPoint;
       
   553 SInt16 modifiers;
       
   554 ControlActionUPP upp = 0;
       
   555 PyObject *callback = 0;
       
   556 
       
   557 if (!PyArg_ParseTuple(_args, "O&h|O",
       
   558                       PyMac_GetPoint, &startPoint,
       
   559                       &modifiers,
       
   560                       &callback))
       
   561         return NULL;
       
   562 if (callback && callback != Py_None) {
       
   563         if (PyInt_Check(callback) && PyInt_AS_LONG(callback) == -1)
       
   564                 upp = (ControlActionUPP)-1;
       
   565         else {
       
   566                 settrackfunc(callback);
       
   567                 upp = mytracker_upp;
       
   568         }
       
   569 }
       
   570 _rv = HandleControlClick(_self->ob_itself,
       
   571                    startPoint,
       
   572                    modifiers,
       
   573                    upp);
       
   574 clrtrackfunc();
       
   575 _res = Py_BuildValue("h",
       
   576                      _rv);
       
   577 return _res;
       
   578 """
       
   579 
       
   580 f = ManualGenerator("HandleControlClick", handlecontrolclick_body);
       
   581 f.docstring = lambda: "(Point startPoint, Integer modifiers, [,trackercallback]) -> (ControlPartCode _rv)"
       
   582 object.add(f)
       
   583 
       
   584 # Manual Generator for SetControlData
       
   585 setcontroldata_body = """
       
   586 OSErr _err;
       
   587 ControlPartCode inPart;
       
   588 ResType inTagName;
       
   589 Size bufferSize;
       
   590 Ptr buffer;
       
   591 
       
   592 if (!PyArg_ParseTuple(_args, "hO&s#",
       
   593                       &inPart,
       
   594                       PyMac_GetOSType, &inTagName,
       
   595                       &buffer, &bufferSize))
       
   596         return NULL;
       
   597 
       
   598 _err = SetControlData(_self->ob_itself,
       
   599                       inPart,
       
   600                       inTagName,
       
   601                       bufferSize,
       
   602                       buffer);
       
   603 
       
   604 if (_err != noErr)
       
   605         return PyMac_Error(_err);
       
   606 _res = Py_None;
       
   607 return _res;
       
   608 """
       
   609 
       
   610 f = ManualGenerator("SetControlData", setcontroldata_body);
       
   611 f.docstring = lambda: "(stuff) -> None"
       
   612 object.add(f)
       
   613 
       
   614 # Manual Generator for GetControlData
       
   615 getcontroldata_body = """
       
   616 OSErr _err;
       
   617 ControlPartCode inPart;
       
   618 ResType inTagName;
       
   619 Size bufferSize;
       
   620 Ptr buffer;
       
   621 Size outSize;
       
   622 
       
   623 if (!PyArg_ParseTuple(_args, "hO&",
       
   624                       &inPart,
       
   625                       PyMac_GetOSType, &inTagName))
       
   626         return NULL;
       
   627 
       
   628 /* allocate a buffer for the data */
       
   629 _err = GetControlDataSize(_self->ob_itself,
       
   630                           inPart,
       
   631                           inTagName,
       
   632                           &bufferSize);
       
   633 if (_err != noErr)
       
   634         return PyMac_Error(_err);
       
   635 buffer = PyMem_NEW(char, bufferSize);
       
   636 if (buffer == NULL)
       
   637         return PyErr_NoMemory();
       
   638 
       
   639 _err = GetControlData(_self->ob_itself,
       
   640                       inPart,
       
   641                       inTagName,
       
   642                       bufferSize,
       
   643                       buffer,
       
   644                       &outSize);
       
   645 
       
   646 if (_err != noErr) {
       
   647         PyMem_DEL(buffer);
       
   648         return PyMac_Error(_err);
       
   649 }
       
   650 _res = Py_BuildValue("s#", buffer, outSize);
       
   651 PyMem_DEL(buffer);
       
   652 return _res;
       
   653 """
       
   654 
       
   655 f = ManualGenerator("GetControlData", getcontroldata_body);
       
   656 f.docstring = lambda: "(part, type) -> String"
       
   657 object.add(f)
       
   658 
       
   659 # Manual Generator for SetControlData_Handle
       
   660 setcontroldata_handle_body = """
       
   661 OSErr _err;
       
   662 ControlPartCode inPart;
       
   663 ResType inTagName;
       
   664 Handle buffer;
       
   665 
       
   666 if (!PyArg_ParseTuple(_args, "hO&O&",
       
   667                       &inPart,
       
   668                       PyMac_GetOSType, &inTagName,
       
   669                       OptResObj_Convert, &buffer))
       
   670         return NULL;
       
   671 
       
   672 _err = SetControlData(_self->ob_itself,
       
   673                       inPart,
       
   674                       inTagName,
       
   675                       sizeof(buffer),
       
   676                       (Ptr)&buffer);
       
   677 
       
   678 if (_err != noErr)
       
   679         return PyMac_Error(_err);
       
   680 _res = Py_None;
       
   681 return _res;
       
   682 """
       
   683 
       
   684 f = ManualGenerator("SetControlData_Handle", setcontroldata_handle_body);
       
   685 f.docstring = lambda: "(ResObj) -> None"
       
   686 object.add(f)
       
   687 
       
   688 # Manual Generator for GetControlData_Handle
       
   689 getcontroldata_handle_body = """
       
   690 OSErr _err;
       
   691 ControlPartCode inPart;
       
   692 ResType inTagName;
       
   693 Size bufferSize;
       
   694 Handle hdl;
       
   695 
       
   696 if (!PyArg_ParseTuple(_args, "hO&",
       
   697                       &inPart,
       
   698                       PyMac_GetOSType, &inTagName))
       
   699         return NULL;
       
   700 
       
   701 /* Check it is handle-sized */
       
   702 _err = GetControlDataSize(_self->ob_itself,
       
   703                           inPart,
       
   704                           inTagName,
       
   705                           &bufferSize);
       
   706 if (_err != noErr)
       
   707         return PyMac_Error(_err);
       
   708 if (bufferSize != sizeof(Handle)) {
       
   709         PyErr_SetString(Ctl_Error, "GetControlDataSize() != sizeof(Handle)");
       
   710         return NULL;
       
   711 }
       
   712 
       
   713 _err = GetControlData(_self->ob_itself,
       
   714                       inPart,
       
   715                       inTagName,
       
   716                       sizeof(Handle),
       
   717                       (Ptr)&hdl,
       
   718                       &bufferSize);
       
   719 
       
   720 if (_err != noErr) {
       
   721         return PyMac_Error(_err);
       
   722 }
       
   723 _res = Py_BuildValue("O&", OptResObj_New, hdl);
       
   724 return _res;
       
   725 """
       
   726 
       
   727 f = ManualGenerator("GetControlData_Handle", getcontroldata_handle_body);
       
   728 f.docstring = lambda: "(part, type) -> ResObj"
       
   729 object.add(f)
       
   730 
       
   731 # Manual Generator for SetControlData_Callback
       
   732 setcontroldata_callback_body = """
       
   733 OSErr _err;
       
   734 ControlPartCode inPart;
       
   735 ResType inTagName;
       
   736 PyObject *callback;
       
   737 UniversalProcPtr c_callback;
       
   738 
       
   739 if (!PyArg_ParseTuple(_args, "hO&O",
       
   740                       &inPart,
       
   741                       PyMac_GetOSType, &inTagName,
       
   742                       &callback))
       
   743         return NULL;
       
   744 
       
   745 if ( setcallback((PyObject *)_self, inTagName, callback, &c_callback) < 0 )
       
   746         return NULL;
       
   747 _err = SetControlData(_self->ob_itself,
       
   748                       inPart,
       
   749                       inTagName,
       
   750                       sizeof(c_callback),
       
   751                       (Ptr)&c_callback);
       
   752 
       
   753 if (_err != noErr)
       
   754         return PyMac_Error(_err);
       
   755 _res = Py_None;
       
   756 return _res;
       
   757 """
       
   758 
       
   759 f = ManualGenerator("SetControlData_Callback", setcontroldata_callback_body);
       
   760 f.docstring = lambda: "(callbackfunc) -> None"
       
   761 object.add(f)
       
   762 
       
   763 
       
   764 
       
   765 createtabscontrol_body = """\
       
   766 OSStatus _err;
       
   767 WindowPtr window;
       
   768 Rect boundsRect;
       
   769 UInt16 size;
       
   770 UInt16 direction;
       
   771 int i;
       
   772 UInt16 numTabs;
       
   773 ControlTabEntry tabArray[MAXTABS];
       
   774 ControlHandle outControl;
       
   775 PyObject *tabArrayObj, *tabEntry;
       
   776 
       
   777 #ifndef CreateTabsControl
       
   778 PyMac_PRECHECK(CreateTabsControl);
       
   779 #endif
       
   780 if (!PyArg_ParseTuple(_args, "O&O&HHO",
       
   781                       WinObj_Convert, &window,
       
   782                       PyMac_GetRect, &boundsRect,
       
   783                       &size,
       
   784                       &direction,
       
   785                       &tabArrayObj))
       
   786         return NULL;
       
   787 
       
   788 i = PySequence_Length(tabArrayObj);
       
   789 if (i == -1)
       
   790         return NULL;
       
   791 if (i > MAXTABS) {
       
   792         PyErr_SetString(Ctl_Error, "Too many tabs");
       
   793         return NULL;
       
   794 }
       
   795 numTabs = i;
       
   796 for (i=0; i<numTabs; i++) {
       
   797         tabEntry = PySequence_GetItem(tabArrayObj, i);
       
   798         if (tabEntry == NULL)
       
   799                 return NULL;
       
   800         if (!PyArg_Parse(tabEntry, "(O&O&B)",
       
   801                          ControlButtonContentInfo_Convert, &tabArray[i].icon,
       
   802                          CFStringRefObj_Convert, &tabArray[i].name,
       
   803                          &tabArray[i].enabled
       
   804                          ))
       
   805                 return NULL;
       
   806 }
       
   807 
       
   808 _err = CreateTabsControl(window,
       
   809                          &boundsRect,
       
   810                          size,
       
   811                          direction,
       
   812                          numTabs,
       
   813                          tabArray,
       
   814                          &outControl);
       
   815 if (_err != noErr) return PyMac_Error(_err);
       
   816 _res = Py_BuildValue("O&",
       
   817                      CtlObj_New, outControl);
       
   818 return _res;"""
       
   819 
       
   820 f = ManualGenerator("CreateTabsControl", createtabscontrol_body)
       
   821 f.docstring = lambda: "(WindowPtr window, Rect boundsRect, UInt16 size, UInt16 direction, ControlTabEntry tabArray) -> (ControlHandle outControl)"
       
   822 module.add(f)
       
   823 
       
   824 # generate output (open the output file as late as possible)
       
   825 SetOutputFileName(OUTPUTFILE)
       
   826 module.generate()