📄 cflib.c
字号:
fld->data[i - 1] = ' '; pos--; } fld->modified = 1; } /* * Check if CTRL-D or delete. */ else if (ch == _cf_globals.del1 || ch == _cf_globals.del2) { for(i = pos + 1; i < fld->len; i++) { fld->data[i - 1] = fld->data[i]; } fld->data[i - 1] = ' '; fld->modified = 1; } /* * CTRL-K clears the rest of the field. */ else if (ch == _cf_globals.kill) { for(i = pos; i < fld->len; i++) { fld->data[i] = ' '; } fld->modified = 1; } /* * CTRL-U clears the whole field. */ else if (ch == _cf_globals.clear) { pos = 0; for(i = pos; i < fld->len; i++) { fld->data[i] = ' '; } fld->modified = 1; } /* * Control characters ?, no no. */ else if (!isprint(ch)) { message("Unknown key (%d)", ch); } /* * This is actually where data gets written into the field. */ else if (check_p(&ch, fld->data, &pos, fld->len)) { if (fld->flags & FLD_UPPERCASE) ch = toupper(ch); for(i = fld->len - 1; i > pos; i--) { fld->data[i] = fld->data[i - 1]; } fld->data[pos] = ch; if (pos < fld->len - 1) pos++; fld->modified = 1; } draw_field(fld); move(y + fld->picture->y, x + fld->picture->x + pos); } if (fld->type & FLD_RIGHT) { adjust_right(fld->data, fld->len); } return OK;}/******************************************************************************* * * M E S S A G E, M E S S A G E _ N R * ---------------------------------- * * Description: * Give message on bottom screen line. Work the same way as printf. * message_nr works the same as message but gives no refresh. * ******************************************************************************/intmessage(const char *fmt, ...){ va_list arg; va_start(arg, fmt); _message(fmt, arg); va_end(arg); refresh(); return OK;}intmessage_nr(const char *fmt, ...){ va_list arg; va_start(arg, fmt); _message(fmt, arg); va_end(arg); return OK;}static int_message(const char *fmt, va_list arg){ static char msg_str[160]; char tmp_str[160]; int x, y; getyx(stdscr, y, x); move(23, 1); if (fmt != NULL) { vsprintf(tmp_str, fmt, arg); sprintf(msg_str, "%-50.50s", tmp_str); } addstr(msg_str); move(y, x); return OK;}/******************************************************************************* * * C H E C K * * ----------- * * Description: * Various check functions depending of type of field. * If function returnes TRUE, the char ch is put into field. * ******************************************************************************/static intcheck_int(int *ch, char *txt, int *pos, int len){ int i; if (isdigit(*ch)) { return TRUE; } if (isspace(*ch)) { for(i = 0; i < *pos; i++) { if (!isspace(txt[i])) { message("No space in middle of value"); return FALSE; } } return TRUE; } message ("Must be digit"); return FALSE;}static intcheck_alnum(int *ch, char *txt, int *pos, int len){ if (isalnum(*ch) || isspace(*ch)) return TRUE; message("Must be alphanumeric"); return FALSE;}static intcheck_char(int *ch, char *txt, int *pos, int len){ if (isprint(*ch)) return TRUE; message("Must be printable"); return FALSE;}/******************************************************************************* * * A D J U S T _ R I G H T * ----------------------- * * Description: * Adjust a text to the right * ******************************************************************************/static intadjust_right(char *txt, int len){ int last_space = len - 1; while(isspace(txt[last_space]) && last_space >= 0) last_space--; last_space++; memcpy(txt + len - last_space, txt, last_space); memset(txt, ' ', len - last_space); return OK;}/******************************************************************************* * * G E N E R A T E _ E V E N T * --------------------------- * * Description: * Calls raw_event to make the event execute. After that it checks if * field has been moved in case move_to_field is called. * * Input: * ev_type - Event type, ex. EVENT_KEY, EVENT_ENTRY. * ev_spec - Specified for type. * ev_code - Code of event (not for all types), ex KEY_UP. * * Return: * Boolean, TRUE if event code was found and called. Events are not * called for 'event forget' statements (function = NULL). * ******************************************************************************/static intgenerate_event(int ev_type, int ev_spec, int ev_code){ if (raw_event(ev_type, ev_spec, ev_code) == FALSE) { return FALSE; } if (_cf_globals.move_field != NULL) { move_to_field(_cf_globals.move_field); } return TRUE;}/******************************************************************************* * * R A W _ E V E N T * ----------------- * * Description: * Generate a event of type and with code, and calls first function * for field or picture that should respond to the event. * * Input: * ev_type - Event type, ex. EVENT_KEY, EVENT_ENTRY. * ev_spec - Event specifier. * ev_code - Code of event (not for all types), ex KEY_UP. * * Return: * Boolean, TRUE if event code was found and called. Events are not * called for 'event forget' statements (function = NULL). * ******************************************************************************/intraw_event(int ev_type, int ev_spec, int ev_code){ int i, m; /* * Check if field has handler for event, exept for DRAW * events that is only sent to picture and module event handlers. */ if (ev_type != EVENT_DRAW) { for(i = 0; _cf_globals.field && i < _cf_globals.field->n_events; i++) { if (_cf_globals.field->event[i].type == ev_type && _cf_globals.field->event[i].code == ev_spec) { if (_cf_globals.field->event[i].func == NULL) { return FALSE; } (*_cf_globals.field->event[i].func)(ev_code); return TRUE; } } } /* * The field did not have a handler for the event, lets check if * the picture has. */ for(i = 0; _cf_globals.picture && i < _cf_globals.picture->n_events; i++) { if (_cf_globals.picture->event[i].type == ev_type && _cf_globals.picture->event[i].code == ev_spec) { if (_cf_globals.picture->event[i].func == NULL) { return FALSE; } (*_cf_globals.picture->event[i].func)(ev_code); return TRUE; } } /* * The picture did not have a handler for the event, lets check if * the module has. */ for(i = 0; i < _cf_globals.picture->module->n_events; i++) { if (_cf_globals.picture->module->event[i].type == ev_type && _cf_globals.picture->module->event[i].code == ev_spec) { if (_cf_globals.picture->module->event[i].func == NULL) { return FALSE; } (*_cf_globals.picture->module->event[i].func)(ev_code); return TRUE; } } /* * This module did not have a handler for the event, lets check if * there is a global event handler. */ for(m = 0; _cf_modules[m] != 0; m++) { for(i = 0; i < _cf_modules[m]->n_events; i++) { if (_cf_modules[m]->event[i].global != 0 && _cf_modules[m]->event[i].type == ev_type && _cf_modules[m]->event[i].code == ev_spec) { if (_cf_modules[m]->event[i].func == NULL) { return FALSE; } (*_cf_modules[m]->event[i].func)(ev_code); return TRUE; } } } /* * No, no event handlers existed for this type of event. */ return FALSE;}/******************************************************************************* * * F L D _ M O V E * --------------- * * Description: * Move to field * * Input: * fld - Field to move to. * * Return: * field moved to (or current if forbidden field). * ******************************************************************************/struct field *fld_move(struct field *fld){ if (fld == NULL) fld = _cf_globals.field; if (fld->flags & FLD_FORBIDDEN) return _cf_globals.field; _cf_globals.move_field = fld; return _cf_globals.move_field;}/******************************************************************************* * * F L D _ G E T * ------------- * * Description: * Return text in field. * ******************************************************************************/const char *fld_get(const struct field *fld){ if (fld == NULL) fld = _cf_globals.field; if (fld) return fld->data; return "";}/******************************************************************************* * * F L D _ G E T _ T R I M M E D * ----------------------------- * * Description: * Return text in field but with trimmed blanks at beginning and end. * ******************************************************************************/char *fld_get_trimmed(const struct field *fld){ static char txt[1024]; char *p; if (fld == NULL) fld = _cf_globals.field; if (fld && fld->data) { for(p = fld->data; isspace(*p); p++) ; strcpy(txt, p); for (p = txt + strlen(txt) - 1; isspace(*p) && p >= txt; p--) ; *(p + 1) = 0; } else { txt[0] = 0; } return txt;}/******************************************************************************* * * F L D _ S E T * ------------- * * Description: * Set value for field. * ******************************************************************************/intfld_set(struct field *fld, const char *data){ int i; if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return FAIL; memset(fld->data, ' ', fld->len); i = strlen(data); if (i > fld->len) i = fld->len; strncpy(fld->data, data, i); fld->modified = 0; if (fld->picture == _cf_globals.picture) { draw_field(fld); refresh(); } return OK;}/******************************************************************************* * * F L D _ N S E T * --------------- * * Description: * Copy at most 'n' chars from input and set field to that. * ******************************************************************************/intfld_nset(struct field *fld, const char *data, int n){ char tmpstr[200]; strncpy(tmpstr, data, n); tmpstr[n] = 0; return fld_set(fld, tmpstr);}/******************************************************************************* * * F L D _ L E N * ------------- * * Description: * Return length of field. * ******************************************************************************/intfld_len(const struct field *fld){ if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return 0; return fld->len;}/******************************************************************************* * * F L D _ N A M E * --------------- * * Description: * Return name of field. * ******************************************************************************/const char *fld_name(const struct field *fld){ if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return 0; return fld->name;}/******************************************************************************* * * F L D _ I S M O D I F I E D * --------------------------- * * Description: * Return TRUE if field has been modified since last fld_set. * ******************************************************************************/intfld_ismodified(const struct field *fld){ if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return 0; return fld->modified;}/******************************************************************************* * * F L D _ S A T T R * ----------------- * * Description: * Set attributes for field. * * Arguments: * attr - Bitmask of attributes to add to field. * * Return none * ******************************************************************************/voidfld_sattr(struct field *fld, unsigned long attr){ if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return; fld->flags |= attr; if (fld->picture == _cf_globals.picture) { draw_field(fld); refresh(); }}/******************************************************************************* * * F L D _ C A T T R * ----------------- * * Description: * Clear attributes for field. * * Arguments: * attr - Bitmask of attributes to clear from field. * * Return none * ******************************************************************************/voidfld_cattr(struct field *fld, unsigned long attr){ if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return; fld->flags &= ~attr; if (fld->picture == _cf_globals.picture) { draw_field(fld); refresh(); }}/******************************************************************************* * * F L D _ T O U C H * ----------------- * * Description: * Make field not modified. * ******************************************************************************/voidfld_touch(struct field *fld){ if (fld == NULL) fld = _cf_globals.field; if (fld == NULL) return; fld->modified = 0;}/******************************************************************************* * * F I E L D * --------- * * Description: * Return pointer to field with name. Name works as printf. * ******************************************************************************/struct field *field(const char *fmt, ...){ static char field_name[100]; struct picture *pic; va_list arg; char *p; int i; va_start(arg, fmt);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -