libyui-ncurses  2.44.1
 All Classes Functions Variables
NCDumbTab.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: NCDumbTab.cc
20 
21  Author: Gabriele Mohr <gs@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include <yui/YDialog.h>
28 #include "NCDialog.h"
29 #include "NCurses.h"
30 #include "NCDumbTab.h"
31 #include "NCPopupList.h"
32 
33 
34 NCDumbTab::NCDumbTab( YWidget * parent )
35  : YDumbTab( parent )
36  , NCWidget( parent )
37  , currentIndex( 0 )
38 {
39  framedim.Pos = wpos( 1 );
40  framedim.Sze = wsze( 2 );
41 }
42 
43 
44 NCDumbTab::~NCDumbTab()
45 {
46  yuiDebug() << std::endl;
47 }
48 
49 
50 int NCDumbTab::preferredWidth()
51 {
52  defsze.W = hasChildren() ? firstChild()->preferredWidth() : 0;
53 
54  YItemIterator listIt = itemsBegin();
55 
56  unsigned int tabBarWidth = 0;
57  NClabel tabLabel;
58 
59  while ( listIt != itemsEnd() )
60  {
61  tabLabel = NClabel( (*listIt)->label() );
62  tabBarWidth += tabLabel.width() + 1;
63  ++listIt;
64  }
65  ++tabBarWidth;
66 
67  if ( tabBarWidth > ( unsigned )defsze.W )
68  defsze.W = tabBarWidth;
69 
70  defsze.W += framedim.Sze.W;
71 
72  if ( defsze.W > NCurses::cols() )
73  defsze.W = NCurses::cols();
74 
75  return defsze.W;
76 }
77 
78 
79 int NCDumbTab::preferredHeight()
80 {
81  defsze.H = hasChildren() ? firstChild()->preferredHeight() : 0;
82  defsze.H += framedim.Sze.H;
83 
84  return defsze.H;
85 }
86 
87 
88 void NCDumbTab::setEnabled( bool do_bv )
89 {
90  yuiDebug() << "Set enabled" << std::endl;
91  NCWidget::setEnabled( do_bv );
92  YDumbTab::setEnabled( do_bv );
93 }
94 
95 
96 void NCDumbTab::setSize( int newwidth, int newheight )
97 {
98  wsze csze( newheight, newwidth );
99  wRelocate( wpos( 0 ), csze );
100  csze = wsze::max( 0, csze - framedim.Sze );
101 
102  if ( hasChildren() )
103  firstChild()->setSize( csze.W, csze.H );
104 }
105 
106 NCursesEvent NCDumbTab::wHandleInput( wint_t key )
107 {
108  NCursesEvent ret = NCursesEvent::none;
109 
110  switch ( key )
111  {
112  case KEY_LEFT:
113  if ( currentIndex > 0 &&
114  currentIndex <= (unsigned)itemsCount() -1 )
115  {
116  currentIndex--;
117  wRedraw();
118 
119  ret = createMenuEvent( currentIndex );
120  }
121  break;
122 
123  case KEY_RIGHT:
124  if ( currentIndex < (unsigned)itemsCount()-1 &&
125  currentIndex >= 0 )
126  {
127  currentIndex++;
128  wRedraw();
129 
130  ret = createMenuEvent( currentIndex );
131  }
132  break;
133 
134  case KEY_HOTKEY:
135  setCurrentTab( hotKey );
136 
137  case KEY_RETURN:
138  ret = createMenuEvent( currentIndex );
139  break;
140 
141  }
142 
143  return ret;
144 }
145 
146 void NCDumbTab::setCurrentTab( wint_t key )
147 {
148 
149  YItemIterator listIt = itemsBegin();
150  NClabel tablabel;
151  unsigned int i = 0;
152 
153  while ( listIt != itemsEnd() )
154  {
155  tablabel = NCstring( (*listIt)->label() );
156  tablabel.stripHotkey();
157  yuiDebug() << "HOTkey: " << tablabel.hotkey() << " key: " << key << std::endl;
158  if ( tolower ( tablabel.hotkey() ) == tolower ( key ) )
159  {
160  currentIndex = i;
161  break;
162  }
163  ++listIt;
164  ++i;
165  }
166 }
167 
168 NCursesEvent NCDumbTab::createMenuEvent( unsigned int index )
169 {
170  NCursesEvent ret = NCursesEvent::menu;
171  YItem * item;
172 
173  item = itemAt( index );
174  if ( item )
175  {
176  yuiMilestone() << "Show tab: " << item->label() << std::endl;
177  ret.selection = (YMenuItem *)item;
178  }
179 
180  return ret;
181 }
182 
183 void NCDumbTab::addItem( YItem * item )
184 {
185  YDumbTab::addItem( item );
186 
187  NClabel tabLabel = NCstring( item->label() );
188  yuiDebug() << "Add item: " << item->label() << std::endl;
189 
190  if ( item->selected() )
191  currentIndex = item->index();
192 }
193 
194 void NCDumbTab::selectItem( YItem * item, bool selected )
195 {
196  if ( selected )
197  {
198  currentIndex = item->index();
199  yuiDebug() << "Select item: " << item->index() << std::endl;
200  }
201 
202  YDumbTab::selectItem( item, selected );
203 
204  wRedraw();
205 }
206 
207 void NCDumbTab::shortcutChanged()
208 {
209  // Any of the items might have its keyboard shortcut changed, but we don't
210  // know which one. So let's simply set all tab labels again.
211 
212  wRedraw();
213 }
214 
215 void NCDumbTab::wRedraw()
216  {
217  if ( !win )
218  return;
219 
220  const NCstyle::StWidget & style( widgetStyle(true) );
221  win->bkgd( style.plain );
222  win->box();
223 
224  YItemIterator listIt = itemsBegin();
225 
226  int winWidth = win->width() - 2;
227  unsigned int labelPos = 1;
228  unsigned int i = 0;
229  bool nonActive = false;
230  NClabel tablabel;
231 
232  while ( listIt != itemsEnd() )
233  {
234  tablabel = NCstring( (*listIt)->label() );
235  tablabel.stripHotkey();
236  hotlabel = &tablabel;
237 
238  nonActive = (i == currentIndex)?false:true;
239 
240  if ( GetState() == NC::WSactive )
241  {
242 
243  tablabel.drawAt( *win,
244  NCstyle::StWidget( widgetStyle( nonActive) ),
245  wpos( 0, labelPos ),
246  wsze( 1, winWidth ),
247  NC::TOPLEFT, false );
248  }
249  else
250  {
251  if ( !nonActive )
252  {
253  tablabel.drawAt( *win,
254  widgetStyle( ).data,
255  widgetStyle( ).data,
256  wpos( 0, labelPos ),
257  wsze( 1, winWidth ),
258  NC::TOPLEFT, false );
259  }
260  else
261  {
262  tablabel.drawAt( *win,
263  NCstyle::StWidget( frameStyle() ),
264  wpos( 0, labelPos ),
265  wsze( 1, winWidth ),
266  NC::TOPLEFT, false );
267  }
268  }
269 
270  labelPos += tablabel.width() + 2;
271 
272  ++listIt;
273  ++i;
274 
275  if ( listIt != itemsEnd() )
276  {
277  winWidth -= tablabel.width() -1;
278  }
279  };
280 
281  if ( firstChild() )
282  {
283  NCWidget * child = dynamic_cast<NCWidget *>( firstChild() );
284 
285  if ( child )
286  child->Redraw();
287 
288  redrawChild( firstChild() );
289  }
290 }
291 
292 bool NCDumbTab::HasHotkey( int key )
293 {
294  bool ret = false;
295 
296  YItemIterator listIt = itemsBegin();
297  NClabel tablabel;
298 
299  while ( listIt != itemsEnd() )
300  {
301  tablabel = NCstring( (*listIt)->label() );
302  tablabel.stripHotkey();
303  if ( tablabel.hasHotkey() && tolower ( tablabel.hotkey() ) == tolower ( key ) )
304  {
305  hotKey = tolower ( key ) ;
306  ret = true;
307  }
308  ++listIt;
309  }
310 
311  yuiDebug() << "Has hot key: " << key << " " << (ret?"yes":"no") << std::endl;
312 
313  return ret;
314 }
315 
316 void NCDumbTab::redrawChild( YWidget *widget )
317 {
318  NCWidget * child;
319 
320  if ( widget->hasChildren() )
321  {
322  YWidgetListConstIterator widgetIt = widget->childrenBegin();
323  while ( widgetIt != widget->childrenEnd() )
324  {
325  child = dynamic_cast<NCWidget *>(*widgetIt);
326  if ( child )
327  child->Redraw();
328  redrawChild( *widgetIt );
329  ++widgetIt;
330  }
331  }
332 }
int bkgd(const chtype ch)
Definition: ncursesw.h:1443
virtual void setEnabled(bool do_bv)
Definition: NCDumbTab.cc:88
Definition: position.h:109
virtual void setEnabled(bool do_bv)=0
Definition: NCWidget.cc:391
Definition: NCtext.h:81
Definition: position.h:154
int width() const
Definition: ncursesw.h:1075