Ncurses learning experience (5) window mechanism details

9. Window mechanism

    Window (Window) mechanism is the core of the entire curses system. Through the previous examples we have seen some operation functions based on the "standard
window " (stdscr). Even designing the simplest graphical user interface (GUI) requires the use of
windows. You may need to split the screen into several parts and deal with them separately, however, it is more efficient to split the screen into individual windows and then deal with each window
independently . Another important reason to use windows is that you should always pursue a better and more manageable way of designing
your programs. If you're designing a large, complex user interface,
designing the parts in advance will improve your programming efficiency.

9.1 Basic Concepts

    A window is created by calling the newwin() function. But nothing
changes . Because the actual function of this function is to allocate memory to the structure used to operate the window, this structure contains
information such as the size and starting coordinates of the window. It can be seen that in curses, the window is an imaginary concept used to deal with each part of the
screen . The newwin() function returns a pointer to the window structure, and functions such as wprintw() require
a window pointer as a parameter. The delwin() function deletes a window and frees the memory and
information used to store the window structure.

9.2 Display window

     Unfortunately, when we create a window we can't see it, so all we have to do now is make the window show

come out. The box() function can draw a border around a defined window. Now let's look at the function in the following program :

#include <ncurses.h>
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
int main(int argc, char *argv[])
{
    WINDOW *my_win;
    int startx, starty, width, height;
    int ch;
    initscr(); /* Initialize and enter curses mode */
    cbreak(); /* line buffering disabled, pass all control information */
    keypad(stdscr, TRUE); /* The program needs to use the F1 function key*/
    height = 3;
    width = 10;
    starty = (LINES height)
    / 2; /* Calculate the number of lines at the center of the window */
    startx = (COLS width)
    / 2; /* Calculate the number of columns at the center of the window */
    printw("Press F1 to exit");
    refresh();
    my_win = create_newwin(height, width, starty, startx);

    while((ch = getch()) != KEY_F(1))
    { switch(ch)
        {     case KEY_LEFT:
              destroy_win(my_win);
                my_win = create_newwin(height, width, starty,startx);
                break;
            case KEY_RIGHT:
                destroy_win(my_win);
                my_win = create_newwin(height, width, starty,++startx);
                break;
            case KEY_UP:
                destroy_win(my_win);
                my_win = create_newwin(height, width, starty,startx);
                break;
            case KEY_DOWN:
                destroy_win(my_win);
                my_win = create_newwin(height, width, ++starty,startx);
                break;
        }
    }
    endwin(); /*end curses mode*/
    return 0;
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{
    WINDOW *local_win;
    local_win = newwin(height, width, starty, startx);
    box(local_win, 0 , 0); /* 0, 0 is the default row and column starting position of the character*/
    wrefresh(local_win); /*Refresh the window buffer, display the box */
    return local_win;
}
void destroy_win(WINDOW *local_win)
{/* box(local_win, ' ', ' '); does not clear window borders as expected. but left residues in the four corners of the window
rest characters */
    wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' ');
/*Parameter annotation 9.3:
* 1. win: the window of the current operation
* 2. ls: the character used to display the left border of the window
* 3. rs: The character used to display the right border of the window
* 4. ts: The character used to display the upper border of the window
* 5. bs: The character used to display the lower border of the window
NCURSES Programming HOWTO Chinese Version (Second Edition)
29
* 6. tl: used to display the character in the upper left corner of the window
* 7. tr: used to display the characters in the upper right corner of the window
* 8. bl: used to display the characters in the lower left corner of the window
* 9. br: used to display the characters in the lower right corner of the window
*/
wrefresh(local_win);
delwin(local_win);
}

3. Program Analysis

    Don't be afraid, this is a really big program, but it does explain something very important: it creates a window,
and you can use the arrow keys to move it around. When the user presses the direction key, it will delete the existing window and create a new window at the next
position, thus realizing the effect of window movement. Note: The window row and column
limit . Let's analyze this program line by line: The
creat_newwin() function creates a window using the newwin() function, and adds a
border to the window using the box() function. The destroy_win() function first fills the window with blank characters, thereby clearing the screen. Then call
the delwin() function to reclaim the memory allocated to the window. As the user presses the arrow keys, the values ​​of startx and starty will continue to
change and a new window will be created with the new coordinates as the starting point.
In destroy_win(), we use wborder instead of box. The reason for this is written in
the program comments (I know you ignored it, go check it out now!). The wborder() function can use characters to draw the
border . These borders are made up of four lines and four corners. In order to understand more clearly, you can try to call like this

wborader() function:
wborder(win, '|', '|', '',
'',
'+', '+', '+', '+');
The window he draws will look like this:
+---------+
|         |
|         |
|         |
|         |
|         |
|         |
+---------+


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324514530&siteId=291194637