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>
9 #include <lemon/list_graph.h>
11 class CanvasExample : public Gnome::Canvas::CanvasAA
13 typedef Gnome::Canvas::CanvasAA Parent;
16 CanvasExample(double *, int);
17 virtual ~CanvasExample();
21 ///Event handler function that handles dragging nodes of triangle
22 bool event_handler(GdkEvent* e, int b);
24 ///Event handler function that handles dragging triangle
25 bool tri_mover(GdkEvent* e);
27 ///Coordinates of Weight Point of tirangle
28 Gnome::Art::Point * wp;
29 ///Array of nodes of planefigure
30 Gnome::Canvas::Ellipse ** nodes;
31 ///Sides of planefigure
32 Gnome::Canvas::Polygon * sides;
33 ///Group of graphical elements of triangle
34 Gnome::Canvas::Group triangle;
36 ///Indicates whether the button of mouse is pressed or not
39 ///Number Of Elements - the number of nodes
42 ///Array of coordinates
45 ///At this location was the mousebutton pressed.
46 ///It helps to calculate the distance of dragging.
47 double clicked_x, clicked_y;
49 ///Remembers which Gnome::Canvas::Item was pressed.
50 ///this variable is needed, because
51 ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
52 ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
53 Gnome::Canvas::Item * active_item;
58 ///When we click on the weight point we can drag the whole triangle. This function resolves it.
59 bool CanvasExample::tri_mover(GdkEvent* e)
63 case GDK_BUTTON_PRESS:
64 clicked_x=e->button.x;
65 clicked_y=e->button.y;
68 case GDK_BUTTON_RELEASE:
72 case GDK_MOTION_NOTIFY:
75 double dx=e->motion.x-clicked_x;
76 double dy=e->motion.y-clicked_y;
78 Gnome::Canvas::Points coos;
80 for(int i=0;i<=noe;i++)
82 nodes[i]->move(dx,dy);
84 double x=(coordinates[2*i]+=dx);
85 double y=(coordinates[2*i+1]+=dy);
87 if(i!=noe)coos.push_back(Gnome::Art::Point(x,y));
91 clicked_x=e->motion.x;
92 clicked_y=e->motion.y;
94 sides->property_points().set_value(coos);
101 ///This function moves only one node of triangle,
102 ///but recalculate the location of wight point,
103 ///and also redraw the sides of the planefigure.
104 bool CanvasExample::event_handler(GdkEvent* e, int b)
108 case GDK_BUTTON_PRESS:
109 clicked_x=e->button.x;
110 clicked_y=e->button.y;
111 active_item=(get_item_at(e->button.x, e->button.y));
114 case GDK_BUTTON_RELEASE:
118 case GDK_MOTION_NOTIFY:
121 //double x1, y1, x2, y2;
122 //(get_item_at(e->motion.x, e->motion.y))->get_bounds(x1, y1, x2, y2);
123 //printf("Item coos: %d %d %d %d\n", (int)x1, (int)y1, (int)x2, (int)y2);
124 //printf("Mouse is moved! %d %d\n",(int)e->motion.x,(int)e->motion.y);
125 double dx=e->motion.x-clicked_x;
126 double dy=e->motion.y-clicked_y;
127 active_item->move(dx, dy);
129 coordinates[2*b]+=dx;
130 coordinates[2*b+1]+=dy;
132 Gnome::Canvas::Points coos;
137 for(int i=0;i<noe;i++)
139 coos.push_back(Gnome::Art::Point(coordinates[2*i], coordinates[2*i+1]));
141 x_wp+=coordinates[2*i];
142 y_wp+=coordinates[2*i+1];
145 sides->property_points().set_value(coos);
150 dx=x_wp-coordinates[noe*2];
151 dy=y_wp-coordinates[noe*2+1];
152 nodes[noe]->move(dx, dy);
154 coordinates[noe*2]+=dx;
155 coordinates[noe*2+1]+=dy;
157 clicked_x=e->motion.x;
158 clicked_y=e->motion.y;
165 CanvasExample::CanvasExample(double * coosarray, int numofcoos):triangle(*(root()), 0, 0),isbutton(false),active_item(NULL)
169 coordinates=new double [numofcoos+2];
174 Gnome::Canvas::Points coos;
175 for(int i=0;i<numofcoos;i+=2)
177 coordinates[i]=coosarray[i];
178 coordinates[i+1]=coosarray[i+1];
179 coos.push_back(Gnome::Art::Point(coordinates[i],
182 x_wp+=coordinates[i];
183 y_wp+=coordinates[i+1];
187 sides=new Gnome::Canvas::Polygon(triangle, coos);
188 *sides << Gnome::Canvas::Properties::outline_color("green");
189 sides->property_width_pixels().set_value(10);
191 nodes=new Gnome::Canvas::Ellipse* [noe+1];
193 for(int i=0; i<noe;i++)
195 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);
196 *(nodes[i]) << Gnome::Canvas::Properties::fill_color("blue");
197 *(nodes[i]) << Gnome::Canvas::Properties::outline_color("black");
198 (nodes[i])->signal_event().connect(sigc::bind(sigc::mem_fun(*this, &CanvasExample::event_handler),i));
201 coordinates[numofcoos]=x_wp/noe;
202 coordinates[numofcoos+1]=y_wp/noe;
204 wp=new Gnome::Art::Point(coordinates[numofcoos],coordinates[numofcoos+1]);
206 nodes[noe]= new Gnome::Canvas::Ellipse
209 coordinates[numofcoos]-20,
210 coordinates[numofcoos+1]-20,
211 coordinates[numofcoos]+20,
212 coordinates[numofcoos+1]+20
214 *(nodes[noe]) << Gnome::Canvas::Properties::fill_color("blue");
215 *(nodes[noe]) << Gnome::Canvas::Properties::outline_color("black");
216 (nodes[noe])->signal_event().connect(sigc::mem_fun(*this, &CanvasExample::tri_mover));
222 CanvasExample::~CanvasExample()
228 class MainWin : public Gtk::Window
231 MainWin(const std::string& title, double *, int);
235 CanvasExample m_canvas;
238 MainWin::MainWin(const std::string& title, double * coosarray, int noc):m_canvas(coosarray, noc)
242 set_default_size(900,600);
249 int main(int argc, char *argv[])
251 if((argc>=7)&& (argc%2) )
253 double * coosarray=new double[argc];
255 for(int i=1;i<argc;i++)
257 coosarray[i-1]=atof(argv[i]);
258 printf("%g%c",coosarray[i-1],i%2?' ':'\n');
261 Gnome::Canvas::init();
262 Gtk::Main app(argc, argv);
264 MainWin mainwin("Magic Triangle",coosarray,argc-1);