class Curses::Window

Description

The means by which to create and manage frames or windows. While there may be more than one window at a time, only one window will receive input.

Usage

require 'curses'

Curses.init_screen()

my_str = "LOOK! PONIES!"
win = Curses::Window.new( 8, (my_str.length + 10),
                          (Curses.lines - 8) / 2,
                          (Curses.cols - (my_str.length + 10)) / 2 )
win.box("|", "-")
win.setpos(2,3)
win.addstr(my_str)
# or even
win << "\nORLY"
win << "\nYES!! " + my_str
win.refresh
win.getch
win.close

Public Class Methods

new(height, width, top, left) click to toggle source

Contruct a new Curses::Window with constraints of height lines, width columns, begin at top line, and begin left most column.

A new window using full screen is called as

Curses::Window.new(0,0,0,0)
static VALUE
window_initialize(VALUE obj, VALUE h, VALUE w, VALUE top, VALUE left)
{
    struct windata *winp;
    WINDOW *window;

    rb_secure(4);
    curses_init_screen();
    TypedData_Get_Struct(obj, struct windata, &windata_type, winp);
    if (winp->window) delwin(winp->window);
    window = newwin(NUM2INT(h), NUM2INT(w), NUM2INT(top), NUM2INT(left));
    wclear(window);
    winp->window = window;

    return obj;
}

Public Instance Methods

<<(str) click to toggle source

<<

Add String str to the current string.

See also #addstr

static VALUE
window_addstr2(VALUE obj, VALUE str)
{
    window_addstr(obj, str);
    return obj;
}
addch(ch) click to toggle source

Add a character ch, with attributes, to the window, then advance the cursor.

see also the system manual for curs_addch(3)

static VALUE
window_addch(VALUE obj, VALUE ch)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    waddch(winp->window, NUM2CH(ch));

    return Qnil;
}
addstr(str) click to toggle source

add a string of characters str, to the window and advance cursor

static VALUE
window_addstr(VALUE obj, VALUE str)
{
    if (!NIL_P(str)) {
        struct windata *winp;

        StringValue(str);
        str = rb_str_export_locale(str);
        GetWINDOW(obj, winp);
        waddstr(winp->window, StringValueCStr(str));
    }
    return Qnil;
}
attroff(attrs) click to toggle source

Turns on the named attributes attrs without affecting any others.

See also #attrset

static VALUE
window_attroff(VALUE obj, VALUE attrs)
{
#ifdef HAVE_WATTROFF
    struct windata *winp;

    GetWINDOW(obj,winp);
    return INT2FIX(wattroff(winp->window,NUM2INT(attrs)));
#else
    return Qtrue;
#endif
}
attron(attrs) click to toggle source

Turns off the named attributes attrs without turning any other attributes on or off.

See also #attrset

static VALUE
window_attron(VALUE obj, VALUE attrs)
{
#ifdef HAVE_WATTRON
    struct windata *winp;
    VALUE val;

    GetWINDOW(obj,winp);
    val = INT2FIX(wattron(winp->window,NUM2INT(attrs)));
    if (rb_block_given_p()) {
        rb_yield(val);
        wattroff(winp->window,NUM2INT(attrs));
        return val;
    }
    else{
        return val;
    }
#else
    return Qtrue;
#endif
}
attrset(attrs) click to toggle source

Sets the current attributes of the given window to attrs.

The following video attributes, defined in <curses.h>, can be passed to the routines #attron, #attroff, and #attrset, or OR'd with the characters passed to addch.

A_NORMAL        Normal display (no highlight)
A_STANDOUT      Best highlighting mode of the terminal.
A_UNDERLINE     Underlining
A_REVERSE       Reverse video
A_BLINK         Blinking
A_DIM           Half bright
A_BOLD          Extra bright or bold
A_PROTECT       Protected mode
A_INVIS         Invisible or blank mode
A_ALTCHARSET    Alternate character set
A_CHARTEXT      Bit-mask to extract a character
COLOR_PAIR(n)   Color-pair number n

TODO: provide some examples here.

see also system manual curs_attr(3)

static VALUE
window_attrset(VALUE obj, VALUE attrs)
{
#ifdef HAVE_WATTRSET
    struct windata *winp;

    GetWINDOW(obj,winp);
    return INT2FIX(wattrset(winp->window,NUM2INT(attrs)));
#else
    return Qtrue;
#endif
}
begx() click to toggle source

A getter for the beginning column (X coord) of the window

static VALUE
window_begx(VALUE obj)
{
    struct windata *winp;
    int x, RB_UNUSED_VAR(y);

    GetWINDOW(obj, winp);
#ifdef getbegyx
    getbegyx(winp->window, y, x);
#else
    x = winp->window->_begx;
#endif
    return INT2FIX(x);
}
begy() click to toggle source

A getter for the beginning line (Y coord) of the window

static VALUE
window_begy(VALUE obj)
{
    struct windata *winp;
    int RB_UNUSED_VAR(x), y;

    GetWINDOW(obj, winp);
#ifdef getbegyx
    getbegyx(winp->window, y, x);
#else
    y = winp->window->_begy;
#endif
    return INT2FIX(y);
}
bkgd(ch) click to toggle source

Set the background of the current window and apply character Integer ch to every character.

see also Curses.bkgd

static VALUE
window_bkgd(VALUE obj, VALUE ch)
{
#ifdef HAVE_WBKGD
    struct windata *winp;

    GetWINDOW(obj,winp);
    return (wbkgd(winp->window, NUM2CH(ch)) == OK) ? Qtrue : Qfalse;
#else
    return Qfalse;
#endif
}
bkgdset(ch) click to toggle source

Manipulate the background of the current window with character Integer ch

see also Curses.bkgdset

static VALUE
window_bkgdset(VALUE obj, VALUE ch)
{
#ifdef HAVE_WBKGDSET
    struct windata *winp;

    GetWINDOW(obj,winp);
    wbkgdset(winp->window, NUM2CH(ch));
#endif
    return Qnil;
}
box(vert, hor) click to toggle source

set the characters to frame the window in. The vertical vert and horizontal hor character.

win = Curses::Window.new(5,5,5,5)
win.box(?|, ?-)
static VALUE
window_box(int argc, VALUE *argv, VALUE self)
{
    struct windata *winp;
    VALUE vert, hor, corn;

    rb_scan_args(argc, argv, "21", &vert, &hor, &corn);

    GetWINDOW(self, winp);
    box(winp->window, NUM2CH(vert), NUM2CH(hor));

    if (!NIL_P(corn)) {
        int cur_x, cur_y, x, y;
        chtype c;

        c = NUM2CH(corn);
        getyx(winp->window, cur_y, cur_x);
        x = NUM2INT(window_maxx(self)) - 1;
        y = NUM2INT(window_maxy(self)) - 1;
        wmove(winp->window, 0, 0);
        waddch(winp->window, c);
        wmove(winp->window, y, 0);
        waddch(winp->window, c);
        wmove(winp->window, y, x);
        waddch(winp->window, c);
        wmove(winp->window, 0, x);
        waddch(winp->window, c);
        wmove(winp->window, cur_y, cur_x);
    }

    return Qnil;
}
clear() click to toggle source

Clear the window.

static VALUE
window_clear(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wclear(winp->window);

    return Qnil;
}
close() click to toggle source

Deletes the window, and frees the memory

static VALUE
window_close(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    delwin(winp->window);
    winp->window = 0;

    return Qnil;
}
clrtoeol() click to toggle source

Clear the window to the end of line, that the cursor is currently on.

static VALUE
window_clrtoeol(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wclrtoeol(winp->window);

    return Qnil;
}
color_set(col) click to toggle source

Sets the current color of the given window to the foreground/background combination described by the Fixnum col.

static VALUE
window_color_set(VALUE obj, VALUE col)
{
    struct windata *winp;
    int res;

    GetWINDOW(obj, winp);
    res = wcolor_set(winp->window, NUM2INT(col), NULL);
    return (res == OK) ? Qtrue : Qfalse;
}
curx() click to toggle source

A getter for the current column (X coord) of the window

static VALUE
window_curx(VALUE obj)
{
    struct windata *winp;
    int x, RB_UNUSED_VAR(y);

    GetWINDOW(obj, winp);
    getyx(winp->window, y, x);
    return INT2FIX(x);
}
cury() click to toggle source

A getter for the current line (Y coord) of the window

static VALUE
window_cury(VALUE obj)
{
    struct windata *winp;
    int RB_UNUSED_VAR(x), y;

    GetWINDOW(obj, winp);
    getyx(winp->window, y, x);
    return INT2FIX(y);
}
delch() click to toggle source

Delete the character under the cursor

static VALUE
window_delch(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wdelch(winp->window);
    return Qnil;
}
deleteln() click to toggle source

Delete the line under the cursor.

static VALUE
window_deleteln(VALUE obj)
{
#if defined(HAVE_WDELETELN) || defined(wdeleteln)
    struct windata *winp;

    GetWINDOW(obj, winp);
    wdeleteln(winp->window);
#endif
    return Qnil;
}
getbkgd() click to toggle source

Returns an Interer (ch) for the character property in the current window.

static VALUE
window_getbkgd(VALUE obj)
{
#ifdef HAVE_WGETBKGD
    chtype c;
    struct windata *winp;

    GetWINDOW(obj,winp);
    return (c = getbkgd(winp->window) != ERR) ? CH2FIX(c) : Qnil;
#else
    return Qnil;
#endif
}
getch() click to toggle source

Read and returns a character from the window.

See Curses::Key to all the function KEY_* available

static VALUE
window_getch(VALUE obj)
{
    struct windata *winp;
    struct wgetch_arg arg;
    int c;

    GetWINDOW(obj, winp);
    arg.win = winp->window;
    rb_thread_call_without_gvl(wgetch_func, (void *)&arg, RUBY_UBF_IO, 0);
    c = arg.c;
    if (c == EOF) return Qnil;
    if (rb_isprint(c)) {
        char ch = (char)c;

        return rb_locale_str_new(&ch, 1);
    }
    return UINT2NUM(c);
}
getstr() click to toggle source

This is equivalent to a series f #getch calls

static VALUE
window_getstr(VALUE obj)
{
    struct windata *winp;
    struct wgetstr_arg arg;

    GetWINDOW(obj, winp);
    arg.win = winp->window;
    rb_thread_call_without_gvl(wgetstr_func, (void *)&arg, RUBY_UBF_IO, 0);
    return rb_locale_str_new_cstr(arg.rtn);
}
idlok(bool) click to toggle source

If bool is true curses considers using the hardware insert/delete line feature of terminals so equipped.

If bool is false, disables use of line insertion and deletion. This option should be enabled only if the application needs insert/delete line, for example, for a screen editor.

It is disabled by default because insert/delete line tends to be visually annoying when used in applications where it is not really needed. If insert/delete line cannot be used, curses redraws the changed portions of all lines.

static VALUE
window_idlok(VALUE obj, VALUE bf)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    idlok(winp->window, RTEST(bf) ? TRUE : FALSE);
    return Qnil;
}
inch() click to toggle source

Returns the character at the current position of the window.

static VALUE
window_inch(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    return CH2FIX(winch(winp->window));
}
insch(ch) click to toggle source

Insert a character ch, before the cursor, in the current window

static VALUE
window_insch(VALUE obj, VALUE ch)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    winsch(winp->window, NUM2CH(ch));

    return Qnil;
}
insertln() click to toggle source

Inserts a line above the cursor, and the bottom line is lost

static VALUE
window_insertln(VALUE obj)
{
#if defined(HAVE_WINSERTLN) || defined(winsertln)
    struct windata *winp;

    GetWINDOW(obj, winp);
    winsertln(winp->window);
#endif
    return Qnil;
}
keypad(bool) click to toggle source

Enables the keypad of the user's terminal.

If enabled (bool is true), the user can press a function key (such as an arrow key) and wgetch returns a single value representing the function key, as in KEY_LEFT. If disabled (bool is false), curses does not treat function keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (made to transmit) and off (made to work locally), turning on this option causes the terminal keypad to be turned on when #getch is called.

The default value for keypad is false.

static VALUE
window_keypad(VALUE obj, VALUE val)
{
    struct windata *winp;

    GetWINDOW(obj,winp);
    /* keypad() of NetBSD's libcurses returns no value */
#if defined(__NetBSD__) && !defined(NCURSES_VERSION)
    keypad(winp->window,(RTEST(val) ? TRUE : FALSE));
    return Qnil;
#else
    /* may have to raise exception on ERR */
    return (keypad(winp->window,RTEST(val) ? TRUE : FALSE)) == OK ?
        Qtrue : Qfalse;
#endif
}
keypad(bool) click to toggle source

Enables the keypad of the user's terminal.

If enabled (bool is true), the user can press a function key (such as an arrow key) and wgetch returns a single value representing the function key, as in KEY_LEFT. If disabled (bool is false), curses does not treat function keys specially and the program has to interpret the escape sequences itself. If the keypad in the terminal can be turned on (made to transmit) and off (made to work locally), turning on this option causes the terminal keypad to be turned on when #getch is called.

The default value for keypad is false.

static VALUE
window_keypad(VALUE obj, VALUE val)
{
    struct windata *winp;

    GetWINDOW(obj,winp);
    /* keypad() of NetBSD's libcurses returns no value */
#if defined(__NetBSD__) && !defined(NCURSES_VERSION)
    keypad(winp->window,(RTEST(val) ? TRUE : FALSE));
    return Qnil;
#else
    /* may have to raise exception on ERR */
    return (keypad(winp->window,RTEST(val) ? TRUE : FALSE)) == OK ?
        Qtrue : Qfalse;
#endif
}
maxx() click to toggle source

A getter for the maximum columns for the window

static VALUE
window_maxx(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
#if defined(getmaxx)
    return INT2FIX(getmaxx(winp->window));
#elif defined(getmaxyx)
    {
        int x, y;
        getmaxyx(winp->window, y, x);
        return INT2FIX(x);
    }
#else
    return INT2FIX(winp->window->_maxx+1);
#endif
}
maxy() click to toggle source

A getter for the maximum lines for the window

static VALUE
window_maxy(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
#if defined(getmaxy)
    return INT2FIX(getmaxy(winp->window));
#elif defined(getmaxyx)
    {
        int x, y;
        getmaxyx(winp->window, y, x);
        return INT2FIX(y);
    }
#else
    return INT2FIX(winp->window->_maxy+1);
#endif
}
move(y,x) click to toggle source

Moves the window so that the upper left-hand corner is at position (y, x)

static VALUE
window_move(VALUE obj, VALUE y, VALUE x)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    mvwin(winp->window, NUM2INT(y), NUM2INT(x));

    return Qnil;
}
nodelay = bool click to toggle source

When in no-delay mode #getch is a non-blocking call. If no input is ready getch returns ERR.

When in delay mode (bool is false which is the default), #getch blocks until a key is pressed.

static VALUE
window_nodelay(VALUE obj, VALUE val)
{
    struct windata *winp;
    GetWINDOW(obj,winp);

    /* nodelay() of NetBSD's libcurses returns no value */
#if defined(__NetBSD__) && !defined(NCURSES_VERSION)
    nodelay(winp->window, RTEST(val) ? TRUE : FALSE);
    return Qnil;
#else
    return nodelay(winp->window,RTEST(val) ? TRUE : FALSE) == OK ? Qtrue : Qfalse;
#endif
}
noutrefresh() click to toggle source

Refreshes the windows and lines.

#noutrefresh allows multiple updates with more efficiency than #refresh alone.

static VALUE
window_noutrefresh(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
#ifdef HAVE_DOUPDATE
    wnoutrefresh(winp->window);
#else
    wrefresh(winp->window);
#endif

    return Qnil;
}
refresh() click to toggle source

Refreshes the windows and lines.

static VALUE
window_refresh(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wrefresh(winp->window);

    return Qnil;
}
resize(lines, cols) click to toggle source

Resize the current window to Fixnum lines and Fixnum cols

static VALUE
window_resize(VALUE obj, VALUE lin, VALUE col)
{
#if defined(HAVE_WRESIZE)
    struct windata *winp;

    GetWINDOW(obj,winp);
    return wresize(winp->window, NUM2INT(lin), NUM2INT(col)) == OK ? Qtrue : Qfalse;
#else
    return Qnil;
#endif
}
scrl(num) click to toggle source

Scrolls the current window Fixnum num lines. The current cursor position is not changed.

For positive num, it scrolls up.

For negative num, it scrolls down.

static VALUE
window_scrl(VALUE obj, VALUE n)
{
#ifdef HAVE_WSCRL
    struct windata *winp;

    GetWINDOW(obj, winp);
    /* may have to raise exception on ERR */
    return (wscrl(winp->window,NUM2INT(n)) == OK) ? Qtrue : Qfalse;
#else
    return Qfalse;
#endif
}
scroll() click to toggle source

Scrolls the current window up one line.

static VALUE
window_scroll(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    /* may have to raise exception on ERR */
    return (scroll(winp->window) == OK) ? Qtrue : Qfalse;
}
scrollok(bool) click to toggle source

Controls what happens when the cursor of a window is moved off the edge of the window or scrolling region, either as a result of a newline action on the bottom line, or typing the last character of the last line.

If disabled, (bool is false), the cursor is left on the bottom line.

If enabled, (bool is true), the window is scrolled up one line (Note that to get the physical scrolling effect on the terminal, it is also necessary to call #idlok)

static VALUE
window_scrollok(VALUE obj, VALUE bf)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    scrollok(winp->window, RTEST(bf) ? TRUE : FALSE);
    return Qnil;
}
setpos(y, x) click to toggle source

A setter for the position of the cursor in the current window, using coordinates x and y

static VALUE
window_setpos(VALUE obj, VALUE y, VALUE x)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wmove(winp->window, NUM2INT(y), NUM2INT(x));
    return Qnil;
}
setscrreg(top, bottom) click to toggle source

Set a software scrolling region in a window. top and bottom are lines numbers of the margin.

If this option and #scrollok are enabled, an attempt to move off the bottom margin line causes all lines in the scrolling region to scroll one line in the direction of the first line. Only the text of the window is scrolled.

static VALUE
window_setscrreg(VALUE obj, VALUE top, VALUE bottom)
{
#ifdef HAVE_WSETSCRREG
    struct windata *winp;
    int res;

    GetWINDOW(obj, winp);
    res = wsetscrreg(winp->window, NUM2INT(top), NUM2INT(bottom));
    /* may have to raise exception on ERR */
    return (res == OK) ? Qtrue : Qfalse;
#else
    return Qfalse;
#endif
}
standend() click to toggle source

Enables the Normal display (no highlight)

This is equivalent to #attron

see also #attrset

static VALUE
window_standend(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wstandend(winp->window);
    return Qnil;
}
standout() click to toggle source

Enables the best highlighting mode of the terminal.

This is equivalent to #attron

see also #attrset

static VALUE
window_standout(VALUE obj)
{
    struct windata *winp;

    GetWINDOW(obj, winp);
    wstandout(winp->window);
    return Qnil;
}
subwin(height, width, top, left) click to toggle source

Contruct a new subwindow with constraints of height lines, width columns, begin at top line, and begin left most column.

static VALUE
window_subwin(VALUE obj, VALUE height, VALUE width, VALUE top, VALUE left)
{
    struct windata *winp;
    WINDOW *window;
    VALUE win;
    int h, w, t, l;

    h = NUM2INT(height);
    w = NUM2INT(width);
    t = NUM2INT(top);
    l = NUM2INT(left);
    GetWINDOW(obj, winp);
    window = subwin(winp->window, h, w, t, l);
    win = prep_window(rb_obj_class(obj), window);

    return win;
}
timeout=(delay) click to toggle source

Sets block and non-blocking reads for the window.

  • If delay is negative, blocking read is used (i.e., waits indefinitely for input).

  • If delay is zero, then non-blocking read is used (i.e., read returns ERR if no input is waiting).

  • If delay is positive, then read blocks for delay milliseconds, and returns ERR if there is still no input.

static VALUE
window_timeout(VALUE obj, VALUE delay)
{
    struct windata *winp;
    GetWINDOW(obj,winp);

    wtimeout(winp->window,NUM2INT(delay));
    return Qnil;
}