File: SCaret.cpp

package info (click to toggle)
yudit 2.5.4-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,528 kB
  • ctags: 8,403
  • sloc: cpp: 59,394; ansic: 2,585; perl: 2,398; makefile: 864; sh: 321
file content (273 lines) | stat: -rw-r--r-- 5,280 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 2002  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2001  Gaspar Sinai <gsinai@yudit.org>  
 *  GNU Copyright (C) 2000  Gaspar Sinai <gsinai@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/SCaret.h>

/**
 * Create a caret.
 * A caret can have two directions : < or >
 */
SCaret::SCaret (void)
  : lrpen (SColor (0.0, 0.0, 0.0, 1.0)), 
  rlpen (SColor (0.0, 0.0, 1.0, 1.0))
{
  lr = '>';
  showCaret = true;
  skipBlink = false;
  state = true;
  lrpen.setBackground (SColor(1.0, 1.0, 1.0, 1.0));
  rlpen.setBackground (SColor(1.0, 1.0, 1.0, 1.0));
  timer = 0;
}

SCaret::~SCaret ()
{
  if (timer) delete timer;
}

/**
 * Set the direction of this cursor.
 * @param lr is true if we are an lr cursor.
 */
void
SCaret::setDirection (char _lr)
{
  lr = _lr;
  redraw ();
}

char
SCaret::getDirection() const
{
  return lr;
}

/**
 * Set the background.
 * @param bg is the new background
 */
void
SCaret::setBackground (const SColor& bg)
{
  lrpen.setBackground (bg);
  rlpen.setBackground (bg);
}

/**
 * Set the foreground.
 * @param fg is the new foreground
 */
void
SCaret::setForeground (const SColor& rlfg, const SColor& lrfg)
{
  lrpen.setForeground (rlfg);
  rlpen.setForeground (lrfg);
}

/**
 * The caret usually takes two glyp positions.
 * The middle is the insertion point
 */
void
SCaret::redraw (SCanvas* canvas)
{
  if (isSaneSize())
  {
    redraw (canvas, location.x, location.y, size.width, size.height);
  }
}

/**
 * The caret usually takes two glyp positions.
 * The middle is the insertion point
 * caret is draw in the middle.
 */
void
SCaret::redraw (SCanvas* canvas, int x, int y, unsigned int width, unsigned int height)
{
  if (!isOn()) return;
  if (state == false) return;
  if (!isSaneSize()) return;

  double middleX = (double)getLocation().x;
  double middleY = (double)getLocation().y + (double)getSize().height/2;
  double dx = (double)getSize().width/3;
  double dy = (double)getSize().height/3;
  char a[64];
  sprintf (a, "caret=%u %u %c;", 
         getSize().width, getSize().height, lr);
  if (lr == '>')
  {
    /* should return false */
    if (!canvas->newpath (middleX, middleY - dy +1, a))
    {
      canvas->moveto (middleX, middleY - dy +1);
      canvas->lineto (middleX + dx -1, middleY);
      canvas->lineto (middleX, middleY + dy -1);
      canvas->closepath ();
    }
    canvas->fill (lrpen);
  }
  else
  {
    /* should return false */
    if (!canvas->newpath (middleX, middleY - dy +1, a))
    {
      canvas->moveto (middleX, middleY - dy +1);
      canvas->lineto (middleX - dx +1, middleY);
      canvas->lineto (middleX, middleY + dy -1);
      canvas->closepath ();
    }
    canvas->fill (rlpen);
  }
}

/**
 * Show or hide the caret
 */
void
SCaret::on (bool _on)
{
  if (_on != showCaret) redraw ();
  showCaret = _on;
  if (state != _on) redraw ();
  state = _on;
  skipBlink = true;
}

/**
 * Request a redraw from parent if possible.
 * The redraw will happen async, later.
 */
void
SCaret::redraw ()
{
  SWindow* w = getWindow();
  if (w != 0)
  {
    if (isSaneSize())
    {
      w->redraw (true, location.x - (int) size.width/2, location.y,
         size.width,  size.height);
    }
  }
}

/**
 * Show if caret is shown
 */
bool
SCaret::isOn () const
{
  return showCaret;
}

/**
 * Show if it is animating.
 */
bool
SCaret::isAnimating() const
{
  return timer!=0;
}

/**
 * The timer event
 */
bool
SCaret::timeout (const SEventSource* s)
{
  if (timer == 0)
  {
    fprintf (stderr, "strange. timeout..\n");
    return false;
  }
  if (!showCaret)
  {
    return true;
  }
  if (skipBlink)
  {
    skipBlink = false;
    return true;
  }
  state = !state;
  redraw ();
  return true;
}

/**
 * Resize the component
 * @param d is the new size
 */
void 
SCaret::resize(const SDimension& d)
{
  redraw ();
  SComponent::resize (d);
  redraw ();
}

/**
 * Move the component
 * @param l is the new location
 */
void 
SCaret::move(const SLocation& l)
{
  redraw ();
  SComponent::move (l);
  redraw ();
}

/**
 * Do bliking animation of 
 * @param _animate is true. 
 * stop animation otherwise.
 */
void
SCaret::animate (bool _animate)
{
  if ((timer!=0)==_animate) return;
  if (timer) delete timer;
  timer = 0;
  if (_animate)
  {
    timer = STimer::newTimer(500, this);
    skipBlink = false;
  }
  state = showCaret;
  redraw ();
}

/**
 * return true if it has a possible size.
 */
bool
SCaret::isSaneSize()
{
  SLocation up = getLocation() + getSize();
  /* out */
  if (up.x < 0 || up.y < 0) return false;
  /* out */
  if (up.x > 8000 || up.y > 8000) return false;
  return true;
}