2 * (GLABELS) Label and Business Card Creation program for GNOME
4 * view_ellipse.c: GLabels label ellipse object widget
6 * Copyright (C) 2001-2003 Jim Evins <evins@snaught.com>.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "view-ellipse.h"
27 #include "view-highlight.h"
30 #include "object-editor.h"
33 #include "pixmaps/cursor_ellipse.xbm"
34 #include "pixmaps/cursor_ellipse_mask.xbm"
38 /*========================================================*/
39 /* Private macros and constants. */
40 /*========================================================*/
44 /*========================================================*/
46 /*========================================================*/
48 struct _glViewEllipsePrivate {
49 GnomeCanvasItem *item;
52 /*========================================================*/
53 /* Private globals. */
54 /*========================================================*/
56 static glViewObjectClass *parent_class = NULL;
59 /*========================================================*/
60 /* Private function prototypes. */
61 /*========================================================*/
63 static void gl_view_ellipse_class_init (glViewEllipseClass *klass);
64 static void gl_view_ellipse_instance_init (glViewEllipse *view_ellipse);
65 static void gl_view_ellipse_finalize (GObject *object);
67 static GtkWidget *construct_properties_editor (glViewObject *view_object);
69 static void update_canvas_item_from_object_cb (glLabelObject *object,
70 glViewEllipse *view_ellipse);
72 static void update_object_from_editor_cb (glObjectEditor *editor,
73 glLabelObject *object);
75 static void update_editor_from_object_cb (glLabelObject *object,
76 glObjectEditor *editor);
78 static void update_editor_from_move_cb (glLabelObject *object,
81 glObjectEditor *editor);
83 static void update_editor_from_label_cb (glLabel *label,
84 glObjectEditor *editor);
88 /*****************************************************************************/
89 /* Boilerplate object stuff. */
90 /*****************************************************************************/
92 gl_view_ellipse_get_type (void)
94 static GType type = 0;
98 sizeof (glViewEllipseClass),
101 (GClassInitFunc) gl_view_ellipse_class_init,
104 sizeof (glViewEllipse),
106 (GInstanceInitFunc) gl_view_ellipse_instance_init,
109 type = g_type_register_static (GL_TYPE_VIEW_OBJECT,
110 "glViewEllipse", &info, 0);
117 gl_view_ellipse_class_init (glViewEllipseClass *klass)
119 GObjectClass *object_class = (GObjectClass *) klass;
120 glViewObjectClass *view_object_class = (glViewObjectClass *) klass;
122 gl_debug (DEBUG_VIEW, "START");
124 parent_class = g_type_class_peek_parent (klass);
126 object_class->finalize = gl_view_ellipse_finalize;
128 view_object_class->construct_editor = construct_properties_editor;
130 gl_debug (DEBUG_VIEW, "END");
134 gl_view_ellipse_instance_init (glViewEllipse *view_ellipse)
136 gl_debug (DEBUG_VIEW, "START");
138 view_ellipse->private = g_new0 (glViewEllipsePrivate, 1);
140 gl_debug (DEBUG_VIEW, "END");
144 gl_view_ellipse_finalize (GObject *object)
148 gl_debug (DEBUG_VIEW, "START");
150 g_return_if_fail (object && GL_IS_VIEW_ELLIPSE (object));
152 G_OBJECT_CLASS (parent_class)->finalize (object);
154 gl_debug (DEBUG_VIEW, "END");
157 /*****************************************************************************/
158 /* NEW ellipse object view. */
159 /*****************************************************************************/
161 gl_view_ellipse_new (glLabelEllipse *object,
164 glViewEllipse *view_ellipse;
166 guint line_color, fill_color;
169 gl_debug (DEBUG_VIEW, "START");
170 g_return_if_fail (object && GL_IS_LABEL_ELLIPSE (object));
171 g_return_if_fail (view && GL_IS_VIEW (view));
173 view_ellipse = g_object_new (gl_view_ellipse_get_type(), NULL);
175 gl_view_object_set_view (GL_VIEW_OBJECT(view_ellipse), view);
176 gl_view_object_set_object (GL_VIEW_OBJECT(view_ellipse),
177 GL_LABEL_OBJECT(object),
178 GL_VIEW_HIGHLIGHT_ELLIPSE_RESIZABLE);
180 /* Query properties of object. */
181 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
182 line_width = gl_label_object_get_line_width(GL_LABEL_OBJECT(object));
183 line_color = gl_label_object_get_line_color(GL_LABEL_OBJECT(object));
184 fill_color = gl_label_object_get_fill_color(GL_LABEL_OBJECT(object));
186 /* Create analogous canvas item. */
187 view_ellipse->private->item =
188 gl_view_object_item_new (GL_VIEW_OBJECT(view_ellipse),
189 gnome_canvas_ellipse_get_type (),
194 "width_units", line_width,
195 "outline_color_rgba", line_color,
196 "fill_color_rgba", fill_color,
199 g_signal_connect (G_OBJECT (object), "changed",
200 G_CALLBACK (update_canvas_item_from_object_cb), view_ellipse);
202 gl_debug (DEBUG_VIEW, "END");
204 return GL_VIEW_OBJECT (view_ellipse);
207 /*****************************************************************************/
208 /* Create a properties editor for an ellipse object. */
209 /*****************************************************************************/
211 construct_properties_editor (glViewObject *view_object)
214 glViewEllipse *view_ellipse = (glViewEllipse *)view_object;
215 glLabelObject *object;
217 gl_debug (DEBUG_VIEW, "START");
219 object = gl_view_object_get_object (GL_VIEW_OBJECT(view_ellipse));
222 editor = gl_object_editor_new (GL_STOCK_ELLIPSE, _("Ellipse object properties"),
223 GL_OBJECT_EDITOR_POSITION_PAGE,
224 GL_OBJECT_EDITOR_SIZE_PAGE,
225 GL_OBJECT_EDITOR_FILL_PAGE,
226 GL_OBJECT_EDITOR_LINE_PAGE,
230 update_editor_from_object_cb (object, GL_OBJECT_EDITOR(editor));
231 update_editor_from_move_cb (object, 0, 0, GL_OBJECT_EDITOR(editor));
232 update_editor_from_label_cb (object->parent, GL_OBJECT_EDITOR(editor));
234 /* Connect signals. */
235 g_signal_connect (G_OBJECT (editor), "changed",
236 G_CALLBACK(update_object_from_editor_cb), object);
237 g_signal_connect (G_OBJECT (object), "changed",
238 G_CALLBACK (update_editor_from_object_cb), editor);
239 g_signal_connect (G_OBJECT (object), "moved",
240 G_CALLBACK (update_editor_from_move_cb), editor);
241 g_signal_connect (G_OBJECT (object->parent), "size_changed",
242 G_CALLBACK (update_editor_from_label_cb), editor);
244 gl_debug (DEBUG_VIEW, "END");
249 /*---------------------------------------------------------------------------*/
250 /* PRIVATE. label object "changed" callback. */
251 /*---------------------------------------------------------------------------*/
253 update_canvas_item_from_object_cb (glLabelObject *object,
254 glViewEllipse *view_ellipse)
257 guint line_color, fill_color;
260 gl_debug (DEBUG_VIEW, "START");
262 /* Query properties of object. */
263 gl_label_object_get_size (GL_LABEL_OBJECT(object), &w, &h);
264 line_width = gl_label_object_get_line_width(GL_LABEL_OBJECT(object));
265 line_color = gl_label_object_get_line_color(GL_LABEL_OBJECT(object));
266 fill_color = gl_label_object_get_fill_color(GL_LABEL_OBJECT(object));
268 /* Adjust appearance of analogous canvas item. */
269 gnome_canvas_item_set (view_ellipse->private->item,
272 "width_units", line_width,
273 "outline_color_rgba", line_color,
274 "fill_color_rgba", fill_color,
277 gl_debug (DEBUG_VIEW, "END");
280 /*---------------------------------------------------------------------------*/
281 /* PRIVATE. editor "changed" callback. */
282 /*---------------------------------------------------------------------------*/
284 update_object_from_editor_cb (glObjectEditor *editor,
285 glLabelObject *object)
288 guint line_color, fill_color;
291 gl_debug (DEBUG_VIEW, "START");
293 g_signal_handlers_block_by_func (G_OBJECT(object),
294 update_editor_from_object_cb,
296 g_signal_handlers_block_by_func (G_OBJECT(object),
297 update_editor_from_move_cb,
301 gl_object_editor_get_position (editor, &x, &y);
302 gl_label_object_set_position (object, x, y);
304 gl_object_editor_get_size (editor, &w, &h);
305 gl_label_object_set_size (object, w, h);
307 fill_color = gl_object_editor_get_fill_color (editor);
308 gl_label_object_set_fill_color (object, fill_color);
310 line_color = gl_object_editor_get_line_color (editor);
311 gl_label_object_set_line_color (object, line_color);
313 line_width = gl_object_editor_get_line_width (editor);
314 gl_label_object_set_line_width (object, line_width);
317 g_signal_handlers_unblock_by_func (G_OBJECT(object),
318 update_editor_from_object_cb,
320 g_signal_handlers_unblock_by_func (G_OBJECT(object),
321 update_editor_from_move_cb,
324 gl_debug (DEBUG_VIEW, "END");
327 /*---------------------------------------------------------------------------*/
328 /* PRIVATE. label object "changed" callback. */
329 /*---------------------------------------------------------------------------*/
331 update_editor_from_object_cb (glLabelObject *object,
332 glObjectEditor *editor)
335 guint line_color, fill_color;
338 gl_debug (DEBUG_VIEW, "START");
340 gl_label_object_get_size (object, &w, &h);
341 gl_object_editor_set_size (editor, w, h);
343 fill_color = gl_label_object_get_fill_color (GL_LABEL_OBJECT(object));
344 gl_object_editor_set_fill_color (editor, fill_color);
346 line_color = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
347 gl_object_editor_set_line_color (editor, line_color);
349 line_width = gl_label_object_get_line_width (GL_LABEL_OBJECT(object));
350 gl_object_editor_set_line_width (editor, line_width);
352 gl_debug (DEBUG_VIEW, "END");
355 /*---------------------------------------------------------------------------*/
356 /* PRIVATE. label object "moved" callback. */
357 /*---------------------------------------------------------------------------*/
359 update_editor_from_move_cb (glLabelObject *object,
362 glObjectEditor *editor)
366 gl_debug (DEBUG_VIEW, "START");
368 gl_label_object_get_position (object, &x, &y);
369 gl_object_editor_set_position (editor, x, y);
371 gl_debug (DEBUG_VIEW, "END");
374 /*---------------------------------------------------------------------------*/
375 /* PRIVATE. label "changed" callback. */
376 /*---------------------------------------------------------------------------*/
378 update_editor_from_label_cb (glLabel *label,
379 glObjectEditor *editor)
381 gdouble label_width, label_height;
383 gl_debug (DEBUG_VIEW, "START");
385 gl_label_get_size (label, &label_width, &label_height);
386 gl_object_editor_set_max_position (GL_OBJECT_EDITOR (editor),
387 label_width, label_height);
388 gl_object_editor_set_max_size (GL_OBJECT_EDITOR (editor),
389 label_width, label_height);
391 gl_debug (DEBUG_VIEW, "END");
394 /*****************************************************************************/
395 /* Return apropos cursor for create object mode. */
396 /*****************************************************************************/
398 gl_view_ellipse_get_create_cursor (void)
400 static GdkCursor *cursor = NULL;
401 GdkPixmap *pixmap_data, *pixmap_mask;
402 GdkColor fg = { 0, 0, 0, 0 };
403 GdkColor bg = { 0, 65535, 65535, 65535 };
405 gl_debug (DEBUG_VIEW, "START");
408 pixmap_data = gdk_bitmap_create_from_data (NULL,
410 cursor_ellipse_width,
411 cursor_ellipse_height);
412 pixmap_mask = gdk_bitmap_create_from_data (NULL,
413 cursor_ellipse_mask_bits,
414 cursor_ellipse_mask_width,
415 cursor_ellipse_mask_height);
417 gdk_cursor_new_from_pixmap (pixmap_data, pixmap_mask, &fg,
418 &bg, cursor_ellipse_x_hot,
419 cursor_ellipse_y_hot);
422 gl_debug (DEBUG_VIEW, "END");
427 /*****************************************************************************/
428 /* Canvas event handler for creating ellipse objects. */
429 /*****************************************************************************/
431 gl_view_ellipse_create_event_handler (GnomeCanvas *canvas,
435 static gdouble x0, y0;
436 static gboolean dragging = FALSE;
437 static glViewObject *view_ellipse;
438 static GObject *object;
439 guint line_color, fill_color;
442 gl_debug (DEBUG_VIEW, "");
444 switch (event->type) {
446 case GDK_BUTTON_PRESS:
447 switch (event->button.button) {
450 gnome_canvas_item_grab ( canvas->root,
451 GDK_POINTER_MOTION_MASK |
452 GDK_BUTTON_RELEASE_MASK |
453 GDK_BUTTON_PRESS_MASK,
454 NULL, event->button.time);
455 gnome_canvas_window_to_world (canvas,
457 event->button.y, &x, &y);
458 object = gl_label_ellipse_new (view->label);
459 gl_label_object_set_position (GL_LABEL_OBJECT(object),
461 gl_label_object_set_size (GL_LABEL_OBJECT(object),
463 line_color = gl_color_set_opacity (gl_view_get_default_line_color(view), 0.5);
464 fill_color = gl_color_set_opacity (gl_view_get_default_fill_color(view), 0.5);
465 gl_label_object_set_line_width (GL_LABEL_OBJECT(object),
466 gl_view_get_default_line_width(view));
467 gl_label_object_set_line_color (GL_LABEL_OBJECT(object),
469 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object),
471 view_ellipse = gl_view_ellipse_new (GL_LABEL_ELLIPSE(object),
481 case GDK_BUTTON_RELEASE:
482 switch (event->button.button) {
485 gnome_canvas_item_ungrab (canvas->root, event->button.time);
486 gnome_canvas_window_to_world (canvas,
488 event->button.y, &x, &y);
489 if ((x0 == x) && (y0 == y)) {
493 gl_label_object_set_position (GL_LABEL_OBJECT(object),
494 MIN (x, x0), MIN (y, y0));
495 w = MAX (x, x0) - MIN (x, x0);
496 h = MAX (y, y0) - MIN (y, y0);
497 gl_label_object_set_size (GL_LABEL_OBJECT(object),
499 gl_label_object_set_line_color (GL_LABEL_OBJECT(object),
500 gl_view_get_default_line_color(view));
501 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object),
502 gl_view_get_default_fill_color(view));
503 gl_view_unselect_all (view);
504 gl_view_object_select (GL_VIEW_OBJECT(view_ellipse));
505 gl_view_arrow_mode (view);
512 case GDK_MOTION_NOTIFY:
513 if (dragging && (event->motion.state & GDK_BUTTON1_MASK)) {
514 gnome_canvas_window_to_world (canvas,
516 event->motion.y, &x, &y);
517 gl_label_object_set_position (GL_LABEL_OBJECT(object),
518 MIN (x, x0), MIN (y, y0));
519 w = MAX (x, x0) - MIN (x, x0);
520 h = MAX (y, y0) - MIN (y, y0);
521 gl_label_object_set_size (GL_LABEL_OBJECT(object),