Drag scrolling in a GTK+ app

瘦欲@ 提交于 2019-12-04 08:03:29

I was able to get something like this working once by starting a 5ms timer when the user presses the mouse button. In the timer I check where the mouse is and decide which way to scroll, including faster scrolling the closer you are to the edge. The result was very smooth scrolling, at least that's what I remember. Here the guts of it, its gtkmm/c++, but you should be able to get the gist of it:

static const int HOT_AREA = 24; 

// convert distance into scroll size.  The larger the 
// value, the faster the scrolling. 
static int accel_fn(int dist) {
    if (dist > HOT_AREA) 
        dist = HOT_AREA; 
    int dif =  dist / (HOT_AREA/4); 
    if (dif <= 0) dif = 1; 
    return dif; 
}


bool scrollerAddin::on_timeout() {
    int ptr_x, ptr_y; 
    o_scroller->get_pointer(ptr_x, ptr_y); 

    int vp_width = o_scroller->get_width(); 
    int vp_height = o_scroller->get_height(); 

    if (o_scroller->get_hscrollbar_visible())
        vp_height -= o_scroller->get_hscrollbar()->get_height(); 
    if (o_scroller->get_vscrollbar_visible())
        vp_width -= o_scroller->get_vscrollbar()->get_width(); 

    if (ptr_x < HOT_AREA)
        scroll_left(accel_fn(HOT_AREA-ptr_x)); 
    else if (ptr_x > vp_width - HOT_AREA)
        scroll_right(accel_fn(ptr_x - (vp_width - HOT_AREA))); 
    if (ptr_y < HOT_AREA)
        scroll_up(accel_fn(HOT_AREA - ptr_y)); 
    else if (ptr_y > vp_height - HOT_AREA)
        scroll_down(accel_fn(ptr_y - (vp_height - HOT_AREA))); 

    return true; 
}

The scroll functions merely adjust the appropriate Adjustment object by the argument.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!