SUMO - Simulation of Urban MObility
FXSevenSegment.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 //
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2004-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 /* =========================================================================
25  * included modules
26  * ======================================================================= */
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <fxver.h>
34 #include <xincs.h>
35 #include <fxdefs.h>
36 #include <fx.h>
37 /*
38 #include <FXStream.h>
39 #include <FXString.h>
40 #include <FXSize.h>
41 #include <FXPoint.h>
42 #include <FXRectangle.h>
43 #include <FXRegistry.h>
44 #include <FXHash.h>
45 #include <FXApp.h>
46 #include <FXDCWindow.h>
47 */
48 using namespace FX;
49 #include "FXSevenSegment.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 using namespace FXEX;
55 namespace FXEX {
56 
57 /* note: this class may change into FXLCDsegment, so as to support 7 or 14 segment display */
58 #define ASCII_ZERO 48
59 
60 // map
61 FXDEFMAP(FXSevenSegment) FXSevenSegmentMap[] = {
62  FXMAPFUNC(SEL_PAINT, 0, FXSevenSegment::onPaint),
63  FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETVALUE, FXSevenSegment::onCmdSetValue),
64  FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETINTVALUE, FXSevenSegment::onCmdSetIntValue),
65  FXMAPFUNC(SEL_COMMAND, FXWindow::ID_GETINTVALUE, FXSevenSegment::onCmdGetIntValue),
66  FXMAPFUNC(SEL_COMMAND, FXWindow::ID_SETSTRINGVALUE, FXSevenSegment::onCmdSetStringValue),
67  FXMAPFUNC(SEL_COMMAND, FXWindow::ID_GETSTRINGVALUE, FXSevenSegment::onCmdGetStringValue),
68  // FXMAPFUNC(SEL_UPDATE,FXWindow::ID_QUERY_TIP,FXSevenSegment::onQueryTip),
69  // FXMAPFUNC(SEL_UPDATE,FXWindow::ID_QUERY_HELP,FXSevenSegment::onQueryHelp),
70 };
71 FXIMPLEMENT(FXSevenSegment, FXFrame, FXSevenSegmentMap, ARRAYNUMBER(FXSevenSegmentMap))
72 
73 // ctor
74 FXSevenSegment::FXSevenSegment(FXComposite* p, FXObject* tgt, FXSelector sel, FXuint opts, FXint pl, FXint pr, FXint pt, FXint pb) : FXFrame(p, opts, 0, 0, 0, 0, pl, pr, pt, pb), value(' '), fgcolor(FXRGB(0, 255, 0)), bgcolor(FXRGB(0, 0, 0)), hsl(8), vsl(8), st(3), groove(1) {
75  setTarget(tgt);
76  setSelector(sel);
77  enable();
78 }
79 
80 // minimum width
81 FXint FXSevenSegment::getDefaultWidth() {
82  return padleft + (groove << 1) + hsl + padright + (border << 1);
83 }
84 
85 // minimum height
86 FXint FXSevenSegment::getDefaultHeight() {
87  return padtop + (groove << 2) + (vsl << 1) + padbottom + (border << 1);
88 }
89 
90 // set value on widget
91 void FXSevenSegment::setText(FXchar val) {
92  if (FXString(val, 1).upper() != FXString(value, 1).upper()) {
93  value = val;
94  recalc();
95  update();
96  }
97 }
98 
99 // set foreground color
100 void FXSevenSegment::setFgColor(const FXColor clr) {
101  if (fgcolor != clr) {
102  fgcolor = clr;
103  recalc();
104  update();
105  }
106 }
107 
108 // set backgound color
109 void FXSevenSegment::setBgColor(const FXColor clr) {
110  if (bgcolor != clr) {
111  bgcolor = clr;
112  recalc();
113  update();
114  }
115 }
116 
117 // set horizontal segment length
118 void FXSevenSegment::setHorizontal(const FXint len) {
119  if (len != hsl) {
120  hsl = (FXshort)len;
121  checkSize();
122  recalc();
123  update();
124  }
125 }
126 
127 // set vertical segment length
128 void FXSevenSegment::setVertical(const FXint len) {
129  if (len != vsl) {
130  vsl = (FXshort)len;
131  checkSize();
132  recalc();
133  update();
134  }
135 }
136 
137 // set segment thickness
138 void FXSevenSegment::setThickness(const FXint width) {
139  if (width != st) {
140  st = (FXshort)width;
141  checkSize();
142  recalc();
143  update();
144  }
145 }
146 
147 // set groove thickness
148 void FXSevenSegment::setGroove(const FXint width) {
149  if (width != groove) {
150  groove = (FXshort)width;
151  checkSize();
152  recalc();
153  update();
154  }
155 }
156 
157 // draw/redraw object
158 long FXSevenSegment::onPaint(FXObject*, FXSelector, void* ptr) {
159  FXEvent* event = (FXEvent*) ptr;
160  FXDCWindow dc(this, event);
161  drawFrame(dc, 0, 0, width, height);
162  dc.setForeground(bgcolor);
163  dc.fillRectangle(border, border, width - (border << 1), height - (border << 1));
164  dc.setForeground(fgcolor);
165  drawFigure(dc, value);
166  return 1;
167 }
168 
169 // set from value
170 long FXSevenSegment::onCmdSetValue(FXObject*, FXSelector, void* ptr) {
171  FXchar* c = (FXchar*)ptr;
172  if (c[0] != '\0') {
173  setText(c[0]);
174  }
175  return 1;
176 }
177 
178 // get value from int
179 long FXSevenSegment::onCmdGetIntValue(FXObject* sender, FXSelector, void*) {
180  FXint i = value - ASCII_ZERO;
181  if (i < 0) {
182  i = 0;
183  }
184  if (i > 9) {
185  i = 9;
186  }
187  sender->handle(this, FXSEL(SEL_COMMAND, ID_SETINTVALUE), (void*)&i);
188  return 1;
189 }
190 
191 // set from int value
192 long FXSevenSegment::onCmdSetIntValue(FXObject*, FXSelector, void* ptr) {
193  FXint i = *((FXint*)ptr);
194  if (i < 0) {
195  i = 0;
196  }
197  if (i > 9) {
198  i = 9;
199  }
200  setText((FXchar)(i + ASCII_ZERO));
201  return 1;
202 }
203 
204 // get value from string
205 long FXSevenSegment::onCmdGetStringValue(FXObject* sender, FXSelector, void*) {
206  FXString s(value, 1);
207  sender->handle(this, FXSEL(SEL_COMMAND, ID_SETSTRINGVALUE), (void*)&s);
208  return 1;
209 }
210 
211 // set from string value
212 long FXSevenSegment::onCmdSetStringValue(FXObject*, FXSelector, void* ptr) {
213  FXString* s = (FXString*)ptr;
214  if ((*s).length()) {
215  setText((*s)[0]);
216  }
217  return 1;
218 }
219 
220 // draw the specific character - figure out which segments to draw
221 void FXSevenSegment::drawFigure(FXDCWindow& dc, FXchar figure) {
222  switch (figure) {
223  case ' ' :
224  drawSegments(dc, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE);
225  break;
226  case '(' :
227  drawSegments(dc, TRUE , TRUE , FALSE, FALSE, TRUE , FALSE, TRUE);
228  break;
229  case ')' :
230  drawSegments(dc, TRUE , FALSE, TRUE , FALSE, FALSE, TRUE , TRUE);
231  break;
232  case '[' :
233  drawSegments(dc, TRUE , TRUE , FALSE, FALSE, TRUE , FALSE, TRUE);
234  break;
235  case ']' :
236  drawSegments(dc, TRUE , FALSE, TRUE , FALSE, FALSE, TRUE , TRUE);
237  break;
238  case '=' :
239  drawSegments(dc, FALSE, FALSE, FALSE, TRUE , FALSE, FALSE, TRUE);
240  break;
241 // case '+' : drawSegments (dc, FALSE,FALSE,FALSE,TRUE ,FALSE,FALSE,FALSE); break;
242  case '-' :
243  drawSegments(dc, FALSE, FALSE, FALSE, TRUE , FALSE, FALSE, FALSE);
244  break;
245  case '_' :
246  case '.' :
247  drawSegments(dc, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE);
248  break;
249  case '0' :
250  drawSegments(dc, TRUE , TRUE , TRUE , FALSE, TRUE , TRUE , TRUE);
251  break;
252  case '1' :
253  drawSegments(dc, FALSE, FALSE, TRUE , FALSE, FALSE, TRUE , FALSE);
254  break;
255  case '2' :
256  drawSegments(dc, TRUE , FALSE, TRUE , TRUE , TRUE , FALSE, TRUE);
257  break;
258  case '3' :
259  drawSegments(dc, TRUE , FALSE, TRUE , TRUE , FALSE, TRUE , TRUE);
260  break;
261  case '4' :
262  drawSegments(dc, FALSE, TRUE , TRUE , TRUE , FALSE, TRUE , FALSE);
263  break;
264  case '5' :
265  drawSegments(dc, TRUE , TRUE , FALSE, TRUE , FALSE, TRUE , TRUE);
266  break;
267  case '6' :
268  drawSegments(dc, TRUE , TRUE , FALSE, TRUE , TRUE , TRUE , TRUE);
269  break;
270  case '7' :
271  drawSegments(dc, TRUE , FALSE, TRUE , FALSE, FALSE, TRUE , FALSE);
272  break;
273  case '8' :
274  drawSegments(dc, TRUE , TRUE , TRUE , TRUE , TRUE , TRUE , TRUE);
275  break;
276  case '9' :
277  drawSegments(dc, TRUE , TRUE , TRUE , TRUE , FALSE, TRUE , TRUE);
278  break;
279  case 'a' :
280  case 'A' :
281  drawSegments(dc, TRUE , TRUE , TRUE , TRUE , TRUE , TRUE , FALSE);
282  break;
283  case 'b' :
284  case 'B' :
285  drawSegments(dc, FALSE, TRUE , FALSE, TRUE , TRUE , TRUE , TRUE);
286  break;
287  case 'c' :
288  case 'C' :
289  drawSegments(dc, TRUE , TRUE , FALSE, FALSE, TRUE , FALSE, TRUE);
290  break;
291  case 'd' :
292  case 'D' :
293  drawSegments(dc, FALSE, FALSE, TRUE , TRUE , TRUE , TRUE , TRUE);
294  break;
295  case 'e' :
296  case 'E' :
297  drawSegments(dc, TRUE , TRUE , FALSE, TRUE , TRUE , FALSE, TRUE);
298  break;
299  case 'f' :
300  case 'F' :
301  drawSegments(dc, TRUE , TRUE , FALSE, TRUE , TRUE , FALSE, FALSE);
302  break;
303  case 'g' :
304  case 'G' :
305  drawSegments(dc, TRUE , TRUE , FALSE, FALSE, TRUE , TRUE , TRUE);
306  break;
307  case 'h' :
308  case 'H' :
309  drawSegments(dc, FALSE, TRUE , FALSE, TRUE , TRUE , TRUE , FALSE);
310  break;
311  case 'i' :
312  case 'I' :
313  drawSegments(dc, FALSE, FALSE, FALSE, FALSE, FALSE, TRUE , FALSE);
314  break;
315  case 'j' :
316  case 'J' :
317  drawSegments(dc, FALSE, FALSE, TRUE , FALSE, TRUE , TRUE , TRUE);
318  break;
319 // case 'k' :
320 // case 'k' : drawSegments (dc, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE); break;
321  case 'l' :
322  case 'L' :
323  drawSegments(dc, FALSE, TRUE , FALSE, FALSE, TRUE , FALSE, TRUE);
324  break;
325 // case 'm' :
326 // case 'M' : drawSegments (dc, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE); break;
327  case 'n' :
328  case 'N' :
329  drawSegments(dc, FALSE, FALSE, FALSE, TRUE , TRUE , TRUE , FALSE);
330  break;
331  case 'o' :
332  case 'O' :
333  drawSegments(dc, TRUE , TRUE , TRUE , FALSE, TRUE , TRUE , TRUE);
334  break;
335  case 'p' :
336  case 'P' :
337  drawSegments(dc, TRUE , TRUE , TRUE , TRUE , TRUE , FALSE, FALSE);
338  break;
339  case 'q' :
340  case 'Q' :
341  drawSegments(dc, TRUE , TRUE , TRUE , TRUE , FALSE, TRUE , FALSE);
342  break;
343  case 'r' :
344  case 'R' :
345  drawSegments(dc, FALSE, FALSE, FALSE, TRUE , TRUE , FALSE, FALSE);
346  break;
347  case 's' :
348  case 'S' :
349  drawSegments(dc, TRUE , TRUE , FALSE, TRUE , FALSE, TRUE , TRUE);
350  break;
351  case 't' :
352  case 'T' :
353  drawSegments(dc, FALSE, TRUE , FALSE, TRUE , TRUE , FALSE, FALSE);
354  break;
355  case 'u' :
356  case 'U' :
357  drawSegments(dc, FALSE, TRUE , TRUE , FALSE, TRUE , TRUE , TRUE);
358  break;
359 // case 'v' :
360 // case 'V' : drawSegments (dc, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE); break;
361 // case 'w' :
362 // case 'W' : drawSegments (dc, FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE); break;
363  case 'x' :
364  case 'X' :
365  drawSegments(dc, FALSE, TRUE , TRUE , TRUE , TRUE , TRUE , FALSE);
366  break;
367  case 'y' :
368  case 'Y' :
369  drawSegments(dc, FALSE, TRUE , TRUE , TRUE , FALSE, TRUE , TRUE);
370  break;
371 // case 'z' :
372 // case 'Z' :
373  default :
374  fxerror("FXSevenSegment doesnt support: %c\n", figure);
375  }
376 }
377 
378 // validates the sizes of the segment dimensions
379 void FXSevenSegment::checkSize() {
380  if (hsl < 3) {
381  hsl = 3;
382  st = 1;
383  }
384  if (vsl < 3) {
385  vsl = 3;
386  st = 1;
387  }
388  if (st < 1) {
389  st = 1;
390  }
391  if (hsl < (st << 1)) {
392  hsl = (st << 1) + 1;
393  }
394  if (vsl < (st << 1)) {
395  vsl = (st << 1) + 1;
396  }
397  if (hsl < 8 || vsl < 8) {
398  groove = 2;
399  }
400  if (hsl < 1 || vsl < 3 || st < 3) {
401  groove = 1;
402  }
403  if (groove >= st) {
404  groove = st - 1;
405  }
406 }
407 
408 // draw each segment, into the available drawing space
409 // if widget is resizeable, caculate new sizes for length/width/grove of each segment
410 void FXSevenSegment::drawSegments(FXDCWindow& dc, FXbool s1, FXbool s2, FXbool s3, FXbool s4, FXbool s5, FXbool s6, FXbool s7) {
411  FXshort sx = (FXshort)(border + padleft), sy = (FXshort)(border + padtop);
412  FXshort x, y;
413  if (options & LAYOUT_FILL) {
414  if (options & LAYOUT_FILL_X) {
415  hsl = (FXshort)(width - padleft - padright - (border << 1));
416  if (hsl < 4) {
417  hsl = 4;
418  }
419  }
420  if (options & LAYOUT_FILL_Y) {
421  vsl = (FXshort)(height - padtop - padbottom - (border << 1)) >> 1;
422  if (vsl < 4) {
423  vsl = 4;
424  }
425  }
426  st = FXMIN(hsl, vsl) / 4;
427  groove = st / 4;
428  if (st < 1) {
429  st = 1;
430  }
431  if (groove < 1) {
432  groove = 1;
433  }
434  if (options & LAYOUT_FILL_X) {
435  hsl -= groove << 1;
436  }
437  if (options & LAYOUT_FILL_Y) {
438  vsl -= groove << 1;
439  }
440  }
441  if (s1) {
442  x = sx + groove;
443  y = sy;
444  drawTopSegment(dc, x, y);
445  }
446  if (s2) {
447  x = sx;
448  y = sy + groove;
449  drawLeftTopSegment(dc, x, y);
450  }
451  if (s3) {
452  x = sx + groove + hsl - st + groove;
453  y = sy + groove;
454  drawRightTopSegment(dc, x, y);
455  }
456  if (s4) {
457  x = sx + groove;
458  y = sy + groove + vsl - (st >> 1) + groove;
459  drawMiddleSegment(dc, x, y);
460  }
461  if (s5) {
462  x = sx;
463  y = sy + (groove << 1) + vsl + groove;
464  drawLeftBottomSegment(dc, x, y);
465  }
466  if (s6) {
467  x = sx + groove + hsl - st + groove;
468  y = sy + (groove << 1) + vsl + groove;
469  drawRightBottomSegment(dc, x, y);
470  }
471  if (s7) {
472  x = sx + groove;
473  y = sy + (groove << 1) + vsl + groove + vsl + groove - st;
474  drawBottomSegment(dc, x, y);
475  }
476 }
477 
478 void FXSevenSegment::drawTopSegment(FXDCWindow& dc, FXshort x, FXshort y) {
479  FXPoint points[4];
480  points[0].x = x;
481  points[0].y = y;
482  points[1].x = x + hsl;
483  points[1].y = y;
484  points[2].x = x + hsl - st;
485  points[2].y = y + st;
486  points[3].x = x + st;
487  points[3].y = y + st;
488  dc.fillPolygon(points, 4);
489 }
490 
491 void FXSevenSegment::drawLeftTopSegment(FXDCWindow& dc, FXshort x, FXshort y) {
492  FXPoint points[4];
493  points[0].x = x;
494  points[0].y = y;
495  points[1].x = x + st;
496  points[1].y = y + st;
497  points[2].x = x + st;
498  points[2].y = y + vsl - (st >> 1);
499  points[3].x = x;
500  points[3].y = y + vsl;
501  dc.fillPolygon(points, 4);
502 }
503 
504 void FXSevenSegment::drawRightTopSegment(FXDCWindow& dc, FXshort x, FXshort y) {
505  FXPoint points[4];
506  points[0].x = x + st;
507  points[0].y = y;
508  points[1].x = x + st;
509  points[1].y = y + vsl;
510  points[2].x = x;
511  points[2].y = y + vsl - (st >> 1);
512  points[3].x = x;
513  points[3].y = y + st;
514  dc.fillPolygon(points, 4);
515 }
516 
517 void FXSevenSegment::drawMiddleSegment(FXDCWindow& dc, FXshort x, FXshort y) {
518  FXPoint points[6];
519  points[0].x = x + st;
520  points[0].y = y;
521  points[1].x = x + hsl - st;
522  points[1].y = y;
523  points[2].x = x + hsl;
524  points[2].y = y + (st >> 1);
525  points[3].x = x + hsl - st;
526  points[3].y = y + st;
527  points[4].x = x + st;
528  points[4].y = y + st;
529  points[5].x = x;
530  points[5].y = y + (st >> 1);
531  dc.fillPolygon(points, 6);
532 }
533 
534 void FXSevenSegment::drawLeftBottomSegment(FXDCWindow& dc, FXshort x, FXshort y) {
535  FXPoint points[4];
536  points[0].x = x;
537  points[0].y = y;
538  points[1].x = x + st;
539  points[1].y = y + (st >> 1);
540  points[2].x = x + st;
541  points[2].y = y + vsl - st;
542  points[3].x = x;
543  points[3].y = y + vsl;
544  dc.fillPolygon(points, 4);
545 }
546 
547 void FXSevenSegment::drawRightBottomSegment(FXDCWindow& dc, FXshort x, FXshort y) {
548  FXPoint points[4];
549  points[0].x = x + st;
550  points[0].y = y;
551  points[1].x = x + st;
552  points[1].y = y + vsl;
553  points[2].x = x;
554  points[2].y = y + vsl - st;
555  points[3].x = x;
556  points[3].y = y + (st >> 1);
557  dc.fillPolygon(points, 4);
558 }
559 
560 void FXSevenSegment::drawBottomSegment(FXDCWindow& dc, FXshort x, FXshort y) {
561  FXPoint points[4];
562  points[0].x = x + st;
563  points[0].y = y;
564  points[1].x = x + hsl - st;
565  points[1].y = y;
566  points[2].x = x + hsl;
567  points[2].y = y + st;
568  points[3].x = x;
569  points[3].y = y + st;
570  dc.fillPolygon(points, 4);
571 }
572 
573 void FXSevenSegment::save(FXStream& store) const {
574  FXFrame::save(store);
575  store << value;
576  store << fgcolor;
577  store << bgcolor;
578  store << hsl;
579  store << vsl;
580  store << st;
581  store << groove;
582 }
583 
584 void FXSevenSegment::load(FXStream& store) {
585  FXFrame::load(store);
586  store >> value;
587  store >> fgcolor;
588  store >> bgcolor;
589  store >> hsl;
590  store >> vsl;
591  store >> st;
592  store >> groove;
593 }
594 
595 // let parent show tip if appropriate
596 long FXSevenSegment::onQueryTip(FXObject* sender, FXSelector sel, void* ptr) {
597  if (getParent()) {
598  return getParent()->handle(sender, sel, ptr);
599  }
600  return 0;
601 }
602 
603 // let parent show help if appropriate
604 long FXSevenSegment::onQueryHelp(FXObject* sender, FXSelector sel, void* ptr) {
605  if (getParent()) {
606  return getParent()->handle(sender, sel, ptr);
607  }
608  return 0;
609 }
610 
611 }
612 
FXDEFMAP(FXRealSpinDialDial) FXSpinDialMap[]
#define ASCII_ZERO
MSNet * load(OptionsCont &oc)
Definition: sumo_main.cpp:86