IUP/Functions/IupSetAttribute

From Vendetta Lua
Revision as of 23:51, 10 May 2023 by Draugath (talk | contribs) (Created page with "__NOTOC__ __NOEDITSECTION__ Defines an attribute for an interface element. ===Parameters/Return=== iup.SetAttribute('''element''': ihandle, '''attribute''': string, '''v...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search


Defines an attribute for an interface element.

Parameters/Return

   iup.SetAttribute(element: ihandle, attribute: string, value: string)
element: Identifier of the interface element.
a: name of the attribute.
v: value of the attribute. If it equals nil, 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:

<source lang=c>

       {
         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)

</source>

When an attribute is set it is always stored at the hash table. If the value is nil, the attribute will be removed from the hash table. Then the driver or the custom control is updated. Finally the attribute is also updated for the children of the element in the driver if they do not have the attribute defined in their own hash table.

See Also

IupGetAttribute, IupSetAttributes, IupGetAttributes, IupStoreAttribute