libyui-ncurses  2.54.5
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 fix heigth of 1 (dont't use win->height() because win might be invalid, bnc#931154)
141  wpos at( ScreenPos() + wpos( 1, 0 ) );
142 
143  NCPopupMenu * dialog = new NCPopupMenu( at,
144  itemsBegin(),
145  itemsEnd() );
146  YUI_CHECK_NEW( dialog );
147 
148  int selection = dialog->post();
149 
150  if ( selection < 0 )
151  {
152  YDialog::deleteTopmostDialog();
153  return NCursesEvent::none;
154  }
155 
156  NCursesEvent ret = NCursesEvent::menu;
157  ret.selection = findMenuItem( selection );
158  YDialog::deleteTopmostDialog();
159 
160  return ret;
161 }
162 
163 
164 void NCMenuButton::activateItem( YMenuItem * item )
165 {
166  NCursesEvent event = NCursesEvent::menu;
167  event.widget = this;
168  event.selection = item;
169  YNCursesUI::ui()->sendEvent( event );
170 }
int printw(const char *fmt,...)
Do a formatted print to the window.
Definition: ncursesw.cc:75
int height() const
Number of lines in this window.
Definition: ncursesw.h:1069
int vline(int len, chtype ch=0)
Draw a vertical line of len characters with the given character.
Definition: ncursesw.h:1498
void bkgdset(chtype ch)
Set the background property.
Definition: ncursesw.h:1447
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCMenuButton.cc:62
int box()
Draw a box around the window with the given vertical and horizontal drawing characters.
Definition: ncursesw.h:1461
virtual void activateItem(YMenuItem *item)
Activate the item selected in the tree.
static YNCursesUI * ui()
Access the global Y2NCursesUI.
Definition: YNCursesUI.h:93
void sendEvent(NCursesEvent event)
Send an event to the UI.
Definition: YNCursesUI.cc:455
Definition: position.h:109
int addch(const char ch)
Put attributed character to the window.
Definition: ncursesw.h:1227
int maxx() const
Largest x coord in window.
Definition: ncursesw.h:1089
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
int width() const
Number of columns in this window.
Definition: ncursesw.h:1074
Definition: position.h:154
int add_wch(const cchar_t *cch)
Put a combined character to the window.
Definition: ncursesw.h:1243