2021-04-12 18:19:04 +02:00
```eval_rst
.. include:: /header.rst
:github_url: |github_link_base|/widgets/obj.md
```
# Base object (lv_obj)
## Overview
The 'Base Object' implements the basic properties of widgets on a screen, such as:
- coordinates
- parent object
- children
2021-04-27 15:36:53 +02:00
- contains the styles
- attributes like *Clickable* , *Scrollable* , etc.
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
In object-oriented thinking, it is the base class from which all other objects in LVGL are inherited.
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
The functions and functionalities of the Base object can be used with other widgets too. For example `lv_obj_set_width(slider, 100)`
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
The Base object can be directly used as a simple widget: it nothing else than a rectangle. In HTML terms, think of it as a `<div>` .
2021-04-12 18:19:04 +02:00
### Coordinates
2021-06-09 15:10:35 +02:00
Only a small subset of coordinate settings is described here. To see all the features of LVGL (padding, coordinates in styles, layouts, etc) visit the [Coordinates ](/overview/coords ) page.
2021-04-27 15:36:53 +02:00
2021-04-12 18:19:04 +02:00
#### Size
The object size can be modified on individual axes with `lv_obj_set_width(obj, new_width)` and `lv_obj_set_height(obj, new_height)` , or both axes can be modified at the same time with `lv_obj_set_size(obj, new_width, new_height)` .
#### Position
2021-06-09 15:10:35 +02:00
You can set the position relative to the parent with `lv_obj_set_x(obj, new_x)` and `lv_obj_set_y(obj, new_y)` , or both axes at the same time with `lv_obj_set_pos(obj, new_x, new_y)` .
2021-04-12 18:19:04 +02:00
#### Alignment
2021-06-09 15:10:35 +02:00
You can align the object on its parent with `lv_obj_set_align(obj, LV_ALIGN_...)` . After this every x and y setting will be ralitive to the set alignment mode.
For example a this will shift the object by 10;20 px from the center of its parent.
2021-04-27 15:36:53 +02:00
```c
lv_obj_set_align(obj, LV_ALIGN_CENTER);
lv_obj_set_pos(obj, 10, 20);
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
//Or in one function
lv_obj_align(obj, LV_ALIGN_CENTER, 10, 20);
```
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
To align one object to another use `lv_obj_align_to(obj_to_align, obj_referece, LV_ALIGN_..., x, y)`
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
For example, to align a text below an image: `lv_obj_align(text, image, LV_ALIGN_OUT_BOTTOM_MID, 0, 10)` .
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
The following align types exist:
2021-04-27 15:36:53 +02:00
![](/misc/align.png "Alignment types in LVGL")
2021-04-12 18:19:04 +02:00
### Parents and children
You can set a new parent for an object with `lv_obj_set_parent(obj, new_parent)` . To get the current parent, use `lv_obj_get_parent(obj)` .
2021-04-27 15:36:53 +02:00
To get a specific children of a parent use `lv_obj_get_child(parent, idx)` . Some examples for `idx` :
2021-06-09 15:10:35 +02:00
- `0` get the child created first child
- `1` get the child created second
- `-1` get the child created last
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
The children can be iterated lke this
2021-04-12 18:19:04 +02:00
```c
2021-04-27 15:36:53 +02:00
uint32_t i;
for(i = 0; i < lv_obj_get_child_cnt ( parent ) ; i + + ) {
lv_obj_t * child = lv_obj_get_child(paernt, i);
/*Do something with child*/
2021-04-12 18:19:04 +02:00
}
```
2021-06-09 15:10:35 +02:00
`lv_obj_get_child_id(obj)` returns the index of the object. That is how many younger children its parent has.
2021-04-27 15:36:53 +02:00
You can bring an object to the foreground or send it to the background with `lv_obj_move_foreground(obj)` and `lv_obj_move_background(obj)` .
2021-04-12 18:19:04 +02:00
### Screens
2021-04-27 15:36:53 +02:00
When you have created a screen like `lv_obj_t * screen = lv_obj_create(NULL)` , you can load it with `lv_scr_load(screen)` . The `lv_scr_act()` function gives you a pointer to the current screen.
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
If you have multiple displays then it's important to know that these functions operate on the most-recently created or on the explicitly selected (with `lv_disp_set_default` ) display.
2021-04-12 18:19:04 +02:00
To get an object's screen use the `lv_obj_get_screen(obj)` function.
### Events
2021-04-27 15:36:53 +02:00
To set an event callback for an object, use `lv_obj_add_event_cb(obj, event_cb, LV_EVENT_..., user_data)` ,
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
To manually send an event to an object, use `lv_event_send(obj, LV_EVENT_..., param)`
2021-04-12 18:19:04 +02:00
Read the [Event overview ](/overview/event ) to learn more about the events.
2021-05-15 20:02:56 +02:00
### Styles
2021-06-09 15:10:35 +02:00
Be sure to read the [Style overview ](/overview/style ). Here only the most essential functions are described.
2021-04-12 18:19:04 +02:00
2021-05-15 20:02:56 +02:00
A new style can be added to an object with `lv_obj_add_style(obj, &new_style, selector)` function.
`selector` is a combination of part and state(s). E.g. `LV_PART_SCROLLBAR | LV_STATE_PRESSED` .
2021-06-09 15:10:35 +02:00
The base objects use `LV_PART_MAIN` style properties and `LV_PART_SCROLLBAR` with the typical backgroud style properties.
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
### Flags
There are some attributes which can be enabled/disabled by `lv_obj_add/clear_flag(obj, LV_OBJ_FLAG_...)` :
2021-05-15 20:02:56 +02:00
- `LV_OBJ_FLAG_HIDDEN` Make the object hidden. (Like it wasn't there at all)
- `LV_OBJ_FLAG_CLICKABLE` Make the object clickable by the input devices
- `LV_OBJ_FLAG_CLICK_FOCUSABLE` Add focused state to the object when clicked
- `LV_OBJ_FLAG_CHECKABLE` Toggle checked state when the object is clicked
- `LV_OBJ_FLAG_SCROLLABLE` Make the object scrollable
- `LV_OBJ_FLAG_SCROLL_ELASTIC` Allow scrolling inside but with slower speed
- `LV_OBJ_FLAG_SCROLL_MOMENTUM` Make the object scroll further when "thrown"
2021-06-09 16:33:27 +02:00
- `LV_OBJ_FLAG_SCROLL_ONE` Allow scrolling only one snappable children
2021-05-15 20:02:56 +02:00
- `LV_OBJ_FLAG_SCROLL_CHAIN` Allow propagating the scroll to a parent
- `LV_OBJ_FLAG_SCROLL_ON_FOCUS` Automatically scroll object to make it visible when focused
2021-06-09 16:33:27 +02:00
- `LV_OBJ_FLAG_SNAPPABLE` If scroll snap is enabled on the parent it can snap to this object
2021-05-15 20:02:56 +02:00
- `LV_OBJ_FLAG_PRESS_LOCK` Keep the object pressed even if the press slid from the object
- `LV_OBJ_FLAG_EVENT_BUBBLE` Propagate the events to the parent too
- `LV_OBJ_FLAG_GESTURE_BUBBLE` Propagate the gestures to the parent
- `LV_OBJ_FLAG_ADV_HITTEST` Allow performing more accurate hit (click) test. E.g. consider rounded corners.
- `LV_OBJ_FLAG_IGNORE_LAYOUT` Make the object position-able by the layouts
- `LV_OBJ_FLAG_FLOATING` Do not scroll the object when the parent scrolls and ignore layout
- `LV_OBJ_FLAG_LAYOUT_1` Custom flag, free to use by layouts
- `LV_OBJ_FLAG_LAYOUT_2` Custom flag, free to use by layouts
- `LV_OBJ_FLAG_WIDGET_1` Custom flag, free to use by widget
- `LV_OBJ_FLAG_WIDGET_2` Custom flag, free to use by widget
- `LV_OBJ_FLAG_USER_1` Custom flag, free to use by user
- `LV_OBJ_FLAG_USER_2` Custom flag, free to use by user
- `LV_OBJ_FLAG_USER_3` Custom flag, free to use by user
- `LV_OBJ_FLAG_USER_4` Custom flag, free to use by usersection.
2021-04-27 15:36:53 +02:00
Some examples:
```c
/*Hide on object*/
lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
/*Make an obejct non-clickable*/
lv_obj_clear_flag(obj, LV_OBJ_FLAG_CLICKABLE);
```
2021-04-12 18:19:04 +02:00
### Groups
2021-04-27 15:36:53 +02:00
Read the [Input devices overview ](/overview/indev ) to learn more about the *Groups* .
2021-06-09 15:10:35 +02:00
Objects are added to a *group* with `lv_group_add_obj(group, obj)` , and you can use `lv_obj_get_group(obj)` to see which group an object belongs to.
2021-04-12 18:19:04 +02:00
2021-06-09 15:10:35 +02:00
`lv_obj_is_focused(obj)` returns if the object is currently focused on its group or not. If the object is not added to a group, `false` will be returned.
2021-04-12 18:19:04 +02:00
### Extended click area
2021-04-27 15:36:53 +02:00
By default, the objects can be clicked only on their coordinates, however, this area can be extended with `lv_obj_set_ext_click_area(obj, size)` .
2021-04-12 18:19:04 +02:00
## Events
2021-06-09 15:10:35 +02:00
- `LV_EVENT_VALUE_CHANGED` when the `LV_OBJ_FLAG_CHECKABLE` flag is enabled and the object clicked (on transition to/from the checked state)
2021-07-07 16:18:56 +02:00
- `LV_EVENT_DRAW_PART_BEGIN` and `LV_EVENT_DRAW_PART_END` is sent for the following types:
- `LV_OBJ_DRAW_PART_RECTANGLE` The main rectangle
- `part` : `LV_PART_MAIN`
- `rect_dsc`
- `draw_area` : the area of the rectangle
- `LV_OBJ_DRAW_PART_BORDER_POST` The border if the `border_post` style property is `true`
- `part` : `LV_PART_MAIN`
- `rect_dsc`
- `draw_area` : the area of the rectangle
- `LV_OBJ_DRAW_PART_SCROLLBAR` the scrollbars
- `part` : `LV_PART_SCROLLBAR`
- `rect_dsc`
- `draw_area` : the area of the rectangle
2021-04-12 18:19:04 +02:00
Learn more about [Events ](/overview/event ).
## Keys
2021-06-09 15:10:35 +02:00
If `LV_OBJ_FLAG_CHECKABLE` is enabled `LV_KEY_RIGHT` and `LV_KEY_UP` make the object checked, and `LV_KEY_LEFT` and `LV_KEY_DOWN` make it unchecked.
2021-04-12 18:19:04 +02:00
2021-04-27 15:36:53 +02:00
Learn more about [Keys ](/overview/indev ).
2021-04-12 18:19:04 +02:00
## Example
```eval_rst
2021-05-31 14:14:58 -04:00
.. include:: ../../examples/widgets/obj/index.rst
2021-04-12 18:19:04 +02:00
```
## API
```eval_rst
.. doxygenfile:: lv_obj.h
:project: lvgl
```