libyui-ncurses  2.44.1
 All Classes Functions Variables
NCMultiSelectionBox.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: NCMultiSelectionBox.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCMultiSelectionBox.h"
28 
29 
30 NCMultiSelectionBox::NCMultiSelectionBox( YWidget * parent,
31  const std::string & nlabel )
32  : YMultiSelectionBox( parent, nlabel )
33  , NCPadWidget( parent )
34 {
35  yuiDebug() << std::endl;
36  InitPad();
37  setLabel( nlabel );
38 }
39 
40 
41 NCMultiSelectionBox::~NCMultiSelectionBox()
42 {
43  yuiDebug() << std::endl;
44 }
45 
46 
47 int NCMultiSelectionBox::preferredWidth()
48 {
49  wsze sze = wGetDefsze();
50  return sze.W > ( int )( labelWidth() + 2 ) ? sze.W : ( labelWidth() + 2 );
51 }
52 
53 
54 int NCMultiSelectionBox::preferredHeight()
55 {
56  return wGetDefsze().H;
57 }
58 
59 
61 {
62  NCWidget::setEnabled( do_bv );
63  YMultiSelectionBox::setEnabled( do_bv );
64 }
65 
66 
67 void NCMultiSelectionBox::setSize( int newwidth, int newheight )
68 {
69  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
70 }
71 
72 
73 YItem * NCMultiSelectionBox::currentItem()
74 {
75  if ( !myPad()->Lines() )
76  return 0;
77 
78  int index = myPad()->CurPos().L;
79 
80  return itemAt( index );
81 }
82 
83 
84 
85 void NCMultiSelectionBox::setCurrentItem( YItem * item )
86 {
87  if ( item )
88  myPad()->ScrlLine( item->index() );
89 }
90 
91 void NCMultiSelectionBox::addItem( YItem * item )
92 {
93  std::vector<NCTableCol*> Items( 2U, 0 );
94 
95  if ( item )
96  {
97  YMultiSelectionBox::addItem( item );
98  Items[0] = new NCTableTag( item, item->selected() );
99 
100  // Do not set style to NCTableCol::PLAIN here, otherwise the current
101  //item will not be highlighted if the cursor is not over the widget
102 
103  Items[1] = new NCTableCol( item->label() );
104  myPad()->Append( Items );
105  DrawPad();
106  }
107 }
108 
109 
110 /**
111  * Return pointer to current line tag
112  * (holds state and yitem pointer)
113  **/
115 {
116  NCTableLine * cl = myPad()->ModifyLine( index );
117 
118  if ( !cl )
119  return 0;
120 
121  return static_cast<NCTableTag *>( cl->GetCol( 0 ) );
122 }
123 
124 
125 const NCTableTag * NCMultiSelectionBox::tagCell( int index ) const
126 {
127  const NCTableLine * cl = myPad()->GetLine( index );
128 
129  if ( !cl )
130  return 0;
131 
132  return static_cast<const NCTableTag *>( cl->GetCol( 0 ) );
133 }
134 
135 
136 
137 void NCMultiSelectionBox::deleteAllItems()
138 {
139  YMultiSelectionBox::deleteAllItems();
140  myPad()->ClearTable();
141  DrawPad();
142 }
143 
144 
145 bool NCMultiSelectionBox::isItemSelected( YItem *item )
146 {
147  if ( item )
148  return item->selected();
149  else
150  return false;
151 }
152 
153 
154 void NCMultiSelectionBox::selectItem( YItem *yitem, bool selected )
155 {
156  if ( yitem )
157  {
158  YMultiSelectionBox::selectItem( yitem, selected );
159 
160  //retrieve pointer to the line tag associated with this item
161  NCTableTag * tag = ( NCTableTag * )yitem->data();
162  YUI_CHECK_PTR( tag );
163 
164  tag->SetSelected( selected );
165 
166  DrawPad();
167  }
168 }
169 
170 
171 void NCMultiSelectionBox::deselectAllItems()
172 {
173  YMultiSelectionBox::deselectAllItems();
174 
175  for ( unsigned int i = 0; i < getNumLines(); i++ )
176  {
177  NCTableTag *t = tagCell( i );
178  YUI_CHECK_PTR( t );
179 
180  t->SetSelected( false );
181  }
182 
183  DrawPad();
184 }
185 
186 
187 
188 /**
189  * Toggle item from selected -> deselected and vice versa
190  **/
192 {
193  YItem *it = currentItem();
194  if ( it )
195  selectItem( it, !( it->selected() ) );
196 }
197 
198 
199 void NCMultiSelectionBox::setLabel( const std::string & nlabel )
200 {
201  YMultiSelectionBox::setLabel( nlabel );
202  NCPadWidget::setLabel( NCstring( nlabel ) );
203 }
204 
205 
206 /**
207  * Create empty MsB pad
208  **/
210 {
211  wsze psze( defPadSze() );
212  NCTablePad * npad = new NCTablePad( psze.H, psze.W, *this );
213  npad->bkgd( listStyle().item.plain );
214  npad->SetSepChar( ' ' );
215  return npad;
216 }
217 
218 
219 void NCMultiSelectionBox::wRecoded()
220 {
221  NCPadWidget::wRecoded();
222 }
223 
224 
225 NCursesEvent NCMultiSelectionBox::wHandleInput( wint_t key )
226 {
227  NCursesEvent ret;
228  bool valueChanged = false;
229  YItem *oldCurrentItem = currentItem();
230 
231  if ( ! handleInput( key ) )
232  {
233  YItem *citem = currentItem();
234 
235  switch ( key )
236  {
237  case KEY_SPACE:
238 
239  case KEY_RETURN:
241  valueChanged = true;
242  break;
243 
244  case '+':
245 
246  if ( !isItemSelected( citem ) )
247  {
248  selectItem( citem, true );
249  valueChanged = true;
250  }
251 
252  myPad()->ScrlDown();
253 
254  break;
255 
256  case '-':
257 
258  if ( isItemSelected( citem ) )
259  {
260  selectItem( citem, false );
261  valueChanged = true;
262  }
263 
264  myPad()->ScrlDown();
265 
266  break;
267  }
268  }
269 
270  if ( notify() )
271  {
272  if ( valueChanged )
273  ret = NCursesEvent::ValueChanged;
274  else if ( oldCurrentItem != currentItem() )
275  ret = NCursesEvent::SelectionChanged;
276  }
277 
278  return ret;
279 }
280 
int bkgd(const chtype ch)
Definition: ncursesw.h:1443
Definition: NCPad.h:93
Definition: position.h:109
virtual void setEnabled(bool do_bv)
virtual NCTablePad * myPad() const
virtual void setEnabled(bool do_bv)=0
Definition: NCWidget.cc:391
NCTableTag * tagCell(int index)
Definition: position.h:154
virtual NCPad * CreatePad()