24 #define YUILogComponent "mga-ncurses" 25 #include <yui/YUILog.h> 26 #include "YMGA_NCCBTable.h" 27 #include <yui/ncurses/NCPopupMenu.h> 28 #include <yui/YMenuButton.h> 29 #include <yui/YTypes.h> 47 YMGA_NCCBTable::YMGA_NCCBTable ( YWidget * parent, YTableHeader *tableHeader, YCBTableMode mode )
48 : YMGA_CBTable ( parent, tableHeader, mode )
49 , NCPadWidget ( parent )
52 yuiDebug() << std::endl;
56 yuiMilestone() <<
" Slection mode " << mode << std::endl;
59 _header.assign ( tableHeader->columns(), NCstring (
"" ) );
60 int columNumber = columns();
61 int checkColumn = (mode == YCBTableCheckBoxOnFirstColumn ? 0 : columNumber - 1);
63 for (
int col = 0; col < columNumber; col++ )
65 if ( hasColumn ( col ) )
68 if (col == checkColumn)
72 setAlignment(col, YAlignBegin);
75 setAlignment ( col, alignment ( col ) );
77 _header[ col ] += NCstring ( tableHeader->header ( col ) ) ;
82 hasHeadline = myPad()->SetHeadline ( _header );
89 YMGA_NCCBTable::~YMGA_NCCBTable()
91 yuiDebug() << std::endl;
99 void YMGA_NCCBTable::cellChanged (
int index,
int colnum,
const std::string & newtext )
101 NCTableLine * cl =
myPad()->ModifyLine ( index );
105 yuiWarning() <<
"No such line: " << wpos ( index, colnum ) << newtext << std::endl;
109 NCTableCol * cc = cl->GetCol ( colnum );
113 yuiWarning() <<
"No such colnum: " << wpos ( index, colnum ) << newtext << std::endl;
118 cc->SetLabel ( NCstring ( newtext ) );
128 void YMGA_NCCBTable::cellChanged (
const YTableCell *cell )
131 cellChanged ( cell->itemIndex(), cell->column(), cell->label() );
139 void YMGA_NCCBTable::setHeader ( std::vector<std::string> head )
141 _header.assign ( head.size(), NCstring (
"" ) );
142 YTableHeader *th =
new YTableHeader();
144 for (
unsigned int i = 0; i < head.size(); i++ )
146 th->addColumn ( head[ i ] );
147 _header[ i ] += NCstring ( head[ i ] ) ;
150 hasHeadline =
myPad()->SetHeadline ( _header );
152 YMGA_CBTable::setTableHeader ( th );
158 void YMGA_NCCBTable::getHeader ( std::vector<std::string> & header )
160 header.assign ( _header.size(),
"" );
162 for (
unsigned int i = 0; i < _header.size(); i++ )
164 header[ i ] = _header[i].Str().substr ( 1 );
173 void YMGA_NCCBTable::setAlignment (
int col, YAlignmentType al )
179 case YAlignUnchanged:
196 _header[ col ] = NCstring ( s );
201 void YMGA_NCCBTable::addItem ( YItem *yitem )
203 addItem ( yitem,
false );
211 void YMGA_NCCBTable::addItem ( YItem *yitem,
bool allAtOnce )
213 YCBTableItem *item =
dynamic_cast<YCBTableItem *
> ( yitem );
214 YUI_CHECK_PTR ( item );
215 YMGA_CBTable::addItem ( item );
216 unsigned int itemCount;
217 YCBTableMode mode = tableMode();
220 itemCount = columns();
224 std::vector<NCTableCol*> Items ( itemCount );
227 if ( mode == YCBTableCheckBoxOnFirstColumn )
230 Items[0] =
new NCTableTag ( yitem, item->checked() );
234 for ( YTableCellIterator it = item->cellsBegin();
235 it != item->cellsEnd();
238 if ( i >= columns() )
240 yuiWarning() <<
"Item contains too many columns, current is " << i
241 <<
" but only " << columns() <<
" columns are configured" << std::endl;
246 yuiDebug() <<
"current column is " << i <<
"/" << columns() <<
" (" << ( *it )->label() <<
")" << std::endl;
247 Items[i] =
new NCTableCol ( NCstring ( ( *it )->label() ) );
251 if ( mode == YCBTableCheckBoxOnLastColumn )
254 Items[columns()-1] =
new NCTableTag ( yitem, item->checked() );
258 NCTableLine *newline =
new NCTableLine ( Items, item->index() );
260 YUI_CHECK_PTR ( newline );
262 newline->setOrigItem ( item );
264 myPad()->Append ( newline );
266 if ( item->selected() )
268 setCurrentItem ( item->index() ) ;
282 void YMGA_NCCBTable::addItems (
const YItemCollection & itemCollection )
285 for ( YItemConstIterator it = itemCollection.begin();
286 it != itemCollection.end();
289 addItem ( *it,
true );
296 void YMGA_NCCBTable::deleteAllItems()
298 myPad()->ClearTable();
300 YMGA_CBTable::deleteAllItems();
307 int YMGA_NCCBTable::getCurrentItem()
309 if ( !
myPad()->Lines() )
312 return keepSorting() ?
myPad()->GetLine (
myPad()->CurPos().L )->getIndex()
313 :
myPad()->CurPos().L;
321 YItem * YMGA_NCCBTable::getCurrentItemPointer()
323 const NCTableLine *cline =
myPad()->GetLine (
myPad()->CurPos().L );
326 return cline->origItem();
335 void YMGA_NCCBTable::setCurrentItem (
int index )
337 myPad()->ScrlLine ( index );
344 void YMGA_NCCBTable::selectItem ( YItem *yitem,
bool selected )
349 YCBTableItem *item =
dynamic_cast<YCBTableItem *
> ( yitem );
350 YUI_CHECK_PTR ( item );
352 NCTableLine *line = ( NCTableLine * ) item->data();
353 YUI_CHECK_PTR ( line );
355 const NCTableLine *current_line =
myPad()->GetLine (
myPad()->CurPos().L );
356 YUI_CHECK_PTR ( current_line );
358 if ( !selected && ( line == current_line ) )
365 setCurrentItem ( line->getIndex() );
366 YMGA_CBTable::selectItem ( item, selected );
379 void YMGA_NCCBTable::selectCurrentItem()
381 const NCTableLine *cline =
myPad()->GetLine (
myPad()->CurPos().L );
384 YMGA_CBTable::selectItem ( cline->origItem(), true );
391 void YMGA_NCCBTable::deselectAllItems()
393 setCurrentItem ( -1 );
394 YMGA_CBTable::deselectAllItems();
403 int YMGA_NCCBTable::preferredWidth()
405 wsze sze = ( biglist ) ?
myPad()->tableSize() + 2 : wGetDefsze();
413 int YMGA_NCCBTable::preferredHeight()
415 wsze sze = ( biglist ) ?
myPad()->tableSize() + 2 : wGetDefsze();
423 void YMGA_NCCBTable::setSize (
int newwidth,
int newheight )
425 wRelocate ( wpos ( 0 ), wsze ( newheight, newwidth ) );
431 void YMGA_NCCBTable::setLabel (
const std::string & nlabel )
434 NCPadWidget::setLabel ( NCstring ( nlabel ) );
441 void YMGA_NCCBTable::setEnabled (
bool do_bv )
443 NCWidget::setEnabled ( do_bv );
444 YMGA_CBTable::setEnabled ( do_bv );
450 bool YMGA_NCCBTable::setItemByKey (
int key )
452 return myPad()->setItemByKey ( key );
460 NCPad * YMGA_NCCBTable::CreatePad()
462 wsze psze ( defPadSze() );
463 NCPad * npad =
new NCTablePad ( psze.H, psze.W, *
this );
464 npad->bkgd ( listStyle().item.plain );
474 NCursesEvent YMGA_NCCBTable::wHandleInput ( wint_t key )
477 int citem = getCurrentItem();
479 if ( ! handleInput ( key ) )
485 if ( ! keepSorting() )
488 wpos at ( ScreenPos() + wpos ( win->height() / 2, 1 ) );
491 ic.reserve ( _header.size() );
494 for ( std::vector<NCstring>::const_iterator it = _header.begin();
495 it != _header.end() ; it++, i++ )
498 std::string col = ( *it ).Str();
501 YMenuItem *item =
new YMenuItem ( col ) ;
504 item->setIndex ( i );
505 ic.push_back ( item );
508 NCPopupMenu *dialog =
new NCPopupMenu ( at, ic.begin(), ic.end() );
510 int column = dialog->post();
513 myPad()->setOrder ( column,
true );
516 YDialog::deleteTopmostDialog();
518 return NCursesEvent::none;
523 if ( notify() && citem != -1 )
524 return NCursesEvent::Activated;
530 YCBTableItem *pItem =
dynamic_cast<YCBTableItem*
>(getCurrentItemPointer());
531 YMGA_CBTable::setChangedItem(pItem);
532 return NCursesEvent::ValueChanged;
540 if ( citem != getCurrentItem() )
542 if ( notify() && immediateMode() )
543 ret = NCursesEvent::SelectionChanged;
552 void YMGA_NCCBTable::checkItem ( YItem* yitem,
bool checked )
554 YCBTableItem * item =
dynamic_cast<YCBTableItem *
> ( yitem );
555 YUI_CHECK_PTR ( item );
556 NCTableLine *line = ( NCTableLine * ) item->data();
557 YUI_CHECK_PTR ( line );
559 item->check(checked);
561 int checkable_column = 0;
562 YCBTableMode mode = tableMode();
564 if ( mode == YCBTableCheckBoxOnLastColumn )
565 checkable_column = columns() -1;
567 yuiDebug() << item->label() <<
" is now " << ( checked?
"checked":
"unchecked" ) << endl;
569 NCTableTag *tag =
static_cast<NCTableTag *
> ( line->GetCol ( checkable_column ) );
570 tag->SetSelected ( checked );
578 YCBTableItem *item =
dynamic_cast<YCBTableItem *
> ( getCurrentItemPointer() );
579 YUI_CHECK_PTR ( item );
580 checkItem(item, !item->checked());
virtual NCTablePad * myPad() const
Overload myPad to narrow the type.
void toggleCurrentItem()
Toggle item from selected -> deselected and vice versa.