libyui-ncurses  2.44.1
 All Classes Functions Variables
NCurses.h
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: NCurses.h
20 
21  Author: Michael Andres <ma@suse.de>
22 
23 /-*/
24 
25 #ifndef NCurses_h
26 #define NCurses_h
27 
28 #include <iostream>
29 #include <string>
30 #include <set>
31 #include <map>
32 
33 #include <yui/YEvent.h>
34 #include <yui/YWidget.h>
35 #include <yui/YMenuItem.h>
36 
37 #include <ncursesw/curses.h> /* curses.h: #define NCURSES_CH_T cchar_t */
38 #include <wchar.h>
39 
40 #include "ncursesw.h"
41 #include "ncursesp.h"
42 #include "position.h"
43 #include "NCstyle.h"
44 
45 class NCWidget;
46 class NCDialog;
47 
48 
50 {
51 public:
52 
53  int errval_i;
54  std::string errmsg_t;
55 
56  NCursesError( const char * msg = "unknown error", ... );
57  NCursesError( int val, const char * msg = "unknown error", ... );
58 
59  virtual ~NCursesError() {}
60 
61  NCursesError & NCError( const char * msg = "unknown error", ... );
62  NCursesError & NCError( int val, const char * msg = "unknown error", ... );
63 
64  virtual const char * location() const { return "NCurses"; }
65 };
66 
67 extern std::ostream & operator<<( std::ostream & STREAM, const NCursesError & OBJ );
68 
69 
70 
72 {
73 
74 public:
75 
76  enum Type
77  {
78  handled = -1,
79  none = 0,
80  cancel,
81  timeout,
82  button,
83  menu,
84  key
85  };
86 
87  enum DETAIL
88  {
89  NODETAIL = -1,
90  CONTINUE = -2,
91  USERDEF = -3
92  };
93 
94  Type type;
95  NCWidget * widget;
96  YMenuItem * selection; // used for MenuEvent (the menu selection)
97 
98  std::string result; // can be used for any (std::string) result
99 
100  std::string keySymbol; // used for KeyEvent (symbol pressed key)
101 
102  int detail;
103 
104  YEvent::EventReason reason;
105 
106  NCursesEvent( Type t = none, YEvent::EventReason r = YEvent::UnknownReason )
107  : type( t )
108  , widget( 0 )
109  , selection( 0 )
110  , result( "" )
111  , detail( NODETAIL )
112  , reason( r )
113  {}
114 
115  virtual ~NCursesEvent() {}
116 
117  // not operator bool() which would be propagated to almost everything
118  operator void*() const { return type != none ? ( void* )1 : ( void* )0; }
119 
120  bool operator==( const NCursesEvent & e ) const { return type == e.type; }
121 
122  bool operator!=( const NCursesEvent & e ) const { return type != e.type; }
123 
124  bool isReturnEvent() const { return type > none; }
125 
126  bool isInternalEvent() const { return type < none; }
127 
128 
129  // Some predefined events that can be used as return values
130 
131  static const NCursesEvent Activated;
132  static const NCursesEvent SelectionChanged;
133  static const NCursesEvent ValueChanged;
134 };
135 
136 extern std::ostream & operator<<( std::ostream & STREAM, const NCursesEvent & OBJ );
137 
138 
139 
140 class NCurses
141 {
142 
143  friend std::ostream & operator<<( std::ostream & STREAM, const NCurses & OBJ );
144 
145  NCurses & operator=( const NCurses & );
146  NCurses( const NCurses & );
147 
148 private:
149 
150  static NCurses * myself;
151 
152  static WINDOW * ripped_w_top;
153  static WINDOW * ripped_w_bottom;
154  static int ripinit_top( WINDOW * , int );
155  static int ripinit_bottom( WINDOW * , int );
156 
157 protected:
158 
159  SCREEN * theTerm;
160  std::string myTerm;
161  std::string envTerm;
162  WINDOW * title_w;
163  WINDOW * status_w;
164  std::string title_t;
165 
166  std::map <int, std::string> status_line;
167 
168  NCstyle * styleset;
169  NCursesPanel * stdpan;
170 
171  void init();
172  bool initialized() const { return stdpan; }
173 
174  virtual bool title_line() { return true; }
175 
176  virtual bool want_colors() { return true; }
177 
178  virtual void setup_screen();
179  virtual void init_title();
180  virtual void init_screen();
181 
182 public:
183 
184  NCurses();
185  virtual ~NCurses();
186 
187  int stdout_save;
188  int stderr_save;
189 
190  static int cols() { return ::COLS; }
191 
192  static int lines() { return ::LINES; }
193 
194  static int tabsize() { return ::TABSIZE; }
195 
196  void run();
197 
198 public:
199 
200  static const NCstyle & style();
201 
202  static void Update();
203  static void Redraw();
204  static void Refresh();
205  static void SetTitle( const std::string & str );
206  static void SetStatusLine( std::map <int, std::string> fkeys );
207  static void ScreenShot( const std::string & name = "screen.shot" );
208 
209  static void drawTitle();
210 
211 public:
212  // actually not for public use
213  static void ForgetDlg( NCDialog * dlg_r );
214  static void RememberDlg( NCDialog * dlg_r );
215  void RedirectToLog();
216  static void ResizeEvent();
217 
218 private:
219  static std::set<NCDialog*> _knownDlgs;
220 };
221 
222 
223 #define CTRL(x) ((x) & 0x1f)
224 #define KEY_TAB 011
225 #define KEY_RETURN 012
226 #define KEY_ESC 033
227 #define KEY_SPACE 040
228 #define KEY_HOTKEY KEY_MAX+1
229 
230 
231 #endif // NCurses_h