27 #define YUILogComponent "ncurses" 28 #include <yui/YUILog.h> 30 #include "NCComboBox.h" 31 #include "NCPopupList.h" 34 NCComboBox::NCComboBox( YWidget * parent,
const std::string & nlabel,
36 : YComboBox( parent, nlabel, editable )
47 , InputMaxLength( -1 )
49 yuiDebug() << std::endl;
56 NCComboBox::~NCComboBox()
60 yuiDebug() << std::endl;
64 int NCComboBox::preferredWidth()
66 return wGetDefsze().W;
70 int NCComboBox::preferredHeight()
72 return wGetDefsze().H;
79 YComboBox::setEnabled( do_bv );
83 void NCComboBox::setSize(
int newwidth,
int newheight )
85 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
89 void NCComboBox::setDefsze()
95 defsze =
wsze( label.height() + 1,
96 ( label.width() > longest_line ) ? label.width() : longest_line + 2 );
100 void NCComboBox::wCreate(
const wrect & newrect )
102 NCWidget::wCreate( newrect );
107 wrect lrect( 0, wsze::min( newrect.Sze,
108 wsze( label.height(), newrect.Sze.W ) ) );
110 wrect trect( 0,
wsze( 1, newrect.Sze.W ) );
112 if ( lrect.Sze.H == newrect.Sze.H )
115 trect.Pos.L = lrect.Sze.H > 0 ? lrect.Sze.H : 0;
118 lrect.Sze.H, lrect.Sze.W,
119 lrect.Pos.L, lrect.Pos.C,
123 trect.Sze.H, trect.Sze.W,
124 trect.Pos.L, trect.Pos.C,
127 fldlength = trect.Sze.W ? trect.Sze.W - 1 : 0;
131 void NCComboBox::wDelete()
141 void NCComboBox::addItem( YItem * item )
145 YComboBox::addItem( item );
147 deflist.push_back( item->label() );
148 std::string::size_type this_line = item->label().size();
153 if (( this_line > longest_line ) && ( this_line <= 40 ) )
156 longest_line = this_line;
160 if ( item->selected() )
162 index = item->index();
163 setText( item->label() );
169 void NCComboBox::addItem(
const std::string & label,
bool selected )
171 YItem * newItem =
new YItem( label, selected );
172 YUI_CHECK_NEW( newItem );
179 void NCComboBox::setLabel(
const std::string & nlabel )
184 YComboBox::setLabel( nlabel );
189 void NCComboBox::setCurrentItem(
int nindex )
192 std::list<std::string>::iterator entry;
194 for ( entry = deflist.begin(); entry != deflist.end(); ++entry, ++idx )
198 std::string strip = *entry;
199 std::string::size_type h = strip.find(
'&' );
201 if ( h != std::string::npos )
215 int NCComboBox::getCurrentItem()
const 221 void NCComboBox::setText(
const std::string & ntext )
224 buffer = privText.str();
227 curpos = mayedit ? buffer.length() : 0;
237 void NCComboBox::selectItem( YItem * item,
bool selected )
241 YComboBox::selectItem( item, selected );
244 index = item->index();
248 std::string NCComboBox::text()
253 return privText.Str();
257 void NCComboBox::setValidChars(
const std::string & validchars )
259 validChars =
NCstring( validchars );
260 YComboBox::setValidChars( validchars );
264 bool NCComboBox::validKey( wint_t key )
const 266 const std::wstring vwch( validChars.str() );
271 if ( key < 0 || WCHAR_MAX < key )
274 return( vwch.find((
wchar_t )key ) != std::wstring::npos );
278 void NCComboBox::wRecoded()
286 buffer = privText.str();
292 void NCComboBox::wRedraw()
300 lwin->bkgd( style.plain );
304 label.drawAt( *lwin, style );
310 void NCComboBox::tUpdate()
315 const std::wstring & str( buffer );
317 if ( curpos > str.length() )
319 curpos = str.length();
323 if ( str.length() >= fldlength )
325 if ( curpos <= fldstart )
327 fldstart = curpos ? curpos - 1 : 0;
330 if ( curpos >= fldstart + fldlength - 1 )
332 fldstart = curpos + ( curpos == str.length() ? 1 : 2 ) - fldlength;
342 twin->bkgd( widgetStyle(
true ).plain );
346 bool utf8 = haveUtf8();
351 unsigned end = fldlength;
352 const wchar_t * cp = str.data() + fldstart;
356 if ( *cp && fldstart )
358 twin->bkgdset( style.scrl );
360 twin->add_wch( WACS_LARROW )
361 : twin->addch( ACS_LARROW );
367 if ( fldstart + fldlength <= str.length() )
373 twin->bkgdset( style.data );
375 for ( ; *cp && i < end; ++i )
377 twin->addwstr( cp, 1 );
381 twin->bkgdset( style.plain );
383 for ( ; i < end; ++i )
385 twin->addch( ACS_CKBOARD );
389 twin->bkgdset( style.scrl );
391 if ( end < fldlength )
394 twin->add_wch( WACS_RARROW )
395 : twin->addch( ACS_RARROW );
401 twin->add_wch( 0, twin->maxx(), WACS_DARROW )
402 : twin->addch( 0, twin->maxx(), ACS_DARROW );
404 if ( mayedit && GetState() == NC::WSactive )
406 twin->move( 0, curpos - fldstart );
407 twin->bkgdset( wStyle().cursor );
409 if ( curpos < buffer.length() )
410 twin->add_attr_char( );
412 twin->addch( ACS_CKBOARD );
422 std::wstring oval = buffer;
428 if ( mayedit && curpos )
430 buffer.erase( --curpos, 1 );
443 if ( mayedit && curpos < buffer.length() )
445 buffer.erase( curpos, 1 );
459 if ( curpos && ( mayedit || fldstart ) )
474 if ( curpos < buffer.length() && ( mayedit || fldstart + fldlength <= buffer.length() ) )
476 curpos = buffer.length();
493 curpos = fldstart - 1;
510 if ( mayedit && curpos < buffer.length() )
514 else if ( fldstart + fldlength <= buffer.length() )
516 curpos = fldstart + fldlength;
539 bool is_special =
false;
547 if ( !mayedit || !validKey( key )
549 ( !is_special && KEY_MIN < key && KEY_MAX > key )
554 ( InputMaxLength >= 0 && InputMaxLength <= (
int )buffer.length() ) )
561 buffer.insert( curpos, 1, key );
578 ret = NCursesEvent::ValueChanged;
585 int NCComboBox::listPopup()
589 if ( !deflist.empty() )
592 wpos at( ScreenPos() +
wpos( 2, -1 ) );
594 YUI_CHECK_NEW( dialog );
595 idx = dialog->post();
598 setCurrentItem( idx );
600 YDialog::deleteTopmostDialog();
607 void NCComboBox::deleteAllItems()
609 YComboBox::deleteAllItems();
615 void NCComboBox::setInputMaxLength(
int nr )
619 if ( nr >= 0 && (
int )buffer.length() > nr )
621 buffer.erase( nr, buffer.length() - nr );
623 curpos = buffer.length();
626 YComboBox::setInputMaxLength( nr );
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.