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(
' ' );
88 npad->AssertMinCols( 2 );
109 if ( _prefSizeDirty )
111 const int minHeight = 5;
112 const int minWidth = 20;
113 int visibleItemsCount = std::min( itemsCount(), visibleItems() );
118 for (
int i=0; i < visibleItemsCount; ++i )
120 if ( _prefSize.H > i )
126 _prefSize.H += lines.size();
128 for (
const string & line: lines )
129 _prefSize.W = std::max( _prefSize.W, (
int) line.size() + _selectorWidth );
133 _prefSize.W = std::max( _prefSize.W, minWidth );
134 _prefSize.H = std::max( _prefSize.H, minHeight );
135 _prefSizeDirty =
false;
144 wRelocate(
wpos( 0 ),
wsze( newheight, newwidth ) );
151 return YWidget::setKeyboardFocus();
160 YItemSelector::setEnabled( do_bv );
166 _prefSizeDirty =
true;
167 YItemSelector::setVisibleItems( newVal );
173 if ( !
myPad()->Lines() )
178 return tag ? tag->origItem() : 0;
195 YItemSelector::addItem( item );
205 vector<NCTableCol*> cells( 2U, 0 );
207 _prefSizeDirty =
true;
217 cells[0] =
new NCTableCol(
"", NCTableCol::SEPARATOR );
218 cells[1] =
new NCTableCol(
"", NCTableCol::SEPARATOR );
230 cells[1]->stripHotkey();
235 if ( enforceSingleSelection() && item->selected() )
244 for (
const string & line: lines )
246 cells[0] =
new NCTableCol(
"", NCTableCol::PLAIN );
247 cells[1] =
new NCTableCol( line, NCTableCol::PLAIN );
268 for (
int lineNo = 0; lineNo < (int)
myPad()->
Lines(); ++lineNo )
272 if ( tag && tag->origItem() == wantedItem )
286 YDescribedItem * descItem =
dynamic_cast<YDescribedItem *
>( item );
289 desc = descItem->description();
299 vector<string> lines;
305 boost::split( lines, desc, boost::is_any_of(
"\n" ) );
313 YItemSelector::deleteAllItems();
323 YItemSelector::selectItem( item, selected );
326 YUI_CHECK_PTR( tag );
328 tag->SetSelected( selected );
337 YItemSelector::deselectAllItems();
344 tag->SetSelected(
false );
400 YItem * changedItem = 0;
414 changedItem = curItem;
426 curItem->status() != 1 &&
430 changedItem = curItem;
433 if ( ! enforceSingleSelection() )
448 curItem->status() > 0 &&
451 setItemStatus( curItem, 0 );
452 changedItem = curItem;
455 if ( ! enforceSingleSelection() )
477 myPad()->ScrlToLastLine();
516 changedItem = curItem;
517 curItem = findItemWithHotkey( _hotKey );
524 changedItem = curItem;
536 if ( notify() && changedItem )
548 event.selection = (YMenuItem *) item;
563 for ( YItemIterator it = itemsBegin(); it != itemsEnd(); ++it )
574 if ( ! findItemWithHotkey( key ) )
604 YUI_CHECK_NEW( tag );
613 if ( enforceSingleSelection() && item && item->selected() )
616 yuiDebug() <<
"Sending ValueChanged event for " << (YItemSelector* )
this << endl;
618 return NCursesEvent::ValueChanged;
628 if ( enforceSingleSelection() )
643 if ( fromStatus == toStatus )
646 if ( toStatus < 0 || toStatus > 1 )
649 if ( enforceSingleSelection() )
650 return toStatus == 1;
658 for ( YItemIterator it = itemsBegin(); it != itemsEnd(); ++it )
660 if ( *it != exceptItem )
662 (*it)->setSelected(
false );
666 tag->SetSelected(
false );
674 YItem* NCItemSelectorBase::findItemWithHotkey(
int key )
const
676 for ( YItemConstIterator it = itemsBegin(); it != itemsEnd(); it++ )
681 if ( label.hasHotkey() && tolower( label.hotkey() ) == tolower( key ) )
Screen dimension (screen size) in the order height, width: (H, W)
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...
A string with an optional hot key.
static YNCursesUI * ui()
Access the global Y2NCursesUI.
void sendEvent(NCursesEvent event)
Send an event to the UI.
virtual bool HasHotkey(int key)
Whether any item has the given hot-key .
void ClearTable()
Clear all content.
virtual NCTablePad * myPad() const
Return the pad for this widget; overloaded to narrow the type.
One cell in an NCTableLine with a label and a cell-specific style.
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.
void createItemWidget(YItem *item)
Create a widget for the given item.
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.
int ScrlLine(int line)
Scroll to a line, keeping the column.
Multi-line string, with optional hotkey, drawable.
virtual void setEnabled(bool do_bv)
Enable or disable this widget.
virtual NCTableTag * createTagCell(YItem *item)
Create a tag cell for an item.
unsigned Lines() const
Return the number of table lines (logical, not screen)
void Append(NCTableLine *item)
Add one item to the end of _items.
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.
NCTableLine * ModifyLine(unsigned idx)
Return line at idx for read-write operations and mark it as modified.
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.
A virtual window with a real viewport (which is NCursesWindow) and a scrolling mechanism.
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.
Screen position pair in the order line, column: (L, C)
virtual void shortcutChanged()
Notification that some shortcut was changed.
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.
NCTableCol * GetCol(unsigned idx)
Return a non-const pointer for read/write operations to the column (the cell) with the specified inde...
One line in a NCTable with multiple cells and an optional tree hierarchy.
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.
A column (one cell) used as a selection marker: [ ]/[x] or ( )/(x).
virtual bool setKeyboardFocus()
Set the keyboard focus to this widget.