gint viper_form_driver(FORM *form,gint request,guint32 flags, chtype active,chtype normal,gshort cursor_color) { WINDOW *window; chtype eraser; chtype temp_ch; gint x,y; gint retval; gshort fg,bg; if(form==NULL) return ERR; if(form_sub(form)!=form_win(form)) window=form_sub(form); else window=form_win(form); getyx(window,y,x); eraser=field_back(current_field(form)); mvwchgat(window,y,x,1,(eraser & A_ATTRIBUTES), PAIR_NUMBER(eraser & A_COLOR),NULL); retval=form_driver(form,request); if(flags & FORM_COLORIZE) viper_form_colorize(form,active,normal,active,normal); if(flags & FORM_CURSOR_NONE) return retval; temp_ch=termattrs(); if((flags & FORM_CURSOR_ULINE) && !(temp_ch & A_UNDERLINE)) return ERR; getyx(window,y,x); temp_ch=field_fore(current_field(form)); if(flags & FORM_CURSOR_ULINE) mvwchgat(window,y,x,1,(temp_ch & A_ATTRIBUTES) | A_UNDERLINE, PAIR_NUMBER(temp_ch & A_COLOR),NULL); else { pair_content(PAIR_NUMBER(temp_ch & A_COLOR),&fg,&bg); if(cursor_color!=-1) { bg=cursor_color; mvwchgat(window,y,x,1,A_NORMAL,viper_color_pair(fg,bg),NULL); } else mvwchgat(window,y,x,1,A_REVERSE,viper_color_pair(fg,bg),NULL); } return E_OK; }
void viper_form_destroy(FORM *form,gboolean free_windows) { WINDOW *parent; WINDOW *window; WINDOW *subwin; FIELD **fields; gint count; unpost_form(form); window=form_win(form); subwin=form_sub(form); parent=window->_parent; count=field_count(form); fields=form_fields(form); free_form(form); while(count>0) { free_field(fields[count-1]); count--; } g_free(fields); if(free_windows==FALSE) return; /* delete subwin if it is completely independent. */ if(subwin!=parent && subwin!=window && subwin!=NULL) delwin(subwin); /* delete window if it is not the parent window. */ if(parent!=NULL && window!=parent && window!=NULL) delwin(window); return; }
static void erase_form(FORM * f) { WINDOW *w = form_win(f); WINDOW *s = form_sub(f); unpost_form(f); werase(w); wrefresh(w); delwin(s); delwin(w); }
void render_form(FORM *form) { int y, x, line; WINDOW *w = form_win(form); WINDOW *inner_w = form_sub(form); int numlines = getmaxy(w)-2; getyx(inner_w, y, x); line = y - (y % numlines); WINDOW *der = derwin(w, getmaxy(w)-2, getmaxx(w)-2, 1, 1); wclear(der); wrefresh(der); delwin(der); copywin(inner_w, w, line, 0, 1, 1, min(numlines, getmaxy(inner_w)-line), 68, 0); wmove(w, y + 1 - line, x + 1); wrefresh(w); }
int edit_field(FORM * form, int *result) { int ch = wgetch(form_win(form)); int status; FIELD *before; unsigned n; char lengths[80]; int length; char *buffer; int before_row; int before_col; int before_off = offset_in_field(form); form_getyx(form, before_row, before_col); before = current_field(form); set_field_back(before, A_NORMAL); if (ch <= KEY_MAX) { set_field_back(before, A_REVERSE); } else if (ch <= MAX_FORM_COMMAND) { inactive_field(before); } *result = ch; for (n = 0; n < SIZEOF(commands); ++n) { if (commands[n].code == ch) { *result = commands[n].result; break; } } status = form_driver(form, *result); if (status == E_OK) { bool modified = TRUE; length = 0; if ((buffer = field_buffer(before, 1)) != 0) length = atoi(buffer); if (length < before_off) length = before_off; switch (*result) { case REQ_CLR_EOF: length = before_off; break; case REQ_CLR_EOL: if (before_row + 1 == before->rows) length = before_off; break; case REQ_CLR_FIELD: length = 0; break; case REQ_DEL_CHAR: if (length > before_off) --length; break; case REQ_DEL_PREV: if (length > 0) { if (before_col > 0) { --length; } else if (before_row > 0) { length -= before->cols + before_col; } } break; case REQ_NEW_LINE: length += before->cols; break; #if 0 /* FIXME: finish these */ case REQ_DEL_LINE: /* delete line */ case REQ_DEL_WORD: /* delete word at cursor */ case REQ_INS_CHAR: /* insert blank char at cursor */ case REQ_INS_LINE: /* insert blank line at cursor */ case REQ_INS_MODE: /* begin insert mode */ case REQ_OVL_MODE: /* begin overlay mode */ #endif /* ignore all of the motion commands */ case REQ_SCR_BCHAR: /* FALLTHRU */ case REQ_SCR_BHPAGE: /* FALLTHRU */ case REQ_SCR_BLINE: /* FALLTHRU */ case REQ_SCR_BPAGE: /* FALLTHRU */ case REQ_SCR_FCHAR: /* FALLTHRU */ case REQ_SCR_FHPAGE: /* FALLTHRU */ case REQ_SCR_FLINE: /* FALLTHRU */ case REQ_SCR_FPAGE: /* FALLTHRU */ case REQ_SCR_HBHALF: /* FALLTHRU */ case REQ_SCR_HBLINE: /* FALLTHRU */ case REQ_SCR_HFHALF: /* FALLTHRU */ case REQ_SCR_HFLINE: /* FALLTHRU */ case REQ_BEG_FIELD: /* FALLTHRU */ case REQ_BEG_LINE: /* FALLTHRU */ case REQ_DOWN_CHAR: /* FALLTHRU */ case REQ_DOWN_FIELD: /* FALLTHRU */ case REQ_END_FIELD: /* FALLTHRU */ case REQ_END_LINE: /* FALLTHRU */ case REQ_FIRST_FIELD: /* FALLTHRU */ case REQ_FIRST_PAGE: /* FALLTHRU */ case REQ_LAST_FIELD: /* FALLTHRU */ case REQ_LAST_PAGE: /* FALLTHRU */ case REQ_LEFT_CHAR: /* FALLTHRU */ case REQ_LEFT_FIELD: /* FALLTHRU */ case REQ_NEXT_CHAR: /* FALLTHRU */ case REQ_NEXT_CHOICE: /* FALLTHRU */ case REQ_NEXT_FIELD: /* FALLTHRU */ case REQ_NEXT_LINE: /* FALLTHRU */ case REQ_NEXT_PAGE: /* FALLTHRU */ case REQ_NEXT_WORD: /* FALLTHRU */ case REQ_PREV_CHAR: /* FALLTHRU */ case REQ_PREV_CHOICE: /* FALLTHRU */ case REQ_PREV_FIELD: /* FALLTHRU */ case REQ_PREV_LINE: /* FALLTHRU */ case REQ_PREV_PAGE: /* FALLTHRU */ case REQ_PREV_WORD: /* FALLTHRU */ case REQ_RIGHT_CHAR: /* FALLTHRU */ case REQ_RIGHT_FIELD: /* FALLTHRU */ case REQ_SFIRST_FIELD: /* FALLTHRU */ case REQ_SLAST_FIELD: /* FALLTHRU */ case REQ_SNEXT_FIELD: /* FALLTHRU */ case REQ_SPREV_FIELD: /* FALLTHRU */ case REQ_UP_CHAR: /* FALLTHRU */ case REQ_UP_FIELD: /* FALLTHRU */ case REQ_VALIDATION: /* FALLTHRU */ modified = FALSE; break; default: modified = FALSE; if (ch >= MIN_FORM_COMMAND) { beep(); } else if (isprint(ch)) { modified = TRUE; } break; } /* * If we do not force a re-validation, then field_buffer 0 will * be lagging by one character. */ if (modified && form_driver(form, REQ_VALIDATION) == E_OK && *result < MIN_FORM_COMMAND) ++length; sprintf(lengths, "%d", length); set_field_buffer(before, 1, lengths); } if (current_field(form) != before) inactive_field(before); return status; }
static void demo_forms(void) { WINDOW *w; FORM *form; FIELD *f[100]; /* FIXME memset to zero */ int finished = 0, c; unsigned n = 0; int pg; WINDOW *also; #ifdef NCURSES_MOUSE_VERSION mousemask(ALL_MOUSE_EVENTS, (mmask_t *) 0); #endif help_edit_field(); MvAddStr(4, 57, "Forms Entry Test"); show_insert_mode(TRUE); refresh(); /* describe the form */ memset(f, 0, sizeof(f)); for (pg = 0; pg < 4; ++pg) { char label[80]; sprintf(label, "Sample Form Page %d", pg + 1); f[n++] = make_label(0, 15, label); set_new_page(f[n - 1], TRUE); switch (pg) { default: f[n++] = make_label(2, 0, "Last Name"); f[n++] = make_field(3, 0, 1, 18); set_field_type(f[n - 1], TYPE_ALPHA, 1); f[n++] = make_label(2, 20, "First Name"); f[n++] = make_field(3, 20, 1, 12); set_field_type(f[n - 1], TYPE_ALPHA, 1); f[n++] = make_label(2, 34, "Middle Name"); f[n++] = make_field(3, 34, 1, 12); set_field_type(f[n - 1], TYPE_ALPHA, 1); break; case 1: f[n++] = make_label(2, 0, "Last Name"); f[n++] = make_field(3, 0, 1, 18); set_field_type(f[n - 1], TYPE_ALPHA, 1); f[n++] = make_label(2, 20, "First Name"); f[n++] = make_field(3, 20, 1, 12); set_field_type(f[n - 1], TYPE_ALPHA, 1); f[n++] = make_label(2, 34, "MI"); f[n++] = make_field(3, 34, 1, 1); set_field_pad(f[n - 1], '?'); set_field_type(f[n - 1], TYPE_ALPHA, 1); break; case 2: f[n++] = make_label(2, 0, "Host Name"); f[n++] = make_field(3, 0, 1, 18); set_field_type(f[n - 1], TYPE_ALNUM, 1); #ifdef NCURSES_VERSION f[n++] = make_label(2, 20, "IP Address"); f[n++] = make_field(3, 20, 1, 12); set_field_type(f[n - 1], TYPE_IPV4, 1); #endif break; case 3: f[n++] = make_label(2, 0, "Four digits"); f[n++] = make_field(3, 0, 1, 18); set_field_type(f[n - 1], TYPE_INTEGER, 4, 0, 0); f[n++] = make_label(2, 20, "Numeric"); f[n++] = make_field(3, 20, 1, 12); set_field_type(f[n - 1], TYPE_NUMERIC, 3, -10000.0, 100000000.0); break; } f[n++] = make_label(5, 0, "Comments"); f[n++] = make_field(6, 0, 4, 46); set_field_buffer(f[n - 1], 0, "HELLO\nWORLD!"); set_field_buffer(f[n - 1], 1, "Hello\nWorld!"); } f[n] = (FIELD *) 0; if ((form = new_form(f)) != 0) { display_form(form); w = form_win(form); also = newwin(getmaxy(stdscr) - getmaxy(w), COLS, getmaxy(w), 0); show_current_field(also, form); while (!finished) { switch (edit_field(form, &c)) { case E_OK: break; case E_UNKNOWN_COMMAND: finished = my_form_driver(form, c); break; default: beep(); break; } show_current_field(also, form); } erase_form(form); free_form(form); } for (c = 0; f[c] != 0; c++) { void *ptr = field_userptr(f[c]); free(ptr); free_field(f[c]); } noraw(); nl(); #ifdef NCURSES_MOUSE_VERSION mousemask(0, (mmask_t *) 0); #endif }