1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * view_box.c: GLabels label box object view
8 * Copyright (C) 2001-2007 Jim Evins <evins@snaught.com>.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <glib/gi18n.h>
29 #include <glib/gmem.h>
32 #include "object-editor.h"
35 #include "pixmaps/cursor_box.xbm"
36 #include "pixmaps/cursor_box_mask.xbm"
40 /*========================================================*/
41 /* Private macros and constants. */
42 /*========================================================*/
45 /*========================================================*/
47 /*========================================================*/
49 struct _glViewBoxPrivate {
50 int place_holder; /* Place holder for future private data. */
53 /*========================================================*/
54 /* Private globals. */
55 /*========================================================*/
58 /*========================================================*/
59 /* Private function prototypes. */
60 /*========================================================*/
62 static void gl_view_box_finalize (GObject *object);
64 static GtkWidget *construct_properties_editor (glViewObject *view_object);
66 static void update_object_from_editor_cb (glObjectEditor *editor,
67 glLabelObject *object);
69 static void update_editor_from_object_cb (glLabelObject *object,
70 glObjectEditor *editor);
72 static void update_editor_from_move_cb (glLabelObject *object,
75 glObjectEditor *editor);
77 static gboolean object_at (glViewObject *view_object,
85 /*****************************************************************************/
86 /* Boilerplate object stuff. */
87 /*****************************************************************************/
88 G_DEFINE_TYPE (glViewBox, gl_view_box, GL_TYPE_VIEW_OBJECT);
92 gl_view_box_class_init (glViewBoxClass *class)
94 GObjectClass *object_class = G_OBJECT_CLASS (class);
95 glViewObjectClass *view_object_class = GL_VIEW_OBJECT_CLASS (class);
97 gl_debug (DEBUG_VIEW, "START");
99 gl_view_box_parent_class = g_type_class_peek_parent (class);
101 object_class->finalize = gl_view_box_finalize;
103 view_object_class->construct_editor = construct_properties_editor;
104 view_object_class->object_at = object_at;
106 gl_debug (DEBUG_VIEW, "END");
110 gl_view_box_init (glViewBox *view_box)
112 gl_debug (DEBUG_VIEW, "START");
114 view_box->priv = g_new0 (glViewBoxPrivate, 1);
116 gl_debug (DEBUG_VIEW, "END");
120 gl_view_box_finalize (GObject *object)
122 glViewBox *view_box = GL_VIEW_BOX (object);
124 gl_debug (DEBUG_VIEW, "START");
126 g_return_if_fail (object && GL_IS_VIEW_BOX (object));
128 g_free (view_box->priv);
130 G_OBJECT_CLASS (gl_view_box_parent_class)->finalize (object);
132 gl_debug (DEBUG_VIEW, "END");
135 /*****************************************************************************/
136 /* NEW box object view. */
137 /*****************************************************************************/
139 gl_view_box_new (glLabelBox *object,
144 gl_debug (DEBUG_VIEW, "START");
146 g_return_val_if_fail (object && GL_IS_LABEL_BOX (object), NULL);
147 g_return_val_if_fail (view && GL_IS_VIEW (view), NULL);
149 view_box = g_object_new (gl_view_box_get_type(), NULL);
151 gl_view_object_set_object (GL_VIEW_OBJECT(view_box),
152 GL_LABEL_OBJECT(object),
153 GL_VIEW_OBJECT_HANDLES_BOX);
154 gl_view_object_set_view (GL_VIEW_OBJECT(view_box), view);
156 gl_debug (DEBUG_VIEW, "END");
158 return GL_VIEW_OBJECT (view_box);
161 /*****************************************************************************/
162 /* Create a properties dialog for a box object. */
163 /*****************************************************************************/
165 construct_properties_editor (glViewObject *view_object)
168 glViewBox *view_box = (glViewBox *)view_object;
169 glLabelObject *object;
171 gl_debug (DEBUG_VIEW, "START");
173 object = gl_view_object_get_object (GL_VIEW_OBJECT(view_box));
176 editor = gl_object_editor_new (GL_STOCK_BOX, _("Box object properties"),
178 GL_OBJECT_EDITOR_SHADOW_PAGE,
179 GL_OBJECT_EDITOR_POSITION_PAGE,
180 GL_OBJECT_EDITOR_SIZE_PAGE,
181 GL_OBJECT_EDITOR_FILL_PAGE,
182 GL_OBJECT_EDITOR_LINE_PAGE,
186 update_editor_from_object_cb (object, GL_OBJECT_EDITOR(editor));
187 update_editor_from_move_cb (object, 0, 0, GL_OBJECT_EDITOR(editor));
189 /* Connect signals. */
190 g_signal_connect (G_OBJECT (editor), "changed",
191 G_CALLBACK(update_object_from_editor_cb), object);
192 g_signal_connect (G_OBJECT (object), "changed",
193 G_CALLBACK (update_editor_from_object_cb), editor);
194 g_signal_connect (G_OBJECT (object), "moved",
195 G_CALLBACK (update_editor_from_move_cb), editor);
197 gl_debug (DEBUG_VIEW, "END");
202 /*---------------------------------------------------------------------------*/
203 /* PRIVATE. editor "changed" callback. */
204 /*---------------------------------------------------------------------------*/
206 update_object_from_editor_cb (glObjectEditor *editor,
207 glLabelObject *object)
210 glColorNode *line_color_node;
212 glColorNode *fill_color_node;
213 gboolean shadow_state;
214 gdouble shadow_x, shadow_y;
215 glColorNode *shadow_color_node;
216 gdouble shadow_opacity;
219 gl_debug (DEBUG_VIEW, "START");
221 g_signal_handlers_block_by_func (G_OBJECT(object),
222 update_editor_from_object_cb,
224 g_signal_handlers_block_by_func (G_OBJECT(object),
225 update_editor_from_move_cb,
228 gl_object_editor_get_position (editor, &x, &y);
229 gl_label_object_set_position (object, x, y);
231 gl_object_editor_get_size (editor, &w, &h);
232 gl_label_object_set_size (object, w, h);
234 fill_color_node = gl_object_editor_get_fill_color (editor);
235 gl_label_object_set_fill_color (object, fill_color_node);
236 gl_color_node_free (&fill_color_node);
238 line_color_node = gl_object_editor_get_line_color (editor);
239 gl_label_object_set_line_color (object, line_color_node);
240 gl_color_node_free (&line_color_node);
242 line_width = gl_object_editor_get_line_width (editor);
243 gl_label_object_set_line_width (object, line_width);
245 shadow_state = gl_object_editor_get_shadow_state (editor);
246 gl_label_object_set_shadow_state (object, shadow_state);
248 gl_object_editor_get_shadow_offset (editor, &shadow_x, &shadow_y);
249 gl_label_object_set_shadow_offset (object, shadow_x, shadow_y);
251 shadow_color_node = gl_object_editor_get_shadow_color (editor);
252 gl_label_object_set_shadow_color (object, shadow_color_node);
253 gl_color_node_free (&shadow_color_node);
255 shadow_opacity = gl_object_editor_get_shadow_opacity (editor);
256 gl_label_object_set_shadow_opacity (object, shadow_opacity);
258 g_signal_handlers_unblock_by_func (G_OBJECT(object),
259 update_editor_from_object_cb,
261 g_signal_handlers_unblock_by_func (G_OBJECT(object),
262 update_editor_from_move_cb,
265 gl_debug (DEBUG_VIEW, "END");
268 /*---------------------------------------------------------------------------*/
269 /* PRIVATE. label object "changed" callback. */
270 /*---------------------------------------------------------------------------*/
272 update_editor_from_object_cb (glLabelObject *object,
273 glObjectEditor *editor)
276 glColorNode *line_color_node;
278 glColorNode *fill_color_node;
279 gboolean shadow_state;
280 gdouble shadow_x, shadow_y;
281 glColorNode *shadow_color_node;
282 gdouble shadow_opacity;
285 gl_debug (DEBUG_VIEW, "START");
287 gl_label_object_get_size (object, &w, &h);
288 gl_object_editor_set_size (editor, w, h);
289 merge = gl_label_get_merge (GL_LABEL(object->parent));
291 fill_color_node = gl_label_object_get_fill_color (GL_LABEL_OBJECT(object));
292 gl_object_editor_set_fill_color (editor, (merge != NULL), fill_color_node);
293 gl_color_node_free (&fill_color_node);
295 line_color_node = gl_label_object_get_line_color (GL_LABEL_OBJECT(object));
296 gl_object_editor_set_line_color (editor, (merge != NULL), line_color_node);
297 gl_color_node_free (&line_color_node);
299 line_width = gl_label_object_get_line_width (GL_LABEL_OBJECT(object));
300 gl_object_editor_set_line_width (editor, line_width);
302 shadow_state = gl_label_object_get_shadow_state (object);
303 gl_object_editor_set_shadow_state (editor, shadow_state);
305 gl_label_object_get_shadow_offset (object, &shadow_x, &shadow_y);
306 gl_object_editor_set_shadow_offset (editor, shadow_x, shadow_y);
308 shadow_color_node = gl_label_object_get_shadow_color (object);
309 gl_object_editor_set_shadow_color (editor, (merge != NULL), shadow_color_node);
310 gl_color_node_free (&shadow_color_node);
312 shadow_opacity = gl_label_object_get_shadow_opacity (object);
313 gl_object_editor_set_shadow_opacity (editor, shadow_opacity);
315 gl_debug (DEBUG_VIEW, "END");
318 /*---------------------------------------------------------------------------*/
319 /* PRIVATE. label object "moved" callback. */
320 /*---------------------------------------------------------------------------*/
322 update_editor_from_move_cb (glLabelObject *object,
325 glObjectEditor *editor)
329 gl_debug (DEBUG_VIEW, "START");
331 gl_label_object_get_position (object, &x, &y);
332 gl_object_editor_set_position (editor, x, y);
334 gl_debug (DEBUG_VIEW, "END");
337 /*****************************************************************************/
338 /* Is object at (x,y)? */
339 /*****************************************************************************/
341 object_at (glViewObject *view_object,
346 glLabelObject *object;
350 object = gl_view_object_get_object (view_object);
352 gl_label_object_get_size (object, &w, &h);
354 cairo_rectangle (cr, 0.0, 0.0, w, h);
356 if (cairo_in_fill (cr, x, y))
361 line_width = gl_label_object_get_line_width (object);
362 cairo_set_line_width (cr, line_width);
363 if (cairo_in_stroke (cr, x, y))
372 /*****************************************************************************/
373 /* Return apropos cursor for create object mode. */
374 /*****************************************************************************/
376 gl_view_box_get_create_cursor (void)
378 GdkCursor *cursor = NULL;
379 GdkPixmap *pixmap_data, *pixmap_mask;
380 GdkColor fg = { 0, 0, 0, 0 };
381 GdkColor bg = { 0, 65535, 65535, 65535 };
383 gl_debug (DEBUG_VIEW, "START");
385 pixmap_data = gdk_bitmap_create_from_data (NULL,
386 (gchar *)cursor_box_bits,
389 pixmap_mask = gdk_bitmap_create_from_data (NULL,
390 (gchar *)cursor_box_mask_bits,
391 cursor_box_mask_width,
392 cursor_box_mask_height);
393 cursor = gdk_cursor_new_from_pixmap (pixmap_data, pixmap_mask, &fg,
394 &bg, cursor_box_x_hot,
397 gl_debug (DEBUG_VIEW, "END");
402 /*****************************************************************************/
403 /* Object creation handler: button press event. */
404 /*****************************************************************************/
406 gl_view_box_create_button_press_event (glView *view,
411 glColorNode *fill_color_node;
412 glColorNode *line_color_node;
414 gl_view_unselect_all (view);
416 fill_color_node = gl_color_node_new_default ();
417 line_color_node = gl_color_node_new_default ();
419 object = gl_label_box_new (view->label);
420 gl_label_object_set_position (GL_LABEL_OBJECT(object), x, y);
421 gl_label_object_set_size (GL_LABEL_OBJECT(object), 0.0, 0.0);
422 line_color_node->color = gl_color_set_opacity (gl_view_get_default_line_color(view), 0.5);
423 fill_color_node->color = gl_color_set_opacity (gl_view_get_default_fill_color(view), 0.5);
424 gl_label_object_set_line_width (GL_LABEL_OBJECT(object),
425 gl_view_get_default_line_width(view));
426 gl_label_object_set_line_color (GL_LABEL_OBJECT(object),
428 gl_label_object_set_fill_color (GL_LABEL_OBJECT(object),
431 gl_color_node_free (&fill_color_node);
432 gl_color_node_free (&line_color_node);
434 view->create_object = GL_LABEL_OBJECT (object);
439 /*****************************************************************************/
440 /* Object creation handler: motion event. */
441 /*****************************************************************************/
443 gl_view_box_create_motion_event (glView *view,
449 gl_label_object_set_position (GL_LABEL_OBJECT(view->create_object),
450 MIN (x, view->create_x0), MIN (y, view->create_y0));
451 w = MAX (x, view->create_x0) - MIN (x, view->create_x0);
452 h = MAX (y, view->create_y0) - MIN (y, view->create_y0);
453 gl_label_object_set_size (GL_LABEL_OBJECT(view->create_object), w, h);
456 /*****************************************************************************/
457 /* Object creation handler: button relesase event. */
458 /*****************************************************************************/
460 gl_view_box_create_button_release_event (glView *view,
464 glColorNode *fill_color_node;
465 glColorNode *line_color_node;
468 fill_color_node = gl_color_node_new_default ();
469 line_color_node = gl_color_node_new_default ();
471 if ((view->create_x0 == x) && (view->create_y0 == y)) {
472 x = view->create_x0 + 36.0;
473 y = view->create_y0 + 36.0;
475 gl_label_object_set_position (GL_LABEL_OBJECT(view->create_object),
476 MIN (x, view->create_x0), MIN (y, view->create_y0));
477 w = MAX (x, view->create_x0) - MIN (x, view->create_x0);
478 h = MAX (y, view->create_y0) - MIN (y, view->create_y0);
479 gl_label_object_set_size (GL_LABEL_OBJECT(view->create_object), w, h);
480 line_color_node->color = gl_view_get_default_line_color(view);
481 gl_label_object_set_line_color (GL_LABEL_OBJECT(view->create_object), line_color_node);
482 fill_color_node->color = gl_view_get_default_fill_color(view);
483 gl_label_object_set_fill_color (GL_LABEL_OBJECT(view->create_object), fill_color_node);
484 gl_color_node_free (&fill_color_node);
485 gl_color_node_free (&line_color_node);