libyui-ncurses  2.44.1
/usr/src/RPM/BUILD/libyui-ncurses-2.44.1/src/NCProgressBar.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019    File:       NCProgressBar.cc
00020 
00021    Author:     Michael Andres <ma@suse.de>
00022 
00023 /-*/
00024 
00025 #define  YUILogComponent "ncurses"
00026 #include <yui/YUILog.h>
00027 #include "NCurses.h"
00028 #include "NCProgressBar.h"
00029 
00030 
00031 NCProgressBar::NCProgressBar( YWidget * parent,
00032                               const std::string & nlabel,
00033                               int maxValue )
00034         : YProgressBar( parent, nlabel, maxValue )
00035         , NCWidget( parent )
00036         , label( nlabel )
00037         , maxval( maxValue )
00038         , cval( 0 )
00039         , lwin( 0 )
00040         , twin( 0 )
00041 {
00042     yuiDebug() << std::endl;
00043 
00044     if ( maxval <= 0 )
00045         maxval = 1;
00046 
00047     hotlabel = &label;
00048 
00049     setLabel( nlabel );
00050 
00051     // initial progress isn't an argument any longer
00052     //setProgress( progress );
00053     wstate = NC::WSdumb;
00054 }
00055 
00056 
00057 NCProgressBar::~NCProgressBar()
00058 {
00059     delete lwin;
00060     delete twin;
00061     yuiDebug() << std::endl;
00062 }
00063 
00064 
00065 int NCProgressBar::preferredWidth()
00066 {
00067     return wGetDefsze().W;
00068 }
00069 
00070 
00071 int NCProgressBar::preferredHeight()
00072 {
00073     return wGetDefsze().H;
00074 }
00075 
00076 
00077 void NCProgressBar::setEnabled( bool do_bv )
00078 {
00079     NCWidget::setEnabled( do_bv );
00080     YProgressBar::setEnabled( do_bv );
00081 }
00082 
00083 
00084 void NCProgressBar::setSize( int newwidth, int newheight )
00085 {
00086     wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
00087 }
00088 
00089 
00090 void NCProgressBar::setDefsze()
00091 {
00092     defsze = wsze( label.height() + 1,
00093                    label.width() < 5 ? 5 : label.width() );
00094 }
00095 
00096 
00097 void NCProgressBar::wCreate( const wrect & newrect )
00098 {
00099     NCWidget::wCreate( newrect );
00100 
00101     if ( !win )
00102         return;
00103 
00104     wrect lrect( 0, wsze::min( newrect.Sze,
00105                                wsze( label.height(), newrect.Sze.W ) ) );
00106 
00107     wrect trect( 0, wsze( 1, newrect.Sze.W ) );
00108 
00109     if ( lrect.Sze.H == newrect.Sze.H )
00110         lrect.Sze.H -= 1;
00111 
00112     trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
00113 
00114     lwin = new NCursesWindow( *win,
00115                               lrect.Sze.H, lrect.Sze.W,
00116                               lrect.Pos.L, lrect.Pos.C,
00117                               'r' );
00118 
00119     twin = new NCursesWindow( *win,
00120                               trect.Sze.H, trect.Sze.W,
00121                               trect.Pos.L, trect.Pos.C,
00122                               'r' );
00123 }
00124 
00125 
00126 void NCProgressBar::wDelete()
00127 {
00128     delete lwin;
00129     delete twin;
00130     lwin = 0;
00131     twin = 0;
00132     NCWidget::wDelete();
00133 }
00134 
00135 
00136 void NCProgressBar::setLabel( const std::string & nlabel )
00137 {
00138     label = NCstring( nlabel );
00139     setDefsze();
00140     YProgressBar::setLabel( nlabel );
00141     Redraw();
00142 }
00143 
00144 
00145 void NCProgressBar::setValue( int newValue )
00146 {
00147     cval = newValue;
00148 
00149     if ( cval < 0 )
00150         cval = 0;
00151     else if ( cval > maxval )
00152         cval = maxval;
00153 
00154     Redraw();
00155 
00156     YProgressBar::setValue( newValue );
00157 }
00158 
00159 
00160 void NCProgressBar::wRedraw()
00161 {
00162     if ( !win )
00163         return;
00164 
00165     // label
00166     chtype bg = wStyle().dumb.text;
00167 
00168     lwin->bkgdset( bg );
00169 
00170     lwin->clear();
00171 
00172     label.drawAt( *lwin, bg, bg );
00173 
00174     tUpdate();
00175 }
00176 
00177 
00178 void NCProgressBar::tUpdate()
00179 {
00180     if ( !win )
00181         return;
00182 
00183     double split = double( twin->maxx() + 1 ) * cval / maxval;
00184 
00185     int cp = int( split );
00186 
00187     if ( cp == 0 && split > 0.0 )
00188         cp = 1;
00189 
00190     const NCstyle::StProgbar & style( wStyle().progbar );
00191 
00192     twin->bkgdset( style.bar.chattr );
00193 
00194     twin->clear();
00195 
00196     if ( cp <= twin->maxx() )
00197     {
00198         twin->bkgdset( NCattribute::getNonChar( style.nonbar.chattr ) );
00199         twin->move( 0, cp );
00200 
00201         for ( int i = 0; i < twin->width() - cp; ++i )
00202         {
00203             twin->addch( NCattribute::getChar( style.nonbar.chattr ) );
00204         }
00205     }
00206 
00207     if ( twin->maxx() >= 6 )
00208     {
00209         Value_t pc  = 100 * cval / maxval;
00210         Value_t off = twin->maxx() / 2 - ( pc == 100 ? 2
00211                                            : pc >= 10 ? 1
00212                                            : 0 );
00213         char buf[5];
00214         sprintf( buf, "%lld%%", pc );
00215         twin->move( 0, off );
00216 
00217         for ( char * ch = buf; *ch; ++ch )
00218         {
00219             chtype a = twin->inch();
00220             NCattribute::setChar( a, *ch );
00221             twin->addch( a );
00222         }
00223     }
00224 }
 All Classes Functions Variables