Scrolling scroll in GTK + application

I am working on a GTK + application that uses goocanvas to display a graph on the screen. I am having problems with a good way to perform a drag and drop scroll.

Currently, the application saves the coordinates at which the user clicked, and then in the callback “motion notify”, makes goo_canvas_scroll_to () in a new position. The problem is that the picture is somewhat slow, and each pixel is moved by the mouse, I get a callback once. This causes the drawing to lag while moving the graph around.

Is there a good way to drag and drop the scroll, so it looks smoother and I could skip some redraws?

+5
source share
1 answer

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

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 simply adjust the corresponding Adjustment object by argument.

+4
source

All Articles