libyui-ncurses  2.47.4
 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  // add fix heigth of 1 (dont't use win->height() because win might be invalid, bnc#931154)
140  wpos at( ScreenPos() + wpos( 1, 0 ) );
141 
142  NCPopupMenu * dialog = new NCPopupMenu( at,
143  itemsBegin(),
144  itemsEnd() );
145  YUI_CHECK_NEW( dialog );
146 
147  int selection = dialog->post();
148 
149  if ( selection < 0 )
150  {
151  YDialog::deleteTopmostDialog();
152  return NCursesEvent::none;
153  }
154 
155  NCursesEvent ret = NCursesEvent::menu;
156  ret.selection = findMenuItem( selection );
157  YDialog::deleteTopmostDialog();
158 
159  return ret;
160 }
161 
162 
int printw(const char *fmt,...)
Do a formatted print to the window.
Definition: ncursesw.cc:75
int vline(int len, chtype ch=0)
Draw a vertical line of len characters with the given character.
Definition: ncursesw.h:1499
void bkgdset(chtype ch)
Set the background property.
Definition: ncursesw.h:1448
virtual void setEnabled(bool do_bv)
Pure virtual to make sure every widget implements it.
Definition: NCMenuButton.cc:61
int maxx() const
Largest x coord in window.
Definition: ncursesw.h:1090
int box()
Draw a box around the window with the given vertical and horizontal drawing characters.
Definition: ncursesw.h:1462
Definition: position.h:109
int height() const
Number of lines in this window.
Definition: ncursesw.h:1070
int addch(const char ch)
Put attributed character to the window.
Definition: ncursesw.h:1228
virtual void setEnabled(bool do_bv)=0
Pure virtual to make sure every widget implements it.
Definition: NCWidget.cc:391
Definition: position.h:154
int width() const
Number of columns in this window.
Definition: ncursesw.h:1075
int add_wch(const cchar_t *cch)
Put a combined character to the window.
Definition: ncursesw.h:1244