Thanx,
now i post the code: the rectangle moves only in diagonal, and if i want to
move the rectangle depending on mouse moving?
Thanx for your help!
Giuseppe.
#include "vw_box.h"
Fl_Double_Window *outer_win;
Fl_Shared_Image *img;
Fl_RGB_Image *rgbimg =NULL;
static double flinear(double val, double smin, double smax, double gmin, double
gmax)
{
if (smin == smax) return gmax;
else return gmin + (gmax - gmin) * (val - smin) / (smax - smin);
}
/***********************************************************************
*/
void vw_box::set_data(char *newdat, int W, int H, int D) {
iw = W; ih = H; id = D;
if (data) delete [] data;
data = newdat;
}
/***********************************************************************
*/
void vw_box::draw() {
if (!data) return;
short wd = w(); short ht =h();
short xoff =0; short yoff =0;
static short xp = 50, yp = 50, dx = 10, dy = 10;
xp += dx; yp += dy;
if (xp < x()) {xp = x(); dx = -dx;}
if ((xp/*+100*/) > (x()+wd)) {xp = (x()+wd/*-100*/); dx = -dx;}
if (yp < y()) {yp = y(); dy = -dy;}
if ((yp/*+100*/) > (y()+ht)) {yp = (y()+ht/*-100*/); dy = -dy;}
if (ih < ht) yoff = (ht - ih) / 2;
if (iw < wd) xoff = (wd - iw) / 2;
short xpos = x() + xoff;
short ypos =y() + yoff;
/* Ensure that the full window is redrawn */
Fl_Box::draw();
fl_push_no_clip();
/* Redraw the whole image */
fl_draw_image((const uchar *)data, xpos, ypos, iw, ih, id);
// draw a box on top of the image
fl_rect(xp, yp, 40, 40, FL_RED);
//fl_line(xp, yp, 100, 100, 100,100);
// int x1 = X + 4;
// int y1 = Y + 4;
// int w1 = W - 2 * 4;
// int h1 = H - 2 * 4;
// int xx = int(flinear(xvalue(), xmin, xmax, x1, x1+w1-1)+.5);
// int yy = int(flinear(yvalue(), ymin, ymax, y1, y1+h1-1)+.5);
// draw_box(box(), X, Y, W, H, color());
// fl_color(selection_color());
// fl_xyline(xp, yy, xp+w1);
// fl_yxline(xx, yp, yp+h1);
//......................
//int x1 = X + 4;
// int y1 = Y + 4;
// int w1 = W - 2 * 4;
// int h1 = H - 2 * 4;
// int xx = int(flinear(xvalue(), xmin, xmax, x1, x1+w1-1)+.5);
// int yy = int(flinear(yvalue(), ymin, ymax, y1, y1+h1-1)+.5);
// draw_box(box(), X, Y, W, H, color());
//
// fl_color(selection_color());
// fl_xyline(x1, yy, x1+w1,FL_RED);
// fl_yxline(xx, y1, y1+h1);
// fl_rect(550,400,x1,x1+w1, FL_RED);
//.......................
fl_pop_clip();
}
/***********************************************************************
*/
vw_box *box_1;
/***********************************************************************
*/
void load_file(const char *n) {
if (img) img->release();
img =Fl_Shared_Image::get(n);
if (!img) {
puts("Image file format not recognized!");
fflush(stdout);
return;
}
Fl_Image *temp;
if (img->w() > img->h()) temp =img->copy(box_1->w(), box_1->h()*
img->h() / img->w());
else temp =img->copy(box_1->w() * img->w() / img->h(),box_1->h());
img->release();
img =(Fl_Shared_Image *)temp;
box_1->image(img);
box_1->set_data((char *)img->data()[0], img->w(), img->h(),
img->d());
box_1->redraw();
}
/***********************************************************************
*/
void quit_cb(Fl_Button*, void*) {
outer_win->hide();
}
/***********************************************************************
*/
void animate_box(void*) {
box_1->redraw();
Fl::repeat_timeout(0.1, animate_box);
}
/***********************************************************************
*/
int vw_box::handle(int event, int X, int Y, int W, int H) {
switch (event) {
case FL_PUSH:
case FL_DRAG:
case FL_RELEASE: {
double x1 = X + 4;
double y1 = Y + 4;
double w1 = W - 2 * 4;
double h1 = H - 2 * 4;
double xx = flinear(Fl::event_x(), x1, x1+w1-1.0, xmin, xmax);
if (xstep_) xx = int(xx/xstep_+0.5) * xstep_;
if (xmin < xmax) {
if (xx < xmin) xx = xmin;
if (xx > xmax) xx = xmax;
} else {
if (xx > xmin) xx = xmin;
if (xx < xmax) xx = xmax;
}
double yy = flinear(Fl::event_y(), y1, y1+h1-1.0, ymin, ymax);
if (ystep_) yy = int(yy/ystep_+0.5) * ystep_;
if (ymin < ymax) {
if (yy < ymin) yy = ymin;
if (yy > ymax) yy = ymax;
} else {
if (yy > ymin) yy = ymin;
if (yy < ymax) yy = ymax;
}
if (value(xx, yy)) set_changed();}
if (!(when() & FL_WHEN_CHANGED ||
(when() & FL_WHEN_RELEASE && event == FL_RELEASE))) return 1;
if (changed() || when()&FL_WHEN_NOT_CHANGED) {
if (event == FL_RELEASE) clear_changed();
do_callback();
}
return 1;
default:
return 0;
}
}
int vw_box::handle(int e) {
return handle(e, x(), y(), w(), h());
}
int vw_box::value(double X, double Y) {
clear_changed();
if (X == xvalue_ && Y == yvalue_) return 0;
xvalue_ = X; yvalue_ = Y;
redraw();
return 1;
}
//-------------------------vw_box.h-------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <FL/Fl.H>
#include <FL/Fl_Box.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Shared_Image.H>
#include <Fl/fl_draw.H>
#include <FL/Fl_Positioner.H>
class vw_box : public Fl_Box {
double xmin, ymin;
double xmax, ymax;
double xvalue_, yvalue_;
double xstep_, ystep_;
protected:
void draw(void);
int iw, ih, id;
int X, Y, W, H;
char *data;
int handle(int, int, int, int, int);
public:
int value(double,double);
int handle(int);
//void draw(int, int, int, int);
vw_box(int X, int Y, int W, int H, const char *L=0) :
Fl_Box(X,Y,W,H,L)
{data =NULL;
X=X;
Y=Y;
W=W;
H=H;}
void set_data(char *newdat, int W, int H, int D);
double xvalue() const {return xvalue_;}
double yvalue() const {return yvalue_;}
};
>
> > ok, and how i do to extract the position of mouse? x and y coords?
>
> Several options are provided - check the docs, in particular
> Fl::event_x() and Fl::event_x_root() would be worth reading up on.
>
>
> > And if I want to change the dimension of rectangle?
>
> Just change the call to fl_rect(xp, yp, 100, 100, FL_RED); in the draw
> method, the two "100" values are the width and height of the rectangle.
>
> You might get some tips from checking the demo examples in the test
> folder of your fltk distribution, or by checking Greg's examples page:
>
> http://www.seriss.com/people/erco/fltk/
>
>
>
>
>
>
> SELEX Sensors and Airborne Systems Limited
> Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex=
> SS14 3EL
> A company registered in England & Wales. Company no. 02426132
> ********************************************************************
> This email and any attachments are confidential to the intended
> recipient and may also be privileged. If you are not the intended
> recipient please delete it from your system and notify the sender.
> You should not copy it or use it for any purpose nor disclose or
> distribute its contents to any other person.
> ********************************************************************
>
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk