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

« back to all changes in this revision

Viewing changes to src/sd/dv/ssddoubleclassbox.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 1999, Vrije Universiteit Amsterdam and University of Twente.
 
5
// Author: Frank Dehne (frank@cs.vu.nl).
 
6
// Author: Henk van de Zandschulp (henkz@cs.utwente.nl).
 
7
//
 
8
// TCM 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.
 
12
//
 
13
// TCM 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.
 
17
//
 
18
// You should have received a copy of the GNU General Public License
 
19
// along with TCM; if not, write to the Free Software Foundation, Inc.,
 
20
// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
//-----------------------------------------------------------------------------
 
22
#include "lstring.h"
 
23
#include "ssdsingleclassbox.h"
 
24
#include "ssddoubleclassbox.h"
 
25
#include "ssdtripleclassbox.h"
 
26
#include "ssdclassnode.h"
 
27
#include "diagramviewer.h"
 
28
#include "inputfile.h"
 
29
#include "outputfile.h"
 
30
#include "grafport.h"
 
31
 
 
32
SSDDoubleClassBox::SSDDoubleClassBox(ShapeView *v, Grafport *g, int x, int y, 
 
33
                int w, int h): DoubleClassBox(v, g, x, y, w, h) {
 
34
        stereotypeLabel = new TextShape(v, g, this);
 
35
        stereotypeLabel->SetOneLine(True);
 
36
        stereotypeLabel->SetDescription("Stereotype");
 
37
        string my_stereo = "<<SSDDoubleClassBox>>";
 
38
        stereotypeLabel->SetString(&my_stereo);
 
39
        propertiesLabel = new TextShape(v, g, this);
 
40
        propertiesLabel->SetOneLine(False);
 
41
        propertiesLabel->SetDescription("Properties");
 
42
        string my_props = "{ SSDDoubleClassBox }";
 
43
        propertiesLabel->SetString(&my_props);
 
44
        showStereotype = False;
 
45
        showProperties = False;
 
46
        SetSize(w, RequiredHeight());
 
47
}
 
48
 
 
49
SSDDoubleClassBox::~SSDDoubleClassBox() {
 
50
        delete stereotypeLabel;
 
51
        delete propertiesLabel;
 
52
}
 
53
 
 
54
SSDDoubleClassBox::SSDDoubleClassBox(const SSDDoubleClassBox &s): DoubleClassBox(s) {
 
55
        showStereotype = s.showStereotype;
 
56
        showProperties = s.showProperties;
 
57
        stereotypeLabel = new TextShape(*s.stereotypeLabel);
 
58
        propertiesLabel = new TextShape(*s.propertiesLabel);
 
59
        stereotypeLabel->SetParent(this);
 
60
        propertiesLabel->SetParent(this);
 
61
        SetSize(s.GetWidth(), s.GetHeight());
 
62
}
 
63
 
 
64
SSDDoubleClassBox::SSDDoubleClassBox(const SSDSingleClassBox &s): DoubleClassBox(s) {
 
65
        showStereotype = s.IsShowStereotype();
 
66
        showProperties = s.IsShowProperties();
 
67
        stereotypeLabel = new TextShape(*s.GetStereotypeLabel());
 
68
        propertiesLabel = new TextShape(*s.GetPropertiesLabel());
 
69
        stereotypeLabel->SetParent(this);
 
70
        propertiesLabel->SetParent(this);
 
71
        SetTextShape();
 
72
        AdjustSizes(GetName()->GetString());
 
73
}
 
74
 
 
75
SSDDoubleClassBox::SSDDoubleClassBox(const SSDTripleClassBox &s): DoubleClassBox(s) {
 
76
        showStereotype = s.IsShowStereotype();
 
77
        showProperties = s.IsShowProperties();
 
78
        stereotypeLabel = new TextShape(*s.GetStereotypeLabel());
 
79
        propertiesLabel = new TextShape(*s.GetPropertiesLabel());
 
80
        stereotypeLabel->SetParent(this);
 
81
        propertiesLabel->SetParent(this);
 
82
        SetTextShape();
 
83
        AdjustSizes(GetName()->GetString());
 
84
}
 
85
 
 
86
void SSDDoubleClassBox::SetGrafport(Grafport *g) {
 
87
        DoubleClassBox::SetGrafport(g);
 
88
        stereotypeLabel->SetGrafport(g);
 
89
        propertiesLabel->SetGrafport(g);
 
90
}
 
91
 
 
92
void SSDDoubleClassBox::SetView(ShapeView *v) {
 
93
        DoubleClassBox::SetView(v);
 
94
        stereotypeLabel->SetView(v);
 
95
        propertiesLabel->SetView(v);
 
96
}
 
97
 
 
98
bool SSDDoubleClassBox::HasTextShape(TextShape *t) const {
 
99
        if (DoubleClassBox::HasTextShape(t))
 
100
                return True;
 
101
        if (GetStereotypeLabel() == t)
 
102
                return True;
 
103
        if (GetPropertiesLabel() == t)
 
104
                return True;
 
105
        return False;
 
106
}
 
107
 
 
108
bool SSDDoubleClassBox::HasString(const string *s, bool sens, bool sub) const {
 
109
        if (DoubleClassBox::HasString(s, sens, sub))
 
110
                return True;
 
111
        if (showStereotype && stereotypeLabel->HasString(s, sens, sub))
 
112
                return True;
 
113
        if (showProperties && propertiesLabel->HasString(s, sens, sub))
 
114
                return True;
 
115
        return False;
 
116
}
 
117
 
 
118
bool SSDDoubleClassBox::HasString(const string *s, bool sens, bool sub,
 
119
                        List<TextShape *> *list) {
 
120
        bool b = DoubleClassBox::HasString(s, sens, sub, list);
 
121
        if (showStereotype && stereotypeLabel->HasString(s, sens, sub, list))
 
122
                b = True;
 
123
        if (showProperties && propertiesLabel->HasString(s, sens, sub, list))
 
124
                b = True;
 
125
        return b;
 
126
}
 
127
 
 
128
void SSDDoubleClassBox::SetTextShape() {
 
129
        DoubleClassBox::SetTextShape();
 
130
        if (!check(GetSubject() && 
 
131
             GetSubject()->GetClassType()==Code::SSD_CLASS_NODE))
 
132
                return;
 
133
        GetStereotypeLabel()->SetString(
 
134
                        ((SSDClassNode *)GetSubject())->GetStereotype());
 
135
        GetStereotypeLabel()->SetParent(this);
 
136
        GetPropertiesLabel()->SetString(
 
137
                        ((SSDClassNode *)GetSubject())->GetProperties());
 
138
        GetPropertiesLabel()->SetParent(this);
 
139
//      CalcPositionLabels();
 
140
}
 
141
 
 
142
void SSDDoubleClassBox::SetFont(XFont *ft) {
 
143
        DoubleClassBox::SetFont(ft);
 
144
        stereotypeLabel->SetFont(ft);
 
145
        propertiesLabel->SetFont(ft);
 
146
//      CalcPositionLabels();
 
147
}
 
148
 
 
149
void SSDDoubleClassBox::SetTextColor(const string *c) {
 
150
        DoubleClassBox::SetTextColor(c);
 
151
        stereotypeLabel->SetColor(c);
 
152
        propertiesLabel->SetColor(c);
 
153
}
 
154
 
 
155
void SSDDoubleClassBox::UpdateShowStereotype(bool b) {
 
156
        if (b == showStereotype)
 
157
                return;
 
158
        if (IsVisible())
 
159
                Undraw();
 
160
        showStereotype = b;
 
161
//      CalcPositionLabels();
 
162
        if (IsVisible())
 
163
                Draw();
 
164
}
 
165
 
 
166
void SSDDoubleClassBox::UpdateShowProperties(bool b) {
 
167
        if (b == showProperties)
 
168
                return;
 
169
        if (IsVisible())
 
170
                Undraw();
 
171
        showProperties = b;
 
172
//      CalcPositionLabels();
 
173
        if (IsVisible())
 
174
                Draw();
 
175
}
 
176
 
 
177
void SSDDoubleClassBox::DrawTextShapes() {
 
178
        string oldBackground;
 
179
        Grafport *g = GetGrafport();
 
180
        if (GetFillStyle() != FillStyle::UNFILLED) {
 
181
                oldBackground = *g->GetBackgroundColor();
 
182
                g->SetBackgroundColor(GetFillColor()->getstr());
 
183
        }
 
184
        NodeShape::DrawTextShapes();
 
185
        int textHeight = GetName()->GetFontHeight();
 
186
        int addLines = (showStereotype ? 1 : 0);
 
187
        // min_firstboxlines : 2 when no <<stereotype>> / {props} shown
 
188
        int min_firstboxlines = ((showStereotype || showProperties) ? 1 : 2);
 
189
        int labelLines = max(GetName()->GetString()->lines(),min_firstboxlines);
 
190
        // calculate first_compartmentHeight
 
191
        if (showProperties)
 
192
                addLines += GetPropertiesLabelString()->lines();
 
193
        int snpHeight = (addLines+labelLines) * textHeight;
 
194
        if (snpHeight < GetHeight()) {
 
195
                g->SetLineStyle(GetSeparatorLineStyle());
 
196
                g->DrawLine(GetLeftMost()+1, GetTopMost()+snpHeight+1,
 
197
                        GetRightMost()-1,
 
198
                        GetTopMost()+snpHeight+1);
 
199
                g->SetLineStyle(GetLineStyle());
 
200
        }
 
201
        int i = snpHeight;
 
202
        for (GetLabels()->first(); !GetLabels()->done(); GetLabels()->next()) {
 
203
                i += textHeight;
 
204
                if (i < GetHeight())
 
205
                        GetLabels()->cur()->Draw();
 
206
        }
 
207
 
 
208
        if (GetFillStyle() != FillStyle::UNFILLED)
 
209
                g->SetBackgroundColor(oldBackground.getstr());
 
210
        if (showStereotype)
 
211
                stereotypeLabel->Draw();
 
212
        if (showProperties)
 
213
                propertiesLabel->Draw();
 
214
}
 
215
 
 
216
void SSDDoubleClassBox::AdjustSize() {
 
217
        int twd = RequiredWidth();
 
218
        int ht = RequiredHeight();
 
219
        if (twd > GetWidth() || ht != GetHeight()) {
 
220
                int wd = max((twd+2*TEXTMARGIN), GetWidth());
 
221
                UpdateSize(wd, ht);
 
222
                GetViewer()->ShapePositionUpdate(this);
 
223
        }
 
224
//      else
 
225
//              CalcPositionLabels();
 
226
}
 
227
 
 
228
void SSDDoubleClassBox::AdjustSizes(const string *) {
 
229
        if (!GetViewer()->IsAutoResize()) {
 
230
//              CalcPositionLabels();
 
231
                return;
 
232
        }
 
233
        int twd = RequiredWidth();
 
234
        int ht = RequiredHeight();
 
235
        if (twd > GetWidth() || GetHeight() != ht) {
 
236
                int wd = max((twd+2*TEXTMARGIN), GetWidth());
 
237
                SetSize(wd, ht);
 
238
                GetViewer()->ShapePositionUpdate(this);
 
239
        }
 
240
//      else
 
241
//              CalcPositionLabels();
 
242
}
 
243
 
 
244
void SSDDoubleClassBox::CalcPositionLabels() {
 
245
        DoubleClassBox::CalcPositionLabels();
 
246
 
 
247
        Point pt = *GetPosition();
 
248
        // calculate labelnameHeight first
 
249
        int textHeight = GetName()->GetFontHeight();
 
250
        int addLines = (showStereotype ? 1 : 0);
 
251
        // min_firstboxlines : 2 when no <<stereotype>> / {props} shown
 
252
        int min_firstboxlines = ((showStereotype || showProperties) ? 1 : 2);
 
253
        int labelLines = max(GetName()->GetString()->lines(),min_firstboxlines);
 
254
        int nameHeight = labelLines * textHeight;
 
255
        // calculate first_compartmentHeight
 
256
        if (showProperties)
 
257
                addLines += GetPropertiesLabelString()->lines();
 
258
        int snpHeight = (addLines+labelLines) * textHeight;
 
259
        if (showStereotype)
 
260
                pt.y = GetTopMost() + textHeight + nameHeight/2;
 
261
        else
 
262
                pt.y = GetTopMost() + nameHeight/2;
 
263
        GetName()->SetPosition(&pt);
 
264
 
 
265
        pt.y = GetTopMost() + snpHeight;
 
266
        pt.y += (3*textHeight)/4;
 
267
        for (GetLabels()->first(); !GetLabels()->done(); GetLabels()->next()) {
 
268
                TextShape *t = GetLabels()->cur();
 
269
                // position and left adjust textshape
 
270
                int w = t->GetStringWidth();
 
271
                pt.x = GetLeftMost() + w/2 + TEXTMARGIN;
 
272
                t->SetPosition(&pt);
 
273
                pt.y += textHeight;
 
274
        }
 
275
 
 
276
        // set index label position.
 
277
        pt.x = GetName()->GetPosition()->x + GetWidth()*5/12;
 
278
        pt.y = GetName()->GetPosition()->y - nameHeight/4;
 
279
        GetIndexLabel()->SetPosition(&pt);
 
280
 
 
281
        if (showStereotype)
 
282
                CalcPositionStereotypeLabel();
 
283
        if (showProperties)
 
284
                CalcPositionPropertiesLabel();
 
285
}
 
286
 
 
287
 
 
288
void SSDDoubleClassBox::CalcPositionStereotypeLabel() {
 
289
        int textHeight = GetName()->GetFontHeight();
 
290
        Point pt;
 
291
        pt.x = GetPosition()->x;
 
292
        pt.y = GetTopMost() + textHeight/2;
 
293
        stereotypeLabel->SetPosition(&pt);
 
294
}
 
295
 
 
296
 
 
297
void SSDDoubleClassBox::CalcPositionPropertiesLabel() {
 
298
        // calculate labelnameHeight first
 
299
        int textHeight = GetName()->GetFontHeight();
 
300
        int addLines = (showStereotype ? 1 : 0);
 
301
        // min_firstboxlines : 2 when no <<stereotype>> / {props} shown
 
302
        int min_firstboxlines = ((showStereotype || showProperties) ? 1 : 2);
 
303
        int labelLines = max(GetName()->GetString()->lines(),min_firstboxlines);
 
304
        int snHeight = (addLines+labelLines) * textHeight;
 
305
        int propHeight = (showProperties ? 
 
306
                        GetPropertiesLabelString()->lines()*textHeight : 0);
 
307
        Point pt;
 
308
        pt.x = GetPosition()->x;
 
309
        pt.y = GetTopMost() + snHeight + propHeight/2;
 
310
        propertiesLabel->SetPosition(&pt);
 
311
}
 
312
 
 
313
 
 
314
int SSDDoubleClassBox::RequiredHeight() {
 
315
        int textHeight = GetName()->GetFontHeight();
 
316
        int addLines = (showStereotype ? 1 : 0);
 
317
        // min_firstboxlines : 2 when no <<stereotype>> / {props} shown
 
318
        int min_firstboxlines = ((showStereotype || showProperties) ? 1 : 2);
 
319
        int labelLines = max(GetName()->GetString()->lines(),min_firstboxlines);
 
320
        // calculate first_compartmentHeight
 
321
        if (showProperties)
 
322
                addLines += GetPropertiesLabelString()->lines();
 
323
        int snpHeight = (addLines+labelLines) * textHeight;
 
324
        int ht = (snpHeight + textHeight + GetLabels()->count()*textHeight);
 
325
//      return max(ht, GetInitialHeight());
 
326
        return ht;
 
327
}
 
328
 
 
329
int SSDDoubleClassBox::RequiredWidth() {
 
330
        int twidth = GetName()->GetWidth();
 
331
        if (showStereotype)
 
332
                twidth = max(twidth, GetStereotypeLabel()->GetWidth());
 
333
        if (showProperties)
 
334
                twidth = max(twidth, GetPropertiesLabel()->GetWidth());
 
335
        // Labels
 
336
        for (GetLabels()->first(); !GetLabels()->done(); GetLabels()->next()) {
 
337
                int wd = GetLabels()->cur()->GetStringWidth();
 
338
                twidth = max(twidth, wd);
 
339
        }
 
340
        return twidth;
 
341
}
 
342
 
 
343
 
 
344
TextShape *SSDDoubleClassBox::ChooseTextShape(int x, int y) {
 
345
        // in first part (of the class name) ?
 
346
        // calculate #labelnameLines first
 
347
        int textHeight = GetName()->GetFontHeight();
 
348
        int addLines = (showStereotype ? 1 : 0);
 
349
        // min_firstboxlines : 2 when no <<stereotype>> / {props} shown
 
350
        int min_firstboxlines = ((showStereotype || showProperties) ? 1 : 2);
 
351
        int labelLines = max(GetName()->GetString()->lines(),min_firstboxlines);
 
352
        int snHeight = (addLines+labelLines) * textHeight;
 
353
        // calculate first_compartmentHeight
 
354
        if (showProperties)
 
355
                addLines += GetPropertiesLabelString()->lines();
 
356
        int snpHeight = (addLines+labelLines) * textHeight;
 
357
        int pos_y = GetTopMost() + snpHeight;
 
358
        if (y <= pos_y) {
 
359
                if (showStereotype) {
 
360
                        if (y <= GetTopMost() + textHeight)
 
361
                                return GetStereotypeLabel();
 
362
                }
 
363
                if (GetViewer()->IsShowIndexes() && !IsFixedIndexLabel()) {
 
364
                        if (y <= GetName()->GetPosition()->y &&
 
365
                            x >= GetName()->GetPosition()->x + GetWidth()/3)
 
366
                                return GetIndexLabel();
 
367
                }
 
368
                if (y > GetTopMost() + snHeight)
 
369
                        return GetPropertiesLabel();
 
370
                if (!IsFixedName())
 
371
                        return GetName();
 
372
        }
 
373
        // in labels part ??
 
374
        pos_y += textHeight/2;
 
375
        for (GetLabels()->first(); !GetLabels()->done(); GetLabels()->next()) {
 
376
                pos_y += textHeight;
 
377
                if (pos_y >= GetTopMost() + GetHeight())
 
378
                        return 0;
 
379
                else if (y <= pos_y)
 
380
                        return GetLabels()->cur();
 
381
        }
 
382
        if (GetViewer()->IsEditing())
 
383
                return 0;
 
384
        // somewhere down the list; create a new label.
 
385
        TextShape *t = new TextShape(GetView(), GetGrafport(), this);
 
386
        t->SetAlignment(TextAlign::LEFT);
 
387
        t->SetSequence(GetLabels()->count());
 
388
        t->SetDescription(&labelDescription);
 
389
        t->SetFont(GetName()->GetFont());
 
390
        t->SetColor(GetName()->GetColor());
 
391
        Undraw();
 
392
        GetLabels()->add(t);
 
393
//      CalcPositionLabels();
 
394
        // move text shape to a better position.
 
395
        Point pt = *t->GetPosition();
 
396
        pt.x += GetWidth()/2 - TEXTMARGIN;
 
397
        t->SetPosition(&pt);
 
398
        Draw();
 
399
        return t;
 
400
}
 
401
 
 
402
void SSDDoubleClassBox::UpdateStereotypeLabelString(const string *s) {
 
403
        if (IsVisible())
 
404
                Undraw();
 
405
        SetStereotypeLabelString(s);
 
406
        if (IsVisible())
 
407
                Draw();
 
408
}
 
409
 
 
410
void SSDDoubleClassBox::UpdatePropertiesLabelString(const string *s) {
 
411
        if (IsVisible())
 
412
                Undraw();
 
413
        SetPropertiesLabelString(s);
 
414
        if (IsVisible())
 
415
                Draw();
 
416
}
 
417
 
 
418
bool SSDDoubleClassBox::ReadMembers(InputFile *ifile, double format) {
 
419
        if (!DoubleClassBox::ReadMembers(ifile, format))
 
420
                return False;
 
421
        if (format >= 1.28) {
 
422
                string val;
 
423
                if (!ifile->ReadAttribute("ShowStereotype", &val))
 
424
                        return False;
 
425
                showStereotype = (val == "True");
 
426
                if (!ifile->ReadAttribute("ShowProperties", &val))
 
427
                        return False;
 
428
                showProperties = (val == "True");
 
429
        }
 
430
        else {
 
431
                showStereotype = False;
 
432
                showProperties = False;
 
433
        }
 
434
        return True;
 
435
}
 
436
 
 
437
void SSDDoubleClassBox::WriteMembers(OutputFile *ofile) {
 
438
        DoubleClassBox::WriteMembers(ofile);
 
439
        (*ofile) << "\t{ ShowStereotype " << (showStereotype?"True":"False") << " }\n";
 
440
        (*ofile) << "\t{ ShowProperties " << (showProperties?"True":"False") << " }\n";
 
441
}