IupSetAttribute

Defines an attribute for an interface element. See also the Attributes Guide section.

Parameters/Return

void IupSetAttribute(Ihandle *ih, const char *name, const char *value); [in C]
iup.SetAttribute(ih: ihandle, name: string, value: string) [in Lua] void IupSetAttributeId(Ihandle *ih, const char *name, int id, const char *value); [in C]
iup.SetAttributeId(ih: ihandle, name: string, id: number, value: string) [in Lua]

ih: Identifier of the interface element. If NULL will set in the global environment.
name: name of the attribute.
id: used when the attribute has an additional id.
value: value of the attribute. If NULL (nil in Lua), the default value will be used.

Notes

See the Attributes Guide for more details.

User Data

The value stored in the attribute is not duplicated. Therefore, you can store your private attributes, such as a structure with data to be used in a callback. When you want IUP to store an attribute by duplicating a string passed as a value, use function IupStoreAttribute.

struct myData* mydata = malloc(sizeof(struct myData));
IupSetAttribute(dlg, "MYDATA", (char*)mydata) // correct (unknown attributes will be stored as pointers)

Examples

A very common mistake when using IupSetAttribute is to use local string arrays to set attributes. For ex:

{
char value[30];
sprintf(value, "%d", i);
IupSetAttribute(dlg, "BADEXAMPLE", value) // WRONG (value pointer will be internally stored,
} // but its memory will be released at the end of this scope)
// Use IupStoreAttribute in this case
{
char *value = malloc(30);
sprintf(value, "%d", i);
IupSetAttribute(dlg, "EXAMPLE", value) // correct (but to avoid memory leaks you should free the pointer
} after the dialog has been destroyed)
IupSetAttribute(dlg, "VISIBLE", "YES")       // correct (static values still exists after this scope)
char attrib[30];
sprintf(attrib, "ITEM%d", i);
IupSetAttribute(dlg, attrib, "Test") // correct (attribute names are always internally duplicated)

Defines a radio’s initial value.

Ihandle *portrait = IupToggle("Portrait" , NULL);
Ihandle *landscape = IupToggle("landscape" , NULL);
Ihandle *box = IupVbox(portrait, IupFill(),landscape, NULL);
Ihandle *mode = IupRadio(box);
IupSetHandle("landscape", landscape); /* associates a name to initialize the radio */
IupSetAttribute(mode, "VALUE", "landscape"); /* defines the radio’s initial value */

Other usages:

  1. IupSetAttribute(text, "VALUE", "Hello!");
  2. IupSetAttribute(indicator, "VALUE", "ON");
  3. struct
{
int x;
int y;
} myData;

IupSetAttribute(text, "myData", (char*)&myData); // correct, BUT myData should be a global variable.

See Also

IupGetAttribute, IupSetAttributes, IupGetAttributes, IupStoreAttribute