25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
29 #include <yui/TreeItem.h>
30 #include <yui/YSelectionWidget.h>
45 mutable chtype * prefix;
47 unsigned prefixLen()
const {
return level + 3; }
54 , level( p ? p->level + 1 : 0 )
59 , multiSel( multiSelection )
67 for ( ; s->nsibling; s = s->nsibling )
74 parent->fchild =
this;
77 if ( !parent->yitem->isOpen() )
86 + yitem->label() ) ) );
91 + yitem->label() ) ) );
99 YTreeItem * YItem()
const {
return yitem; }
101 unsigned Level()
const {
return level; }
103 virtual bool isVisible()
const
105 return !parent || ( !isHidden() && parent->isVisible() );
109 virtual int ChangeToVisible()
116 parent->ChangeToVisible();
118 for (
NCTreeLine * c = parent->fchild; c; c = c->nsibling )
120 c->ClearState( S_HIDDEN );
121 c->YItem()->setOpen(
true );
126 ClearState( S_HIDDEN );
127 yitem->setOpen(
true );
134 virtual unsigned Hotspot(
unsigned & at )
const
141 virtual int handleInput( wint_t key )
150 if ( fchild->isVisible() )
157 if ( !fchild->isVisible() )
173 if ( fchild->isVisible() )
175 yitem->setOpen(
false );
176 yuiMilestone() <<
"Closing item " << yitem->label() << std::endl;
178 for (
NCTreeLine * c = fchild; c; c = c->nsibling )
179 c->SetState( S_HIDDEN );
183 yitem->setOpen(
true );
184 yuiMilestone() <<
"Opening item " << yitem->label() << std::endl;
186 for (
NCTreeLine * c = fchild; c; c = c->nsibling )
187 c->ClearState( S_HIDDEN );
199 NCTableLine::DrawAt( w, at, tableStyle, active );
202 w.
bkgdset( tableStyle.getBG( vstate, NCTableCol::SEPARATOR ) );
206 prefix =
new chtype[prefixLen()];
207 chtype * tagend = &prefix[prefixLen()-1];
208 *tagend-- = ACS_HLINE;
209 *tagend-- = fchild ? ACS_TTEE : ACS_HLINE;
213 *tagend-- = nsibling ? ACS_LTEE : ACS_LLCORNER;
215 for (
NCTreeLine * p = parent; p; p = p->parent )
217 *tagend-- = p->nsibling ? ACS_VLINE : (
' '&A_CHARTEXT );
222 *tagend-- = ACS_HLINE;
226 w.
move( at.Pos.L, at.Pos.C );
230 for ( ; i < prefixLen(); ++i )
231 w.
addch( prefix[i] );
233 w.
move( at.Pos.L, at.Pos.C + prefixLen() - 2 );
235 if ( fchild && !isSpecial() )
236 w.
bkgdset( tableStyle.highlightBG( vstate, NCTableCol::HINT,
237 NCTableCol::SEPARATOR ) );
239 if ( fchild && !fchild->isVisible() )
242 w.
addch( prefix[prefixLen() - 2] );
251 NCTree::NCTree( YWidget * parent,
const std::string & nlabel,
bool multiselection,
bool recursiveselection )
252 : YTree( parent, nlabel, multiselection, recursiveselection )
254 , multiSel ( multiselection )
256 yuiDebug() << std::endl;
258 if ( multiselection && recursiveselection )
259 yuiMilestone() <<
"NCTree recursive multi selection ON" << std::endl;
260 else if ( multiselection )
261 yuiMilestone() <<
"NCTree multi selection ON" << std::endl;
270 yuiDebug() << std::endl;
277 inline const NCTreeLine * NCTree::getTreeLine(
unsigned idx )
const
289 inline NCTreeLine * NCTree::modifyTreeLine(
unsigned idx )
303 int NCTree::preferredWidth()
305 wsze sze = wsze::max( defsze,
wsze( 0, labelWidth() + 2 ) );
313 int NCTree::preferredHeight()
315 wsze sze = wsze::max( defsze,
wsze( 0, labelWidth() + 2 ) );
326 YWidget::setEnabled( do_bv );
332 void NCTree::setSize(
int newwidth,
int newheight )
334 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
342 YTreeItem * NCTree::getCurrentItem()
const
344 YTreeItem * yitem = 0;
351 yitem = cline->YItem();
354 yuiDebug() <<
"-> " << ( yitem ? yitem->label().c_str() :
"noitem" ) << std::endl;
359 void NCTree::deselectAllItems()
363 YItemCollection selectedItems = YTree::selectedItems();
365 for ( YItemConstIterator it = selectedItems.begin(); it != selectedItems.end(); ++it )
367 selectItem( *it,
false );
371 YTree::deselectAllItems();
376 void NCTree::selectItem( YItem *item,
bool selected )
381 YTreeItem * treeItem =
dynamic_cast<YTreeItem *
>( item );
382 YUI_CHECK_PTR( treeItem );
383 YTreeItem *citem = getCurrentItem();
386 int at = treeItem->index();
393 cline = modifyTreeLine( at );
396 ccol = cline->GetCol(0);
402 if ( !multiSel && (treeItem == citem) )
404 YTree::deselectAllItems();
408 YTree::selectItem ( treeItem,
false );
411 ccol->SetLabel(
NCstring( std::string( cline->Level() + 3,
' ' ) +
"[ ] "
418 YTree::selectItem( treeItem, selected );
420 if ( multiSel && ccol )
422 ccol->SetLabel(
NCstring( std::string( cline->Level() + 3,
' ' ) +
"[x] "
428 myPad()->ShowItem( getTreeLine( at ) );
437 void NCTree::selectItem(
int index )
439 YItem * item = YTree::itemAt( index );
443 selectItem( item,
true );
446 YUI_THROW( YUIException(
"Can't find selected item" ) );
451 void NCTree::setLabel(
const std::string & nlabel )
453 YTree::setLabel( nlabel );
454 NCPadWidget::setLabel(
NCstring( nlabel ) );
459 void NCTree::rebuildTree()
469 NCPad * NCTree::CreatePad()
471 wsze psze( defPadSze() );
473 npad->
bkgd( listStyle().item.plain );
484 item->setIndex( idx++ );
486 YTreeItem * treeItem =
dynamic_cast<YTreeItem *
>( item );
487 YUI_CHECK_PTR( treeItem );
494 for ( YItemIterator it = item->childrenBegin(); it < item->childrenEnd(); ++it )
496 CreateTreeLines( line, pad, *it );
501 YTreeItem * NCTree::currentItem()
503 return getCurrentItem();
507 void NCTree::DrawPad()
511 yuiWarning() <<
"PadWidget not yet created" << std::endl;
517 for ( YItemIterator it = itemsBegin(); it < itemsEnd(); ++it )
519 CreateTreeLines( 0,
myPad(), *it );
523 NCPadWidget::DrawPad();
531 YTreeItem * oldCurrentItem = getCurrentItem();
533 bool handled = handleInput( key );
534 const YItem * currentItem = getCurrentItem();
548 if ( currentItem->selected() )
549 selectItem( const_cast<YItem *>(currentItem), false );
551 selectItem( const_cast<YItem *>(currentItem),
true );
555 return NCursesEvent::ValueChanged;
572 return NCursesEvent::Activated;
578 YTree::selectItem( const_cast<YItem *>( currentItem ),
true );
581 if ( notify() && immediateMode() && ( oldCurrentItem != currentItem ) )
582 ret = NCursesEvent::SelectionChanged;
584 yuiDebug() <<
"Notify: " << ( notify() ?
"true" :
"false" ) <<
585 " Return event: " << ret.reason << std::endl;
596 void NCTree::deleteAllItems()
598 YTree::deleteAllItems();
599 myPad()->ClearTable();
virtual NCTreePad * myPad() const
Return the current pad.
int bkgd(const chtype ch)
Set the background property and apply it to the window.
void bkgdset(chtype ch)
Set the background property.
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.