File: FX7Segment.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (628 lines) | stat: -rw-r--r-- 16,494 bytes parent folder | download | duplicates (2)
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/********************************************************************************
*                                                                               *
*                7 - S e g m e n t   D i s p l a y   W i d g e t                *
*                                                                               *
*********************************************************************************
* Copyright (C) 2004,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library 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 Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxmath.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXStringDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXEvent.h"
#include "FXWindow.h"
#include "FXDCWindow.h"
#include "FXApp.h"
#include "FX7Segment.h"


/*
  Notes:
  - Emulate old LED or LCD Display.
  - Segment numbering:

             01
           -----
       02 |     | 04      ++
          |  08 |         ++
           -----
       10 |     | 20      ++    100
          |     |         ++
           -----  o 80
             40

  - Still to add: decimal point, a few more letters.
  - Perhaps some control over cell spacing.
*/


#define JUSTIFY_MASK            (JUSTIFY_HZ_APART|JUSTIFY_VT_APART)
#define SEVENSEGMENT_MASK       (SEVENSEGMENT_NORMAL|SEVENSEGMENT_SHADOW)


using namespace FX;

/*******************************************************************************/

namespace FX {


const FXuint segm[]={
  0x0000,         // 20   SPACE
  0x0000,         // 21   !
  0x0000,         // 22   "
  0x0000,         // 23   #
  0x0000,         // 24   $
  0x0000,         // 25   %
  0x0000,         // 26   &
  0x0000,         // 27   '
  0x0053,         // 28   (
  0x0065,         // 29   )
  0x0000,         // 2A   *
  0x0000,         // 2B   +
  0x0080,         // 2C   ,
  0x0008,         // 2D   -
  0x0080,         // 2E   .
  0x0000,         // 2F   /

  0x0077,         // 30   0
  0x0024,         // 31   1
  0x005d,         // 32   2
  0x006d,         // 33   3
  0x002e,         // 34   4
  0x006b,         // 35   5
  0x007b,         // 36   6
  0x0025,         // 37   7
  0x007f,         // 38   8
  0x006f,         // 39   9
  0x0100,         // 3A   :
  0x0100,         // 3B   ;
  0x0000,         // 3C   <
  0x0048,         // 3D   =
  0x0000,         // 3E   >
  0x0000,         // 3F   ?

  0x0000,         // 40   @
  0x003F,         // 41   A
  0x007A,         // 42   B
  0x0058,         // 43   C
  0x007C,         // 44   D
  0x005B,         // 45   E
  0x001B,         // 46   F
  0x0073,         // 47   G
  0x003A,         // 48   H
  0x0024,         // 49   I
  0x0064,         // 4A   J
  0x001A,         // 4B   K
  0x0052,         // 4C   L
  0x0037,         // 4D   M
  0x0038,         // 4E   N
  0x0078,         // 4F   O

  0x001F,         // 50   P
  0x002F,         // 51   Q
  0x0018,         // 52   R
  0x006B,         // 53   S
  0x005A,         // 54   T
  0x0070,         // 55   U
  0x0076,         // 56   V
  0x0076,         // 57   W
  0x0049,         // 58   X
  0x006E,         // 59   Y
  0x005d,         // 5A   Z
  0x0053,         // 5B   [
  0x0000,         // 5C
  0x0065,         // 5D   ]
  0x0000,         // 5E   ^
  0x0040,         // 5F   _

  0x0000,         // 60   `
  0x003F,         // 61   a
  0x007A,         // 62   b
  0x0058,         // 63   c
  0x007C,         // 64   d
  0x005b,         // 65   e
  0x001B,         // 66   f
  0x0073,         // 67   g
  0x003A,         // 68   h
  0x0024,         // 69   i
  0x0064,         // 6A   j
  0x001A,         // 6B   k
  0x0052,         // 6C   l
  0x0037,         // 6D   m
  0x0038,         // 6E   n
  0x0078,         // 6F   o

  0x001F,         // 70   p
  0x002F,         // 71   q
  0x0018,         // 72   r
  0x006B,         // 73   s
  0x005A,         // 74   t
  0x0070,         // 75   u
  0x0076,         // 76   v
  0x0076,         // 77   w
  0x0049,         // 78   x
  0x006E,         // 79   y
  0x005d,         // 7A   z
  0x0053,         // 7B   {
  0x0024,         // 7C   |
  0x0065,         // 7D   }
  0x0001,         // 7E   ~
  };


// map
FXDEFMAP(FX7Segment) FX7SegmentMap[]={
  FXMAPFUNC(SEL_PAINT,0,FX7Segment::onPaint),
  FXMAPFUNC(SEL_QUERY_TIP,0,FX7Segment::onQueryTip),
  FXMAPFUNC(SEL_QUERY_HELP,0,FX7Segment::onQueryHelp),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETVALUE,FX7Segment::onCmdSetValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETINTVALUE,FX7Segment::onCmdSetIntValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_GETINTVALUE,FX7Segment::onCmdGetIntValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETREALVALUE,FX7Segment::onCmdSetRealValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_GETREALVALUE,FX7Segment::onCmdGetRealValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETLONGVALUE,FX7Segment::onCmdSetLongValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_GETLONGVALUE,FX7Segment::onCmdGetLongValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETSTRINGVALUE,FX7Segment::onCmdSetStringValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_GETSTRINGVALUE,FX7Segment::onCmdGetStringValue),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETHELPSTRING,FX7Segment::onCmdSetHelp),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_GETHELPSTRING,FX7Segment::onCmdGetHelp),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_SETTIPSTRING,FX7Segment::onCmdSetTip),
  FXMAPFUNC(SEL_COMMAND,FX7Segment::ID_GETTIPSTRING,FX7Segment::onCmdGetTip),
  };


FXIMPLEMENT(FX7Segment,FXFrame,FX7SegmentMap,ARRAYNUMBER(FX7SegmentMap))


// For serialization
FX7Segment::FX7Segment(){
  flags|=FLAG_ENABLED;
  textColor=0;
  thickness=3;
  cellwidth=12;
  cellheight=18;
  }


// Construct 7 segment display
FX7Segment::FX7Segment(FXComposite* p,const FXString& text,FXuint opts,FXint x,FXint y,FXint w,FXint h,FXint pl,FXint pr,FXint pt,FXint pb):FXFrame(p,opts,x,y,w,h,pl,pr,pt,pb),label(text){
  flags|=FLAG_ENABLED;
  textColor=getApp()->getForeColor();
  thickness=3;
  cellwidth=12;
  cellheight=18;
  }


// Get default width
FXint FX7Segment::getDefaultWidth(){
  FXint w=(cellwidth+2)*label.length();
  if(label.length()) w-=2;
  return padleft+padright+(border<<1)+w;
  }


// Get default height
FXint FX7Segment::getDefaultHeight(){
  return padtop+padbottom+(border<<1)+cellheight;
  }


// Update value from a message
long FX7Segment::onCmdSetValue(FXObject*,FXSelector,void *ptr){
  setText((const FXchar*)ptr);
  return 1;
  }


// Get value as int
long FX7Segment::onCmdGetIntValue(FXObject*,FXSelector,void* ptr){
  *((FXint*)ptr)=label.toInt();
  return 1;
  }


// Set value from int
long FX7Segment::onCmdSetIntValue(FXObject*,FXSelector,void *ptr){
  setText(FXString::value(*((FXint*)ptr)));
  return 1;
  }


// Update value from a message
long FX7Segment::onCmdSetLongValue(FXObject*,FXSelector,void* ptr){
  setText(FXString::value(*((FXlong*)ptr)));
  return 1;
  }


// Obtain value with a message
long FX7Segment::onCmdGetLongValue(FXObject*,FXSelector,void* ptr){
  *((FXlong*)ptr)=label.toLong();
  return 1;
  }


// Get value as double
long FX7Segment::onCmdGetRealValue(FXObject*,FXSelector,void* ptr){
  *((FXdouble*)ptr)=label.toDouble();
  return 1;
  }


// Set value from double
long FX7Segment::onCmdSetRealValue(FXObject*,FXSelector,void* ptr){
  setText(FXString::value(*((FXdouble*)ptr)));
  return 1;
  }


// Get value as string
long FX7Segment::onCmdGetStringValue(FXObject*,FXSelector,void* ptr){
  *((FXString*)ptr)=getText();
  return 1;
  }


// Set value from string
long FX7Segment::onCmdSetStringValue(FXObject*,FXSelector,void *ptr){
  setText(*((FXString*)ptr));
  return 1;
  }


// Set help using a message
long FX7Segment::onCmdSetHelp(FXObject*,FXSelector,void* ptr){
  setHelpText(*((FXString*)ptr));
  return 1;
  }


// Get help using a message
long FX7Segment::onCmdGetHelp(FXObject*,FXSelector,void* ptr){
  *((FXString*)ptr)=getHelpText();
  return 1;
  }


// Set tip using a message
long FX7Segment::onCmdSetTip(FXObject*,FXSelector,void* ptr){
  setTipText(*((FXString*)ptr));
  return 1;
  }


// Get tip using a message
long FX7Segment::onCmdGetTip(FXObject*,FXSelector,void* ptr){
  *((FXString*)ptr)=getTipText();
  return 1;
  }


// We were asked about tip text
long FX7Segment::onQueryTip(FXObject* sender,FXSelector sel,void* ptr){
  if(FXFrame::onQueryTip(sender,sel,ptr)) return 1;
  if((flags&FLAG_TIP) && !tip.empty()){
    sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&tip);
    return 1;
    }
  return 0;
  }


// We were asked about status text
long FX7Segment::onQueryHelp(FXObject* sender,FXSelector sel,void* ptr){
  if(FXFrame::onQueryHelp(sender,sel,ptr)) return 1;
  if((flags&FLAG_HELP) && !help.empty()){
    sender->handle(this,FXSEL(SEL_COMMAND,ID_SETSTRINGVALUE),(void*)&help);
    return 1;
    }
  return 0;
  }


// Handle repaint
long FX7Segment::onPaint(FXObject*,FXSelector,void *ptr){
  FXEvent *event=(FXEvent*)ptr;
  FXint tx,ty,tw,ch,cw;
  FXDCWindow dc(this,event);

  // Draw frame
  drawFrame(dc,0,0,width,height);

  // Draw background
  dc.setForeground(backColor);
  dc.fillRectangle(border,border,width-(border<<1),height-(border<<1));

  // Non empty
  if(label.length()){

    cw=cellwidth;
    ch=cellheight;

    tw=label.length()*(cw+2);
    if(label.length()) tw-=2;

    // Justify in x
    if((options&JUSTIFY_LEFT) && (options&JUSTIFY_RIGHT)){      // FIXME not good yet
      tx=border+padleft;
      tw=width-padleft-padright-(border<<1);
      cw=tw/label.length();
      }
    else if(options&JUSTIFY_LEFT){
      tx=border+padleft;
      }
    else if(options&JUSTIFY_RIGHT){
      tx=width-padright-border-tw;
      }
    else{
      tx=border+padleft+(width-padleft-padright-(border<<1)-tw)/2;
      }

    // Justify in y
    if((options&JUSTIFY_TOP) && (options&JUSTIFY_BOTTOM)){
      ty=border+padtop;
      ch=height-padbottom-padtop-(border<<1);
      }
    else if(options&JUSTIFY_TOP){
      ty=border+padtop;
      }
    else if(options&JUSTIFY_BOTTOM){
      ty=height-padbottom-border-ch;
      }
    else{
      ty=border+padtop+(height-padbottom-padtop-(border<<1)-ch)/2;
      }

    // Draw cells with shadow
    if(options&SEVENSEGMENT_SHADOW){
      dc.setForeground(shadowColor);
      drawCells(dc,tx+1,ty+1,cw,ch);
      }

    // Draw cells normally
    dc.setForeground(textColor);
    drawCells(dc,tx,ty,cw,ch);
    }
  return 1;
  }


// Draw cells
void FX7Segment::drawCells(FXDCWindow &dc,FXint x,FXint y,FXint cw,FXint ch){
  for(FXint c=0; c<label.length(); c++){
    FXint t=(FXuchar)label[c];
    if(' '<=t && t<127){
      drawSegments(dc,x+c*(cellwidth+2),y,cw,ch,segm[t-' ']);
      }
    }
  }


// Draw segments
void FX7Segment::drawSegments(FXDCWindow &dc,FXint x,FXint y,FXint w,FXint h,FXuint segments){
  FXPoint points[6];
  if(segments&0x02){            // Upper left
    points[0].x=x;
    points[0].y=y;
    points[1].x=x+thickness;
    points[1].y=y+thickness;
    points[2].x=x+thickness;
    points[2].y=y+(h>>1)-(thickness>>1)-1;
    points[3].x=x;
    points[3].y=y+(h>>1);
    dc.fillPolygon(points,4);
    }
  if(segments&0x04){            // Upper right
    points[0].x=x+w;
    points[0].y=y;
    points[1].x=x+w;
    points[1].y=y+(h>>1);
    points[2].x=x+w-thickness;
    points[2].y=y+(h>>1)-(thickness>>1)-1;
    points[3].x=x+w-thickness;
    points[3].y=y+thickness;
    dc.fillPolygon(points,4);
    }
  if(segments&0x10){            // Lower left
    points[0].x=x;
    points[0].y=y+(h>>1);
    points[1].x=x+thickness;
    points[1].y=y+(h>>1)-(thickness>>1)+thickness;
    points[2].x=x+thickness;
    points[2].y=y+h-thickness-1;
    points[3].x=x;
    points[3].y=y+h-1;
    dc.fillPolygon(points,4);
    }
  if(segments&0x20){            // Lower right
    points[0].x=x+w;
    points[0].y=y+(h>>1);
    points[1].x=x+w;
    points[1].y=y+h-1;
    points[2].x=x+w-thickness;
    points[2].y=y+h-thickness-1;
    points[3].x=x+w-thickness;
    points[3].y=y+(h>>1)-(thickness>>1)+thickness;
    dc.fillPolygon(points,4);
    }
  if(segments&0x01){            // Top
    points[0].x=x+1;
    points[0].y=y;
    points[1].x=x+w-1;
    points[1].y=y;
    points[2].x=x+w-thickness-1;
    points[2].y=y+thickness;
    points[3].x=x+thickness+1;
    points[3].y=y+thickness;
    dc.fillPolygon(points,4);
    }
  if(segments&0x40){            // Bottom
    points[0].x=x;
    points[0].y=y+h;
    points[1].x=x+w;
    points[1].y=y+h;
    points[2].x=x+w-thickness;
    points[2].y=y+h-thickness;
    points[3].x=x+thickness;
    points[3].y=y+h-thickness;
    dc.fillPolygon(points,4);
    }
  if(segments&0x08){            // Middle
    points[0].x=x+1;
    points[0].y=y+(h>>1);
    points[1].x=x+thickness;
    points[1].y=y+(h>>1)-(thickness>>1);
    points[2].x=x+w-thickness;
    points[2].y=y+(h>>1)-(thickness>>1);
    points[3].x=x+w-1;
    points[3].y=y+(h>>1);
    points[4].x=x+w-thickness-2;
    points[4].y=y+(h>>1)-(thickness>>1)+thickness;
    points[5].x=x+thickness+1;
    points[5].y=y+(h>>1)-(thickness>>1)+thickness;
    dc.fillPolygon(points,6);
    }
  if(segments&128){             // Decimal
    }
  if(segments&256){             // Colon
    dc.fillRectangle(x+(w>>1)-(thickness>>1)-1,y+(h>>1)-(thickness>>1)-thickness-1,thickness,thickness);
    dc.fillRectangle(x+(w>>1)-(thickness>>1)-1,y+(h>>1)-(thickness>>1)+thickness+1,thickness,thickness);
    }
  }


// Change text
void FX7Segment::setText(const FXString& text){
  if(label!=text){
    if(label.length()!=text.length()) recalc();
    label=text;
    update();
    }
  }


// Set text color
void FX7Segment::setTextColor(FXColor clr){
  if(textColor!=clr){
    textColor=clr;
    update();
    }
  }


// Get/set cell width
void FX7Segment::setCellWidth(FXint w){
  if(cellwidth!=w){
    cellwidth=w;
    recalc();
    update();
    }
  }


// Get/set cell height
void FX7Segment::setCellHeight(FXint h){
  if(cellheight!=h){
    cellheight=h;
    recalc();
    update();
    }
  }


// set segment thickness
void FX7Segment::setThickness(FXint t){
  if(t<1) t=1;
  if(!(t&1)) t|=1;
  if(thickness!=t){
    thickness=t;
    recalc();
    update();
    }
  }


// Change 7 segment style
void FX7Segment::set7SegmentStyle(FXuint style){
  FXuint opts=(options&~SEVENSEGMENT_MASK) | (style&SEVENSEGMENT_MASK);
  if(options!=opts){
    options=opts;
    update();
    }
  }


// Get 7 segment
FXuint FX7Segment::get7SegmentStyle() const {
  return (options&SEVENSEGMENT_MASK);
  }


// Set text justify style
void FX7Segment::setJustify(FXuint style){
  FXuint opts=(options&~JUSTIFY_MASK) | (style&JUSTIFY_MASK);
  if(options!=opts){
    options=opts;
    update();
    }
  }


// Get text justify style
FXuint FX7Segment::getJustify() const {
  return (options&JUSTIFY_MASK);
  }


// Save object to stream
void FX7Segment::save(FXStream &store) const {
  FXFrame::save(store);
  store << label;
  store << textColor;
  store << thickness;
  store << cellwidth;
  store << cellheight;
  store << tip;
  store << help;
  }


// Load object from stream
void FX7Segment::load(FXStream &store) {
  FXFrame::load(store);
  store >> label;
  store >> textColor;
  store >> thickness;
  store >> cellwidth;
  store >> cellheight;
  store >> tip;
  store >> help;
  }

}