25 #define YUILogComponent "ncurses" 26 #include <yui/YUILog.h> 27 #include "NCTextPad.h" 36 #define MY_TEXT_STYLE ( parw.widgetStyle( true ).data ) 38 NCTextPad::NCTextPad(
int l,
int c,
const NCWidget & p )
41 , cline( lines.begin() )
43 , InputMaxLength( -1 )
45 bkgd( MY_TEXT_STYLE );
50 NCTextPad::~NCTextPad()
56 void NCTextPad::resize(
wsze nsze )
60 if ( nsze.H != height()
61 || nsze.W != width() )
68 NCursesPad::resize( nsze.H, nsze.W );
77 void NCTextPad::assertSze(
wsze minsze )
79 if ( minsze.W > width()
80 || minsze.H > height() )
86 void NCTextPad::assertWidth(
unsigned minw )
88 if ( minw >= (
unsigned )width() )
89 resize(
wsze( height(), minw + 10 ) );
94 void NCTextPad::assertHeight(
unsigned minh )
96 if ( minh > (
unsigned )height() )
97 resize(
wsze( minh + 10, width() ) );
102 wpos NCTextPad::CurPos()
const 109 void NCTextPad::cursor(
bool on )
113 if (( curson = on ) )
115 bkgdset( parw.wStyle().cursor );
116 add_attr_char( curs.L, curs.C );
117 bkgdset( MY_TEXT_STYLE );
121 add_attr_char( curs.L, curs.C );
128 int NCTextPad::setpos()
132 chtype oldbkgd = NCattribute::getColor( getbkgd() );
133 bkgd( MY_TEXT_STYLE );
135 if ( NCattribute::getColor( getbkgd() ) != oldbkgd )
138 for (
int l = 0; l < height(); ++l )
139 for (
int c = 0; c < width(); ++ c )
141 add_attr_char( l, c );
144 cursor( parw.GetState() == NC::WSactive );
145 return setpos( CurPos() );
150 int NCTextPad::setpos(
const wpos & newpos )
152 wpos npos( newpos.between( 0,
wpos( maxy(), maxx() ) ) );
154 if ((
unsigned )npos.L >= lines.size() )
156 npos.L = lines.size() - 1;
160 else if ( npos.L != curs.L )
162 advance( cline, npos.L - curs.L );
165 if ((
unsigned )npos.C > *cline )
172 if ( ocurs ) cursorOff();
176 if ( ocurs ) cursorOn();
180 if ( drect.Sze >
wsze( 0 ) )
182 padpos = ( padpos / drect.Sze ) * drect.Sze;
185 return NCPad::setpos( padpos );
190 bool NCTextPad::handleInput( wint_t key )
237 if ((
unsigned )curs.C < ( *cline ) )
241 else if ((
unsigned )curs.L + 1 < lines.size() )
257 if ((
unsigned )curs.L + 1 < lines.size() )
274 setpos(
wpos( curs.L - 3, curs.C ) );
286 if ((
unsigned )curs.L + 1 < lines.size() )
288 setpos(
wpos( curs.L + 3, curs.C ) );
311 if ((
unsigned )curs.C < ( *cline ) )
319 beep = !delch(
true );
333 if ( InputMaxLength >= 0 && InputMaxLength < (
int )getText().length() )
340 beep = !insert( key );
358 bool NCTextPad::insert( wint_t key )
365 if ( key < 32 || ( key >= 127 && key < 160 ) || UCHAR_MAX < key )
370 assertWidth( ++( *cline ) );
375 int ret = wattr_get( w, &attr, &color, NULL );
383 ret = setcchar( &cchar, wch, attr, color, NULL );
389 #ifdef NCURSES_EXT_COLORS 392 ret = ins_wch( curs.L, curs.C++, &cchar );
399 bool NCTextPad::openLine()
401 assertHeight( lines.size() + 1 );
402 std::list<unsigned>::iterator newl( cline );
403 newl = lines.insert( ++newl, 0 );
410 ( *newl ) = ( *cline );
416 move( curs.L + 1, 0 );
419 if ((
unsigned )curs.C < ( *cline ) )
422 ( *newl ) = ( *cline ) - curs.C;
425 move( curs.L, curs.C );
426 copywin( *
this, curs.L, curs.C, curs.L + 1, 0, curs.L + 1, ( *newl ),
false );
457 if ((
unsigned )curs.C < *cline )
464 else if ((
unsigned )curs.L + 1 < lines.size() )
467 std::list<unsigned>::iterator nextl( cline );
469 ( *cline ) += ( *nextl );
470 lines.erase( nextl );
472 assertWidth(( *cline ) );
473 copywin( *
this, curs.L + 1, 0, curs.L, curs.C, curs.L, ( *cline ),
false );
475 move( curs.L + 1, 0 );
485 void NCTextPad::setText(
const NCtext & ntext )
487 bkgd( MY_TEXT_STYLE );
490 if ( ocurs ) cursorOff();
493 assertSze(
wsze( ntext.Lines(), ntext.Columns() + 1 ) );
499 wattr_get( w, &attr, &color, NULL );
507 for ( NCtext::const_iterator line = ntext.begin(); line != ntext.end(); ++line )
509 lines.push_back(( *line ).str().length() );
513 for ( std::wstring::const_iterator c = line->str().begin(); c != line->str().end(); c++ )
525 setcchar( &cchar, wch, attr, color, NULL );
528 #ifdef NCURSES_EXT_COLORS 531 ins_wch( cl, cc++, &cchar );
538 lines.push_back( 0U );
540 cline = lines.begin();
550 std::wstring NCTextPad::getText()
const 559 wchar_t wch[CCHARW_MAX+1];
563 for ( std::list<unsigned>::const_iterator cgetl( lines.begin() ); cgetl != lines.end(); ++cgetl )
565 for (
unsigned c = 0; c < ( *cgetl ); ++c )
568 getcchar( &cchar, wch, &attr, &colorpair, NULL );
572 if ( wch[0] == 8677 )
580 if ( l < lines.size() )
590 std::ostream & operator<<( std::ostream & STREAM,
const NCTextPad & OBJ )
592 STREAM <<
"at " << OBJ.CurPos() <<
" on " <<
wsze( OBJ.
height(), OBJ.
width() )
593 <<
" lines " << OBJ.lines.size() <<
" (" << *OBJ.cline <<
")";
597 void NCTextPad::setInputMaxLength(
int nr )
601 if ( nr >= 0 && nr < (
int )getText().length() )
603 NCstring newtext = getText().substr( 0, nr );
int height() const
Number of lines in this window.
int delch()
Delete character under the cursor.
int in_wchar(cchar_t *cchar)
Retrieve combined character under the current cursor position.
int width() const
Number of columns in this window.