libyui-ncurses  2.44.1
 All Classes Functions Variables
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 
31 
32 NCMenuButton::NCMenuButton( YWidget * parent,
33  std::string nlabel )
34  : YMenuButton( parent, nlabel )
35  , NCWidget( parent )
36 {
37  yuiDebug() << std::endl;
38  setLabel( nlabel );
39  hotlabel = &label;
40 }
41 
42 
43 NCMenuButton::~NCMenuButton()
44 {
45  yuiDebug() << std::endl;
46 }
47 
48 
49 int NCMenuButton::preferredWidth()
50 {
51  return wGetDefsze().W;
52 }
53 
54 
55 int NCMenuButton::preferredHeight()
56 {
57  return wGetDefsze().H;
58 }
59 
60 
61 void NCMenuButton::setEnabled( bool do_bv )
62 {
63  NCWidget::setEnabled( do_bv );
64  YMenuButton::setEnabled( do_bv );
65 }
66 
67 
68 void NCMenuButton::setSize( int newwidth, int newheight )
69 {
70  wRelocate( wpos( 0 ), wsze( newheight, newwidth ) );
71 }
72 
73 
74 NCursesEvent NCMenuButton::wHandleInput( wint_t key )
75 {
76  NCursesEvent ret;
77 
78  switch ( key )
79  {
80  case KEY_HOTKEY:
81  case KEY_SPACE:
82  case KEY_RETURN:
83  case KEY_DOWN:
84  ret = postMenu();
85  break;
86  }
87 
88  return ret;
89 }
90 
91 
92 void NCMenuButton::setLabel( const std::string & nlabel )
93 {
94  label = NCstring( nlabel );
95  label.stripHotkey();
96  defsze = wsze( label.height(), label.width() + 3 );
97  YMenuButton::setLabel( nlabel );
98  Redraw();
99 }
100 
101 
102 void NCMenuButton::wRedraw()
103 {
104  if ( !win )
105  return;
106 
107  const NCstyle::StWidget & style( widgetStyle() );
108 
109  win->bkgdset( style.plain );
110 
111  if ( label.height() > 1 )
112  {
113  win->box( wrect( 0, win->size() - wsze( 0, 1 ) ) );
114  }
115 
116  win->printw( 0, 0, "[" );
117 
118  win->printw( 0, win->maxx(), "]" );
119 
120  label.drawAt( *win, style, wpos( 0, 1 ), wsze( -1, win->width() - 3 ),
121  NC::CENTER );
122 
123  win->bkgdset( style.scrl );
124  win->vline( 0, win->maxx() - 1, win->height(), ' ' );
125  haveUtf8() ?
126  win->add_wch( 0, win->maxx() - 1, WACS_DARROW )
127  : win->addch( 0, win->maxx() - 1, ACS_DARROW );
128 }
129 
130 
131 void NCMenuButton::rebuildMenuTree()
132 {
133  // NOP
134 }
135 
136 
137 NCursesEvent NCMenuButton::postMenu()
138 {
139  wpos at( ScreenPos() + wpos( win->height(), 0 ) );
140  NCPopupMenu * dialog = new NCPopupMenu( at,
141  itemsBegin(),
142  itemsEnd() );
143  YUI_CHECK_NEW( dialog );
144 
145  int selection = dialog->post();
146 
147  if ( selection < 0 )
148  {
149  YDialog::deleteTopmostDialog();
150  return NCursesEvent::none;
151  }
152 
153  NCursesEvent ret = NCursesEvent::menu;
154  ret.selection = findMenuItem( selection );
155  YDialog::deleteTopmostDialog();
156 
157  return ret;
158 }
159 
160 
int printw(const char *fmt,...)
Definition: ncursesw.cc:75
int vline(int len, chtype ch=0)
Definition: ncursesw.h:1499
void bkgdset(chtype ch)
Definition: ncursesw.h:1448
virtual void setEnabled(bool do_bv)
Definition: NCMenuButton.cc:61
int maxx() const
Definition: ncursesw.h:1090
Definition: position.h:109
int height() const
Definition: ncursesw.h:1070
int addch(const char ch)
Definition: ncursesw.h:1228
virtual void setEnabled(bool do_bv)=0
Definition: NCWidget.cc:391
Definition: position.h:154
int width() const
Definition: ncursesw.h:1075
int add_wch(const cchar_t *cch)
Definition: ncursesw.h:1244