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 YCBTableMode mode = tableMode();
111 int col = (mode == YCBTableCheckBoxOnFirstColumn) ? colnum + 1: colnum ;
112 NCTableCol * cc = cl->GetCol ( col );
116 yuiWarning() <<
"No such colnum: " << wpos ( index, colnum ) << newtext << std::endl;
121 cc->SetLabel ( NCstring ( newtext ) );
131 void YMGA_NCCBTable::cellChanged (
const YTableCell *cell )
134 cellChanged ( cell->itemIndex(), cell->column(), cell->label() );
142 void YMGA_NCCBTable::setHeader ( std::vector<std::string> head )
144 _header.assign ( head.size(), NCstring (
"" ) );
145 YTableHeader *th =
new YTableHeader();
147 for (
unsigned int i = 0; i < head.size(); i++ )
149 th->addColumn ( head[ i ] );
150 _header[ i ] += NCstring ( head[ i ] ) ;
153 hasHeadline =
myPad()->SetHeadline ( _header );
155 YMGA_CBTable::setTableHeader ( th );
161 void YMGA_NCCBTable::getHeader ( std::vector<std::string> & header )
163 header.assign ( _header.size(),
"" );
165 for (
unsigned int i = 0; i < _header.size(); i++ )
167 header[ i ] = _header[i].Str().substr ( 1 );
176 void YMGA_NCCBTable::setAlignment (
int col, YAlignmentType al )
182 case YAlignUnchanged:
199 _header[ col ] = NCstring ( s );
204 void YMGA_NCCBTable::addItem ( YItem *yitem )
206 addItem ( yitem,
false );
214 void YMGA_NCCBTable::addItem ( YItem *yitem,
bool allAtOnce )
216 YCBTableItem *item =
dynamic_cast<YCBTableItem *
> ( yitem );
217 YUI_CHECK_PTR ( item );
218 YMGA_CBTable::addItem ( item );
219 unsigned int itemCount;
220 YCBTableMode mode = tableMode();
223 itemCount = columns();
227 std::vector<NCTableCol*> Items ( itemCount );
230 if ( mode == YCBTableCheckBoxOnFirstColumn )
233 Items[0] =
new NCTableTag ( yitem, item->checked() );
237 for ( YTableCellIterator it = item->cellsBegin();
238 it != item->cellsEnd();
241 if ( i >= columns() )
243 yuiWarning() <<
"Item contains too many columns, current is " << i
244 <<
" but only " << columns() <<
" columns are configured" << std::endl;
249 yuiDebug() <<
"current column is " << i <<
"/" << columns() <<
" (" << ( *it )->label() <<
")" << std::endl;
250 Items[i] =
new NCTableCol ( NCstring ( ( *it )->label() ) );
254 if ( mode == YCBTableCheckBoxOnLastColumn )
257 Items[columns()-1] =
new NCTableTag ( yitem, item->checked() );
261 NCTableLine *newline =
new NCTableLine ( Items, item->index() );
263 YUI_CHECK_PTR ( newline );
265 newline->setOrigItem ( item );
267 myPad()->Append ( newline );
269 if ( item->selected() )
271 setCurrentItem ( item->index() ) ;
285 void YMGA_NCCBTable::addItems (
const YItemCollection & itemCollection )
288 for ( YItemConstIterator it = itemCollection.begin();
289 it != itemCollection.end();
292 addItem ( *it,
true );
299 void YMGA_NCCBTable::deleteAllItems()
301 myPad()->ClearTable();
303 YMGA_CBTable::deleteAllItems();
310 int YMGA_NCCBTable::getCurrentItem()
312 if ( !
myPad()->Lines() )
315 return keepSorting() ?
myPad()->GetLine (
myPad()->CurPos().L )->getIndex()
316 :
myPad()->CurPos().L;
324 YItem * YMGA_NCCBTable::getCurrentItemPointer()
326 const NCTableLine *cline =
myPad()->GetLine (
myPad()->CurPos().L );
329 return cline->origItem();
338 void YMGA_NCCBTable::setCurrentItem (
int index )
340 myPad()->ScrlLine ( index );
347 void YMGA_NCCBTable::selectItem ( YItem *yitem,
bool selected )
352 YCBTableItem *item =
dynamic_cast<YCBTableItem *
> ( yitem );
353 YUI_CHECK_PTR ( item );
355 NCTableLine *line = ( NCTableLine * ) item->data();
356 YUI_CHECK_PTR ( line );
358 const NCTableLine *current_line =
myPad()->GetLine (
myPad()->CurPos().L );
359 YUI_CHECK_PTR ( current_line );
361 if ( !selected && ( line == current_line ) )
368 setCurrentItem ( line->getIndex() );
369 YMGA_CBTable::selectItem ( item, selected );
382 void YMGA_NCCBTable::selectCurrentItem()
384 const NCTableLine *cline =
myPad()->GetLine (
myPad()->CurPos().L );
387 YMGA_CBTable::selectItem ( cline->origItem(),
true );
394 void YMGA_NCCBTable::deselectAllItems()
396 setCurrentItem ( -1 );
397 YMGA_CBTable::deselectAllItems();
406 int YMGA_NCCBTable::preferredWidth()
408 wsze sze = ( biglist ) ?
myPad()->tableSize() + 2 : wGetDefsze();
416 int YMGA_NCCBTable::preferredHeight()
418 wsze sze = ( biglist ) ?
myPad()->tableSize() + 2 : wGetDefsze();
426 void YMGA_NCCBTable::setSize (
int newwidth,
int newheight )
428 wRelocate ( wpos ( 0 ), wsze ( newheight, newwidth ) );
434 void YMGA_NCCBTable::setLabel (
const std::string & nlabel )
437 NCPadWidget::setLabel ( NCstring ( nlabel ) );
444 void YMGA_NCCBTable::setEnabled (
bool do_bv )
446 NCWidget::setEnabled ( do_bv );
447 YMGA_CBTable::setEnabled ( do_bv );
453 bool YMGA_NCCBTable::setItemByKey (
int key )
455 return myPad()->setItemByKey ( key );
463 NCPad * YMGA_NCCBTable::CreatePad()
465 wsze psze ( defPadSze() );
466 NCPad * npad =
new NCTablePad ( psze.H, psze.W, *
this );
467 npad->bkgd ( listStyle().item.plain );
477 NCursesEvent YMGA_NCCBTable::wHandleInput ( wint_t key )
480 int citem = getCurrentItem();
482 if ( ! handleInput ( key ) )
488 if ( ! keepSorting() )
491 wpos at ( ScreenPos() + wpos ( win->height() / 2, 1 ) );
494 ic.reserve ( _header.size() );
497 for ( std::vector<NCstring>::const_iterator it = _header.begin();
498 it != _header.end() ; it++, i++ )
501 std::string col = ( *it ).Str();
504 YMenuItem *item =
new YMenuItem ( col ) ;
507 item->setIndex ( i );
508 ic.push_back ( item );
511 NCPopupMenu *dialog =
new NCPopupMenu ( at, ic.begin(), ic.end() );
513 int column = dialog->post();
516 myPad()->setOrder ( column,
true );
519 YDialog::deleteTopmostDialog();
521 return NCursesEvent::none;
526 if ( notify() && citem != -1 )
527 return NCursesEvent::Activated;
533 YCBTableItem *pItem =
dynamic_cast<YCBTableItem*
>(getCurrentItemPointer());
534 YMGA_CBTable::setChangedItem(pItem);
535 return NCursesEvent::ValueChanged;
543 if ( citem != getCurrentItem() )
545 if ( notify() && immediateMode() )
546 ret = NCursesEvent::SelectionChanged;
555 void YMGA_NCCBTable::checkItem ( YItem* yitem,
bool checked )
557 YCBTableItem * item =
dynamic_cast<YCBTableItem *
> ( yitem );
558 YUI_CHECK_PTR ( item );
559 NCTableLine *line = ( NCTableLine * ) item->data();
560 YUI_CHECK_PTR ( line );
562 item->check(checked);
564 int checkable_column = 0;
565 YCBTableMode mode = tableMode();
567 if ( mode == YCBTableCheckBoxOnLastColumn )
568 checkable_column = columns() -1;
570 yuiDebug() << item->label() <<
" is now " << ( checked?
"checked":
"unchecked" ) << endl;
572 NCTableTag *tag =
static_cast<NCTableTag *
> ( line->GetCol ( checkable_column ) );
573 tag->SetSelected ( checked );
581 YCBTableItem *item =
dynamic_cast<YCBTableItem *
> ( getCurrentItemPointer() );
582 YUI_CHECK_PTR ( item );
583 checkItem(item, !item->checked());