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:
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Box.H>
#include <Fl/Fl_Value_Output.H>
#include <FL/fl_draw.h>
class Canvas : public Fl_Box // a canvas widget to draw and click on
{
public:
Canvas( int x, int y, int w, int h, const char *l); // constructor
~Canvas() {}; // destructor
int XC; // stores coordinates of the mouse
int YC;
void draw();
int handle(int event);
};
Canvas::Canvas( int x, int y, int w, int h, const char *l) : Fl_Box( x, y, w,
h, l)
{
box(FL_FLAT_BOX);
color((Fl_Color)255);
XC = 0; // initializing the variables
YC = 0;
}
void Canvas::draw() // overriding draw()
{
Fl_Box::draw();
fl_push_clip( x(), y(), w(), h() ); // star clipp
fl_color(FL_BLACK);
fl_rect( x(), y(), w(), h() ); // draws a contour to look better
fl_rect(XC,YC,50,20); // draws the rectangle
fl_pop_clip();
}
int Canvas::handle(int event) // handling events
{
switch(event)
{
case FL_FOCUS:
return 1;
break;
case FL_PUSH:
XC = Fl::event_x();
YC = Fl::event_y();
redraw();
return 1;
break;
default:
return Fl_Box::handle(event);
break;
}
}
struct Data // a simple struct to store the 2 Fl_Outputs to pass both
// in the same time to the callback
{
Fl_Value_Output *X_Out;
Fl_Value_Output *Y_Out;
};
void canvas_cb(Fl_Widget *w, void *v) // the callback from the Canvas widget
{
Canvas *c = (Canvas*) w;
Data *outputs = (Data*) v;
outputs->X_Out->value(c->XC);
outputs->X_Out->redraw();
outputs->Y_Out->value(c->YC);
outputs->Y_Out->redraw();
}
/*************************************************************************/
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
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??
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk