1# Objects 2 3In LVGL the **basic building blocks** of a user interface are the objects, also called *Widgets*. 4For example a [Button](/widgets/core/btn), [Label](/widgets/core/label), [Image](/widgets/core/img), [List](/widgets/extra/list), [Chart](/widgets/extra/chart) or [Text area](/widgets/core/textarea). 5 6You can see all the [Object types](/widgets/index) here. 7 8All objects are referenced using an `lv_obj_t` pointer as a handle. This pointer can later be used to set or get the attributes of the object. 9 10## Attributes 11 12### Basic attributes 13 14All object types share some basic attributes: 15- Position 16- Size 17- Parent 18- Styles 19- Event handlers 20- Etc 21 22You can set/get these attributes with `lv_obj_set_...` and `lv_obj_get_...` functions. For example: 23 24```c 25/*Set basic object attributes*/ 26lv_obj_set_size(btn1, 100, 50); /*Set a button's size*/ 27lv_obj_set_pos(btn1, 20,30); /*Set a button's position*/ 28``` 29 30To see all the available functions visit the [Base object's documentation](/widgets/obj). 31 32### Specific attributes 33 34The object types have special attributes too. For example, a slider has 35- Minimum and maximum values 36- Current value 37 38For these special attributes, every object type may have unique API functions. For example for a slider: 39 40```c 41/*Set slider specific attributes*/ 42lv_slider_set_range(slider1, 0, 100); /*Set the min. and max. values*/ 43lv_slider_set_value(slider1, 40, LV_ANIM_ON); /*Set the current value (position)*/ 44``` 45 46The API of the widgets is described in their [Documentation](/widgets/index) but you can also check the respective header files (e.g. *widgets/lv_slider.h*) 47 48## Working mechanisms 49 50### Parent-child structure 51 52A parent object can be considered as the container of its children. Every object has exactly one parent object (except screens), but a parent can have any number of children. 53There is no limitation for the type of the parent but there are objects which are typically a parent (e.g. button) or a child (e.g. label). 54 55### Moving together 56 57If the position of a parent changes, the children will move along with it. 58Therefore, all positions are relative to the parent. 59 60![](/misc/par_child1.png "Objects are moving together 1") 61 62```c 63lv_obj_t * parent = lv_obj_create(lv_scr_act()); /*Create a parent object on the current screen*/ 64lv_obj_set_size(parent, 100, 80); /*Set the size of the parent*/ 65 66lv_obj_t * obj1 = lv_obj_create(parent); /*Create an object on the previously created parent object*/ 67lv_obj_set_pos(obj1, 10, 10); /*Set the position of the new object*/ 68``` 69 70Modify the position of the parent: 71 72![](/misc/par_child2.png "Graphical objects are moving together 2") 73 74```c 75lv_obj_set_pos(parent, 50, 50); /*Move the parent. The child will move with it.*/ 76``` 77 78(For simplicity the adjusting of colors of the objects is not shown in the example.) 79 80### Visibility only on the parent 81 82If a child is partially or fully outside its parent then the parts outside will not be visible. 83 84![](/misc/par_child3.png "A graphical object is visible on its parent") 85 86```c 87lv_obj_set_x(obj1, -30); /*Move the child a little bit off the parent*/ 88``` 89 90This behavior can be overwritten with `lv_obj_add_flag(obj, LV_OBJ_FLAG_OVERFLOW_VISIBLE);` which allow the children to be drawn out of the parent. 91 92 93### Create and delete objects 94 95In LVGL, objects can be created and deleted dynamically at run time. It means only the currently created (existing) objects consume RAM. 96 97This allows for the creation of a screen just when a button is clicked to open it, and for deletion of screens when a new screen is loaded. 98 99UIs can be created based on the current environment of the device. For example one can create meters, charts, bars and sliders based on the currently attached sensors. 100 101Every widget has its own **create** function with a prototype like this: 102```c 103lv_obj_t * lv_<widget>_create(lv_obj_t * parent, <other parameters if any>); 104``` 105 106Typically, the create functions only have a *parent* parameter telling them on which object to create the new widget. 107 108The return value is a pointer to the created object with `lv_obj_t *` type. 109 110 111There is a common **delete** function for all object types. It deletes the object and all of its children. 112 113```c 114void lv_obj_del(lv_obj_t * obj); 115``` 116 117`lv_obj_del` will delete the object immediately. 118If for any reason you can't delete the object immediately you can use `lv_obj_del_async(obj)` which will perform the deletion on the next call of `lv_timer_handler()`. 119This is useful e.g. if you want to delete the parent of an object in the child's `LV_EVENT_DELETE` handler. 120 121You can remove all the children of an object (but not the object itself) using `lv_obj_clean(obj)`. 122 123You can use `lv_obj_del_delayed(obj, 1000)` to delete an object after some time. The delay is expressed in milliseconds. 124 125 126## Screens 127 128### Create screens 129The screens are special objects which have no parent object. So they can be created like: 130```c 131lv_obj_t * scr1 = lv_obj_create(NULL); 132``` 133 134Screens can be created with any object type. For example, a [Base object](/widgets/obj) or an image to make a wallpaper. 135 136### Get the active screen 137There is always an active screen on each display. By default, the library creates and loads a "Base object" as a screen for each display. 138 139To get the currently active screen use the `lv_scr_act()` function. 140 141### Load screens 142 143To load a new screen, use `lv_scr_load(scr1)`. 144 145### Layers 146There are two automatically generated layers: 147- top layer 148- system layer 149 150They are independent of the screens and they will be shown on every screen. The *top layer* is above every object on the screen and the *system layer* is above the *top layer*. 151You can add any pop-up windows to the *top layer* freely. But, the *system layer* is restricted to system-level things (e.g. mouse cursor will be placed there with `lv_indev_set_cursor()`). 152 153The `lv_layer_top()` and `lv_layer_sys()` functions return pointers to the top and system layers respectively. 154 155Read the [Layer overview](/overview/layer) section to learn more about layers. 156 157 158#### Load screen with animation 159 160A new screen can be loaded with animation by using `lv_scr_load_anim(scr, transition_type, time, delay, auto_del)`. The following transition types exist: 161- `LV_SCR_LOAD_ANIM_NONE` Switch immediately after `delay` milliseconds 162- `LV_SCR_LOAD_ANIM_OVER_LEFT/RIGHT/TOP/BOTTOM` Move the new screen over the current towards the given direction 163- `LV_SCR_LOAD_ANIM_OUT_LEFT/RIGHT/TOP/BOTTOM` Move out the old screen over the current towards the given direction 164- `LV_SCR_LOAD_ANIM_MOVE_LEFT/RIGHT/TOP/BOTTOM` Move both the current and new screens towards the given direction 165- `LV_SCR_LOAD_ANIM_FADE_IN/OUT` Fade the new screen over the old screen, or vice versa 166 167Setting `auto_del` to `true` will automatically delete the old screen when the animation is finished. 168 169The new screen will become active (returned by `lv_scr_act()`) when the animation starts after `delay` time. 170All inputs are disabled during the screen animation. 171 172### Handling multiple displays 173Screens are created on the currently selected *default display*. 174The *default display* is the last registered display with `lv_disp_drv_register`. You can also explicitly select a new default display using `lv_disp_set_default(disp)`. 175 176`lv_scr_act()`, `lv_scr_load()` and `lv_scr_load_anim()` operate on the default display. 177 178Visit [Multi-display support](/overview/display) to learn more. 179 180## Parts 181 182The widgets are built from multiple parts. For example a [Base object](/widgets/obj) uses the main and scrollbar parts but a [Slider](/widgets/core/slider) uses the main, indicator and knob parts. 183Parts are similar to *pseudo-elements* in CSS. 184 185The following predefined parts exist in LVGL: 186- `LV_PART_MAIN` A background like rectangle 187- `LV_PART_SCROLLBAR` The scrollbar(s) 188- `LV_PART_INDICATOR` Indicator, e.g. for slider, bar, switch, or the tick box of the checkbox 189- `LV_PART_KNOB` Like a handle to grab to adjust the value 190- `LV_PART_SELECTED` Indicate the currently selected option or section 191- `LV_PART_ITEMS` Used if the widget has multiple similar elements (e.g. table cells) 192- `LV_PART_TICKS` Ticks on scales e.g. for a chart or meter 193- `LV_PART_CURSOR` Mark a specific place e.g. text area's or chart's cursor 194- `LV_PART_CUSTOM_FIRST` Custom parts can be added from here. 195 196The main purpose of parts is to allow styling the "components" of the widgets. 197They are described in more detail in the [Style overview](/overview/style) section. 198 199## States 200The object can be in a combination of the following states: 201- `LV_STATE_DEFAULT` Normal, released state 202- `LV_STATE_CHECKED` Toggled or checked state 203- `LV_STATE_FOCUSED` Focused via keypad or encoder or clicked via touchpad/mouse 204- `LV_STATE_FOCUS_KEY` Focused via keypad or encoder but not via touchpad/mouse 205- `LV_STATE_EDITED` Edit by an encoder 206- `LV_STATE_HOVERED` Hovered by mouse (not supported now) 207- `LV_STATE_PRESSED` Being pressed 208- `LV_STATE_SCROLLED` Being scrolled 209- `LV_STATE_DISABLED` Disabled state 210- `LV_STATE_USER_1` Custom state 211- `LV_STATE_USER_2` Custom state 212- `LV_STATE_USER_3` Custom state 213- `LV_STATE_USER_4` Custom state 214 215The states are usually automatically changed by the library as the user interacts with an object (presses, releases, focuses, etc.). 216However, the states can be changed manually too. 217To set or clear given state (but leave the other states untouched) use `lv_obj_add/clear_state(obj, LV_STATE_...)` 218In both cases OR-ed state values can be used as well. E.g. `lv_obj_add_state(obj, part, LV_STATE_PRESSED | LV_PRESSED_CHECKED)`. 219 220To learn more about the states read the related section of the [Style overview](/overview/style). 221 222## Snapshot 223A snapshot image can be generated for an object together with its children. Check details in [Snapshot](/others/snapshot). 224