27 #define YUILogComponent "ncurses"
28 #include <yui/YUILog.h>
30 #include "NCInputField.h"
31 #include "YNCursesUI.h"
36 NCInputField::NCInputField( YWidget * parent,
37 const std::string & nlabel,
41 : YInputField( parent, nlabel, passwordMode )
43 , passwd( passwordMode )
46 , maxFldLength( maxFld )
47 , maxInputLength( maxInput )
52 , returnOnReturn_b( false )
53 , InputMaxLength( -1 )
55 yuiDebug() << std::endl;
57 if ( maxInputLength &&
58 ( !maxFldLength || maxFldLength > maxInputLength ) )
60 maxFldLength = maxInputLength;
72 NCInputField::~NCInputField()
76 yuiDebug() << std::endl;
81 int NCInputField::preferredWidth()
83 return wGetDefsze().W;
88 int NCInputField::preferredHeight()
90 return wGetDefsze().H;
98 YInputField::setEnabled( do_bv );
103 void NCInputField::setSize(
int newwidth,
int newheight )
105 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
110 void NCInputField::setDefsze()
112 unsigned defwidth = maxFldLength ? maxFldLength : 5;
114 if ( label.width() > defwidth )
115 defwidth = label.width();
117 defsze =
wsze( label.height() + 1, defwidth );
122 void NCInputField::wCreate(
const wrect & newrect )
124 NCWidget::wCreate( newrect );
129 wrect lrect( 0, wsze::min( newrect.Sze,
130 wsze( label.height(), newrect.Sze.W ) ) );
132 if ( lrect.Sze.H == newrect.Sze.H )
135 wrect trect( 0,
wsze( 1, newrect.Sze.W ) );
137 trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
140 lrect.Sze.H, lrect.Sze.W,
141 lrect.Pos.L, lrect.Pos.C,
145 trect.Sze.H, trect.Sze.W,
146 trect.Pos.L, trect.Pos.C,
149 if ( maxFldLength && maxFldLength < (
unsigned )newrect.Sze.W )
150 trect.Sze.W = maxFldLength;
152 fldlength = trect.Sze.W;
157 void NCInputField::wDelete()
168 void NCInputField::setLabel(
const std::string & nlabel )
172 YInputField::setLabel( nlabel );
179 void NCInputField::setValue(
const std::string & ntext )
181 std::string old_value = value();
184 if ( maxInputLength && buffer.length() > maxInputLength )
186 buffer = buffer.erase( maxInputLength );
191 curpos = buffer.length();
194 if (notify() && old_value != ntext)
204 std::string NCInputField::value( )
213 void NCInputField::setValidChars(
const std::string & validchars )
215 validChars =
NCstring( validchars );
216 YInputField::setValidChars( validchars );
221 bool NCInputField::validKey( wint_t key )
const
224 const std::wstring vwch( validChars.str() );
229 if ( key < 0 || WCHAR_MAX < key )
232 return( vwch.find((
wchar_t )key ) != std::wstring::npos );
237 void NCInputField::wRedraw()
245 lwin->
bkgd( style.plain );
249 label.drawAt( *lwin, style );
256 inline bool NCInputField::bufferFull()
const
258 return( maxInputLength && buffer.length() == maxInputLength );
263 inline unsigned NCInputField::maxCursor()
const
265 return( bufferFull() ? buffer.length() - 1 : buffer.length() );
270 void NCInputField::tUpdate()
275 unsigned maxc = maxCursor();
284 if ( maxc < fldlength )
290 if ( curpos <= fldstart )
292 fldstart = curpos ? curpos - 1 : 0;
295 if ( curpos >= fldstart + fldlength - 1 )
297 fldstart = curpos + ( curpos == maxc ? 1 : 2 ) - fldlength;
303 twin->
bkgd( widgetStyle(
true ).plain );
309 unsigned end = fldlength;
311 const wchar_t * cp = buffer.data() + fldstart;
314 if ( *cp && fldstart )
317 twin->
addch( ACS_LARROW );
323 if ( fldstart + fldlength <= maxc )
331 for ( ; *cp && i < end; ++i )
347 for ( ; i < end; ++i )
349 twin->
addch( ACS_CKBOARD );
353 if ( end < fldlength )
356 twin->
addch( ACS_RARROW );
360 if ( GetState() == NC::WSactive )
362 twin->
move( 0, curpos - fldstart );
363 twin->
bkgdset( wStyle().cursor );
365 if ( curpos < buffer.length() )
368 twin->
addch( ACS_CKBOARD );
388 if ( bufferFull() && curpos == maxCursor() )
392 buffer.erase( curpos, 1 );
396 buffer.erase( --curpos, 1 );
408 if ( curpos < buffer.length() )
410 buffer.erase( curpos, 1 );
438 if ( curpos < maxCursor() )
440 curpos = maxCursor();
466 if ( curpos < maxCursor() )
481 if ( notify() || returnOnReturn_b )
482 ret = NCursesEvent::Activated;
492 bool is_special =
false;
500 if (( !is_special && KEY_MIN < key && KEY_MAX > key )
505 ( InputMaxLength >= 0 && InputMaxLength <= (
int )buffer.length() ) )
510 else if ( fldtype == NUMBER )
512 if ( bufferFull() && key != L
'+' )
532 if ( curpos || buffer.empty() || buffer[0] != L
'-' )
534 buffer.insert( std::wstring::size_type( curpos ), 1, key );
536 if ( curpos < maxCursor() )
549 if ( !buffer.empty() && buffer[0] == L
'-' )
551 buffer.erase( std::wstring::size_type( 0 ), 1 );
565 if ( buffer.empty() || buffer[0] != L
'-' )
567 buffer.insert( std::wstring::size_type( 0 ), 1, L
'-' );
569 if ( curpos < maxCursor() )
590 if ( bufferFull() || !validKey( key ) )
597 buffer.insert( std::wstring::size_type( curpos ), 1, key );
599 if ( curpos < maxCursor() )
613 ret = NCursesEvent::ValueChanged;
624 void NCInputField::setInputMaxLength(
int numberOfChars )
626 int nr = numberOfChars;
631 if ( nr >= 0 && (
int )buffer.length() > nr )
633 buffer.erase( nr, maxCursor() - nr );
635 curpos = buffer.length();
640 YInputField::setInputMaxLength( numberOfChars );