Пример #1
0
static StructRNA *rna_Panel_register(
        Main *bmain, ReportList *reports, void *data, const char *identifier,
        StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
{
	ARegionType *art;
	PanelType *pt, dummypt = {NULL};
	Panel dummypanel = {NULL};
	PointerRNA dummyptr;
	int have_function[3];

	/* setup dummy panel & panel type to store static properties in */
	dummypanel.type = &dummypt;
	RNA_pointer_create(NULL, &RNA_Panel, &dummypanel, &dummyptr);

	/* We have to set default context! Else we get a void string... */
	strcpy(dummypt.translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);

	/* validate the python class */
	if (validate(&dummyptr, data, have_function) != 0)
		return NULL;

	if (strlen(identifier) >= sizeof(dummypt.idname)) {
		BKE_reportf(reports, RPT_ERROR, "Registering panel class: '%s' is too long, maximum length is %d",
		            identifier, (int)sizeof(dummypt.idname));
		return NULL;
	}

	if ((dummypt.category[0] == '\0') && (dummypt.region_type == RGN_TYPE_TOOLS)) {
		/* Use a fallback, otherwise an empty value will draw the panel in every category. */
		strcpy(dummypt.category, PNL_CATEGORY_FALLBACK);
	}

	if (!(art = region_type_find(reports, dummypt.space_type, dummypt.region_type)))
		return NULL;

	/* check if we have registered this panel type before, and remove it */
	for (pt = art->paneltypes.first; pt; pt = pt->next) {
		if (STREQ(pt->idname, dummypt.idname)) {
			if (pt->ext.srna)
				rna_Panel_unregister(bmain, pt->ext.srna);
			else
				BLI_freelinkN(&art->paneltypes, pt);
			break;
		}
	}
	if (!RNA_struct_available_or_report(reports, dummypt.idname)) {
		return NULL;
	}
	if (!RNA_struct_bl_idname_ok_or_report(reports, dummypt.idname, "_PT_")) {
		return NULL;
	}

	/* create a new panel type */
	pt = MEM_callocN(sizeof(PanelType), "python buttons panel");
	memcpy(pt, &dummypt, sizeof(dummypt));

	pt->ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, pt->idname, &RNA_Panel);
	RNA_def_struct_translation_context(pt->ext.srna, pt->translation_context);
	pt->ext.data = data;
	pt->ext.call = call;
	pt->ext.free = free;
	RNA_struct_blender_type_set(pt->ext.srna, pt);
	RNA_def_struct_flag(pt->ext.srna, STRUCT_NO_IDPROPERTIES);

	pt->poll = (have_function[0]) ? panel_poll : NULL;
	pt->draw = (have_function[1]) ? panel_draw : NULL;
	pt->draw_header = (have_function[2]) ? panel_draw_header : NULL;

	/* XXX use "no header" flag for some ordering of panels until we have real panel ordering */
	if (pt->flag & PNL_NO_HEADER) {
		PanelType *pth = art->paneltypes.first;
		while (pth && pth->flag & PNL_NO_HEADER)
			pth = pth->next;

		if (pth)
			BLI_insertlinkbefore(&art->paneltypes, pth, pt);
		else
			BLI_addtail(&art->paneltypes, pt);
	}
	else
		BLI_addtail(&art->paneltypes, pt);

	/* update while blender is running */
	WM_main_add_notifier(NC_WINDOW, NULL);

	return pt->ext.srna;
}
Пример #2
0
static StructRNA *rna_Panel_register(Main *bmain, ReportList *reports, void *data, const char *identifier,
                                     StructValidateFunc validate, StructCallbackFunc call, StructFreeFunc free)
{
    ARegionType *art;
    PanelType *pt, dummypt = {NULL};
    Panel dummypanel= {NULL};
    PointerRNA dummyptr;
    int have_function[3];

    /* setup dummy panel & panel type to store static properties in */
    dummypanel.type= &dummypt;
    RNA_pointer_create(NULL, &RNA_Panel, &dummypanel, &dummyptr);

    /* validate the python class */
    if(validate(&dummyptr, data, have_function) != 0)
        return NULL;

    if(strlen(identifier) >= sizeof(dummypt.idname)) {
        BKE_reportf(reports, RPT_ERROR, "registering panel class: '%s' is too long, maximum length is %d",
                    identifier, (int)sizeof(dummypt.idname));
        return NULL;
    }

    if(!(art=region_type_find(reports, dummypt.space_type, dummypt.region_type)))
        return NULL;

    /* check if we have registered this panel type before, and remove it */
    for(pt=art->paneltypes.first; pt; pt=pt->next) {
        if(strcmp(pt->idname, dummypt.idname) == 0) {
            if(pt->ext.srna)
                rna_Panel_unregister(bmain, pt->ext.srna);
            else
                BLI_freelinkN(&art->paneltypes, pt);
            break;
        }
    }

    /* create a new panel type */
    pt= MEM_callocN(sizeof(PanelType), "python buttons panel");
    memcpy(pt, &dummypt, sizeof(dummypt));

    pt->ext.srna= RNA_def_struct(&BLENDER_RNA, pt->idname, "Panel");
    pt->ext.data= data;
    pt->ext.call= call;
    pt->ext.free= free;
    RNA_struct_blender_type_set(pt->ext.srna, pt);
    RNA_def_struct_flag(pt->ext.srna, STRUCT_NO_IDPROPERTIES);

    pt->poll= (have_function[0])? panel_poll: NULL;
    pt->draw= (have_function[1])? panel_draw: NULL;
    pt->draw_header= (have_function[2])? panel_draw_header: NULL;

    /* XXX use "no header" flag for some ordering of panels until we have real panel ordering */
    if(pt->flag & PNL_NO_HEADER) {
        PanelType *pth = art->paneltypes.first;
        while(pth && pth->flag & PNL_NO_HEADER)
            pth=pth->next;

        if(pth)
            BLI_insertlinkbefore(&art->paneltypes, pth, pt);
        else
            BLI_addtail(&art->paneltypes, pt);
    }
    else
        BLI_addtail(&art->paneltypes, pt);

    /* update while blender is running */
    WM_main_add_notifier(NC_SCREEN|NA_EDITED, NULL);

    return pt->ext.srna;
}