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