Hi,

Generally when we drag the mouse on some scroll bar or any other control then 
mouse drag function is called 2 time. In first call value of x(horizontal) is 
the value where we start dragging and in second call value of x is bigger 
value(upto where we move the mouse).
But in my code drag function call only one time. And due to this dragging is 
very slow. Please check the below code where i'm drawing some lines on mouse 
dragging. Program contains one scroll box. To change the size of scroll box use 
up and down arrow keys.After reducing the size you may drag the scroll box by 
mouse.

// dragmouse.cpp : Defines the entry point for the console application.
//
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Group.H>
#include <FL/fl_draw.H>

class CLineDraw:public Fl_Box
{
        bool IsDrawing;
public:
        bool LeftClick_ScrollBar;
        void draw();

        CLineDraw(int x,int y,int w,int h);

        ~CLineDraw(){}
};

CLineDraw::CLineDraw(int x,int y,int w,int h):Fl_Box(x,y,w,h)
{
        //end();
        IsDrawing = false;
        LeftClick_ScrollBar = false;
}

void CLineDraw::draw()
{
        //return;

        Fl_Box::draw();

        if(!LeftClick_ScrollBar)
                return;

        if(IsDrawing)
                return;
        IsDrawing = true;

        fl_color(255,0,0);
        for (int i=0; i < w(); i+=2)
        {
                fl_line(x() + i,y(),x() + i,y() + h()-10);
        }
        IsDrawing = false;
}

class MainWindow:public Fl_Double_Window
{
        int m_nLastX,m_nLastY;
        bool Isdrawing;
        Fl_Box* m_pScaleBox;
        CLineDraw* w_Line;
        Fl_Group *m_pWindowsGroup;
        int startpos,DrawingArea;

        void Event_MouseDrag();
        void Event_MouseLeftClick();
        void Event_MouseRelease();
public:
        MainWindow();
        ~MainWindow(){}

        void draw();

        int handle(int e)
        {
                try
                {
                        int evnt = 0;
                        switch(e)
                        {
                                case FL_DRAG:
                                        Event_MouseDrag();
                                        break;

                                case FL_PUSH:
                                        evnt = Fl::event_button();
                                        if(evnt == FL_LEFT_MOUSE )
                                                Event_MouseLeftClick();
                                        break;

                                case FL_RELEASE:
                                        Event_MouseRelease();
                                        break;

                                case FL_KEYDOWN:
                                        evnt = Fl::event_key();
                                        if(evnt != 0)
                                        {       // use up and down arrow keys 
to reduce scroll box size
                                                if(evnt == FL_Up)
                                                {
                                                        DrawingArea /= 2;
                                                        redraw();
                                                }
                                                else if(evnt == FL_Down)
                                                {
                                                        DrawingArea *= 2;
                                                        redraw();
                                                }
                                        }
                        }
                        return Fl_Double_Window::handle(e);
                }
                catch(...)
                {
                }
        }
};

MainWindow::MainWindow():Fl_Double_Window(500, 200)
{
    MainWindow *o = this;
        o->Fl_Group::current()->resizable(o);

        {
                Fl_Group* o = new Fl_Group(0, 0, 550, 470);
                o->color((Fl_Color)15);
                o->box(FL_UP_BOX);

                {
                        Fl_Box* o = new Fl_Box(0, 10, 500, 15);
                        o->box(FL_BORDER_BOX);
                        o->color((Fl_Color)16);
                        o->labeltype(FL_NO_LABEL);
                        o->show();
                        Fl_Group::current()->resizable(o);
                        m_pScaleBox = o;
                }

                {       CLineDraw* o = new CLineDraw(0, 100, 500, 40);
                        o->box(FL_BORDER_BOX);
                        o->color((Fl_Color)48);
                        o->labeltype(FL_NO_LABEL);
                        o->show();
                        Fl_Group::current()->resizable(o);
                        w_Line = o;
                }
                o->end();
                m_pWindowsGroup = o;
        }
        end();
        DrawingArea = 500;
        startpos = 0;

        Isdrawing = false;
}

void MainWindow::draw()
{
        Fl_Double_Window::draw();

        Fl::visible_focus(0);
        if(Isdrawing)
                return;
        Isdrawing = true;

    {
                int w = w_Line->w();
                int total_width = DrawingArea - startpos;
        int scrollwidth =w*total_width/500 ;

                int scalepos =(double)startpos/(double)500 *w;

                if(scrollwidth>=w)
                        scrollwidth=w;

                m_pScaleBox->resize(scalepos ,m_pScaleBox->y(), scrollwidth, 
m_pScaleBox->h());


        fl_color(50,50,145);
        fl_line_style(0, 4);

        fl_rect(m_pScaleBox->x(),m_pScaleBox->y(),scrollwidth,m_pScaleBox->h());
        fl_line_style(0, 1);
    }
        w_Line->redraw();
        Isdrawing = false;
        w_Line->LeftClick_ScrollBar = false;
        m_nLastX = 0 ;
        m_nLastY = 0 ;
}

void MainWindow::Event_MouseLeftClick()
{
        int x = Fl::event_x();

        int y = Fl::event_y();

        if(y >= m_pScaleBox->y() && y <= m_pScaleBox->h() + m_pScaleBox->y() && 
x>=m_pScaleBox->x() && x<=m_pScaleBox->x() + m_pScaleBox->w())
                w_Line->LeftClick_ScrollBar = true;

        m_nLastX = x ;
        m_nLastY = y ;
}

void MainWindow::Event_MouseDrag()
{
        int x = Fl::event_x();

        int y = Fl::event_y();

        if(w_Line->LeftClick_ScrollBar)
        {
                int time = DrawingArea - startpos;
                int w = w_Line->w();
                if(x>m_pWindowsGroup->x() && 
y>m_pWindowsGroup->y())//m_pScaleBox->x() + m_pScaleBox->w() && x< this->w()
                {
                        int st = startpos+double(x - m_nLastX)/(double)w * 500;
                        int en = st+time;

                        if(en<=500 && st>=0)
                        {
                                startpos = st;
                                DrawingArea = en;

                                redraw();

                        }
                        else if(st < 0)
                        {
                                DrawingArea -= startpos;
                                startpos = 0;
                        }
                        else if(en > 500)
                        {
                                startpos += (500 - DrawingArea);
                                DrawingArea = 500;
                        }
                        //redraw();
                }
        }

        m_nLastX = x ;
        m_nLastY = y ;
}

void MainWindow::Event_MouseRelease()
{
        w_Line->LeftClick_ScrollBar = false;
        int x = Fl::event_x();

        int y = Fl::event_y();

        redraw();
        m_nLastX = x ;
        m_nLastY = y ;
}


int main(int argc, char* argv[])
{
        MainWindow *main_window;
        main_window=new MainWindow();
        main_window->size_range(500,200);
        main_window->show(argc, argv);
        return Fl::run();
}



_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to