示例#1
0
static PyObject *Icn_RegisterIconRefFromIconFile(PyObject *_self, PyObject *_args)
{
	PyObject *_res = NULL;
	OSErr _err;
	OSType creator;
	OSType iconType;
	FSSpec iconFile;
	IconRef theIconRef;
#ifndef RegisterIconRefFromIconFile
	PyMac_PRECHECK(RegisterIconRefFromIconFile);
#endif
	if (!PyArg_ParseTuple(_args, "O&O&O&",
	                      PyMac_GetOSType, &creator,
	                      PyMac_GetOSType, &iconType,
	                      PyMac_GetFSSpec, &iconFile))
		return NULL;
	_err = RegisterIconRefFromIconFile(creator,
	                                   iconType,
	                                   &iconFile,
	                                   &theIconRef);
	if (_err != noErr) return PyMac_Error(_err);
	_res = Py_BuildValue("O&",
	                     ResObj_New, theIconRef);
	return _res;
}
// --------------------------------------------------------------------------------------
static DataBrowserItemID addItemsToIconDB(ControlRef iconDataBrowser, ControlRef *userPanes)
{
    IconDBItemDataRec *itemsData, *rowItemData;
    CFBundleRef mainBundle;
    CFIndex rowNumber;
    DataBrowserItemID items[kNumberOfRows];
    CFStringRef catNameKeys[kNumberOfRows];

    itemsData = CFAllocatorAllocate(kCFAllocatorDefault,
                                    kNumberOfRows * sizeof(IconDBItemDataRec), 0);
    mainBundle = CFBundleGetMainBundle();

    if (!gIconsRegistered)		// if we didn't register our icons already, we need to
    {
        CFArrayRef iconURLs;
        FSRef iconFile;
        FSSpec iconSpec;

        iconURLs = CFBundleCopyResourceURLsOfType(mainBundle, CFSTR("icns"),
                   CFSTR("PrefCategories"));
        for (rowNumber = 0; rowNumber < kNumberOfRows; rowNumber++)
        {
            CFURLGetFSRef(CFArrayGetValueAtIndex(iconURLs, rowNumber), &iconFile);

#if TARGET_API_MAC_OSX
#pragma unused (iconSpec)
            RegisterIconRefFromFSRef(kAppSignature, 'Cat0' + rowNumber, &iconFile,
                                     &((itemsData + rowNumber)->icon));
#else
            FSGetCatalogInfo(&iconFile, kFSCatInfoNone, NULL, NULL, &iconSpec, NULL);
            RegisterIconRefFromIconFile(kAppSignature, 'Cat0' + rowNumber, &iconSpec,
                                        &((itemsData + rowNumber)->icon));
#endif
        }

        CFRelease(iconURLs);
        gIconsRegistered = true;
    }
    else	// the icons are already registered so we just have to get them
    {
        for (rowNumber = 0; rowNumber < kNumberOfRows; rowNumber++)
        {
            GetIconRef(kOnSystemDisk, kAppSignature, 'Cat0' + rowNumber,
                       &((itemsData + rowNumber)->icon));
        }
    }

    catNameKeys[0] = CFSTR("Panel 1");	// this is necessary because you have to use
    catNameKeys[1] = CFSTR("Panel 2");	// constant strings with CFSTR
    catNameKeys[2] = CFSTR("Panel 3");	// if we didn't do this then we couldn't get
    catNameKeys[3] = CFSTR("Panel 4");	// the strings in a loop
    catNameKeys[4] = CFSTR("Panel 5");
    catNameKeys[5] = CFSTR("Panel 6");
    catNameKeys[6] = CFSTR("Panel 7");
    catNameKeys[7] = CFSTR("Panel 8");
    catNameKeys[8] = CFSTR("Panel 9");
    catNameKeys[9] = CFSTR("Panel 10");

    for (rowNumber = 0; rowNumber < kNumberOfRows; rowNumber++)		// now get the names
    {   // and set the user panes
        rowItemData = itemsData + rowNumber;
        items[rowNumber] = (DataBrowserItemID)rowItemData;

        /* We use CFCopyLocalilzedStringFromTableInBundle here instead of
           CFCopyLocalizedString to avoid a call to CFBundleGetMainBundle every trip
           around the loop. */
        rowItemData->name = CFCopyLocalizedStringFromTableInBundle(catNameKeys[rowNumber],
                            NULL, mainBundle, NULL);
        rowItemData->userPane = *(userPanes + rowNumber);
    }

    AddDataBrowserItems(iconDataBrowser, kDataBrowserNoItem, kNumberOfRows, items,
                        kDataBrowserItemNoProperty);

    return *items;
}