C pointer principle (60)-Ncurses-text terminal graphics

Source: Internet
Author: User

We define the delete key to delete a character, the carriage return to indicate a line break, and F12 to delete the entire line, and F1 to exit.

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/

Dp @ dp :~ /Cursestest % cat a. c

# Include

# Include

# Include

// Code by myhaspl@myhaspl.com

// Date: 2014/1/17

Int main (int argc, char * argv [])

{

Setlocale (LC_ALL ,"");

Initscr ();

Clear ();

Noecho ();

Cbreak ();

If (has_colors () = FALSE)

{

Endwin ();

Printf ("your terminal does not support color! \ N ");

Return (1 );

}

Start_color ();/* Start the color mechanism */

Mvprintw (5, COLS/2-10, "simple Editor-limited to single screen editing ");

Refresh ();

Init_pair (1, COLOR_GREEN, COLOR_BLACK );

WINDOW * win1;

Int width = COLS-14;

Int height = LINES-14;

Int x, y;

Win1 = newwin (height, width, 7,7); // new window (row, column, begin_y, begin_x)

Keypad (win1, TRUE );

Wattron (win1, COLOR_PAIR (1 ));

Box (win1, ACS_VLINE, ACS_HLINE );

Wrefresh (win1 );

Getyx (win1, y, x );

++ Y; ++ x;

While (1 ){

Int c = mvwgetch (win1, y, x );

Switch (c)

{

Case KEY_RIGHT:

++ X;

If (x> = width-1 ){

++ Y;

X = 1;

}

Break;

Case KEY_LEFT: -- x;

If (x <1 ){

-- Y;

X = width-2;

}

Break;

Case KEY_UP:

-- Y;

If (y <1 ){

Y = height-2;

}

Break;

Case KEY_DOWN:

++ Y;

If (y> = height-1 ){

Y = 1;

}

Break;

Case 10:

++ Y;

If (y> = height-1 ){

Y = 1;

}

Break;

Case KEY_F (1 ):

// Exit

Mvprintw (LINES-3, 2, "exit the editor? ");

Mvprintw (LINES-2, 2 ,"");

Refresh ();

Int ans = getch ();

If (ans = 'y' | ans = 'y ')

{

Mvprintw (LINES-2, 2, "Yes \ n ");

Refresh ();

Return 0;

} Else

Mvprintw (LINES-2, 2, "No \ n ");

Refresh ();

Break;

Case KEY_F (12 ):

// Delete a row

Wdeleteln (win1 );

Winsertln (win1 );

Box (win1, ACS_VLINE, ACS_HLINE );

Case KEY_DC:

// Delete a character

Mvwprintw (win1, y, x ,"");

Break;

Default:

Mvwprintw (win1, y, x, "% c", c );

++ X;

If (x> = width-1 ){

++ Y;

X = 1;

}

If (y> = height-1 ){

Y = 1;

}

Wrefresh (win1 );

}

}

Wattroff (win1, COLOR_PAIR (1 ));

Endwin ();

Return 0;

}

Run

Dp @ dp :~ /Cursestest % gcc-lncursesw a. c-o mytest

Dp @ dp :~ /Cursestest %./mytest

All contents of the good AI Park blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.