27 #define YUILogComponent "ncurses"
28 #include <yui/YUILog.h>
30 #include "NCInputField.h"
35 NCInputField::NCInputField( YWidget * parent,
36 const std::string & nlabel,
40 : YInputField( parent, nlabel, passwordMode )
42 , passwd( passwordMode )
45 , maxFldLength( maxFld )
46 , maxInputLength( maxInput )
51 , returnOnReturn_b( false )
52 , InputMaxLength( -1 )
54 yuiDebug() << std::endl;
56 if ( maxInputLength &&
57 ( !maxFldLength || maxFldLength > maxInputLength ) )
59 maxFldLength = maxInputLength;
71 NCInputField::~NCInputField()
75 yuiDebug() << std::endl;
80 int NCInputField::preferredWidth()
82 return wGetDefsze().W;
87 int NCInputField::preferredHeight()
89 return wGetDefsze().H;
97 YInputField::setEnabled( do_bv );
102 void NCInputField::setSize(
int newwidth,
int newheight )
104 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
109 void NCInputField::setDefsze()
111 unsigned defwidth = maxFldLength ? maxFldLength : 5;
113 if ( label.width() > defwidth )
114 defwidth = label.width();
116 defsze =
wsze( label.height() + 1, defwidth );
121 void NCInputField::wCreate(
const wrect & newrect )
123 NCWidget::wCreate( newrect );
128 wrect lrect( 0, wsze::min( newrect.Sze,
129 wsze( label.height(), newrect.Sze.W ) ) );
131 if ( lrect.Sze.H == newrect.Sze.H )
134 wrect trect( 0,
wsze( 1, newrect.Sze.W ) );
136 trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
139 lrect.Sze.H, lrect.Sze.W,
140 lrect.Pos.L, lrect.Pos.C,
144 trect.Sze.H, trect.Sze.W,
145 trect.Pos.L, trect.Pos.C,
148 if ( maxFldLength && maxFldLength < (
unsigned )newrect.Sze.W )
149 trect.Sze.W = maxFldLength;
151 fldlength = trect.Sze.W;
156 void NCInputField::wDelete()
167 void NCInputField::setLabel(
const std::string & nlabel )
171 YInputField::setLabel( nlabel );
178 void NCInputField::setValue(
const std::string & ntext )
182 if ( maxInputLength && buffer.length() > maxInputLength )
184 buffer = buffer.erase( maxInputLength );
189 curpos = buffer.length();
195 std::string NCInputField::value( )
204 void NCInputField::setValidChars(
const std::string & validchars )
206 validChars =
NCstring( validchars );
207 YInputField::setValidChars( validchars );
212 bool NCInputField::validKey( wint_t key )
const
215 const std::wstring vwch( validChars.str() );
220 if ( key < 0 || WCHAR_MAX < key )
223 return( vwch.find((
wchar_t )key ) != std::wstring::npos );
228 void NCInputField::wRedraw()
236 lwin->
bkgd( style.plain );
240 label.drawAt( *lwin, style );
247 inline bool NCInputField::bufferFull()
const
249 return( maxInputLength && buffer.length() == maxInputLength );
254 inline unsigned NCInputField::maxCursor()
const
256 return( bufferFull() ? buffer.length() - 1 : buffer.length() );
261 void NCInputField::tUpdate()
266 unsigned maxc = maxCursor();
275 if ( maxc < fldlength )
281 if ( curpos <= fldstart )
283 fldstart = curpos ? curpos - 1 : 0;
286 if ( curpos >= fldstart + fldlength - 1 )
288 fldstart = curpos + ( curpos == maxc ? 1 : 2 ) - fldlength;
294 twin->
bkgd( widgetStyle(
true ).plain );
300 unsigned end = fldlength;
302 const wchar_t * cp = buffer.data() + fldstart;
305 if ( *cp && fldstart )
308 twin->
addch( ACS_LARROW );
314 if ( fldstart + fldlength <= maxc )
322 for ( ; *cp && i < end; ++i )
338 for ( ; i < end; ++i )
340 twin->
addch( ACS_CKBOARD );
344 if ( end < fldlength )
347 twin->
addch( ACS_RARROW );
351 if ( GetState() == NC::WSactive )
353 twin->
move( 0, curpos - fldstart );
354 twin->
bkgdset( wStyle().cursor );
356 if ( curpos < buffer.length() )
359 twin->
addch( ACS_CKBOARD );
377 if ( bufferFull() && curpos == maxCursor() )
381 buffer.erase( curpos, 1 );
385 buffer.erase( --curpos, 1 );
397 if ( curpos < buffer.length() )
399 buffer.erase( curpos, 1 );
425 if ( curpos < maxCursor() )
427 curpos = maxCursor();
453 if ( curpos < maxCursor() )
468 if ( notify() || returnOnReturn_b )
469 ret = NCursesEvent::Activated;
479 bool is_special =
false;
487 if (( !is_special && KEY_MIN < key && KEY_MAX > key )
492 ( InputMaxLength >= 0 && InputMaxLength <= (
int )buffer.length() ) )
497 else if ( fldtype == NUMBER )
499 if ( bufferFull() && key != L
'+' )
519 if ( curpos || buffer.empty() || buffer[0] != L
'-' )
521 buffer.insert( std::wstring::size_type( curpos ), 1, key );
523 if ( curpos < maxCursor() )
536 if ( !buffer.empty() && buffer[0] == L
'-' )
538 buffer.erase( std::wstring::size_type( 0 ), 1 );
552 if ( buffer.empty() || buffer[0] != L
'-' )
554 buffer.insert( std::wstring::size_type( 0 ), 1, L
'-' );
556 if ( curpos < maxCursor() )
577 if ( bufferFull() || !validKey( key ) )
584 buffer.insert( std::wstring::size_type( curpos ), 1, key );
586 if ( curpos < maxCursor() )
600 ret = NCursesEvent::ValueChanged;
611 void NCInputField::setInputMaxLength(
int numberOfChars )
613 int nr = numberOfChars;
618 if ( nr >= 0 && (
int )buffer.length() > nr )
620 buffer.erase( nr, maxCursor() - nr );
622 curpos = buffer.length();
627 YInputField::setInputMaxLength( numberOfChars );
int bkgd(const chtype ch)
int addwstr(const wchar_t *str, int n=-1)
int add_attr_char(int y, int x)