static int get_user_input(char *msg, char *buf, int buflen) { newtComponent form; newtComponent ok; newtComponent cancel; newtComponent inpfield; const char *input; int res = -1; struct newtExitStruct es; newtCenteredWindow(60,7, msg); inpfield = newtEntry(5, 2, "", 50, _NEWT_CAST &input, 0); ok = newtButton(22, 3, "OK"); cancel = newtButton(32, 3, "Cancel"); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, inpfield, ok, cancel, NULL); newtFormRun(form, &es); strncpy(buf, input, buflen - 1); if (es.u.co == ok) res = 0; else res = -1; newtPopWindow(); newtFormDestroy(form); return res; }
static int newtWinOkCancel(const char* title, const char* message, int width, int height, const char* btn_txt_ok, const char* btn_txt_cancel) { int ret = 1; unsigned int btn_width_ok = strlen(btn_txt_ok); unsigned int btn_width_cancel = strlen(btn_txt_cancel); // Maybe make the box wider to fix both buttons inside unsigned int min_width = btn_width_ok + btn_width_cancel + 5; if (width < min_width) width = min_width; unsigned int btn_pos_ok = (width / 3) - (btn_width_ok / 2) - 1; unsigned int btn_pos_cancel = (width * 2 / 3) - (btn_width_cancel / 2) - 1; // Move buttons a bit if they overlap while ((btn_pos_ok + btn_width_ok + 5) > btn_pos_cancel) { // Move the cancel button to the right if there is enough space left if ((btn_pos_cancel + btn_width_cancel + 2) < width) { ++btn_pos_cancel; continue; } // Move the OK button to the left if possible if (btn_pos_ok > 1) { --btn_pos_ok; continue; } // If they still overlap, we cannot fix the situtation // and break. Should actually never get here, because we // adjust the width of the window earlier. break; } newtCenteredWindow(width, height, title); newtComponent form = newtForm(NULL, NULL, 0); newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6, NEWT_FLAG_WRAP); newtTextboxSetText(textbox, message); newtFormAddComponent(form, textbox); newtComponent btn_ok = newtButton(btn_pos_ok, height - 4, btn_txt_ok); newtComponent btn_cancel = newtButton(btn_pos_cancel, height - 4, btn_txt_cancel); newtFormAddComponents(form, btn_ok, btn_cancel, NULL); newtComponent answer = newtRunForm(form); if (answer == btn_ok) { ret = 0; } newtFormDestroy(form); newtPopWindow(); return ret; }
static int manage_calls(char *host) { newtComponent form; newtComponent quit; newtComponent hangup; newtComponent redirect; newtComponent channels; struct newtExitStruct es; char tmp[80]; /* If there's one thing you learn from this code, it is this... Never, ever fly Air France. Their customer service is absolutely the worst. I've never heard the words "That's not my problem" as many times as I have from their staff -- It should, without doubt be their corporate motto if it isn't already. Don't bother giving them business because you're just a pain in their side and they will be sure to let you know the first time you speak to them. If you ever want to make me happy just tell me that you, too, will never fly Air France again either (in spite of their excellent cuisine). */ snprintf(tmp, sizeof(tmp), "Asterisk Manager at %s", host); newtCenteredWindow(74, 20, tmp); form = newtForm(NULL, NULL, 0); newtFormWatchFd(form, session.fd, NEWT_FD_READ); newtFormSetTimer(form, 100); quit = newtButton(62, 16, "Quit"); redirect = newtButton(35, 16, "Redirect"); hangup = newtButton(50, 16, "Hangup"); channels = newtListbox(1,1,14, NEWT_FLAG_SCROLL); newtFormAddComponents(form, channels, redirect, hangup, quit, NULL); newtListboxSetWidth(channels, 72); show_doing("Getting Status", "Retrieving system status..."); try_status(); hide_doing(); for(;;) { newtFormRun(form, &es); if (has_input(&session) || (es.reason == NEWT_EXIT_FDREADY)) { if (input_check(&session, NULL)) { show_message("Disconnected", "Disconnected from remote host"); break; } } else if (es.reason == NEWT_EXIT_COMPONENT) { if (es.u.co == quit) break; if (es.u.co == hangup) { try_hangup(channels); } else if (es.u.co == redirect) { try_redirect(channels); } } rebuild_channels(channels); } newtFormDestroy(form); return 0; }
// ======================================================= int CInterfaceNewt::askLogin(char *szLogin, char *szPasswd, WORD size) { newtComponent editLoginText; newtComponent editPasswdText; newtComponent labelLoginText; newtComponent labelPasswdText; newtComponent form; newtComponent btnOk; newtComponent btnCancel; newtComponent temp; int nOk = 0; while (!nOk) { newtCenteredWindow(65, 20, i18n("Password required")); labelLoginText = newtTextbox(1, 5, 60, 1, 0); newtTextboxSetText(labelLoginText, i18n("Enter your login")); editLoginText = newtEntry(1, 6, "", 55, NULL, NEWT_ENTRY_SCROLL); labelPasswdText = newtTextbox(1, 9, 60, 1, 0); newtTextboxSetText(labelPasswdText, i18n("Enter your password")); editPasswdText = newtEntry(1, 10, "", 55, NULL, NEWT_FLAG_HIDDEN); btnOk = newtButton(15, 14, i18n("Ok")); btnCancel = newtButton(40, 14, i18n("Cancel")); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, labelLoginText, editLoginText, labelPasswdText, editPasswdText, btnOk, btnCancel, NULL); temp = newtRunForm(form); newtPopWindow(); if (temp == btnCancel) { newtFormDestroy(form); RETURN_int(-1); } strncpy(szLogin, newtEntryGetValue(editLoginText), size-1); *(szLogin+size-1) = '\0'; strncpy(szPasswd, newtEntryGetValue(editPasswdText), size-1); *(szPasswd+size-1) = '\0'; if (!(*szLogin) || !(*szPasswd)) msgBoxError(i18n("Text cannot be empty")); else nOk = 1; newtFormDestroy(form); } return 0; }
// ============================================================================ unsigned int CExceptionsGUI::windowError(char *szTitle, char *szText, char *szButton, char *szCurPath) { BEGIN; //char szTemp[] = "/tmp/image"; unsigned int nRes; newtComponent btnOther; newtComponent formMain = newtForm(NULL, NULL, 0); if (szButton) btnOther = newtButton(30, 15, szButton); else btnOther = newtButton(30, 15, "(none)"); newtComponent btnOk = newtButton(10, 15, i18n("Change")); newtComponent btnCancel = newtButton(50, 15, i18n("Cancel")); newtComponent editFilename = newtEntry(5, 10, szCurPath, 45, NULL, 0); newtComponent labelFilename = newtLabel(5, 9, i18n("Enter new filename")); newtComponent labelText = newtTextbox(1, 1, 60, 7, 0); newtTextboxSetText(labelText, szText); newtComponent widgetTemp; newtCenteredWindow(67, 20, szTitle); if (szButton) newtFormAddComponents(formMain, editFilename, btnOk, btnOther, btnCancel, labelFilename, labelText, NULL); else newtFormAddComponents(formMain, editFilename, btnOk, btnCancel, labelFilename, labelText, NULL); widgetTemp = newtRunForm(formMain); if (widgetTemp == btnCancel) nRes = ERR_QUIT; else if (szButton && widgetTemp == btnOther) nRes = ERR_CONT; else { strncpy(szNewString, newtEntryGetValue(editFilename), 1023); *(szNewString+1023) = '\0'; while (szNewString[strlen(szNewString)-1] == '/') szNewString[strlen(szNewString)-1] = '\0'; nRes = ERR_RETRY; } newtFormDestroy(formMain); newtPopWindow(); RETURN_WORD(nRes); }
static int newtLicenseBox(const char* title, const char* text, int width, int height) { int ret = 1; newtCenteredWindow(width, height, title); newtComponent form = newtForm(NULL, NULL, 0); newtComponent textbox = newtTextbox(1, 1, width - 2, height - 7, NEWT_FLAG_WRAP|NEWT_FLAG_SCROLL); newtTextboxSetText(textbox, text); newtFormAddComponent(form, textbox); char choice; newtComponent checkbox = newtCheckbox(3, height - 3, _("I accept this license"), ' ', " *", &choice); newtComponent btn = newtButton(width - 15, height - 4, _("OK")); newtFormAddComponents(form, checkbox, btn, NULL); newtComponent answer = newtRunForm(form); if (answer == btn && choice == '*') ret = 0; newtFormDestroy(form); newtPopWindow(); return ret; }
/** * Ask the user how much compression they would like to use. * The choices are "None" (0), "Minimum" (1), "Average" (4), and "Maximum" (9). * @return The compression level (0-9) chosen, or -1 for "Exit". */ int which_compression_level() { /*@ char ************************************************************ */ int output = none; /*@ newt ************************************************************ */ newtComponent b1; newtComponent b2; newtComponent b3; newtComponent b4; newtComponent b5; newtComponent b_res; newtComponent myForm; newtDrawRootText(18, 0, WELCOME_STRING); newtPushHelpLine (_(" Please specify the level of compression that you want.")); // newtOpenWindow (23, 3, 34, 13, "How much compression?"); newtCenteredWindow(34, 13, _("How much compression?")); b1 = newtButton(4, 1, _("Maximum")); b2 = newtButton(18, 1, _("Average")); b3 = newtButton(4, 5, _("Minimum")); b4 = newtButton(18, 5, _(" None ")); b5 = newtButton(4, 9, _(" Exit ")); myForm = newtForm(NULL, NULL, 0); newtFormAddComponents(myForm, b1, b3, b2, b4, b5, NULL); b_res = newtRunForm(myForm); newtFormDestroy(myForm); newtPopWindow(); if (b_res == b1) { output = 9; } else if (b_res == b2) { output = 4; } else if (b_res == b3) { output = 1; } else if (b_res == b4) { output = 0; } else if (b_res == b5) { output = -1; } newtPopHelpLine(); return (output); }
// ======================================================= int CInterfaceNewt::askText(char *szMessage, char *szTitle, char *szDestText, WORD size) { newtComponent editText; newtComponent labelText; newtComponent form; newtComponent btnOk; newtComponent btnCancel; newtComponent temp; *szDestText = 0; while (!*szDestText) { newtCenteredWindow(65, 20, szTitle); labelText = newtTextbox(1, 1, 60, 7, 0/*NEWT_TEXTBOX_SCROLL*/); newtTextboxSetText(labelText, szMessage); editText = newtEntry(1, 9, "", 55, NULL, NEWT_ENTRY_SCROLL); btnOk = newtButton(15, 14, i18n("Ok")); btnCancel = newtButton(40, 14, i18n("Cancel")); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, labelText, editText, btnOk, btnCancel, NULL); temp = newtRunForm(form); newtPopWindow(); if (temp == btnCancel) { newtFormDestroy(form); RETURN_int(-1); } strncpy(szDestText, newtEntryGetValue(editText), size-1); *(szDestText+size-1) = '\0'; if (*szDestText == 0) msgBoxError(i18n("Text cannot be empty")); newtFormDestroy(form); } return 0; }
static int show_message(char *title, char *msg) { newtComponent form; newtComponent label; newtComponent ok; struct newtExitStruct es; newtCenteredWindow(60,7, title); label = newtLabel(4,1,msg); ok = newtButton(27, 3, "OK"); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, label, ok, NULL); newtFormRun(form, &es); newtPopWindow(); newtFormDestroy(form); return 0; }
// ======================================================= void CInterfaceNewt::askDescription(char *szDescription, DWORD dwMaxLen) { newtComponent editDesc; newtComponent labelText; newtComponent form; newtComponent btnOk; memset(szDescription, 0, dwMaxLen); newtCenteredWindow(65, 18, i18n("Partition description")); labelText = newtLabel(1, 1, i18n("You can enter a description of the saved partition:")); editDesc = newtEntry(1, 3, "", 60, NULL, NEWT_ENTRY_SCROLL); btnOk = newtButton(27, 13, i18n("Ok")); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, labelText, editDesc, btnOk, NULL); newtRunForm(form); newtPopWindow(); strncpy(szDescription, newtEntryGetValue(editDesc), dwMaxLen); newtFormDestroy(form); }
static void show_spans(void) { newtComponent form; newtComponent quit; newtComponent label; newtComponent sel; struct newtExitStruct es; quit = newtButton(50,14,"Quit"); sel = newtButton(10,14,"Select"); spans = newtListbox(5, 2, 10, NEWT_FLAG_SCROLL); newtListboxSetWidth(spans, 65); label = newtLabel(5,1,"Alarms Span"); newtCenteredWindow(72,18, "DAHDI Telephony Interfaces"); form = newtForm(NULL, NULL, 0); newtFormSetTimer(form, 200); newtFormAddComponents(form, spans, sel, quit, label, NULL); newtComponentAddCallback(spans, sel_callback, NULL); newtFormAddHotKey(form, NEWT_KEY_F1); newtFormAddHotKey(form, NEWT_KEY_F10); for(;;) { /* Wait for user to select something */ do { add_cards(spans); newtFormRun(form, &es); } while(es.reason == NEWT_EXIT_TIMER); switch(es.reason) { case NEWT_EXIT_COMPONENT: if (es.u.co == quit) { /* Quit if appropriate */ newtFormDestroy(form); return; } else if (es.u.co == sel) { show_span(-1); } break; case NEWT_EXIT_HOTKEY: switch(es.u.key) { case NEWT_KEY_F1: show_span(-1); break; case NEWT_KEY_F10: newtFormDestroy(form); return; } break; default: break; } } }
static void show_span(int span) { newtComponent form; newtComponent back; newtComponent loop; newtComponent label; newtComponent bitbox; newtComponent inuse; newtComponent levels; newtComponent bpvcount; newtComponent alarms; newtComponent syncsrc; newtComponent irqmisses; char s1[] = " 1111111111222222222233"; char s2[] = "1234567890123456789012345678901"; int x; int looped = 0; struct newtExitStruct es; void *ss; char info2[256]; if (span < 0) { /* Display info on a span */ ss = newtListboxGetCurrent(spans); if (ss) { span = (long)(ss); } } snprintf(info2, sizeof(info2), "%-59s F10=Back", s[span].desc); newtOpenWindow(10,2,60,20, s[span].desc); newtPushHelpLine(info2); back = newtButton(48,8,"Back"); loop = newtButton(48,14,"Loop"); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, back, loop, NULL); span_max_chan_pos = s[span].totalchans; for (x=0;x<DAHDI_MAX_CHANNELS;x++) { DAHDI_PARAMS zp; int res; memset(&zp, 0, sizeof(zp)); zp.channo = x; res = ioctl(ctl, DAHDI_GET_PARAMS, &zp); if (!res && zp.spanno == span && zp.chanpos > span_max_chan_pos ) span_max_chan_pos = zp.chanpos; } if (span_max_chan_pos > 32) span_max_chan_pos = 32; s1[span_max_chan_pos] = '\0'; s2[span_max_chan_pos] = '\0'; bitbox = newtTextbox(8,10,span_max_chan_pos,9,0); newtFormAddComponent(form, bitbox); label = newtLabel(8,8,s1); newtFormAddComponent(form, label); label = newtLabel(8,9,s2); newtFormAddComponent(form, label); newtFormAddHotKey(form, NEWT_KEY_F10); newtFormSetTimer(form, 200); label = newtLabel(4,10,"TxA"); newtFormAddComponent(form, label); label = newtLabel(4,11,"TxB"); newtFormAddComponent(form, label); label = newtLabel(4,12,"TxC"); newtFormAddComponent(form, label); label = newtLabel(4,13,"TxD"); newtFormAddComponent(form, label); label = newtLabel(4,15,"RxA"); newtFormAddComponent(form, label); label = newtLabel(4,16,"RxB"); newtFormAddComponent(form, label); label = newtLabel(4,17,"RxC"); newtFormAddComponent(form, label); label = newtLabel(4,18,"RxD"); newtFormAddComponent(form, label); label = newtLabel(4,7,"Total/Conf/Act: "); newtFormAddComponent(form, label); inuse = newtTextbox(24,7,12,1,0); newtFormAddComponent(form, inuse); label = newtLabel(4,6,"Tx/Rx Levels: "); newtFormAddComponent(form, label); levels = newtTextbox(24,6,30,1,0); newtFormAddComponent(form, levels); label = newtLabel(4,5,"Bipolar Viol: "); newtFormAddComponent(form, label); bpvcount = newtTextbox(24,5,30,1,0); newtFormAddComponent(form, bpvcount); label = newtLabel(4,4,"IRQ Misses: "); newtFormAddComponent(form, label); irqmisses = newtTextbox(24,4,30,1,0); newtFormAddComponent(form, irqmisses); label = newtLabel(4,3,"Sync Source: "); newtFormAddComponent(form, label); syncsrc = newtTextbox(24,3,30,1,0); newtFormAddComponent(form, syncsrc); label = newtLabel(4,2,"Current Alarms: "); newtFormAddComponent(form, label); alarms = newtTextbox(24,2,30,1,0); newtFormAddComponent(form, alarms); for(;;) { /* Wait for user to select something */ do { add_cards(NULL); show_bits(span, bitbox, inuse, levels, bpvcount, alarms, syncsrc, irqmisses); newtFormRun(form, &es); } while(es.reason == NEWT_EXIT_TIMER); switch(es.reason) { case NEWT_EXIT_COMPONENT: if (es.u.co == loop) { looped = !looped; do_loop(span, looped); newtFormSetTimer(form, 200); } if (es.u.co == back) { goto out; } break; case NEWT_EXIT_HOTKEY: switch(es.u.key) { #if 0 case NEWT_KEY_F1: show_span(-1); break; #endif case NEWT_KEY_F10: goto out; } break; default: break; } } out: newtFormDestroy(form); newtPopWindow(); newtPopHelpLine(); span_max_chan_pos = 0; }
/* only supports up to 50 buttons and entries -- shucks! */ static int mynewtWinEntries(char * title, char * text, int suggestedWidth, int flexDown, int flexUp, int dataWidth, void (*callback_func)(char ** strings), struct newtWinEntry * items, char * button1, ...) { newtComponent buttons[50], result, form, textw; newtGrid grid, buttonBar, subgrid; int numItems; int rc, i; int numButtons; char * buttonName; newtComponent entries[50]; va_list args; textw = newtTextboxReflowed(-1, -1, text, suggestedWidth, flexDown, flexUp, 0); for (numItems = 0; items[numItems].text; numItems++); buttonName = button1, numButtons = 0; va_start(args, button1); while (buttonName) { buttons[numButtons] = newtButton(-1, -1, buttonName); numButtons++; buttonName = va_arg(args, char *); } va_end(args); buttonBar = newtCreateGrid(numButtons, 1); for (i = 0; i < numButtons; i++) { newtGridSetField(buttonBar, i, 0, NEWT_GRID_COMPONENT, buttons[i], i ? 1 : 0, 0, 0, 0, 0, 0); } if (callback_func) { callback_real_function = callback_func; entries[numItems] = NULL; } else callback_real_function = NULL; subgrid = newtCreateGrid(2, numItems); for (i = 0; i < numItems; i++) { newtComponent entr = newtEntry(-1, -1, items[i].value ? *items[i].value : NULL, dataWidth, (const char**)items[i].value, items[i].flags); newtGridSetField(subgrid, 0, i, NEWT_GRID_COMPONENT, newtLabel(-1, -1, items[i].text), 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0); newtGridSetField(subgrid, 1, i, NEWT_GRID_COMPONENT, entr, 1, 0, 0, 0, 0, 0); if (callback_func) { entries[i] = entr; newtComponentAddCallback(entr, default_callback, entries); } } grid = newtCreateGrid(1, 3); form = newtForm(NULL, 0, 0); newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, textw, 0, 0, 0, 0, NEWT_ANCHOR_LEFT, 0); newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, 0, 1, 0, 0, 0, 0); newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttonBar, 0, 1, 0, 0, 0, NEWT_GRID_FLAG_GROWX); newtGridAddComponentsToForm(grid, form, 1); newtGridWrappedWindow(grid, title); newtGridFree(grid, 1); result = newtRunForm(form); for (rc = 0; rc < numItems; rc++) *items[rc].value = strdup(*items[rc].value); for (rc = 0; result != buttons[rc] && rc < numButtons; rc++); if (rc == numButtons) rc = 0; /* F12 */ else rc++; newtFormDestroy(form); newtPopWindow(); return rc; }
static int login(char *hostname) { newtComponent form; newtComponent cancel; newtComponent login; newtComponent username; newtComponent password; newtComponent label; newtComponent ulabel; newtComponent plabel; const char *user; const char *pass; struct message *m; struct newtExitStruct es; char tmp[55]; struct hostent *hp; int res = -1; session.fd = socket(AF_INET, SOCK_STREAM, 0); if (session.fd < 0) { snprintf(tmp, sizeof(tmp), "socket() failed: %s\n", strerror(errno)); show_message("Socket failed", tmp); return -1; } snprintf(tmp, sizeof(tmp), "Looking up %s\n", hostname); show_doing("Connecting....", tmp); hp = gethostbyname(hostname); if (!hp) { snprintf(tmp, sizeof(tmp), "No such address: %s\n", hostname); show_message("Host lookup failed", tmp); return -1; } hide_doing(); snprintf(tmp, sizeof(tmp), "Connecting to %s", hostname); show_doing("Connecting...", tmp); session.sin.sin_family = AF_INET; session.sin.sin_port = htons(DEFAULT_MANAGER_PORT); memcpy(&session.sin.sin_addr, hp->h_addr, sizeof(session.sin.sin_addr)); if (connect(session.fd,(struct sockaddr*)&session.sin, sizeof(session.sin))) { snprintf(tmp, sizeof(tmp), "%s failed: %s\n", hostname, strerror(errno)); show_message("Connect Failed", tmp); return -1; } hide_doing(); login = newtButton(5, 6, "Login"); cancel = newtButton(25, 6, "Cancel"); newtCenteredWindow(40, 10, "Asterisk Manager Login"); snprintf(tmp, sizeof(tmp), "Host: %s", hostname); label = newtLabel(4,1, tmp); ulabel = newtLabel(4,2,"Username:"******"Password:"******"", 20, _NEWT_CAST &user, 0); password = newtEntry(14, 3, "", 20, _NEWT_CAST &pass, NEWT_FLAG_HIDDEN); form = newtForm(NULL, NULL, 0); newtFormAddComponents(form, username, password, login, cancel, label, ulabel, plabel,NULL); newtFormRun(form, &es); if (es.reason == NEWT_EXIT_COMPONENT) { if (es.u.co == login) { snprintf(tmp, sizeof(tmp), "Logging in '%s'...", user); show_doing("Logging in", tmp); /* Check to see if the remote host supports MD5 Authentication */ manager_action("Challenge", "AuthType: MD5\r\n"); m = wait_for_response(10000); if (m && !strcasecmp(get_header(m, "Response"), "Success")) { char *challenge = get_header(m, "Challenge"); int x; int len = 0; char md5key[256] = ""; struct MD5Context md5; unsigned char digest[16]; MD5Init(&md5); MD5Update(&md5, (unsigned char *)challenge, strlen(challenge)); MD5Update(&md5, (unsigned char *)pass, strlen(pass)); MD5Final(digest, &md5); for (x=0; x<16; x++) len += sprintf(md5key + len, "%2.2x", digest[x]); manager_action("Login", "AuthType: MD5\r\n" "Username: %s\r\n" "Key: %s\r\n", user, md5key); m = wait_for_response(10000); hide_doing(); if (!strcasecmp(get_header(m, "Response"), "Success")) { res = 0; } else { show_message("Login Failed", get_header(m, "Message")); } } else { memset(m, 0, sizeof(m)); manager_action("Login", "Username: %s\r\n" "Secret: %s\r\n", user, pass); m = wait_for_response(10000); hide_doing(); if (m) { if (!strcasecmp(get_header(m, "Response"), "Success")) { res = 0; } else { show_message("Login Failed", get_header(m, "Message")); } } } } } newtFormDestroy(form); return res; }
int main(void) { newtComponent b1, b2, r1, r2, r3, e2, e3, l1, l2, l3, scale; newtComponent lb, t, rsf, answer, timeLabel; newtComponent cs[10]; newtComponent f, chklist, e1; struct callbackInfo cbis[3]; char results[10]; char * enr2, * enr3, * scaleVal; void ** selectedList; int i, numsel; char buf[20]; const char * spinner = "-\\|/\\|/"; const char * spinState; struct newtExitStruct es; newtInit(); newtCls(); newtSetSuspendCallback(suspend, NULL); newtSetHelpCallback(helpCallback); newtDrawRootText(0, 0, "Newt test program"); newtPushHelpLine(NULL); newtDrawRootText(-50, 0, "More root text"); newtOpenWindow(2, 2, 30, 10, "first window"); newtOpenWindow(10, 5, 65, 16, "window 2"); f = newtForm(NULL, "This is some help text", 0); chklist = newtForm(NULL, NULL, 0); b1 = newtButton(3, 1, "Exit"); b2 = newtButton(18, 1, "Update"); r1 = newtRadiobutton(20, 10, "Choice 1", 0, NULL); r2 = newtRadiobutton(20, 11, "Chc 2", 1, r1); r3 = newtRadiobutton(20, 12, "Choice 3", 0, r2); rsf = newtForm(NULL, NULL, 0); newtFormAddComponents(rsf, r1, r2, r3, NULL); newtFormSetBackground(rsf, NEWT_COLORSET_CHECKBOX); for (i = 0; i < 10; i++) { sprintf(buf, "Check %d", i); cs[i] = newtCheckbox(3, 10 + i, buf, ' ', NULL, &results[i]); newtFormAddComponent(chklist, cs[i]); } l1 = newtLabel(3, 6, "Scale:"); l2 = newtLabel(3, 7, "Scrolls:"); l3 = newtLabel(3, 8, "Hidden:"); e1 = newtEntry(12, 6, "", 20, &scaleVal, 0); e2 = newtEntry(12, 7, "Default", 20, &enr2, NEWT_FLAG_SCROLL); /* e3 = newtEntry(12, 8, NULL, 20, &enr3, NEWT_FLAG_HIDDEN); */ e3 = newtEntry(12, 8, NULL, 20, &enr3, NEWT_FLAG_PASSWORD); cbis[0].state = &results[0]; cbis[0].en = e1; newtComponentAddCallback(cs[0], disableCallback, &cbis[0]); scale = newtScale(3, 14, 32, 100); newtFormSetHeight(chklist, 3); newtFormAddComponents(f, b1, b2, l1, l2, l3, e1, e2, e3, chklist, NULL); newtFormAddComponents(f, rsf, scale, NULL); lb = newtListbox(45, 1, 6, NEWT_FLAG_MULTIPLE | NEWT_FLAG_BORDER | NEWT_FLAG_SCROLL | NEWT_FLAG_SHOWCURSOR); newtListboxAppendEntry(lb, "First", (void *) 1); newtListboxAppendEntry(lb, "Second", (void *) 2); newtListboxAppendEntry(lb, "Third", (void *) 3); newtListboxAppendEntry(lb, "Fourth", (void *) 4); newtListboxAppendEntry(lb, "Sixth", (void *) 6); newtListboxAppendEntry(lb, "Seventh", (void *) 7); newtListboxAppendEntry(lb, "Eighth", (void *) 8); newtListboxAppendEntry(lb, "Ninth", (void *) 9); newtListboxAppendEntry(lb, "Tenth", (void *) 10); newtListboxInsertEntry(lb, "Fifth", (void *) 5, (void *) 4); newtListboxInsertEntry(lb, "Eleventh", (void *) 11, (void *) 10); newtListboxDeleteEntry(lb, (void *) 11); spinState = spinner; timeLabel = newtLabel(45, 8, "Spinner: -"); t = newtTextbox(45, 10, 17, 5, NEWT_FLAG_WRAP); newtTextboxSetText(t, "This is some text does it look okay?\nThis should be alone.\nThis shouldn't be printed"); newtFormAddComponents(f, lb, timeLabel, t, NULL); newtRefresh(); newtFormSetTimer(f, 200); do { newtFormRun(f, &es); if (es.reason == NEWT_EXIT_COMPONENT && es.u.co == b2) { newtScaleSet(scale, atoi(scaleVal)); newtRefresh(); answer = NULL; } else if (es.reason == NEWT_EXIT_TIMER) { spinState++; if (!*spinState) spinState = spinner; sprintf(buf, "Spinner: %c", *spinState); newtLabelSetText(timeLabel, buf); } } while (es.reason != NEWT_EXIT_COMPONENT || es.u.co == b2); scaleVal = strdup(scaleVal); enr2 = strdup(enr2); enr3 = strdup(enr3); selectedList = newtListboxGetSelection(lb, &numsel); newtFormDestroy(f); newtPopWindow(); newtPopWindow(); newtFinished(); printf("got string 1: %s\n", scaleVal); printf("got string 2: %s\n", enr2); printf("got string 3: %s\n", enr3); if(selectedList) { printf("\nSelected listbox items:\n"); for(i = 0; i < numsel; i++) puts(selectedList[i]); } return 0; }
int handledhcp(void) { char *results[MAX_BOXES]; char enabledresult; char startenabled; struct newtExitStruct es; newtComponent header; newtComponent labels[MAX_BOXES]; newtComponent ok, cancel; char message[1000]; char *labeltexts[MAX_BOXES] = { _("Start address:"), _("End address:"), _("Primary DNS:"), _("Secondary DNS:"), _("Default lease (mins):"), _("Max lease (mins):"), _("Domain name suffix:") }; char *varnames[MAX_BOXES] = { "START_ADDR_GREEN", "END_ADDR_GREEN", "DNS1_GREEN", "DNS2_GREEN", "DEFAULT_LEASE_TIME_GREEN", "MAX_LEASE_TIME_GREEN", "DOMAIN_NAME_GREEN" }; char defaults[MAX_BOXES][STRING_SIZE]; int result; int c; char temp[STRING_SIZE]; struct keyvalue *mainkv = initkeyvalues(); struct keyvalue *dhcpkv = initkeyvalues(); struct keyvalue *ethernetkv = initkeyvalues(); int error; FILE *file; char greenaddress[STRING_SIZE]; char greennetaddress[STRING_SIZE]; char greennetmask[STRING_SIZE]; memset(defaults, 0, sizeof(char) * STRING_SIZE * MAX_BOXES); if (!(readkeyvalues(dhcpkv, CONFIG_ROOT "/dhcp/settings"))) { freekeyvalues(dhcpkv); freekeyvalues(ethernetkv); errorbox(_("Unable to open settings file")); return 0; } if (!(readkeyvalues(ethernetkv, CONFIG_ROOT "/ethernet/settings"))) { freekeyvalues(dhcpkv); freekeyvalues(ethernetkv); errorbox(_("Unable to open settings file")); return 0; } if (!(readkeyvalues(mainkv, CONFIG_ROOT "/main/settings"))) { freekeyvalues(dhcpkv); freekeyvalues(ethernetkv); freekeyvalues(mainkv); errorbox(_("Unable to open settings file")); return 0; } /* Set default values. */ findkey(ethernetkv, "GREEN_ADDRESS", defaults[PRIMARY_DNS]); findkey(mainkv, "DOMAINNAME", defaults[DOMAIN_NAME_SUFFIX]); strcpy(defaults[DEFAULT_LEASE_TIME], "60"); strcpy(defaults[MAX_LEASE_TIME], "120"); newtCenteredWindow(55, 18, _("DHCP server configuration")); dhcpform = newtForm(NULL, NULL, 0); header = newtTextboxReflowed(1, 1, _("Configure the DHCP server by entering the settings information."), 52, 0, 0, 0); newtFormAddComponent(dhcpform, header); strcpy(temp, ""); findkey(dhcpkv, "ENABLE_GREEN", temp); if (strcmp(temp, "on") == 0) startenabled = '*'; else startenabled = ' '; enabledcheckbox = newtCheckbox(2, TOP + 0, _("Enabled"), startenabled, " *", &enabledresult); newtFormAddComponent(dhcpform, enabledcheckbox); newtComponentAddCallback(enabledcheckbox, dhcpdialogcallbackdhcp, NULL); for (c = 0; c < MAX_BOXES; c++) { labels[c] = newtTextbox(2, TOP + 2 + c, 33, 1, 0); newtTextboxSetText(labels[c], labeltexts[c]); newtFormAddComponent(dhcpform, labels[c]); strcpy(temp, defaults[c]); findkey(dhcpkv, varnames[c], temp); entries[c] = newtEntry(34, TOP + 2 + c, temp, 18, &results[c], 0); newtFormAddComponent(dhcpform, entries[c]); if (startenabled == ' ') newtEntrySetFlags(entries[c], NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); } ok = newtButton(10, c + 7, _("OK")); cancel = newtButton(34, c + 7, _("Cancel")); newtFormAddComponents(dhcpform, ok, cancel, NULL); do { error = 0; newtFormRun(dhcpform, &es); if (es.u.co == ok) { /* OK was pressed; verify the contents of each entry. */ if (enabledresult == '*') { strcpy(message, _("The following fields are invalid:\n\n")); if (inet_addr(results[START_ADDRESS]) == INADDR_NONE) { strcat(message, _("Start address")); strcat(message, "\n"); error = 1; } if (inet_addr(results[END_ADDRESS]) == INADDR_NONE) { strcat(message, _("End address")); strcat(message, "\n"); error = 1; } if (strlen(results[SECONDARY_DNS])) { if (inet_addr(results[PRIMARY_DNS]) == INADDR_NONE) { strcat(message, _("Primary DNS")); strcat(message, "\n"); error = 1; } } if (strlen(results[SECONDARY_DNS])) { if (inet_addr(results[SECONDARY_DNS]) == INADDR_NONE) { strcat(message, _("Secondary DNS")); strcat(message, "\n"); error = 1; } } if (!(atol(results[DEFAULT_LEASE_TIME]))) { strcat(message, _("Default lease time")); strcat(message, "\n"); error = 1; } if (!(atol(results[MAX_LEASE_TIME]))) { strcat(message, _("Max. lease time")); strcat(message, "\n"); error = 1; } } if (error) errorbox(message); else { for (c = 0; c < MAX_BOXES; c++) replacekeyvalue(dhcpkv, varnames[c], results[c]); if (enabledresult == '*') { replacekeyvalue(dhcpkv, "ENABLE_GREEN", "on"); fclose(fopen(CONFIG_ROOT "/dhcp/enable_green", "w")); chown(CONFIG_ROOT "/dhcp/enable_green", 99, 99); mysystem(NULL, "/usr/local/bin/dhcpctrl enable"); } else { replacekeyvalue(dhcpkv, "ENABLE_GREEN", "off"); unlink(CONFIG_ROOT "/dhcp/enable_green"); mysystem(NULL, "/usr/local/bin/dhcpctrl disable"); } replacekeyvalue(dhcpkv, "VALID", "yes"); writekeyvalues(dhcpkv, CONFIG_ROOT "/dhcp/settings"); findkey(ethernetkv, "GREEN_ADDRESS", greenaddress); findkey(ethernetkv, "GREEN_NETADDRESS", greennetaddress); findkey(ethernetkv, "GREEN_NETMASK", greennetmask); file = fopen(CONFIG_ROOT "/dhcp/dhcpd.conf", "w"); fprintf(file, "ddns-update-style none;\n"); fprintf(file, "authoritative;\n"); fprintf(file, "subnet %s netmask %s\n", greennetaddress, greennetmask); fprintf(file, "{\n"); fprintf(file, "\toption subnet-mask %s;\n", greennetmask); fprintf(file, "\toption domain-name \"%s\";\n", results[DOMAIN_NAME_SUFFIX]); fprintf(file, "\toption routers %s;\n", greenaddress); if (strlen(results[PRIMARY_DNS])) { fprintf(file, "\toption domain-name-servers "); fprintf(file, "%s", results[PRIMARY_DNS]); if (strlen(results[SECONDARY_DNS])) fprintf(file, ", %s", results[SECONDARY_DNS]); fprintf(file, ";\n"); } fprintf(file, "\trange %s %s;\n", results[START_ADDRESS], results[END_ADDRESS]); fprintf(file, "\tdefault-lease-time %d;\n", (int) atol(results[DEFAULT_LEASE_TIME]) * 60); fprintf(file, "\tmax-lease-time %d;\n", (int) atol(results[MAX_LEASE_TIME]) * 60); fprintf(file, "}\n"); fclose(file); chown(CONFIG_ROOT "/dhcp/dhcpd.conf", 99, 99); if (automode == 0) mysystem(NULL, "/usr/local/bin/dhcpctrl enable"); } result = 1; } else result = 0; } while (error); newtFormDestroy(dhcpform); newtPopWindow(); freekeyvalues(dhcpkv); freekeyvalues(ethernetkv); freekeyvalues(mainkv); return result; }
/* Small window to change IP and Netmask of some colour */ void changeaddress(char *colour, int *changed_flag) { newtComponent networkform; newtComponent text; newtComponent ok, cancel; struct newtExitStruct exitstruct; char keyvalue[STRING_SIZE]; char addresskey[STRING_SIZE]; char netmaskkey[STRING_SIZE]; char netaddresskey[STRING_SIZE]; newtComponent addresslabel; newtComponent netmasklabel; newtComponent addressentry; newtComponent netmaskentry; const char *addressresult; const char *netmaskresult; char message[STRING_SIZE_LARGE]; int error; int numLines; char *tmpstring; /* Build some key strings. */ sprintf(addresskey, "%s_1_ADDRESS", colour); sprintf(netmaskkey, "%s_1_NETMASK", colour); sprintf(netaddresskey, "%s_1_NETADDRESS", colour); /* workaround gcc warning, there is really 1 %s there */ tmpstring=strdup(gettext("TR_ENTER_THE_IP_ADDRESS_INFORMATION")); snprintf(message, STRING_SIZE, tmpstring, colour); free(tmpstring); text = newtTextboxReflowed(1, 1, message, 68, 0, 0, 0); numLines = newtTextboxGetNumLines(text); /* workaround gcc warning, there is really 1 %s there */ tmpstring=strdup(gettext("TR_INTERFACE")); snprintf(message, STRING_SIZE, tmpstring, colour); free(tmpstring); newtCenteredWindow(72, 10 + numLines, message); networkform = newtForm(NULL, NULL, 0); newtFormAddComponent(networkform, text); /* Address */ addresslabel = newtTextbox(2, 2 + numLines, 18, 1, 0); newtTextboxSetText(addresslabel, gettext("TR_IP_ADDRESS_PROMPT")); if (!strcmp(colour, "GREEN")) { /* green only for now */ strcpy(keyvalue, DEFAULT_IP); } else { strcpy(keyvalue, ""); } find_kv_default(eth_kv, addresskey, keyvalue); addressentry = newtEntry(20, 2 + numLines, keyvalue, 20, &addressresult, 0); newtEntrySetFilter(addressentry, filterip, NULL); newtFormAddComponent(networkform, addresslabel); newtFormAddComponent(networkform, addressentry); /* Netmask */ netmasklabel = newtTextbox(2, 3 + numLines, 18, 1, 0); newtTextboxSetText(netmasklabel, gettext("TR_NETMASK_PROMPT")); strcpy(keyvalue, DEFAULT_NETMASK); find_kv_default(eth_kv, netmaskkey, keyvalue); netmaskentry = newtEntry(20, 3 + numLines, keyvalue, 20, &netmaskresult, 0); newtEntrySetFilter(netmaskentry, filterip, NULL); newtFormAddComponent(networkform, netmasklabel); newtFormAddComponent(networkform, netmaskentry); ok = newtButton(6, 5 + numLines, gettext("TR_OK")); /* In case of installer we need a valid address, no turning back */ if (flag_is_state == setupchroot) { newtFormAddComponent(networkform, ok); } else { cancel = newtButton(26, 5 + numLines, gettext("TR_GO_BACK")); newtFormAddComponents(networkform, ok, cancel, NULL); } newtRefresh(); newtDrawForm(networkform); do { error = 0; newtFormRun(networkform, &exitstruct); if (exitstruct.u.co == ok) { strcpy(message, gettext("TR_INVALID_FIELDS")); if (VALID_IP(addressresult) == FALSE) { strcat(message, gettext("TR_IP_ADDRESS_CR")); error = 1; newtFormSetCurrent(networkform, addressentry); } if (VALID_IP(netmaskresult) == FALSE) { strcat(message, gettext("TR_NETWORK_MASK_CR")); error = 1; newtFormSetCurrent(networkform, netmaskentry); } // TODO: additional network mask validation if (error) { errorbox(message); } else { /* all is well, calc netaddress and store everything */ unsigned long int intaddress; unsigned long int intnetaddress; unsigned long int intnetmask; struct in_addr i_addr; char *netaddress; update_kv(ð_kv, addresskey, (char *) addressresult); update_kv(ð_kv, netmaskkey, (char *) netmaskresult); /* calculate netaddress */ intaddress = inet_addr(addressresult); intnetmask = inet_addr(netmaskresult); intnetaddress = intaddress & intnetmask; i_addr.s_addr = intnetaddress; netaddress = inet_ntoa(i_addr); update_kv(ð_kv, netaddresskey, (char *) netaddress); changed_config = 1; *changed_flag = 1; } } } while (error); newtFormDestroy(networkform); newtPopWindow(); }
static void nmt_newt_dialog_g_log_handler (const char *log_domain, GLogLevelFlags log_level, const char *message, gpointer user_data) { const char *level_name; char *full_message; int screen_width, screen_height; newtComponent text, ok, form; newtGrid grid; g_assert (!(log_level & G_LOG_FLAG_RECURSION)); if (log_level & G_LOG_LEVEL_DEBUG) return; switch (log_level & G_LOG_LEVEL_MASK) { case G_LOG_LEVEL_ERROR: level_name = "ERROR"; break; case G_LOG_LEVEL_CRITICAL: level_name = "CRITICAL"; break; case G_LOG_LEVEL_WARNING: level_name = "WARNING"; break; case G_LOG_LEVEL_MESSAGE: level_name = "Message"; break; default: level_name = NULL; } full_message = g_strdup_printf ("%s%s%s%s%s", log_domain ? log_domain : "", log_domain && level_name ? " " : "", level_name ? level_name : "", log_domain || level_name ? ": " : "", message); /* newtWinMessage() wraps the window too narrowly by default, so * we don't want to use that. But we intentionally avoid using any * NmtNewt classes, to avoid possible error recursion. */ newtGetScreenSize (&screen_width, &screen_height); text = newtTextboxReflowed (-1, -1, full_message, MAX (70, screen_width - 10), 0, 0, 0); g_free (full_message); ok = newtButton (-1, -1, "OK"); grid = newtCreateGrid (1, 2); newtGridSetField (grid, 0, 0, NEWT_GRID_COMPONENT, text, 0, 0, 0, 0, 0, 0); newtGridSetField (grid, 0, 1, NEWT_GRID_COMPONENT, ok, 0, 1, 0, 0, NEWT_ANCHOR_RIGHT, 0); newtGridWrappedWindow (grid, (char *) (level_name ? level_name : "")); newtGridFree (grid, TRUE); form = newtForm (NULL, NULL, 0); newtFormAddComponents (form, text, ok, NULL); newtRunForm (form); newtFormDestroy (form); newtPopWindow (); }
/** * Ask the user which backup media type they would like to use. * The choices are @p none (exit to shell), @c cdr, @c cdrw, @c dvd, * @c tape, @c cdstream, @c udev (only when @p g_text_mode is TRUE), @c nfs, * and @c iso. * @param restoring TRUE if we're restoring, FALSE if we're backing up. * @return The backup type chosen, or @c none if the user chose "Exit to shell". */ t_bkptype which_backup_media_type(bool restoring) { /*@ char ************************************************************ */ t_bkptype output; /*@ newt ************************************************************ */ char *title_sz; char *minimsg_sz; static t_bkptype possible_bkptypes[] = { none, cdr, cdrw, dvd, tape, cdstream, udev, nfs, iso }; static char *possible_responses[] = { "none", "cdr", "cdrw", "dvd", "tape", "cdstream", "udev", "nfs", "iso", NULL }; char *outstr = NULL; t_bkptype backup_type; int i; size_t n = 0; newtComponent b1; newtComponent b2; newtComponent b3; newtComponent b4; newtComponent b5; newtComponent b6; newtComponent b7; newtComponent b8; newtComponent b_res; newtComponent myForm; if (g_text_mode) { for (backup_type = none; backup_type == none;) { printf(_("Backup type (")); for (i = 0; possible_responses[i]; i++) { printf("%c%s", (i == 0) ? '\0' : ' ', possible_responses[i]); } printf(")\n--> "); (void) getline(&outstr, &n, stdin); strip_spaces(outstr); for (i = 0; possible_responses[i]; i++) { if (!strcmp(possible_responses[i], outstr)) { backup_type = possible_bkptypes[i]; } } } paranoid_free(outstr); return (backup_type); } newtDrawRootText(18, 0, WELCOME_STRING); if (restoring) { asprintf(&title_sz, _("Please choose the backup media from which you want to read data.")); asprintf(&minimsg_sz, _("Read from:")); } else { asprintf(&title_sz, _("Please choose the backup media to which you want to archive data.")); asprintf(&minimsg_sz, _("Backup to:")); } newtPushHelpLine(title_sz); paranoid_free(title_sz); // newtOpenWindow (23, 3, 34, 17, minimsg_sz); newtCenteredWindow(34, 17, minimsg_sz); paranoid_free(minimsg_sz); b1 = newtButton(1, 1, _("CD-R disks ")); b2 = newtButton(17, 1, _("CD-RW disks")); b3 = newtButton(1, 9, _("Tape drive ")); b4 = newtButton(17, 5, _("CD streamer")); b5 = newtButton(1, 5, _(" DVD disks ")); b6 = newtButton(17, 9, _(" NFS mount ")); b7 = newtButton(1, 13, _(" Hard disk ")); b8 = newtButton(17, 13, _(" Exit ")); myForm = newtForm(NULL, NULL, 0); newtFormAddComponents(myForm, b1, b5, b3, b7, b2, b4, b6, b8, NULL); b_res = newtRunForm(myForm); newtFormDestroy(myForm); newtPopWindow(); if (b_res == b1) { output = cdr; } else if (b_res == b2) { output = cdrw; } else if (b_res == b3) { output = tape; } else if (b_res == b4) { output = cdstream; } else if (b_res == b5) { output = dvd; } else if (b_res == b6) { output = nfs; } else if (b_res == b7) { output = iso; } else { output = none; } newtPopHelpLine(); return (output); }
int main(void) { newtComponent b1, b2, b3, b4; newtComponent answer, f, t; newtGrid grid, subgrid; char * flowedText; int textWidth, textHeight, rc; char * menuContents[] = { "One", "Two", "Three", "Four", "Five", NULL }; char * entries[10]; struct newtWinEntry autoEntries[] = { { "An entry", entries + 0, 0 }, { "Another entry", entries + 1, 0 }, { "Third entry", entries + 2, 0 }, { "Fourth entry", entries + 3, 0 }, { NULL, NULL, 0 } }; memset(entries, 0, sizeof(entries)); newtInit(); newtCls(); b1 = newtCheckbox(-1, -1, "An pretty long checkbox for testing", ' ', NULL, NULL); b2 = newtButton(-1, -1, "Another Button"); b3 = newtButton(-1, -1, "But, but"); b4 = newtButton(-1, -1, "But what?"); f = newtForm(NULL, NULL, 0); grid = newtCreateGrid(2, 2); newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, b1, 0, 0, 0, 0, NEWT_ANCHOR_RIGHT, 0); newtGridSetField(grid, 0, 1, NEWT_GRID_COMPONENT, b2, 0, 0, 0, 0, 0, 0); newtGridSetField(grid, 1, 0, NEWT_GRID_COMPONENT, b3, 0, 0, 0, 0, 0, 0); newtGridSetField(grid, 1, 1, NEWT_GRID_COMPONENT, b4, 0, 0, 0, 0, 0, 0); newtFormAddComponents(f, b1, b2, b3, b4, NULL); newtGridWrappedWindow(grid, "first window"); newtGridFree(grid, 1); answer = newtRunForm(f); newtFormDestroy(f); newtPopWindow(); flowedText = newtReflowText("This is a quite a bit of text. It is 40 " "columns long, so some wrapping should be " "done. Did you know that the quick, brown " "fox jumped over the lazy dog?\n\n" "In other news, it's pretty important that we " "can properly force a line break.", 40, 5, 5, &textWidth, &textHeight); t = newtTextbox(-1, -1, textWidth, textHeight, NEWT_FLAG_WRAP); newtTextboxSetText(t, flowedText); free(flowedText); b1 = newtButton(-1, -1, "Okay"); b2 = newtButton(-1, -1, "Cancel"); grid = newtCreateGrid(1, 2); subgrid = newtCreateGrid(2, 1); newtGridSetField(subgrid, 0, 0, NEWT_GRID_COMPONENT, b1, 0, 0, 0, 0, 0, 0); newtGridSetField(subgrid, 1, 0, NEWT_GRID_COMPONENT, b2, 0, 0, 0, 0, 0, 0); newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, t, 0, 0, 0, 1, 0, 0); newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid, 0, 0, 0, 0, 0, NEWT_GRID_FLAG_GROWX); newtGridWrappedWindow(grid, "another example"); newtGridDestroy(grid, 1); f = newtForm(NULL, NULL, 0); newtFormAddComponents(f, b1, t, b2, NULL); answer = newtRunForm(f); newtPopWindow(); newtFormDestroy(f); newtWinMessage("Simple", "Ok", "This is a simple message window"); newtWinChoice("Simple", "Ok", "Cancel", "This is a simple choice window"); textWidth = 0; rc = newtWinMenu("Test Menu", "This is a sample invovation of the " "newtWinMenu() call. It may or may not have a scrollbar, " "depending on the need for one.", 50, 5, 5, 3, menuContents, &textWidth, "Ok", "Cancel", NULL); rc = newtWinEntries("Text newtWinEntries()", "This is a sample invovation of " "newtWinEntries() call. It lets you get a lot of input " "quite easily.", 50, 5, 5, 20, autoEntries, "Ok", "Cancel", NULL); newtFinished(); printf("rc = 0x%x item = %d\n", rc, textWidth); return 0; }
/* This is a groovie dialog for showing network info. Takes a keyvalue list, * a colour and a dhcp flag. Shows the current settings, and rewrites them * if necessary. DHCP flag sets wether to show the dhcp checkbox. */ int changeaddress(struct keyvalue *kv, char *colour, int typeflag, char *defaultdhcphostname) { char *addressresult; char *netmaskresult; char *dhcphostnameresult; char *dhcpforcemturesult; struct newtExitStruct es; newtComponent header; newtComponent addresslabel; newtComponent netmasklabel; newtComponent dhcphostnamelabel; newtComponent dhcpforcemtulabel; newtComponent ok, cancel; char message[1000]; char temp[STRING_SIZE]; char addressfield[STRING_SIZE]; char netmaskfield[STRING_SIZE]; char typefield[STRING_SIZE]; char dhcphostnamefield[STRING_SIZE]; char dhcpforcemtufield[STRING_SIZE]; int error; int result = 0; char type[STRING_SIZE]; int startstatictype = 0; int startdhcptype = 0; int startpppoetype = 0; /* Build some key strings. */ sprintf(addressfield, "%s_ADDRESS", colour); sprintf(netmaskfield, "%s_NETMASK", colour); sprintf(typefield, "%s_TYPE", colour); sprintf(dhcphostnamefield, "%s_DHCP_HOSTNAME", colour); sprintf(dhcpforcemtufield, "%s_DHCP_FORCE_MTU", colour); sprintf(message, _("Interface - %s"), colour); newtCenteredWindow(44, (typeflag ? 18 : 12), message); networkform = newtForm(NULL, NULL, 0); sprintf(message, _("Enter the IP address information for the %s interface."), colour); header = newtTextboxReflowed(1, 1, message, 42, 0, 0, 0); newtFormAddComponent(networkform, header); /* See if we need a dhcp checkbox. If we do, then we shift the contents * of the window down two rows to make room. */ if (typeflag) { strcpy(temp, "STATIC"); findkey(kv, typefield, temp); if (strcmp(temp, "STATIC") == 0) startstatictype = 1; if (strcmp(temp, "DHCP") == 0) startdhcptype = 1; if (strcmp(temp, "PPPOE") == 0) startpppoetype = 1; statictyperadio = newtRadiobutton(2, 4, _("Static"), startstatictype, NULL); dhcptyperadio = newtRadiobutton(2, 5, _("DHCP"), startdhcptype, statictyperadio); pppoetyperadio = newtRadiobutton(2, 6, _("PPP DIALUP (PPPoE, modem, ATM ...)"), startpppoetype, dhcptyperadio); newtFormAddComponents(networkform, statictyperadio, dhcptyperadio, pppoetyperadio, NULL); newtComponentAddCallback(statictyperadio, networkdialogcallbacktype, NULL); newtComponentAddCallback(dhcptyperadio, networkdialogcallbacktype, NULL); newtComponentAddCallback(pppoetyperadio, networkdialogcallbacktype, NULL); dhcphostnamelabel = newtTextbox(2, 8, 18, 1, 0); newtTextboxSetText(dhcphostnamelabel, _("DHCP Hostname:")); dhcpforcemtulabel = newtTextbox(2, 9, 18, 1, 0); newtTextboxSetText(dhcpforcemtulabel, _("Force DHCP MTU:")); strcpy(temp, defaultdhcphostname); findkey(kv, dhcphostnamefield, temp); dhcphostnameentry = newtEntry(20, 8, temp, 20, &dhcphostnameresult, 0); strcpy(temp, ""); findkey(kv, dhcpforcemtufield, temp); dhcpforcemtuentry = newtEntry(20, 9, temp, 20, &dhcpforcemturesult, 0); newtFormAddComponent(networkform, dhcphostnamelabel); newtFormAddComponent(networkform, dhcphostnameentry); newtFormAddComponent(networkform, dhcpforcemtulabel); newtFormAddComponent(networkform, dhcpforcemtuentry); if (startdhcptype == 0) { newtEntrySetFlags(dhcphostnameentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); newtEntrySetFlags(dhcpforcemtuentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); } } /* Address */ addresslabel = newtTextbox(2, (typeflag ? 11 : 4) + 0, 18, 1, 0); newtTextboxSetText(addresslabel, _("IP address:")); strcpy(temp, ""); findkey(kv, addressfield, temp); addressentry = newtEntry(20, (typeflag ? 11 : 4) + 0, temp, 20, &addressresult, 0); newtEntrySetFilter(addressentry, ip_input_filter, NULL); if (typeflag == 1 && startstatictype == 0) newtEntrySetFlags(addressentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); newtFormAddComponent(networkform, addresslabel); newtFormAddComponent(networkform, addressentry); /* Netmask */ netmasklabel = newtTextbox(2, (typeflag ? 11 : 4) + 1, 18, 1, 0); newtTextboxSetText(netmasklabel, _("Network mask:")); strcpy(temp, "255.255.255.0"); findkey(kv, netmaskfield, temp); netmaskentry = newtEntry(20, (typeflag ? 11 : 4) + 1, temp, 20, &netmaskresult, 0); newtEntrySetFilter(netmaskentry, ip_input_filter, NULL); if (typeflag == 1 && startstatictype == 0) newtEntrySetFlags(netmaskentry, NEWT_FLAG_DISABLED, NEWT_FLAGS_SET); newtFormAddComponent(networkform, netmasklabel); newtFormAddComponent(networkform, netmaskentry); /* Buttons. */ ok = newtButton(8, (typeflag ? 14 : 7), _("OK")); cancel = newtButton(26, (typeflag ? 14 : 7), _("Cancel")); newtFormAddComponents(networkform, ok, cancel, NULL); newtRefresh(); newtDrawForm(networkform); do { error = 0; newtFormRun(networkform, &es); if (es.u.co == ok) { /* OK was pressed; verify the contents of each entry. */ strcpy(message, _("The following fields are invalid:")); strcat(message, "\n\n"); strcpy(type, "STATIC"); if (typeflag) gettype(type); if (strcmp(type, "STATIC") == 0) { if (inet_addr(addressresult) == INADDR_NONE) { strcat(message, _("IP address")); strcat(message, "\n"); error = 1; } if (inet_addr(netmaskresult) == INADDR_NONE) { strcat(message, _("Network mask")); strcat(message, "\n"); error = 1; } } if (strcmp(type, "DHCP") == 0) { if (!strlen(dhcphostnameresult)) { strcat(message, _("DHCP hostname")); strcat(message, "\n"); error = 1; } } if (error) errorbox(message); else { /* No errors! Set new values, depending on dhcp flag etc. */ if (typeflag) { replacekeyvalue(kv, dhcphostnamefield, dhcphostnameresult); replacekeyvalue(kv, dhcpforcemtufield, dhcpforcemturesult); if (strcmp(type, "STATIC") != 0) { replacekeyvalue(kv, addressfield, "0.0.0.0"); replacekeyvalue(kv, netmaskfield, "0.0.0.0"); } else { replacekeyvalue(kv, addressfield, addressresult); replacekeyvalue(kv, netmaskfield, netmaskresult); } replacekeyvalue(kv, typefield, type); } else { replacekeyvalue(kv, addressfield, addressresult); replacekeyvalue(kv, netmaskfield, netmaskresult); } setnetaddress(kv, colour); result = 1; } } /* Workaround for a bug that dhcp radiobutton also end the dialog at arm */ else { if (es.u.co != cancel) { error = 1; } } } while (error); newtFormDestroy(networkform); newtPopWindow(); return result; }
/** * Ask the user to enter a value. * @param title The title of the dialog box. * @param b The blurb (e.g. what you want the user to enter). * @param output The string to put the user's answer in. It has to be freed by the caller * @return TRUE if the user pressed OK, FALSE if they pressed Cancel. */ bool popup_and_get_string(char *title, char *b, char *output) { /*@ newt ************************************************************ */ newtComponent myForm; newtComponent b_1; newtComponent b_2; newtComponent b_res; newtComponent text; newtComponent type_here; /*@ pointers ********************************************************* */ char *entry_value = NULL; /*@ buffers ********************************************************** */ char *blurb = NULL; size_t n = 0; bool ret = TRUE; assert_string_is_neither_NULL_nor_zerolength(title); assert(b != NULL); if (g_text_mode) { printf ("---promptstring---1--- %s\r\n---promptstring---2--- %s\r\n---promptstring---Q---\r\n--> ", title, b); paranoid_free(output); (void) getline(&output, &n, stdin); if (output[strlen(output) - 1] == '\n') output[strlen(output) - 1] = '\0'; return (ret); } asprintf(&blurb, b); text = newtTextboxReflowed(2, 1, blurb, 48, 5, 5, 0); type_here = newtEntry(2, newtTextboxGetNumLines(text) + 2, output, 50, &entry_value, NEWT_FLAG_RETURNEXIT ); b_1 = newtButton(6, newtTextboxGetNumLines(text) + 4, _(" OK ")); b_2 = newtButton(18, newtTextboxGetNumLines(text) + 4, _("Cancel")); newtCenteredWindow(54, newtTextboxGetNumLines(text) + 9, title); myForm = newtForm(NULL, NULL, 0); newtFormAddComponents(myForm, text, type_here, b_1, b_2, NULL); /* BERLIOS: center_string is now broken replace it ! */ //center_string(blurb, 80); newtPushHelpLine(blurb); paranoid_free(blurb); b_res = newtRunForm(myForm); newtPopHelpLine(); if (b_res == b_2) { ret = FALSE; } else { // Copy entry_value before destroying the form // clearing potentially output before paranoid_alloc(output,entry_value); } newtFormDestroy(myForm); newtPopWindow(); return(ret); }
/** * Pop up a dialog box with user-defined buttons. * @param p The text to put in the dialog box. * @param button1 The label on the first button. * @param button2 The label on the second button, or "" if you only want one button. * @return TRUE if @p button1 was pushed, FALSE otherwise. */ bool popup_with_buttons(char *p, char *button1, char *button2) { /*@ buffers *********************************************************** */ char *prompt; char *tmp = NULL; size_t n = 0; /*@ newt ************************************************************** */ newtComponent myForm; newtComponent b_1; newtComponent b_2; newtComponent b_res; newtComponent text; assert_string_is_neither_NULL_nor_zerolength(p); assert(button1 != NULL); assert(button2 != NULL); if (g_text_mode) { if (strlen(button2) == 0) { printf("%s (%s) --> ", p, button1); } else { printf("%s (%s or %s) --> ", p, button1, button2); } for (asprintf(&tmp, " "); strcmp(tmp, button1) && (strlen(button2) == 0 || strcmp(tmp, button2));) { printf("--> "); paranoid_free(tmp); (void) getline(&tmp, &n, stdin); } if (!strcmp(tmp, button1)) { paranoid_free(tmp); return (TRUE); } else { paranoid_free(tmp); return (FALSE); } } asprintf(&prompt, p); text = newtTextboxReflowed(1, 1, prompt, 40, 5, 5, 0); b_1 = newtButton(20 - ((button2[0] != '\0') ? strlen(button1) + 2 : strlen(button1) / 2), newtTextboxGetNumLines(text) + 3, button1); if (button2[0] != '\0') { b_2 = newtButton(24, newtTextboxGetNumLines(text) + 3, button2); } else { b_2 = NULL; } // newtOpenWindow (25, 5, 46, newtTextboxGetNumLines (text) + 7, "Alert"); newtCenteredWindow(46, newtTextboxGetNumLines(text) + 7, _("Alert")); myForm = newtForm(NULL, NULL, 0); newtFormAddComponents(myForm, text, b_1, b_2, NULL); /* BERLIOS: center_string is now broken replace it ! */ //center_string(prompt, 80); newtPushHelpLine(prompt); paranoid_free(prompt); b_res = newtRunForm(myForm); newtPopHelpLine(); newtFormDestroy(myForm); newtPopWindow(); if (b_res == b_1) { return (TRUE); } else { return (FALSE); } }
static int newtChecklist(const char* title, const char* message, unsigned int width, unsigned int height, unsigned int num_entries, const char** entries, int* states) { int ret; const int list_height = 4; char cbstates[num_entries]; for (unsigned int i = 0; i < num_entries; i++) { cbstates[i] = states[i] ? '*' : ' '; } newtCenteredWindow(width, height, title); newtComponent textbox = newtTextbox(1, 1, width - 2, height - 6 - list_height, NEWT_FLAG_WRAP); newtTextboxSetText(textbox, message); int top = newtTextboxGetNumLines(textbox) + 2; newtComponent form = newtForm(NULL, NULL, 0); newtComponent sb = NULL; if (list_height < num_entries) { sb = newtVerticalScrollbar( width - 4, top + 1, list_height, NEWT_COLORSET_CHECKBOX, NEWT_COLORSET_ACTCHECKBOX); newtFormAddComponent(form, sb); } newtComponent subform = newtForm(sb, NULL, 0); newtFormSetBackground(subform, NEWT_COLORSET_CHECKBOX); newtFormSetHeight(subform, list_height); newtFormSetWidth(subform, width - 10); for (unsigned int i = 0; i < num_entries; i++) { newtComponent cb = newtCheckbox(4, top + i, entries[i], cbstates[i], NULL, &cbstates[i]); newtFormAddComponent(subform, cb); } newtFormAddComponents(form, textbox, subform, NULL); newtComponent btn_okay = newtButton((width - 18) / 3, height - 4, _("OK")); newtComponent btn_cancel = newtButton((width - 18) / 3 * 2 + 9, height - 4, _("Cancel")); newtFormAddComponents(form, btn_okay, btn_cancel, NULL); newtComponent answer = newtRunForm(form); if ((answer == NULL) || (answer == btn_cancel)) { ret = -1; } else { ret = 0; for (unsigned int i = 0; i < num_entries; i++) { states[i] = (cbstates[i] != ' '); if (states[i]) ret++; } } newtFormDestroy(form); newtPopWindow(); return ret; }
int main(void){ newtComponent hForm, button1, button2; newtComponent astIpLabel, astPortLabel, astUserLabel, astPassLabel; newtComponent astIpEntry, astPortEntry, astUserEntry, astPassEntry; const char *astIpVal, *astPortVal, *astUserVal, *astPassVal; newtComponent humbugHostLabel, humbugPortLabel, humbugApiLabel; newtComponent humbugHostEntry, humbugPortEntry, humbugApiEntry; const char *humbugHostVal, *humbugPortVal, *humbugApiVal; int i, y; newtComponent eventsLabel, eventsChk[EVENTS_SIZE], chkList; newtComponent encChk, encLabel, encEntry, chkLog; char encVal, logVal; const char *encKey; char eventsList[EVENTS_SIZE][16]; char eventsResults[EVENTS_SIZE]; strcpy(eventsList[0], "CDR"); strcpy(eventsList[1], "NewExten"); strcpy(eventsList[2], "Reload"); strcpy(eventsList[3], "Shutdown"); strcpy(eventsList[4], "Alarm"); strcpy(eventsList[5], "AlarmClear"); strcpy(eventsList[6], "CollectorAlarm"); struct newtExitStruct es; params = (parameters*) calloc(1, sizeof(parameters)); if(readHumbugConfig() < 0) { exit(EXIT_FAILURE); } newtInit(); newtCls(); newtDrawRootText(1, 0, "Text Mode Setup for humbug-collector (c) 2011 Humbug Telecom Labs, Ltd."); newtPushHelpLine(" <Tab>/<Alt-Tab> between elements | <ESC> exits without save | <F12> for help"); newtCenteredWindow(66, 17, "Humbug Setup"); astIpLabel = newtLabel(1, 1, " Manager IP:"); astIpEntry = newtEntry(19, 1, params->astIpVal, 20, &astIpVal, 0); astPortLabel = newtLabel(1, 2, " Manager Port:"); astPortEntry = newtEntry(19, 2, params->astPortVal, 20, &astPortVal, 0); astUserLabel = newtLabel(1, 3, "Manager Username:"******"Manager Password:"******" Humbug Host:"); humbugHostEntry = newtEntry(19, 6, params->humbugHost, 20, &humbugHostVal, 0); humbugPortLabel = newtLabel(1, 7, " Humbug Port:"); humbugPortEntry = newtEntry(19, 7, params->humbugPort, 20, &humbugPortVal, 0); humbugApiLabel = newtLabel(1, 8, " Humbug API key:"); humbugApiEntry = newtEntry(19, 8, params->humbugApikey, 20, &humbugApiVal, NEWT_FLAG_SCROLL); if(1 == params->encrypted){ encChk = newtCheckbox(19, 10, "Encrypted", 'X', " X", &encVal); }else{ encChk = newtCheckbox(19, 10, "Encrypted", ' ', " X", &encVal); } encLabel = newtLabel(1, 11, " Encryption key:"); encEntry = newtEntry(19, 11, params->humbugKey, 42, &encKey, NEWT_FLAG_SCROLL); if(strlen(params->log_file) > 0 ){ chkLog = newtCheckbox(19, 13, "Log ON/OFF", 'X', " X", &logVal); }else{ chkLog = newtCheckbox(19, 13, "Log ON/OFF", ' ', " X", &logVal); } eventsLabel = newtLabel(42, 1, "Events for analyze:"); button1 = newtButton(36, 13, "Save & Exit"); button2 = newtButton(53, 13, "Quit"); chkList = newtForm(NULL, NULL, 0); newtFormSetBackground(chkList, NEWT_COLORSET_CHECKBOX); for(i=0; i<EVENTS_SIZE; i++) { int selected = 0; for(y=0; y<params->eventsSize; y++){ if(0 == strcasecmp(eventsList[i], params->events[y])){ selected = 1; break; } } if (1 == selected) { eventsChk[i] = newtCheckbox(47, 2 + i, eventsList[i], 'X', " X", &eventsResults[i]); } else { eventsChk[i] = newtCheckbox(47, 2 + i, eventsList[i], ' ', " X", &eventsResults[i]); } newtFormAddComponent(chkList, eventsChk[i]); } hForm = newtForm(NULL, NULL, 0); newtFormAddComponents(hForm, button1, button2, astIpLabel, astPortLabel, astUserLabel, astPassLabel, astIpEntry, astPortEntry, astUserEntry, astPassEntry, humbugHostLabel, humbugPortLabel, humbugApiLabel, humbugHostEntry, humbugPortEntry, humbugApiEntry, eventsLabel, chkList, encChk, encLabel, encEntry, chkLog, NULL); for(;;){ do{ newtFormRun(hForm, &es); }while(es.reason == NEWT_EXIT_TIMER); if (es.reason == NEWT_EXIT_HOTKEY) { int done = 0; switch (es.u.key) { case NEWT_KEY_ESCAPE: done = 1; break; case NEWT_KEY_F12: show_help(); break; } if(done) break; }else if (es.reason == NEWT_EXIT_COMPONENT) { if(es.u.co == button1){ strcpy(params->astIpVal, astIpVal); strcpy(params->astPortVal, astPortVal); strcpy(params->astUserVal, astUserVal); strcpy(params->astPassVal, astPassVal); strcpy(params->humbugApikey, humbugApiVal); params->eventsSize = 0; for(i=0; i<EVENTS_SIZE; i++) { if(eventsResults[i] == 'X') { strcpy(params->events[params->eventsSize], eventsList[i]); params->eventsSize++; } } params->encrypted = (encVal == 'X') ? 1 : 0; strcpy(params->humbugKey, encKey); strcpy(params->log_file, HBG_LOG_FILE); saveHumbugConfig(); } break; } } newtPopWindow(); newtPopHelpLine(); newtFinished(); newtFormDestroy(hForm); free(params); return(EXIT_SUCCESS); }