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 );
427 if ( curpos < maxCursor() )
429 curpos = maxCursor();
455 if ( curpos < maxCursor() )
470 if ( notify() || returnOnReturn_b )
471 ret = NCursesEvent::Activated;
481 bool is_special =
false;
489 if (( !is_special && KEY_MIN < key && KEY_MAX > key )
494 ( InputMaxLength >= 0 && InputMaxLength <= (
int )buffer.length() ) )
499 else if ( fldtype == NUMBER )
501 if ( bufferFull() && key != L
'+' )
521 if ( curpos || buffer.empty() || buffer[0] != L
'-' )
523 buffer.insert( std::wstring::size_type( curpos ), 1, key );
525 if ( curpos < maxCursor() )
538 if ( !buffer.empty() && buffer[0] == L
'-' )
540 buffer.erase( std::wstring::size_type( 0 ), 1 );
554 if ( buffer.empty() || buffer[0] != L
'-' )
556 buffer.insert( std::wstring::size_type( 0 ), 1, L
'-' );
558 if ( curpos < maxCursor() )
579 if ( bufferFull() || !validKey( key ) )
586 buffer.insert( std::wstring::size_type( curpos ), 1, key );
588 if ( curpos < maxCursor() )
602 ret = NCursesEvent::ValueChanged;
613 void NCInputField::setInputMaxLength(
int numberOfChars )
615 int nr = numberOfChars;
620 if ( nr >= 0 && (
int )buffer.length() > nr )
622 buffer.erase( nr, maxCursor() - nr );
624 curpos = buffer.length();
629 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.