File: STextTable.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 (252 lines) | stat: -rw-r--r-- 6,093 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/** 
 *  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/STextTable.h"

STextTable::STextTable (const SStringVector& titles) : border(SBorder::ETCHED)
{
  SPanel::forceLayout (SLayout(SDimension(2000,2000)));
  listener = 0;
  lastSelected = -1;

  SLabel *l = 0;
  STextList *tl = 0;
  for (unsigned int i=0; i<titles.size(); i++)
  { 
    l  = new  SLabel (titles[i]);
    tl = new STextList();
    topLabels.append (l);
    textLists.append (tl);
    add (l);
    add (tl);
    tl->setListListener (this);
//    l->changeStyle (SBorder::SOLID_VISIBLE);
    l->setAlignment (SD_Center);
  }
  slider = new SSlider();
  add (slider);
  recalc();
}

STextTable::~STextTable ()
{
}
void
STextTable::setText (const SStringTable& t)
{
  table = t;
  for (unsigned int i=0; i<textLists.size(); i++)
  {
     textLists[i]->setText (table[i]);
  }
}

bool
STextTable::selectText (const SString& s, unsigned int column)
{
  SStringVector sl = table [column];
  for (unsigned int i=0; i<sl.size(); i++)
  {
     if (s == sl[i])
     {
       return selectItem ((int) i);
     }
  }
  return false;
}

bool
STextTable::selectItem (int item)
{
  bool ret = true;
  for (unsigned int i=0; i<textLists.size(); i++)
  {
    ret = ret && textLists[i]->selectItem (item);
  }
  return ret;
}

void
STextTable::setListListener (SListListener* l)
{
  listener = l;
}

void
STextTable::setBackground (const SColor& bg)
{
  SPanel::setBackground (bg);
  border.setBackground (bg);
}

void
STextTable::setLabelForeground (const SColor& fg)
{
  for (unsigned int i=0; i<topLabels.size(); i++)
  {
     topLabels[i]->setForeground (fg);
  }
}

void
STextTable::setSliderBackground (const SColor& bg)
{
  slider->setSliderBackground (bg);
}

void
STextTable::setFont (const SString& font, double fontSize)
{
  for (unsigned int i=0; i<topLabels.size(); i++)
  {
    topLabels[i]->setFont (font, fontSize);
    textLists[i]->setFont (font, fontSize);
  }
  recalc ();
}
void
STextTable::setFontSize (double fontSize)
{
  for (unsigned int i=0; i<topLabels.size(); i++)
  {
    topLabels[i]->setFontSize (fontSize);
    textLists[i]->setFontSize (fontSize);
  }
  recalc ();
}
int
STextTable::getLastSelected ()
{
  return lastSelected;
}
void
STextTable::itemSelected (void* source, const SAccelerator* acc)
{
  STextList* listSource = (STextList*) source;
  lastSelected = listSource->getLastSelected();
  for (unsigned int i=0; i<topLabels.size(); i++)
  {
    if (textLists[i] == listSource) continue;
    textLists[i]->selectItem (lastSelected);
  }
  if (listener) listener->itemSelected (this, acc);
}

void
STextTable::itemHighlighted (void* source, int item)
{
  STextList* listSource = (STextList*) source;
  for (unsigned int i=0; i<topLabels.size(); i++)
  {
    if (textLists[i] == listSource) continue;
    textLists[i]->selectItem (item);
  }
}

void
STextTable::resize(const SDimension& d)
{
  border.resize(d);
  SPanel::resize (d);
}

void
STextTable::redraw (SCanvas *canvas, int x, int y, 
     unsigned int width, unsigned int height)
{
  //SPanel::redraw (canvas, x, y, width, height);
  border.redraw(canvas, x, y, width, height);
}

/**
 * recalculate the Layout of each component
 * preferredSize should be calculated before calling.
 */
void
STextTable::recalc()
{
  unsigned int w = 20;
  unsigned int i;
  for (i=0; i<topLabels.size(); i++)
  { 
    w += topLabels[i]->getPreferredSize().width;
  }
  unsigned int h = topLabels[0]->getPreferredSize().height;
  preferredSize = SDimension (w + 4, h * 2 + 4);
  SDimension d = preferredSize;

  //unsigned int sliderWidth = 20;
  unsigned int sliderWidth = (int) (SAwt::getScale() * 20.0);

  unsigned int lh = topLabels[0]->getPreferredSize().height;
  SDimension bd = border.getBorderSize();
  
  unsigned int fullWidth = 0;
  for (i=0; i<topLabels.size(); i++)
  {
    fullWidth += topLabels[i]->getPreferredSize().width;
  }
  SDimension estate;
  estate  = d - (bd * 2);
  if (estate.width > sliderWidth) estate.width -= sliderWidth;

  unsigned int widthCount = 0;
  unsigned int current = 0;
  for (i=0; i<topLabels.size(); i++)
  {
    current = topLabels[i]->getPreferredSize().width;
    current = (current * estate.width)/fullWidth;
    if (current == 0) current = 2;
    topLabels[i]->setLayout (
      SLayout (
        SLocation (widthCount + bd.width, bd.height),
        SLocation (widthCount + current + bd.width, bd.height + lh),
        SLocation (widthCount * 100 / estate.width, 0),
        SLocation ((widthCount + current) * 100 / estate.width, 0)
      )
    );
    textLists[i]->setLayout (
      SLayout (
        SLocation (widthCount + bd.width, bd.height + lh),
        //SLocation (widthCount + current + bd.width, d.height + bd.height),
        SLocation (widthCount + current + bd.width, d.height - bd.height),
        SLocation (widthCount * 100 / estate.width, 0),
        SLocation ((widthCount + current) * 100 / estate.width, 100)
      )
    );
    widthCount += current;
  }
  slider->setLayout (
    SLayout (
      SLocation (estate.width + bd.width, lh+(int)bd.height),
      SLocation (d.width-(int)bd.width, d.height - bd.height),
      SLocation (100, 0),
      SLocation (100, 100)
    )
  );

  /* save current */
  SLayout goodlayout = layout;

  /* pretend we have this layout */
  forceLayout (preferredSize);

  /* accept old layout */
  setLayout (goodlayout);
}