FXLCDLabel.cpp

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  LCD text label widget                                        *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003 by Mathew Robertson.   All Rights Reserved.                *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (at your option) any later version.            *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************/
00022 /* =========================================================================
00023  * included modules
00024  * ======================================================================= */
00025 #ifdef _MSC_VER
00026 #include <windows_config.h>
00027 #else
00028 #include <config.h>
00029 #endif
00030 
00031 #include <fxver.h>
00032 #include <xincs.h>
00033 #include <fxdefs.h>
00034 #include <fx.h>
00035 /*
00036 #include <FXStream.h>
00037 #include <FXString.h>
00038 #include <FXSize.h>
00039 #include <FXPoint.h>
00040 #include <FXRectangle.h>
00041 #include <FXRegistry.h>
00042 #include <FXHash.h>
00043 #include <FXApp.h>
00044 #include <FXDCWindow.h>
00045 #include <FXLabel.h>
00046 #include <FXFrame.h>
00047 */
00048 using namespace FX;
00049 #include "FXSevenSegment.h"
00050 #include "FXLCDLabel.h"
00051 #include "FXBaseObject.h"
00052 
00053 #ifdef CHECK_MEMORY_LEAKS
00054 #include <foreign/nvwa/debug_new.h>
00055 #endif // CHECK_MEMORY_LEAKS
00056 using namespace FXEX;
00057 namespace FXEX {
00058 
00059 FXDEFMAP(FXLCDLabel) FXLCDLabelMap[]={
00060     FXMAPFUNC(SEL_PAINT,0,FXLCDLabel::onPaint),
00061     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETVALUE,FXLCDLabel::onCmdSetValue),
00062     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETINTVALUE,FXLCDLabel::onCmdSetIntValue),
00063     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETREALVALUE,FXLCDLabel::onCmdSetRealValue),
00064     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_SETSTRINGVALUE,FXLCDLabel::onCmdSetStringValue),
00065     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_GETINTVALUE,FXLCDLabel::onCmdGetIntValue),
00066     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_GETREALVALUE,FXLCDLabel::onCmdGetRealValue),
00067     FXMAPFUNC(SEL_COMMAND,FXWindow::ID_GETSTRINGVALUE,FXLCDLabel::onCmdGetStringValue),
00068     //  FXMAPFUNC(SEL_UPDATE,FXWindow::ID_QUERY_TIP,FXLCDLabel::onQueryTip),
00069     //  FXMAPFUNC(SEL_UPDATE,FXWindow::ID_QUERY_HELP,FXLCDLabel::onQueryHelp),
00070     FXMAPKEY(FXLCDLabel::ID_SEVENSEGMENT,FXLCDLabel::onRedirectEvent),
00071 };
00072 FXIMPLEMENT(FXLCDLabel,FXHorizontalFrame,FXLCDLabelMap,ARRAYNUMBER(FXLCDLabelMap))
00073 
00074 FXLCDLabel::FXLCDLabel(FXComposite* p,FXuint nfig,FXObject* tgt,FXSelector sel,FXuint opts,FXint pl,FXint pr,FXint pt,FXint pb,FXint hs) : FXHorizontalFrame(p,opts,0,0,0,0,pl,pr,pt,pb,hs,0) {
00075     if (nfig==0) fxerror("%s: must have at least one figure.\n",getClassName());
00076     nfigures=nfig;
00077     setTarget(tgt);
00078     setSelector(sel);
00079     enable();
00080     for (FXint i=0; i<nfigures; i++) {
00081         new FXSevenSegment(this,this,ID_SEVENSEGMENT,0,0,0,0);
00082     }
00083 }
00084 
00085 FXLCDLabel::~FXLCDLabel() {
00086     /*
00087       for (FXSevenSegment *child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()){
00088         delete child;
00089         }
00090     */
00091 }
00092 
00093 // create resources
00094 void FXLCDLabel::create() {
00095     FXHorizontalFrame::create();
00096     for (FXWindow *child=getFirst(); child; child=child->getNext()) {
00097         child->create();
00098     }
00099 }
00100 
00101 // detach resources
00102 void FXLCDLabel::detach() {
00103     for (FXWindow *child=getFirst(); child; child=child->getNext()) {
00104         child->detach();
00105     }
00106     FXHorizontalFrame::detach();
00107 }
00108 
00109 // destroy resources
00110 void FXLCDLabel::destroy() {
00111     for (FXWindow *child=getFirst(); child; child=child->getNext()) {
00112         child->destroy();
00113     }
00114     FXHorizontalFrame::destroy();
00115 }
00116 
00117 // get the foreground color
00118 FXColor FXLCDLabel::getFgColor() const {
00119     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00120     return child->getFgColor();
00121 }
00122 
00123 // set the foreground color
00124 void FXLCDLabel::setFgColor(FXColor clr) {
00125     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00126     if (clr!=child->getFgColor()) {
00127         for (child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()) {
00128             child->setFgColor(clr);
00129         }
00130     }
00131 }
00132 
00133 // get the background color
00134 FXColor FXLCDLabel::getBgColor() const {
00135     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00136     return child->getBgColor();
00137 }
00138 
00139 // set the background color
00140 void FXLCDLabel::setBgColor(FXColor clr) {
00141     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00142     if (clr!=child->getBgColor()) {
00143         for (child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()) {
00144             child->setBgColor(clr);
00145         }
00146     }
00147 }
00148 
00149 // set the text
00150 void FXLCDLabel::setText(FXString lbl) {
00151     if (lbl!=label) {
00152         label=lbl;
00153         recalc();
00154         update();
00155     }
00156 }
00157 
00158 // get the length of the horizontal segments
00159 FXint FXLCDLabel::getHorizontal() const {
00160     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00161     return child->getHorizontal();
00162 }
00163 
00164 // set the length of the horizontal segments
00165 void FXLCDLabel::setHorizontal(const FXint len) {
00166     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00167     if (len!=child->getHorizontal()) {
00168         for (child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()) {
00169             child->setHorizontal(len);
00170         }
00171         recalc();
00172         update();
00173     }
00174 }
00175 
00176 // get the length of the vertical segments
00177 FXint FXLCDLabel::getVertical() const {
00178     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00179     return child->getVertical();
00180 }
00181 
00182 // set the length of the vertical segments
00183 void FXLCDLabel::setVertical(const FXint len) {
00184     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00185     if (len!=child->getVertical()) {
00186         for (child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()) {
00187             child->setVertical(len);
00188         }
00189         recalc();
00190         update();
00191     }
00192 }
00193 
00194 // get the width of the segments
00195 FXint FXLCDLabel::getThickness() const {
00196     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00197     return child->getThickness();
00198 }
00199 
00200 // set the width of the segments
00201 void FXLCDLabel::setThickness(const FXint width) {
00202     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00203     if (width!=child->getThickness()) {
00204         for (child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()) {
00205             child->setThickness(width);
00206         }
00207         recalc();
00208         update();
00209     }
00210 }
00211 
00212 // get the width of the segments
00213 FXint FXLCDLabel::getGroove() const {
00214     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00215     return child->getGroove();
00216 }
00217 
00218 // set the groove width
00219 void FXLCDLabel::setGroove(const FXint width) {
00220     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00221     if (width!=child->getGroove()) {
00222         for (child=(FXSevenSegment*)getFirst(); child; child=(FXSevenSegment*)child->getNext()) {
00223             child->setGroove(width);
00224         }
00225         recalc();
00226         update();
00227     }
00228 }
00229 
00230 // Update value from a message
00231 long FXLCDLabel::onCmdSetValue(FXObject*,FXSelector,void* ptr) {
00232     setText((const FXchar*)ptr);
00233     return 1;
00234 }
00235 
00236 // Update value from a message
00237 long FXLCDLabel::onCmdSetIntValue(FXObject*,FXSelector,void* ptr) {
00238     setText(FXStringVal(*((FXint*)ptr)));
00239     return 1;
00240 }
00241 
00242 // Update value from a message
00243 long FXLCDLabel::onCmdSetRealValue(FXObject*,FXSelector,void* ptr) {
00244     setText(FXStringVal(*((FXdouble*)ptr)));
00245     return 1;
00246 }
00247 
00248 // Update value from a message
00249 long FXLCDLabel::onCmdSetStringValue(FXObject*,FXSelector,void* ptr) {
00250     setText(*((FXString*)ptr));
00251     return 1;
00252 }
00253 
00254 // Obtain value from text field
00255 long FXLCDLabel::onCmdGetIntValue(FXObject*,FXSelector,void* ptr) {
00256     *((FXint*)ptr)=FXIntVal(getText());
00257     return 1;
00258 }
00259 
00260 // Obtain value from text field
00261 long FXLCDLabel::onCmdGetRealValue(FXObject*,FXSelector,void* ptr) {
00262     *((FXdouble*)ptr)=FXDoubleVal(getText());
00263     return 1;
00264 }
00265 
00266 // Obtain value from text field
00267 long FXLCDLabel::onCmdGetStringValue(FXObject*,FXSelector,void* ptr) {
00268     *((FXString*)ptr)=getText();
00269     return 1;
00270 }
00271 
00272 // handle paint event
00273 long FXLCDLabel::onPaint(FXObject*,FXSelector,void *ptr) {
00274     FXEvent *event= (FXEvent*) ptr;
00275     FXDCWindow dc(this, event);
00276     drawFrame(dc,0,0,width,height);
00277     FXSevenSegment *child = (FXSevenSegment*)getFirst();
00278     // Fill the background
00279     dc.setForeground(child->getBgColor());
00280     dc.fillRectangle(border, border, width-(border<<1), height-(border<<1));
00281     // Draw the current string
00282     dc.setForeground(child->getFgColor());
00283     drawString(label);
00284     return 1;
00285 }
00286 
00287 // draw a specified string/label
00288 void FXLCDLabel::drawString(const FXString& lbl) {
00289     FXint i=0;
00290     FXString displayString(' ',nfigures);
00291     if (options&LCDLABEL_LEADING_ZEROS && (FXIntVal(lbl) || lbl=="0")) {
00292         FXString txt = lbl;
00293         if (txt[0]=='-') {
00294             displayString.replace(0,'-');
00295             txt.erase(0);
00296             i=1;
00297         }
00298         for (; (i+txt.length())<nfigures; i++) {
00299             displayString.replace(i,'0');
00300         }
00301         displayString.insert(i,txt);
00302     } else if (options&JUSTIFY_RIGHT) {
00303         for (; (i+lbl.length())<nfigures; i++) {}
00304         displayString.insert(i,lbl);
00305     } else {
00306         displayString.insert(0,lbl);
00307     }
00308     displayString.trunc(nfigures);
00309     i=0;
00310 
00311     // FIXME: at the moment, if we resize the parent widget, we must use integer multiples
00312     //        of the SevenSegment width.  The problem is that it makes the padding on the
00313     //        RHS look wrong.  What we need to do is to extend the horizontal segment width
00314     //        for the last sevensegment, so as to fill the remaining space.
00315     FXSevenSegment *child=(FXSevenSegment*)getFirst();
00316     if (options&LAYOUT_FILL) {
00317         FXint width=this->width-padleft-padright-(border<<1);
00318         FXint height=this->height-padtop-padbottom-(border<<1);
00319         hspacing=FXMAX(width,height)/50;
00320         if (hspacing<1) hspacing=1;
00321         FXint hsl=(width-(nfigures-1)*hspacing)/nfigures;
00322         if (hsl<5) hsl=5;
00323         FXint vsl=height>>1;
00324         if (vsl<5) vsl=5;
00325         FXint st=FXMIN(hsl,vsl)/4;
00326         if (st<1) st=1;
00327         FXint groove=st/4;
00328         if (groove<1) groove=1;
00329         if (options&LAYOUT_FILL_X) {
00330             hsl-=groove<<1;
00331             for (; child; child=(FXSevenSegment*)child->getNext()) {
00332                 child->setHorizontal(hsl);
00333             }
00334             child=(FXSevenSegment*)getFirst();
00335         }
00336         if (options&LAYOUT_FILL_Y) {
00337             vsl-=groove<<1;
00338             for (; child; child=(FXSevenSegment*)child->getNext()) {
00339                 child->setVertical(vsl);
00340             }
00341             child=(FXSevenSegment*)getFirst();
00342         }
00343         for (; child; child=(FXSevenSegment*)child->getNext()) {
00344             child->setText(displayString[i++]);
00345             child->setGroove(groove);
00346             child->setThickness(st);
00347         }
00348     } else {
00349         for (; child; child=(FXSevenSegment*)child->getNext()) {
00350             child->setText(displayString[i++]);
00351         }
00352     }
00353 }
00354 
00355 // redirect events to main window
00356 long FXLCDLabel::onRedirectEvent(FXObject*,FXSelector sel,void *ptr) {
00357     FXuint seltype = FXSELTYPE(sel);
00358     if (isEnabled()) {
00359         if (target) target->handle(this,FXSEL(seltype,message),ptr);
00360     }
00361     return 1;
00362 }
00363 
00364 // return minimum width
00365 FXint FXLCDLabel::getDefaultWidth() {
00366     return padleft + getFirst()->getDefaultWidth()*nfigures + hspacing*(nfigures-1) + padright + (border<<1);
00367 }
00368 
00369 // return minimum height
00370 FXint FXLCDLabel::getDefaultHeight() {
00371     return padtop + getFirst()->getDefaultHeight() + padbottom + (border<<1);
00372 }
00373 
00374 // save resources
00375 void FXLCDLabel::save(FXStream &store) const {
00376     FXHorizontalFrame::save(store);
00377     store << label;
00378     store << nfigures;
00379 }
00380 
00381 // load resources
00382 void FXLCDLabel::load(FXStream &store) {
00383     FXHorizontalFrame::load(store);
00384     store >> label;
00385     store >> nfigures;
00386 }
00387 
00388 // let parent show tip if appropriate
00389 long FXLCDLabel::onQueryTip(FXObject *sender,FXSelector sel,void *ptr) {
00390     if (getParent()) return getParent()->handle(sender,sel,ptr);
00391     return 0;
00392 }
00393 
00394 // let parent show help if appropriate
00395 long FXLCDLabel::onQueryHelp(FXObject *sender,FXSelector sel,void *ptr) {
00396     if (getParent()) return getParent()->handle(sender,sel,ptr);
00397     return 0;
00398 }
00399 
00400 }
00401 

Generated on Wed May 5 00:06:29 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6