Debian Codegraph @

src/tools/dlgedit/dlg_node.cc

raw
1 /*
2 Copyright (C) 2002 Kai Sterker <[email protected]>
3 Part of the Adonthell Project <http://adonthell.nongnu.org>
4
5 Dlgedit is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 Dlgedit is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with Dlgedit. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /**
20 * @file dlg_node.cc
21 *
22 * @author Kai Sterker
23 * @brief Base class for the dialogue objects.
24 */
25
26 #include <math.h>
27 #include "dlg_node.h"
28
29 // Coonstructor
30 DlgNode::DlgNode (node_type t, DlgRect area)
31 {
32 type_ = t;
33 index_ = -1;
34 top_left = area.topLeft ();
35 bottom_right = area.bottomRight ();
36 }
37
38 // Get the next node
39 DlgNode* DlgNode::next (query_type pos, int offset)
40 {
41 if (next_.empty ()) return NULL;
42 else return getNode (n, next_, pos, offset);
43 }
44
45 // Get the previous node
46 DlgNode* DlgNode::prev (query_type pos, int offset)
47 {
48 if (prev_.empty ()) return NULL;
49 else return getNode (p, prev_, pos, offset);
50 }
51
52 // Get a node from the specified list
53 DlgNode* DlgNode::getNode (std::list<DlgNode*>::iterator &it,
54 std::list<DlgNode*> &lst, query_type &pos, int &offset)
55 {
56 switch (pos)
57 {
58 case FIRST:
59 // point to the next element
60 it = lst.begin ();
61 offset -= 1;
62 break;
63 case NEXT:
64 // nothing needs be done
65 break;
66 case LAST:
67 // point to the last element
68 it = lst.end ();
69 offset = -offset;
70 break;
71 case PREV:
72 // reverse searching
73 offset = -offset;
74 break;
75 case CURRENT:
76 if (it != lst.end ()) return *it;
77 else return *(lst.begin ());
78 break;
79 default:
80 return NULL;
81 }
82
83 // forward iteration
84 if (offset >= 0)
85 {
86 for (int i = 0; i < offset && it != lst.end (); i++, it++);
87 if (it != lst.end ()) return *it;
88 }
89
90 // backward iteration
91 else
92 {
93 int i;
94 for (i = offset; i < 0 && it != lst.begin (); i++, it--);
95 if (i == 0) return *it;
96 }
97
98 return NULL;
99 }
100
101 // add node to list of followers
102 void DlgNode::addNext (DlgNode *node)
103 {
104 std::list<DlgNode*>::iterator i = next_.begin ();
105 DlgRect *cmp1, *cmp2 = node->type () == LINK ? node->next (FIRST) : node;
106
107 // search the proper place for insertion
108 while (i != next_.end ())
109 {
110 cmp1 = (*i)->type () == LINK ? (*i)->next (FIRST) : *i;
111 if (*cmp2 < *cmp1) break;
112 i++;
113 }
114
115 // insert
116 next_.insert (i, node);
117 }
118
119 // add node to list of precedessors
120 void DlgNode::addPrev (DlgNode *node)
121 {
122 std::list<DlgNode*>::iterator i = prev_.begin ();
123 DlgRect *cmp1, *cmp2 = node->type () == LINK ? node->prev (FIRST) : node;
124
125 // search the proper place for insertion
126 while (i != prev_.end ())
127 {
128 cmp1 = (*i)->type () == LINK ? (*i)->prev (FIRST) : *i;
129 if (*cmp2 < *cmp1) break;
130 i++;
131 }
132
133 prev_.insert (i, node);
134 }
135
136 // remove a node from the list of followers
137 void DlgNode::removeNext (DlgNode *node)
138 {
139 next_.remove (node);
140 n = next_.begin ();
141 }
142
143 // remove a node from the list of precedessors
144 void DlgNode::removePrev (DlgNode *node)
145 {
146 prev_.remove (node);
147 p = prev_.begin ();
148 }
149
150 // draw the node (with a certain mode)
151 void DlgNode::draw (cairo_surface_t *surface, DlgPoint &offset, GtkWidget *widget, mode_type m)
152 {
153 // change the mode temporarily
154 mode_type oldMode = mode_;
155 mode_ = m;
156
157 // draw
158 draw (surface, offset, widget);
159
160 // restore old mode
161 mode_ = oldMode;
162 }
163
164 void DlgNode::drawPolygon (cairo_t *cr, const GdkColor *color, const bool & filled, const DlgPoint *points, const int &num_points)
165 {
166 gdk_cairo_set_source_color (cr, color);
167 cairo_set_line_width (cr, 0.6);
168 cairo_move_to (cr, points[0].x(), points[0].y());
169 for (int i = 1; i < num_points; i++)
170 {
171 cairo_line_to (cr, points[i].x(), points[i].y());
172 }
173 filled ? cairo_fill (cr) : cairo_stroke (cr);
174 }
175
176 void DlgNode::drawCircle (cairo_t *cr, const GdkColor *color, const bool & filled, const int & x, const int & y, const int & radius)
177 {
178 gdk_cairo_set_source_color (cr, color);
179 cairo_set_line_width (cr, 0.6);
180 cairo_arc (cr, x, y, radius, 0, 2 * M_PI);
181 filled ? cairo_fill (cr) : cairo_stroke (cr);
182 }
183
184 void DlgNode::drawRectangle (cairo_t *cr, const GdkColor *color, const bool & filled, const int & x, const int & y, const int & width, const int & height)
185 {
186 gdk_cairo_set_source_color (cr, color);
187 cairo_set_line_width (cr, 0.6);
188 cairo_rectangle (cr, x, y, width, height);
189 filled ? cairo_fill (cr) : cairo_stroke (cr);
190 }