LCOV - code coverage report
Current view: top level - src/utils/foxtools - MFXLCDLabel.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 81 195 41.5 %
Date: 2024-04-27 15:34:54 Functions: 16 42 38.1 %

          Line data    Source code
       1             : /****************************************************************************/
       2             : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3             : // Copyright (C) 2004-2024 German Aerospace Center (DLR) and others.
       4             : // This program and the accompanying materials are made available under the
       5             : // terms of the Eclipse Public License 2.0 which is available at
       6             : // https://www.eclipse.org/legal/epl-2.0/
       7             : // This Source Code may also be made available under the following Secondary
       8             : // Licenses when the conditions for such availability set forth in the Eclipse
       9             : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10             : // or later which is available at
      11             : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12             : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13             : /****************************************************************************/
      14             : /// @file    MFXLCDLabel.cpp
      15             : /// @author  Mathew Robertson
      16             : /// @author  Daniel Krajzewicz
      17             : /// @author  Michael Behrisch
      18             : /// @date    2004-03-19
      19             : ///
      20             : //
      21             : /****************************************************************************/
      22             : 
      23             : 
      24             : /* =========================================================================
      25             :  * included modules
      26             :  * ======================================================================= */
      27             : #include <config.h>
      28             : 
      29             : #include "MFXSevenSegment.h"
      30             : #include "MFXLCDLabel.h"
      31             : #include "MFXBaseObject.h"
      32             : 
      33             : 
      34             : // ===========================================================================
      35             : // FOX callback mapping
      36             : // ===========================================================================
      37             : 
      38             : FXDEFMAP(MFXLCDLabel) MFXLCDLabelMap[] = {
      39             :     FXMAPFUNC(SEL_PAINT,        0,                              MFXLCDLabel::onPaint),
      40             :     FXMAPFUNC(SEL_ENTER,        0,                              MFXLCDLabel::onEnter),
      41             :     FXMAPFUNC(SEL_LEAVE,        0,                              MFXLCDLabel::onLeave),
      42             :     FXMAPFUNC(SEL_MOTION,       0,                              MFXLCDLabel::onMotion),
      43             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_SETVALUE,          MFXLCDLabel::onCmdSetValue),
      44             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_SETINTVALUE,       MFXLCDLabel::onCmdSetIntValue),
      45             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_SETREALVALUE,      MFXLCDLabel::onCmdSetRealValue),
      46             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_SETSTRINGVALUE,    MFXLCDLabel::onCmdSetStringValue),
      47             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_GETINTVALUE,       MFXLCDLabel::onCmdGetIntValue),
      48             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_GETREALVALUE,      MFXLCDLabel::onCmdGetRealValue),
      49             :     FXMAPFUNC(SEL_COMMAND,      FXWindow::ID_GETSTRINGVALUE,    MFXLCDLabel::onCmdGetStringValue),
      50             :     //FXMAPFUNC(SEL_UPDATE,     FXWindow::ID_QUERY_TIP,         MFXLCDLabel::onQueryTip),
      51             :     //FXMAPFUNC(SEL_UPDATE,     FXWindow::ID_QUERY_HELP,        MFXLCDLabel::onQueryHelp),
      52             :     // map key
      53             :     FXMAPKEY(MFXLCDLabel::ID_SEVENSEGMENT,  MFXLCDLabel::onRedirectEvent),
      54             : };
      55             : 
      56             : // Object implementation
      57   122431790 : FXIMPLEMENT(MFXLCDLabel, FXHorizontalFrame, MFXLCDLabelMap, ARRAYNUMBER(MFXLCDLabelMap))
      58             : 
      59             : 
      60             : // ===========================================================================
      61             : // method definitions
      62             : // ===========================================================================
      63             : 
      64       36270 : MFXLCDLabel::MFXLCDLabel(FXComposite* p, MFXStaticToolTip* staticToolTip, FXuint nfig, FXObject* tgt, FXSelector sel, FXuint opts, FXint pl, FXint pr, FXint pt, FXint pb, FXint hs) :
      65             :     FXHorizontalFrame(p, opts, 0, 0, 0, 0, pl, pr, pt, pb, hs, 0),
      66       36270 :     myNFigures(nfig),
      67       36270 :     myStaticToolTip(staticToolTip) {
      68       36270 :     if (nfig == 0) {
      69           0 :         fxerror("%s: must have at least one figure.\n", getClassName());
      70             :     }
      71             :     setTarget(tgt);
      72             :     setSelector(sel);
      73       36270 :     enable();
      74      529542 :     for (FXint i = 0; i < myNFigures; i++) {
      75      493272 :         new MFXSevenSegment(this, this, ID_SEVENSEGMENT, 0, 0, 0, 0);
      76             :     }
      77       36270 : }
      78             : 
      79             : 
      80       72370 : MFXLCDLabel::~MFXLCDLabel() {
      81             :     /*
      82             :       for (MFXSevenSegment *child=(MFXSevenSegment*)getFirst(); child; child=(MFXSevenSegment*)child->getNext()){
      83             :         delete child;
      84             :         }
      85             :     */
      86       72370 : }
      87             : 
      88             : 
      89             : void
      90       36270 : MFXLCDLabel::create() {
      91       36270 :     FXHorizontalFrame::create();
      92      529542 :     for (FXWindow* child = getFirst(); child; child = child->getNext()) {
      93      493272 :         child->create();
      94             :     }
      95       36270 : }
      96             : 
      97             : 
      98             : void
      99           0 : MFXLCDLabel::detach() {
     100           0 :     for (FXWindow* child = getFirst(); child; child = child->getNext()) {
     101           0 :         child->detach();
     102             :     }
     103           0 :     FXHorizontalFrame::detach();
     104           0 : }
     105             : 
     106             : 
     107             : void
     108           0 : MFXLCDLabel::destroy() {
     109           0 :     for (FXWindow* child = getFirst(); child; child = child->getNext()) {
     110           0 :         child->destroy();
     111             :     }
     112           0 :     FXHorizontalFrame::destroy();
     113           0 : }
     114             : 
     115             : 
     116             : FXColor
     117           0 : MFXLCDLabel::getFgColor() const {
     118             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     119           0 :     return child->getFgColor();
     120             : }
     121             : 
     122             : 
     123             : void
     124           0 : MFXLCDLabel::setFgColor(FXColor clr) {
     125             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     126           0 :     if (clr != child->getFgColor()) {
     127           0 :         for (child = (MFXSevenSegment*)getFirst(); child; child = (MFXSevenSegment*)child->getNext()) {
     128           0 :             child->setFgColor(clr);
     129             :         }
     130             :     }
     131           0 : }
     132             : 
     133             : 
     134             : FXColor
     135           0 : MFXLCDLabel::getBgColor() const {
     136             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     137           0 :     return child->getBgColor();
     138             : }
     139             : 
     140             : 
     141             : void
     142           0 : MFXLCDLabel::setBgColor(FXColor clr) {
     143             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     144           0 :     if (clr != child->getBgColor()) {
     145           0 :         for (child = (MFXSevenSegment*)getFirst(); child; child = (MFXSevenSegment*)child->getNext()) {
     146           0 :             child->setBgColor(clr);
     147             :         }
     148             :     }
     149           0 : }
     150             : 
     151             : 
     152             : void
     153     5381840 : MFXLCDLabel::setText(FXString lbl) {
     154     5381840 :     if (lbl != myLabel) {
     155     2013047 :         myLabel = lbl;
     156     2013047 :         recalc();
     157     2013047 :         update();
     158             :     }
     159     5381840 : }
     160             : 
     161             : 
     162             : FXString
     163           0 : MFXLCDLabel::getText() const {
     164           0 :     return myLabel;
     165             : }
     166             : 
     167             : 
     168             : FXint
     169           0 : MFXLCDLabel::getHorizontal() const {
     170             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     171           0 :     return child->getHorizontal();
     172             : }
     173             : 
     174             : 
     175             : void
     176       36270 : MFXLCDLabel::setHorizontal(const FXint len) {
     177             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     178       36270 :     if (len != child->getHorizontal()) {
     179      529542 :         for (child = (MFXSevenSegment*)getFirst(); child; child = (MFXSevenSegment*)child->getNext()) {
     180      493272 :             child->setHorizontal(len);
     181             :         }
     182       36270 :         recalc();
     183       36270 :         update();
     184             :     }
     185       36270 : }
     186             : 
     187             : 
     188             : FXint
     189           0 : MFXLCDLabel::getVertical() const {
     190             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     191           0 :     return child->getVertical();
     192             : }
     193             : 
     194             : 
     195             : void
     196       36270 : MFXLCDLabel::setVertical(const FXint len) {
     197             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     198       36270 :     if (len != child->getVertical()) {
     199      529542 :         for (child = (MFXSevenSegment*)getFirst(); child; child = (MFXSevenSegment*)child->getNext()) {
     200      493272 :             child->setVertical(len);
     201             :         }
     202       36270 :         recalc();
     203       36270 :         update();
     204             :     }
     205       36270 : }
     206             : 
     207             : 
     208             : FXint
     209           0 : MFXLCDLabel::getThickness() const {
     210             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     211           0 :     return child->getThickness();
     212             : }
     213             : 
     214             : 
     215             : void
     216       36270 : MFXLCDLabel::setThickness(const FXint w) {
     217             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     218       36270 :     if (w != child->getThickness()) {
     219      529542 :         for (child = (MFXSevenSegment*)getFirst(); child; child = (MFXSevenSegment*)child->getNext()) {
     220      493272 :             child->setThickness(w);
     221             :         }
     222       36270 :         recalc();
     223       36270 :         update();
     224             :     }
     225       36270 : }
     226             : 
     227             : 
     228             : FXint
     229           0 : MFXLCDLabel::getGroove() const {
     230             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     231           0 :     return child->getGroove();
     232             : }
     233             : 
     234             : 
     235             : void
     236       36270 : MFXLCDLabel::setGroove(const FXint w) {
     237             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     238       36270 :     if (w != child->getGroove()) {
     239      529542 :         for (child = (MFXSevenSegment*)getFirst(); child; child = (MFXSevenSegment*)child->getNext()) {
     240      493272 :             child->setGroove(w);
     241             :         }
     242       36270 :         recalc();
     243       36270 :         update();
     244             :     }
     245       36270 : }
     246             : 
     247             : 
     248             : long
     249           0 : MFXLCDLabel::onCmdSetValue(FXObject*, FXSelector, void* ptr) {
     250           0 :     setText((const FXchar*)ptr);
     251           0 :     return 1;
     252             : }
     253             : 
     254             : 
     255             : long
     256           0 : MFXLCDLabel::onCmdSetIntValue(FXObject*, FXSelector, void* ptr) {
     257           0 :     setText(FXStringVal(*((FXint*)ptr)));
     258           0 :     return 1;
     259             : }
     260             : 
     261             : 
     262             : long
     263           0 : MFXLCDLabel::onCmdSetRealValue(FXObject*, FXSelector, void* ptr) {
     264           0 :     setText(FXStringVal(*((FXdouble*)ptr)));
     265           0 :     return 1;
     266             : }
     267             : 
     268             : 
     269             : long
     270           0 : MFXLCDLabel::onCmdSetStringValue(FXObject*, FXSelector, void* ptr) {
     271           0 :     setText(*((FXString*)ptr));
     272           0 :     return 1;
     273             : }
     274             : 
     275             : 
     276             : long
     277           0 : MFXLCDLabel::onCmdGetIntValue(FXObject*, FXSelector, void* ptr) {
     278           0 :     *((FXint*)ptr) = FXIntVal(getText());
     279           0 :     return 1;
     280             : }
     281             : 
     282             : 
     283             : long
     284           0 : MFXLCDLabel::onCmdGetRealValue(FXObject*, FXSelector, void* ptr) {
     285           0 :     *((FXdouble*)ptr) = FXDoubleVal(getText());
     286           0 :     return 1;
     287             : }
     288             : 
     289             : 
     290             : long
     291           0 : MFXLCDLabel::onCmdGetStringValue(FXObject*, FXSelector, void* ptr) {
     292           0 :     *((FXString*)ptr) = getText();
     293           0 :     return 1;
     294             : }
     295             : 
     296             : 
     297             : long
     298      758057 : MFXLCDLabel::onPaint(FXObject*, FXSelector, void* ptr) {
     299             :     FXEvent* event = (FXEvent*) ptr;
     300      758057 :     FXDCWindow dc(this, event);
     301      758057 :     drawFrame(dc, 0, 0, width, height);
     302             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     303             :     // Fill the background
     304      758057 :     dc.setForeground(child->getBgColor());
     305      758057 :     dc.fillRectangle(border, border, width - (border << 1), height - (border << 1));
     306             :     // Draw the current string
     307      758057 :     dc.setForeground(child->getFgColor());
     308      758057 :     drawString(myLabel);
     309      758057 :     return 1;
     310      758057 : }
     311             : 
     312             : 
     313             : long
     314           0 : MFXLCDLabel::onEnter(FXObject* obj, FXSelector sel, void* ptr) {
     315             :     // show static toolTip depending of myToolTipText
     316           0 :     if (!myToolTipText.empty()) {
     317             :         // show tip show
     318           0 :         myStaticToolTip->showStaticToolTip(myToolTipText);
     319             :     } else {
     320           0 :         myStaticToolTip->hideStaticToolTip();
     321             :     }
     322           0 :     return FXHorizontalFrame::onEnter(obj, sel, ptr);
     323             : }
     324             : 
     325             : 
     326             : long
     327           0 : MFXLCDLabel::onLeave(FXObject* obj, FXSelector sel, void* ptr) {
     328             :     // hide static toolTip
     329           0 :     myStaticToolTip->hideStaticToolTip();
     330           0 :     return FXHorizontalFrame::onLeave(obj, sel, ptr);
     331             : }
     332             : 
     333             : 
     334             : long
     335           0 : MFXLCDLabel::onMotion(FXObject* sender, FXSelector sel, void* ptr) {
     336             :     // update static tooltip
     337           0 :     myStaticToolTip->onUpdate(sender, sel, ptr);
     338           0 :     return FXHorizontalFrame::onMotion(sender, sel, ptr);
     339             : }
     340             : 
     341             : 
     342             : long
     343   121673733 : MFXLCDLabel::onRedirectEvent(FXObject*, FXSelector sel, void* ptr) {
     344             :     FXuint seltype = FXSELTYPE(sel);
     345   121673733 :     if (isEnabled()) {
     346   121673733 :         if (target) {
     347           0 :             target->handle(this, FXSEL(seltype, message), ptr);
     348             :         }
     349             :     }
     350   121673733 :     return 1;
     351             : }
     352             : 
     353             : 
     354             : FXint
     355     1419664 : MFXLCDLabel::getDefaultWidth() {
     356     1419664 :     return padleft + getFirst()->getDefaultWidth() * myNFigures + hspacing * (myNFigures - 1) + padright + (border << 1);
     357             : }
     358             : 
     359             : 
     360             : FXint
     361     1140315 : MFXLCDLabel::getDefaultHeight() {
     362     1140315 :     return padtop + getFirst()->getDefaultHeight() + padbottom + (border << 1);
     363             : }
     364             : 
     365             : 
     366             : void
     367        7254 : MFXLCDLabel::setToolTipText(const FXString& text) {
     368        7254 :     myToolTipText = text;
     369        7254 : }
     370             : 
     371             : 
     372             : void
     373           0 : MFXLCDLabel::save(FXStream& store) const {
     374           0 :     FXHorizontalFrame::save(store);
     375           0 :     store << myLabel;
     376           0 :     store << myNFigures;
     377           0 : }
     378             : 
     379             : 
     380             : void
     381           0 : MFXLCDLabel::load(FXStream& store) {
     382           0 :     FXHorizontalFrame::load(store);
     383           0 :     store >> myLabel;
     384           0 :     store >> myNFigures;
     385           0 : }
     386             : 
     387             : 
     388             : long
     389           0 : MFXLCDLabel::onQueryTip(FXObject* sender, FXSelector sel, void* ptr) {
     390           0 :     if (getParent()) {
     391           0 :         return getParent()->handle(sender, sel, ptr);
     392             :     }
     393             :     return 0;
     394             : }
     395             : 
     396             : 
     397             : long
     398           0 : MFXLCDLabel::onQueryHelp(FXObject* sender, FXSelector sel, void* ptr) {
     399           0 :     if (getParent()) {
     400           0 :         return getParent()->handle(sender, sel, ptr);
     401             :     }
     402             :     return 0;
     403             : }
     404             : 
     405             : 
     406             : void
     407      758057 : MFXLCDLabel::drawString(const FXString& lbl) {
     408             :     FXint i = 0;
     409      758057 :     FXString displayString(' ', myNFigures);
     410      758057 :     if (options & LCDLABEL_LEADING_ZEROS && (FXIntVal(lbl) || lbl == "0")) {
     411           0 :         FXString txt = lbl;
     412           0 :         if (txt[0] == '-') {
     413           0 :             displayString.replace(0, '-');
     414           0 :             txt.erase(0);
     415             :             i = 1;
     416             :         }
     417           0 :         for (; (i + txt.length()) < myNFigures; i++) {
     418           0 :             displayString.replace(i, '0');
     419             :         }
     420           0 :         displayString.insert(i, txt);
     421      758057 :     } else if (options & JUSTIFY_RIGHT) {
     422     7915627 :         for (; (i + lbl.length()) < myNFigures; i++) {}
     423      758057 :         displayString.insert(i, lbl);
     424             :     } else {
     425           0 :         displayString.insert(0, lbl);
     426             :     }
     427      758057 :     displayString.trunc(myNFigures);
     428             :     i = 0;
     429             : 
     430             :     // FIXME: at the moment, if we resize the parent widget, we must use integer multiples
     431             :     //        of the SevenSegment width.  The problem is that it makes the padding on the
     432             :     //        RHS look wrong.  What we need to do is to extend the horizontal segment width
     433             :     //        for the last sevensegment, so as to fill the remaining space.
     434             :     MFXSevenSegment* child = (MFXSevenSegment*)getFirst();
     435      758057 :     if (options & LAYOUT_FILL) {
     436           0 :         const FXint w = this->width - padleft - padright - (border << 1);
     437           0 :         const FXint h = this->height - padtop - padbottom - (border << 1);
     438           0 :         hspacing = FXMAX(w, h) / 50;
     439           0 :         if (hspacing < 1) {
     440           0 :             hspacing = 1;
     441             :         }
     442           0 :         FXint hsl = (w - (myNFigures - 1) * hspacing) / myNFigures;
     443             :         if (hsl < 5) {
     444             :             hsl = 5;
     445             :         }
     446           0 :         FXint vsl = h >> 1;
     447             :         if (vsl < 5) {
     448             :             vsl = 5;
     449             :         }
     450           0 :         FXint st = FXMIN(hsl, vsl) / 4;
     451             :         if (st < 1) {
     452             :             st = 1;
     453             :         }
     454           0 :         FXint groove = st / 4;
     455           0 :         if (groove < 1) {
     456             :             groove = 1;
     457             :         }
     458           0 :         if (options & LAYOUT_FILL_X) {
     459           0 :             hsl -= groove << 1;
     460           0 :             for (; child; child = (MFXSevenSegment*)child->getNext()) {
     461           0 :                 child->setHorizontal(hsl);
     462             :             }
     463             :             child = (MFXSevenSegment*)getFirst();
     464             :         }
     465           0 :         if (options & LAYOUT_FILL_Y) {
     466           0 :             vsl -= groove << 1;
     467           0 :             for (; child; child = (MFXSevenSegment*)child->getNext()) {
     468           0 :                 child->setVertical(vsl);
     469             :             }
     470             :             child = (MFXSevenSegment*)getFirst();
     471             :         }
     472           0 :         for (; child; child = (MFXSevenSegment*)child->getNext()) {
     473           0 :             child->setText(displayString[i++]);
     474           0 :             child->setGroove(groove);
     475           0 :             child->setThickness(st);
     476             :         }
     477             :     } else {
     478    12886969 :         for (; child; child = (MFXSevenSegment*)child->getNext()) {
     479    12128912 :             child->setText(displayString[i++]);
     480             :         }
     481             :     }
     482      758057 : }

Generated by: LCOV version 1.14