A scroller holds (and clips) a single object and allows you to scroll across it. This means that the user can use a scroll bar or their finger to drag the viewable region across the object, moving through a much larger area than is contained in the viewport. The scroller will always have a default minimum size that is not limited by its contents.
The scroller widget inherits all the functions of the Layout Container.
To create a scroller, use the elm_scroller_add()
function:
Evas_Object *scroller = elm_scroller_add(parent);
To add an object to the scroller, use the elm_object_content_set()
function:
Evas_Object *image; image = elm_image_add(parent); elm_image_file_set(image, "image.png", NULL); evas_object_show(image); evas_object_size_hint_min_set(image, 2560, 1600); elm_object_content_set(scroller, image);
In the above code, we set a minimum size of 2560 x 1600 pixels for the image. The scroller is smaller than the image, so you can scroll across the image.
If you want to be informed when the user begins scrolling the image, use the following code:
evas_object_smart_callback_add(scroller, "scroll,drag,start", _scroll_start_cb, NULL); // Callback function for the "animate,begin" signal. // This callback is called when the user begins scrolling the image. void _scroll_start_cb(void *data, Evas_Object *obj, void *event_info) { printf("Scroll starts\n"); }
When scrolling content, the scroller may âbounceâ when reaching the edge of the content. This is a visual way of indicating that there is no more content to scroll in that direction. Bounce is enabled by default for both axes. To enable or disable the bounce for either or both axes, use the elm_scroller_bounce_set() function. The following code disables the bounce for the horizontal axis and enables it for the vertical axis:
elm_scroller_bounce_set(scroller, EINA_FALSE, EINA_TRUE);
The scroller can limit the scrolling to âpagesâ. In this case, the scrolling
occurs in page-sized chunks of content rather than in a purely continuous
fashion, with the scroller displaying a single âpageâ at a time. This feature
sets the size of the page relative to the viewport of the scroller. A size of
1.0 equals 1 viewport (horizontally or vertically). A size of 0.0 disables
paging for that axis. These settings are mutually exclusive with page size
(see the elm_scroller_page_size_set()
function). A size of 0.5 equals half a
viewport. Usable size values are normally between 0.0 and 1.0, including 1.0.
If you only want a single axis to scroll in pages, use 0.0 for the other axis.
The scroller emits the following signals, which you can catch in your application:
âedge,leftâ
- the left edge of the content has been reachedâedge,rightâ
- the right edge of the content has been reachedâedge,topâ
- the top edge of the content has been reachedâedge,bottomâ
- the bottom edge of the content has been reachedâscrollâ
- the content has been scrolled (moved)âscroll,leftâ
- the content has been scrolled (moved) leftwardsâscroll,rightâ
- the content has been scrolled (moved) rightwardsâscroll,upâ
- the content has been scrolled (moved) upwardsâscroll,downâ
- the content has been scrolled (moved) downwardsâscroll,anim,startâ
- scrolling animation has startedâscroll,anim,stopâ
- scrolling animation has stoppedâscroll,drag,startâ
- dragging the contents around has startedâscroll,drag,stopâ
- dragging the contents around has stoppedâvbar,dragâ
- the vertical scroll bar has been draggedâvbar,pressâ
- the vertical scroll bar has been pressedâvbar,unpressâ
- the vertical scroll bar has been unpressedâhbar,dragâ
- the horizontal scroll bar has been draggedâhbar,pressâ
- the horizontal scroll bar has been pressedâhbar,unpressâ
- the horizontal scroll bar has been unpressedâscroll,page,changedâ
- the visible page has changedâfocusedâ
- When the scroller has received focus. (since 1.8)âunfocusedâ
- When the scroller has lost focus. (since 1.8)
A good example of the scroller is a picture slideshow: we add images to the
scroller and limit the scrolling to pages (one picture at a time). In the
following code, we disable the scroll bars for both axes, limit the scrolling
to pages by using the elm_scroller_page_scroll_limit_set()
function, and lock
the scrolling on the Y axis by using the elm_object_scroll_lock_y_set()
function:
elm_scroller_policy_set(scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF); elm_scroller_page_scroll_limit_set(scroller, 1, 0); elm_object_scroll_lock_y_set(scroller, EINA_TRUE);
We create a horizontal box, which will contain all the images, and which itself will be contained by the scroller:
box = elm_box_add(scroller); elm_box_horizontal_set(box, EINA_TRUE); elm_object_content_set(scroller, box); evas_object_show(box);
We then create all the images and add them to the horizontal box:
img = elm_image_add(scroller); snprintf(buf, sizeof(buf), IMAGE_DIR"/%d.jpg", i); elm_image_file_set(img, buf, NULL); evas_object_show(img); pages = eina_list_append(pages, img); elm_box_pack_end(box, img);
We store references to the images in an eina_list
for easy retrieval later.
Finally, we display the first page of the scroller:
elm_scroller_page_show(scroller, 0, 0);
The size of the scroller depends on the size of the parent. When the parent
changes, for example when the window is resized or rotated, the scroller is
also resized. Since we need to be informed when the scroller is resized, we
add a callback on the EVAS_CALLBACK_RESIZE
event for the scroller:
evas_object_event_callback_add(scroller, EVAS_CALLBACK_RESIZE, _scroller_resize_cb, NULL);
The callback retrieves the new size of the scroller by using the
evas_object_geometry_get()
function on the object pointer. The pointer is
relative to the object that has been resized, which in our case is the
scroller itself. We can then iterate through the images of the scroller and
set the minimum size to fit the scroller size:
EINA_LIST_FOREACH(images, l, page) { evas_object_size_hint_min_set(page, w, h); }
Finally, we set the page size of the scroller to match the scroller size and display the first page:
elm_scroller_page_size_set(obj, w, h); elm_scroller_page_show(obj, 0, 0);