1 /* Dia -- an diagram creation/manipulation program
2  * Copyright (C) 1999 Alexander Larsson
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #include <assert.h>
24 #include <math.h>
25 
26 #include "intl.h"
27 #include "object.h"
28 #include "poly_conn.h"
29 #include "connectionpoint.h"
30 #include "diarenderer.h"
31 #include "attributes.h"
32 #include "widgets.h"
33 #include "diamenu.h"
34 #include "message.h"
35 #include "properties.h"
36 
37 #include "create.h"
38 
39 #include "tool-icons.h"
40 
41 #define DEFAULT_WIDTH 0.15
42 
43 typedef struct _Polyline {
44   PolyConn poly;
45 
46   Color line_color;
47   LineStyle line_style;
48   real dashlength;
49   real line_width;
50   real corner_radius;
51   Arrow start_arrow, end_arrow;
52   real absolute_start_gap, absolute_end_gap;
53 } Polyline;
54 
55 
56 static ObjectChange* polyline_move_handle(Polyline *polyline, Handle *handle,
57 					  Point *to, ConnectionPoint *cp,
58 					  HandleMoveReason reason, ModifierKeys modifiers);
59 static ObjectChange* polyline_move(Polyline *polyline, Point *to);
60 static void polyline_select(Polyline *polyline, Point *clicked_point,
61 			      DiaRenderer *interactive_renderer);
62 static void polyline_draw(Polyline *polyline, DiaRenderer *renderer);
63 static DiaObject *polyline_create(Point *startpoint,
64 				 void *user_data,
65 				 Handle **handle1,
66 				 Handle **handle2);
67 static real polyline_distance_from(Polyline *polyline, Point *point);
68 static void polyline_update_data(Polyline *polyline);
69 static void polyline_destroy(Polyline *polyline);
70 static DiaObject *polyline_copy(Polyline *polyline);
71 
72 static PropDescription *polyline_describe_props(Polyline *polyline);
73 static void polyline_get_props(Polyline *polyline, GPtrArray *props);
74 static void polyline_set_props(Polyline *polyline, GPtrArray *props);
75 
76 static void polyline_save(Polyline *polyline, ObjectNode obj_node,
77 			  const char *filename);
78 static DiaObject *polyline_load(ObjectNode obj_node, int version,
79 			     const char *filename);
80 static DiaMenu *polyline_get_object_menu(Polyline *polyline, Point *clickedpoint);
81 void polyline_calculate_gap_endpoints(Polyline *polyline, Point *gap_endpoints);
82 static void polyline_exchange_gap_points(Polyline *polyline,  Point *gap_points);
83 
84 static ObjectTypeOps polyline_type_ops =
85 {
86   (CreateFunc)polyline_create,   /* create */
87   (LoadFunc)  polyline_load,     /* load */
88   (SaveFunc)  polyline_save,      /* save */
89   (GetDefaultsFunc)   NULL /*polyline_get_defaults*/,
90   (ApplyDefaultsFunc) NULL /*polyline_apply_defaults*/
91 };
92 
93 static DiaObjectType polyline_type =
94 {
95   "Standard - PolyLine",   /* name */
96   0,                         /* version */
97   (char **) polyline_icon,      /* pixmap */
98 
99   &polyline_type_ops,       /* ops */
100   NULL, /* pixmap_file */
101   0 /* default_user_data */
102 };
103 
104 DiaObjectType *_polyline_type = (DiaObjectType *) &polyline_type;
105 
106 
107 static ObjectOps polyline_ops = {
108   (DestroyFunc)         polyline_destroy,
109   (DrawFunc)            polyline_draw,
110   (DistanceFunc)        polyline_distance_from,
111   (SelectFunc)          polyline_select,
112   (CopyFunc)            polyline_copy,
113   (MoveFunc)            polyline_move,
114   (MoveHandleFunc)      polyline_move_handle,
115   (GetPropertiesFunc)   object_create_props_dialog,
116   (ApplyPropertiesDialogFunc) object_apply_props_from_dialog,
117   (ObjectMenuFunc)      polyline_get_object_menu,
118   (DescribePropsFunc)   polyline_describe_props,
119   (GetPropsFunc)        polyline_get_props,
120   (SetPropsFunc)        polyline_set_props,
121   (TextEditFunc) 0,
122   (ApplyPropertiesListFunc) object_apply_props,
123 };
124 
125 static PropNumData polyline_corner_radius_data = { 0.0, 10.0, 0.1 };
126 static PropNumData gap_range = { -G_MAXFLOAT, G_MAXFLOAT, 0.1};
127 
128 static PropDescription polyline_props[] = {
129   POLYCONN_COMMON_PROPERTIES,
130   PROP_STD_LINE_WIDTH,
131   PROP_STD_LINE_COLOUR,
132   PROP_STD_LINE_STYLE,
133   PROP_STD_START_ARROW,
134   PROP_STD_END_ARROW,
135   { "corner_radius", PROP_TYPE_REAL, PROP_FLAG_VISIBLE,
136     N_("Corner radius"), NULL, &polyline_corner_radius_data },
137   PROP_FRAME_BEGIN("gaps",0,N_("Line gaps")),
138   { "absolute_start_gap", PROP_TYPE_REAL, PROP_FLAG_VISIBLE,
139     N_("Absolute start gap"), NULL, &gap_range },
140   { "absolute_end_gap", PROP_TYPE_REAL, PROP_FLAG_VISIBLE,
141     N_("Absolute end gap"), NULL, &gap_range },
142   PROP_FRAME_END("gaps",0),
143   PROP_DESC_END
144 };
145 
146 static PropDescription *
polyline_describe_props(Polyline * polyline)147 polyline_describe_props(Polyline *polyline)
148 {
149   if (polyline_props[0].quark == 0)
150     prop_desc_list_calculate_quarks(polyline_props);
151   return polyline_props;
152 }
153 
154 static PropOffset polyline_offsets[] = {
155   POLYCONN_COMMON_PROPERTIES_OFFSETS,
156   { PROP_STDNAME_LINE_WIDTH, PROP_STDTYPE_LINE_WIDTH, offsetof(Polyline, line_width) },
157   { "line_colour", PROP_TYPE_COLOUR, offsetof(Polyline, line_color) },
158   { "line_style", PROP_TYPE_LINESTYLE,
159     offsetof(Polyline, line_style), offsetof(Polyline, dashlength) },
160   { "start_arrow", PROP_TYPE_ARROW, offsetof(Polyline, start_arrow) },
161   { "end_arrow", PROP_TYPE_ARROW, offsetof(Polyline, end_arrow) },
162   { "corner_radius", PROP_TYPE_REAL, offsetof(Polyline, corner_radius) },
163   PROP_OFFSET_FRAME_BEGIN("gaps"),
164   { "absolute_start_gap", PROP_TYPE_REAL, offsetof(Polyline, absolute_start_gap) },
165   { "absolute_end_gap", PROP_TYPE_REAL, offsetof(Polyline, absolute_end_gap) },
166   PROP_OFFSET_FRAME_END("gaps"),
167   { NULL, 0, 0 }
168 };
169 
170 static void
polyline_get_props(Polyline * polyline,GPtrArray * props)171 polyline_get_props(Polyline *polyline, GPtrArray *props)
172 {
173   object_get_props_from_offsets(&polyline->poly.object, polyline_offsets,
174 				props);
175 }
176 
177 static void
polyline_set_props(Polyline * polyline,GPtrArray * props)178 polyline_set_props(Polyline *polyline, GPtrArray *props)
179 {
180   object_set_props_from_offsets(&polyline->poly.object, polyline_offsets,
181 				props);
182   polyline_update_data(polyline);
183 }
184 
185 static real
polyline_distance_from(Polyline * polyline,Point * point)186 polyline_distance_from(Polyline *polyline, Point *point)
187 {
188   PolyConn *poly = &polyline->poly;
189   real dist;
190   Point gap_endpoints[2];
191   polyline_calculate_gap_endpoints(polyline, gap_endpoints);
192   polyline_exchange_gap_points(polyline, gap_endpoints);
193   dist = polyconn_distance_from(poly, point, polyline->line_width);
194   polyline_exchange_gap_points(polyline, gap_endpoints);
195   return dist;
196 }
197 
polyline_closest_handle(Polyline * polyline,Point * point)198 static Handle *polyline_closest_handle(Polyline *polyline, Point *point) {
199   return polyconn_closest_handle(&polyline->poly, point);
200 }
201 
polyline_closest_segment(Polyline * polyline,Point * point)202 static int polyline_closest_segment(Polyline *polyline, Point *point) {
203   PolyConn *poly = &polyline->poly;
204   return polyconn_closest_segment(poly, point, polyline->line_width);
205 }
206 
207 static void
polyline_select(Polyline * polyline,Point * clicked_point,DiaRenderer * interactive_renderer)208 polyline_select(Polyline *polyline, Point *clicked_point,
209 		  DiaRenderer *interactive_renderer)
210 {
211   polyconn_update_data(&polyline->poly);
212 }
213 
214 static ObjectChange*
polyline_move_handle(Polyline * polyline,Handle * handle,Point * to,ConnectionPoint * cp,HandleMoveReason reason,ModifierKeys modifiers)215 polyline_move_handle(Polyline *polyline, Handle *handle,
216 		     Point *to, ConnectionPoint *cp,
217 		     HandleMoveReason reason, ModifierKeys modifiers)
218 {
219   assert(polyline!=NULL);
220   assert(handle!=NULL);
221   assert(to!=NULL);
222 
223   polyconn_move_handle(&polyline->poly, handle, to, cp, reason, modifiers);
224   polyline_update_data(polyline);
225 
226   return NULL;
227 }
228 
229 
230 static ObjectChange*
polyline_move(Polyline * polyline,Point * to)231 polyline_move(Polyline *polyline, Point *to)
232 {
233   polyconn_move(&polyline->poly, to);
234   polyline_update_data(polyline);
235 
236   return NULL;
237 }
238 
239 void
polyline_calculate_gap_endpoints(Polyline * polyline,Point * gap_endpoints)240 polyline_calculate_gap_endpoints(Polyline *polyline, Point *gap_endpoints)
241 {
242   Point  start_vec, end_vec;
243   ConnectionPoint *start_cp, *end_cp;
244   int n = polyline->poly.numpoints;
245 
246   gap_endpoints[0] = polyline->poly.points[0];
247   gap_endpoints[1] = polyline->poly.points[n-1];
248 
249   start_cp = (polyline->poly.object.handles[0])->connected_to;
250   end_cp = (polyline->poly.object.handles[polyline->poly.object.num_handles-1])->connected_to;
251 
252   if (connpoint_is_autogap(start_cp)) {
253       gap_endpoints[0] = calculate_object_edge(&gap_endpoints[0],
254 					   &polyline->poly.points[1],
255 					   start_cp->object);
256   }
257   if (connpoint_is_autogap(end_cp)) {
258       gap_endpoints[1] = calculate_object_edge(&gap_endpoints[1],
259 					   &polyline->poly.points[n-2],
260 					   end_cp->object);
261   }
262 
263   start_vec = gap_endpoints[0];
264   point_sub(&start_vec, &polyline->poly.points[0]);
265   point_normalize(&start_vec);
266 
267   end_vec = gap_endpoints[1];
268   point_sub(&end_vec, &polyline->poly.points[n-1]);
269   point_normalize(&end_vec);
270 
271   /* add absolute gap */
272   point_add_scaled(&gap_endpoints[0], &start_vec, polyline->absolute_start_gap);
273   point_add_scaled(&gap_endpoints[1], &end_vec, polyline->absolute_end_gap);
274 }
275 static void
polyline_exchange_gap_points(Polyline * polyline,Point * gap_points)276 polyline_exchange_gap_points(Polyline *polyline,  Point *gap_points)
277 {
278         Point tmp[2];
279         int n = polyline->poly.numpoints;
280         tmp[0] = gap_points[0];
281         tmp[1] = gap_points[1];
282         gap_points[0] = polyline->poly.points[0];
283         gap_points[1] = polyline->poly.points[n-1];
284         polyline->poly.points[0] = tmp[0];
285         polyline->poly.points[n-1] = tmp[1];
286 }
287 
288 
289 static void
polyline_draw(Polyline * polyline,DiaRenderer * renderer)290 polyline_draw(Polyline *polyline, DiaRenderer *renderer)
291 {
292   Point gap_endpoints[2];
293   DiaRendererClass *renderer_ops = DIA_RENDERER_GET_CLASS (renderer);
294   PolyConn *poly = &polyline->poly;
295   Point *points;
296   int n;
297 
298   points = &poly->points[0];
299   n = poly->numpoints;
300   renderer_ops->set_linewidth(renderer, polyline->line_width);
301   renderer_ops->set_linestyle(renderer, polyline->line_style);
302   renderer_ops->set_dashlength(renderer, polyline->dashlength);
303   if (polyline->corner_radius > 0.0)
304     renderer_ops->set_linejoin(renderer, LINEJOIN_ROUND);
305   else
306     renderer_ops->set_linejoin(renderer, LINEJOIN_MITER);
307   renderer_ops->set_linecaps(renderer, LINECAPS_BUTT);
308 
309   polyline_calculate_gap_endpoints(polyline, gap_endpoints);
310   polyline_exchange_gap_points(polyline, gap_endpoints);
311   renderer_ops->draw_rounded_polyline_with_arrows(renderer,
312 						  points, n,
313 						  polyline->line_width,
314 						  &polyline->line_color,
315 						  &polyline->start_arrow,
316 						  &polyline->end_arrow,
317 						  polyline->corner_radius);
318   polyline_exchange_gap_points(polyline, gap_endpoints);
319 }
320 
321 /** user_data is a struct polyline_create_data, containing an array of
322     points and a count.
323     If user_data is NULL, the startpoint is used and a 1x1 line is created.
324     Otherwise, the startpoint is ignored.
325 */
326 static DiaObject *
polyline_create(Point * startpoint,void * user_data,Handle ** handle1,Handle ** handle2)327 polyline_create(Point *startpoint,
328 		  void *user_data,
329 		  Handle **handle1,
330 		  Handle **handle2)
331 {
332   Polyline *polyline;
333   PolyConn *poly;
334   DiaObject *obj;
335   Point defaultlen = { 1.0, 1.0 };
336 
337   /*polyline_init_defaults();*/
338   polyline = g_malloc0(sizeof(Polyline));
339   poly = &polyline->poly;
340   obj = &poly->object;
341 
342   obj->type = &polyline_type;
343   obj->ops = &polyline_ops;
344 
345   if (user_data == NULL) {
346     polyconn_init(poly, 2);
347 
348     poly->points[0] = *startpoint;
349     poly->points[1] = *startpoint;
350 
351     point_add(&poly->points[1], &defaultlen);
352 
353     *handle1 = poly->object.handles[0];
354     *handle2 = poly->object.handles[1];
355   } else {
356     MultipointCreateData *pcd = (MultipointCreateData *)user_data;
357 
358     polyconn_init(poly, pcd->num_points);
359 
360     /* Handles are set up by polyconn_init and polyconn_update_data */
361     polyconn_set_points(poly, pcd->num_points, pcd->points);
362 
363     *handle1 = poly->object.handles[0];
364     *handle2 = poly->object.handles[pcd->num_points-1];
365   }
366 
367 
368   polyline->line_width =  attributes_get_default_linewidth();
369   polyline->line_color = attributes_get_foreground();
370   attributes_get_default_line_style(&polyline->line_style,
371 				    &polyline->dashlength);
372   polyline->start_arrow = attributes_get_default_start_arrow();
373   polyline->end_arrow = attributes_get_default_end_arrow();
374   polyline->corner_radius = 0.0;
375 
376   polyline_update_data(polyline);
377 
378   return &polyline->poly.object;
379 }
380 
381 static void
polyline_destroy(Polyline * polyline)382 polyline_destroy(Polyline *polyline)
383 {
384   polyconn_destroy(&polyline->poly);
385 }
386 
387 static DiaObject *
polyline_copy(Polyline * polyline)388 polyline_copy(Polyline *polyline)
389 {
390   Polyline *newpolyline;
391   PolyConn *poly, *newpoly;
392   DiaObject *newobj;
393 
394   poly = &polyline->poly;
395 
396   newpolyline = g_malloc0(sizeof(Polyline));
397   newpoly = &newpolyline->poly;
398   newobj = &newpoly->object;
399 
400   polyconn_copy(poly, newpoly);
401 
402   newpolyline->line_color = polyline->line_color;
403   newpolyline->line_width = polyline->line_width;
404   newpolyline->line_style = polyline->line_style;
405   newpolyline->dashlength = polyline->dashlength;
406   newpolyline->start_arrow = polyline->start_arrow;
407   newpolyline->end_arrow = polyline->end_arrow;
408   newpolyline->corner_radius = polyline->corner_radius;
409   newpolyline->absolute_start_gap = polyline->absolute_start_gap;
410   newpolyline->absolute_end_gap = polyline->absolute_end_gap;
411 
412   polyline_update_data(newpolyline);
413 
414   return &newpolyline->poly.object;
415 }
416 
417 static void
polyline_update_data(Polyline * polyline)418 polyline_update_data(Polyline *polyline)
419 {
420   PolyConn *poly = &polyline->poly;
421   DiaObject *obj = &poly->object;
422   PolyBBExtras *extra = &poly->extra_spacing;
423   Point gap_endpoints[2];
424 
425   polyconn_update_data(&polyline->poly);
426 
427   extra->start_trans =  (polyline->line_width / 2.0);
428   extra->end_trans =     (polyline->line_width / 2.0);
429   extra->middle_trans = (polyline->line_width / 2.0);
430   extra->start_long = (polyline->line_width / 2.0);
431   extra->end_long   = (polyline->line_width / 2.0);
432 
433   polyline_calculate_gap_endpoints(polyline, gap_endpoints);
434   polyline_exchange_gap_points(polyline, gap_endpoints);
435 
436   polyconn_update_boundingbox(poly);
437 
438   if (polyline->start_arrow.type != ARROW_NONE) {
439     Rectangle bbox;
440     Point move_arrow, move_line;
441     Point to = gap_endpoints[0];
442     Point from = poly->points[1];
443     calculate_arrow_point(&polyline->start_arrow, &to, &from,
444                           &move_arrow, &move_line, polyline->line_width);
445     /* move them */
446     point_sub(&to, &move_arrow);
447     point_sub(&from, &move_line);
448 
449     arrow_bbox (&polyline->start_arrow, polyline->line_width, &to, &from, &bbox);
450     rectangle_union (&obj->bounding_box, &bbox);
451   }
452   if (polyline->end_arrow.type != ARROW_NONE) {
453     Rectangle bbox;
454     int n = polyline->poly.numpoints;
455     Point move_arrow, move_line;
456     Point to = gap_endpoints[1];
457     Point from = poly->points[n-2];
458     calculate_arrow_point(&polyline->start_arrow, &to, &from,
459                           &move_arrow, &move_line, polyline->line_width);
460     /* move them */
461     point_sub(&to, &move_arrow);
462     point_sub(&from, &move_line);
463 
464     arrow_bbox (&polyline->end_arrow, polyline->line_width, &to, &from, &bbox);
465     rectangle_union (&obj->bounding_box, &bbox);
466   }
467 
468   polyline_exchange_gap_points(polyline, gap_endpoints);
469 
470   obj->position = poly->points[0];
471 }
472 
473 static void
polyline_save(Polyline * polyline,ObjectNode obj_node,const char * filename)474 polyline_save(Polyline *polyline, ObjectNode obj_node,
475 	      const char *filename)
476 {
477   polyconn_save(&polyline->poly, obj_node);
478 
479   if (!color_equals(&polyline->line_color, &color_black))
480     data_add_color(new_attribute(obj_node, "line_color"),
481 		   &polyline->line_color);
482 
483   if (polyline->line_width != 0.1)
484     data_add_real(new_attribute(obj_node, PROP_STDNAME_LINE_WIDTH),
485 		  polyline->line_width);
486 
487   if (polyline->line_style != LINESTYLE_SOLID)
488     data_add_enum(new_attribute(obj_node, "line_style"),
489 		  polyline->line_style);
490 
491   if (polyline->line_style != LINESTYLE_SOLID &&
492       polyline->dashlength != DEFAULT_LINESTYLE_DASHLEN)
493     data_add_real(new_attribute(obj_node, "dashlength"),
494 		  polyline->dashlength);
495 
496   if (polyline->start_arrow.type != ARROW_NONE) {
497     save_arrow(obj_node, &polyline->start_arrow, "start_arrow",
498 	     "start_arrow_length", "start_arrow_width");
499   }
500 
501   if (polyline->end_arrow.type != ARROW_NONE) {
502     save_arrow(obj_node, &polyline->end_arrow, "end_arrow",
503 	     "end_arrow_length", "end_arrow_width");
504   }
505 
506   if (polyline->absolute_start_gap)
507     data_add_real(new_attribute(obj_node, "absolute_start_gap"),
508                  polyline->absolute_start_gap);
509   if (polyline->absolute_end_gap)
510     data_add_real(new_attribute(obj_node, "absolute_end_gap"),
511                  polyline->absolute_end_gap);
512 
513   if (polyline->corner_radius > 0.0)
514     data_add_real(new_attribute(obj_node, "corner_radius"),
515                   polyline->corner_radius);
516 }
517 
518 static DiaObject *
polyline_load(ObjectNode obj_node,int version,const char * filename)519 polyline_load(ObjectNode obj_node, int version, const char *filename)
520 {
521   Polyline *polyline;
522   PolyConn *poly;
523   DiaObject *obj;
524   AttributeNode attr;
525 
526   polyline = g_malloc0(sizeof(Polyline));
527 
528   poly = &polyline->poly;
529   obj = &poly->object;
530 
531   obj->type = &polyline_type;
532   obj->ops = &polyline_ops;
533 
534   polyconn_load(poly, obj_node);
535 
536   polyline->line_color = color_black;
537   attr = object_find_attribute(obj_node, "line_color");
538   if (attr != NULL)
539     data_color(attribute_first_data(attr), &polyline->line_color);
540 
541   polyline->line_width = 0.1;
542   attr = object_find_attribute(obj_node, PROP_STDNAME_LINE_WIDTH);
543   if (attr != NULL)
544     polyline->line_width = data_real(attribute_first_data(attr));
545 
546   polyline->line_style = LINESTYLE_SOLID;
547   attr = object_find_attribute(obj_node, "line_style");
548   if (attr != NULL)
549     polyline->line_style = data_enum(attribute_first_data(attr));
550 
551   polyline->dashlength = DEFAULT_LINESTYLE_DASHLEN;
552   attr = object_find_attribute(obj_node, "dashlength");
553   if (attr != NULL)
554     polyline->dashlength = data_real(attribute_first_data(attr));
555 
556   load_arrow(obj_node, &polyline->start_arrow, "start_arrow",
557 	     "start_arrow_length", "start_arrow_width");
558 
559   load_arrow(obj_node, &polyline->end_arrow, "end_arrow",
560 	     "end_arrow_length", "end_arrow_width");
561 
562   polyline->absolute_start_gap = 0.0;
563   attr = object_find_attribute(obj_node, "absolute_start_gap");
564   if (attr != NULL)
565     polyline->absolute_start_gap =  data_real( attribute_first_data(attr) );
566   polyline->absolute_end_gap = 0.0;
567   attr = object_find_attribute(obj_node, "absolute_end_gap");
568   if (attr != NULL)
569     polyline->absolute_end_gap =  data_real( attribute_first_data(attr) );
570 
571 
572   polyline->corner_radius = 0.0;
573   attr = object_find_attribute(obj_node, "corner_radius");
574   if (attr != NULL)
575     polyline->corner_radius =  data_real( attribute_first_data(attr) );
576 
577   polyline_update_data(polyline);
578 
579   return &polyline->poly.object;
580 }
581 
582 static ObjectChange *
polyline_add_corner_callback(DiaObject * obj,Point * clicked,gpointer data)583 polyline_add_corner_callback (DiaObject *obj, Point *clicked, gpointer data)
584 {
585   Polyline *poly = (Polyline*) obj;
586   int segment;
587   ObjectChange *change;
588 
589   segment = polyline_closest_segment(poly, clicked);
590   change = polyconn_add_point(&poly->poly, segment, clicked);
591   polyline_update_data(poly);
592   return change;
593 }
594 
595 static ObjectChange *
polyline_delete_corner_callback(DiaObject * obj,Point * clicked,gpointer data)596 polyline_delete_corner_callback (DiaObject *obj, Point *clicked, gpointer data)
597 {
598   Handle *handle;
599   int handle_nr, i;
600   Polyline *poly = (Polyline*) obj;
601   ObjectChange *change;
602 
603   handle = polyline_closest_handle(poly, clicked);
604 
605   for (i = 0; i < obj->num_handles; i++) {
606     if (handle == obj->handles[i]) break;
607   }
608   handle_nr = i;
609   change = polyconn_remove_point(&poly->poly, handle_nr);
610   polyline_update_data(poly);
611   return change;
612 }
613 
614 
615 static DiaMenuItem polyline_menu_items[] = {
616   { N_("Add Corner"), polyline_add_corner_callback, NULL, 1 },
617   { N_("Delete Corner"), polyline_delete_corner_callback, NULL, 1 },
618 };
619 
620 static DiaMenu polyline_menu = {
621   "Polyline",
622   sizeof(polyline_menu_items)/sizeof(DiaMenuItem),
623   polyline_menu_items,
624   NULL
625 };
626 
627 static DiaMenu *
polyline_get_object_menu(Polyline * polyline,Point * clickedpoint)628 polyline_get_object_menu(Polyline *polyline, Point *clickedpoint)
629 {
630   /* Set entries sensitive/selected etc here */
631   polyline_menu_items[0].active = 1;
632   polyline_menu_items[1].active = polyline->poly.numpoints > 2;
633   return &polyline_menu;
634 }
635