> now i post the code: the rectangle moves only in diagonal, 
> and if i want  to move the rectangle depending on mouse moving?

Here's another example - this one draws a small rectangle that follows
the mouse - there should be enough information in that to see what you
need to do, I think.



/***********************************************************************
*/
// Compile as:
// fltk-config --use-images --compile image_draw_mouse.cxx
//
// invoke as:
// image_draw_mouse {something.jpg | something.png | etc...}
//
#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> 

Fl_Double_Window *outer_win;
Fl_Shared_Image *img;
Fl_RGB_Image *rgbimg = NULL;

/***********************************************************************
*/ 
class vw_box : public Fl_Box { 
protected:
        void draw(void);
        int iw, ih, id;
        char *data; 

public:
        vw_box(int X, int Y, int W, int H, const char *L=0) :
Fl_Box(X,Y,W,H,L) 
        {data = NULL;}
        
        void set_data(char *newdat, int W, int H, int D);
        
        int handle(int event);
}; 

/***********************************************************************
*/ 
int vw_box::handle(int event)
{
        int res = Fl_Box::handle(event);
        
        switch (event)
        {
        case FL_MOVE: // any mouse movement...
                redraw();
                return 1;
        default:
                break;
        }
        return res;
}
/***********************************************************************
*/ 
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(void) {
        if (!data) return;
        
        short wd = w(); short ht = h();
        short xoff = 0; short yoff = 0; 

        short box_w = 30, box_h = 30;
        short xp = Fl::event_x() - (box_w / 2);
        short yp = Fl::event_y() - (box_h / 2);
        
        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, at current mouse x,y co-ords
        fl_rect(xp, yp, box_w, box_h, 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();
}
/***********************************************************************
*/
int main(int argc, char **argv) {
        fl_register_images();
        Fl::get_system_colors();
        
        outer_win = new Fl_Double_Window(600, 650, "Picture Window");
        
        box_1 = new vw_box(5, 5, 590, 500);
        box_1->box(FL_FLAT_BOX);

        Fl_Button *quit = new Fl_Button(535, 615, 60, 30);
        quit->label("Quit");
        quit->callback((Fl_Callback*)quit_cb);
        outer_win->end();

        if (argv[1]) load_file(argv[1]);
        
        outer_win->show();

        return Fl::run();
}
/* End of File */


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

Reply via email to