# Widgets ## Common Attributes and Children Many widgets have specific attributes, however, they all share a set of the [common attributes](../../../code/gui/ui/index.md#common_params). Besides, all widgets can have the following children. ### tooltip Sets a tooltip text for the widget. Attributes: - *translate* - **0** or **no** The tooltip should always have the same text. - **1** or **yes** The tooltip text should be translated according to the selected language. For example: ```xml ``` For the *tooltip* tag rich text formatting is always enabled. ![](widgets/tooltip.png) ### attach Attaches the widget to another one. - When applied to several [*checkbox*](../../../code/gui/ui/ui_widgets.md#checkbox) widgets, converts them into a group of radio buttons. - A [*hslider*](../../../code/gui/ui/ui_widgets.md#hslider) / [*vslider*](../../../code/gui/ui/ui_widgets.md#vslider) can be attached to a [*label*](../../../code/gui/ui/ui_widgets.md#label) or an [*editline*](../../../code/gui/ui/ui_widgets.md#editline). - A [*spinbox*](#spinbox) can be attached to the*editline*. - The *editline* can be attached to the *hslider* /*vslider* and *spinbox*. Attributes: - *format* This attribute takes a [format string](../../../api/library/common/class.string_cpp.md#format_const_char_ptr_va_list_StringStacktmplargs). - *multiplier* A multiplier used to downscale values provided by the attached widget (integer). - *flags* - **log10** Logarithmic slider. - **expand** Indicates whether the minimum or maximum value of the attached widget can be changed by writing a lower or greater value respectively into the associated *editline*. - **min_expand** Indicates whether the minimum value of the attached widget can be lowered by writing a lower value into the associated *editline*. - **max_expand** Indicates whether the maximum value of the attached widget can be raised by writing a greater value into the associated *editline*. For example, the maximum value of the following horizontal slider can be changed by writing the greater value into the editline: ```xml editline ``` ### callback Sets a callback for the widget. Contains a name of the callback function. Attributes: - *type* - **show** The widget has become visible. Supported by all widgets. - **hide** The widget is removed from GUI layout (via [Gui::removeChild()](../../../api/library/gui/class.gui_cpp.md#removeChild_Widget_void)). Supported by all widgets. - **focus_in** The widget has got focus. Supported by all widgets. - **focus_out** The widget has lost focus. Supported by all widgets. - **changed** The state of the widget has changed. Supported by the following widgets: | [button](#button) | [checkbox](#checkbox) | [combobox](#combobox) | [editline](#editline) | [edittext](#edittext) | |---|---|---|---|---| | [hpaned](../../../code/gui/ui/ui_containers.md#hpaned) | [hscroll](#hscroll) | [hslider](#hslider) | [icon](#icon) | [label](#label) | | [listbox](#listbox) | [spinbox](#spinbox) | [scrollbox](../../../code/gui/ui/ui_containers.md#scrollbox) | [treebox](#treebox) | [vscroll](#vscroll) | | [vslider](#vslider) | [vpaned](../../../code/gui/ui/ui_containers.md#vpaned) | | | | - **clicked** The widget has been clicked on and released. (See also *[pressed](#pressed)* callback). Supported by the following widgets: | [button](#button) | [checkbox](#checkbox) | [combobox](#combobox) | [editline](#editline) | [edittext](#edittext) | |---|---|---|---|---| | [hpaned](../../../code/gui/ui/ui_containers.md#hpaned) | [hscroll](#hscroll) | [hslider](#hslider) | [icon](#icon) | [listbox](#listbox) | | [menubox](#menubox) | [spinbox](#spinbox) | [vpaned](../../../code/gui/ui/ui_containers.md#vpaned) | [vscroll](#vscroll) | [vslider](#vslider) | - **double_clicked** The widget has been double clicked. Supported by the following widgets: | [button](#button) | [checkbox](#checkbox) | [combobox](#combobox) | [editline](#editline) | [edittext](#edittext) | |---|---|---|---|---| | [hpaned](../../../code/gui/ui/ui_containers.md#hpaned) | [hscroll](#hscroll) | [icon](#icon) | [listbox](#listbox) | [spinbox](#spinbox) | | [treebox](#treebox) | [vpaned](../../../code/gui/ui/ui_containers.md#vpaned) | [vscroll](#vscroll) | | | - **pressed** The widget has been clicked on by any mouse button or ENTER is pressed. (See also *[clicked](#clicked)* callback). Supported by the following widgets: | [button](#button) | [editline](#editline) | [hslider](#hslider) | [icon](#icon) | |---|---|---|---| | [listbox](#listbox) | [treebox](#treebox) | [spinbox](#spinbox) | [vslider](#vslider) | - **released** The widget is in focus, and the mouse was released after clicking somewhere on a widget. Supported by the following widgets: | [button](#button) | [icon](#icon) | |---|---| - **key_pressed** The widget is in focus, and a key has been pressed. Supported by the following widgets: | [editline](#editline) | [edittext](#edittext) | |---|---| - **enter** The mouse pointer has run over the widget. Supported by all widgets. - **leave** The mouse pointer has run out from the widget. Supported by all widgets. - **drag_drop** The widget is dragged and dropped over another widget. Callbacks of this type should be defined both for the widget being dragged and for the target widget. Supported by all widgets. - *widget* The *widget* attribute sets a widget reference to be passed as an argument to the callback function. Any existing widget [name](../../../code/gui/ui/index.md#param_name) is allowed. - *string* The *string* attribute sets a string argument for the callback function. Any valid string is acceptable. This attribute can also be used to pass any other data types. - *variable* The *variable* attribute sets an argument of any variable type for the callback function. You can also pass [vectors](../../../code/uniginescript/language/containers/index.md#vector) and [maps](../../../code/uniginescript/language/containers/index.md#maps). - *accel_key* The *accel_key* attribute sets a hotkey for a given callback. For example, to run the user-defined *esc_clicked()* callback by pressing the ESC button, use the following: ```xml ``` The following keys can be set as the hotkey: - **ascii symbol** Any button, which has a corresponding ASCII code. - **esc** ESC. - **tab** TAB. - **backspace** BACKSPACE. - **return** ENTER. - **delete** DELETE. - **insert** INSERT. - **home** HOME. - **end** END. - **pgup** PAGE UP. - **pgdown** PAGE DOWN. - **left** ARROW LEFT. - **right** ARROW RIGHT. - **up** ARROW UP. - **down** ARROW DOWN. - **f1�f12** Any of twelve functional buttons. - *accel_ctrl* Whether the CTRL button is pressed or not. - **0** or **no** CTRL button isn't pressed. - **1** or **yes** CTRL button is pressed. - *accel_alt* Whether the ALT button is pressed or not. - **0** or **no** ALT button isn't pressed. - **1** or **yes** ALT button is pressed. - *accel_shift* Whether the SHIFT button is pressed or not. - **0** or **no** SHIFT button isn't pressed. - **1** or **yes** SHIFT button is pressed. It is possible to set a hotkey as a combination of keys by combining the *accel_key*, *accel_ctrl*, *accel_alt* and *accel_shift* attributes. For example, to assign the **CTRL+N** hotkey to the *ctrl_n_clicked()* callback function, define the following: ```xml ``` ## button Corresponds to an object of the [WidgetButton](../../../api/library/gui/class.widgetbutton_cpp.md) class. ![button widget](widgets/button.png) Attributes: - *background* Whether to render button background or not. The default is **1** (boolean). - *toggleable* Whether the widget is a toggle button or an ordinary one. The default is **0** (boolean). - *toggled* Whether the toggle button is pressed by default or not. The default is **0** (boolean). - *texture* Path to a button image texture. Children: - [*text*](../../../code/gui/ui/index.md#text) Text displayed on the button. In addition to the [described](../../../code/gui/ui/index.md#text) attribute, the following attribute are accepted: > **Notice:** If no text is specified, the button will be automatically hidden. - *align* with values **center**, **left**, **right**. ```xml ``` To add a button without background, set the *background* attribute to 0: ```xml ``` The second button is the same as the first, but it has no background and image: ![](examples/button.png) ## canvas Corresponds to an object of the [WidgetCanvas](../../../api/library/gui/class.widgetcanvas_cpp.md) class. ![canvas widget with polygons](widgets/canvas.png) After a canvas widget was created, it can be used to draw polygons (either textured or not), lines and text via UnigineScript. The canvas can be used to create different trackers, charts, tables, etc. Attributes: - *texture* Background texture to be drawn across the canvas ```xml ``` To create a simple canvas, define the *canvas* tag in your UI file as follows: ```xml Window Title ``` Then create the corresponding objects for each widget and describe the required geometry in the script: ```cpp class Test { WidgetWindow window; WidgetCanvas canvas; Test() { ui = new UserInterface(engine.getGui(),"canvas.ui"); } int show(int x,int y)() { Gui gui = engine.getGui(); // add the window widget and its children to the GUI gui.addChild(window,GUI_ALIGN_CENTER); window.setPosition(x,y); // draw polygons int id = canvas.addPolygon(); canvas.setPolygonColor(id,vec4(0.8f,0.4f,0.0f,1.0f)); canvas.setPolygonTransform(id,translate(130.0f,64.0f,0.0f)); forloop(int i = 0; 7) { float angle = PI2 * i / 6; canvas.addPolygonPoint(id,vec3(sin(angle),cos(angle),0.0f) * 64.0f); } // draw lines id = canvas.addLine(); canvas.setLineColor(id,vec4(0.0f,0.0f,0.0f,1.0f)); canvas.setLineTransform(id,translate(128.0f,64.0f,0.0f)); forloop(int i = 0; 6) { float angle = PI2 * i * 1 / 3; canvas.addLinePoint(id,vec3(sin(angle),cos(angle),0.0f) * 48.0f); } } } int init(){ Window window_0 = new Window(); window_0.show(150,150); return 1; } ``` The described canvas is shown as follows: ![](examples/canvas.png) ## checkbox Corresponds to an object of the [WidgetCheckBox](../../../api/library/gui/class.widgetcheckbox_cpp.md) class. ![checkbox widget](widgets/checkbox.png) Attributes: - *checked* Whether the checkbox is selected by default or not. The default is **0** (boolean). Children: - [*text*](../../../code/gui/ui/index.md#text) Text label. - [*attach*](#child_attach) First checkbox widget of a radio button group. ```xml Checkbox 0 Checkbox 1 ``` ![radiobutton widget](widgets/radiobutton.png) To create a radio button group, use the [*attach*](#child_attach) child: ```xml Variant 0 Test::radiobutton0 Variant 1 ``` ## combobox Corresponds to an object of the [WidgetComboBox](../../../api/library/gui/class.widgetcombobox_cpp.md) class. ![combobox widget](widgets/combobox.png) Attributes: - *texture* Path to a texture with mini-icons. This texture is a strip of N pixels in width and N�M pixels in height. Children: - [*text*](../../../code/gui/ui/index.md#text) Text of a list item. Multiple items are supported, the default item is the first one. In addition to the [described](../../../code/gui/ui/index.md#text) attributes, the following attributes are accepted: - *texture*, which sets the ID of a mini-icon to be used for the item starting from zero. - *data*, which sets the item data. The data can be used as a text identifier of the item (instead of using the number of the item). ```xml item 0 item 1 item 2 item 3 ``` To set a mini-icon for each item, use the *texture* attribute as follows: ```xml item 0 item 1 item 2 item 3 ``` The result is a list of items with icons: ![](examples/combobox.png) The `combobox_icons.png` image is a vertical strip of square (16�16 pixels) mini-icons that have a transparent background: ![](examples/combobox_icons.png) *16�64 strip of mini-icons* See the article on [Skin Layout](../../../code/gui/skin/index.md) for more details. ## editline Corresponds to an object of the [WidgetEditLine](../../../api/library/gui/class.widgeteditline_cpp.md) class. ![editline widget](widgets/editline.png) Attributes: - *editable* Whether to allow modifying the text or not. The default is **1** (boolean). - *password* Whether the text should be displayed as dots. The default is **0** (boolean). - *background* Whether the widget has a background or not. The default is 1 (boolean). - *capacity* Maximum number of characters that can be entered (integer). - *validator* Value validator, acceptable types are any, int, uint, float or ascii. - *color* Text color. Changes the text color only if the *text* tag is not specified as a child. If the child tag is specified, this attribute won't affect the text color. Children: - [*text*](../../../code/gui/ui/index.md#text) The initial string. > **Notice:** If there is no the *text* child, all of the [text attributes](../../../code/gui/ui/index.md#text) can be defined as attributes of *editline*. ```xml EditLine ``` The following example shows possible ways of using the *editline* widget: ```xml ``` The first editline allows user to input the integer values only. The other symbols are not allowed to be typed. The text in the second editline is displayed as dots, because the *password* attribute is set to 1. The last editline has no background, and the text color is defined directly in the *editline* tag by using the *color* attribute. ![](examples/editline.png) ## edittext Corresponds to an object of the [WidgetEditText](../../../api/library/gui/class.widgetedittext_cpp.md) class. ![edittext widget](widgets/edittext.png) Attributes: - *editable* Whether to allow modifying the text or not. The default is **1** (boolean). - *background* Whether the widget has a background or not. The default is 1 (boolean). - *color* Text color. Changes the text color only if the *text* tag is not specified as a child. If the child tag is specified, this attribute won't affect the text color. Children: - [*text*](../../../code/gui/ui/index.md#text) The initial string. > **Notice:** If there is no the *text* child, all of the [text attributes](../../../code/gui/ui/index.md#text) can be defined as attributes of *edittext*. ```xml EditLine Multiline ``` The *edittext* widget can be used, for example, to input some source code: ```xml ``` The result is the following: ![](examples/edittext.png) If the text scrolling is required, define the *edittext* widget inside the [*scrollbox*](../../../code/gui/ui/ui_containers.md#scrollbox) container: ```xml ``` If you type the long text, the following result is shown: ![](examples/edittext_scroll.png) ## hscroll Corresponds to an object of the [WidgetScroll](../../../api/library/gui/class.widgetscroll_cpp.md) class. ![hscroll widget](widgets/hscroll.png) Attributes: - *object* Width of the area to scroll (in pixels). The default is **100**. - *frame* Width of the visible frame (in pixels). The default is **10**. - *step* Number of pixels to scroll, when the user clicks one of the arrows. The default is **1**. - *value* Initial position of the scroller. Cannot exceed *object-frame* pixels. ```xml ``` ## hslider Corresponds to an object of the [WidgetSlider](../../../api/library/gui/class.widgetslider_cpp.md) class. ![hslider widget](widgets/hslider.png) Attributes: - *min* Minimum value (integer). - *max* Maximum value (integer). - *value* Initial value (integer). - *min_expand* Minimum value, up to which the slider's lower limit can be dropped (integer). The lower limit of the slider can be changed only via the editline, to which it is attached with the [*expand*](#expand) of [*min_expand*](#min_expand) flag. - *max_expand* Maximum value, up to which the slider's upper limit can be raised (integer). The upper limit of the slider can be changed only via the editline, to which it is attached with the [*expand*](#expand) of [*max_expand*](#max_expand) flag. - *button_width* Width of the handle. Children: - [*attach*](#child_attach) Widget name, which will display the current value of the slider. ```xml hslider_label