IupSetAttribute

IUP - Portable User Interface

IupSetAttribute

Defines an attribute for an interface element.

Parameters/Return

void IupSetAttribute(Ihandle *ih, char *name, char *value); [in C]
iup.SetAttribute(ih: iulua_tag, name: string, value: string) [in Lua] 

ih: Identifier of the interface element. If NULL will set in the global environment.
name: name of the attribute.
value: value of the attribute. If NULL (nil in Lua), the attribute will be removed from the element.

Notes

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. For further information on memory allocation by IupSetAttribute, see IupGetAttribute’s notes section.

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)
struct myData* mydata = malloc(sizeof(struct myData));
IupSetAttribute(dlg, "MYDATA", (char*)mydata)     //  correct  (unknown attributes will be stored as pointers)

Internal Attribute Environment

When an attribute is set it is stored in the internal attribute environment if not processed by the element specific implementation. If the value is NULL, the attribute will be removed from the environment.

Inheritance

After the attribute is set because of the attribute inheritance its children will be notifyed that an attribute has changed.

Examples

Defines a radio’s initial value.

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

Some usages:

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

     IupSetAttribute(texto, "myData", (char*)&myData);

See Also

IupGetAttribute, IupSetAttributes, IupGetAttributes, IupStoreAttribute