LINUX.ORG.RU

[ncurses] Как получить текст из полей в формах?

 


0

1
#include <form.h>

int main()
{	FIELD *field[3];
	FORM  *my_form;
	int ch;
	
	/* Initialize curses */
	initscr();
	cbreak();
	noecho();
	keypad(stdscr, TRUE);

	/* Initialize the fields */
	field[0] = new_field(1, 10, 4, 18, 0, 0);
	field[1] = new_field(1, 10, 6, 18, 0, 0);
	field[2] = NULL;

	/* Set field options */
	set_field_back(field[0], A_UNDERLINE); 	/* Print a line for the option 	*/
	field_opts_off(field[0], O_AUTOSKIP);  	/* Don't go to next field when this */
						/* Field is filled up 		*/
	set_field_back(field[1], A_UNDERLINE); 
	field_opts_off(field[1], O_AUTOSKIP);

	/* Create the form and post it */
	my_form = new_form(field);
	post_form(my_form);
	refresh();
	
	mvprintw(4, 10, "Value 1:");
	mvprintw(6, 10, "Value 2:");
	refresh();

	/* Loop through to get user requests */
	while((ch = getch()) != KEY_F(1))
	{	switch(ch)
		{	case KEY_DOWN:
				/* Go to next field */
				form_driver(my_form, REQ_NEXT_FIELD);
				/* Go to the end of the present buffer */
				/* Leaves nicely at the last character */
				form_driver(my_form, REQ_END_LINE);
				break;
			case KEY_UP:
				/* Go to previous field */
				form_driver(my_form, REQ_PREV_FIELD);
				form_driver(my_form, REQ_END_LINE);
				break;
			default:
				/* If this is a normal character, it gets */
				/* Printed				  */	
				form_driver(my_form, ch);
				break;
		}
	}

	/* Un post form and free the memory */
	unpost_form(my_form);
	free_form(my_form);
	free_field(field[0]);
	free_field(field[1]); 

	endwin();
	return 0;
}

Не могу разобраться, как получить текст из полей. Решаю проблему дублированием пользовательского ввода в собственный буфер. Возможно ли вытащить текст более элегантно?

★★★★

Вот чего нашел здесь: http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/forms.html#FILEDVALIDATE

The forms library allocates one buffer per field to store the data user enters. Using the last parameter to new_field() we can specify it to allocate some additional buffers. These can be used for any purpose you like.

Но как этим рулить?

fero ★★★★
() автор топика

Ещё один вопрос. Допустим необходимо вводить и выводить кирилические символы (UTF8):

#include <ncursesw/curses.h>
#include <wchar.h>
#include <locale.h>


int main()
 {
    setlocale (LC_ALL,"ru_RU.utf8");
    initscr();
    start_color();
    init_pair(1, COLOR_RED, COLOR_BLACK);
    curs_set(0);
    noecho();
    cbreak();
    wchar_t foo;
    get_wch(&foo); 
    printw("%lc",foo);
    refresh();
    pause();
    endwin();
 }
Аналогом для get_wch служит add_wch? Не могу разобраться с ней. printw(«%lc»,foo) выглядит убого, да и отдельно менять атрибуты символа не удобно. Как замутить addch('a'|A_BOLD) для кирилических символов?

fero ★★★★
() автор топика

Вопрос почти снят:

#include <form.h>
#include <locale.h>
#include <string.h>

int main() {
    FIELD *field[4];
    FORM  *my_form;
    char buff[256];
    int ch, i, curr_field;
	
    /* Initialize curses */
    setlocale(LC_ALL,"");
    initscr();
    cbreak();
    noecho();
    keypad(stdscr, TRUE);

    /* Initialize the fields */
    field[0] = new_field(1, 30, 4, 18, 0, 0);
    field[1] = new_field(1, 30, 6, 18, 0, 0);
    field[2] = new_field(1, 20, 8, 18, 0, 0);
    field[3] = NULL;

    /* Set field options */
    set_field_back(field[0], A_UNDERLINE); 	/* Print a line for the option 	*/
    field_opts_off(field[0], O_AUTOSKIP);  	/* Don't go to next field when this */
						/* Field is filled up 		*/
    set_field_back(field[1], A_UNDERLINE); 
    field_opts_off(field[1], O_AUTOSKIP);

    set_field_back(field[2], A_UNDERLINE); 
    field_opts_off(field[2], O_AUTOSKIP);

    /* Create the form and post it */
    my_form = new_form(field);
    post_form(my_form);
    refresh();
	
    mvprintw(4, 10, "Field 0:");
    mvprintw(6, 10, "Field 1:");
    mvprintw(8, 10, "Field 2:");
    refresh();

    /* Loop through to get user requests */
    form_driver(my_form, REQ_VALIDATION);
    while((ch = getch()) != KEY_F(1)) {
	switch (ch) {
	    case KEY_DOWN: /* Go to next field */
		form_driver(my_form, REQ_NEXT_FIELD);
		/* Go to the end of the present buffer */
		/* Leaves nicely at the last character */
		form_driver(my_form, REQ_END_LINE);
		break;
	    case KEY_UP: /* Go to previous field */
		form_driver(my_form, REQ_PREV_FIELD);
		form_driver(my_form, REQ_END_LINE);
		break;
	    case KEY_LEFT:
		form_driver(my_form, REQ_PREV_CHAR);
		break;
	    case KEY_RIGHT:
		form_driver(my_form, REQ_NEXT_CHAR);
		break;
	    case KEY_DC:
		form_driver(my_form, REQ_DEL_CHAR);
		break;
	    default: /* If this is a normal character, it gets Printed	*/	
		form_driver(my_form, ch);
		form_driver(my_form, REQ_VALIDATION);
		curr_field = field_index(current_field(my_form));
		strcpy(buff, field_buffer(field[curr_field],0));
		mvprintw(18,0,"field=%d value=%s=                     ",
		  curr_field, buff);
		mvprintw(19,0,"hex=");
		for (i=0;i<20;i++) printw("%x ",buff[i]&0xff);
		break;
	}
    }

    /* Un post form and free the memory */
    unpost_form(my_form);
    free_form(my_form);
    free_field(field[0]);
    free_field(field[1]); 
    free_field(field[2]); 

    endwin();
    return 0;
}

Взял в треде: http://lists.gnu.org/archive/html/bug-ncurses/2006-12/msg00036.html

fero ★★★★
() автор топика
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.