File: SOpaqueLabel.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (230 lines) | stat: -rw-r--r-- 5,177 bytes parent folder | download
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "swidget/SOpaqueLabel.h"
#include "swindow/SAwt.h"

#define SS_ORIG_MGN 2

SOpaqueLabel::SOpaqueLabel (const SString& string) : textView (string)
{
  alignment = SD_Left;
  textView.setMultiline (false);
  textView.setLineEndMark (false);
  textView.textData.fireEvent();
  processLabelText();
  icon = 0;
  recalcSize();
}

SOpaqueLabel::SOpaqueLabel (const SString& string, SIcon* _icon) :
   textView (string)
{
  alignment = SD_Left;
  textView.setMultiline (false);
  textView.setLineEndMark (true);
  textView.textData.fireEvent();
  processLabelText();
  icon = _icon;
  recalcSize();
}

SOpaqueLabel::~SOpaqueLabel ()
{
  if (icon) delete icon;
}

void
SOpaqueLabel::redraw(SCanvas* w, int x, int y,
     unsigned int width ,unsigned int height)
{
//fprintf (stderr, "Icon %d,%d, %ux%u\n", x, y, width, height);
  if (icon) {
        icon->redraw (w, x, y, width, height);
  }
  textView.redraw (w, x, y, width, height);
}

void
SOpaqueLabel::resize (const SDimension& size)
{
  SComponent::resize(size);
  recalcSize();
}

void
SOpaqueLabel::move (const SLocation& loc)
{
  SComponent::move (loc);
  recalcSize ();
}

void
SOpaqueLabel::recalcSize()
{
  SDimension di;
  if (icon)
  {
     di = icon->getPreferredSize();
  }
  SDimension dt = textView.getPreferredSize();
  SString s = textView.textData.getText();
  unsigned int mp = (s.size()==0) ? 2: SAwt::getRasterScale() +2;
  //mp = mp * SAwt::getRasterScale();
  int MGN = SS_ORIG_MGN * SAwt::getRasterScale();
  SDimension w = SDimension (di.width + dt.width + mp * MGN , 
        (di.height > dt.height) ? di.height : dt.height
  );
  preferredSize = w;
  SDimension _size = getSize();

  /* centering */
  int dw = ((int)_size.width-(int)w.width - getLocation().x * 2);
  int dh = ((int)_size.height-(int)w.height - getLocation().y * 2);


  SLocation nl = getLocation ();
  if (alignment == SD_Center)
  {
//fprintf (stderr,"center: dw=%d nl.x=%d size.width=%u w.width=%u new nl.x=%d\n", 
 //   dw, nl.x, _size.width, w.width, nl.x + dw/2);
     nl.x = nl.x + dw/2 + getLocation ().x;
     nl.y = nl.y + dh/2 + getLocation ().y;
  }
  else if (alignment == SD_Right)
  {
     nl.x = nl.x + dw;
     nl.y = nl.y + dh;
  }
  //SDimension dd ((dw<=0)?2:dw, (dh<=0)?2:dh);
  //SDimension nd (_size.width-dd/2); 

  int h = w.height;
  if (icon)
  {
     icon->move (SLocation (nl.x + MGN, nl.y + (h-(int)di.height)/2));
  }
  textView.move (SLocation (nl.x + (int)di.width +2*MGN, 
       nl.y + (h-(int)dt.height)));

  if (icon) icon->resize (di);
  textView.resize (dt);
}

void
SOpaqueLabel::setFontSize (double fontSize)
{
  textView.setFontSize (fontSize);
  recalcSize();
}

void
SOpaqueLabel::setFont (const SString& font, double fontSize)
{
  textView.setFont(font, fontSize);
  recalcSize();
}

void
SOpaqueLabel::setForeground (const SColor& fg)
{
  textView.setForeground (fg, fg);
}

void
SOpaqueLabel::setForeground (const SColor& lrfg, const SColor& rlfg)
{
  textView.setForeground (lrfg, rlfg);
}

void
SOpaqueLabel::setBackground (const SColor& bg)
{
  textView.setBackground (bg);
  if (icon) icon->setBackground (bg);
}


const SDimension&
SOpaqueLabel::getPreferredSize ()
{
  recalcSize();
  return SComponent::getPreferredSize();
}

const SColor&
SOpaqueLabel::getBackground ()
{
  return textView.getBackground();
}

const SColor&
SOpaqueLabel::getForeground (bool lr)
{
  return textView.getForeground(lr);
}

void
SOpaqueLabel::setIcon (SIcon* _icon)
{
  if (icon) delete icon;
  icon = _icon;
// SGC
  if (icon) icon->setBackground (getBackground());
  recalcSize();
}

void
SOpaqueLabel::setText (const SString& text)
{
  textView.setText (text);
  processLabelText();
  recalcSize();
}

void
SOpaqueLabel::processLabelText()
{
  /* underline __keys__ */
  textView.textData.move(STextIndex(0,0));
  STextIndex st = textView.textData.find ("__");
  if (st == STextIndex(0,0)) return;
  textView.textData.move(st);
  STextIndex et = textView.textData.find ("__");
  if (et == STextIndex(0,0))
  {
    return;
  }
  textView.textData.underline (st);

  textView.textData.move(STextIndex(0,0));
  st = textView.textData.find ("__");
  textView.textData.remove (st);
  textView.textData.fireEvent ();
  st = textView.textData.find ("__");
  textView.textData.remove (st);
  textView.textData.fireEvent ();
}

void
SOpaqueLabel::setAlignment (SAlignment _alignment)
{
  alignment = _alignment;
  recalcSize();
}