libyui-ncurses  2.57.2
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 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 
108 NCursesEvent NCDumbTab::wHandleInput( wint_t key )
109 {
110  NCursesEvent ret = NCursesEvent::none;
111 
112  switch ( key )
113  {
114  case KEY_LEFT:
115  if ( currentIndex > 0 &&
116  currentIndex <= (unsigned) itemsCount() - 1 )
117  {
118  currentIndex--;
119  wRedraw();
120 
121  ret = createMenuEvent( currentIndex );
122  }
123  break;
124 
125  case KEY_RIGHT:
126  if ( currentIndex < (unsigned) itemsCount() - 1 &&
127  currentIndex >= 0 )
128  {
129  currentIndex++;
130  wRedraw();
131 
132  ret = createMenuEvent( currentIndex );
133  }
134  break;
135 
136  case KEY_HOTKEY:
137  setCurrentTab( hotKey );
138 
139  case KEY_RETURN:
140  ret = createMenuEvent( currentIndex );
141  break;
142 
143  }
144 
145  return ret;
146 }
147 
148 
149 void NCDumbTab::setCurrentTab( wint_t key )
150 {
151 
152  YItemIterator listIt = itemsBegin();
153  NClabel tablabel;
154  unsigned i = 0;
155 
156  while ( listIt != itemsEnd() )
157  {
158  tablabel = NCstring( (*listIt)->label() );
159  tablabel.stripHotkey();
160  // yuiDebug() << "HOTkey: " << tablabel.hotkey() << " key: " << key << std::endl;
161  if ( tolower ( tablabel.hotkey() ) == tolower ( key ) )
162  {
163  currentIndex = i;
164  break;
165  }
166  ++listIt;
167  ++i;
168  }
169 }
170 
171 
172 NCursesEvent NCDumbTab::createMenuEvent( unsigned index )
173 {
174  NCursesEvent ret = NCursesEvent::menu;
175  YItem * item;
176 
177  item = itemAt( index );
178  if ( item )
179  {
180  yuiDebug() << "Show tab: " << item->label() << std::endl;
181  ret.selection = (YMenuItem *)item;
182  }
183 
184  return ret;
185 }
186 
187 
188 void NCDumbTab::addItem( YItem * item )
189 {
190  YDumbTab::addItem( item );
191 
192  NClabel tabLabel = NCstring( item->label() );
193  // yuiDebug() << "Add item: " << item->label() << std::endl;
194 
195  if ( item->selected() )
196  currentIndex = item->index();
197 }
198 
199 
200 void NCDumbTab::selectItem( YItem * item, bool selected )
201 {
202  if ( selected )
203  {
204  currentIndex = item->index();
205  // yuiDebug() << "Select item: " << item->index() << std::endl;
206  }
207 
208  YDumbTab::selectItem( item, selected );
209 
210  wRedraw();
211 }
212 
213 
214 void NCDumbTab::shortcutChanged()
215 {
216  // Any of the items might have its keyboard shortcut changed, but we don't
217  // know which one. So let's simply set all tab labels again.
218 
219  wRedraw();
220 }
221 
222 
223 void NCDumbTab::wRedraw()
224 {
225  if ( !win )
226  return;
227 
228  const NCstyle::StWidget & style( widgetStyle(true) );
229  win->bkgd( style.plain );
230  win->box();
231 
232  YItemIterator listIt = itemsBegin();
233 
234  int winWidth = win->width() - 2;
235  unsigned labelPos = 1;
236  unsigned i = 0;
237  bool nonActive = false;
238  NClabel tablabel;
239 
240  while ( listIt != itemsEnd() )
241  {
242  tablabel = NCstring( (*listIt)->label() );
243  tablabel.stripHotkey();
244  hotlabel = &tablabel;
245 
246  nonActive = ( i != currentIndex );
247 
248  if ( GetState() == NC::WSactive )
249  {
250 
251  tablabel.drawAt( *win,
252  NCstyle::StWidget( widgetStyle( nonActive) ),
253  wpos( 0, labelPos ),
254  wsze( 1, winWidth ),
255  NC::TOPLEFT, false );
256  }
257  else
258  {
259  if ( !nonActive )
260  {
261  tablabel.drawAt( *win,
262  widgetStyle().data,
263  widgetStyle().data,
264  wpos( 0, labelPos ),
265  wsze( 1, winWidth ),
266  NC::TOPLEFT, false );
267  }
268  else
269  {
270  tablabel.drawAt( *win,
271  NCstyle::StWidget( frameStyle() ),
272  wpos( 0, labelPos ),
273  wsze( 1, winWidth ),
274  NC::TOPLEFT, false );
275  }
276  }
277 
278  labelPos += tablabel.width() + 2;
279 
280  ++listIt;
281  ++i;
282 
283  if ( listIt != itemsEnd() )
284  {
285  winWidth -= tablabel.width() -1;
286  }
287  };
288 
289  if ( firstChild() )
290  {
291  NCWidget * child = dynamic_cast<NCWidget *>( firstChild() );
292 
293  if ( child )
294  child->Redraw();
295 
296  redrawChild( firstChild() );
297  }
298 }
299 
300 
301 bool NCDumbTab::HasHotkey( int key )
302 {
303  bool ret = false;
304 
305  YItemIterator listIt = itemsBegin();
306  NClabel tablabel;
307 
308  while ( listIt != itemsEnd() )
309  {
310  tablabel = NCstring( (*listIt)->label() );
311  tablabel.stripHotkey();
312  if ( tablabel.hasHotkey() && tolower ( tablabel.hotkey() ) == tolower ( key ) )
313  {
314  hotKey = tolower( key ) ;
315  ret = true;
316  }
317  ++listIt;
318  }
319 
320  // yuiDebug() << "Has hotkey: " << key << " " << (ret?"yes":"no") << std::endl;
321 
322  return ret;
323 }
324 
325 
326 void NCDumbTab::redrawChild( YWidget *widget )
327 {
328  NCWidget * child;
329 
330  if ( widget->hasChildren() )
331  {
332  YWidgetListConstIterator widgetIt = widget->childrenBegin();
333 
334  while ( widgetIt != widget->childrenEnd() )
335  {
336  child = dynamic_cast<NCWidget *>( *widgetIt );
337  if ( child )
338  child->Redraw();
339  redrawChild( *widgetIt );
340  ++widgetIt;
341  }
342  }
343 }
344 
345 
347 {
348  NCursesEvent event = NCursesEvent::menu;
349  event.widget = this;
350  // Set selected item to the event
351  YItem * item = selectedItem();
352  if ( item )
353  event.selection = (YMenuItem *) item;
354 
355  YNCursesUI::ui()->sendEvent(event);
356 }
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
YNCursesUI::ui
static YNCursesUI * ui()
Access the global Y2NCursesUI.
Definition: YNCursesUI.h:93
YNCursesUI::sendEvent
void sendEvent(NCursesEvent event)
Send an event to the UI.
Definition: YNCursesUI.cc:456
NCWidget::win
NCursesWindow * win
(owned)
Definition: NCWidget.h:103
NCWidget::setEnabled
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:392
NClabel
Multi-line string, with optional hotkey, drawable.
Definition: NCtext.h:82
NCursesWindow::box
int box()
Draw a box around the window with the given vertical and horizontal drawing characters.
Definition: ncursesw.h:1464
NCDumbTab::setEnabled
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCDumbTab.cc:89
NCWidget
Definition: NCWidget.h:46
NCstyle::StWidget
Definition: NCstyle.h:358
NCursesWindow::width
int width() const
Number of columns in this window.
Definition: ncursesw.h:1077
wpos
Screen position pair in the order line, column: (L, C)
Definition: position.h:110
NCursesWindow::bkgd
int bkgd(const chtype ch)
Set the background property and apply it to the window.
Definition: ncursesw.h:1445
NCDumbTab::activate
virtual void activate()
Activate selected tab.
Definition: NCDumbTab.cc:346
NCursesEvent
Definition: NCurses.h:73