[SESI logo]

Prisms Development Toolkit Documentation

Side Effects Software Inc. 1997

Custom SOPs

Adding Menus for User Choices

There are two kinds of menus available in the SOP dialog box. The first is a rollover menu, which presents a fixed set of options from within a chooser; the user selects one. The choice is shown on the menu bar.

The second is a programmer defined menu which typically is built dynamically. This is used to offer the user choices which the programmer cannot know about in advance. For example, the DXF SOP uses a User Menu (dynamic menu) to offer the user a choice of layers to convert. Obviously, the layer names cannot be known in advance, so the SOP reads the DXF file, constructs a menu, and then allows the user to choose from it.

Here, the example just allows a choice, then appends that choice to the string in the input box.

Source Listing


#include <custom.h>			/* you must have this! */

/*
 * Define the amount of space used by the sop data and name the fields
 */
typedef struct sop_data 
{
    
    SOP_EXPR	Menu;
    SOP_EXPR	UserMenu;
    
} SOP_DATA;

/*
 * Creata a structure that the sop library can use to figure out 
 * what is in your SOP_DATA struct. NOTE: This structure MUST 
 * correspond to the one above.
 */
static SOP_PARMS	sopParms[] =
{
    
    { SOPPARM_MENU,	"Menu Label",	NULL , {NULL} },
    { SOPPARM_USERMENU,	"UserMenu Label",	NULL , {NULL} },
    
    { NULL },
};


/* Good practice to have #defines for your items */
#define ITEM1 0
#define ITEM2 1
#define ITEM3 2

/*
 * This is the definition of the menu items for the first menu 
 * menuTexts contains the text that will be in the menu. It is
 * also used by the SopPrint and the SopScan routines.
 * menuItems is used in the InitSop routine.
 */
static char	*menuTexts[] =
{
    "Item1",
    "Item2",
    "Item3"
};

static MENU	menuItems[] = 
{
    { NULLMFUNC, "Item1", (char *)ITEM1 },
    { NULLMFUNC, "Item2", (char *)ITEM2 },
    { NULLMFUNC, "Item3", (char *)ITEM3 },
    { NULL }
};

/* return 0 to indicate an error */
/*
 * this function takes care of Creating, drawing and responding to 
 * a menu. In this particular case, it responds by appending the
 * selection to the string in the field to its right.
 */
static int
doUserMenu(SOP *sop, UI_STRING *sp, SOP_EXPR *sep)
{
    SOP_DATA	*sb;
    UI_MENU	*menu;
    MENU	*selection;
    char	 tmpstr[BUFSIZ];

    sb = (SOP_DATA *)sop->special;

    /*
     * GeneralBigMenu is a global array in action.
     * It is used as a temporary space for creating menus on the fly.
     */
    GeneralBigMenu[0].func = NULLMFUNC;
    GeneralBigMenu[0].menu = "First Item";
    GeneralBigMenu[0].keyboard = (char *)0;

    GeneralBigMenu[1].func = NULLMFUNC;
    GeneralBigMenu[1].menu = "";	/* Indicates a separator */
    GeneralBigMenu[1].keyboard = (char *)0;

    GeneralBigMenu[2].func = NULLMFUNC;
    GeneralBigMenu[2].menu = "Second Item";
    GeneralBigMenu[2].keyboard = (char *)0;

    GeneralBigMenu[3].func = NOFUNC;	/* Indicate end of menu */
    GeneralBigMenu[3].menu = NOSTRING;

    menu = UI_InitMenu("UserMenu Title", GeneralBigMenu);
    UI_SelectMenu(menu);
    selection = UI_RenderMenu();
    UI_CleanMenu(menu);

    if (selection == NULL)
	return 0;
    printf("selection: '%s'\n", selection->menu);
    strcpy(tmpstr, sep->value.sval);
    strcat(tmpstr, ", ");
    strcat(tmpstr, selection->menu);	/* Add to end of the string */
    strreplace(&sep->value.sval, tmpstr);
    /* Update the Gadget too */
    UI_SETSTRING(sp, sep->value.sval);
    return 1;
}


/*
 * This function is used when creating a new sop. It sets up the
 * initial values of the sop.
 */
static void
sopDefaults(SOP *sop)		/* Put in default values */
{
    SOP_DATA	*sb;

    if (sop->special) free(sop->special);
    sb = (SOP_DATA *)Calloc(sizeof(SOP_DATA), "Sample sopDefaults");
    sop->special = (char *)sb;

    
    InitSopMenu(&sb->Menu, menuItems, ITEM1, "Menu Title");
    /* 
     * Make a UserMenu -- A field with a pop-up menu at the end.
     * The creation and behaviour of the menu are in doUserMenu
     */
    InitSopUserMenu(&sb->UserMenu, "NOTHING", doUserMenu);
    
}

/*
 * This function is used to read data into the sop.
 * You should make sure that its format matches that which is 
 * output by sopPrint
 */
static void
sopScan(SOP *sop, int argc, char *argv[])
{
    SOP_DATA	*sb;	

    sb = (SOP_DATA *) sop->special;	

    
    /* 
     * Arguments to SopScan... are:
     *		character to be used for option.
     *		Pointer to storage area	in SOP
     *		The SOP pointer
     * Note :   The following option characters are reserved
     *		and shouldn't be used:
     *		a,f,o,s,H,I,J,K,L,M,N,Y,Z
     */
    SopScanMenu    ('m', &sb->Menu,     menuTexts, 6);
    SopScanUserMenu('u', &sb->UserMenu, sop);
    
}

/*
 * This function is used to output the sops data to a file.
 * The data will later be read back in by sopScan.
 */
static void
sopPrint(FILE *fp, SOP *sop)
{
    SOP_DATA	*sb;
    sb = (SOP_DATA *)sop->special;

    
    /*
     * Arguments to SopPrint... are:
     *		File Pointer
     *		Data Pointer
     *		Default values (NOTE: Double has 2, 
     *			and Triple has 3, Toggle 0)
     *		The string that precedes the data
     * Data are only printed if different from the default.
     */
    SopPrintMenu    (fp, &sb->Menu,     menuTexts, "", "-m");
    SopPrintUserMenu(fp, &sb->UserMenu, "", "-u");
    
}

static void
sopCook(SOP *sop)
{
    SOP_DATA	*sb;
    OBJECT	*obp;
    char	*expanded;
    int		 idx;
    PolyDetail	*sourcePdp;
    PolyPointPointer	tmppt;
    int		npts;

    if (sop->pdp) FreePolyDetail(sop->pdp);
    sop->pdp = 0;

    
    idx = sb->Menu.value.ival;
    printf("Menu : %d = %s\n", idx, menuItems[idx].menu);
    printf("UserMenu's field: %s\n", sb->UserMenu.value.sval);
    

}

SOP_FUNC	EverythingSopRef = {
	"everything",
	"S:F:e:d::t:::Tm:u:",
	sopScan, 
	sopPrint, 
	sopCook, 
	sopDefaults, 
	sopClean, 
	sopGPMake,
	sopGetParms,
	SOPUSAGE,
};

For More Detailed Information:

The sopParms table and SOP_DATA structures
The sopDefault routine
The sopScan routine
The sopPrint routine
The SOP_FUNC reference structure

Tookit Overview
Expression Functions | User Functions | Custom SOPs
Prisms Internal Format | Programmer's Reference
Compiling SOPs | Using sopbuild | SOP Routine Reference
Dynamic SOPs | Prisms Types | Macros
Math Routines | Data Routines | Polygon Routines | Winged-edge Routines

Copyright © 1997 Side Effects Software Inc.
477 Richmond Street West, Toronto, Ontario, Canada M5V 3E7