~ubuntu-branches/ubuntu/intrepid/tcm/intrepid

« back to all changes in this revision

Viewing changes to src/sd/tr/trviewer.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2003-07-03 20:08:21 UTC
  • Revision ID: james.westby@ubuntu.com-20030703200821-se4xtqx25e5miczi
Tags: upstream-2.20
ImportĀ upstreamĀ versionĀ 2.20

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
//
 
3
// This file is part of Toolkit for Conceptual Modeling (TCM).
 
4
// (c) copyright 1995, Vrije Universiteit Amsterdam.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
//
 
7
// TCM is free software; you can redistribute it and/or modify
 
8
// it under the terms of the GNU General Public License as published by
 
9
// the Free Software Foundation; either version 2 of the License, or
 
10
// (at your option) any later version.
 
11
//
 
12
// TCM is distributed in the hope that it will be useful,
 
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
// GNU General Public License for more details.
 
16
//
 
17
// You should have received a copy of the GNU General Public License
 
18
// along with TCM; if not, write to the Free Software
 
19
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
20
// 02111-1307, USA.
 
21
////////////////////////////////////////////////////////////////////////////////
 
22
#include "trviewer.h"
 
23
#include "trview.h"
 
24
#include "trwindow.h"
 
25
#include "shape.h"
 
26
#include "trdiagram.h"
 
27
#include "shapeview.h"
 
28
#include "grafport.h"
 
29
 
 
30
TRViewer::TRViewer(Config *c, TRWindow *d): DiagramViewer(c, d) { 
 
31
        treeMode = False;
 
32
}
 
33
 
 
34
void TRViewer::Initialize() {
 
35
        TRView *v = new TRView(this);
 
36
        GetViews()->add(v);
 
37
        SetView(v);
 
38
}
 
39
 
 
40
void TRViewer::UpdateTreeMode(bool newMode) {
 
41
        if (newMode)
 
42
                SetStatus("view as forked tree");
 
43
        else
 
44
                SetStatus("view as editable graph");
 
45
        if (newMode != treeMode && !IsPasting() && !IsZigZag()) {
 
46
                DeselectAll();
 
47
                treeMode = newMode;
 
48
                if (treeMode) {
 
49
                        GetMainWindow()->EnableUndo(False);
 
50
                        GetMainWindow()->EnableRedo(False);
 
51
                }
 
52
                else {
 
53
                        GetMainWindow()->EnableUndo(True);
 
54
                        GetMainWindow()->EnableRedo(True);
 
55
                }
 
56
                Refresh();
 
57
        }
 
58
}
 
59
 
 
60
void TRViewer::Select(int x, int y) {
 
61
        if (!treeMode)
 
62
                DiagramViewer::Select(x, y);
 
63
}
 
64
 
 
65
void TRViewer::Adjust(int x, int y) {
 
66
        if (!treeMode)
 
67
                DiagramViewer::Adjust(x, y);
 
68
}
 
69
 
 
70
void TRViewer::MovingPointer(int x, int y) {
 
71
        if (!treeMode)
 
72
                DiagramViewer::MovingPointer(x, y);
 
73
}
 
74
 
 
75
void TRViewer::KeyTyped(int x, int y, int c) {
 
76
        if (!treeMode)
 
77
                DiagramViewer::KeyTyped(x, y, c);
 
78
}
 
79
 
 
80
Command *TRViewer::Drag(int x, int y) {
 
81
        if (!treeMode)
 
82
                return DiagramViewer::Drag(x, y);
 
83
        else
 
84
                return 0;
 
85
}
 
86
 
 
87
Command *TRViewer::Connect(int x, int y) {
 
88
        if (!treeMode)
 
89
                return DiagramViewer::Connect(x, y);
 
90
        else
 
91
                return 0;
 
92
}
 
93
 
 
94
void TRViewer::SelectAll() {
 
95
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::SelectAll();
 
96
}
 
97
 
 
98
void TRViewer::Cut() {
 
99
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::Cut();
 
100
}
 
101
 
 
102
void TRViewer::Copy() {
 
103
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::Copy();
 
104
}
 
105
 
 
106
void TRViewer::UpdateCurve() {
 
107
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::UpdateCurve();
 
108
}
 
109
 
 
110
void TRViewer::Paste() {
 
111
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::Paste();
 
112
}
 
113
 
 
114
void TRViewer::DeleteAll() {
 
115
        treeMode? SetStatus("Aborted: in view mode"):
 
116
                DiagramViewer::DeleteAll();
 
117
}
 
118
 
 
119
void TRViewer::DeleteSubjects() {
 
120
        treeMode? SetStatus("Aborted: in view mode"):
 
121
                DiagramViewer::DeleteSubjects();
 
122
}
 
123
 
 
124
void TRViewer::AlignNodes(NodeAlign::Type n) {
 
125
        treeMode? SetStatus("Aborted: in view mode"):
 
126
                DiagramViewer::AlignNodes(n);
 
127
}
 
128
 
 
129
void TRViewer::SameSize() {
 
130
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::SameSize();
 
131
}
 
132
 
 
133
void TRViewer::MoveAllShapes(Document::MoveType t) {
 
134
        DiagramViewer::MoveAllShapes(t);
 
135
        if (treeMode) {
 
136
                GetMainWindow()->EnableUndo(False);
 
137
                GetMainWindow()->EnableRedo(False);
 
138
        }
 
139
}
 
140
 
 
141
void TRViewer::Annotate() {
 
142
        treeMode? SetStatus("Aborted: in view mode"):DiagramViewer::Annotate();
 
143
}
 
144
 
 
145
void TRViewer::FindAll(const string *s, bool b1, bool b2, bool b3) {
 
146
        // treeMode? SetStatus("Aborted: in view mode"):
 
147
        DiagramViewer::FindAll(s, b1, b2, b3);
 
148
}
 
149
 
 
150
void TRViewer::FindNext(const string *s, bool b1, bool b2, bool b3) {
 
151
        // treeMode? SetStatus("Aborted: in view mode"):
 
152
        DiagramViewer::FindNext(s, b1, b2, b3);
 
153
}
 
154
 
 
155
void TRViewer::ReplaceNext(const string *s1, const string *s2, 
 
156
                           bool b1, bool b2, bool b3) {
 
157
        treeMode? SetStatus("Aborted: in view mode"):
 
158
                  DiagramViewer::ReplaceNext(s1, s2, b1, b2, b3);
 
159
}
 
160
 
 
161
void TRViewer::ReplaceAll(const string *s1, const string *s2, 
 
162
                        bool b1, bool b2, bool b3) {
 
163
        treeMode? SetStatus("Aborted: in view mode"):
 
164
                  DiagramViewer::ReplaceAll(s1, s2, b1, b2, b3);
 
165
}
 
166
 
 
167
void TRViewer::Draw() {
 
168
        treeMode? DrawTreeMode(): DrawNormalMode();
 
169
}
 
170
 
 
171
void TRViewer::DrawNormalMode() {
 
172
        GetCurView()->Draw();
 
173
}
 
174
 
 
175
void TRViewer::DrawTreeMode() {
 
176
        GetGrafport()->SetLineStyle(LineStyle::SOLID);
 
177
        ((TRDiagram *)GetDiagram())->DrawTreeLayout();
 
178
        GetCurView()->DrawNames();
 
179
}
 
180
 
 
181
void TRViewer::Undraw() {
 
182
        Draw();
 
183
}
 
184
 
 
185
void TRViewer::DrawTreeFork(Shape *root, List<Shape *> *children) {
 
186
        if (children->count() == 0)
 
187
                return;
 
188
        List<Shape *> childrenTop;
 
189
        List<Shape *> childrenBottom;
 
190
        List<Shape *> childrenLeft;
 
191
        List<Shape *> childrenRight;
 
192
        // int rootLeftMost = root->GetLeftMost();
 
193
        // int rootRightMost = root->GetRightMost();
 
194
        Point rootPosition = *root->GetPosition();
 
195
        for (children->first(); !children->done(); children->next()) {
 
196
                Shape *child = children->cur();
 
197
                int leftMost = child->GetLeftMost();
 
198
                int rightMost = child->GetRightMost();
 
199
                Point position = *child->GetPosition();
 
200
                // if (rightMost < rootRightMost - 10)
 
201
                if (rightMost <= rootPosition.x)
 
202
                        childrenLeft.add(child);
 
203
                // else if (leftMost > rootLeftMost)
 
204
                else if (leftMost >= rootPosition.x)
 
205
                        childrenRight.add(child);
 
206
                if (position.y <= rootPosition.y)
 
207
                        childrenTop.add(child);
 
208
                else 
 
209
                        childrenBottom.add(child);
 
210
        }
 
211
        if (childrenLeft.count() == children->count() || 
 
212
            childrenRight.count() == children->count()) {
 
213
                if (childrenTop.count() == children->count()) {
 
214
                        DrawVerticalTreeFork(root, children, UP);
 
215
                }
 
216
                else if (childrenBottom.count() == children->count()) {
 
217
                        DrawVerticalTreeFork(root, children, DOWN);
 
218
                }
 
219
                else {
 
220
                        DrawVerticalTreeFork(root, &childrenTop, UP);
 
221
                        DrawVerticalTreeFork(root, &childrenBottom, DOWN);
 
222
                }
 
223
        }
 
224
        else {
 
225
                DrawHorizontalTreeFork(root, &childrenTop, UP);
 
226
                DrawHorizontalTreeFork(root, &childrenBottom, DOWN);
 
227
        }
 
228
}
 
229
 
 
230
void TRViewer::DrawHorizontalTreeFork(Shape *root, List<Shape *> *children, 
 
231
                DirectionType d) {
 
232
        if (children->count() == 0)
 
233
                return;
 
234
        Point rp = *root->GetPosition();
 
235
        // find extreme coordinates of the children.
 
236
        int minx = 0, maxx = 0, maxy = 0, miny = 0;
 
237
        if (children->first()) {
 
238
                Shape *child = children->cur();
 
239
                Point p = *child->GetPosition();
 
240
                miny = child->GetTopMost();
 
241
                maxy = child->GetBottomMost();
 
242
                minx = p.x;
 
243
                maxx = p.x;
 
244
                do {
 
245
                        child = children->cur();
 
246
                        Point pt = *child->GetPosition();
 
247
                        int top = child->GetTopMost();
 
248
                        int bottom = child->GetBottomMost();
 
249
 
 
250
                        if (pt.x < minx)
 
251
                                minx = pt.x;
 
252
                        if (pt.x > maxx)
 
253
                                maxx = pt.x;
 
254
                                
 
255
                        if (top < miny)
 
256
                                miny = top;
 
257
                        else if (bottom > maxy)
 
258
                                maxy = bottom;
 
259
                } while (children->next());
 
260
        }
 
261
        // find points to attach the root.
 
262
        // and find y coordinate of horizontal bar.
 
263
        Point p1; int hbar_y;
 
264
        if (d == DOWN) {
 
265
                p1 = Point(rp.x, root->GetBottomMost());
 
266
                hbar_y = max(p1.y + 5, miny - 20);
 
267
        }
 
268
        else {
 
269
                p1 = Point(rp.x, root->GetTopMost());
 
270
                hbar_y = min(p1.y - 5, maxy + 20);
 
271
        }
 
272
        // draw line from root to hbar.
 
273
        Point p2 = Point(rp.x, hbar_y);
 
274
        GetGrafport()->DrawLine(p1.x, p1.y, p2.x, p2.y);
 
275
        // draw horizontal bar.
 
276
        Point p5 = Point(min(minx, rp.x), hbar_y);
 
277
        Point p6 = Point(max(maxx, rp.x), hbar_y);
 
278
        GetGrafport()->DrawLine(p5.x, p5.y, p6.x, p6.y);
 
279
        // draw lines from children to the horizontal bar.
 
280
        for (children->first(); !children->done(); children->next()) {
 
281
                Shape *child = children->cur();
 
282
                Point pt = *child->GetPosition();
 
283
                Point p3;
 
284
                if (d == DOWN)
 
285
                        p3 = Point(pt.x, child->GetTopMost());
 
286
                else 
 
287
                        p3 = Point(pt.x, child->GetBottomMost());
 
288
                Point p4 = Point(p3.x, hbar_y);
 
289
                GetGrafport()->DrawLine(p3.x, p3.y, p4.x, p4.y);
 
290
        } 
 
291
}
 
292
 
 
293
void TRViewer::DrawVerticalTreeFork(Shape *root, List<Shape *> *children, 
 
294
                DirectionType d) {
 
295
        Point p1;
 
296
        if (d == DOWN)
 
297
                p1 = Point(root->GetPosition()->x, root->GetBottomMost());
 
298
        else
 
299
                p1 = Point(root->GetPosition()->x, root->GetTopMost());
 
300
        if (children->first()) {
 
301
                int py = children->cur()->GetPosition()->y;
 
302
                do {
 
303
                        Shape *child = children->cur();
 
304
                        int cury = child->GetPosition()->y;
 
305
                        Point p2 = Point(p1.x, cury);
 
306
                        Point p3 = p2;
 
307
                        if (child->GetPosition()->x < p1.x)
 
308
                                p3.x = child->GetRightMost() + 4;
 
309
                        else
 
310
                                p3.x = child->GetLeftMost() - 4;
 
311
                        GetGrafport()->DrawLine(p2.x, p2.y, p3.x, p3.y);
 
312
                        if (d == DOWN)
 
313
                                py = max(cury, py);
 
314
                        else
 
315
                                py = min(cury, py);
 
316
                } while (children->next());
 
317
                Point p4 = Point(p1.x, py);
 
318
                GetGrafport()->DrawLine(p1.x, p1.y, p4.x, p4.y);
 
319
        }
 
320
}