NCURSES function brief reference

xiaoxiao2021-03-06  113

NCURSES function brief reference

Benbear

table of Contents

Description Introduction Initialization and End Basic Input, Output Function Preview Function Name, Parameter Habits Getch () Window Window Color Panel, Menus and Forms Other Resources

Description

Write here for the experience of using the NCurses library, I hope to be useful to you. Due to the shortage of conditions, I just used NCurses in Debian Linux 3.0 r2 installed in WMware WorkStation 4.0.5, so the one-sidedness mentioned here is inevitable, please check. If you have any questions, you can send me an email: cxj1024@etang.com.

Introduction

NCURSES is a library developed under Linux, which is quite powerful. It is based on Window, on which, on this basis, supports Panel, Memus, and Forms, etc., more powerful elements. For more information, please refer to the information.

Initialization and end

Of course, in order to use the ncurses library, you should #include , you should add -lncurses when compiling.

Before using the ncurses library, you must initialize first; you must end after it is used, otherwise the terminal may not normal. Initialization is a series of actions, content related to function keys, input echo, wrap, color, etc.

In general, the first function call must be initscr (). This function initializes the terminal and clears the screen. Its parameters are empty, and the return value is generally not used. It will initially initialize a variable called STDSCR, stdscr is a bit like stdin or stdout, which is NCurses default window.

Then, INITSCR () should be other initialization, hereby introduced.

Instant button. RAW () and CBREAK () two functions are used to turn off row buffers to handle the buttons immediately. Just cbreak () can withdrew from the program while pressing Ctrl-C, I usually use this. There is no parameters in two functions.

Use the function key. KeyPad () can be used to open a function key of a window that enables the program to identify the buttons such as F1, F2, Up, DOWN. This feature of STDSCR is typically opened with keypad (stdscr, true).

Press button. Echo () and noecho (), the latter can turn off the button echo, which is especially useful when using getch (). In the middle of the program, both can be used alternately. There is no parameters in two functions.

Removal. NL () and nonl (), used to set the wrap, whether or '/ r' '/ n'. Generally, it is necessary to use the default, so don't call it. There is no parameters in two functions.

Start color. HAS_COLORS () returns a BOOL value to indicate whether the terminal supports color; support is true. If you support colors, you can use start_color () to open the color function.

Terminal, my initialization code is generally:

INITSCR (); cbreak (); noecho (); start_color (); keypad (stdscr, true);

End is simple, just call endwin (); but you must remember call. as follows:

Endwin ();

Basic input, output function preview

Since ncurses have taken over the input and output, the standard I / O in is not easy to use; of course, you can use it, but it is likely to be wrong.

I am very happy that the basic input output in ncurses is similar to the standard I / O, such as the output of PrintW (), and the input has scanw (), huh, huh, just convert "F" to "W", other definitions exactly the same. The basic output function has addCH (), addStr (), printw (), which outputs a character, outputs a string and formatted output. Many functions will evolve on these functions. The two major elements of evolution are: coordinates and windows.

The coordinates are from (0, 0), but note that the subscript in nCurses is represented by (Y, X) in the upper left corner of the window, but this must be remembered. Move (Y, X); mobile STDSCR's cursor to a location can be moved directly. Window Window is the basic operation object of NCurses, in fact, all input and outputs are associated with the window, if the default is STDSCR.

Of course, it is also an evolution from AddStr () to AddNSTR ().

For example, addCH () This series of functions are: addCH (), MVADDCH (), WADCH () and MvwadDCH () four. "MV" represents Move, which is to move to a coordinate. And "w" indicates that Window is output to a window. Move () can also vary into WMOVE ().

It should be noted that these outputs are not directly output to the screen, but output to a window (of course, the default is STDSCR). So you should use refresh () to refresh the output of STDSCR; corresponding, useless () can refresh the output of a window; WREFRESH (STDSCR) == Refresh ().

Basic input functions have getCh (), getStr (), scanw (), or also evolve a lot of functions.

Echo's settings will have a certain impact on the input function, which may not be returned when NOECHO is entered. If so, you can open the ECHO first and then turn it off. According to the information I see, the function like Scanw () will automatically open the Echo, but it actually does have no resumes.

Function life, parameter habits

These input and output functions are very standardized, and below is summarized.

If there is no prefix, it means input or output at the current location of STDSCR. If the prefix is ​​"W", it means operating at the current location of a window. If the prefix is ​​"MV", it means to operate at a location to STDSCR. If the prefix is ​​"MVW", it means operation on a certain location of a window.

The arrangement of the parameters is like this: ([WINDOW,] [Y, X,] Arg1, AGR2, ...). That is, if there is a window, the window is the first parameter; second, if there is (Y, X), before the window is in other parameters. For example, the AddCH () series is: AddCH (CH), MVADDCH (Y, X, CH), WADDCH (WIN, CH), MVWADDCH (WIN, Y, X, CH).

Know these rules, it is beneficial to many functions of many memories.

Getch ()

Getch () is a very useful function that can read the function key, so it is almost a function that must be used. If echo (), getch () is echoed; otherwise, nocho () did not return. Function keys can only be used after KeyPad (win, true).

Getch () has no parameters, the return value indicates the value of the button, which can be an ASCII value or a function key. Function keys can be found in Man 3 getch, all in KEY_. Some commonly used: Return key: key_backspaceinsert: key_icdelete: key_dc key: key_up left direction key: key_leftf1: key_f (1) f2: key_f (2) Pageup: key_ppagepagedown: key_npage

Tab, ESC, ENTER is all corresponding to the ASCII value.

Getch () reads the key value from the STDSCR, and WgetCH () reads the key value from a window.

Window window

Window is the basic unit of ncurses management, and the basis of the application, and the Menus Library and Forms Library you want to say will be used to manage its output.

Each window corresponds to a data structure, and Ncurses assigns these spaces when creating a window, so the window should be deleted. A window's data structure records the size of the window, the position of the upper left corner of the window, a number of data display buffers, current colors, etc.

You should know that stdscr is the NCURSES default window, but also the first window after the startup. NewWin () can create a new window, its statement is:

Window * newwin (int Height, int width, int y, int x);

Where (Y, X) is the coordinates of the upper left corner of the window. After the window is established, if you want to open the function key, you should call KeyPad (). I usually use two together.

After the window is used, you should delete it, the function is DELWIN (), the parameter is the window pointer to be deleted. Before deleting the window, you may have to clear the content in the window, you can use wclear (). But wclear () does not seem to use the current color, but only makes the window black.

Box () is a useful function that draws a single frame for a window, which is generally used: Box (win, 0, 0) ;. Similar functions have wborder (). When the picture is like a character other than some ASCII, ncurses define some characters, starting with ASC_, if the terminal does not support these characters, NCurses will automatically replace similar characters.

In the window, you can output something, addch (), addstr () and printw () several types of functions can be used freely, but must remember WREFRESH (). It should be noted that the Refresh's refresh is optimized, which only refreshes the changed portion, so efficiency is relatively high.

When the window is more, in order to display the window buried below, you can use the RedRawwin (win) to redraw the window, this time WREFRESH () is not so effective.

WnoutRefresh (win) and doupdate () are a good pair of matching. When you have a lot of windows, you use Wrefresh () for each window than when you are. At this point you can use WnoutRefresh () to update the buffer of each window, and finally displays with DOUPDATE ().

I didn't use a complicated many windows, so it is not too understandable between the windows.

You can also create a sub-window for a window, and the function is subwin () and derwin (), and their definition is:

Window * Subwin (Window * ORIG, INT LINES, INT COLS, INT Y, INT X); Window * Derwin (Window * Orig, Int Lines, Int Cols, Int Y, INT X); they are created in window ORIG A LINES * COLS size sub-window, their difference is that the former (Y, X) is relative to the screen, while the latter (Y, X) is relative to the window ORIG. Sub window sharing window ORIG memory. Before the Wrefresh sub-window, TouchWin (ORIG) or Touchline (ORIG) should be called, otherwise it may be not output.

colour

In fact, color in ncurses is just part of the character properties, and the other is highlighting. I prefer to collect them as colors. You can get a beautiful interface by setting the properties of the character and highlighting.

Only after start_color (), you can make the color; of course, highlight does not need this.

NCURSES predefined eight colors, starting with Color_, with color_black, color_red, color_green, etc. What is quite strange is that you have to use these colors, that is, specify the foreground color and background colors. INIT_PAIR (Pair, G, B) is used to specify a color_pair ("color pair"), and Pair is a value between 1 to Color_PAIRS-1. Color_pairs specifies the maximum number of chromatic pairs, and the corresponding colors specifies the maximum number of colors.

Highlight attributes are starting with A_, with A_NORMAL, A_REVERSE, A_UNDERLINE, A_BOLD, and the like. But these highlight properties have a big relationship with the terminal, for example, I can only use A_REVERSE, which is basically the anti-color.

Attron () and attroff () are used to set the properties of the character, and the characters output will have the changed properties. For example, attron (a_reverse) can open the anti-color properties, it is possible to turn black words in black. Attron (color_pair (n)) can open the foreground and background corresponding to N. Note that after Attron (), it should be used with attroff (). Atttron (), highlighting and color should be available simultaneously through "or".

A tip of setting colors is to use color itself as a serial number, such as init_pair (color_red, color_red, color_black); so color_pair (color_red) is obvious.

The CHTYPE type can carry characters and their properties simultaneously. This is the parameter type of addCH ().

Wchgat () is a very useful function that changes the properties of several bytes from the current cursor position, which is especially useful when the menu or button is displayed. But I was depressed when I was using this function. I don't know why, this function is always inappropriate.

I am now using wchgat () is: Apply for a sub-window via Derwin (), then use wchgat () to change the properties of the character in the sub-window, and then update to the parent window. There is a lot of problems, actually mvwchgat () does not work properly, so I have only first wmove () and then wchgat ().

The parameters of chgat () have four, the first is to change the length of the character of the property, -1 indicate the end of the window; the second is the attribute; the third is color number, be careful not to use color_pair, direct use color pair The serial number; the fourth parameter is retained, and it is really filled with NULL. It returns the original cursor from the current cursor to change the properties of several characters. Oh, you should know the relationship between chgat () and wchgat (). Panel, Menus and Forms

Ncurses also have these three libraries: Panel Library, Menus Library and FormsLibrary. The header files are , and ; -lpanel, -lmenu, and -lform, respectively, pay attention to -lncurses, respectively.

Panel does not understand; Menus and Forms are just simple copy examples.

Menus and Forms must be manually created for basic elements, then have a "driver" function to drive Menus or Forms. What you have to do is to receive a button from a wgetch (), transform into a command and then drive Menus or Forms through the Drive function.

Menus mainly provides support for menus; Forms provides some input components, and the like.

other

There are a lot of functions I have not used, and it is simply listed below.

ERASE () is similar to Clear (); CLRTOBOT () and CLRTOEOL () are smaller ranges. Mvwin () can be used to move the window; more functions reference Man 3 newwin.

The function key of the getch () seems to contain a button.

I don't know what function can be displayed in newwin (), saving the display of the screen, restored after Delwin ().

NCURSES is not good for stacking windows.

Reference

1, Eric S. Raymond and Zeyd M. Ben-Halim, "Writing Programs with ncurses"

2, Pradeep Padala, "NCURSES Programming Howto"

3, Man ncurses (3X)

4, http://poet.cosoft.org.cn/index.htm, "NCURS program programming hoewto"

转载请注明原文地址:https://www.9cbs.com/read-105205.html

New Post(0)