1 /* Dia -- an diagram creation/manipulation program -*- c -*-
2 * Copyright (C) 1998 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 #include "highlight.h"
20
21 #include "glib.h"
22
23 #include "diagram.h"
24 #include "diagramdata.h"
25 #include "object.h"
26 #include "object_ops.h"
27 #include "group.h"
28
29 /* One could argue that the actual setting of the object's field
30 * should happen inside lib rather than app. I think that'd be overkill.
31 * -Lars
32 */
33
34 /* The highlighting must happen within a certain small area around the bbox.
35 * Highlighting sets the bbox to ... some more.
36 * Problem: The bbox is the same for all views, but highlighting size should
37 * depend on the zoom level. The renderer obviously can figure out to make
38 * it a few more pixels or something, but we need the bbox to also be
39 * enlarged by a bit. I guess the object_add_updates call must handle that,
40 * as it knows about the conversion.
41 */
42
43 static Color red = { 1.0, 0.0, 0.0 };
44
45 void
highlight_object(DiaObject * obj,Color * col,Diagram * dia)46 highlight_object(DiaObject *obj, Color *col, Diagram *dia)
47 {
48 if (col)
49 obj->highlight_color = col;
50 else
51 obj->highlight_color = &red;
52
53 object_add_updates(obj, dia);
54 }
55
56 void
highlight_object_off(DiaObject * obj,Diagram * dia)57 highlight_object_off(DiaObject *obj, Diagram *dia)
58 {
59 if (obj->highlight_color != NULL) {
60 /* Must add updates first, so we get the border erased. */
61 object_add_updates(obj, dia);
62 obj->highlight_color = NULL;
63 }
64 }
65
66 /** Resets all highlighting in this layer. Helper function for
67 * highlight_reset_all
68 */
69 static void
highlight_reset_objects(GList * objects,Diagram * dia)70 highlight_reset_objects(GList *objects, Diagram *dia)
71 {
72 for (; objects != NULL; objects = g_list_next(objects)) {
73 DiaObject *object = (DiaObject*)objects->data;
74 highlight_object_off(object, dia);
75 if (IS_GROUP(object)) {
76 highlight_reset_objects(group_objects(object), dia);
77 }
78 }
79 }
80
81 /* Currently does a brute-force run-through of all objects.
82 * If this is too slow, we could create a list of currently highlighted
83 * objects in the diagram and traverse that before killing it.
84 * Note that we're not assuming that all highlighted objects are in
85 * the active layer.
86 */
87 void
highlight_reset_all(Diagram * dia)88 highlight_reset_all(Diagram *dia) {
89 int i;
90 for (i = 0; i < dia->data->layers->len; i++) {
91 highlight_reset_objects(((Layer*)g_ptr_array_index(dia->data->layers, i))->objects, dia);
92 }
93 }
94