libyui-ncurses  2.57.2
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 void NCMultiSelectionBox::setCurrentItem( YItem * item )
85 {
86  if ( item )
87  myPad()->ScrlLine( item->index() );
88 }
89 
90 
91 void NCMultiSelectionBox::addItem( YItem * item )
92 {
93  std::vector<NCTableCol*> cells( 2U, 0 );
94 
95  if ( item )
96  {
97  item->setIndex( itemsCount() );
98  YMultiSelectionBox::addItem( item );
99  cells[0] = new NCTableTag( item, item->selected() );
100 
101  // Do not set style to NCTableCol::PLAIN here, otherwise the current
102  // item will not be highlighted if the cursor is not over the widget
103 
104  cells[1] = new NCTableCol( item->label() );
105  myPad()->Append( cells, item->index() );
106  DrawPad();
107  }
108 }
109 
110 
111 /**
112  * Return pointer to current line tag
113  * (holds state and yitem pointer)
114  **/
116 {
117  NCTableLine * cl = myPad()->ModifyLine( index );
118 
119  if ( !cl )
120  return 0;
121 
122  return static_cast<NCTableTag *>( cl->GetCol( 0 ) );
123 }
124 
125 
126 const NCTableTag * NCMultiSelectionBox::tagCell( int index ) const
127 {
128  const NCTableLine * cl = myPad()->GetLine( index );
129 
130  if ( !cl )
131  return 0;
132 
133  return static_cast<const NCTableTag *>( cl->GetCol( 0 ) );
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 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 
wsze
Screen dimension (screen size) in the order height, width: (H, W)
Definition: position.h:154
NCstring
A string with an optional hot key.
Definition: NCstring.h:36
NCTablePadBase::ClearTable
void ClearTable()
Clear all content.
Definition: NCTablePadBase.cc:51
NCTableCol
One cell in an NCTableLine with a label and a cell-specific style.
Definition: NCTableItem.h:422
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
NCTablePad
An NCPad for an NCTable.
Definition: NCTablePad.h:62
NCTablePadBase::Append
void Append(NCTableLine *item)
Add one item to the end of _items.
Definition: NCTablePadBase.h:147
NCMultiSelectionBox::CreatePad
virtual NCPad * CreatePad()
Create empty MsB pad.
Definition: NCMultiSelectionBox.cc:209
NCMultiSelectionBox::toggleCurrentItem
void toggleCurrentItem()
Toggle item from selected -> deselected and vice versa.
Definition: NCMultiSelectionBox.cc:191
NCTablePadBase::CurPos
virtual wpos CurPos() const
CurPos().L is the index of the selected item.
Definition: NCTablePadBase.cc:188
NCTablePadBase::ModifyLine
NCTableLine * ModifyLine(unsigned idx)
Return line at idx for read-write operations and mark it as modified.
Definition: NCTablePadBase.cc:93
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
NCMultiSelectionBox::myPad
virtual NCTablePad * myPad() const
Overload myPad to narrow the type.
Definition: NCMultiSelectionBox.h:48
NCursesWindow::bkgd
int bkgd(const chtype ch)
Set the background property and apply it to the window.
Definition: ncursesw.h:1445
NCTableLine::GetCol
NCTableCol * GetCol(unsigned idx)
Return a non-const pointer for read/write operations to the column (the cell) with the specified inde...
Definition: NCTableItem.cc:293
NCMultiSelectionBox::setEnabled
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCMultiSelectionBox.cc:60
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
NCMultiSelectionBox::tagCell
NCTableTag * tagCell(int index)
Return pointer to current line tag (holds state and yitem pointer)
Definition: NCMultiSelectionBox.cc:115
NCTableTag
A column (one cell) used as a selection marker: [ ]/[x] or ( )/(x).
Definition: NCTableItem.h:647