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

« back to all changes in this revision

Viewing changes to src/dg/textshape.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 "grafport.h"
 
23
#include "shapeview.h"
 
24
#include "diagram.h"
 
25
#include "diagramviewer.h"
 
26
#include "textshape.h"
 
27
#include "nodeshape.h"
 
28
#include "subject.h"
 
29
 
 
30
TextShape::TextShape(ShapeView *v, Grafport *g, Shape *shape): Shape(v,g) {
 
31
        SetNrHandles(0);
 
32
        description = "Name";
 
33
        parent = shape;
 
34
        text = "";
 
35
        alignment = v->GetViewer()->GetDefaultTextAlignment();
 
36
        font = v->GetViewer()->GetDefaultFont();
 
37
        SetColor(v->GetViewer()->GetDefaultTextColor());
 
38
        stringWidth = 0;
 
39
        stringHeight = 0;
 
40
        sequence = 1;
 
41
        oneLine = False;
 
42
        wellPositioned = True;
 
43
        underlined = v->GetViewer()->GetDefaultFont()->IsUnderlined();
 
44
        directed = False;
 
45
        readDirection = ReadDirection::NONE;
 
46
}
 
47
 
 
48
TextShape::TextShape(const TextShape &t): Shape(t) {
 
49
        SetNrHandles(0);
 
50
        text = t.text;
 
51
        description = t.description;
 
52
        oneLine = t.oneLine;
 
53
        font = t.font;
 
54
        alignment = t.alignment;
 
55
        stringWidth = t.stringWidth;
 
56
        stringHeight = t.stringHeight;
 
57
        sequence = t.sequence;
 
58
        wellPositioned = True;
 
59
        underlined = t.underlined;
 
60
        directed = t.directed;
 
61
        readDirection = t.readDirection;
 
62
        parent = 0;
 
63
}
 
64
 
 
65
void TextShape::SetString(const string *s) {
 
66
        text = *s;
 
67
        if (oneLine)
 
68
                text.replace('\r', ' ');
 
69
        int w, h;
 
70
        font->Box(&text, w, h);
 
71
        stringWidth = w;
 
72
        stringHeight = h;
 
73
        SetSize(w+2, h);
 
74
}
 
75
 
 
76
void TextShape::SetString(const char *ch) {
 
77
        text = ch;
 
78
        if (oneLine)
 
79
                text.replace('\r', ' ');
 
80
        int w, h;
 
81
        font->Box(&text, w, h);
 
82
        stringWidth = w;
 
83
        stringHeight = h;
 
84
        SetSize(w+2, h);
 
85
}
 
86
 
 
87
void TextShape::TextBox(int &wd, int &ht) {
 
88
        wd = stringWidth;
 
89
        ht = stringHeight;
 
90
}
 
91
 
 
92
void TextShape::SetPosition(const Point *pos, bool) {
 
93
        Shape::SetPosition(pos, False);
 
94
}
 
95
 
 
96
void TextShape::DrawOutLine(const Point *c) {
 
97
        int x = c->x - GetWidth()  / 2;
 
98
        int y = c->y - GetHeight() / 2;
 
99
        SetOutlineAttributes();
 
100
        GetGrafport()->DrawRectangle(x, y, GetWidth(), GetHeight());
 
101
}
 
102
 
 
103
void TextShape::UpdateAlignment(TextAlign::Type a) {
 
104
        if (a != alignment) {
 
105
                Undraw();
 
106
                alignment = a;
 
107
                Draw();
 
108
        }
 
109
}
 
110
 
 
111
void TextShape::SetFont(XFont *ft) {
 
112
        font = ft;
 
113
        font->Box(&text, stringWidth, stringHeight);
 
114
        SetSize(stringWidth+2, stringHeight);
 
115
}
 
116
 
 
117
void TextShape::UpdateFont(XFont *ft) {
 
118
        if (ft != font) {
 
119
                Undraw();
 
120
                SetFont(ft);
 
121
                Draw();
 
122
        }
 
123
}
 
124
 
 
125
void TextShape::Draw() {
 
126
        bool setempty = False;
 
127
        if (underlined && text.length() == 0) {
 
128
                // show little dash with empty underlined text. 
 
129
                text = " ";
 
130
                setempty = True;
 
131
        }
 
132
        if (text.length() > 0) {
 
133
                SetDrawAttributes();
 
134
                GetGrafport()->SetFont(font);
 
135
                Point pos = *GetPosition();
 
136
                if (alignment == TextAlign::CENTER)
 
137
                        GetGrafport()->DrawStringsCentered(
 
138
                                        pos.x, pos.y, &text, underlined);
 
139
                else if (alignment == TextAlign::LEFT) {
 
140
                        double tlx = pos.x - stringWidth * 0.5;
 
141
                        double tly = pos.y - stringHeight * 0.5;
 
142
                        GetGrafport()->DrawStringsLeft(tlx, tly, &text, 
 
143
                                        underlined);
 
144
                }
 
145
                else {  // RIGHT
 
146
                        double tlx = pos.x + stringWidth * 0.5;
 
147
                        double tly = pos.y - stringHeight * 0.5;
 
148
                        GetGrafport()->DrawStringsRight(tlx, tly, &text, 
 
149
                                        underlined);
 
150
                }
 
151
        }
 
152
        if (setempty) {
 
153
                text = "";
 
154
        }
 
155
}
 
156
 
 
157
void TextShape::DrawSelect() {
 
158
        if (text.length() > 0 && IsSelected()) {
 
159
                Point pos = *GetPosition();
 
160
                int x = pos.x - GetWidth() / 2;
 
161
                int y = pos.y - GetHeight() / 2;
 
162
                GetGrafport()->SetLineWidth(1);
 
163
                GetGrafport()->SetLineStyle(LineStyle::DOTTED);
 
164
                GetGrafport()->DrawRectangle(x, y, GetWidth(), GetHeight());
 
165
        }
 
166
}
 
167
 
 
168
bool TextShape::HasString(const string *s, bool sens, bool substr) const {
 
169
        string t1 = *s;
 
170
        string t2 = text;
 
171
        if (!sens) {
 
172
                // make them the same case.
 
173
                t1.downcase();
 
174
                t2.downcase();
 
175
        }
 
176
        if (substr)
 
177
                return (t2.contains(t1));
 
178
        else
 
179
                return (t1 == t2);
 
180
}
 
181
 
 
182
int TextShape::ReplaceString(const string *s1, const string *s2, 
 
183
                bool sens, bool substr) {
 
184
        string pat = *s1;
 
185
        string repl = *s2;
 
186
        string copy = text;
 
187
        if (!substr && pat != text)
 
188
                return 0;
 
189
        if (pat == repl)
 
190
                return 0;
 
191
        int n = copy.replace(pat, repl, sens);
 
192
        if (copy != text)
 
193
                GetViewer()->GetDiagram()->SetText(this, &copy);
 
194
        return n;
 
195
}
 
196
 
 
197
void TextShape::UpdateUnderlined(bool b) {
 
198
        if (underlined != b) {
 
199
                Undraw();
 
200
                underlined = b;
 
201
                Draw();
 
202
        }
 
203
}
 
204
 
 
205
bool TextShape::HasString(const string *s1, 
 
206
                bool sens, bool substr, List<TextShape *> *list) {
 
207
        bool b= HasString(s1, sens, substr);
 
208
        if (b) {
 
209
                TextShape *me = this;
 
210
                list->add(me);
 
211
        }
 
212
        return b;
 
213
}
 
214
 
 
215
bool TextShape::InTextArea(int x, int y) {
 
216
        return (ContainsPt(x, y));
 
217
}
 
218
 
 
219
bool TextShape::ContainsPt(int x, int y) {
 
220
        if (text.length() == 0)
 
221
                return False;
 
222
        Rectangle rect(GetLeftMost(), GetTopMost(), GetWidth(), GetHeight());
 
223
        bool iR1 = rect.Inside(x, y);
 
224
        bool iR2 = rect.Inside(x, y);   // also init
 
225
        
 
226
        if (directed) {         // directed textshape
 
227
                Rectangle rect2(GetLeftMost(), GetTopMost(), 
 
228
                                                GetWidth() + 16,  GetHeight());
 
229
                iR2 = rect2.Inside(x, y);
 
230
        }
 
231
        return (iR1 || iR2 ); 
 
232
}
 
233
 
 
234
void TextShape::UpdateReadDirection(ReadDirection::Type d) {
 
235
        if (readDirection != d) {
 
236
                Undraw();
 
237
                readDirection = d;
 
238
                Draw();
 
239
        }
 
240
}