From 07aae7ea69b7b6bdecbb7d3df96a07db2a528882 Mon Sep 17 00:00:00 2001 From: Liu Chao Date: Wed, 9 Mar 2016 22:31:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=BB=9A=E5=8A=A8=E6=9D=A1?= =?UTF-8?q?=EF=BC=8C=E8=A1=A5=E5=85=85=E9=BC=A0=E6=A0=87=E6=BB=9A=E8=BD=AE?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=93=8D=E5=BA=94=EF=BC=8C=E7=BA=A0=E6=AD=A3?= =?UTF-8?q?=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/LCUI/gui/widget/scrollbar.h | 5 ++- src/gui/widget/scrollbar.c | 50 +++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/include/LCUI/gui/widget/scrollbar.h b/include/LCUI/gui/widget/scrollbar.h index a1137fab..7d1ffc39 100644 --- a/include/LCUI/gui/widget/scrollbar.h +++ b/include/LCUI/gui/widget/scrollbar.h @@ -51,8 +51,11 @@ LCUI_API void ScrollBar_BindBox( LCUI_Widget w, LCUI_Widget box ); LCUI_API void ScrollBar_BindLayer( LCUI_Widget w, LCUI_Widget layer ); +/** 获取滚动条的位置 */ +LCUI_API int ScrollBar_GetPosition( LCUI_Widget w ); + /** 将与滚动条绑定的内容滚动至指定位置 */ -LCUI_API void ScrollBar_ScrollTo( LCUI_Widget w, int pos ); +LCUI_API void ScrollBar_SetPosition( LCUI_Widget w, int pos ); /** 设置滚动条的方向 */ LCUI_API void ScrollBar_SetDirection( LCUI_Widget w, int direction ); diff --git a/src/gui/widget/scrollbar.c b/src/gui/widget/scrollbar.c index 175efe87..7dcdd217 100644 --- a/src/gui/widget/scrollbar.c +++ b/src/gui/widget/scrollbar.c @@ -53,6 +53,7 @@ typedef struct LCUI_ScrollBarRec_ { int mouse_x, mouse_y; /**< 拖拽开始时的鼠标坐标 */ int direction; /**< 滚动条的方向(垂直或水平) */ int eids[2]; /**< 鼠标事件的绑定ID */ + int scroll_step; /**< 每次滚动的距离,主要针对使用鼠标滚轮触发的滚动 */ int pos; /**< 当前的位置 */ } LCUI_ScrollBarRec, *LCUI_ScrollBar; @@ -187,6 +188,7 @@ static void ScrollBar_OnInit( LCUI_Widget w ) Widget_Append( w, slider ); self->direction = SBD_VERTICAL; self->is_dragging = FALSE; + self->scroll_step = 40; self->slider = slider; self->layer = NULL; self->box = NULL; @@ -225,6 +227,19 @@ static void ScrollBar_UpdateSize( LCUI_Widget w ) Widget_UpdateStyle( slider, FALSE ); } +static void ScrollLayer_OnWheel( LCUI_Widget layer, LCUI_WidgetEvent *e, void *arg ) +{ + LCUI_Widget w = e->data; + LCUI_ScrollBar scrollbar = w->private_data; + int pos = ScrollBar_GetPosition( w ); + if( e->z_delta > 0 ) { + pos -= scrollbar->scroll_step; + } else { + pos += scrollbar->scroll_step; + } + ScrollBar_SetPosition( w, pos ); +} + static void ScrollBar_OnUpdateSize( LCUI_Widget box, LCUI_WidgetEvent *e, void *arg ) { ScrollBar_UpdateSize( e->data ); @@ -249,36 +264,57 @@ void ScrollBar_BindLayer( LCUI_Widget w, LCUI_Widget layer ) } scrollbar->layer = layer; Widget_BindEvent( layer, "resize", ScrollBar_OnUpdateSize, w, NULL ); + Widget_BindEvent( layer, "mousewheel", ScrollLayer_OnWheel, w, NULL ); ScrollBar_UpdateSize( w ); } -void ScrollBar_ScrollTo( LCUI_Widget w, int pos ) +int ScrollBar_GetPosition( LCUI_Widget w ) +{ + LCUI_ScrollBar scrollbar = w->private_data; + return scrollbar->pos; +} + +void ScrollBar_SetPosition( LCUI_Widget w, int pos ) { int box_size, size, slider_pos; LCUI_ScrollBar scrollbar = w->private_data; LCUI_Widget slider = scrollbar->slider; + LCUI_Widget layer = scrollbar->layer; + + if( !layer ) { + return; + } if( scrollbar->direction == SBD_HORIZONTAL ) { size = scrollbar->layer->width; box_size = scrollbar->box->box.content.width; - if( pos + size > box_size ) { - pos = box_size - size; + if( pos + box_size > size ) { + pos = size - box_size; + } + if( pos < 0 ) { + pos = 0; } scrollbar->pos = pos; slider_pos = w->box.content.width - slider->width; - slider_pos = slider_pos * pos / (box_size - size); + slider_pos = slider_pos * pos / (size - box_size); SetStyle( slider->custom_style, key_left, slider_pos, px ); + SetStyle( layer->custom_style, key_left, -pos, px ); } else { size = scrollbar->layer->height; box_size = scrollbar->box->box.content.height; - if( pos + size > box_size ) { - pos = box_size - size; + if( pos + box_size > size ) { + pos = size - box_size; + } + if( pos < 0 ) { + pos = 0; } scrollbar->pos = pos; slider_pos = w->box.content.height - slider->height; - slider_pos = slider_pos * pos / (box_size - size); + slider_pos = slider_pos * pos / (size - box_size); SetStyle( slider->custom_style, key_top, slider_pos, px ); + SetStyle( layer->custom_style, key_top, -pos, px ); } Widget_UpdateStyle( slider, FALSE ); + Widget_UpdateStyle( layer, FALSE ); } void ScrollBar_SetDirection( LCUI_Widget w, int direction )