25 #define YUILogComponent "ncurses" 26 #include <yui/YUILog.h> 28 #include "YNCursesUI.h" 30 #include <yui/TreeItem.h> 31 #include <yui/YSelectionWidget.h> 46 mutable chtype * prefix;
48 unsigned prefixLen()
const {
return level + 3; }
55 , level( p ? p->level + 1 : 0 )
60 , multiSel( multiSelection )
68 for ( ; s->nsibling; s = s->nsibling )
75 parent->fchild =
this;
78 if ( !parent->yitem->isOpen() )
87 + yitem->label() ) ) );
92 + yitem->label() ) ) );
100 YTreeItem * YItem()
const {
return yitem; }
102 unsigned Level()
const {
return level; }
104 virtual bool isVisible()
const 106 return !parent || ( !isHidden() && parent->isVisible() );
110 virtual int ChangeToVisible()
117 parent->ChangeToVisible();
119 for (
NCTreeLine * c = parent->fchild; c; c = c->nsibling )
121 c->ClearState( S_HIDDEN );
122 c->YItem()->setOpen(
true );
127 ClearState( S_HIDDEN );
128 yitem->setOpen(
true );
135 virtual unsigned Hotspot(
unsigned & at )
const 142 virtual int handleInput( wint_t key )
151 if ( fchild->isVisible() )
158 if ( !fchild->isVisible() )
174 if ( fchild->isVisible() )
176 yitem->setOpen(
false );
177 yuiMilestone() <<
"Closing item " << yitem->label() << std::endl;
179 for (
NCTreeLine * c = fchild; c; c = c->nsibling )
180 c->SetState( S_HIDDEN );
184 yitem->setOpen(
true );
185 yuiMilestone() <<
"Opening item " << yitem->label() << std::endl;
187 for (
NCTreeLine * c = fchild; c; c = c->nsibling )
188 c->ClearState( S_HIDDEN );
200 NCTableLine::DrawAt( w, at, tableStyle, active );
203 w.
bkgdset( tableStyle.getBG( vstate, NCTableCol::SEPARATOR ) );
207 prefix =
new chtype[prefixLen()];
208 chtype * tagend = &prefix[prefixLen()-1];
209 *tagend-- = ACS_HLINE;
210 *tagend-- = fchild ? ACS_TTEE : ACS_HLINE;
214 *tagend-- = nsibling ? ACS_LTEE : ACS_LLCORNER;
216 for (
NCTreeLine * p = parent; p; p = p->parent )
218 *tagend-- = p->nsibling ? ACS_VLINE : (
' '&A_CHARTEXT );
223 *tagend-- = ACS_HLINE;
227 w.
move( at.Pos.L, at.Pos.C );
231 for ( ; i < prefixLen(); ++i )
232 w.
addch( prefix[i] );
234 w.
move( at.Pos.L, at.Pos.C + prefixLen() - 2 );
236 if ( fchild && !isSpecial() )
237 w.
bkgdset( tableStyle.highlightBG( vstate, NCTableCol::HINT,
238 NCTableCol::SEPARATOR ) );
240 if ( fchild && !fchild->isVisible() )
243 w.
addch( prefix[prefixLen() - 2] );
252 NCTree::NCTree( YWidget * parent,
const std::string & nlabel,
bool multiselection,
bool recursiveselection )
253 : YTree( parent, nlabel, multiselection, recursiveselection )
255 , multiSel ( multiselection )
257 yuiDebug() << std::endl;
259 if ( multiselection && recursiveselection )
260 yuiMilestone() <<
"NCTree recursive multi selection ON" << std::endl;
261 else if ( multiselection )
262 yuiMilestone() <<
"NCTree multi selection ON" << std::endl;
271 yuiDebug() << std::endl;
278 inline const NCTreeLine * NCTree::getTreeLine(
unsigned idx )
const 290 inline NCTreeLine * NCTree::modifyTreeLine(
unsigned idx )
304 int NCTree::preferredWidth()
306 wsze sze = wsze::max( defsze,
wsze( 0, labelWidth() + 2 ) );
314 int NCTree::preferredHeight()
316 wsze sze = wsze::max( defsze,
wsze( 0, labelWidth() + 2 ) );
327 YWidget::setEnabled( do_bv );
333 void NCTree::setSize(
int newwidth,
int newheight )
335 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
343 YTreeItem * NCTree::getCurrentItem()
const 345 YTreeItem * yitem = 0;
352 yitem = cline->YItem();
355 yuiDebug() <<
"-> " << ( yitem ? yitem->label().c_str() :
"noitem" ) << std::endl;
360 void NCTree::deselectAllItems()
364 YItemCollection selectedItems = YTree::selectedItems();
366 for ( YItemConstIterator it = selectedItems.begin(); it != selectedItems.end(); ++it )
368 selectItem( *it,
false );
372 YTree::deselectAllItems();
377 void NCTree::selectItem( YItem *item,
bool selected )
382 YTreeItem * treeItem =
dynamic_cast<YTreeItem *
>( item );
383 YUI_CHECK_PTR( treeItem );
384 YTreeItem *citem = getCurrentItem();
387 int at = treeItem->index();
394 cline = modifyTreeLine( at );
397 ccol = cline->GetCol(0);
403 if ( !multiSel && (treeItem == citem) )
405 YTree::deselectAllItems();
409 YTree::selectItem ( treeItem,
false );
412 ccol->SetLabel(
NCstring( std::string( cline->Level() + 3,
' ' ) +
"[ ] " 419 YTree::selectItem( treeItem, selected );
421 if ( multiSel && ccol )
423 ccol->SetLabel(
NCstring( std::string( cline->Level() + 3,
' ' ) +
"[x] " 429 myPad()->ShowItem( getTreeLine( at ) );
438 void NCTree::selectItem(
int index )
440 YItem * item = YTree::itemAt( index );
444 selectItem( item,
true );
447 YUI_THROW( YUIException(
"Can't find selected item" ) );
452 void NCTree::setLabel(
const std::string & nlabel )
454 YTree::setLabel( nlabel );
455 NCPadWidget::setLabel(
NCstring( nlabel ) );
460 void NCTree::rebuildTree()
470 NCPad * NCTree::CreatePad()
472 wsze psze( defPadSze() );
474 npad->
bkgd( listStyle().item.plain );
485 item->setIndex( idx++ );
487 YTreeItem * treeItem =
dynamic_cast<YTreeItem *
>( item );
488 YUI_CHECK_PTR( treeItem );
493 if (item->selected())
496 int at = treeItem->index();
501 cline = modifyTreeLine( at );
504 ccol = cline->GetCol(0);
508 ccol->SetLabel(
NCstring( std::string( cline->Level() + 3,
' ' ) +
"[x] " 514 pad->ShowItem( getTreeLine( at ) );
518 for ( YItemIterator it = item->childrenBegin(); it < item->childrenEnd(); ++it )
520 CreateTreeLines( line, pad, *it );
525 YTreeItem * NCTree::currentItem()
527 return getCurrentItem();
531 void NCTree::DrawPad()
535 yuiWarning() <<
"PadWidget not yet created" << std::endl;
541 for ( YItemIterator it = itemsBegin(); it < itemsEnd(); ++it )
543 CreateTreeLines( 0,
myPad(), *it );
547 NCPadWidget::DrawPad();
555 YTreeItem * oldCurrentItem = getCurrentItem();
557 bool handled = handleInput( key );
558 const YItem * currentItem = getCurrentItem();
572 if ( currentItem->selected() )
573 selectItem( const_cast<YItem *>(currentItem), false );
575 selectItem( const_cast<YItem *>(currentItem),
true );
579 return NCursesEvent::ValueChanged;
596 return NCursesEvent::Activated;
602 YTree::selectItem( const_cast<YItem *>( currentItem ),
true );
605 if ( notify() && immediateMode() && ( oldCurrentItem != currentItem ) )
606 ret = NCursesEvent::SelectionChanged;
608 yuiDebug() <<
"Notify: " << ( notify() ?
"true" :
"false" ) <<
609 " Return event: " << ret.reason << std::endl;
626 void NCTree::deleteAllItems()
628 YTree::deleteAllItems();
629 myPad()->ClearTable();
int bkgd(const chtype ch)
Set the background property and apply it to the window.
void bkgdset(chtype ch)
Set the background property.
virtual void activate()
Activate the item selected in the tree.
static YNCursesUI * ui()
Access the global Y2NCursesUI.
void sendEvent(NCursesEvent event)
Send an event to the UI.
int addch(const char ch)
Put attributed character to the window.
virtual NCTreePad * myPad() const
Return the current pad.
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.