libyui-ncurses  2.44.1
 All Classes Functions Variables
NCIntField.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: NCIntField.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 "NCIntField.h"
29 #include "NCPopupTextEntry.h"
30 #include "stringutil.h"
31 #include "stdutil.h"
32 
33 using stdutil::numstring;
34 
35 const unsigned NCIntField::taglen = 2; // "^v"
36 
37 
38 NCIntField::NCIntField( YWidget * parent,
39  const std::string & nlabel,
40  int minV, int maxV,
41  int initialV )
42  : YIntField( parent, nlabel,
43  minV <= maxV ? minV : maxV,
44  maxV >= minV ? maxV : minV )
45  , NCWidget( parent )
46  , lwin( 0 )
47  , twin( 0 )
48  , cvalue( initialV )
49  , vlen( 0 )
50  , vstart( 0 )
51 {
52  yuiDebug() << std::endl;
53  vlen = numstring( minValue() ).length();
54  unsigned tmpval = numstring( maxValue() ).length();
55 
56  if ( tmpval > vlen )
57  vlen = tmpval;
58 
59  setLabel( nlabel );
60  hotlabel = &label;
61  setValue( initialV );
62 }
63 
64 
65 NCIntField::~NCIntField()
66 {
67  delete lwin;
68  delete twin;
69  yuiDebug() << std::endl;
70 }
71 
72 
73 int NCIntField::preferredWidth()
74 {
75  return wGetDefsze().W;
76 }
77 
78 
79 int NCIntField::preferredHeight()
80 {
81  return wGetDefsze().H;
82 }
83 
84 
85 void NCIntField::setEnabled( bool do_bv )
86 {
87  NCWidget::setEnabled( do_bv );
88  YIntField::setEnabled( do_bv );
89 }
90 
91 
92 void NCIntField::setSize( int newwidth, int newheight )
93 {
94  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
95 }
96 
97 
98 void NCIntField::setDefsze()
99 {
100  unsigned cols = vlen + taglen;
101  defsze = wsze( label.height() + 1,
102  label.width() < cols ? cols : label.width() );
103 }
104 
105 
106 void NCIntField::wCreate( const wrect & newrect )
107 {
108  NCWidget::wCreate( newrect );
109 
110  if ( !win )
111  return;
112 
113  wrect lrect( 0, wsze::min( newrect.Sze,
114  wsze( label.height(), newrect.Sze.W ) ) );
115 
116  wrect trect( 0, wsze( 1, newrect.Sze.W ) );
117 
118  if ( lrect.Sze.H == newrect.Sze.H )
119  lrect.Sze.H -= 1;
120 
121  trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
122 
123  lwin = new NCursesWindow( *win,
124  lrect.Sze.H, lrect.Sze.W,
125  lrect.Pos.L, lrect.Pos.C,
126  'r' );
127 
128  twin = new NCursesWindow( *win,
129  trect.Sze.H, trect.Sze.W,
130  trect.Pos.L, trect.Pos.C,
131  'r' );
132 
133  //vstart = ( vlen + 2 < ( unsigned )trect.Sze.W ) ? label.width() - vlen - 2 : 0;
134  vstart = 0;
135  // vstart is calculated from label width only if value length (+ tags) is smaller
136  // than window width AND smaller than label width, otherwise start with 0
137  // (bug #488757)
138  if ( vlen + 2 < ( unsigned )trect.Sze.W && vlen + 2 < label.width() )
139  {
140  vstart = label.width() - vlen - 2;
141  }
142 }
143 
144 
145 void NCIntField::wDelete()
146 {
147  delete lwin;
148  delete twin;
149  lwin = 0;
150  twin = 0;
151  NCWidget::wDelete();
152  vstart = 0;
153 }
154 
155 
156 void NCIntField::setLabel( const std::string & nlabel )
157 {
158  label = NCstring( nlabel );
159  label.stripHotkey();
160  setDefsze();
161  YIntField::setLabel( nlabel );
162  Redraw();
163 }
164 
165 
166 void NCIntField::setValueInternal( int newValue )
167 {
168  // checking newValue is done by YIntField
169  // -> no checks required
170  cvalue = newValue;
171  tUpdate();
172 }
173 
174 
175 bool NCIntField::Increment( const bool bigstep )
176 {
177  unsigned dist = maxValue() - cvalue;
178 
179  if ( !dist )
180  return false;
181 
182  unsigned step = bigstep ? 10 : 1;
183 
184  if ( step < dist )
185  setValue( cvalue + step );
186  else
187  setValue( maxValue() );
188 
189  return true;
190 }
191 
192 
193 bool NCIntField::Decrement( const bool bigstep )
194 {
195  unsigned dist = cvalue - minValue();
196 
197  if ( !dist )
198  return false;
199 
200  unsigned step = bigstep ? 10 : 1;
201 
202  if ( step < dist )
203  setValue( cvalue - step );
204  else
205  setValue( minValue() );
206 
207  return true;
208 }
209 
210 
211 void NCIntField::wRedraw()
212 {
213  if ( !win )
214  return;
215 
216  // label
217  const NCstyle::StWidget & style( widgetStyle( true ) );
218 
219  lwin->bkgd( style.plain );
220 
221  lwin->clear();
222 
223  label.drawAt( *lwin, style );
224 
225  tUpdate();
226 }
227 
228 
229 void NCIntField::tUpdate()
230 {
231  if ( !win )
232  return;
233 
234  const NCstyle::StWidget & style( widgetStyle() );
235 
236  twin->bkgd( widgetStyle( true ).plain );
237 
238  twin->bkgdset( style.data );
239 
240  twin->printw( 0, vstart, " %*d ", vlen, cvalue );
241 
242  twin->bkgdset( style.scrl );
243 
244  twin->addch( 0, vstart,
245  ( cvalue != minValue() ? ACS_DARROW : ' ' ) );
246 
247  twin->addch( 0, vstart + vlen + 1,
248  ( cvalue != maxValue() ? ACS_UARROW : ' ' ) );
249 }
250 
251 
252 NCursesEvent NCIntField::wHandleInput( wint_t key )
253 {
254  NCursesEvent ret;
255  bool beep = false;
256  int ovlue = cvalue;
257 
258  switch ( key )
259  {
260  case KEY_UP:
261  beep = !Increment();
262  break;
263 
264  case KEY_DOWN:
265  beep = !Decrement();
266  break;
267 
268  case KEY_PPAGE:
269  beep = !Increment( true );
270  break;
271 
272  case KEY_NPAGE:
273  beep = !Decrement( true );
274  break;
275 
276  case KEY_HOME:
277 
278  if ( cvalue != maxValue() )
279  setValue( maxValue() );
280  else
281  beep = true;
282  break;
283 
284  case KEY_END:
285  if ( cvalue != minValue() )
286  setValue( minValue() );
287  else
288  beep = true;
289  break;
290 
291  case L'0':
292  case L'1':
293  case L'2':
294  case L'3':
295  case L'4':
296  case L'5':
297  case L'6':
298  case L'7':
299  case L'8':
300  case L'9':
301  case L'-':
302  enterPopup( key );
303  break;
304 
305  case L'+':
306  enterPopup();
307  break;
308 
309  case KEY_HOTKEY:
310  break;
311 
312  default:
313  beep = true;
314  break;
315  }
316 
317  if ( beep )
318  ::beep();
319 
320  if ( notify() && ovlue != cvalue )
321  ret = NCursesEvent::ValueChanged;
322 
323  return ret;
324 }
325 
326 
327 int NCIntField::enterPopup( wchar_t first )
328 {
329  std::wstring wch( &first );
330  std::string utf8;
331 
332  wpos at( ScreenPos() + wpos( win->maxy() - 1, vstart + 1 ) );
333  std::string label( std::string( "[" ) + numstring( minValue() )
334  + "," + numstring( maxValue() ) + "]" );
335 
336  std::string text( 1, ( char )first );
337  NCPopupTextEntry * dialog = new NCPopupTextEntry( at, label, text, vlen, 0,
338  NCInputField::NUMBER );
339  YUI_CHECK_NEW( dialog );
340 
341  while ( dialog->post() != -1 )
342  {
343  int nval = atoi( dialog->value().c_str() );
344 
345  if ( nval < minValue() )
346  {
347  dialog->setValue( numstring( minValue() ) );
348  }
349  else if ( maxValue() < nval )
350  {
351  dialog->setValue( numstring( maxValue() ) );
352  }
353  else
354  {
355  setValue( nval );
356  break;
357  }
358 
359  ::beep();
360  }
361 
362  YDialog::deleteTopmostDialog();
363 
364  return 0;
365 }
C++ class for windows.
Definition: ncursesw.h:904
int printw(const char *fmt,...)
Definition: ncursesw.cc:75
int bkgd(const chtype ch)
Definition: ncursesw.h:1443
void bkgdset(chtype ch)
Definition: ncursesw.h:1448
Definition: position.h:109
int addch(const char ch)
Definition: ncursesw.h:1228
int maxy() const
Definition: ncursesw.h:1095
virtual void setEnabled(bool do_bv)=0
Definition: NCWidget.cc:391
Definition: position.h:154
virtual void setEnabled(bool do_bv)
Definition: NCIntField.cc:85