libyui-ncurses  2.44.1
 All Classes Functions Variables
NCProgressBar.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCProgressBar.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCurses.h"
28 #include "NCProgressBar.h"
29 
30 
31 NCProgressBar::NCProgressBar( YWidget * parent,
32  const std::string & nlabel,
33  int maxValue )
34  : YProgressBar( parent, nlabel, maxValue )
35  , NCWidget( parent )
36  , label( nlabel )
37  , maxval( maxValue )
38  , cval( 0 )
39  , lwin( 0 )
40  , twin( 0 )
41 {
42  yuiDebug() << std::endl;
43 
44  if ( maxval <= 0 )
45  maxval = 1;
46 
47  hotlabel = &label;
48 
49  setLabel( nlabel );
50 
51  // initial progress isn't an argument any longer
52  //setProgress( progress );
53  wstate = NC::WSdumb;
54 }
55 
56 
57 NCProgressBar::~NCProgressBar()
58 {
59  delete lwin;
60  delete twin;
61  yuiDebug() << std::endl;
62 }
63 
64 
65 int NCProgressBar::preferredWidth()
66 {
67  return wGetDefsze().W;
68 }
69 
70 
71 int NCProgressBar::preferredHeight()
72 {
73  return wGetDefsze().H;
74 }
75 
76 
77 void NCProgressBar::setEnabled( bool do_bv )
78 {
79  NCWidget::setEnabled( do_bv );
80  YProgressBar::setEnabled( do_bv );
81 }
82 
83 
84 void NCProgressBar::setSize( int newwidth, int newheight )
85 {
86  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
87 }
88 
89 
90 void NCProgressBar::setDefsze()
91 {
92  defsze = wsze( label.height() + 1,
93  label.width() < 5 ? 5 : label.width() );
94 }
95 
96 
97 void NCProgressBar::wCreate( const wrect & newrect )
98 {
99  NCWidget::wCreate( newrect );
100 
101  if ( !win )
102  return;
103 
104  wrect lrect( 0, wsze::min( newrect.Sze,
105  wsze( label.height(), newrect.Sze.W ) ) );
106 
107  wrect trect( 0, wsze( 1, newrect.Sze.W ) );
108 
109  if ( lrect.Sze.H == newrect.Sze.H )
110  lrect.Sze.H -= 1;
111 
112  trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
113 
114  lwin = new NCursesWindow( *win,
115  lrect.Sze.H, lrect.Sze.W,
116  lrect.Pos.L, lrect.Pos.C,
117  'r' );
118 
119  twin = new NCursesWindow( *win,
120  trect.Sze.H, trect.Sze.W,
121  trect.Pos.L, trect.Pos.C,
122  'r' );
123 }
124 
125 
126 void NCProgressBar::wDelete()
127 {
128  delete lwin;
129  delete twin;
130  lwin = 0;
131  twin = 0;
132  NCWidget::wDelete();
133 }
134 
135 
136 void NCProgressBar::setLabel( const std::string & nlabel )
137 {
138  label = NCstring( nlabel );
139  setDefsze();
140  YProgressBar::setLabel( nlabel );
141  Redraw();
142 }
143 
144 
145 void NCProgressBar::setValue( int newValue )
146 {
147  cval = newValue;
148 
149  if ( cval < 0 )
150  cval = 0;
151  else if ( cval > maxval )
152  cval = maxval;
153 
154  Redraw();
155 
156  YProgressBar::setValue( newValue );
157 }
158 
159 
160 void NCProgressBar::wRedraw()
161 {
162  if ( !win )
163  return;
164 
165  // label
166  chtype bg = wStyle().dumb.text;
167 
168  lwin->bkgdset( bg );
169 
170  lwin->clear();
171 
172  label.drawAt( *lwin, bg, bg );
173 
174  tUpdate();
175 }
176 
177 
178 void NCProgressBar::tUpdate()
179 {
180  if ( !win )
181  return;
182 
183  double split = double( twin->maxx() + 1 ) * cval / maxval;
184 
185  int cp = int( split );
186 
187  if ( cp == 0 && split > 0.0 )
188  cp = 1;
189 
190  const NCstyle::StProgbar & style( wStyle().progbar );
191 
192  twin->bkgdset( style.bar.chattr );
193 
194  twin->clear();
195 
196  if ( cp <= twin->maxx() )
197  {
198  twin->bkgdset( NCattribute::getNonChar( style.nonbar.chattr ) );
199  twin->move( 0, cp );
200 
201  for ( int i = 0; i < twin->width() - cp; ++i )
202  {
203  twin->addch( NCattribute::getChar( style.nonbar.chattr ) );
204  }
205  }
206 
207  if ( twin->maxx() >= 6 )
208  {
209  Value_t pc = 100 * cval / maxval;
210  Value_t off = twin->maxx() / 2 - ( pc == 100 ? 2
211  : pc >= 10 ? 1
212  : 0 );
213  char buf[5];
214  sprintf( buf, "%lld%%", pc );
215  twin->move( 0, off );
216 
217  for ( char * ch = buf; *ch; ++ch )
218  {
219  chtype a = twin->inch();
220  NCattribute::setChar( a, *ch );
221  twin->addch( a );
222  }
223  }
224 }
C++ class for windows.
Definition: ncursesw.h:904
void bkgdset(chtype ch)
Definition: ncursesw.h:1448
int maxx() const
Definition: ncursesw.h:1090
Definition: position.h:109
int addch(const char ch)
Definition: ncursesw.h:1228
virtual void setEnabled(bool do_bv)
int move(int y, int x)
Definition: ncursesw.h:1155
virtual void setEnabled(bool do_bv)=0
Definition: NCWidget.cc:391
Definition: position.h:154
chtype inch() const
Definition: ncursesw.h:1325
int width() const
Definition: ncursesw.h:1075