Bugfix, thanks to Janos.
1 // This example was started by Guillaume Laurent.
2 // It has become a place to dump code that tests parts of the
3 // gnomemm canvas code. Little thought has been given to the
4 // actual on-screen output.
6 #include <libgnomecanvasmm.h>
7 #include <libgnomecanvasmm/polygon.h>
10 class CanvasExample : public Gnome::Canvas::CanvasAA
12 typedef Gnome::Canvas::CanvasAA Parent;
15 CanvasExample(double *, int);
16 virtual ~CanvasExample();
20 ///Event handler function that handles dragging nodes of triangle
21 bool event_handler(GdkEvent* e, int b);
23 ///Event handler function that handles dragging triangle
24 bool tri_mover(GdkEvent* e);
26 ///Coordinates of Weight Point of tirangle
27 Gnome::Art::Point * wp;
28 ///Array of nodes of planefigure
29 Gnome::Canvas::Ellipse ** nodes;
30 ///Sides of planefigure
31 Gnome::Canvas::Polygon * sides;
32 ///Group of graphical elements of triangle
33 Gnome::Canvas::Group triangle;
35 ///Indicates whether the button of mouse is pressed or not
38 ///Number Of Elements - the number of nodes
41 ///Array of coordinates
44 ///At this location was the mousebutton pressed.
45 ///It helps to calculate the distance of dragging.
46 double clicked_x, clicked_y;
48 ///Remembers which Gnome::Canvas::Item was pressed.
49 ///this variable is needed, because
50 ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
51 ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
52 Gnome::Canvas::Item * active_item;
57 ///When we click on the weight point we can drag the whole triangle. This function resolves it.
58 bool CanvasExample::tri_mover(GdkEvent* e)
62 case GDK_BUTTON_PRESS:
63 clicked_x=e->button.x;
64 clicked_y=e->button.y;
67 case GDK_BUTTON_RELEASE:
71 case GDK_MOTION_NOTIFY:
74 double dx=e->motion.x-clicked_x;
75 double dy=e->motion.y-clicked_y;
77 Gnome::Canvas::Points coos;
79 for(int i=0;i<=noe;i++)
81 nodes[i]->move(dx,dy);
83 double x=(coordinates[2*i]+=dx);
84 double y=(coordinates[2*i+1]+=dy);
86 if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
90 clicked_x=e->motion.x;
91 clicked_y=e->motion.y;
93 sides->property_points().set_value(coos);
100 ///This function moves only one node of triangle,
101 ///but recalculate the location of wight point,
102 ///and also redraw the sides of the planefigure.
103 bool CanvasExample::event_handler(GdkEvent* e, int b)
107 case GDK_BUTTON_PRESS:
108 clicked_x=e->button.x;
109 clicked_y=e->button.y;
110 active_item=(get_item_at(e->button.x, e->button.y));
113 case GDK_BUTTON_RELEASE:
117 case GDK_MOTION_NOTIFY:
120 //double x1, y1, x2, y2;
121 //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
122 //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
123 //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
124 double dx=e->motion.x-clicked_x;
125 double dy=e->motion.y-clicked_y;
126 active_item->move(dx, dy);
128 coordinates[2*b]+=dx;
129 coordinates[2*b+1]+=dy;
131 Gnome::Canvas::Points coos;
136 for(int i=0;i<noe;i++)
138 coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
140 x_wp+=coordinates[2*i];
141 y_wp+=coordinates[2*i+1];
144 sides->property_points().set_value(coos);
149 dx=x_wp-coordinates[noe*2];
150 dy=y_wp-coordinates[noe*2+1];
151 nodes[noe]->move(dx, dy);
153 coordinates[noe*2]+=dx;
154 coordinates[noe*2+1]+=dy;
156 clicked_x=e->motion.x;
157 clicked_y=e->motion.y;
164 CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
168 coordinates=new double [numofcoos+2];
173 Gnome::Canvas::Points coos;
174 for(int i=0;i<numofcoos;i+=2)
176 coordinates[i]=coosarray[i];
177 coordinates[i+1]=coosarray[i+1];
178 coos.push_back(Gnome::Art::Point(coordinates[i],
181 x_wp+=coordinates[i];
182 y_wp+=coordinates[i+1];
186 sides=new Gnome::Canvas::Polygon(triangle, coos);
187 *sides << Gnome::Canvas::Properties::outline_color("green");
188 sides->property_width_pixels().set_value(10);
190 nodes=new Gnome::Canvas::Ellipse* [noe+1];
192 for(int i=0; i<noe;i++)
194 nodes[i]= new Gnome::Canvas::Ellipse(triangle, coos[i].get_x()-20, coos[i].get_y()-20, coos[i].get_x()+20, coos[i].get_y()+20);
195 *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
196 *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
197 (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
200 coordinates[numofcoos]=x_wp/noe;
201 coordinates[numofcoos+1]=y_wp/noe;
203 wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
205 nodes[noe]= new Gnome::Canvas::Ellipse
208 coordinates[numofcoos]-20,
209 coordinates[numofcoos+1]-20,
210 coordinates[numofcoos]+20,
211 coordinates[numofcoos+1]+20
213 *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
214 *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
215 (nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
221 CanvasExample::~CanvasExample()
227 class MainWin : public Gtk::Window
230 MainWin(const std::string& title, double *, int);
234 CanvasExample m_canvas;
237 MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
241 set_default_size(900,600);
248 int main(int argc, char *argv[])
250 if((argc>=7)&& (argc%2) )
252 double * coosarray=new double[argc];
254 for(int i=1;i<argc;i++)
256 coosarray[i-1]=atof(argv[i]);
257 printf("%g%c",coosarray[i-1],i%2?' ':'\n');
260 Gnome::Canvas::init();
261 Gtk::Main app(argc, argv);
263 MainWin mainwin("Magic Triangle",coosarray,argc-1);