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