在GTK +应用程序中拖动滚动 [英] Drag scrolling in a GTK+ app

查看:125
本文介绍了在GTK +应用程序中拖动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用goocanvas在屏幕上显示图形的GTK +应用程序。我有问题想出一个很好的方式来实现拖动滚动。

I am working on a GTK+ application that uses goocanvas to display a graph on screen. I am having problems coming up with a good way to implement drag scrolling.

目前,应用程序保存用户点击的坐标,然后保存动作通知信号回调,做goo_canvas_scroll_to()到新的位置。问题在于绘图速度有点慢,并且每一个像素都被鼠标移动,我得到的回调被调用一次。当拖动图形时,这会使绘图滞后。

Currently the app saves the coordinates where the user clicked and then in a "motion-notify" signal callback, does goo_canvas_scroll_to() to the new position. The problem is that drawing is somewhat slow, and with each pixel moved by the mouse, I get the callback invoked once. This makes the drawing lag behind when dragging the graph around.

有没有一种很好的拖动滚动方式,所以它看起来更加平滑,我可以跳过一些重绘?

Is there a good way to do drag scrolling, so it'd appear more smooth and I could skip some of the redraws?

推荐答案

当用户按下鼠标按钮时,我可以通过启动5ms定时器。在计时器中,我检查鼠标的位置,并决定滚动的方式,包括更快地滚动距离边缘越近。结果是非常顺利的滚动,至少这是我记得的。这里有它的gtkmm / c ++,但你应该能够得到它的要点:

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.

这篇关于在GTK +应用程序中拖动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆