rt_inline rt_uint32_t _rtgui_scrollbar_get_thumb_position(rtgui_scrollbar_t* bar) { rt_uint32_t thumb_position; /* calculate thumb position */ thumb_position = (rtgui_scrollbar_get_value(bar) - bar->min_position) * _rtgui_scrollbar_get_length(bar) / (bar->max_position - bar->min_position); return thumb_position; }
rt_uint32_t get_scrollbar_pos(rtgui_scrollbar_t* bar) { double pos,ftmp; rt_uint32_t result; /* calculate thumb position */ pos = bar->value; ftmp = _rtgui_scrollbar_get_length(bar); pos *= ftmp; ftmp = bar->count; pos /= ftmp; result = pos; return result; }
static void _rtgui_scrollbar_on_mouseclick(struct rtgui_widget * widget, struct rtgui_event * event) { rtgui_rect_t btn_rect, bar_rect; rt_uint32_t thumb_size, thumb_position; struct rtgui_scrollbar* bar = (struct rtgui_scrollbar*)widget; struct rtgui_event_mouse* mouse = (struct rtgui_event_mouse*)event; /* get the thumb size and position */ thumb_size = bar->thumb_size * (bar->max_position - bar->min_position) / _rtgui_scrollbar_get_length(bar); thumb_position = _rtgui_scrollbar_get_thumb_position(bar); if (bar->orient == RTGUI_VERTICAL) { /* get up arrow button rect */ btn_rect.x1 = widget->extent.x1; btn_rect.x2 = widget->extent.x2; btn_rect.y1 = widget->extent.y1; btn_rect.y2 = widget->extent.y1 + (widget->extent.x2 - widget->extent.x1); if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) { if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { bar->status |= SBS_UPARROW; /* line step */ bar->thumb_position -= bar->line_step; if (bar->thumb_position < bar->min_position) bar->thumb_position = bar->min_position; } else if (mouse->button & RTGUI_MOUSE_BUTTON_UP) { bar->status = 0; } goto __exit; } /* get bar rect */ bar_rect.x1 = widget->extent.x1; bar_rect.x2 = widget->extent.x2; bar_rect.y1 = widget->extent.y1 + (widget->extent.x2 - widget->extent.x1); bar_rect.y2 = widget->extent.y2 - (widget->extent.x2 - widget->extent.x1); if (rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK) { if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { /* page step */ if (mouse->y < bar_rect.y1 + thumb_position) { bar->thumb_position -= bar->page_step; if (bar->thumb_position < bar->min_position) bar->thumb_position = bar->min_position; } else if (mouse->y > thumb_position + bar->thumb_size) { bar->thumb_position += bar->page_step; if (bar->thumb_position > bar->max_position - thumb_size) bar->thumb_position = bar->max_position - thumb_size; } } goto __exit; } /* get down arrow button rect */ btn_rect.y1 = widget->extent.y2 - (widget->extent.x2 - widget->extent.x1); btn_rect.y2 = widget->extent.y2; bar_rect.y1 = widget->extent.y1 + ((widget->extent.y2 - widget->extent.y1)/2); bar_rect.y2 = widget->extent.y2 - (widget->extent.x2 - widget->extent.x1); if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) { if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { bar->status |= SBS_DOWNARROW; /* line step */ bar->thumb_position += bar->line_step; if (bar->thumb_position > bar->max_position - thumb_size) bar->thumb_position = bar->max_position - thumb_size; } else if (mouse->button & RTGUI_MOUSE_BUTTON_UP) bar->status = 0; } } else { /* get left arrow button rect */ btn_rect.x1 = widget->extent.x1; btn_rect.x2 = widget->extent.x1 + (widget->extent.y2 - widget->extent.y1); btn_rect.y1 = widget->extent.y1; btn_rect.y2 = widget->extent.y2; if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) { if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { bar->status |= SBS_LEFTARROW; /* line step */ bar->thumb_position -= bar->line_step; if (bar->thumb_position < bar->min_position) bar->thumb_position = bar->min_position; } else if (mouse->button & RTGUI_MOUSE_BUTTON_UP) bar->status = 0; goto __exit; } /* get bar rect */ bar_rect.x1 = widget->extent.x1 + (widget->extent.y2 - widget->extent.y1); bar_rect.x2 = widget->extent.x2 - (widget->extent.y2 - widget->extent.y1); bar_rect.y1 = widget->extent.y1; bar_rect.y2 = widget->extent.y2; if (rtgui_rect_contains_point(&bar_rect, mouse->x, mouse->y) == RT_EOK) { if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { /* page step */ if (mouse->x < bar_rect.x1 + thumb_position) { bar->thumb_position -= bar->page_step; if (bar->thumb_position < bar->min_position) bar->thumb_position = bar->min_position; } else if (mouse->x > thumb_position + bar->thumb_size) { bar->thumb_position += bar->page_step; if (bar->thumb_position > bar->max_position - thumb_size) bar->thumb_position = bar->max_position - thumb_size; } } goto __exit; } /* get right arrow button rect */ btn_rect.x1 = widget->extent.x2 - (widget->extent.y2 - widget->extent.y1); btn_rect.x2 = widget->extent.x2; bar_rect.x1 = widget->extent.x1 + ((widget->extent.x2 - widget->extent.x1)/2); bar_rect.x2 = widget->extent.x2 - (widget->extent.y2 - widget->extent.y1); if (rtgui_rect_contains_point(&btn_rect, mouse->x, mouse->y) == RT_EOK) { if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { bar->status |= SBS_RIGHTARROW; /* line step */ bar->thumb_position += bar->line_step; if (bar->thumb_position > bar->max_position - bar->line_step) bar->thumb_position = bar->max_position - bar->line_step; } else if (mouse->button & RTGUI_MOUSE_BUTTON_UP) bar->status = 0; } } __exit: rtgui_theme_draw_scrollbar(bar); if ((mouse->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) == (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_DOWN)) { if (bar->on_scroll != RT_NULL) bar->on_scroll(widget, RT_NULL); } }
/* thumb chunk activity */ static void _rtgui_scrollbar_on_mousemotion(rtgui_scrollbar_t *bar, rtgui_event_t * event) { float tmppos; rt_uint32_t pos; struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event; rt_kprintf("sbar mouse motion.\n"); tmppos = _rtgui_scrollbar_get_length(bar); tmppos /= bar->count; pos = (rt_uint32_t)tmppos; if(bar->orient == RTGUI_VERTICAL) { if(bar->status & SBS_VERTTHUMB) { /* from then on mouseclick */ if((mouse->y-sbar_mouse_move_size.y) > 3) { bar->status |= SBS_DOWNTHUMB; } else if((mouse->y-sbar_mouse_move_size.y) < -3) { bar->status |= SBS_UPTHUMB; } else bar->status &= ~(SBS_UPTHUMB|SBS_DOWNTHUMB); if(abs(mouse->y-sbar_mouse_move_size.y) >= pos) { int step = abs(mouse->y-sbar_mouse_move_size.y)/pos; sbar_mouse_move_size.y = mouse->y; if(bar->status & SBS_UPTHUMB) { bar->value -= step; if(bar->value < 0) bar->value = 0; } else if(bar->status & SBS_DOWNTHUMB) { bar->value += step; if(bar->value > bar->count) bar->value = bar->count; } goto __exit; } } else if(bar->status & SBS_UPARROW) { /* on-going push down uparrow button */ if(bar->value==0)return; bar->value -= bar->line_step; if(bar->value < 0) bar->value = 0; goto __exit; } else if(bar->status & SBS_DOWNARROW) { /* on-going push down downarrow button */ if(bar->value==bar->count)return; bar->value += bar->line_step; if(bar->value > bar->count) bar->value = bar->count; goto __exit; } /*else if(bar->status & SBS_UPSPACE) { bar->value -= bar->page_step; if(bar->value < 0) bar->value = 0; goto __exit; } else if(bar->status & SBS_DOWNSPACE) { bar->value += bar->page_step; if(bar->value > bar->count) bar->value = bar->count; goto __exit; }*/ return; } else { if(bar->status & SBS_HORZTHUMB) {rt_kprintf("HORZTHUMB, move event\n"); if((mouse->x-sbar_mouse_move_size.x) > 5) { bar->status |= SBS_RIGHTTHUMB; } else if((mouse->x-sbar_mouse_move_size.x) < -5) { bar->status |= SBS_LEFTTHUMB; } if(abs(mouse->x-sbar_mouse_move_size.x) > pos) { int step = abs(mouse->x-sbar_mouse_move_size.x)/pos; sbar_mouse_move_size.x = mouse->x; if(bar->status & SBS_LEFTTHUMB) { bar->value -= step; if(bar->value < 0) bar->value = 0; } else if(bar->status & SBS_RIGHTTHUMB) { bar->value += step; if(bar->value > bar->count) bar->value = bar->count; } goto __exit; } } else if(bar->status & SBS_LEFTARROW) { if(bar->value==0)return; bar->value -= bar->line_step; if(bar->value < 0) bar->value = 0; goto __exit; } else if(bar->status & SBS_RIGHTARROW) { if(bar->value==bar->count)return; bar->value += bar->line_step; if(bar->value > bar->count) bar->value = bar->count; goto __exit; } /*else if(bar->status & SBS_LEFTSPACE) { bar->value -= bar->page_step; if(bar->value < bar->min) bar->value = bar->min; goto __exit; } else if(bar->status & SBS_RIGHTSPACE) { bar->value += bar->page_step; if(bar->value > bar->count-1) bar->value = bar->count-1; goto __exit; }*/ return; } __exit: rtgui_scrollbar_ondraw(bar); if(bar->widget_link != RT_NULL && bar->on_scroll != RT_NULL) { rtgui_widget_focus(bar->widget_link); bar->on_scroll(RTGUI_OBJECT(bar->widget_link), event); } }