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 )
361 : twin->
addch( ACS_LARROW );
367 if ( fldstart + fldlength <= str.length() )
375 for ( ; *cp && i < end; ++i )
383 for ( ; i < end; ++i )
385 twin->
addch( ACS_CKBOARD );
391 if ( end < fldlength )
395 : twin->
addch( ACS_RARROW );
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() )
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 );
458 if ( curpos && ( mayedit || fldstart ) )
472 if ( curpos < buffer.length() && ( mayedit || fldstart + fldlength <= buffer.length() ) )
474 curpos = buffer.length();
491 curpos = fldstart - 1;
508 if ( mayedit && curpos < buffer.length() )
512 else if ( fldstart + fldlength <= buffer.length() )
514 curpos = fldstart + fldlength;
537 bool is_special =
false;
545 if ( !mayedit || !validKey( key )
547 ( !is_special && KEY_MIN < key && KEY_MAX > key )
552 ( InputMaxLength >= 0 && InputMaxLength <= (
int )buffer.length() ) )
559 buffer.insert( curpos, 1, key );
576 ret = NCursesEvent::ValueChanged;
583 int NCComboBox::listPopup()
587 if ( !deflist.empty() )
591 YUI_CHECK_NEW( dialog );
592 idx = dialog->post();
595 setCurrentItem( idx );
597 YDialog::deleteTopmostDialog();
604 void NCComboBox::deleteAllItems()
606 YComboBox::deleteAllItems();
612 void NCComboBox::setInputMaxLength(
int nr )
616 if ( nr >= 0 && (
int )buffer.length() > nr )
618 buffer.erase( nr, buffer.length() - nr );
620 curpos = buffer.length();
623 YComboBox::setInputMaxLength( nr );
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 maxx() const
Largest x coord in window.
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 height() const
Number of lines in this window.
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.
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
int add_wch(const cchar_t *cch)
Put a combined character to the window.