hegyi@1288: // This example was started by Guillaume Laurent. hegyi@1288: // It has become a place to dump code that tests parts of the hegyi@1288: // gnomemm canvas code. Little thought has been given to the hegyi@1288: // actual on-screen output. hegyi@1288: hegyi@1288: #include hegyi@1288: #include hegyi@1288: #include hegyi@1288: #include hegyi@1288: hegyi@1288: class CanvasExample : public Gnome::Canvas::CanvasAA hegyi@1288: { hegyi@1288: typedef Gnome::Canvas::CanvasAA Parent; hegyi@1288: hegyi@1288: public: hegyi@1288: CanvasExample(double *, int); hegyi@1288: virtual ~CanvasExample(); hegyi@1288: hegyi@1288: private: hegyi@1288: hegyi@1288: ///Event handler function that handles dragging nodes of triangle hegyi@1288: bool event_handler(GdkEvent* e, int b); hegyi@1288: hegyi@1288: ///Event handler function that handles dragging triangle hegyi@1288: bool tri_mover(GdkEvent* e); hegyi@1288: hegyi@1288: ///Coordinates of Weight Point of tirangle hegyi@1288: Gnome::Art::Point * wp; hegyi@1288: ///Array of nodes of planefigure hegyi@1288: Gnome::Canvas::Ellipse ** nodes; hegyi@1288: ///Sides of planefigure hegyi@1288: Gnome::Canvas::Polygon * sides; hegyi@1288: ///Group of graphical elements of triangle hegyi@1288: Gnome::Canvas::Group triangle; hegyi@1288: hegyi@1288: ///Indicates whether the button of mouse is pressed or not hegyi@1288: bool isbutton; hegyi@1288: hegyi@1288: ///Number Of Elements - the number of nodes hegyi@1288: int noe; hegyi@1288: hegyi@1288: ///Array of coordinates hegyi@1288: double * coordinates; hegyi@1288: hegyi@1288: ///At this location was the mousebutton pressed. hegyi@1288: ///It helps to calculate the distance of dragging. hegyi@1288: double clicked_x, clicked_y; hegyi@1288: hegyi@1288: ///Remembers which Gnome::Canvas::Item was pressed. hegyi@1288: ///this variable is needed, because hegyi@1288: ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault hegyi@1288: ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution hegyi@1288: Gnome::Canvas::Item * active_item; hegyi@1288: hegyi@1288: hegyi@1288: }; hegyi@1288: hegyi@1288: ///When we click on the weight point we can drag the whole triangle. This function resolves it. hegyi@1288: bool CanvasExample::tri_mover(GdkEvent* e) hegyi@1288: { hegyi@1288: switch(e->type) hegyi@1288: { hegyi@1288: case GDK_BUTTON_PRESS: hegyi@1288: clicked_x=e->button.x; hegyi@1288: clicked_y=e->button.y; hegyi@1288: isbutton=true; hegyi@1288: break; hegyi@1288: case GDK_BUTTON_RELEASE: hegyi@1288: isbutton=false; hegyi@1288: active_item=NULL; hegyi@1288: break; hegyi@1288: case GDK_MOTION_NOTIFY: hegyi@1288: if(isbutton) hegyi@1288: { hegyi@1288: double dx=e->motion.x-clicked_x; hegyi@1288: double dy=e->motion.y-clicked_y; hegyi@1288: hegyi@1288: Gnome::Canvas::Points coos; hegyi@1288: hegyi@1288: for(int i=0;i<=noe;i++) hegyi@1288: { hegyi@1288: nodes[i]->move(dx,dy); hegyi@1288: hegyi@1288: double x=(coordinates[2*i]+=dx); hegyi@1288: double y=(coordinates[2*i+1]+=dy); hegyi@1288: hegyi@1288: if(i!=noe)coos.push_back(Gnome::Art::Point(x,y)); hegyi@1288: hegyi@1288: } hegyi@1288: hegyi@1288: clicked_x=e->motion.x; hegyi@1288: clicked_y=e->motion.y; hegyi@1288: hegyi@1288: sides->property_points().set_value(coos); hegyi@1288: } hegyi@1288: default: break; hegyi@1288: } hegyi@1288: return true; hegyi@1288: } hegyi@1288: hegyi@1288: ///This function moves only one node of triangle, hegyi@1288: ///but recalculate the location of wight point, hegyi@1288: ///and also redraw the sides of the planefigure. hegyi@1288: bool CanvasExample::event_handler(GdkEvent* e, int b) hegyi@1288: { hegyi@1288: switch(e->type) hegyi@1288: { hegyi@1288: case GDK_BUTTON_PRESS: hegyi@1288: clicked_x=e->button.x; hegyi@1288: clicked_y=e->button.y; hegyi@1288: active_item=(get_item_at(e->button.x, e->button.y)); hegyi@1288: isbutton=true; hegyi@1288: break; hegyi@1288: case GDK_BUTTON_RELEASE: hegyi@1288: isbutton=false; hegyi@1288: active_item=NULL; hegyi@1288: break; hegyi@1288: case GDK_MOTION_NOTIFY: hegyi@1288: if(isbutton) hegyi@1288: { hegyi@1288: //double x1, y1, x2, y2; hegyi@1288: //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2); hegyi@1288: //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2); hegyi@1288: //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y); hegyi@1288: double dx=e->motion.x-clicked_x; hegyi@1288: double dy=e->motion.y-clicked_y; hegyi@1288: active_item->move(dx, dy); hegyi@1288: hegyi@1288: coordinates[2*b]+=dx; hegyi@1288: coordinates[2*b+1]+=dy; hegyi@1288: hegyi@1288: Gnome::Canvas::Points coos; hegyi@1288: hegyi@1288: double x_wp=0; hegyi@1288: double y_wp=0; hegyi@1288: hegyi@1288: for(int i=0;iproperty_points().set_value(coos); hegyi@1288: hegyi@1288: x_wp/=noe; hegyi@1288: y_wp/=noe; hegyi@1288: hegyi@1288: dx=x_wp-coordinates[noe*2]; hegyi@1288: dy=y_wp-coordinates[noe*2+1]; hegyi@1288: nodes[noe]->move(dx, dy); hegyi@1288: hegyi@1288: coordinates[noe*2]+=dx; hegyi@1288: coordinates[noe*2+1]+=dy; hegyi@1288: hegyi@1288: clicked_x=e->motion.x; hegyi@1288: clicked_y=e->motion.y; hegyi@1288: } hegyi@1288: default: break; hegyi@1288: } hegyi@1288: return true; hegyi@1288: } hegyi@1288: hegyi@1288: CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL) hegyi@1288: { hegyi@1288: noe=numofcoos/2; hegyi@1288: hegyi@1288: coordinates=new double [numofcoos+2]; hegyi@1288: hegyi@1288: double x_wp=0; hegyi@1288: double y_wp=0; hegyi@1288: hegyi@1288: Gnome::Canvas::Points coos; hegyi@1288: for(int i=0;iproperty_width_pixels().set_value(10); hegyi@1288: hegyi@1288: nodes=new Gnome::Canvas::Ellipse* [noe+1]; hegyi@1288: hegyi@1288: for(int i=0; isignal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i)); hegyi@1288: } hegyi@1288: hegyi@1288: coordinates[numofcoos]=x_wp/noe; hegyi@1288: coordinates[numofcoos+1]=y_wp/noe; hegyi@1288: hegyi@1288: wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]); hegyi@1288: hegyi@1288: nodes[noe]= new Gnome::Canvas::Ellipse hegyi@1288: ( hegyi@1288: triangle, hegyi@1288: coordinates[numofcoos]-20, hegyi@1288: coordinates[numofcoos+1]-20, hegyi@1288: coordinates[numofcoos]+20, hegyi@1288: coordinates[numofcoos+1]+20 hegyi@1288: ); hegyi@1288: *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue"); hegyi@1288: *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black"); hegyi@1288: (nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover)); hegyi@1288: hegyi@1288: hegyi@1288: hegyi@1288: } hegyi@1288: hegyi@1288: CanvasExample::~CanvasExample() hegyi@1288: { hegyi@1288: } hegyi@1288: hegyi@1288: //MainWin: hegyi@1288: hegyi@1288: class MainWin : public Gtk::Window hegyi@1288: { hegyi@1288: public: hegyi@1288: MainWin(const std::string& title, double *, int); hegyi@1288: hegyi@1288: protected: hegyi@1288: //Member widgets: hegyi@1288: CanvasExample m_canvas; hegyi@1288: }; hegyi@1288: hegyi@1288: MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc) hegyi@1288: { hegyi@1288: set_title (title); hegyi@1288: add(m_canvas); hegyi@1288: set_default_size(900,600); hegyi@1288: hegyi@1288: show_all(); hegyi@1288: } hegyi@1288: hegyi@1288: //main(): hegyi@1288: hegyi@1288: int main(int argc, char *argv[]) hegyi@1288: { hegyi@1288: if((argc>=7)&& (argc%2) ) hegyi@1288: { hegyi@1288: double * coosarray=new double[argc]; hegyi@1288: hegyi@1288: for(int i=1;i