BigW Consortium Gitlab

lv_page.c 25.6 KB
Newer Older
1 2 3 4 5 6 7 8
/**
 * @file lv_page.c
 * 
 */

/*********************
 *      INCLUDES
 *********************/
9
#include "lv_conf.h"
10 11
#if USE_LV_PAGE != 0

12
#include "misc/math/math_base.h"
13
#include "../lv_obj/lv_group.h"
14
#include "../lv_objx/lv_page.h"
15 16
#include "../lv_draw/lv_draw.h"
#include "../lv_obj/lv_refr.h"
17
#include "misc/gfx/anim.h"
18 19 20 21

/*********************
 *      DEFINES
 *********************/
22
#define LV_PAGE_SB_MIN_SIZE    (LV_DPI / 8)
23 24 25 26 27 28 29 30

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/
Gabor committed
31
static void lv_page_sb_refresh(lv_obj_t * main);
32 33
static bool lv_page_design(lv_obj_t * scrl, const area_t * mask, lv_design_mode_t mode);
static bool lv_scrl_design(lv_obj_t * scrl, const area_t * mask, lv_design_mode_t mode);
34 35
static lv_res_t lv_page_signal(lv_obj_t * page, lv_signal_t sign, void * param);
static lv_res_t lv_page_scrollable_signal(lv_obj_t * scrl, lv_signal_t sign, void * param);
36 37 38 39

/**********************
 *  STATIC VARIABLES
 **********************/
40
static lv_design_func_t ancestor_design;
41
static lv_signal_func_t ancestor_signal;
42

43 44 45 46 47 48 49 50 51 52
/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

/**
 * Create a page objects
Gabor committed
53 54
 * @param par pointer to an object, it will be the parent of the new page
 * @param copy pointer to a page object, if not NULL then the new object will be copied from it
55 56
 * @return pointer to the created page
 */
Gabor committed
57
lv_obj_t * lv_page_create(lv_obj_t * par, lv_obj_t * copy)
58
{
59
    /*Create the ancestor object*/
60
    lv_obj_t * new_page = lv_cont_create(par, copy);
Gabor committed
61
    dm_assert(new_page);
62 63
    if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_page);
    if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_page);
64

65
    /*Allocate the object type specific extended data*/
66
    lv_page_ext_t * ext = lv_obj_allocate_ext_attr(new_page, sizeof(lv_page_ext_t));
Gabor committed
67
    dm_assert(ext);
68 69 70
    ext->scrl = NULL;
    ext->pr_action = NULL;
    ext->rel_action = NULL;
71 72 73 74
    ext->sb.hor_draw = 0;
    ext->sb.ver_draw = 0;
    ext->sb.style = &lv_style_pretty;
    ext->sb.mode = LV_PAGE_SB_MODE_AUTO;
75

76
    /*Init the new page object*/
Gabor committed
77
    if(copy == NULL) {
78
	    ext->scrl = lv_cont_create(new_page, NULL);
79 80
	    lv_obj_set_signal_func(ext->scrl, lv_page_scrollable_signal);
        lv_obj_set_design_func(ext->scrl, lv_scrl_design);
81 82
		lv_obj_set_drag(ext->scrl, true);
		lv_obj_set_drag_throw(ext->scrl, true);
83
		lv_obj_set_protect(ext->scrl, LV_PROTECT_PARENT);
84
		lv_cont_set_fit(ext->scrl, false, true);
85

Gabor committed
86
		/* Add the signal function only if 'scrolling' is created
87
		 * because everything has to be ready before any signal is received*/
88 89
	    lv_obj_set_signal_func(new_page, lv_page_signal);
	    lv_obj_set_design_func(new_page, lv_page_design);
90 91 92 93

	    lv_page_set_style(new_page, LV_PAGE_STYLE_BG, &lv_style_pretty_color);
        lv_page_set_style(new_page, LV_PAGE_STYLE_SCRL, &lv_style_pretty);
        lv_page_set_style(new_page, LV_PAGE_STYLE_SB, &lv_style_pretty_color);
94
        lv_page_set_sb_mode(new_page, ext->sb.mode);
95
    } else {
96
    	lv_page_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
97
    	ext->scrl = lv_cont_create(new_page, copy_ext->scrl);
98
	    lv_obj_set_signal_func(ext->scrl, lv_page_scrollable_signal);
99

100 101 102
        lv_page_set_press_action(new_page, copy_ext->pr_action);
        lv_page_set_release_action(new_page, copy_ext->rel_action);
        lv_page_set_sb_mode(new_page, copy_ext->sb.mode);
103 104 105 106

        lv_page_set_style(new_page, LV_PAGE_STYLE_BG, lv_page_get_style(copy, LV_PAGE_STYLE_BG));
        lv_page_set_style(new_page, LV_PAGE_STYLE_SCRL, lv_page_get_style(copy, LV_PAGE_STYLE_SCRL));
        lv_page_set_style(new_page, LV_PAGE_STYLE_SB, lv_page_get_style(copy, LV_PAGE_STYLE_SB));
107

Gabor committed
108
		/* Add the signal function only if 'scrolling' is created
109
		 * because everything has to be ready before any signal is received*/
110 111
	    lv_obj_set_signal_func(new_page, lv_page_signal);
	    lv_obj_set_design_func(new_page, lv_page_design);
112

113
        /*Refresh the style with new signal function*/
114
        lv_obj_refresh_style(new_page);
115 116
    }
    
Gabor committed
117
    lv_page_sb_refresh(new_page);
118
                
Gabor committed
119
    return new_page;
120 121
}

Gabor committed
122 123 124 125
/*=====================
 * Setter functions
 *====================*/

126 127 128 129 130
/**
 * Set a release action for the page
 * @param page pointer to a page object
 * @param rel_action a function to call when the page is released
 */
131
void lv_page_set_release_action(lv_obj_t * page, lv_action_t rel_action)
132
{
133
	lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
134 135 136 137 138 139 140 141
	ext->rel_action = rel_action;
}

/**
 * Set a press action for the page
 * @param page pointer to a page object
 * @param pr_action a function to call when the page is pressed
 */
142
void lv_page_set_press_action(lv_obj_t * page, lv_action_t pr_action)
143
{
144
	lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
145 146 147
	ext->pr_action = pr_action;
}

148 149 150
/**
 * Set the scroll bar mode on a page
 * @param page pointer to a page object
151
 * @param sb.mode the new mode from 'lv_page_sb.mode_t' enum
152 153 154
 */
void lv_page_set_sb_mode(lv_obj_t * page, lv_page_sb_mode_t sb_mode)
{
155
    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
156 157 158 159
    ext->sb.mode = sb_mode;
    ext->sb.hor_draw = 0;
    ext->sb.ver_draw = 0;
    lv_page_sb_refresh(page);
160
    lv_obj_invalidate(page);
161 162 163
}

/**
164
 * Set a style of a page
165
 * @param page pointer to a page object
166 167 168 169
 * @param type which style should be set
 * @param style pointer to a style
 *  */
void lv_page_set_style(lv_obj_t *page, lv_page_style_t type, lv_style_t *style)
170
{
171
    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
172 173 174 175 176 177 178 179 180 181 182 183 184

    switch (type) {
        case LV_PAGE_STYLE_BG:
            lv_obj_set_style(page, style);
            break;
        case LV_PAGE_STYLE_SCRL:
            lv_obj_set_style(ext->scrl, style);
            break;
        case LV_PAGE_STYLE_SB:
            ext->sb.style = style;
            area_set_height(&ext->sb.hor_area, ext->sb.style->body.padding.inner);
            area_set_width(&ext->sb.ver_area, ext->sb.style->body.padding.inner);
            lv_page_sb_refresh(page);
185
            lv_obj_refresh_ext_size(page);
186 187
            lv_obj_invalidate(page);
            break;
188
    }
189 190 191 192 193
}

/*=====================
 * Getter functions
 *====================*/
194

195 196 197 198 199 200 201 202 203 204
/**
 * Get the scrollable object of a page
 * @param page pointer to a page object
 * @return pointer to a container which is the scrollable part of the page
 */
lv_obj_t * lv_page_get_scrl(lv_obj_t * page)
{
	lv_page_ext_t * ext = lv_obj_get_ext_attr(page);

	return ext->scrl;
205
}
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
/**
 * Set the scroll bar mode on a page
 * @param page pointer to a page object
 * @return the mode from 'lv_page_sb.mode_t' enum
 */
lv_page_sb_mode_t lv_page_get_sb_mode(lv_obj_t * page)
{
    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
    return ext->sb.mode;
}

/**
 * Get a style of a page
 * @param page pointer to page object
 * @param type which style should be get
 * @return style pointer to a style
 *  */
lv_style_t * lv_page_get_style(lv_obj_t *page, lv_page_style_t type)
{
    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);

    switch (type) {
        case LV_PAGE_STYLE_BG:     return lv_obj_get_style(page);
        case LV_PAGE_STYLE_SCRL:   return lv_obj_get_style(ext->scrl);
        case LV_PAGE_STYLE_SB:     return ext->sb.style;
        default: return NULL;
    }

    /*To avoid warning*/
    return NULL;
}

/*=====================
 * Other functions
 *====================*/

243 244
/**
 * Glue the object to the page. After it the page can be moved (dragged) with this object too.
245
 * @param obj pointer to an object on a page
Gabor committed
246
 * @param glue true: enable glue, false: disable glue
247
 */
248
void lv_page_glue_obj(lv_obj_t * obj, bool glue)
249
{
250 251
    lv_obj_set_drag_parent(obj, glue);
    lv_obj_set_drag(obj, glue);
252 253
}

254 255 256 257
/**
 * Focus on an object. It ensures that the object will be visible on the page.
 * @param page pointer to a page object
 * @param obj pointer to an object to focus (must be on the page)
Gabor committed
258
 * @param anim_time scroll animation time in milliseconds (0: no animation)
259
 */
260
void lv_page_focus(lv_obj_t * page, lv_obj_t * obj, uint16_t anim_time)
261
{
262 263 264
    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
    lv_style_t * style = lv_page_get_style(page, LV_PAGE_STYLE_BG);
    lv_style_t * style_scrl = lv_page_get_style(page, LV_PAGE_STYLE_SCRL);
265

266 267 268 269
    cord_t obj_y = obj->coords.y1 - ext->scrl->coords.y1;
    cord_t obj_h = lv_obj_get_height(obj);
    cord_t scrlable_y = lv_obj_get_y(ext->scrl);
    cord_t page_h = lv_obj_get_height(page);
270

271 272
    cord_t top_err = -(scrlable_y + obj_y);
    cord_t bot_err = scrlable_y + obj_y + obj_h - page_h;
273

274
    /*If obj is higher then the page focus where the "error" is smaller*/
275

276 277 278 279 280 281 282 283 284 285
    /*Out of the page on the top*/
    if((obj_h <= page_h && top_err > 0) ||
       (obj_h > page_h && top_err < bot_err)) {
        /*Calculate a new position and let some space above*/
        scrlable_y = -(obj_y - style_scrl->body.padding.ver - style->body.padding.ver);
        scrlable_y += style_scrl->body.padding.ver;
    }
    /*Out of the page on the bottom*/
    else if((obj_h <= page_h && bot_err > 0) ||
            (obj_h > page_h && top_err >= bot_err)) {
286
        /*Calculate a new position and let some space below*/
287 288
        scrlable_y = -obj_y;
        scrlable_y += page_h - obj_h;
289
        scrlable_y -= style_scrl->body.padding.ver;
290 291 292 293
    } else {
        /*Already in focus*/
        return;
    }
294

295
    if(anim_time == 0) {
296
        lv_obj_set_y(ext->scrl, scrlable_y);
297 298 299 300 301 302
    }
    else {
        anim_t a;
        a.act_time = 0;
        a.start = lv_obj_get_y(ext->scrl);
        a.end = scrlable_y;
Gabor committed
303
        a.time = anim_time;
304 305 306 307 308 309 310 311
        a.end_cb = NULL;
        a.playback = 0;
        a.repeat = 0;
        a.var = ext->scrl;
        a.path = anim_get_path(ANIM_PATH_LIN);
        a.fp = (anim_fp_t) lv_obj_set_y;
        anim_create(&a);
    }
312
}
Gabor committed
313

314 315 316 317
/**********************
 *   STATIC FUNCTIONS
 **********************/

318 319
/**
 * Handle the drawing related tasks of the pages
Gabor committed
320
 * @param page pointer to an object
321 322 323 324 325 326 327
 * @param mask the object will be drawn only in this area
 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
 *                                  (return 'true' if yes)
 *             LV_DESIGN_DRAW: draw the object (always return 'true')
 *             LV_DESIGN_DRAW_POST: drawing after every children are drawn
 * @param return true/false, depends on 'mode'
 */
328
static bool lv_page_design(lv_obj_t * scrl, const area_t * mask, lv_design_mode_t mode)
329 330
{
    if(mode == LV_DESIGN_COVER_CHK) {
331
    	return ancestor_design(scrl, mask, mode);
332
    } else if(mode == LV_DESIGN_DRAW_MAIN) {
333
		ancestor_design(scrl, mask, mode);
334
	} else if(mode == LV_DESIGN_DRAW_POST) { /*Draw the scroll bars finally*/
335
		ancestor_design(scrl, mask, mode);
336
		lv_page_ext_t * ext = lv_obj_get_ext_attr(scrl);
337 338

		/*Draw the scrollbars*/
339
		area_t sb_area;
340
		if(ext->sb.hor_draw) {
341
		    /*Convert the relative coordinates to absolute*/
342
            area_cpy(&sb_area, &ext->sb.hor_area);
343 344 345 346
		    sb_area.x1 += scrl->coords.x1;
            sb_area.y1 += scrl->coords.y1;
            sb_area.x2 += scrl->coords.x1;
            sb_area.y2 += scrl->coords.y1;
347
			lv_draw_rect(&sb_area, mask, ext->sb.style);
348 349
		}

350
		if(ext->sb.ver_draw) {
351
            /*Convert the relative coordinates to absolute*/
352
            area_cpy(&sb_area, &ext->sb.ver_area);
353 354 355 356
            sb_area.x1 += scrl->coords.x1;
            sb_area.y1 += scrl->coords.y1;
            sb_area.x2 += scrl->coords.x1;
            sb_area.y2 += scrl->coords.y1;
357
			lv_draw_rect(&sb_area, mask, ext->sb.style);
358 359 360 361 362
		}
	}

	return true;
}
363

364 365 366 367 368 369 370 371 372 373 374 375 376
/**
 * Handle the drawing related tasks of the scrollable object
 * @param scrl pointer to an object
 * @param mask the object will be drawn only in this area
 * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area
 *                                  (return 'true' if yes)
 *             LV_DESIGN_DRAW: draw the object (always return 'true')
 *             LV_DESIGN_DRAW_POST: drawing after every children are drawn
 * @param return true/false, depends on 'mode'
 */
static bool lv_scrl_design(lv_obj_t * scrl, const area_t * mask, lv_design_mode_t mode)
{
    if(mode == LV_DESIGN_COVER_CHK) {
377
        return ancestor_design(scrl, mask, mode);
378 379 380 381 382 383 384 385 386
    } else if(mode == LV_DESIGN_DRAW_MAIN) {
#if LV_OBJ_GROUP != 0
        /* If the page is the active in a group and
         * the background (page) is not visible (transparent or empty)
         * then activate the style of the scrollable*/
        lv_style_t * style_ori = lv_obj_get_style(scrl);
        lv_obj_t * page = lv_obj_get_parent(scrl);
        lv_style_t * style_page = lv_obj_get_style(page);
        lv_group_t * g = lv_obj_get_group(page);
387
        if(style_page->body.empty != 0 || style_page->body.opa == OPA_TRANSP) { /*Background is visible?*/
388
            if(lv_group_get_focused(g) == page) {
389
                lv_style_t * style_mod;
390
                style_mod = lv_group_mod_style(g, style_ori);
391 392 393 394
                scrl->style_p = style_mod;  /*Temporally change the style to the activated */
            }
        }
#endif
395
        ancestor_design(scrl, mask, mode);
396 397 398 399 400

#if LV_OBJ_GROUP != 0
        scrl->style_p = style_ori;  /*Revert the style*/
#endif
    } else if(mode == LV_DESIGN_DRAW_POST) {
401
        ancestor_design(scrl, mask, mode);
402 403 404 405 406
    }

    return true;
}

407 408 409 410 411 412 413 414 415 416 417 418 419
/**
 * 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);
420
    if(res != LV_RES_OK) return res;
421

422 423 424 425 426 427 428 429 430 431 432 433
    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);
434 435
            }
        }
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    }
    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) != area_get_width(param) ||
                                 lv_obj_get_height(page) != area_get_height(param)))
        {
456 457 458 459 460
            /*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);
            }

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

463 464 465
            /*The scrollbars are important only if they are visible now*/
            if(ext->sb.hor_draw || ext->sb.ver_draw) lv_page_sb_refresh(page);
        }
466 467 468 469
    }
    else if(sign == LV_SIGNAL_PRESSED) {
        if(ext->pr_action != NULL) {
            ext->pr_action(page);
470
        }
471 472 473 474 475
    }
    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);
476 477
            }
        }
478 479 480 481 482
    }
    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;
483 484
    }

485
    return res;
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
}

/**
 * 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);
501
    if(res != LV_RES_OK) return res;
502

503 504 505
    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);
506

507 508 509 510 511
    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);
        }
512

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
        /*Limit the position of the scrollable object to be always visible
         * (Do not let its edge inner then its parent respective edge)*/
        cord_t new_x;
        cord_t new_y;
        bool refr_x = false;
        bool refr_y = false;
        area_t page_cords;
        area_t scrl_cords;
        cord_t hpad = page_style->body.padding.hor;
        cord_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(area_get_width(&scrl_cords) + 2 * hpad < area_get_width(&page_cords)) {
            if(scrl_cords.x1 != page_cords.x1 + hpad) {
                new_x = hpad;
                refr_x = true;
534
            }
535 536 537 538 539
        } else {
            /*The edges of the scrollable can not be in the page (minus hpad) */
            if(scrl_cords.x2  < page_cords.x2 - hpad) {
               new_x =  area_get_width(&page_cords) - area_get_width(&scrl_cords) - hpad;   /* Right align */
               refr_x = true;
540
            }
541 542 543 544 545
            if (scrl_cords.x1 > page_cords.x1 + hpad) {
                new_x = hpad;  /*Left align*/
                refr_x = true;
            }
        }
546

547 548 549 550 551
        /*scrollable height smaller then page height? -> align to left*/
        if(area_get_height(&scrl_cords) + 2 * vpad < area_get_height(&page_cords)) {
            if(scrl_cords.y1 != page_cords.y1 + vpad) {
                new_y = vpad;
                refr_y = true;
552
            }
553 554 555 556 557
        } else {
            /*The edges of the scrollable can not be in the page (minus vpad) */
            if(scrl_cords.y2 < page_cords.y2 - vpad) {
               new_y =  area_get_height(&page_cords) - area_get_height(&scrl_cords) - vpad;   /* Bottom align */
               refr_y = true;
558
            }
559 560 561
            if (scrl_cords.y1  > page_cords.y1 + vpad) {
                new_y = vpad;  /*Top align*/
                refr_y = true;
562 563
            }
        }
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
        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_PAGE_SB_MODE_DRAG) {
            area_t sb_area_tmp;
            if(page_ext->sb.hor_draw) {
                area_cpy(&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;
582
            }
583 584 585 586 587 588 589 590 591 592 593 594 595 596
            if(page_ext->sb.ver_draw)  {
                area_cpy(&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);
597
        }
598 599 600 601 602
    }
    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);
603 604 605 606
            }
        }
    }

607
    return res;
608
}
609

610

611
/**
Gabor committed
612 613
 * Refresh the position and size of the scroll bars.
 * @param page pointer to a page object
614
 */
Gabor committed
615
static void lv_page_sb_refresh(lv_obj_t * page)
616
{
617

618
    lv_page_ext_t * ext = lv_obj_get_ext_attr(page);
619 620
    lv_style_t * style = lv_obj_get_style(page);
    lv_obj_t * scrl = ext->scrl;
621
    cord_t size_tmp;
622 623
    cord_t scrl_w = lv_obj_get_width(scrl);
    cord_t scrl_h =  lv_obj_get_height(scrl);
624 625
    cord_t hpad = style->body.padding.hor;
    cord_t vpad = style->body.padding.ver;
Gabor committed
626 627 628
    cord_t obj_w = lv_obj_get_width(page);
    cord_t obj_h = lv_obj_get_height(page);

629 630 631 632 633 634
    /*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  */
    cord_t sb_hor_pad = MATH_MAX(ext->sb.style->body.padding.inner, style->body.padding.hor);
    cord_t sb_ver_pad = MATH_MAX(ext->sb.style->body.padding.inner, style->body.padding.ver);
635

636 637 638 639 640
    if(ext->sb.mode == LV_PAGE_SB_MODE_OFF) return;

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

643
    /*Invalidate the current (old) scrollbar areas*/
644
    area_t sb_area_tmp;
645 646
    if(ext->sb.hor_draw != 0) {
        area_cpy(&sb_area_tmp, &ext->sb.hor_area);
647 648 649 650
        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;
651 652
        lv_inv_area(&sb_area_tmp);
    }
653 654
    if(ext->sb.ver_draw != 0)  {
        area_cpy(&sb_area_tmp, &ext->sb.ver_area);
655 656 657 658
        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;
659 660
        lv_inv_area(&sb_area_tmp);
    }
661

662 663 664 665 666 667 668 669

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

    }

670
    /*Horizontal scrollbar*/
671
    if(scrl_w <= obj_w - 2 * hpad) {        /*Full sized scroll bar*/
672 673 674
        area_set_width(&ext->sb.hor_area, obj_w - 2 * sb_hor_pad);
        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_PAGE_SB_MODE_AUTO || ext->sb.mode == LV_PAGE_SB_MODE_DRAG)  ext->sb.hor_draw = 0;
675
    } else {
676 677 678
        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;
        area_set_width(&ext->sb.hor_area,  size_tmp);
679

680 681 682 683
        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);
684

685
        if(ext->sb.mode == LV_PAGE_SB_MODE_AUTO || ext->sb.mode == LV_PAGE_SB_MODE_DRAG)  ext->sb.hor_draw = 1;
686 687 688
    }
    
    /*Vertical scrollbar*/
689
    if(scrl_h <= obj_h - 2 * vpad) {        /*Full sized scroll bar*/
690 691 692
        area_set_height(&ext->sb.ver_area,  obj_h - 2 * sb_ver_pad);
        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_PAGE_SB_MODE_AUTO || ext->sb.mode == LV_PAGE_SB_MODE_DRAG)  ext->sb.ver_draw = 0;
693
    } else {
694 695 696
        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;
        area_set_height(&ext->sb.ver_area,  size_tmp);
697

698 699 700
        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)) /
701
                                      (scrl_h + 2 * vpad - obj_h ));
702

703
        if(ext->sb.mode == LV_PAGE_SB_MODE_AUTO || ext->sb.mode == LV_PAGE_SB_MODE_DRAG)  ext->sb.ver_draw = 1;
704
    }
705 706

    /*Invalidate the new scrollbar areas*/
707 708
    if(ext->sb.hor_draw != 0) {
        area_cpy(&sb_area_tmp, &ext->sb.hor_area);
709 710 711 712
        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;
713 714
        lv_inv_area(&sb_area_tmp);
    }
715 716
    if(ext->sb.ver_draw != 0)  {
        area_cpy(&sb_area_tmp, &ext->sb.ver_area);
717 718 719 720
        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;
721 722
        lv_inv_area(&sb_area_tmp);
    }
723
}
724

725
#endif