50 #include <ncursesw/term.h>
54 #define YUILogComponent "ncurses"
55 #include <yui/YUILog.h>
61 #define COLORS_NEED_INITIALIZATION -1
62 #define COLORS_NOT_INITIALIZED 0
63 #define COLORS_MONOCHROME 1
64 #define COLORS_ARE_REALLY_THERE 2
70 bool NCursesWindow::b_initialized = FALSE;
78 va_start( args, fmt );
80 vsprintf( buf, fmt, args );
82 return waddstr(
w, buf );
90 va_start( args, fmt );
91 int result = wmove(
w, y, x );
96 vsprintf( buf, fmt, args );
97 result = waddstr(
w, buf );
108 const std::wstring wstr( str );
111 if ( NCstring::terminalEncoding() !=
"UTF-8" )
113 NCstring::RecodeFromWchar( wstr, NCstring::terminalEncoding(), &out );
114 return ::mvwaddnstr(
w, y, x, out.c_str(), n );
117 return ::mvwaddnwstr(
w, y, x, (
wchar_t* )str, n );
125 const std::wstring wstr( str );
128 if ( NCstring::terminalEncoding() !=
"UTF-8" )
130 NCstring::RecodeFromWchar( wstr, NCstring::terminalEncoding(), &out );
131 return ::waddnstr(
w, out.c_str(), n );
134 return ::waddnwstr(
w, (
wchar_t* )str, n );
141 int ret = mvwin_wch(
w, y, x, combined );
142 combined->attr = combined->attr & ( A_CHARTEXT | A_ALTCHARSET );
146 #ifdef NCURSES_EXT_COLORS
147 combined->ext_color = 0;
155 int ret = win_wch(
w, combined );
156 combined->attr = combined->attr & ( A_CHARTEXT | A_ALTCHARSET );
159 #ifdef NCURSES_EXT_COLORS
160 combined->ext_color = 0;
170 if ( NCstring::terminalEncoding() !=
"UTF-8" )
172 ret =
addch( inchar( y, x ) );
189 NCursesWindow::add_attr_char( )
193 if ( NCstring::terminalEncoding() !=
"UTF-8" )
195 ret =
addch( inchar() );
212 NCursesWindow::init(
void )
219 if (!env && (env = ::getenv(
"TERM"))) {
220 if (::strncmp(env,
"linux", 5) == 0)
221 back_color_erase = FALSE;
232 THROW(
new NCursesException( msg ) );
236 NCursesWindow::initialize()
238 if ( !b_initialized )
241 b_initialized = TRUE;
243 if ( colorInitialized == COLORS_NEED_INITIALIZATION )
245 colorInitialized = COLORS_NOT_INITIALIZED;
256 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
258 if ( !b_initialized )
261 w =
static_cast<WINDOW *
>(0);
269 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
271 if ( !b_initialized )
286 yuiDebug() <<
"Lines: " << lines <<
" Cols: " << cols <<
" y: " << begin_y <<
" x: " << begin_x << std::endl;
288 w = ::newwin( lines, cols, begin_y, begin_x );
301 : w(0), alloced(FALSE), par(0), subwins(0), sib(0)
303 if ( !b_initialized )
306 w = window ? window : ::stdscr;
314 int begin_y,
int begin_x,
char absrel )
315 : w(0), alloced(TRUE), par(0), subwins(0), sib(0)
331 begin_y -= win.
begy();
332 begin_x -= win.
begx();
335 if ( l + begin_y > win.
height() )
336 l = win.
height() - begin_y;
338 if ( c + begin_x > win.
width() )
339 c = win.
width() - begin_x;
345 w = ::derwin( win.
w, l, c, begin_y, begin_x );
349 yuiError() <<
"Throw " <<
wpos( begin_y, begin_x ) <<
wsze( l, c ) << std::endl;
366 WINDOW *d = ::dupwin(
w );
376 static RIPOFFINIT R_INIT[5];
377 static int r_init_idx = 0;
378 static RIPOFFINIT* prip = R_INIT;
380 extern "C" int _nc_ripoffline(
int,
int ( *init )( WINDOW*,
int ) );
385 assert((
w->_maxx + 1 ) == cols );
390 int NCursesWindow::ripoff_init( WINDOW *w,
int cols )
394 RIPOFFINIT init = *prip++;
408 int code = ::_nc_ripoffline( ripoff_lines, ripoff_init );
410 if ( code == OK && init && ripoff_lines )
412 R_INIT[r_init_idx++] = init;
427 if ( p->isDescendant( win ) )
440 p->kill_subwindows();
468 else if ( win ==
this )
496 else if (
count < 0 )
505 int NCursesWindow::colorInitialized = COLORS_NOT_INITIALIZED;
510 if ( colorInitialized == COLORS_NOT_INITIALIZED )
514 if ( ::has_colors() )
517 colorInitialized = COLORS_ARE_REALLY_THERE;
520 colorInitialized = COLORS_MONOCHROME;
523 colorInitialized = COLORS_NEED_INITIALIZATION;
532 if ( colorInitialized == COLORS_ARE_REALLY_THERE )
534 if ( pair_content( PAIR_NUMBER( w->_attrs ), &fore, &back ) )
544 return getback ? back : fore;
549 if ( colorInitialized == COLORS_ARE_REALLY_THERE )
558 if ( colorInitialized == COLORS_ARE_REALLY_THERE )
559 return PAIR_NUMBER( w->_attrs );
565 NCursesWindow::setpalette(
short fore,
short back,
short pair )
567 if ( colorInitialized == COLORS_ARE_REALLY_THERE )
568 return init_pair( pair, fore, back );
574 NCursesWindow::setpalette(
short fore,
short back )
576 if ( colorInitialized == COLORS_ARE_REALLY_THERE )
577 return setpalette( fore, back, PAIR_NUMBER( w->_attrs ) );
586 if ( colorInitialized == COLORS_ARE_REALLY_THERE )
588 if (( pair < 1 ) || ( pair > COLOR_PAIRS ) )
599 extern "C" int _nc_has_mouse(
void );
603 return (( ::has_key( KEY_MOUSE ) || ::_nc_has_mouse() )
607 NCursesPad::NCursesPad(
int lines,
int cols ) :
NCursesWindow()
615 w = ::newpad( lines, cols );
617 if ( w == ( WINDOW* )0 )
630 wrect box_area( dim.intersectRelTo( area() ) );
632 if ( box_area.Sze > 0 )
634 hline( box_area.Pos.L, box_area.Pos.C, box_area.Sze.W );
635 hline( box_area.Pos.L + box_area.Sze.H - 1, box_area.Pos.C, box_area.Sze.W );
636 vline( box_area.Pos.L, box_area.Pos.C, box_area.Sze.H );
637 vline( box_area.Pos.L, box_area.Pos.C + box_area.Sze.W - 1, box_area.Sze.H );
639 addch( box_area.Pos.L + box_area.Sze.H - 1, box_area.Pos.C, ACS_LLCORNER );
640 addch( box_area.Pos.L, box_area.Pos.C + box_area.Sze.W - 1, ACS_URCORNER );
641 addch( box_area.Pos.L + box_area.Sze.H - 1, box_area.Pos.C + box_area.Sze.W - 1, ACS_LRCORNER );
642 addch( box_area.Pos.L, box_area.Pos.C, ACS_ULCORNER );
651 int NCursesWindow::mvsubwin(
NCursesWindow * sub,
int begin_y,
int begin_x )
655 if ( sub && sub->
parent() )
658 ret = ::mvderwin( sub->
w, begin_y, begin_x );
662 ret = mvsubwin( ch, ch->w->_pary, ch->w->_parx );
671 int NCursesWindow::resize(
int lines,
int columns )
679 return ::wresize( w, lines, columns );
683 std::ostream & operator<<( std::ostream & Stream,
const NCursesWindow * Obj_Cv )
686 return Stream << *Obj_Cv;
688 return Stream <<
"(NoNCWin)";
692 std::ostream & operator<<( std::ostream & Stream,
const NCursesWindow & Obj_Cv )
694 return Stream <<
"NCWin(" << Obj_Cv.
w
void kill_subwindows()
Destroy all subwindows.
int printw(const char *fmt,...)
Do a formatted print to the window.
static int lines()
Number of lines on terminal, not window.
short getcolor() const
Actual color pair.
NCursesWindow * parent()
Get my parent.
int vline(int len, chtype ch=0)
Draw a vertical line of len characters with the given character.
NCursesWindow * par
parent, if subwindow
bool isDescendant(NCursesWindow &win)
Return TRUE if win is a descendant of this.
static int cols()
Number of cols on terminal, not window.
static int NumberOfColors()
Number of available colors.
int box()
Draw a box around the window with the given vertical and horizontal drawing characters.
int setcolor(short pair)
Set actually used palette entry.
int hline(int len, chtype ch=0)
Draw a horizontal line of len characters with the given character.
int attroff(chtype at)
Switch off the window attributes;.
int addwstr(const wchar_t *str, int n=-1)
Write the wchar_t str to the window, stop writing if the terminating NUL or the limit n is reached...
void err_handler(const char *) const THROWS(NCursesException)
Signal an error with the given message text.
virtual ~NCursesWindow()
Destructor.
int height() const
Number of lines in this window.
int add_attr_char(int y, int x)
Put attributed character from given position to the window.
int addch(const char ch)
Put attributed character to the window.
int in_wchar(cchar_t *cchar)
Retrieve combined character under the current cursor position.
static long count
count of all active windows
int begy() const
Line of top left corner relative to stdscr.
NCursesWindow * sibling()
Get the next child of my parent.
NCursesWindow()
Only for use by derived classes.
int begx() const
Column of top left corner relative to stdscr.
int keypad(bool bf)
If called with bf=TRUE, the application will interpret function keys.
static void useColors(void)
Call this routine very early if you want to have colors.
int leaveok(bool bf)
If bf is TRUE, curses will leave the cursor after an update whereever it is after the update...
bool has_mouse() const
Return TRUE if terminal supports a mouse, FALSE otherwise.
NCursesWindow * sib
next subwindow of parent
int attrset(chtype at)
Set the window attributes;.
NCursesWindow Clone()
Make an exact copy of the window.
NCursesWindow * subwins
head of subwindows std::list
int width() const
Number of columns in this window.
WINDOW * w
the curses WINDOW
static int ripoffline(int ripoff_lines, int(*init)(NCursesWindow &win))
This function is used to generate a window of ripped-of lines.
int meta(bool bf)
If called with bf=TRUE, keys may generate 8-Bit characters.
NCursesWindow * child()
Get the first child window.
int add_wch(const cchar_t *cch)
Put a combined character to the window.
bool alloced
TRUE if we own the WINDOW.