#include #include #include #include #include "diadef.h" #include "dialog.h" #include "dialog.m" #include "../diajava/proto.h" PUBLIC FIELD::FIELD(const char *_prompt) { readonly = false; if (_prompt == NULL) _prompt = "\n"; // See diagui_send_label prompt = strdup(_prompt); box.width = 30; // Default visible width maybeempty = true; may_select = false; is_head = false; vsize = 1; donotcheckold = false; msg = NULL; original_prompt = _prompt; //We want the pointer regkey = NULL; } /* Record the extra GUI parameters for field. See ../doc/guiapi.sgml to learn which parameter are available. In general, you have dc=drawing_context */ PUBLIC void FIELD::set_guiparms(const char *_parms) { guiparms.setfrom (_parms); } PUBLIC void FIELD::set_guipath(const char *_path) { int path_len = (_path != NULL)?strlen(_path):0; int sufix_len = guipath_sufix.getlen(); int buf_size = path_len + sufix_len + 2; char *buf = (char*) malloc(buf_size); if (path_len > 0 && sufix_len > 0){ sprintf(buf,"%s.%s",_path,guipath_sufix.get()); }else if (path_len > 0){ strcpy(buf,_path); }else if (sufix_len > 0){ strcpy(buf,guipath_sufix.get()); }else{ *buf = 0; } guipath.setfrom(buf); } PUBLIC VIRTUAL FIELD::~FIELD() { free (prompt); } PUBLIC void FIELD::set_helpdia(PRIVATE_MESSAGE &_msg) { msg = &_msg; } /* Draw a component of a popup menu in GUI mode By default, does nothing. */ PUBLIC VIRTUAL void FIELD::popup_draw( int, // ID int &) // level { } /* Return the notepad level. 0 for no notepad. Normal edit field return 0. FIELD_TITLE return their level. This allows DIALOG::editgui() to tell where are the FIELD_TITLE field and create a special form at the beginning of the dialog. */ PUBLIC VIRTUAL int FIELD::getnotepadlevel() const { return 0; } /* Indicate that this field may not be empty. This does not apply to all field. For checkbox, it is meaningless. */ PUBLIC VIRTUAL void FIELD::set_noempty() { maybeempty = false; } /* Indicate that this field may be selected or not. */ PUBLIC VIRTUAL void FIELD::set_selectable(bool _may_select) { may_select = _may_select; } /* Return true if this field is selectable */ PUBLIC VIRTUAL bool FIELD::is_selectable() { return may_select; } /* Return true if this field is a passthrough */ PUBLIC VIRTUAL bool FIELD::is_passthrough() { return false; } /* This function is called when the user quits the dialog (with accept) each field may return -1 if something is not appropriate. The field is responsible to signal the error and it will become the current field immediatly. */ PUBLIC VIRTUAL int FIELD::post_validate() { return 0; } /* Is the field editable or is it write protect. Title field do use this feature. */ PUBLIC bool FIELD::is_readonly() { return readonly; } /* Set the readonly status of a field. */ PUBLIC void FIELD::set_readonly() { readonly = true; } /* Process conditionnally a message and conditionnally redraw yourself if needed. See radio.c for the intended application. */ PROTECTED VIRTUAL void FIELD::processmsg(WINDOW *, FIELD_MSG &, int) { } /* Get the width of the columns of a field. This is generally used by menu item and title field. */ PROTECTED VIRTUAL int FIELD::getwidths ( FIELD_COL_WIDTHS &, // Will contain the widths bool &) // Will contain true if this field is starting // a new disposition set // It already contain 0 so no need to touch it { return 0; } /* Set the width of the first column of a field. This is only meaningful for a Menu item. */ PROTECTED VIRTUAL void FIELD::setwidths (FIELD_COL_WIDTHS &) { } /* Return the second string of a menu item */ PUBLIC VIRTUAL const char *FIELD::getmenustr() const { return NULL; } PUBLIC VIRTUAL const char *FIELD::getmenuicon() const { return NULL; } /* Build a key that uniquely identify this field in the dialog */ PUBLIC VIRTUAL void FIELD::format_htmlkey(char *key, int nof) { html_formatkey (key,"%s-%d",prompt,nof); } PRIVATE void FIELD_STRING_BASE::init (int maxsiz) { x.input = x.scroll = 0; password_mode = 0; size = maxsiz; buf = (char*)malloc_err (size+1); maybeempty = 1; fwidth = 30; } PROTECTED FIELD_STRING_BASE::FIELD_STRING_BASE( const char *_prompt, const char *_str, int maxsiz) : FIELD (_prompt) { init(maxsiz); strncpy (buf,_str,maxsiz); buf[maxsiz] = '\0'; backup = _str; } PROTECTED FIELD_STRING_BASE::FIELD_STRING_BASE( const char *_prompt, int maxsiz) : FIELD (_prompt) { init(maxsiz); buf[0] = '\0'; } PUBLIC FIELD_STRING_BASE::~FIELD_STRING_BASE() { free (buf); } /* Return true if the field is empty */ PUBLIC bool FIELD_STRING_BASE::is_empty() { bool ret = true; char *pt = buf; while (*pt != '\0'){ if (*pt != ' '){ ret = false; break; } pt++; } return ret; } PUBLIC int FIELD_STRING_BASE::post_validate() { int ret = 0; if (!maybeempty && is_empty()){ if (prompt[0] != '\0'){ xconf_error (MSG_U(E_NOEMPTYNAME ,"The field `%s`\nmay not be empty") ,prompt); }else{ xconf_error (MSG_U(E_NOEMPTY,"This field may not be empty")); } ret = -1; } return ret; } PUBLIC FIELD_STRING::FIELD_STRING( const char *_prompt, char *_str, int _maxsiz, bool _mayreload) : FIELD_STRING_BASE (_prompt, _str, _maxsiz) { mayreload = _mayreload; str = _str; } PUBLIC FIELD_STRING::~FIELD_STRING() { } PUBLIC FIELD_PASSWORD::FIELD_PASSWORD( const char *_prompt, SSTRING &_str) : FIELD_SSTRING (_prompt,_str,30) { buf[0] = '\0'; password_mode = 1; } PUBLIC void FIELD_STRING::save() { if (mayreload){ strcpy (str,buf); strip_end (str); } } /* Restore the original value of the field from the backup */ PUBLIC void FIELD_STRING::restore() { if (mayreload) backup.copy (str); } const char *field_formatpath ( char tmp[1000], const char *diapath, const char *fldpath) { if (diapath[0] == '\0' && fldpath[0] == '\0'){ strcpy (tmp,"\"\""); }else if (diapath[0] != '\0' && fldpath[0] != '\0'){ snprintf (tmp,999,"%s.%s",diapath,fldpath); }else{ snprintf (tmp,999,"%s%s",diapath,fldpath); } return tmp; } PROTECTED const char *FIELD::formatpath ( char tmp[1000], const char *diapath) { return field_formatpath (tmp,diapath,guipath.get()); } /* Send the value to the front end if needed */ PROTECTED void FIELD::sendval( const char *dianame, int nof, char type, const char *val) { if (dianame != NULL){ char tmp[1000],tmp1[1000]; diagui_sendcmd (P_Setval,"%s %c%d %s\n",formatpath(tmp1,dianame) ,type,nof ,diagui_quote(val,tmp)); } } PROTECTED void FIELD::sendval( const char *dianame, int nof, char type, int val) { if (dianame != NULL){ char tmp1[1000]; diagui_sendcmd (P_Setval,"%s %c%d %d\n",formatpath(tmp1,dianame) ,type,nof,val); } } /* Reload the input buffer from the client variable value. */ PUBLIC void FIELD_STRING::reload(const char *dianame, int nof) { if (mayreload){ if (strcmp(buf,str)!=0){ strcpy (buf,str); sendval (dianame,nof,getidprefix(),buf); } } } /* Draw only the input part of a field */ PUBLIC void FIELD_STRING_BASE::drawtxt (WINDOW *dialog, int, int, int) { int blank_start; wattrset(dialog, inputbox_attr); wmove(dialog, box.y,box.x); if (password_mode){ blank_start = 0; }else{ // Make sure the string is not too long char *instr = buf + x.scroll; char *last_visible = instr + box.width; char tmp = *last_visible; *last_visible = '\0'; waddstr (dialog,instr); blank_start = strlen(instr); *last_visible = tmp; } const int box_width = box.width; for (int i=blank_start; i