andrei_c wrote:
> Hy, I ran into some trouble whit a program that is supposed to display the X
> and Y coordinates of the mouse relative to the widget it's clicked on when
> the mouse button is pressed.The X and Y coords should be displayed in 2
> Fl_Output widgets.And the program should even draws a rectangle relative to
> the coords of the mouse when the button is pressed.
> Here's what I got so far, I don't know why it doesn't work:
[...]
The callback will never be called, unless ... you call it ;-)
If you write your own handle() method, then you must also call the
callback from this handle() method. It's up to you when() to call it,
but you must do it. Please see below.
> int Canvas::handle(int event) // handling events
> {
> switch(event)
> {
> case FL_FOCUS:
> return 1;
> break;
The following 3 cases are optional, try without them first !
// case FL_ENTER: // enable, if you want FL_MOVE events
// case FL_MOVE: // enable, if you want FL_MOVE events
// case FL_DRAG: // enable, if you want to see dragging
> case FL_PUSH:
> XC = Fl::event_x();
> YC = Fl::event_y();
// if (when() & <something>) // test, if you call the callback
do_callback(); // <<< added here <<<
> redraw();
> return 1;
> break;
> default:
> return Fl_Box::handle(event);
> break;
> }
> }
[...]
>
> /*************************************************************************/
> int main()
> {
> Fl_Double_Window *my_win = new Fl_Double_Window(300,350, "Draw mouse
> coords");
> my_win->begin();
>
> Canvas my_canvas(20,20,260,260,0);
> Fl_Value_Output x_coord(30,290,40,20,"X");
> Fl_Value_Output y_coord(30,320,40,20,"Y");
>
> Data xy_data;
> xy_data.X_Out = &x_coord; // asign the adresses
> xy_data.Y_Out = &y_coord;
>
> my_canvas.when(FL_PUSH); // just to make shore the callback happens
FL_PUSH is not a valid value for when(), please read the docs. However
it doesn't matter for now, because you don't use it in your handle()
method anyway.
> my_canvas.callback(canvas_cb, &xy_data);
>
> my_win->end();
> my_win->show();
>
> //Fl::focus(&my_canvas); // not needed
>
> return Fl::run();
> }
> /*************************************************************************/
>
> Unfortunately, even if I set the callback to FL_PUSH trough the when()
> function, it still doesn't work.What am I doing wrong??
If you enable FL_ENTER, FL_MOVE, and FL_DRAG, then you can see the mouse
coordinates change, even if the mouse is outside the canvas area while
you _drag_ it (hold the button).
You should maybe read chapter 6 of the docs (again).
http://www.fltk.org/doc-1.1/events.html
And, BTW. the coordinates are _not_ relative to the canvas, but I'll
leave this up to you.
Other than that: nice demo ;-)
Albrecht
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk