libyui-ncurses  2.57.2
NCSelectionBox.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: NCSelectionBox.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCSelectionBox.h"
28 
29 
30 
31 NCSelectionBox::NCSelectionBox( YWidget * parent, const std::string & nlabel )
32  : YSelectionBox( parent, nlabel )
33  , NCPadWidget( parent )
34  , biglist( false )
35 {
36  // yuiDebug() << std::endl;
37  InitPad();
38  setLabel( nlabel );
39 }
40 
41 
42 NCSelectionBox::~NCSelectionBox()
43 {
44  // yuiDebug() << std::endl;
45 }
46 
47 
48 int NCSelectionBox::preferredWidth()
49 {
50  wsze sze = biglist ? myPad()->tableSize() + 2 : wGetDefsze();
51  return sze.W > (int)( labelWidth() + 2 ) ? sze.W : ( labelWidth() + 2 );
52 }
53 
54 
55 int NCSelectionBox::preferredHeight()
56 {
57  wsze sze = biglist ? myPad()->tableSize() + 2 : wGetDefsze();
58  return sze.H;
59 }
60 
61 
62 void NCSelectionBox::setSize( int newwidth, int newheight )
63 {
64  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
65 }
66 
67 
68 void NCSelectionBox::setEnabled( bool do_bv )
69 {
70  NCWidget::setEnabled( do_bv );
71  YSelectionBox::setEnabled( do_bv );
72 }
73 
74 
75 int NCSelectionBox::getCurrentItem() const
76 {
77  if ( !myPad()->Lines() )
78  return -1;
79 
80  // yuiDebug() << "Current pos: " << myPad()->CurPos().L << std::endl;
81 
82  return myPad()->CurPos().L;
83 }
84 
85 
86 std::string NCSelectionBox::getLine( int index )
87 {
88  NCTableLine * line = const_cast<NCTableLine*>( myPad()->GetLine( index ) );
89  NCTableCol * value;
90  std::string val;
91 
92  if ( line->Cols() == 1 )
93  {
94  value = line->GetItems()[0];
95  const NClabel label = value->Label();
96  const std::list<NCstring> text = label.getText();
97  std::list<NCstring>::const_iterator it = text.begin();
98 
99  while ( it != text.end() )
100  {
101  val += ( *it ).Str();
102  ++it;
103  }
104  }
105 
106  return val;
107 }
108 
109 
110 void NCSelectionBox::setCurrentItem( int index )
111 {
112  myPad()->ScrlLine( index );
113 }
114 
115 
116 void NCSelectionBox::selectItem( YItem *item, bool selected )
117 {
118  YSelectionBox::selectItem( item, selected );
119 
120  myPad()->ScrlLine( selected ? item->index() : -1 );
121 }
122 
123 
124 void NCSelectionBox::selectItem( int index )
125 {
126  YSelectionBox::deselectAllItems();
127 
128  if ( hasItems() && index >= 0 )
129  {
130  YItem * item = YSelectionBox::itemAt( index );
131 
132  if ( item )
133  {
134  // yuiDebug() << "selectItem: " << item->label().c_str() << std::endl;
135  item->setSelected( true );
136  }
137  else
138  YUI_THROW( YUIException( "Can't find selected item" ) );
139  }
140 }
141 
142 
143 void NCSelectionBox::addItem( YItem * item )
144 {
145  std::vector<NCTableCol*> cells( 1U, 0 );
146 
147  if ( item )
148  {
149  item->setIndex( itemsCount() );
150  YSelectionBox::addItem( item );
151  cells[0] = new NCTableCol( item->label() );
152  myPad()->Append( cells, item->index() );
153  DrawPad();
154 
155  if ( item->selected() )
156  myPad()->ScrlLine( myPad()->Lines() );
157  }
158 }
159 
160 
161 void NCSelectionBox::addItem( const std::string & description, bool selected )
162 {
163  YSelectionWidget::addItem( description, selected );
164 }
165 
166 
167 void NCSelectionBox::setLabel( const std::string & nlabel )
168 {
169  YSelectionBox::setLabel( nlabel );
170  NCPadWidget::setLabel( NCstring( nlabel ) );
171 }
172 
173 
174 NCPad * NCSelectionBox::CreatePad()
175 {
176  wsze psze( defPadSze() );
177  NCPad * npad = new NCTablePad( psze.H, psze.W, *this );
178  npad->bkgd( listStyle().item.plain );
179 
180  return npad;
181 }
182 
183 
184 void NCSelectionBox::wRecoded()
185 {
186  NCPadWidget::wRecoded();
187 }
188 
189 
190 NCursesEvent NCSelectionBox::wHandleInput( wint_t key )
191 {
192  NCursesEvent ret = NCursesEvent::none;
193 
194  int oldItem = getCurrentItem();
195 
196  // handle key event first
197 
198  if ( sendKeyEvents() &&
199  ( key == KEY_LEFT || key == KEY_RIGHT ) )
200  {
201  ret = NCursesEvent::key;
202 
203  switch ( key )
204  {
205  case KEY_LEFT:
206  ret.keySymbol = "CursorLeft";
207  break;
208 
209  case KEY_RIGHT:
210  ret.keySymbol = "CursorRight";
211  break;
212  }
213 
214  return ret;
215  }
216 
217  // call handleInput of NCPad
218  handleInput( key );
219 
220  int citem = getCurrentItem();
221 
222  selectItem( citem );
223 
224  switch ( key )
225  {
226  case KEY_SPACE:
227  case KEY_RETURN:
228 
229  if ( notify() && citem != -1 )
230  {
231  return NCursesEvent::Activated;
232  }
233 
234  break;
235  }
236 
237  if ( notify() && immediateMode() && oldItem != citem )
238  {
239  ret = NCursesEvent::SelectionChanged;
240  }
241 
242  return ret;
243 }
244 
245 
246 /**
247  * Clear the table and the lists holding the values
248  **/
250 {
251  YSelectionBox::deleteAllItems();
252  clearTable();
253  DrawPad();
254 }
wsze
Screen dimension (screen size) in the order height, width: (H, W)
Definition: position.h:154
NCSelectionBox::myPad
virtual NCTablePad * myPad() const
Overload myPad to narrow the type.
Definition: NCSelectionBox.h:48
NCstring
A string with an optional hot key.
Definition: NCstring.h:36
NCSelectionBox::deleteAllItems
void deleteAllItems()
Clear the table and the lists holding the values.
Definition: NCSelectionBox.cc:249
NCTableCol
One cell in an NCTableLine with a label and a cell-specific style.
Definition: NCTableItem.h:422
NCTableLine::Cols
unsigned Cols() const
Return the number of columns (cells) in this line.
Definition: NCTableItem.h:142
NCWidget::setEnabled
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:392
NCPad::ScrlLine
int ScrlLine(int line)
Scroll to a line, keeping the column.
Definition: NCPad.h:206
NClabel
Multi-line string, with optional hotkey, drawable.
Definition: NCtext.h:82
NCTablePad
An NCPad for an NCTable.
Definition: NCTablePad.h:62
NCSelectionBox::setEnabled
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCSelectionBox.cc:68
NCTablePadBase::Append
void Append(NCTableLine *item)
Add one item to the end of _items.
Definition: NCTablePadBase.h:147
NCTableLine::GetItems
std::vector< NCTableCol * > GetItems() const
Return all columns (cells).
Definition: NCTableItem.h:163
NCTablePadBase::CurPos
virtual wpos CurPos() const
CurPos().L is the index of the selected item.
Definition: NCTablePadBase.cc:188
NCTablePadBase::GetLine
const NCTableLine * GetLine(unsigned idx) const
Return the line at idx for read-only operations.
Definition: NCTablePadBase.cc:87
NCPad
A virtual window with a real viewport (which is NCursesWindow) and a scrolling mechanism.
Definition: NCPad.h:113
wpos
Screen position pair in the order line, column: (L, C)
Definition: position.h:110
NCursesWindow::bkgd
int bkgd(const chtype ch)
Set the background property and apply it to the window.
Definition: ncursesw.h:1445
NCTableLine
One line in a NCTable with multiple cells and an optional tree hierarchy.
Definition: NCTableItem.h:68
NCursesEvent
Definition: NCurses.h:73
NCPadWidget
Base class for widgets with scrollable contents.
Definition: NCPadWidget.h:40