2
* GooCanvas. Copyright (C) 2005 Damon Chaplin.
3
* Released under the GNU LGPL license. See COPYING for details.
5
* goocanvasellipse.c - ellipse item.
9
* SECTION:goocanvasellipse
10
* @Title: GooCanvasEllipse
11
* @Short_Description: an ellipse item.
13
* GooCanvasEllipse represents an ellipse item.
15
* It is a subclass of #GooCanvasItemSimple and so inherits all of the style
16
* properties such as "stroke-color", "fill-color" and "line-width".
18
* It also implements the #GooCanvasItem interface, so you can use the
19
* #GooCanvasItem functions such as goo_canvas_item_raise() and
20
* goo_canvas_item_rotate().
22
* To create a #GooCanvasEllipse use goo_canvas_ellipse_new().
24
* To get or set the properties of an existing #GooCanvasEllipse, use
25
* g_object_get() and g_object_set().
27
* The ellipse can be specified either with the "center-x", "center-y",
28
* "radius-x" and "radius-y" properties, or with the "x", "y", "width" and
29
* "height" properties.
33
#include <glib/gi18n-lib.h>
35
#include "goocanvasellipse.h"
53
static void canvas_item_interface_init (GooCanvasItemIface *iface);
54
static void goo_canvas_ellipse_finalize (GObject *object);
55
static void goo_canvas_ellipse_get_property (GObject *object,
59
static void goo_canvas_ellipse_set_property (GObject *object,
63
static void goo_canvas_ellipse_create_path (GooCanvasItemSimple *simple,
66
G_DEFINE_TYPE_WITH_CODE (GooCanvasEllipse, goo_canvas_ellipse,
67
GOO_TYPE_CANVAS_ITEM_SIMPLE,
68
G_IMPLEMENT_INTERFACE (GOO_TYPE_CANVAS_ITEM,
69
canvas_item_interface_init))
73
goo_canvas_ellipse_install_common_properties (GObjectClass *gobject_class)
75
g_object_class_install_property (gobject_class, PROP_CENTER_X,
76
g_param_spec_double ("center-x",
78
_("The x coordinate of the center of the ellipse"),
83
g_object_class_install_property (gobject_class, PROP_CENTER_Y,
84
g_param_spec_double ("center-y",
86
_("The y coordinate of the center of the ellipse"),
91
g_object_class_install_property (gobject_class, PROP_RADIUS_X,
92
g_param_spec_double ("radius-x",
94
_("The horizontal radius of the ellipse"),
95
0.0, G_MAXDOUBLE, 0.0,
98
g_object_class_install_property (gobject_class, PROP_RADIUS_Y,
99
g_param_spec_double ("radius-y",
101
_("The vertical radius of the ellipse"),
102
0.0, G_MAXDOUBLE, 0.0,
105
g_object_class_install_property (gobject_class, PROP_X,
106
g_param_spec_double ("x",
108
_("The x coordinate of the left side of the ellipse"),
113
g_object_class_install_property (gobject_class, PROP_Y,
114
g_param_spec_double ("y",
116
_("The y coordinate of the top of the ellipse"),
121
g_object_class_install_property (gobject_class, PROP_WIDTH,
122
g_param_spec_double ("width",
124
_("The width of the ellipse"),
125
0.0, G_MAXDOUBLE, 0.0,
128
g_object_class_install_property (gobject_class, PROP_HEIGHT,
129
g_param_spec_double ("height",
131
_("The height of the ellipse"),
132
0.0, G_MAXDOUBLE, 0.0,
138
goo_canvas_ellipse_class_init (GooCanvasEllipseClass *klass)
140
GObjectClass *gobject_class = (GObjectClass*) klass;
141
GooCanvasItemSimpleClass *simple_class = (GooCanvasItemSimpleClass*) klass;
143
gobject_class->finalize = goo_canvas_ellipse_finalize;
145
gobject_class->get_property = goo_canvas_ellipse_get_property;
146
gobject_class->set_property = goo_canvas_ellipse_set_property;
148
simple_class->simple_create_path = goo_canvas_ellipse_create_path;
150
goo_canvas_ellipse_install_common_properties (gobject_class);
155
goo_canvas_ellipse_init (GooCanvasEllipse *ellipse)
157
ellipse->ellipse_data = g_slice_new0 (GooCanvasEllipseData);
162
* goo_canvas_ellipse_new:
163
* @parent: the parent item, or %NULL. If a parent is specified, it will assume
164
* ownership of the item, and the item will automatically be freed when it is
165
* removed from the parent. Otherwise call g_object_unref() to free it.
166
* @center_x: the x coordinate of the center of the ellipse.
167
* @center_y: the y coordinate of the center of the ellipse.
168
* @radius_x: the horizontal radius of the ellipse.
169
* @radius_y: the vertical radius of the ellipse.
170
* @...: optional pairs of property names and values, and a terminating %NULL.
172
* Creates a new ellipse item.
176
* Here's an example showing how to create an ellipse centered at (100.0,
177
* 100.0), with a horizontal radius of 50.0 and a vertical radius of 30.0.
178
* It is drawn with a red outline with a width of 5.0 and filled with blue:
180
* <informalexample><programlisting>
181
* GooCanvasItem *ellipse = goo_canvas_ellipse_new (mygroup, 100.0, 100.0, 50.0, 30.0,
182
* "stroke-color", "red",
184
* "fill-color", "blue",
186
* </programlisting></informalexample>
188
* Returns: a new ellipse item.
191
goo_canvas_ellipse_new (GooCanvasItem *parent,
199
GooCanvasEllipse *ellipse;
200
GooCanvasEllipseData *ellipse_data;
201
const char *first_property;
204
item = g_object_new (GOO_TYPE_CANVAS_ELLIPSE, NULL);
205
ellipse = (GooCanvasEllipse*) item;
207
ellipse_data = ellipse->ellipse_data;
208
ellipse_data->center_x = center_x;
209
ellipse_data->center_y = center_y;
210
ellipse_data->radius_x = radius_x;
211
ellipse_data->radius_y = radius_y;
213
va_start (var_args, radius_y);
214
first_property = va_arg (var_args, char*);
216
g_object_set_valist ((GObject*) item, first_property, var_args);
221
goo_canvas_item_add_child (parent, item, -1);
222
g_object_unref (item);
230
goo_canvas_ellipse_finalize (GObject *object)
232
GooCanvasItemSimple *simple = (GooCanvasItemSimple*) object;
233
GooCanvasEllipse *ellipse = (GooCanvasEllipse*) object;
235
/* Free our data if we didn't have a model. (If we had a model it would
236
have been reset in dispose() and simple_data will be NULL.) */
237
if (simple->simple_data)
238
g_slice_free (GooCanvasEllipseData, ellipse->ellipse_data);
239
ellipse->ellipse_data = NULL;
241
G_OBJECT_CLASS (goo_canvas_ellipse_parent_class)->finalize (object);
246
goo_canvas_ellipse_get_common_property (GObject *object,
247
GooCanvasEllipseData *ellipse_data,
255
g_value_set_double (value, ellipse_data->center_x);
258
g_value_set_double (value, ellipse_data->center_y);
261
g_value_set_double (value, ellipse_data->radius_x);
264
g_value_set_double (value, ellipse_data->radius_y);
267
g_value_set_double (value, ellipse_data->center_x - ellipse_data->radius_x);
270
g_value_set_double (value, ellipse_data->center_y - ellipse_data->radius_y);
273
g_value_set_double (value, 2.0 * ellipse_data->radius_x);
276
g_value_set_double (value, 2.0 * ellipse_data->radius_y);
279
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
286
goo_canvas_ellipse_get_property (GObject *object,
291
GooCanvasEllipse *ellipse = (GooCanvasEllipse*) object;
293
goo_canvas_ellipse_get_common_property (object, ellipse->ellipse_data,
294
prop_id, value, pspec);
299
goo_canvas_ellipse_set_common_property (GObject *object,
300
GooCanvasEllipseData *ellipse_data,
310
ellipse_data->center_x = g_value_get_double (value);
311
g_object_notify (object, "x");
314
ellipse_data->center_y = g_value_get_double (value);
315
g_object_notify (object, "y");
318
ellipse_data->radius_x = g_value_get_double (value);
319
g_object_notify (object, "width");
322
ellipse_data->radius_y = g_value_get_double (value);
323
g_object_notify (object, "height");
326
ellipse_data->center_x = g_value_get_double (value) + ellipse_data->radius_x;
327
g_object_notify (object, "center-x");
330
ellipse_data->center_y = g_value_get_double (value) + ellipse_data->radius_y;
331
g_object_notify (object, "center-y");
334
/* Calculate the current x coordinate. */
335
x = ellipse_data->center_x - ellipse_data->radius_x;
336
/* Calculate the new radius_x, which is half the width. */
337
ellipse_data->radius_x = g_value_get_double (value) / 2.0;
338
/* Now calculate the new center_x. */
339
ellipse_data->center_x = x + ellipse_data->radius_x;
341
g_object_notify (object, "center-x");
342
g_object_notify (object, "radius-x");
345
/* Calculate the current y coordinate. */
346
y = ellipse_data->center_y - ellipse_data->radius_y;
347
/* Calculate the new radius_y, which is half the height. */
348
ellipse_data->radius_y = g_value_get_double (value) / 2.0;
349
/* Now calculate the new center_y. */
350
ellipse_data->center_y = y + ellipse_data->radius_y;
352
g_object_notify (object, "center-y");
353
g_object_notify (object, "radius-y");
356
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
363
goo_canvas_ellipse_set_property (GObject *object,
368
GooCanvasItemSimple *simple = (GooCanvasItemSimple*) object;
369
GooCanvasEllipse *ellipse = (GooCanvasEllipse*) object;
373
g_warning ("Can't set property of a canvas item with a model - set the model property instead");
377
goo_canvas_ellipse_set_common_property (object, ellipse->ellipse_data,
378
prop_id, value, pspec);
379
goo_canvas_item_simple_changed (simple, TRUE);
384
goo_canvas_ellipse_create_path (GooCanvasItemSimple *simple,
387
GooCanvasEllipse *ellipse = (GooCanvasEllipse*) simple;
388
GooCanvasEllipseData *ellipse_data = ellipse->ellipse_data;
392
cairo_translate (cr, ellipse_data->center_x, ellipse_data->center_y);
393
cairo_scale (cr, ellipse_data->radius_x, ellipse_data->radius_y);
394
cairo_arc (cr, 0.0, 0.0, 1.0, 0.0, 2.0 * M_PI);
401
goo_canvas_ellipse_set_model (GooCanvasItem *item,
402
GooCanvasItemModel *model)
404
GooCanvasItemSimple *simple = (GooCanvasItemSimple*) item;
405
GooCanvasEllipse *ellipse = (GooCanvasEllipse*) item;
406
GooCanvasEllipseModel *emodel = (GooCanvasEllipseModel*) model;
408
/* If our ellipse_data was allocated, free it. */
410
g_slice_free (GooCanvasEllipseData, ellipse->ellipse_data);
412
/* Now use the new model's ellipse_data instead. */
413
ellipse->ellipse_data = &emodel->ellipse_data;
415
/* Let the parent GooCanvasItemSimple code do the rest. */
416
goo_canvas_item_simple_set_model (simple, model);
421
canvas_item_interface_init (GooCanvasItemIface *iface)
423
iface->set_model = goo_canvas_ellipse_set_model;
428
* SECTION:goocanvasellipsemodel
429
* @Title: GooCanvasEllipseModel
430
* @Short_Description: a model for ellipse items.
432
* GooCanvasEllipseModel represents a model for ellipse items.
434
* It is a subclass of #GooCanvasItemModelSimple and so inherits all of the
435
* style properties such as "stroke-color", "fill-color" and "line-width".
437
* It also implements the #GooCanvasItemModel interface, so you can use the
438
* #GooCanvasItemModel functions such as goo_canvas_item_model_raise() and
439
* goo_canvas_item_model_rotate().
441
* To create a #GooCanvasEllipseModel use goo_canvas_ellipse_model_new().
443
* To get or set the properties of an existing #GooCanvasEllipseModel, use
444
* g_object_get() and g_object_set().
446
* The ellipse can be specified either with the "center-x", "center-y",
447
* "radius-x" and "radius-y" properties, or with the "x", "y", "width" and
448
* "height" properties.
450
* To respond to events such as mouse clicks on the ellipse you must connect
451
* to the signal handlers of the corresponding #GooCanvasEllipse objects.
452
* (See goo_canvas_get_item() and #GooCanvas::item-created.)
455
static void item_model_interface_init (GooCanvasItemModelIface *iface);
456
static void goo_canvas_ellipse_model_finalize (GObject *object);
457
static void goo_canvas_ellipse_model_get_property (GObject *object,
461
static void goo_canvas_ellipse_model_set_property (GObject *object,
466
G_DEFINE_TYPE_WITH_CODE (GooCanvasEllipseModel, goo_canvas_ellipse_model,
467
GOO_TYPE_CANVAS_ITEM_MODEL_SIMPLE,
468
G_IMPLEMENT_INTERFACE (GOO_TYPE_CANVAS_ITEM_MODEL,
469
item_model_interface_init))
473
goo_canvas_ellipse_model_class_init (GooCanvasEllipseModelClass *klass)
475
GObjectClass *gobject_class = (GObjectClass*) klass;
477
gobject_class->finalize = goo_canvas_ellipse_model_finalize;
479
gobject_class->get_property = goo_canvas_ellipse_model_get_property;
480
gobject_class->set_property = goo_canvas_ellipse_model_set_property;
482
goo_canvas_ellipse_install_common_properties (gobject_class);
487
goo_canvas_ellipse_model_init (GooCanvasEllipseModel *emodel)
494
* goo_canvas_ellipse_model_new:
495
* @parent: the parent model, or %NULL. If a parent is specified, it will
496
* assume ownership of the item, and the item will automatically be freed when
497
* it is removed from the parent. Otherwise call g_object_unref() to free it.
498
* @center_x: the x coordinate of the center of the ellipse.
499
* @center_y: the y coordinate of the center of the ellipse.
500
* @radius_x: the horizontal radius of the ellipse.
501
* @radius_y: the vertical radius of the ellipse.
502
* @...: optional pairs of property names and values, and a terminating %NULL.
504
* Creates a new ellipse model.
508
* Here's an example showing how to create an ellipse centered at (100.0,
509
* 100.0), with a horizontal radius of 50.0 and a vertical radius of 30.0.
510
* It is drawn with a red outline with a width of 5.0 and filled with blue:
512
* <informalexample><programlisting>
513
* GooCanvasItemModel *ellipse = goo_canvas_ellipse_model_new (mygroup, 100.0, 100.0, 50.0, 30.0,
514
* "stroke-color", "red",
516
* "fill-color", "blue",
518
* </programlisting></informalexample>
520
* Returns: a new ellipse model.
523
goo_canvas_ellipse_model_new (GooCanvasItemModel *parent,
530
GooCanvasItemModel *model;
531
GooCanvasEllipseModel *emodel;
532
GooCanvasEllipseData *ellipse_data;
533
const char *first_property;
536
model = g_object_new (GOO_TYPE_CANVAS_ELLIPSE_MODEL, NULL);
537
emodel = (GooCanvasEllipseModel*) model;
539
ellipse_data = &emodel->ellipse_data;
540
ellipse_data->center_x = center_x;
541
ellipse_data->center_y = center_y;
542
ellipse_data->radius_x = radius_x;
543
ellipse_data->radius_y = radius_y;
545
va_start (var_args, radius_y);
546
first_property = va_arg (var_args, char*);
548
g_object_set_valist ((GObject*) model, first_property, var_args);
553
goo_canvas_item_model_add_child (parent, model, -1);
554
g_object_unref (model);
562
goo_canvas_ellipse_model_finalize (GObject *object)
564
/*GooCanvasEllipseModel *emodel = (GooCanvasEllipseModel*) object;*/
566
G_OBJECT_CLASS (goo_canvas_ellipse_model_parent_class)->finalize (object);
571
goo_canvas_ellipse_model_get_property (GObject *object,
576
GooCanvasEllipseModel *emodel = (GooCanvasEllipseModel*) object;
578
goo_canvas_ellipse_get_common_property (object, &emodel->ellipse_data,
579
prop_id, value, pspec);
584
goo_canvas_ellipse_model_set_property (GObject *object,
589
GooCanvasEllipseModel *emodel = (GooCanvasEllipseModel*) object;
591
goo_canvas_ellipse_set_common_property (object, &emodel->ellipse_data,
592
prop_id, value, pspec);
593
g_signal_emit_by_name (emodel, "changed", TRUE);
597
static GooCanvasItem*
598
goo_canvas_ellipse_model_create_item (GooCanvasItemModel *model,
603
item = g_object_new (GOO_TYPE_CANVAS_ELLIPSE, NULL);
604
goo_canvas_item_set_model (item, model);
611
item_model_interface_init (GooCanvasItemModelIface *iface)
613
iface->create_item = goo_canvas_ellipse_model_create_item;