File: SButton.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 (451 lines) | stat: -rw-r--r-- 9,175 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/** 
 *  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/SButton.h"

#define SS_MGN 2

SButtonListener::SButtonListener(void)
{
}

SButtonListener::~SButtonListener()
{
}
void
SButtonListener::enterComponent (void* source)
{
}

void
SButtonListener::leaveComponent (void* source)
{
}

/**
 * create a button.
 * @param autoacc indicates that the letter preceded by '&'
 * should contain the accelerator.
 */
SButton::SButton (const SString& string) : label (string) , border(SBorder::ETCHED),  back("white")
{
  pressed = false;
  inside = false;
  listener = 0;
  button = 0;
  applyAccelerator();
  recalcSize();
  clip (true);
}

SButton::SButton (SIcon* icon, const SString& accel) :
  label ("", icon), border(SBorder::ETCHED), back ("white")
{
  pressed = false;
  inside = false;
  listener = 0;
  button = 0;
  addAccelerator (accel);
  recalcSize();
  clip (true);
}

/**
 * create a button.
 * @param autoacc indicates that the letter preceded by '&'
 * should contain the accelerator.
 */
SButton::SButton (const SString& string, SIcon* _icon) 
  : label (string, _icon), border(SBorder::ETCHED), back ("white")
{
  pressed = false;
  inside = false;
  button = 0;
  listener = 0;
  applyAccelerator();
  recalcSize();
  clip (true);
}

/**
 * get the &char from text and apply accel on it.
 */
void
SButton::applyAccelerator()
{
  processLabelText();
}

void 
SButton::processLabelText ()
{
  label.textView.textData.move(STextIndex(0,0));
  /* accelerate &key */
  STextIndex inx = label.textView.textData.find ("&");
  if (inx!=STextIndex(0,0))
  {
    STextIndex at = label.textView.textData.getTextIndex ();
    label.textView.textData.remove (inx);
    SString orig = label.textView.textData.getText (inx, at);
    if (addAccelerator (orig))
    {
      label.textView.textData.underline (inx);
    }
  }
  label.textView.textData.fireEvent();
}

bool
SButton::addAccelerator(const SString& orig)
{
  SString upper = orig;
  SString lower = orig;
  upper.upper ();
  lower.lower ();

  int id0 = getButtonID (upper);
  if (id0 < 0)
  {
    fprintf (stderr, "SButton::applyAccelerator no id for '%*.*s'.\n",
        SSARGS (upper));
    return false;
  }
  int id1 = getButtonID (lower);
  if (id1 < 0)
  {
    fprintf (stderr, "SButton::applyAccelerator no id for '%*.*s'.\n",
        SSARGS (lower));
    return false;
  }
  /* ctrl */
  addAccelerator ((SWindowListener::SKey)id0, true, false, false);
  addAccelerator ((SWindowListener::SKey)id1, true, false, false);

  /* alt */
  addAccelerator ((SWindowListener::SKey)id0, false, false, true);
  addAccelerator ((SWindowListener::SKey)id1, false, false, true);

  /* alt-ctrl */
  addAccelerator ((SWindowListener::SKey)id0, true, false, true);
  addAccelerator ((SWindowListener::SKey)id1, true, false, true);

  return true;
}

SButton::~SButton ()
{
}

void
SButton::redraw(SCanvas* w, int x, int y,
     unsigned int width ,unsigned int height)
{
  clip(true);
  label.redraw (w, x, y, width, height);
  clip(false);
  border.redraw (w, x, y, width, height);
  clip(true);
}

/**
 * The pointer entered the button.
 */
void
SButton::enterWindow (SWindow* w)
{
  if (listener) listener->enterComponent (this);
}

/**
 * The pointer left the button.
 */
void
SButton::leaveWindow (SWindow* w)
{
  if (listener) listener->leaveComponent (this);
}

void
SButton::resize (const SDimension& _size)
{
  SPanel::resize (_size);
  border.resize (_size);

  SDimension ds = getSize() - (border.getBorderSize() * 2);

  SLocation dl = SLocation (
     border.getBorderSize().width,
     border.getBorderSize().height);
  label.move (dl);
  label.resize (ds);
}

void
SButton::move (const SLocation& loc)
{
//fprintf (stderr, "Move from %d -> %d\n", getLocation().x, loc.x); 
  SPanel::move (loc);
//if (location.x == 0) {
//fprintf (stderr, " XXX Moving to location 0.\n");
//}

}

void
SButton::recalcSize()
{
  SDimension db = border.getBorderSize();
  SDimension dl = label.getPreferredSize();
  preferredSize = (db *2 ) + dl;
  label.move (SLocation (db.width, db.height));

}

void
SButton::setFontSize (double fontSize)
{
  label.setFontSize (fontSize);
  recalcSize();
}

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

void
SButton::setForeground (const SColor& fg)
{
  label.setForeground (fg, fg);
}

void
SButton::setForeground (const SColor& lrfg, const SColor& rlfg)
{
  label.setForeground (lrfg, rlfg);
}

void
SButton::setBackground (const SColor& bg)
{
  back = bg;
  SPanel::setBackground(bg);
  border.setBackground (bg);
  label.setBackground (bg);
  window->redraw (true, 0, 0, getSize().width, getSize().height);
}

void
SButton::setButtonBackground (const SColor& bg)
{
  back = bg;
  SPanel::setBackground(bg);
  label.setBackground (bg);
  window->redraw (true, 0, 0, getSize().width, getSize().height);
}

/**
 * Turn clipping at the border on and off.
 * @param on is true if we turn on clipping.
 */
void
SButton::clip (bool on)
{
  if (!on)
  {
    window->removeClippingArea ();
    return;
  }
  window->setClippingArea (
       (int) border.getBorderSize().width ,
       (int) border.getBorderSize().height,
       size.width - 2 * border.getBorderSize().width,
       size.height - 2 * border.getBorderSize().height);
}


/**
 * called when the accelerator key combination is pressed 
 */
void
SButton::acceleratorPressed (const SAccelerator& a)
{
  if (!pressed)
  {
    pressed = true;
    inside = true;
    button = -1;
    changed ();
  }
}

void
SButton::acceleratorReleased (const SAccelerator& a)
{
  if (pressed)
  {
    pressed = false;
    inside = false;
    button = -1;
    changed ();
    if (listener)
    {
      listener->buttonPressedAccel(this, &a);
    }
  }
}

void
SButton::buttonPressed (SWindow * w, int _button, int x, int y)
{
  // scroll
  if (_button == 3 ||  _button == 4) return;
  if (!pressed)
  {
    pressed = true;
    inside = true;
    button = _button;
    changed ();
  }
}

void
SButton::buttonReleased (SWindow * w, int _button, int x, int y)
{
  if (button != _button)
  {
    return;
  }
  bool ins;
  if (x >= 0 && y >= 0 && x < (int) getSize().width 
     && y < (int) getSize().height)
  {
    ins = true;
  } 
  else
  {
    ins = false;
  }
  if (ins != inside)
  {
    inside = ins;
    changed ();
  }
  bool callb = false;
  /* call callback if pressed and inside */
  if (pressed && inside)
  {
    callb = true;
  }
  pressed = false;
  inside = false;
  changed ();
  if (callb && listener)
  {
    // SGC OSX
    listener->buttonPressedAccel(this, 0);
  }
}

void
SButton::buttonDragged (SWindow * w, int button, int x, int y)
{
  bool ins;
  if (x >= 0 && y >= 0 
      && x < (int) getSize().width && y < (int) getSize().height)
  {
    ins = true;
  } 
  else
  {
    ins = false;
  }
  if (ins != inside)
  {
    inside = ins;
    changed ();
  }
}

void
SButton::changed ()
{
  if (pressed && inside)
  {
    SPanel::setBackground(back.darker());
    label.setBackground (back.darker());
  }
  else
  {
    SPanel::setBackground(back);
    label.setBackground (back);
  }

  window->redraw (true, 0, 0, getSize().width, getSize().height);
  // commented out because of double buffer   
  // window->clear (0, 0, getSize().width, getSize().height);
  // label.redraw (window, 0, 0, 
  // getSize().width, getSize().height);
}

void
SButton::setButtonListener (SButtonListener* l)
{
  listener = l;
}

void
SButton::addAccelerator (SWindowListener::SKey k, bool ctrl, 
          bool shift, bool meta)
{
  window->addAccelerator (SAccelerator ((int)k, 
         (int)ctrl, (int) shift, (int) meta), this);
}

void
SButton::setIcon (SIcon* icon)
{
  label.setIcon (icon);
  window->redraw (true, 0, 0, getSize().width, getSize().height);
  // commented out for the above
  // window->clear (0, 0, getSize().width, getSize().height);
  // label.redraw (window, 0, 0, 
  //     getSize().width, getSize().height);
}
void
SButton::setText (const SString& text)
{
  label.setText (text);
  processLabelText ();
  recalcSize();
  window->redraw (true, 0, 0, getSize().width, getSize().height);
  // commented out for the above
  // window->clear (0, 0, getSize().width, getSize().height);
  // label.redraw (window, 0, 0, 
  //     getSize().width, getSize().height);
}

void
SButton::setAlignment (SAlignment _alignment)
{
  label.setAlignment (_alignment);
  window->redraw (true, 0, 0, getSize().width, getSize().height);
}