// This example was started by Guillaume Laurent. // It has become a place to dump code that tests parts of the // gnomemm canvas code. Little thought has been given to the // actual on-screen output. #include #include #include class CanvasExample : public Gnome::Canvas::CanvasAA { typedef Gnome::Canvas::CanvasAA Parent; public: CanvasExample(double *, int); virtual ~CanvasExample(); private: int noe; bool isbutton; double clicked_x, clicked_y; ///this variable is needed, because ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution Gnome::Canvas::Item * active_item; bool event_handler(GdkEvent* e, bool b); bool tri_mover(GdkEvent* e, bool b); Gnome::Art::Point * wp; Gnome::Canvas::Ellipse ** nodes; Gnome::Canvas::Polygon * sides; Gnome::Canvas::Group triangle; }; bool CanvasExample::tri_mover(GdkEvent* e, bool b) { switch(e->type) { case GDK_BUTTON_PRESS: clicked_x=e->button.x; clicked_y=e->button.y; isbutton=true; break; case GDK_BUTTON_RELEASE: isbutton=false; active_item=NULL; break; case GDK_MOTION_NOTIFY: if(isbutton) { //double x1, y1, x2, y2; //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); double dx=e->motion.x-clicked_x; double dy=e->motion.y-clicked_y; for(int i=0;i<=noe/2;i++) { nodes[i]->move(dx,dy); } clicked_x=e->motion.x; clicked_y=e->motion.y; Gnome::Canvas::Points coos; for(int i=0;iget_bounds(x1,y1,x2,y2); double x=(x1+x2)/2; double y=(y1+y2)/2; coos.push_back(Gnome::Art::Point(x, y)); } sides->property_points().set_value(coos); } default: break; } } bool CanvasExample::event_handler(GdkEvent* e, bool b) { switch(e->type) { case GDK_BUTTON_PRESS: clicked_x=e->button.x; clicked_y=e->button.y; active_item=(get_item_at(e->button.x, e->button.y)); isbutton=true; break; case GDK_BUTTON_RELEASE: isbutton=false; active_item=NULL; break; case GDK_MOTION_NOTIFY: if(isbutton) { //double x1, y1, x2, y2; //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); double dx=e->motion.x-clicked_x; double dy=e->motion.y-clicked_y; active_item->move(dx, dy); Gnome::Canvas::Points coos; double x_wp=0; double y_wp=0; for(int i=0;iget_bounds(x1,y1,x2,y2); double x=(x1+x2)/2; double y=(y1+y2)/2; coos.push_back(Gnome::Art::Point(x, y)); if(i<3) { x_wp+=x; y_wp+=y; } } sides->property_points().set_value(coos); x_wp/=3; y_wp/=3; double x1,y1,x2,y2; nodes[noe/2]->get_bounds(x1,y1,x2,y2); double x=(x1+x2)/2; double y=(y1+y2)/2; dx=x_wp-x; dy=y_wp-y; nodes[noe/2]->move(dx, dy); clicked_x=e->motion.x; clicked_y=e->motion.y; } default: break; } } CanvasExample::CanvasExample(double * coosarray, int numofels):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL) { noe=numofels; int j=0; double ax=coosarray[j++]; double ay=coosarray[j++]; double bx=coosarray[j++]; double by=coosarray[j++]; double cx=coosarray[j++]; double cy=coosarray[j++]; Gnome::Canvas::Points coos; for(int i=0;iproperty_width_pixels().set_value(10); nodes=new ( Gnome::Canvas::Ellipse * ) [noe/2+1]; for(int i=0; isignal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),true)); } wp=new Gnome::Art::Point((ax+bx+cx)/3,(ay+by+cy)/3); nodes[noe/2]= new Gnome::Canvas::Ellipse(triangle, wp->get_x()-20, wp->get_y()-20, wp->get_x()+20, wp->get_y()+20); *(nodes[noe/2]) << Gnome::Canvas::Properties::fill_color("blue"); *(nodes[noe/2]) << Gnome::Canvas::Properties::outline_color("black"); (nodes[noe/2])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::tri_mover),true)); } CanvasExample::~CanvasExample() { } //MainWin: class MainWin : public Gtk::Window { public: MainWin(const std::string& title, double *, int); protected: //Member widgets: CanvasExample m_canvas; }; MainWin::MainWin(const std::string& title, double * coosarray, int noe):m_canvas(coosarray, noe) { set_title (title); add(m_canvas); set_default_size(900,600); show_all(); } //main(): int main(int argc, char *argv[]) { if((argc>=7)&&( (argc/2)!=( (argc+1)/2 ) ) ) { double * coosarray=new double[argc]; for(int i=1;i