libyui-ncurses  2.57.2
NCMenuButton.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: NCMenuButton.cc
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
27 #include "NCurses.h"
28 #include "NCMenuButton.h"
29 #include "NCPopupMenu.h"
30 #include "YNCursesUI.h"
31 
32 
33 NCMenuButton::NCMenuButton( YWidget * parent,
34  std::string nlabel )
35  : YMenuButton( parent, nlabel )
36  , NCWidget( parent )
37 {
38  // yuiDebug() << std::endl;
39  setLabel( nlabel );
40  hotlabel = &label;
41 }
42 
43 
44 NCMenuButton::~NCMenuButton()
45 {
46  // yuiDebug() << std::endl;
47 }
48 
49 
50 int NCMenuButton::preferredWidth()
51 {
52  return wGetDefsze().W;
53 }
54 
55 
56 int NCMenuButton::preferredHeight()
57 {
58  return wGetDefsze().H;
59 }
60 
61 
62 void NCMenuButton::setEnabled( bool do_bv )
63 {
64  NCWidget::setEnabled( do_bv );
65  YMenuButton::setEnabled( do_bv );
66 }
67 
68 
69 void NCMenuButton::setSize( int newwidth, int newheight )
70 {
71  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
72 }
73 
74 
75 NCursesEvent NCMenuButton::wHandleInput( wint_t key )
76 {
77  NCursesEvent ret;
78 
79  switch ( key )
80  {
81  case KEY_HOTKEY:
82  case KEY_SPACE:
83  case KEY_RETURN:
84  case KEY_DOWN:
85  ret = postMenu();
86  break;
87  }
88 
89  return ret;
90 }
91 
92 
93 void NCMenuButton::setLabel( const std::string & nlabel )
94 {
95  label = NCstring( nlabel );
96  label.stripHotkey();
97  defsze = wsze( label.height(), label.width() + 3 );
98  YMenuButton::setLabel( nlabel );
99  Redraw();
100 }
101 
102 
103 void NCMenuButton::wRedraw()
104 {
105  if ( !win )
106  return;
107 
108  const NCstyle::StWidget & style( widgetStyle() );
109 
110  win->bkgdset( style.plain );
111 
112  if ( label.height() > 1 )
113  {
114  win->box( wrect( 0, win->size() - wsze( 0, 1 ) ) );
115  }
116 
117  win->printw( 0, 0, "[" );
118 
119  win->printw( 0, win->maxx(), "]" );
120 
121  label.drawAt( *win, style, wpos( 0, 1 ), wsze( -1, win->width() - 3 ),
122  NC::CENTER );
123 
124  win->bkgdset( style.scrl );
125  win->vline( 0, win->maxx() - 1, win->height(), ' ' );
126  haveUtf8() ?
127  win->add_wch( 0, win->maxx() - 1, WACS_DARROW )
128  : win->addch( 0, win->maxx() - 1, ACS_DARROW );
129 }
130 
131 
132 void NCMenuButton::rebuildMenuTree()
133 {
134  // NOP
135 }
136 
137 
138 NCursesEvent NCMenuButton::postMenu()
139 {
140  // Add fixed heigth of 1;
141  // dont't use win->height() because win might be invalid (bnc#931154)
142  wpos at( ScreenPos() + wpos( 1, 0 ) );
143 
144  NCPopupMenu * dialog = new NCPopupMenu( at,
145  itemsBegin(),
146  itemsEnd() );
147  YUI_CHECK_NEW( dialog );
148 
149  NCursesEvent event;
150  dialog->post( &event );
151 
152  YDialog::deleteTopmostDialog();
153 
154  NCursesEvent newEvent = NCursesEvent::none;
155 
156  if ( event == NCursesEvent::button )
157  {
158  newEvent = NCursesEvent::menu;
159  newEvent.selection = event.selection;
160  }
161 
162  return newEvent;
163 }
164 
165 
166 void NCMenuButton::activateItem( YMenuItem * item )
167 {
168  NCursesEvent event = NCursesEvent::menu;
169  event.widget = this;
170  event.selection = item;
171  YNCursesUI::ui()->sendEvent( event );
172 }
NCursesWindow::addch
int addch(const char ch)
Put attributed character to the window.
Definition: ncursesw.h:1230
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
NCMenuButton::activateItem
virtual void activateItem(YMenuItem *item)
Activate the item selected in the tree.
Definition: NCMenuButton.cc:166
NCWidget::setEnabled
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:392
NCursesWindow::box
int box()
Draw a box around the window with the given vertical and horizontal drawing characters.
Definition: ncursesw.h:1464
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
NCMenuButton::setEnabled
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCMenuButton.cc:62
NCursesWindow::vline
int vline(int len, chtype ch=0)
Draw a vertical line of len characters with the given character.
Definition: ncursesw.h:1501
NCursesWindow::height
int height() const
Number of lines in this window.
Definition: ncursesw.h:1072
NCursesWindow::add_wch
int add_wch(const cchar_t *cch)
Put a combined character to the window.
Definition: ncursesw.h:1246
NCursesWindow::printw
int printw(const char *fmt,...)
Do a formatted print to the window.
Definition: ncursesw.cc:76
NCursesEvent
Definition: NCurses.h:73
wrect
A rectangle is defined by its position and size: wpos Pos, wsze Sze.
Definition: position.h:194
NCursesWindow::bkgdset
void bkgdset(chtype ch)
Set the background property.
Definition: ncursesw.h:1450
NCursesWindow::maxx
int maxx() const
Largest x coord in window.
Definition: ncursesw.h:1092
NCPopupMenu
Definition: NCPopupMenu.h:33