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

« back to all changes in this revision

Viewing changes to src/tb/simplelabel.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 1996, 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 "rectangle.h"
 
23
#include "grafport.h"
 
24
#include "simplelabel.h"
 
25
 
 
26
const int SimpleLabel::MIN_TEXT_WIDTH = 20;
 
27
const int SimpleLabel::MIN_TEXT_HEIGHT = 20;
 
28
 
 
29
SimpleLabel::SimpleLabel(Grafport *g, XFont *ft) {
 
30
        text = "";
 
31
        width = 0;
 
32
        height = 0;
 
33
        grafport = g;
 
34
        font = ft;
 
35
}
 
36
 
 
37
SimpleLabel::SimpleLabel(const SimpleLabel &t) {
 
38
        width = t.width;
 
39
        height = t.height;
 
40
        position = t.position;
 
41
        topLeft = t.topLeft;
 
42
        text = t.text;
 
43
        grafport = t.grafport;
 
44
        font = t.font;
 
45
}
 
46
 
 
47
SimpleLabel::~SimpleLabel() { }
 
48
 
 
49
int operator==(const SimpleLabel &t1, const SimpleLabel &t2) {
 
50
        return strcmp((t1.text).getstr(),(t2.text).getstr());
 
51
}
 
52
 
 
53
void SimpleLabel::Undraw() {
 
54
        Draw();
 
55
}
 
56
 
 
57
void SimpleLabel::DrawOutLine(const Point *center) {
 
58
        int x = center->x - width / 2;
 
59
        int y = center->y - height / 2;
 
60
        int wd = max(width, MIN_TEXT_WIDTH);
 
61
        int ht = max(height, MIN_TEXT_HEIGHT);
 
62
        GetGrafport()->SetLineWidth(1);
 
63
        GetGrafport()->SetLineStyle(LineStyle::DOTTED);
 
64
        grafport->DrawRectangle(x, y, wd, ht);
 
65
}
 
66
 
 
67
 
 
68
void SimpleLabel::UpdatePosition(const Point *pt) {
 
69
        Undraw();
 
70
        SetPosition(pt);
 
71
        Draw();
 
72
}
 
73
 
 
74
void SimpleLabel::UpdateText(const string *s) {
 
75
        Undraw();
 
76
        SetText(s);
 
77
        Draw();
 
78
}
 
79
 
 
80
void SimpleLabel::UpdateFont(XFont *ft) {
 
81
        Undraw();
 
82
        SetFont(ft);
 
83
        Draw();
 
84
}
 
85
 
 
86
void SimpleLabel::SetPosition(const Point *pos) {
 
87
        position.x = pos->x;
 
88
        position.y = pos->y;
 
89
        topLeft.x = position.x - width / 2;
 
90
        topLeft.y = position.y - height / 2;
 
91
        PositiveCoord();
 
92
}
 
93
 
 
94
void SimpleLabel::SetFont(XFont *ft) {
 
95
        font = ft;
 
96
        CalcSize();
 
97
}
 
98
 
 
99
void SimpleLabel::SetText(const string *s) {
 
100
        text = *s;
 
101
        CalcSize();
 
102
}
 
103
 
 
104
void SimpleLabel::CalcSize() {
 
105
        int w, h;
 
106
        font->Box(&text, w, h);
 
107
        SetSize(w, h);
 
108
}
 
109
 
 
110
bool SimpleLabel::HitText(int x, int y) {
 
111
        if (text.length() == 0)
 
112
                return False;
 
113
        else {
 
114
                int dist = 5;
 
115
                int tx = topLeft.x - dist;
 
116
                int ty = topLeft.y - dist;
 
117
                int w = max(width, MIN_TEXT_WIDTH) + 2*dist;
 
118
                int h = max(height, MIN_TEXT_HEIGHT) + 2*dist;
 
119
                Rectangle rect(tx, ty, w, h);
 
120
                return rect.Inside(x, y);
 
121
        }
 
122
}
 
123
 
 
124
void SimpleLabel::SetSize(int w, int h) {
 
125
        width = w;
 
126
        height = h;
 
127
        PositiveCoord();
 
128
        topLeft.x = position.x - width/2;
 
129
        topLeft.y = position.y - height/2;
 
130
}
 
131
 
 
132
void SimpleLabel::PositiveCoord() {
 
133
        int delta;
 
134
        delta = topLeft.x;
 
135
        if (delta < 0) {
 
136
                Point pt = position;
 
137
                pt.x += -delta;
 
138
                SetPosition(&pt);
 
139
        }
 
140
        delta = topLeft.y;
 
141
        if (delta < 0) {
 
142
                Point pt = position;
 
143
                pt.y += -delta;
 
144
                SetPosition(&pt);
 
145
        }
 
146
}