libyui-ncurses  2.44.1
 All Classes Functions Variables
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()
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( const 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*> Items( 1U, 0 );
146 
147  if ( item )
148  {
149  YSelectionBox::addItem( item );
150  Items[0] = new NCTableCol( item->label() );
151  myPad()->Append( Items );
152  DrawPad();
153 
154  if ( item->selected() )
155  myPad()->ScrlLine( myPad()->Lines() );
156  }
157 }
158 
159 
160 void NCSelectionBox::addItem( const std::string & description, bool selected )
161 {
162  YSelectionWidget::addItem( description, selected );
163 }
164 
165 
166 void NCSelectionBox::setLabel( const std::string & nlabel )
167 {
168  YSelectionBox::setLabel( nlabel );
169  NCPadWidget::setLabel( NCstring( nlabel ) );
170 }
171 
172 
173 NCPad * NCSelectionBox::CreatePad()
174 {
175  wsze psze( defPadSze() );
176  NCPad * npad = new NCTablePad( psze.H, psze.W, *this );
177  npad->bkgd( listStyle().item.plain );
178 
179  return npad;
180 }
181 
182 
183 void NCSelectionBox::wRecoded()
184 {
185  NCPadWidget::wRecoded();
186 }
187 
188 
189 NCursesEvent NCSelectionBox::wHandleInput( wint_t key )
190 {
191  NCursesEvent ret = NCursesEvent::none;
192 
193  int oldItem = getCurrentItem();
194 
195  // handle key event first
196 
197  if ( sendKeyEvents() &&
198  ( key == KEY_LEFT || key == KEY_RIGHT ) )
199  {
200  ret = NCursesEvent::key;
201 
202  switch ( key )
203  {
204  case KEY_LEFT:
205  ret.keySymbol = "CursorLeft";
206  break;
207 
208  case KEY_RIGHT:
209  ret.keySymbol = "CursorRight";
210  break;
211  }
212 
213  return ret;
214  }
215 
216  // call handleInput of NCPad
217  handleInput( key );
218 
219  int citem = getCurrentItem();
220 
221  selectItem( citem );
222 
223  switch ( key )
224  {
225  case KEY_SPACE:
226  case KEY_RETURN:
227 
228  if ( notify() && citem != -1 )
229  {
230  return NCursesEvent::Activated;
231  }
232 
233  break;
234  }
235 
236  if ( notify() && immediateMode() && oldItem != citem )
237  {
238  ret = NCursesEvent::SelectionChanged;
239  }
240 
241  return ret;
242 }
243 
244 
245 /**
246  * Clear the table and the lists holding the values
247  **/
249 {
250  YSelectionBox::deleteAllItems();
251  clearTable();
252  DrawPad();
253 }
int bkgd(const chtype ch)
Definition: ncursesw.h:1443
Definition: NCPad.h:93
Definition: position.h:109
virtual NCTablePad * myPad() const
virtual void setEnabled(bool do_bv)
virtual void setEnabled(bool do_bv)=0
Definition: NCWidget.cc:391
Definition: NCtext.h:81
Definition: position.h:154