25 #include <boost/algorithm/string.hpp>
29 #define YUILogComponent "ncurses"
30 #include <yui/YUILog.h>
31 #include "NCItemSelector.h"
32 #include "YNCursesUI.h"
40 : YItemSelector( parent, enforceSingleSelection )
43 , _prefSizeDirty( true )
44 , _selectorWidth( string(
"|[x] |" ).size() )
52 const YItemCustomStatusVector & customStates )
53 : YItemSelector( parent, customStates )
56 , _prefSizeDirty( true )
64 for (
int i=0; i < customStatusCount(); ++i )
66 int len = customStatus( i ).textIndicator().size();
68 if ( _selectorWidth < len )
72 _selectorWidth += string(
"| |" ).size();
84 wsze psze( defPadSze() );
86 npad->
bkgd( listStyle().item.plain );
87 npad->SetSepChar(
' ' );
106 if ( _prefSizeDirty )
108 const int minHeight = 5;
109 const int minWidth = 20;
110 int visibleItemsCount = std::min( itemsCount(), visibleItems() );
115 for (
int i=0; i < visibleItemsCount; ++i )
117 if ( _prefSize.H > i )
123 _prefSize.H += lines.size();
125 for (
const string & line: lines )
126 _prefSize.W = std::max( _prefSize.W, (
int) line.size() + _selectorWidth );
130 _prefSize.W = std::max( _prefSize.W, minWidth );
131 _prefSize.H = std::max( _prefSize.H, minHeight );
132 _prefSizeDirty =
false;
141 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
148 return YWidget::setKeyboardFocus();
157 YItemSelector::setEnabled( do_bv );
163 _prefSizeDirty =
true;
164 YItemSelector::setVisibleItems( newVal );
170 if ( !
myPad()->Lines() )
175 return tag ? tag->origItem() : 0;
184 myPad()->ScrlLine( lineNo );
190 vector<NCTableCol*> cells( 2U, 0 );
194 _prefSizeDirty =
true;
195 int lineNo =
myPad()->Lines();
197 if ( lineNo > itemsCount() )
204 cells[0] =
new NCTableCol(
"", NCTableCol::SEPARATOR );
205 cells[1] =
new NCTableCol(
"", NCTableCol::SEPARATOR );
206 myPad()->Append( cells );
214 YItemSelector::addItem( item );
219 myPad()->Append( tableLine );
221 if ( enforceSingleSelection() && item->selected() )
222 myPad()->ScrlLine( lineNo );
229 for (
const string & line: lines )
231 cells[0] =
new NCTableCol(
"", NCTableCol::PLAIN );
232 cells[1] =
new NCTableCol( line, NCTableCol::PLAIN );
233 myPad()->Append( cells );
248 return dynamic_cast<NCTableTag *
> ( tableLine->GetCol( 0 ) );
254 for (
int lineNo = 0; lineNo < (int)
myPad()->Lines(); ++lineNo )
258 if ( tag && tag->origItem() == wantedItem )
272 YDescribedItem * descItem =
dynamic_cast<YDescribedItem *
>( item );
275 desc = descItem->description();
285 vector<string> lines;
291 boost::split( lines, desc, boost::is_any_of(
"\n" ) );
299 YItemSelector::deleteAllItems();
300 myPad()->ClearTable();
309 YItemSelector::selectItem( item, selected );
312 YUI_CHECK_PTR( tag );
314 tag->SetSelected( selected );
323 YItemSelector::deselectAllItems();
330 tag->SetSelected(
false );
386 YItem * changedItem = 0;
400 changedItem = curItem;
412 curItem->status() != 1 &&
415 setItemStatus( curItem, 1 );
416 changedItem = curItem;
419 if ( ! enforceSingleSelection() )
434 curItem->status() > 0 &&
437 setItemStatus( curItem, 0 );
438 changedItem = curItem;
441 if ( ! enforceSingleSelection() )
463 myPad()->ScrlToLastLine();
505 if ( notify() && changedItem )
517 event.selection = (YMenuItem *) item;
545 YUI_CHECK_NEW( tag );
554 if ( enforceSingleSelection() && item && item->selected() )
557 yuiDebug() <<
"Sending ValueChanged event for " << (YItemSelector* )
this << endl;
559 return NCursesEvent::ValueChanged;
569 if ( enforceSingleSelection() )
584 if ( fromStatus == toStatus )
587 if ( toStatus < 0 || toStatus > 1 )
590 if ( enforceSingleSelection() )
591 return toStatus == 1;
599 for ( YItemIterator it = itemsBegin(); it != itemsEnd(); ++it )
601 if ( *it != exceptItem )
603 (*it)->setSelected(
false );
607 tag->SetSelected(
false );
virtual void activateItem(YItem *item)
Activate selected item.
virtual NCTableTag * tagCell(int index) const
Return the tag cell (the cell with the "[x]" or "(x)" selector) for the item with the specified index...
static YNCursesUI * ui()
Access the global Y2NCursesUI.
void sendEvent(NCursesEvent event)
Send an event to the UI.
virtual NCTablePad * myPad() const
Return the pad for this widget; overloaded to narrow the type.
virtual NCPad * CreatePad()
Create the pad for this widget.
virtual YItem * currentItem() const
Return the current item, i.e.
virtual void deselectAllItems()
Deselect all items.
virtual int preferredHeight()
Return the preferred height for this widget.
virtual void cycleCurrentItemStatus()
Cycle the status of the current item through its possible values.
std::vector< std::string > descriptionLines(YItem *item) const
Return the description text for an item as multiple lines.
virtual void setEnabled(bool do_bv)
Enable or disable this widget.
virtual NCTableTag * createTagCell(YItem *item)
Create a tag cell for an item.
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
NCItemSelectorBase(YWidget *parent, bool enforceSingleSelection)
Standard constructor.
virtual void selectItem(YItem *item, bool selected)
Select or deselect an item.
virtual void cycleCurrentItemStatus()=0
Cycle the status of the current item through its possible values.
virtual ~NCItemSelector()
Destructor.
virtual void setSize(int newWidth, int newHeight)
Set the size of this widget.
virtual void setVisibleItems(int newVal)
Set the number of visible items for this widget.
virtual void addItem(YItem *item)
Add an item to this widget.
virtual bool statusChangeAllowed(int fromStatus, int toStatus)
Return 'true' if a status change (by user interaction) from status 'fromStatus' to status 'toStatus' ...
virtual NCursesEvent valueChangedNotify(YItem *item)
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
YItem * scrollDownToNextItem()
If the cursor is not on the first line of an item (the line with the "[x]" selector),...
int bkgd(const chtype ch)
Set the background property and apply it to the window.
YItem * scrollUpToPreviousItem()
If the cursor is not on the first line of an item (the line with the "[x]" selector),...
virtual ~NCItemSelectorBase()
Destructor.
NCItemSelector(YWidget *parent, bool enforceSingleSelection)
Constructor.
std::string description(YItem *item) const
Return the desription text for an item.
virtual NCursesEvent valueChangedNotify(YItem *item)=0
Notification that a status value was just changed in the input handler and the 'notify' flag is set.
virtual NCursesEvent wHandleInput(wint_t key)
Handle keyboard input.
virtual NCTableTag * createTagCell(YItem *item)=0
Create a tag cell for an item.
void deselectAllItemsExcept(YItem *exceptItem)
Deselect all items except the specified one.
int linesCount() const
Return the number of lines in this widget.
virtual void setCurrentItem(YItem *item)
Set the current item, i.e.
virtual wsze preferredSize()
Return the preferred size for this widget.
virtual void deleteAllItems()
Delete all items.
int currentLine() const
Return number of the current line, i.e.
int findItemLine(YItem *item) const
Return the line number that contains the first line of 'item' or -1 if not found.
virtual int preferredWidth()
Return the preferred width for this widget.
virtual bool setKeyboardFocus()
Set the keyboard focus to this widget.