summaryrefslogtreecommitdiffstats
path: root/tksao/util/attribute.C
blob: 22ae98ddd161129a4ba9dbd02c04791335c798ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include "attribute.h"
#include "util.h"
#include "widget.h"

Attribute::Attribute(Widget* p) : parent(p)
{
  style_ = SOLID;
  width_ = 1;

  font_ = 2; // helvetica normal roman
  size_ = 10;
  tkfont_ = NULL;
  psfont_ = NULL;
  initFonts();

  colour_ = 0xffffff; // white
  colorName_ = dupstr("white");
  color_ = parent->getColor("white");
}

Attribute::~Attribute()
{
  if (tkfont_)
    Tk_FreeFont(tkfont_);
  if (psfont_)
    Tk_FreeFont(psfont_);

  if (colorName_)
    delete [] colorName_;
}

void Attribute::setStyle(double v)
{
  switch ((int)v) {
  case SOLID:
  case DASH:
    break;
  default:
    return;
  }

  style_ = (Style)((int)v);
}

void Attribute::setWidth(double v)
{
  if (v>0)
    width_ = v;
  else
    width_ = 1;
}

void Attribute::setSize(double v)
{
  if (v >= 1) {
#ifdef MAC_OSX_TK
    size_ = int(v*parent->getDisplayRatio());
#else
    size_ = (int)v;
#endif
    initFonts();
  }
}

void Attribute::setFont(double v)
{
  font_ = (int)v;
  initFonts();
}

void Attribute::initFonts()
{
  if (tkfont_)
    Tk_FreeFont(tkfont_);
  tkfont_ = NULL;
  if (psfont_)
    Tk_FreeFont(psfont_);
  psfont_ = NULL;

  WidgetOptions* opts = parent->options;

  ostringstream fstr;
  ostringstream pstr;
  switch (font_) {
  case 0:
  case 2:
  case 3:
    fstr << '{' << opts->helvetica << '}' << ' ' << size_ << " normal roman" << ends;
    pstr << "helvetica " << size_ << " normal roman" << ends;
    break;
  case 1:
    fstr << '{' << opts->times << '}' << ' ' << size_ << " normal roman" << ends;
    pstr << "times " << size_ << " normal roman" << ends;
    break;
  case 4:
    fstr << '{' << opts->courier << '}' << ' ' << size_ << " normal roman" << ends;
    pstr << "courier " << size_ << " normal roman" << ends;
    break;

  case 10:
  case 12:
  case 13:
    fstr << '{' << opts->helvetica << '}' << ' ' << size_ << " bold roman" << ends;
    pstr << "helvetica " << size_ << " bold roman" << ends;
    break;
  case 11:
    fstr << '{' << opts->times << '}' << ' ' << size_ << " bold roman" << ends;
    pstr << "times " << size_ << " bold roman" << ends;
    break;
  case 14:
    fstr << '{' << opts->courier << '}' << ' ' << size_ << " bold roman" << ends;
    pstr << "courier " << size_ << " bold roman" << ends;
    break;

  case 20:
  case 22:
  case 23:
    fstr << '{' << opts->helvetica << '}' << ' ' << size_ << " normal italic" << ends;
    pstr << "helvetica " << size_ << " normal italic" << ends;
    break;
  case 21:
    fstr << '{' << opts->times << '}' << ' ' << size_ << " normal italic" << ends;
    pstr << "times " << size_ << " normal italic" << ends;
    break;
  case 24:
    fstr << '{' << opts->courier << '}' << ' ' << size_ << " normal italic" << ends;
    pstr << "courier " << size_ << " normal italic" << ends;
    break;

  case 30:
  case 32:
  case 33:
    fstr << '{' << opts->helvetica << '}' << ' ' << size_ << " bold italic" << ends;
    pstr << "helvetica " << size_ << " bold italic" << ends;
    break;
  case 31:
    fstr << '{' << opts->times << '}' << ' ' << size_ << " bold italic" << ends;
    pstr << "times " << size_ << " bold italic" << ends;
    break;
  case 34:
    fstr << '{' << opts->courier << '}' << ' ' << size_ << " bold italic" << ends;
    pstr << "courier " << size_ << " bold italic" << ends;
    break;

  default:
    fstr << '{' << opts->helvetica << '}' << ' ' << size_ << " normal roman" << ends;
    pstr << "helvetica " << size_ << " normal roman" << ends;
    font_ = 2;
    break;
  }

  tkfont_ = Tk_GetFont(parent->getInterp(), parent->getTkwin(), 
		       fstr.str().c_str());
  psfont_ = Tk_GetFont(parent->getInterp(), parent->getTkwin(), 
		       pstr.str().c_str());
}

void Attribute::setColour(double v)
{
  if (v == colour_)
    return;

  if (colorName_)
    delete [] colorName_;
  colorName_ = NULL;

  // still provide backward compatibility for old color scheme
  if (v==1)
    colour_ = (int)0xffffff; // white
  else if (v==2)
    colour_ = (int)0xff0000; // red
  else if (v==3)
    colour_ = (int)0x00ff00; // green
  else if (v==4)
    colour_ = (int)0x0000ff; // blue
  else if (v==5)
    colour_ = (int)0x00ffff; // cyan
  else if (v==6)
    colour_ = (int)0xff00ff; // magneta
  else if (v==7)
    colour_ = (int)0xffff00; // yellow
  else
    colour_ = (int)v;

  ostringstream str;
  str << '#' << setw(6) << setfill('0') << hex << colour_ << ends;
  colorName_ = dupstr(str.str().c_str());
  color_ = parent->getColor(str.str().c_str());
 }