#include #include #include #include "internal.h" #include "dialog.h" #include "dialog.m" #include "../diajava/proto.h" class FIELD_CHECK_MULTI_VAL: public FIELD_CHECK_MULTI{ int &numvar; int backup; char cvar; const int *vals; struct { int input; // Current position in window */ int scroll; // Amount of horizontal scrolling so far */ }x; char buf[20]; char retbuf[20]; int optlen; bool hexmode; /*~PROTOBEG~ FIELD_CHECK_MULTI_VAL */ public: FIELD_CHECK_MULTI_VAL (const char *_prompt, int &_var, const int _defvar, const int _vals[], const char *_options[], bool _hexmode); MENU_STATUS dokey (WINDOW *dialog, int key, FIELD_MSG&, bool&); void drawtxt (WINDOW *dialog, int offset, int start_line, int end_line); const char *get_registry_value (void); private: int getcurval (void)const; public: void gui_draw (int nof, SSTRINGS&); MENU_STATUS gui_get (int nof, const char *, const char *); void html_draw (int nof); int html_validate (int nof); int post_validate (void); void reload (const char *dianame, int nof); void restore (void); void save (void); void set_registry_value (const char *value); void setcursor (WINDOW *dialog, int offset); /*~PROTOEND~ FIELD_CHECK_MULTI_VAL */ }; PUBLIC FIELD_CHECK_MULTI_VAL::FIELD_CHECK_MULTI_VAL( const char *_prompt, int &_var, const int _defvar, const int _vals[], // Value corresponding to the titles const char *_options[], bool _hexmode) : FIELD_CHECK_MULTI (_prompt,cvar,_options), numvar(_var) { hexmode = _hexmode; backup = numvar; vals = _vals; reload(NULL,0); box.width += 10; x.input = x.scroll = 0; buf[0] = '\0'; if (val == options.getnb()){ snprintf (buf,sizeof(buf)-1,hexmode ? "%x": "%d",backup); } else if (_defvar != 0){ snprintf (buf,sizeof(buf)-1,hexmode ? "%x": "%d",_defvar); } optlen = 0; for (int i=0; _options[i] != NULL; i++){ const char *str = _options[i]; optlen += strlen(str)+4+1; } } /* Compute the current value of the field. It is using either the value associated with the checkbox or the text input field */ PRIVATE int FIELD_CHECK_MULTI_VAL::getcurval() const { int ret = 0; if (val == options.getnb()){ if (buf[0] != '\0'){ if (hexmode){ sscanf (buf,"%x",&ret); }else{ ret = atoi(buf); } } }else{ ret = vals[val]; } return ret; } PUBLIC void FIELD_CHECK_MULTI_VAL::save( ) { numvar = getcurval(); } PUBLIC void FIELD_CHECK_MULTI_VAL::restore( ) { numvar = backup; FIELD_CHECK_MULTI::restore(); } PUBLIC void FIELD_CHECK_MULTI_VAL::reload(const char *dianame, int nof) { int nbopt = options.getnb(); for (int i=0; i= '0'; i++){ waddch (dialog,buf[i]); } } } PUBLIC void FIELD_CHECK_MULTI_VAL::setcursor (WINDOW *dialog, int offset) { if (val == options.getnb()){ wmove (dialog,box.y,box.x+optlen+x.input-x.scroll); }else{ FIELD_CHECK_MULTI::setcursor(dialog,offset); } } PUBLIC MENU_STATUS FIELD_CHECK_MULTI_VAL::dokey ( WINDOW *dialog, int key, FIELD_MSG &, bool &) { int nbopt = options.getnb(); if (val == nbopt){ // Now we are editing the text field int newval = -1; switch (key){ case KEY_HOME: newval = 0; break; case KEY_LEFT: if (x.input == 0 && x.scroll == 0){ newval = nbopt-1; } break; } if (newval != -1){ val = newval; drawtxt (dialog,0,0,0); }else if (field_editline (dialog,false ,hexmode ? FIELDEDIT_HEXNUM : FIELDEDIT_DIGIT ,key,10,box.y,box.x+optlen ,10,x.input,x.scroll,buf)){ drawtxt (dialog,0,0,0); } }else{ char lastopt = (char)(nbopt-1); char newval = val; switch (key){ case ' ': case KEY_RIGHT: newval = val + 1; break; case KEY_HOME: newval = 0; break; case 5: /* ^E like Emacs */ case KEY_END: newval = lastopt; break; case KEY_LEFT: if (newval == 0){ newval = lastopt; }else{ newval--; } break; } if (newval != val){ val = newval; drawtxt (dialog,0,0,0); } } return MENU_NULL; } PUBLIC void FIELD_CHECK_MULTI_VAL::html_draw(int nof) { char key[100]; format_htmlkey (key,nof); html_defvarcur(key,backup); FIELD_CHECK_MULTI::html_draw (nof); // We generate one more radio as compared with FIELD_CHECK_MULTI // The supplementary radio enable the text field int n = options.getnb(); html_defvar ("radio",key,n,val == n ? "checked" : ""); strcat (key,"-s"); html_defvar ("text",key,buf,"size=10"); } PUBLIC int FIELD_CHECK_MULTI_VAL::html_validate(int nof) { int ret = -1; char key[100]; format_htmlkey (key,nof); int oldval = atoi(html_getoldval(key)); if (backup == oldval){ ret = 0; val = atoi(html_getval(key)); strcat (key,"-s"); strcpy_cut (buf,html_getval(key),10); } return ret; } PUBLIC void FIELD_CHECK_MULTI_VAL::gui_draw(int nof, SSTRINGS &) { guisendprompt(); int n = options.getnb(); diagui_sendcmd (P_Form,"F%d $vexpand=0\n",nof); for (int i=0; iget(); char tmp[1000]; diagui_sendcmd (P_Radio,"R%d %d %d %s\n",nof,i,i==val ,diagui_quote(str,tmp)); } diagui_sendcmd (P_Radio,"R%d %d %d %s\n",nof,n,n==val,""); char tmp[1000]; diagui_sendcmd (P_String,"S%d %d %s\n",nof,10,diagui_quote(buf,tmp)); diagui_sendcmd (P_End,"\n"); } PUBLIC MENU_STATUS FIELD_CHECK_MULTI_VAL::gui_get(int nof, const char *, const char *) { val = atoi(diagui_getval('R',nof)); strcpy_cut (buf,diagui_getval('S',nof),10); return MENU_NULL; } PUBLIC int FIELD_CHECK_MULTI_VAL::post_validate() { int ret = 0; if (val == options.getnb()){ const char *p = buf; for (;*p != 0; p++){ if (hexmode){ if (!isxdigit(*p)){ xconf_error(MSG_U(E_INVDATA,"Invalid data: '%s'"), buf); ret = -1; break; } }else if (!isdigit(*p) && *p != '-') { xconf_error(MSG_R(E_INVDATA), buf); ret = -1; break; } } } return ret; } PUBLIC FIELD_CHECK_MULTI *DIALOG::newf_chkm_num( const char *prompt, int &var, const int vals[], // Value corresponding to the options const char *options[]) { FIELD_CHECK_MULTI *s = new FIELD_CHECK_MULTI_VAL(prompt,var,0,vals ,options,false); add (s); return s; } PUBLIC FIELD_CHECK_MULTI *DIALOG::newf_chkm_hexnum( const char *prompt, int &var, const int vals[], // Value corresponding to the options const char *options[]) { FIELD_CHECK_MULTI *s = new FIELD_CHECK_MULTI_VAL(prompt,var,0,vals ,options,true); add (s); return s; } PUBLIC FIELD_CHECK_MULTI *DIALOG::newf_chkm_num( const char *prompt, int &var, const int defvar, const int vals[], // Value corresponding to the options const char *options[]) { FIELD_CHECK_MULTI *s = new FIELD_CHECK_MULTI_VAL(prompt,var,defvar,vals ,options,false); add (s); return s; } PUBLIC FIELD_CHECK_MULTI *DIALOG::newf_chkm_hexnum( const char *prompt, int &var, const int defvar, const int vals[], // Value corresponding to the options const char *options[]) { FIELD_CHECK_MULTI *s = new FIELD_CHECK_MULTI_VAL(prompt,var,defvar,vals ,options,true); add (s); return s; }