Exemple #1
0
/**
 * Signal function of the scrollable part of the roller.
 * @param roller_scrl ointer to the scrollable part of roller (page)
 * @param sign a signal type from lv_signal_t enum
 * @param param pointer to a signal specific variable
 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
 */
static lv_res_t lv_roller_scrl_signal(lv_obj_t * roller_scrl, lv_signal_t sign, void * param)
{
    lv_res_t res;

    /* Include the ancient signal function */
    res = ancestor_scrl_signal(roller_scrl, sign, param);
    if(res != LV_RES_OK) return res;

    lv_indev_t * indev = lv_indev_get_act();
    int32_t id = -1;
    lv_obj_t * roller = lv_obj_get_parent(roller_scrl);
    lv_roller_ext_t * ext = lv_obj_get_ext_attr(roller);

    if(ext->ddlist.label == NULL) return LV_RES_INV;    /*On delete the ddlist signal deletes the label so nothing left to do here*/

    lv_style_t * style_label = lv_obj_get_style(ext->ddlist.label);
    const lv_font_t * font = style_label->text.font;
    lv_coord_t font_h = lv_font_get_height_scale(font);

    if(sign == LV_SIGNAL_DRAG_END) {
        /*If dragged then align the list to there be an element in the middle*/
        lv_coord_t label_y1 = ext->ddlist.label->coords.y1 - roller->coords.y1;
        lv_coord_t label_unit = font_h + style_label->text.line_space;
        lv_coord_t mid = (roller->coords.y2 - roller->coords.y1) / 2;
        id = (mid - label_y1 + style_label->text.line_space / 2) / label_unit;
        if(id < 0) id = 0;
        if(id >= ext->ddlist.option_cnt) id = ext->ddlist.option_cnt - 1;
        ext->ddlist.sel_opt_id = id;
        if(ext->ddlist.action) ext->ddlist.action(roller);
    }
    else if(sign == LV_SIGNAL_RELEASED) {
        /*If picked an option by clicking then set it*/
        if(!lv_indev_is_dragging(indev)) {
            lv_point_t p;
            lv_indev_get_point(indev, &p);
            p.y = p.y - ext->ddlist.label->coords.y1;
            id = p.y / (font_h + style_label->text.line_space);
            if(id < 0) id = 0;
            if(id >= ext->ddlist.option_cnt) id = ext->ddlist.option_cnt - 1;
            ext->ddlist.sel_opt_id = id;
            if(ext->ddlist.action) ext->ddlist.action(roller);
        }
    }

    /*Position the scrollable according to the new selected option*/
    if(id != -1) {
        refr_position(roller, true);
    }

    return res;
}
Exemple #2
0
/**
 * Called when a drop down list is released to open it or set new option
 * @param ddlist pointer to a drop down list object
 * @return LV_ACTION_RES_INV if the ddlist it deleted in the user callback else LV_ACTION_RES_OK
 */
static lv_res_t lv_ddlist_release_action(lv_obj_t * ddlist)
{
    lv_ddlist_ext_t * ext = lv_obj_get_ext_attr(ddlist);

    if(ext->opened == 0) { /*Open the list*/
        ext->opened = 1;
        lv_obj_set_drag(lv_page_get_scrl(ddlist), true);
    } else {
        ext->opened = 0;
        lv_obj_set_drag(lv_page_get_scrl(ddlist), false);

        /*Search the clicked option*/
        lv_indev_t *indev = lv_indev_get_act();
        lv_point_t p;
        lv_indev_get_point(indev, &p);
        p.x -= ext->label->coords.x1;
        p.y -= ext->label->coords.y1;
        uint16_t letter_i;
        letter_i = lv_label_get_letter_on(ext->label, &p);

        uint16_t new_opt = 0;
        const char * txt = lv_label_get_text(ext->label);
        uint16_t i;
        for(i = 0; i < letter_i; i++) {
            if(txt[i] == '\n') new_opt ++;
        }

        ext->sel_opt_id = new_opt;

        if(ext->action != NULL) {
            ext->action(ddlist);
        }
    }
    lv_ddlist_refr_size(ddlist, true);

    return LV_RES_OK;

}
Exemple #3
0
/**
 * Signal function of the switch
 * @param sw pointer to a switch object
 * @param sign a signal type from lv_signal_t enum
 * @param param pointer to a signal specific variable
 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
 */
static lv_res_t lv_sw_signal(lv_obj_t * sw, lv_signal_t sign, void * param)
{
    lv_sw_ext_t * ext = lv_obj_get_ext_attr(sw);

    /*Save the current (old) value before slider signal modifies it*/
    int16_t old_val;

    if(sign == LV_SIGNAL_PRESSING) old_val = ext->slider.drag_value;
    else old_val = lv_slider_get_value(sw);

    /*Do not let the slider to call the callback. The Switch will do it if required*/
    lv_action_t slider_action = ext->slider.action;
    ext->slider.action = NULL;

    lv_res_t res;
    /* Include the ancient signal function */
    res = ancestor_signal(sw, sign, param);
    if(res != LV_RES_OK) return res;

    if(sign == LV_SIGNAL_CLEANUP) {
        /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
    }
    else if(sign == LV_SIGNAL_PRESSED) {

        /*Save the x coordinate of the pressed point to see if the switch was slid*/
        lv_indev_t * indev = lv_indev_get_act();
        if(indev) {
            lv_point_t p;
            lv_indev_get_point(indev,  &p);
            ext->start_x = p.x;
        }
        ext->slided = 0;
        ext->changed = 0;
    }
    else if(sign == LV_SIGNAL_PRESSING) {
        /*See if the switch was slid*/
        lv_indev_t * indev = lv_indev_get_act();
        if(indev) {
            lv_point_t p = {0,0};
            lv_indev_get_point(indev,  &p);
            if(LV_MATH_ABS(p.x - ext->start_x) > LV_INDEV_DRAG_LIMIT) ext->slided = 1;
        }

        /*If didn't slide then revert the min/max value. So click without slide won't move the switch as a slider*/
        if(ext->slided == 0) {
            if(lv_sw_get_state(sw)) ext->slider.drag_value = LV_SWITCH_SLIDER_ANIM_MAX;
            else ext->slider.drag_value = 0;
        }

        /*If explicitly changed (by slide) don't need to be toggled on release*/
        int16_t threshold  = LV_SWITCH_SLIDER_ANIM_MAX / 2;
        if((old_val < threshold && ext->slider.drag_value > threshold) ||
                (old_val > threshold && ext->slider.drag_value < threshold))
        {
            ext->changed = 1;
        }
    }
    else if(sign == LV_SIGNAL_PRESS_LOST) {
        if(lv_sw_get_state(sw)) {
            lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_on);
#if USE_LV_ANIMATION
            lv_sw_anim_to_value(sw, LV_SWITCH_SLIDER_ANIM_MAX);
#endif
        }
        else {
            lv_slider_set_style(sw, LV_SLIDER_STYLE_KNOB, ext->style_knob_off);
#if USE_LV_ANIMATION
            lv_sw_anim_to_value(sw, 0);
#endif
        }
    }
    else if(sign == LV_SIGNAL_RELEASED) {
        /*If not dragged then toggle the switch*/
        if(ext->changed == 0) {
            if(lv_sw_get_state(sw)) lv_sw_off_anim(sw);
            else lv_sw_on_anim(sw);

            if(slider_action != NULL) res = slider_action(sw);
        }
        /*If the switch was dragged then calculate the new state based on the current position*/
        else {
            int16_t v = lv_slider_get_value(sw);
            if(v > LV_SWITCH_SLIDER_ANIM_MAX / 2) lv_sw_on_anim(sw);
            else lv_sw_off_anim(sw);

            if(slider_action != NULL) res = slider_action(sw);
        }

    } else if(sign == LV_SIGNAL_CONTROLL) {

        char c = *((char *)param);
        if(c == LV_GROUP_KEY_ENTER) {
            if(old_val) lv_sw_off_anim(sw);
            else lv_sw_on_anim(sw);

            if(slider_action) res = slider_action(sw);
        } else if(c == LV_GROUP_KEY_UP || c == LV_GROUP_KEY_RIGHT) {
            lv_sw_on_anim(sw);
            if(slider_action) res = slider_action(sw);
        } else if(c == LV_GROUP_KEY_DOWN || c == LV_GROUP_KEY_LEFT) {
            lv_sw_off_anim(sw);
            if(slider_action) res = slider_action(sw);
        }
    } else if(sign == LV_SIGNAL_GET_EDITABLE) {
        bool * editable = (bool *)param;
        *editable = false;          /*The ancestor slider is editable the switch is not*/
    } else if(sign == LV_SIGNAL_GET_TYPE) {
        lv_obj_type_t * buf = param;
        uint8_t i;
        for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) {  /*Find the last set data*/
            if(buf->type[i] == NULL) break;
        }
        buf->type[i] = "lv_sw";
    }

    /*Restore the callback*/
    if(res == LV_RES_OK) ext->slider.action = slider_action;

    return res;
}
Exemple #4
0
/**
 * Refresh the position and size of the scroll bars.
 * @param page pointer to a page object
 */
static void lv_page_sb_refresh(lv_obj_t * page)
{

    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
    lv_style_t * style = lv_obj_get_style(page);
    lv_obj_t * scrl = ext->scrl;
    lv_coord_t size_tmp;
    lv_coord_t scrl_w = lv_obj_get_width(scrl);
    lv_coord_t scrl_h =  lv_obj_get_height(scrl);
    lv_coord_t hpad = style->body.padding.hor;
    lv_coord_t vpad = style->body.padding.ver;
    lv_coord_t obj_w = lv_obj_get_width(page);
    lv_coord_t obj_h = lv_obj_get_height(page);

    /*Always let 'scrollbar width' padding above, under, left and right to the scrollbars
     * else:
     * - horizontal and vertical scrollbars can overlap on the corners
     * - if the page has radius the scrollbar can be out of the radius  */
    lv_coord_t sb_hor_pad = LV_MATH_MAX(ext->sb.style->body.padding.inner, style->body.padding.hor);
    lv_coord_t sb_ver_pad = LV_MATH_MAX(ext->sb.style->body.padding.inner, style->body.padding.ver);

    if(ext->sb.mode == LV_SB_MODE_OFF) return;

    if(ext->sb.mode == LV_SB_MODE_ON) {
        ext->sb.hor_draw = 1;
        ext->sb.ver_draw = 1;
    }

    /*Invalidate the current (old) scrollbar areas*/
    lv_area_t sb_area_tmp;
    if(ext->sb.hor_draw != 0) {
        lv_area_copy(&sb_area_tmp, &ext->sb.hor_area);
        sb_area_tmp.x1 += page->coords.x1;
        sb_area_tmp.y1 += page->coords.y1;
        sb_area_tmp.x2 += page->coords.x2;
        sb_area_tmp.y2 += page->coords.y2;
        lv_inv_area(&sb_area_tmp);
    }
    if(ext->sb.ver_draw != 0)  {
        lv_area_copy(&sb_area_tmp, &ext->sb.ver_area);
        sb_area_tmp.x1 += page->coords.x1;
        sb_area_tmp.y1 += page->coords.y1;
        sb_area_tmp.x2 += page->coords.x2;
        sb_area_tmp.y2 += page->coords.y2;
        lv_inv_area(&sb_area_tmp);
    }


    if(ext->sb.mode == LV_SB_MODE_DRAG && lv_indev_is_dragging(lv_indev_get_act()) == false) {
        ext->sb.hor_draw = 0;
        ext->sb.ver_draw = 0;
        return;

    }

    /*Horizontal scrollbar*/
    if(scrl_w <= obj_w - 2 * hpad) {        /*Full sized scroll bar*/
        lv_area_set_width(&ext->sb.hor_area, obj_w - 2 * sb_hor_pad);
        lv_area_set_pos(&ext->sb.hor_area, sb_hor_pad, obj_h - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.ver);
        if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG)  ext->sb.hor_draw = 0;
    } else {
        size_tmp = (obj_w * (obj_w - (2 * sb_hor_pad))) / (scrl_w + 2 * hpad);
        if(size_tmp < LV_PAGE_SB_MIN_SIZE) size_tmp = LV_PAGE_SB_MIN_SIZE;
        lv_area_set_width(&ext->sb.hor_area,  size_tmp);

        lv_area_set_pos(&ext->sb.hor_area, sb_hor_pad +
                   (-(lv_obj_get_x(scrl) - hpad) * (obj_w - size_tmp -  2 * sb_hor_pad)) /
                   (scrl_w + 2 * hpad - obj_w ),
                   obj_h - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.ver);

        if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG)  ext->sb.hor_draw = 1;
    }
    
    /*Vertical scrollbar*/
    if(scrl_h <= obj_h - 2 * vpad) {        /*Full sized scroll bar*/
        lv_area_set_height(&ext->sb.ver_area,  obj_h - 2 * sb_ver_pad);
        lv_area_set_pos(&ext->sb.ver_area, obj_w - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.hor, sb_ver_pad);
        if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG)  ext->sb.ver_draw = 0;
    } else {
        size_tmp = (obj_h * (obj_h - (2 * sb_ver_pad))) / (scrl_h + 2 * vpad);
        if(size_tmp < LV_PAGE_SB_MIN_SIZE) size_tmp = LV_PAGE_SB_MIN_SIZE;
        lv_area_set_height(&ext->sb.ver_area,  size_tmp);

        lv_area_set_pos(&ext->sb.ver_area,  obj_w - ext->sb.style->body.padding.inner - ext->sb.style->body.padding.hor,
        		    sb_ver_pad +
                   (-(lv_obj_get_y(scrl) - vpad) * (obj_h - size_tmp -  2 * sb_ver_pad)) /
                                      (scrl_h + 2 * vpad - obj_h ));

        if(ext->sb.mode == LV_SB_MODE_AUTO || ext->sb.mode == LV_SB_MODE_DRAG)  ext->sb.ver_draw = 1;
    }

    /*Invalidate the new scrollbar areas*/
    if(ext->sb.hor_draw != 0) {
        lv_area_copy(&sb_area_tmp, &ext->sb.hor_area);
        sb_area_tmp.x1 += page->coords.x1;
        sb_area_tmp.y1 += page->coords.y1;
        sb_area_tmp.x2 += page->coords.x2;
        sb_area_tmp.y2 += page->coords.y2;
        lv_inv_area(&sb_area_tmp);
    }
    if(ext->sb.ver_draw != 0)  {
        lv_area_copy(&sb_area_tmp, &ext->sb.ver_area);
        sb_area_tmp.x1 += page->coords.x1;
        sb_area_tmp.y1 += page->coords.y1;
        sb_area_tmp.x2 += page->coords.x2;
        sb_area_tmp.y2 += page->coords.y2;
        lv_inv_area(&sb_area_tmp);
    }
}
Exemple #5
0
/**
 * Signal function of the scrollable part of a page
 * @param scrl pointer to the scrollable object
 * @param sign a signal type from lv_signal_t enum
 * @param param pointer to a signal specific variable
 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
 */
static lv_res_t lv_page_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void * param)
{
    lv_res_t res;

    /* Include the ancient signal function */
    res = ancestor_signal(scrl, sign, param);
    if(res != LV_RES_OK) return res;

    lv_obj_t * page = lv_obj_get_parent(scrl);
    lv_style_t * page_style = lv_obj_get_style(page);
    lv_page_ext_t * page_ext = lv_obj_get_ext_attr(page);

    if(sign == LV_SIGNAL_CORD_CHG) {
        /*Be sure the width of the scrollable is correct*/
        if(lv_cont_get_hor_fit(scrl) == false) {
            lv_obj_set_width(scrl, lv_obj_get_width(page) - 2 * page_style->body.padding.hor);
        }

        /*Limit the position of the scrollable object to be always visible
         * (Do not let its edge inner then its parent respective edge)*/
        lv_coord_t new_x;
        lv_coord_t new_y;
        bool refr_x = false;
        bool refr_y = false;
        lv_area_t page_cords;
        lv_area_t scrl_cords;
        lv_coord_t hpad = page_style->body.padding.hor;
        lv_coord_t vpad = page_style->body.padding.ver;

        new_x = lv_obj_get_x(scrl);
        new_y = lv_obj_get_y(scrl);
        lv_obj_get_coords(scrl, &scrl_cords);
        lv_obj_get_coords(page, &page_cords);

        /*scrollable width smaller then page width? -> align to left*/
        if(lv_area_get_width(&scrl_cords) + 2 * hpad < lv_area_get_width(&page_cords)) {
            if(scrl_cords.x1 != page_cords.x1 + hpad) {
                new_x = hpad;
                refr_x = true;
            }
        } else {
            /*The edges of the scrollable can not be in the page (minus hpad) */
            if(scrl_cords.x2  < page_cords.x2 - hpad) {
               new_x =  lv_area_get_width(&page_cords) - lv_area_get_width(&scrl_cords) - hpad;   /* Right align */
               refr_x = true;
            }
            if (scrl_cords.x1 > page_cords.x1 + hpad) {
                new_x = hpad;  /*Left align*/
                refr_x = true;
            }
        }

        /*scrollable height smaller then page height? -> align to left*/
        if(lv_area_get_height(&scrl_cords) + 2 * vpad < lv_area_get_height(&page_cords)) {
            if(scrl_cords.y1 != page_cords.y1 + vpad) {
                new_y = vpad;
                refr_y = true;
            }
        } else {
            /*The edges of the scrollable can not be in the page (minus vpad) */
            if(scrl_cords.y2 < page_cords.y2 - vpad) {
               new_y =  lv_area_get_height(&page_cords) - lv_area_get_height(&scrl_cords) - vpad;   /* Bottom align */
               refr_y = true;
            }
            if (scrl_cords.y1  > page_cords.y1 + vpad) {
                new_y = vpad;  /*Top align*/
                refr_y = true;
            }
        }
        if(refr_x != false || refr_y != false) {
            lv_obj_set_pos(scrl, new_x, new_y);
        }

        lv_page_sb_refresh(page);
    }
    else if(sign == LV_SIGNAL_DRAG_END) {
        /*Hide scrollbars if required*/
        if(page_ext->sb.mode == LV_SB_MODE_DRAG) {
            lv_area_t sb_area_tmp;
            if(page_ext->sb.hor_draw) {
                lv_area_copy(&sb_area_tmp, &page_ext->sb.hor_area);
                sb_area_tmp.x1 += page->coords.x1;
                sb_area_tmp.y1 += page->coords.y1;
                sb_area_tmp.x2 += page->coords.x2;
                sb_area_tmp.y2 += page->coords.y2;
                lv_inv_area(&sb_area_tmp);
                page_ext->sb.hor_draw = 0;
            }
            if(page_ext->sb.ver_draw)  {
                lv_area_copy(&sb_area_tmp, &page_ext->sb.ver_area);
                sb_area_tmp.x1 += page->coords.x1;
                sb_area_tmp.y1 += page->coords.y1;
                sb_area_tmp.x2 += page->coords.x2;
                sb_area_tmp.y2 += page->coords.y2;
                lv_inv_area(&sb_area_tmp);
                page_ext->sb.ver_draw = 0;
            }
        }
    }
    else if(sign == LV_SIGNAL_PRESSED) {
        if(page_ext->pr_action != NULL) {
            page_ext->pr_action(page);
        }
    }
    else if(sign == LV_SIGNAL_RELEASED) {
        if(lv_indev_is_dragging(lv_indev_get_act()) == false) {
            if(page_ext->rel_action != NULL) {
                page_ext->rel_action(page);
            }
        }
    }

    return res;
}
Exemple #6
0
/**
 * Signal function of the page
 * @param page pointer to a page object
 * @param sign a signal type from lv_signal_t enum
 * @param param pointer to a signal specific variable
 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
 */
static lv_res_t lv_page_signal(lv_obj_t * page, lv_signal_t sign, void * param)
{
    lv_res_t res;

    /* Include the ancient signal function */
    res = ancestor_signal(page, sign, param);
    if(res != LV_RES_OK) return res;

    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
    lv_style_t * style = lv_obj_get_style(page);
    lv_obj_t * child;
    if(sign == LV_SIGNAL_CHILD_CHG) { /*Automatically move children to the scrollable object*/
        child = lv_obj_get_child(page, NULL);
        while(child != NULL) {
            if(lv_obj_is_protected(child, LV_PROTECT_PARENT) == false) {
                lv_obj_t * tmp = child;
                child = lv_obj_get_child(page, child); /*Get the next child before move this*/
                lv_obj_set_parent(tmp, ext->scrl);
            } else {
                child = lv_obj_get_child(page, child);
            }
        }
    }
    else if(sign == LV_SIGNAL_STYLE_CHG) {
        /*If no hor_fit enabled set the scrollable's width to the page's width*/
        if(lv_cont_get_hor_fit(ext->scrl) == false) {
            lv_obj_set_width(ext->scrl, lv_obj_get_width(page) - 2 * style->body.padding.hor);
        } else {
            ext->scrl->signal_func(ext->scrl, LV_SIGNAL_CORD_CHG, &ext->scrl->coords);
        }

        /*The scrollbars are important only if they are visible now*/
        if(ext->sb.hor_draw || ext->sb.ver_draw) lv_page_sb_refresh(page);

        /*Refresh the ext. size because the scrollbars might be positioned out of the page*/
        lv_obj_refresh_ext_size(page);
    }
    else if(sign == LV_SIGNAL_CORD_CHG) {
        /*Refresh the scrollbar and notify the scrl if the size is changed*/
        if(ext->scrl != NULL && (lv_obj_get_width(page) != lv_area_get_width(param) ||
                                 lv_obj_get_height(page) != lv_area_get_height(param)))
        {
            /*If no hor_fit enabled set the scrollable's width to the page's width*/
            if(lv_cont_get_hor_fit(ext->scrl) == false) {
                lv_obj_set_width(ext->scrl, lv_obj_get_width(page) - 2 * style->body.padding.hor);
            }

            ext->scrl->signal_func(ext->scrl, LV_SIGNAL_CORD_CHG, &ext->scrl->coords);

            /*The scrollbars are important only if they are visible now*/
            if(ext->sb.hor_draw || ext->sb.ver_draw) lv_page_sb_refresh(page);
        }
    }
    else if(sign == LV_SIGNAL_PRESSED) {
        if(ext->pr_action != NULL) {
            ext->pr_action(page);
        }
    }
    else if(sign == LV_SIGNAL_RELEASED) {
        if(lv_indev_is_dragging(lv_indev_get_act()) == false) {
            if(ext->rel_action != NULL) {
                ext->rel_action(page);
            }
        }
    }
    else if(sign == LV_SIGNAL_REFR_EXT_SIZE) {
        /*Ensure ext. size for the scrollbars if they are out of the page*/
        if(page->ext_size < (-ext->sb.style->body.padding.hor)) page->ext_size = -ext->sb.style->body.padding.hor;
        if(page->ext_size < (-ext->sb.style->body.padding.ver)) page->ext_size = -ext->sb.style->body.padding.ver;
    }
    else if(sign == LV_SIGNAL_GET_TYPE) {
        lv_obj_type_t * buf = param;
        uint8_t i;
        for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) {  /*Find the last set data*/
            if(buf->type[i] == NULL) break;
        }
        buf->type[i] = "lv_page";
    }

    return res;
}