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 );
379 if ( bufferFull() && curpos == maxCursor() )
383 buffer.erase( curpos, 1 );
387 buffer.erase( --curpos, 1 );
399 if ( curpos < buffer.length() )
401 buffer.erase( curpos, 1 );
429 if ( curpos < maxCursor() )
431 curpos = maxCursor();
457 if ( curpos < maxCursor() )
472 if ( notify() || returnOnReturn_b )
473 ret = NCursesEvent::Activated;
483 bool is_special =
false;
491 if (( !is_special && KEY_MIN < key && KEY_MAX > key )
496 ( InputMaxLength >= 0 && InputMaxLength <= (
int )buffer.length() ) )
501 else if ( fldtype == NUMBER )
503 if ( bufferFull() && key != L
'+' )
523 if ( curpos || buffer.empty() || buffer[0] != L
'-' )
525 buffer.insert( std::wstring::size_type( curpos ), 1, key );
527 if ( curpos < maxCursor() )
540 if ( !buffer.empty() && buffer[0] == L
'-' )
542 buffer.erase( std::wstring::size_type( 0 ), 1 );
556 if ( buffer.empty() || buffer[0] != L
'-' )
558 buffer.insert( std::wstring::size_type( 0 ), 1, L
'-' );
560 if ( curpos < maxCursor() )
581 if ( bufferFull() || !validKey( key ) )
588 buffer.insert( std::wstring::size_type( curpos ), 1, key );
590 if ( curpos < maxCursor() )
604 ret = NCursesEvent::ValueChanged;
615 void NCInputField::setInputMaxLength(
int numberOfChars )
617 int nr = numberOfChars;
622 if ( nr >= 0 && (
int )buffer.length() > nr )
624 buffer.erase( nr, maxCursor() - nr );
626 curpos = buffer.length();
631 YInputField::setInputMaxLength( numberOfChars );
int clear()
Clear the window.
int bkgd(const chtype ch)
Set the background property and apply it to the window.
void bkgdset(chtype ch)
Set the background property.
int addwstr(const wchar_t *str, int n=-1)
Write the wchar_t str to the window, stop writing if the terminating NUL or the limit n is reached.
int add_attr_char(int y, int x)
Put attributed character from given position to the window.
int addch(const char ch)
Put attributed character to the window.
int move(int y, int x)
Move cursor the this position.