FLTK logo

Re: [fltk.general] Suggestion for Point selector widget Fl_Point

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 All Forums  |  Back to fltk.general  ]
 
Previous Message ]New Message | Reply ]Next Message ]

Re: Suggestion for Point selector widget Fl_Point Ian MacArthur Oct 20, 2021  
 
On Tuesday, 19 October 2021 at 19:16:33 UTC+1 erco wrote:

On 10/19/21 11:00 AM, anmol wrote:

I am trying to create a widget that can be pointed into a box to select the pixel co-ordinates within. It has a click box next to it to activate the point selection crosshairs, and then the point co-ordinates inside the box will be selected.

Any suggestions ?


    I'd suggest deriving a widget from Fl_Group that implements this pixel coordinate picking
    behavior, handle() to track the mouse movement. If I remember correctly, in response to
    FL_ENTER, return 1 so your widget gets FL_MOVE events, which gives tells you where the
    mouse is whenever it moves. Save the x/y coords from FL_MOVE in your widget and trigger
    a redraw() so that your draw() routine can draw() the crosshairs over the child widget.


Or you can probably use ::belowmouse() to figure out the co-ordinates even relative to non-child widgets too, I think.
Hmm, testing 
OK, example follows. Seems to work.

/*
 * fltk-config --compile track-cursor.cxx
 */

/* Standard headers */
#include <stdlib.h>

/* Fltk headers */
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/platform.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Value_Output.H>
#include <FL/fl_draw.H>

// constants to define the view etc.
static const int win_size = 512;
// Output the mouse (x,y)
static Fl_Value_Output *valx = NULL;
static Fl_Value_Output *valy = NULL;

/*****************************************************************************/
class tracker_win : public Fl_Double_Window
{
public:
  tracker_win (int W, int H, const char *L = 0)
    : Fl_Double_Window(W, H, L) {
  }

private:
  int handle(int event);
};

// Event handling
int tracker_win::handle(int ev) {
  int res = Fl_Double_Window::handle(ev);
  switch (ev) {
    case FL_MOVE:
    {
      int X = Fl::event_x();
      int Y = Fl::event_y();
      Fl_Widget *tgt_widget = Fl::belowmouse ();
      int xw = tgt_widget->x();
      int yw = tgt_widget->y();
      int dx = X - xw;
      int dy = Y - yw;
      if (this == tgt_widget) {
        dx = X;
        dy = Y;
      }
      valx->value(dx);
      valy->value(dy);
    }
      res = 1;
      break;

    default:
      break;
  }
  return res;
} /* end of handle() method */
/*****************************************************************************/
static tracker_win *main_window = 0;

/*****************************************************************************/
// Quit button callback (closes the window)
void quit_cb(Fl_Button *b, void *) {
  main_window->hide();
}
/*****************************************************************************/
int main(int argc, char **argv)
{
  main_window = new tracker_win(win_size, win_size, "Mouse tracking demo");
  main_window->begin();
  main_window->box (FL_FLAT_BOX);

  Fl_Box *dummy_box = new Fl_Box (10, 10, 100, 100);
  dummy_box->box (FL_BORDER_BOX);

  Fl_Box *other_box = new Fl_Box (220, 220, 100, 100);
  other_box->box (FL_DOWN_BOX);

  valx = new Fl_Value_Output (130, 10, 40, 20, "X");
  valx->box (FL_THIN_DOWN_BOX);

  valy = new Fl_Value_Output (190, 10, 40, 20, "Y");
  valy->box (FL_THIN_DOWN_BOX);

  Fl_Button *quit_bt = new Fl_Button (10, 472, 60, 30, "Quit");
  quit_bt->box(FL_THIN_UP_BOX);
  quit_bt->callback((Fl_Callback *)quit_cb);

  main_window->end();
  main_window->resizable(main_window);

  main_window->show(argc, argv);

  return Fl::run();
} // main




--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/16e6e74c-6729-464e-95a6-bfdd698f8680n%40googlegroups.com.
Direct Link to Message ]
 
     
Previous Message ]New Message | Reply ]Next Message ]
 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.