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