c++fltk

How do I handle simultaneous Left and Right drags in C++ FLTK


I am using FLTK 1.3.3, when implementing the Fl_Widget::Handle() function, I am trying to handle simultaneous left-click drags and right-click drags, however, the events I get is not what I would expect. The problem appears when I hold down both left and right click, then release one of them, then continue to drag. I am still dragging with a click but I receive FL_MOVE instead.

Action Expected Event Actual Event
Left-click down FL_PUSH FL_PUSH
Right-click down FL_PUSH FL_PUSH
Dragging FL_DRAG FL_DRAG
Left-click up FL_RELEASE FL_RELEASE
More dragging FL_DRAG > FL_MOVE <
Right-click up FL_RELEASE FL_RELEASE

This is preventing my code from working properly since I'm still holding the click and dragging but the event doesn't represent it.

Here is a sample of the code that illustrates the issue:

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Widget.H>
#include <FL/names.h>   // fl_eventnames[]
#include <FL/fl_draw.H>
#include <stdio.h>

class MyWidget : public Fl_Widget {
public:
    MyWidget(int X,int Y,int W,int H) : Fl_Widget(X,Y,W,H) { }
    int handle(int event) {
        int ret = Fl_Widget::handle(event);                         // let Fl_Widget access events too

        printf("Event was %s (%d)\n", fl_eventnames[event], event); // For illustrating the bug.
        switch(event) {
            case FL_ENTER:
                return 1;

            case FL_PUSH:
                if (Fl::event_button()>1) printf("Right Click\n");
                else                      printf("Left Click\n");
                return 1;
            case FL_DRAG:
                return 1;
            case FL_RELEASE:
                return 1;
            case FL_MOVE:
                return 1;
        }
        return ret;     // return Fl_Widget::handle()'s value
    }
    void draw() {
      fl_color(color());
      fl_rectf(x(),y(),w(),h());
    }
};

int main() {
    Fl_Window win(300,300);
    MyWidget widget(10,10,100,100);
    widget.color(FL_RED);
    win.show();
    return Fl::run();
}

How can I keep track of simultaneous left and right drags properly? Preferably through FLTK itself instead of working around it by keeping track of held clicks with custom code.


Solution

  • For the record:

    This issue has been fixed in FLTK 1.4.0 which will be released soon. You can download a current snapshot from the FLTK download page or use git (branch master).

    FLTK 1.3.9 still shows the bug though (and will not be fixed).