/* * sends command to the menu */ static int wdg_file_driver(struct wdg_object *wo, int key, struct wdg_mouse_event *mouse) { WDG_WO_EXT(struct wdg_file_handle, ww); int c; struct stat buf; c = menu_driver(ww->m, wdg_file_virtualize(key) ); /* skip non selectable items */ if ( !(item_opts(current_item(ww->m)) & O_SELECTABLE) ) c = menu_driver(ww->m, wdg_file_virtualize(key) ); /* one item has been selected */ if (c == E_UNKNOWN_COMMAND) { /* the item is not selectable (probably selected with mouse) */ if ( !(item_opts(current_item(ww->m)) & O_SELECTABLE) ) return WDG_ESUCCESS; stat(item_name(current_item(ww->m)), &buf); /* if it is a directory, change to it */ if (S_ISDIR(buf.st_mode)) { chdir(item_name(current_item(ww->m))); return -WDG_ENOTHANDLED; } else { /* invoke the callback and return */ wdg_file_callback(wo, ww->curpath, (char *)item_name(current_item(ww->m))); return WDG_ESUCCESS; } } wnoutrefresh(ww->mwin); return WDG_ESUCCESS; }
int main(int argc, char const *argv[]) { ITEM **my_items; int c; MENU *my_menu; WINDOW *my_menu_win; int n_choices, i; initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); init_pair(1, COLOR_RED, COLOR_BLACK); n_choices = ARRAY_SIZE(choices); my_items = (ITEM **) calloc(n_choices, sizeof(ITEM *)); for (i = 0; i < n_choices; ++i) my_items[i] = new_item(choices[i], choices[i]); my_menu = new_menu((ITEM **) my_items); my_menu_win = newwin(10, 40, 4, 4); keypad(my_menu_win, TRUE); set_menu_win(my_menu, my_menu_win); set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); set_menu_mark(my_menu, " * "); box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 40, "My Menu", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 38); mvwaddch(my_menu_win, 2, 39, ACS_RTEE); mvprintw(LINES - 2, 0, "F1 to exit"); refresh(); post_menu(my_menu); wrefresh(my_menu_win); while ((c = wgetch(my_menu_win)) != KEY_F(1)) { switch (c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; } wrefresh(my_menu_win); } unpost_menu(my_menu); free_menu(my_menu); for (i = 0; i < n_choices; ++i) free_item(my_items[i]); endwin(); return 0; }
int generic_menu::get_choice() { if (choices.size() == return_values.size()) { //create the menu and the item pointer vector MENU* my_menu; std::vector<ITEM*> my_items; WINDOW* menu_win = derwin(screen, choices.size(), 30, ypos, xpos); ITEM* cur = NULL; for (int x = 0; x < choices.size(); x++) { //populate the items vector with the string data in choices //c_str because ncurses is very old my_items.push_back(new_item(choices.at(x).c_str(), NULL)); } //pushback a null item for safety my_items.push_back((ITEM*)NULL); //create the menu and attach the items my_menu = new_menu((ITEM**)my_items.data()); //print the desc and post the menu mvwprintw(screen, LINES - 3, 0, "%s\n", desc.c_str()); //set_current_item(my_menu, lastItem); set_menu_win(my_menu, menu_win); keypad(menu_win, TRUE); post_menu(my_menu); touchwin(screen); wrefresh(menu_win); std::vector<int>::iterator return_iter = return_values.begin(); int c = 0; while((c = wgetch(menu_win)) != '\n') { touchwin(screen); wrefresh(menu_win); switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); if (return_iter != return_values.end()) { return_iter++; } break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); if (return_iter != return_values.begin()) { return_iter--; } break; } } unpost_menu(my_menu); for (int x = 0; x < my_items.size(); x++) { free_item(my_items[x]); } free_menu(my_menu); return *return_iter; } }
int mainmenu(int height, int width) { int ret; ITEM **menu_items; MENU *main_menu; int n_choices, i; clean_main_menu(); build_main_menu(instance_path); n_choices = menu_size; menu_items = (ITEM **) calloc(n_choices + 1, sizeof (ITEM *)); for (i = 0; i < n_choices; ++i) { menu_items[i] = new_item(menu_values[i], menu_values[i]); set_item_userptr(menu_items[i], (void *) menu_selected); } menu_items[n_choices] = (ITEM *) NULL; main_menu = new_menu((ITEM **) menu_items); menu_opts_off(main_menu, O_SHOWDESC); set_menu_sub(main_menu, derwin(stdscr, 10, 50, 6, 10)); post_menu(main_menu); attron(A_BOLD); mvprintw(0, 0, name); mvprintw(0, width - strlen(vers), vers); attroff(A_BOLD); refresh(); pos_menu_cursor(main_menu); while ((i = getch()) != KEY_F(4)) { switch (i) { case KEY_DOWN: menu_driver(main_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(main_menu, REQ_UP_ITEM); break; case 10: { ITEM *cur; int (*p)(char *); cur = current_item(main_menu); p = (int (*)(char *))item_userptr(cur); ret = p((char *) item_name(cur)); pos_menu_cursor(main_menu); goto menu_sel; } } } if (i == KEY_F(4)) { ret = menu_size - 1; } menu_sel: unpost_menu(main_menu); free_menu(main_menu); for (i = 0; i < n_choices; ++i) free_item(menu_items[i]); return ret; }
void displayMenu(int y, int x, char title[],int numOfOpt, char *options[], void (*p[])(void)){ int i,c; WINDOW *menuWindow; MENU *myMenu; ITEM **myOptions; ITEM *currentOption; initscr(); noecho(); cbreak(); keypad(stdscr,TRUE); init_pair(1,COLOR_CYAN,COLOR_BLACK); myOptions=(ITEM **)calloc(numOfOpt+1,sizeof(ITEM *)); for(i=0;i<numOfOpt;i++){ myOptions[i]=new_item(options[i]," "); set_item_userptr(myOptions[i],p[i]); } myOptions[numOfOpt]=(ITEM *)NULL; myMenu=new_menu(myOptions); menuWindow = newwin(8,20,(LINES-y)/2,(COLS-x)/2); keypad(menuWindow,TRUE); set_menu_win(myMenu,menuWindow); set_menu_sub(myMenu, derwin(menuWindow,y-4,x-2,3,1)); set_menu_format(myMenu,numOfOpt,1); menu_opts_off(myMenu, O_SHOWDESC); set_menu_mark(myMenu," * "); post_menu(myMenu); wrefresh(menuWindow); while((c=wgetch(menuWindow))!=KEY_F(2)){ switch(c){ case KEY_UP: menu_driver(myMenu,REQ_UP_ITEM); break; case KEY_DOWN: menu_driver(myMenu,REQ_DOWN_ITEM); break; case 10:{ ITEM *temp; temp=current_item(myMenu); void(*pointer)(void); pointer=item_userptr(temp); pointer(); pos_menu_cursor(myMenu); menu_driver(myMenu,REQ_DOWN_ITEM); break; } wrefresh(menuWindow); } } /*unpost_menu(myMenu);*/ /*for(i=0;i<numOfOpt;++i){*/ /*free_item(myOptions[i]);*/ /*}*/ /*free_menu(myMenu);*/ /*free(optionsNumbers);*/ }
int main() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) my_items[i] = new_item(choices[i], choices[i]); my_items[n_choices] = (ITEM *)NULL; my_menu = new_menu((ITEM **)my_items); post_menu(my_menu); refresh(); while((c = getch()) != KEY_F(1)) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case 10: /* Enter */ cur_item = current_item(my_menu); move(LINES - 2, 0); clrtoeol(); mvprintw(LINES - 2, 0, "You have chosen %d item with name %s and description %s", item_index(cur_item) + 1, item_name(cur_item), item_description(cur_item)); refresh(); pos_menu_cursor(my_menu); break; } } free_item(my_items[0]); free_item(my_items[1]); free_menu(my_menu); endwin(); }
/* * Switch from field to buttons. * This change the foreground color of the button so the user think the cursor * is on the first button. */ static void switch_to_buttons(void) { form_driver(popup_form, REQ_VALIDATION); menu_driver(popup_menu, REQ_FIRST_ITEM); is_on_button = true; set_menu_fore(popup_menu, A_REVERSE); // "show" the button }
int nc_menu_driver(MENU *m) { int c; while((c = getch()) != 'q'/*27*/) { switch(c) { case KEY_DOWN: menu_driver(m, REQ_SCR_DLINE); menu_driver(m, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(m, REQ_SCR_ULINE); menu_driver(m, REQ_UP_ITEM); break; case 10: return ITEM_SELECTED; } } }
//Update left window logic. If enter key was not pressed //return NULL. Otherwise return pointer to selected item. ITEM * main_menu_left(WINDOW * window, MENU * menu){ int c = wgetch(window); switch(c){ case KEY_DOWN: menu_driver(menu, REQ_DOWN_ITEM); wrefresh(window); break; case KEY_UP: menu_driver(menu, REQ_UP_ITEM); wrefresh(window); break; case 10: return current_item(menu); break; } return NULL; }
int main(int argc, char *argv[]) { int ch; initscr(); atexit(quit); clear(); noecho(); curs_set(0); nl(); keypad(stdscr, TRUE); it = (ITEM **)calloc(5, sizeof(ITEM *)); it[0] = new_item("M1", ""); it[1] = new_item("M2", ""); it[2] = new_item("M3", ""); it[3] = new_item("Ende", ""); it[4] = 0; me = new_menu(it); post_menu(me); mvaddstr(7, 3, "Programm mittels Menü oder F2-Funktionstaste beenden"); refresh(); while ((ch = getch()) != KEY_F(2)) { switch (ch) { case KEY_DOWN: menu_driver(me, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(me, REQ_UP_ITEM); break; case 0xA: if (item_index(current_item(me)) == 3) { exit(0); } break; } } return 0; }
void exec_action_context_services(int ch) { ITEM *item; switch (ch) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case KEY_ENTER: case 10: item = current_item(my_menu); exec_action(item_userptr(item)); break; } }
int main() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) my_items[i] = new_item(choices[i], choices[i]); my_items[n_choices] = (ITEM *)NULL; my_menu = new_menu((ITEM **)my_items); mvprintw(LINES - 2, 0, "F1 to Exit"); post_menu(my_menu); refresh(); while((c = getch()) != KEY_F(1)) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; } } free_item(my_items[0]); free_item(my_items[1]); free_menu(my_menu); endwin(); }
void handle_char(smsgloader *_this,int ch) { if(_this->loadmenu) switch(ch) { case KEY_DOWN: menu_driver(_this->loadmenu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(_this->loadmenu, REQ_UP_ITEM); break; case KEY_LEFT: menu_driver(_this->loadmenu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(_this->loadmenu, REQ_RIGHT_ITEM); break; case KEY_NPAGE: menu_driver(_this->loadmenu, REQ_SCR_DPAGE); break; case KEY_PPAGE: menu_driver(_this->loadmenu, REQ_SCR_UPAGE); break; } }
gint vwm_fmod_wndlist_ON_KEYSTROKE(gint32 keystroke,WINDOW *window) { MENU *menu = NULL; MEVENT mevent; // ITEM *item; // guint32 window_state; menu = (MENU*)viper_window_get_userptr(window); if(keystroke == KEY_MOUSE) { menu_driver(menu,keystroke); getmouse(&mevent); if((mevent.bstate & BUTTON1_DOUBLE_CLICKED) == BUTTON1_DOUBLE_CLICKED) keystroke = KEY_CRLF; } viper_thread_enter(); if(keystroke == KEY_UP) menu_driver(menu,REQ_UP_ITEM); if(keystroke == KEY_DOWN) menu_driver(menu,REQ_DOWN_ITEM); if(keystroke == KEY_CRLF) { // item = current_item(menu); /* viper_wnd=(VIPER_WND*)item_userptr(item); */ viper_window_destroy(window); /* if((viper_wnd->window_state & STATE_VISIBLE)==FALSE) viper_window_unhide(viper_wnd->window); viper_window_set_top(viper_wnd->window); viper_window_focus(TOPMOST_WINDOW); */ /* viper_screen_redraw(REDRAW_ALL); */ viper_thread_leave(); return 1; } viper_window_redraw(window); viper_thread_leave(); return 1; }
void show_chooser_win(MENU *dirmenu, size_t items, char *buf) { int c = 0; size_t buflen, offset = 0; char *tmp; buflen = strlen(buf) - 1; set_menu_fore(dirmenu, A_REVERSE); returnval = set_menu_format(dirmenu, LINES, 1); post_menu(dirmenu); refresh(); while ((c = getch()) != 'q') { switch(c) { case 'j': case KEY_DOWN: menu_driver(dirmenu, REQ_DOWN_ITEM); break; case 'k': case KEY_UP: menu_driver(dirmenu, REQ_UP_ITEM); break; case KEY_NPAGE: menu_driver(dirmenu, REQ_SCR_DPAGE); break; case KEY_PPAGE: menu_driver(dirmenu, REQ_SCR_UPAGE); break; case KEY_END: menu_driver(dirmenu, REQ_LAST_ITEM); break; case KEY_HOME: menu_driver(dirmenu, REQ_FIRST_ITEM); break; case 10: /* Enter */ move(20, 0); clrtoeol(); tmp = (char *)item_name(current_item(dirmenu)); offset = strlcpy(buf, tmp, MAX_CHOICESIZE); if (offset >= MAX_CHOICESIZE) goto toolong; if (strlcpy(buf + offset, item_description(current_item(dirmenu)), MAX_CHOICESIZE - offset)) goto toolong; pos_menu_cursor(dirmenu); break; } } toolong: unpost_menu(dirmenu); fprintf(stdout, "shit happens\n"); fprintf(stdout, "VALUE WAS: %d\n", returnval); }
/* menu handler */ void menu_handler(void) { int c; keypad(sub_window,TRUE); /* enable keypad on the menu */ wtimeout(sub_window,-1); /* wait for input */ while((c=wgetch(sub_window))) { switch (c) { case KEY_MOUSE: menu_driver(menu_ptr,KEY_MOUSE); break; case 9: /* TAB */ case 'k': case KEY_DOWN: menu_driver(menu_ptr,REQ_DOWN_ITEM); break; case 'j': case KEY_UP: menu_driver(menu_ptr,REQ_UP_ITEM); break; case 27: /* ESC */ case 'q': delete_menu(); finish(0); break; case 13: case KEY_ENTER: case KEY_RIGHT: func_ptr = item_userptr(current_item(menu_ptr)); delete_menu(); return; default: break; } } return; }
static struct result get_selection(struct selection *selection) { while (true) { int ch = wgetch(selection->window); if (ERR == ch) return result_ncurses_err(); if (KEY_DOWN == ch) { int result = menu_driver(selection->menu, REQ_DOWN_ITEM); if (E_OK != result && E_REQUEST_DENIED != result) { return result_ncurses_error(result); } } if (KEY_UP == ch) { int result = menu_driver(selection->menu, REQ_UP_ITEM); if (E_OK != result && E_REQUEST_DENIED != result) { return result_ncurses_error(result); } } if ('\r' == ch) break; if (!isalnum(ch)) continue; for (int i = 0; i < selection->items_count; ++i) { char const *name = item_name(selection->items[i]); if (name && ch == name[0]) { set_current_item(selection->menu, selection->items[i]); return result_success(); } } } return result_success(); }
/* this function make sure that the user selection is valid--not a category or white space */ void vwm_menu_marshall(MENU *menu,gint32 key_vector) { gchar *item_text; if(key_vector!=REQ_UP_ITEM && key_vector!=REQ_DOWN_ITEM) return; do { item_text=(gchar*)item_name(current_item(menu)); if(memcmp(item_text,"..",2)==0) break; menu_driver(menu,key_vector); } while(1); return; }
//Toggles between the options on the menu bar int ToggleMenu(WINDOW *my_menu_win, MENU *my_menu, int i){ int c, choice; choice = 0; while((c = wgetch(my_menu_win)) != KEY_F(6)){ switch(c){ case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); if (i < 5) i = i + 1; break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); if (i > 0) i = i - 1; break; case KEY_LEFT: menu_driver(my_menu, REQ_LEFT_ITEM); if (i > 0) i = i - 1; break; case KEY_RIGHT: menu_driver(my_menu, REQ_RIGHT_ITEM); if (i < 5) i = i + 1; break; case KEY_NPAGE: if (i < 5) i = i + 1; menu_driver(my_menu, REQ_SCR_DPAGE); break; case KEY_PPAGE: if (i > 0) i = i - 1; menu_driver(my_menu, REQ_SCR_UPAGE); break; case KEY_ENTER: case KEY_BREAK: case 10: case 13: choice = 1; break; } wrefresh(my_menu_win); if (choice) break; } return i; }
void viewNewPlayer(FootBallGame::TeamNumber teamNumber) { const char *itemName; const ITEM *currentItem; const int window_nlines = 10, window_ncols = 90; char *choices[] = { "Add", "Cancel", }; for (;;) { // Create new window. WINDOW *window = newwin(window_nlines, window_ncols, headerWindow_begin_y + headerWindow_nlines + 1, 0); // Set new window options. keypad(window, TRUE); // Create the form fields FIELD *field[6]; for (unsigned i = 0; i < ARRAY_SIZE(field)-1; i++) field[i] = new_field(1, 26, i+2, 23, 0, 0); field[ARRAY_SIZE(field)-1] = NULL; // Set feild options for (unsigned i = 0; i < ARRAY_SIZE(field)-1; i++) { set_field_back(field[i], A_UNDERLINE); field_opts_off(field[i], O_AUTOSKIP); } // Set the field types set_field_type(field[0], TYPE_REGEXP, "^[A-Za-z ]*$"); set_field_type(field[1], TYPE_INTEGER, 0, 1, 999); set_field_type(field[2], TYPE_REGEXP, "^[A-Za-z ]*$"); set_field_type(field[3], TYPE_NUMERIC, 2, 1.00, 1500.00); set_field_type(field[4], TYPE_NUMERIC, 2, 1.00, 300.00); // Create the form FORM *form = new_form(field); set_form_win(form, window); set_form_sub(form, derwin(window, 10, 90, 0, 0)); // Setup the menu items int nChoices = ARRAY_SIZE(choices)+1; ITEM **items = new ITEM* [nChoices]; for (int i = 0; i < nChoices-1; i++) items[i] = new_item(choices[i], NULL); items[nChoices-1] = NULL; // Create the menu MENU *menu = new_menu(items); // Menu options set_menu_format(menu, 1, 2); set_menu_mark(menu, NULL); // Attach the menu to the window set_menu_win(menu, window); set_menu_sub(menu, derwin(window, 1, 20, 8, 17)); // Make window and menu visible; post_form(form); post_menu(menu); mvwprintw(window, 0, 10, "Player Properties: "); mvwprintw(window, 2, 10, "Name: "); mvwprintw(window, 3, 10, "Number: "); mvwprintw(window, 4, 10, "Position: "); mvwprintw(window, 5, 10, "weight (kg): "); mvwprintw(window, 6, 10, "height (cm): "); wrefresh(window); refresh(); // Start user interaction loop. int c; // The restart variable is used to tell the function to rebuild the // menu by starting at the top of the for(;;) loop above. bool restart = false; while (!restart && (c = wgetch(window))) { mvprintw(LINES-1, 0, " "); refresh(); switch(c) { case KEY_DOWN: case 0x09: // Go to next field form_driver(form, REQ_NEXT_FIELD); // GO to the end of the presend buffer // Leaves nicely at the last character form_driver(form, REQ_END_LINE); break; case KEY_UP: // Go to previous field form_driver(form, REQ_PREV_FIELD); form_driver(form, REQ_END_LINE); break; case 0x7F: form_driver(form, REQ_DEL_PREV); break; case KEY_LEFT: menu_driver(menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(menu, REQ_RIGHT_ITEM); break; case 10: // When the user hits enter determine the currently selected // item and do nessary actions. currentItem = current_item(menu); itemName = item_name(currentItem); // Cancel if (strncmp(itemName, choices[1], strlen(choices[1])) == 0) { // Delete allocated data unpost_form(form); unpost_menu(menu); free_form(form); free_menu(menu); for (unsigned i = 0; i < ARRAY_SIZE(field)-1; i++) free_field(field[i]); for (int i = 0; i < nChoices; i++) free_item(items[i]); delete [] items; delwin(window); return; } else if (strncmp(itemName, choices[0], strlen(choices[0])) == 0) { form_driver(form, REQ_VALIDATION); bool invalid = false; for (unsigned i = 0; i < ARRAY_SIZE(field)-1; i++) if (field_status(field[i])==false) invalid = true;; if (invalid == false) { FootBallPlayer *player = new FootBallPlayer; player->setName(field_buffer(field[0], 0)); player->setNumber(atoi(field_buffer(field[1], 0))); player->setPosition(field_buffer(field[2], 0)); player->setWeight(strtof(field_buffer(field[3], 0), NULL)); player->setHeight(strtof(field_buffer(field[4], 0), NULL)); FootBallTeam *team = game.getFootBallTeam(teamNumber); if (team == NULL) { team = new FootBallTeam; game.setFootBallTeam(teamNumber, team); } team->addFootBallPlayer(player); // Delete allocated data unpost_form(form); unpost_menu(menu); free_form(form); free_menu(menu); for (unsigned i = 0; i < ARRAY_SIZE(field)-1; i++) free_field(field[i]); for (int i = 0; i < nChoices; i++) free_item(items[i]); delete [] items; delwin(window); return; } else { mvprintw(LINES-1, 0, "Fill out the form correctly!!"); refresh(); } } break; default: // If this is a normal character, it gets // printed form_driver(form, c); break; } } } }
void display_execution(int num_instruction, mot * tab_mot_instruction, int nb_instruction, int* registres,int nb_reg, int PC, int SP, int SR){ ITEM *item_en_cour = NULL; char ** tab_instruction; // for instructions ITEM **instructions_items; int c; WINDOW *instructions_win; MENU *instruction_menu; int i; instructions_items = (ITEM **)calloc(nb_instruction + 1, sizeof(ITEM *)); int menu_instruction_alrdy_dlt = 0; //pour ne pas supprimer le menu 2 fois --> évite les erreur de segmentation lorsqu'on quitte char ** files; //in case of F2 // for register char ** tab_register; ITEM **register_items; WINDOW *register_win; MENU *register_menu; register_items = (ITEM **)calloc(nb_reg + 1 +3, sizeof(ITEM *)); //+3 pour PC SP et SR int menu_register_alrdy_dlt = 0; //pour ne pas supprimer le menu 2 fois --> évite les erreur de segmentation lorsqu'on quitte char dest_string[5]; char source_string[5]; char brut_string[10]; char pc_string[6], sp_string[6], sr_string[6]; int is_brut = 0; //si le mot précedent contient un mode direct, alors le mot suivant est un brut //si DIRIMM les 2 suivant sont des brut //allocation de mémoire pour les insctruction sous forme de chaine tab_instruction = (char**) malloc (nb_instruction* sizeof(char*)); //allocation de mémoire pour le tabeau de registre sous forme de string tab_register = (char**) malloc (nb_reg* sizeof(char *)); int num_choix; char * choix = NULL; for(i = 0; i < nb_instruction; ++i){ tab_instruction[i] = malloc(50 * sizeof(char)); //plus simple pour le moment, une instruction ne peut dépasser 50caractères ... a améliorer si assez de temps if(is_brut == 0){ sprintf(dest_string, "%d", tab_mot_instruction[i].codage.dest); sprintf(source_string, "%d", tab_mot_instruction[i].codage.source); //si l'instruction contient une source ou destination en mode immediat ou direct, la prochaine instruction sera un brut if(is_brut==2 || tab_mot_instruction[i].codage.mode == REGIMM || tab_mot_instruction[i].codage.mode == INDIMM || tab_mot_instruction[i].codage.mode == REGDIR || tab_mot_instruction[i].codage.mode == DIRREG){ is_brut = 1; } if(tab_mot_instruction[i].codage.mode == DIRIMM){ is_brut = 2; } strcpy(tab_instruction[i], codeop_tostring(tab_mot_instruction[i].codage.codeop) ); //on met tout les élément sous forme de string concaténé et espacé strcat(tab_instruction[i], " "); strcat(tab_instruction[i], mode_tostring(tab_mot_instruction[i].codage.mode)); strcat(tab_instruction[i], " "); strcat(tab_instruction[i], dest_string); strcat(tab_instruction[i], " "); strcat(tab_instruction[i], source_string); } else{ sprintf(brut_string, "%d", tab_mot_instruction[i].brut); strcpy(tab_instruction[i], brut_string ); if(is_brut == 2){ is_brut = 1; // cas du DIRIMM } else{ is_brut = 0; } } //mvprintw(i+2, 0, "%s", tab_instruction[i]); instructions_items[i] = new_item(tab_instruction[i], ""); //ajoute les éléments dans mon tableau d'item if(i == num_instruction){ //on sauvegarde l'adresse de l'item que l'on est entrain de traiter (celui donner par num_instruction) item_en_cour = instructions_items[i]; //n le desactive, cela permet de lui doonner une autre apparence pur le repérer item_opts_off(instructions_items[i], O_SELECTABLE); } } for(i = 0; i < nb_reg; ++i){ //contient le registre sous forme de string par exemple R1 ou PC tab_register[i] = malloc(3 * sizeof(char)); sprintf(tab_register[i], "R%i: %d",i, registres[i]); //mvprintw(i+2, 0, "%s", tab_instruction[i]); register_items[i] = new_item(tab_register[i], ""); //ajoute les éléments dans mon tableau d'item } sprintf(pc_string, "%d", PC); sprintf(sp_string, "%d", SP); sprintf(sr_string, "%d", SR); register_items[nb_reg] = new_item("PC:", pc_string); //register_items[8] register_items[nb_reg+1] = new_item("SP:", sp_string); //register_items[9] register_items[nb_reg+2] = new_item("SR:", sr_string); //register_items[10] //pour cacher la selection du premier registre item_opts_off(register_items[0], O_SELECTABLE); instruction_menu = new_menu((ITEM **)instructions_items); //creer un menu contenant les instructions register_menu = new_menu((ITEM **)register_items); //creer un menu contenant les registres mvprintw(LINES - 2, 0, "F9 to close the menu"); instructions_win = newwin((LINES-4)/2, 40 , 3, (COLS/2)- (COLS-4)/4); //créer une nouvelle fenetre pour les instructions register_win = newwin(16, 20 , 3, (COLS/2) + 10); //créer une nouvelle fenetre pour les registres keypad(instructions_win, TRUE); //active le clavier sur les instructions /* Set main window and sub window */ set_menu_win(instruction_menu, instructions_win); //set main menu set_menu_sub(instruction_menu, derwin(instructions_win, ((LINES-4)/2)-4, 38, 3, 1)); // set sub window set_menu_format(instruction_menu, ((LINES-4)/2)-4, 1); set_menu_win(register_menu, register_win); //set main menu set_menu_sub(register_menu, derwin(register_win, 13, 18, 3, 1)); // set sub window set_menu_format(register_menu, 13, 1); /* Set menu mark to the string " * " */ set_menu_mark(instruction_menu, " * "); set_menu_mark(register_menu, ""); /* Print a border around the main window and print a title */ box(instructions_win, 0, 0); print_in_middle(instructions_win, 1, 0, 40, "liste des instructions", COLOR_PAIR(1)); mvwaddch(instructions_win, 2, 0, ACS_LTEE); mvwhline(instructions_win, 2, 1, ACS_HLINE, 43); mvwaddch(instructions_win, 2, 39, ACS_RTEE); box(register_win, 0, 0); print_in_middle(register_win, 1, 0, 20, "Registres", COLOR_PAIR(1)); mvwaddch(register_win, 2, 0, ACS_LTEE); mvwhline(register_win, 2, 1, ACS_HLINE, 22); mvwaddch(register_win, 2, 19, ACS_RTEE); refresh(); post_menu(instruction_menu); post_menu(register_menu); //on se place sur l'instruction en cour set_current_item (instruction_menu, item_en_cour); wrefresh(instructions_win); wrefresh(register_win); while((c = getch()) != KEY_F(9) && c != 32) { switch(c) { case KEY_F(5): mvprintw(LINES-2, 0, "Exiting..."); endwin(); /* End curses mode */ exit(0); case KEY_F(2): files = list_file("", &i); draw_menu(files, execute_file_menu, "", i); case KEY_DOWN: menu_driver(instruction_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(instruction_menu, REQ_UP_ITEM); break; //case KEY_NPAGE: // menu_driver(my_menu, REQ_SCR_DPAGE); // break; //case KEY_PPAGE: // menu_driver(my_menu, REQ_SCR_UPAGE); // break; case 10: move(20, 0); clrtoeol(); break; } wrefresh(instructions_win); } if(menu_instruction_alrdy_dlt == 0){ clean_menu(instruction_menu); clean_window(instructions_win); } if(menu_register_alrdy_dlt == 0){ clean_menu(register_menu); clean_window(register_win); } }
void draw_menu(char ** menu_liste, void (*ptrfonction)(int,const char *, char *), char * folder, int taille_menu){ ITEM **my_items; int c; WINDOW *my_menu_win; MENU *my_menu; int i; ITEM *cur_item; my_items = (ITEM **)calloc(taille_menu + 1, sizeof(ITEM *)); int menu_alrdy_dlt = 0; //pour ne pas supprimer le menu 2 fois --> évite les erreur de segmentation lorsqu'on quitte char ** files; //pour le cas ou on utilise F2 (ouvrir un fichier) int num_choix; char choix[FILE_MAX]; for(i = 0; i < taille_menu; ++i){ my_items[i] = new_item(menu_liste[i], ""); //ajoute les éléments dans mon tableau d'item } //my_items[taille_menu+1] = (ITEM *)NULL; //ajoute un item vide à la fin du tableau my_menu = new_menu((ITEM **)my_items); //creer un menu contenan les items mvprintw(LINES - 2, 0, "F9 to close the menu"); //affiche un pied de page pour fermer le menu my_menu_win = newwin(10, 45, (LINES-10)/2, (COLS-45)/2); //créer une nouvelle fenetre keypad(my_menu_win, TRUE); //active le clavier pour le menu /* Set main window and sub window */ set_menu_win(my_menu, my_menu_win); //set main menu set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); // set sub window set_menu_format(my_menu, 5, 1); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Menu Principal", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); refresh(); post_menu(my_menu); wrefresh(my_menu_win); while((c = getch()) != KEY_F(9) && c != 27) { switch(c) { case KEY_F(5): mvprintw(LINES-2, 0, "Exiting..."); endwin(); /* End curses mode */ exit(0); case KEY_F(2): files = list_file("", &i); clean_menu(my_menu); clean_window(my_menu_win); draw_menu(files, execute_file_menu, "", i); return; case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case KEY_NPAGE: menu_driver(my_menu, REQ_SCR_DPAGE); break; case KEY_PPAGE: menu_driver(my_menu, REQ_SCR_UPAGE); break; case 10: move(20, 0); clrtoeol(); num_choix = item_index(current_item(my_menu)); strcpy(choix, item_name(current_item(my_menu))); //mvprintw(5, 0, "Item selected is : %s", item_name(current_item(my_menu))); clean_menu(my_menu); menu_alrdy_dlt = 1; clean_window(my_menu_win); (*ptrfonction)(num_choix, choix, folder); pos_menu_cursor(my_menu); break; } wrefresh(my_menu_win); } if(menu_alrdy_dlt == 0) clean_menu(my_menu); clean_window(my_menu_win); }
int btn_dialog(WINDOW *main_window, const char *msg, int btn_num, ...) { va_list ap; char *btn; int btns_width = 0; int msg_lines = 0; int msg_width = 0; int total_width; int win_rows = 0; WINDOW *win; WINDOW *msg_win; WINDOW *menu_win; MENU *menu; ITEM *btns[btn_num+1]; int i, x, y; int res = -1; va_start(ap, btn_num); for (i = 0; i < btn_num; i++) { btn = va_arg(ap, char *); btns[i] = new_item(btn, ""); btns_width += strlen(btn)+1; } va_end(ap); btns[btn_num] = NULL; /* find the widest line of msg: */ msg_lines = get_line_no(msg); for (i = 0; i < msg_lines; i++) { const char *line = get_line(msg, i); int len = get_line_length(line); if (msg_width < len) msg_width = len; } total_width = max(msg_width, btns_width); /* place dialog in middle of screen */ y = (LINES-(msg_lines+4))/2; x = (COLS-(total_width+4))/2; /* create the windows */ if (btn_num > 0) win_rows = msg_lines+4; else win_rows = msg_lines+2; win = newwin(win_rows, total_width+4, y, x); keypad(win, TRUE); menu_win = derwin(win, 1, btns_width, win_rows-2, 1+(total_width+2-btns_width)/2); menu = new_menu(btns); msg_win = derwin(win, win_rows-2, msg_width, 1, 1+(total_width+2-msg_width)/2); set_menu_fore(menu, attributes[DIALOG_MENU_FORE]); set_menu_back(menu, attributes[DIALOG_MENU_BACK]); wattrset(win, attributes[DIALOG_BOX]); box(win, 0, 0); /* print message */ wattrset(msg_win, attributes[DIALOG_TEXT]); fill_window(msg_win, msg); set_menu_win(menu, win); set_menu_sub(menu, menu_win); set_menu_format(menu, 1, btn_num); menu_opts_off(menu, O_SHOWDESC); menu_opts_off(menu, O_SHOWMATCH); menu_opts_on(menu, O_ONEVALUE); menu_opts_on(menu, O_NONCYCLIC); set_menu_mark(menu, ""); post_menu(menu); touchwin(win); refresh_all_windows(main_window); while ((res = wgetch(win))) { switch (res) { case KEY_LEFT: menu_driver(menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(menu, REQ_RIGHT_ITEM); break; case 10: /* ENTER */ case 27: /* ESCAPE */ case ' ': case KEY_F(F_BACK): case KEY_F(F_EXIT): break; } touchwin(win); refresh_all_windows(main_window); if (res == 10 || res == ' ') { res = item_index(current_item(menu)); break; } else if (res == 27 || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { res = KEY_EXIT; break; } } unpost_menu(menu); free_menu(menu); for (i = 0; i < btn_num; i++) free_item(btns[i]); delwin(win); return res; }
int main_giris() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; WINDOW *my_menu_win; char *anamenu[] = { _("Market ") , _("Current Module"), _("Cheque Module"), _("Stock Module"), _("Reports"), _("Configuration"), _("Help"), _("About"), _("Exit"), (char *)NULL, }; (int) signal (SIGINT, sonlandir); (int) signal (SIGILL, sonlandir); (int) signal (SIGTERM, sonlandir); //backup control yedek_kontrol_et(); initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); //sql configuration ayar_dosyasi_oku (); ana_win = newwin(LINES, 80, 0, 0); temizle_win=newwin(LINES, 80, 0, 0); init_pair(1, COLOR_WHITE, COLOR_RED); init_pair(2, COLOR_WHITE, COLOR_BLUE); terminal_kontrol_et(); if (irsaliye_gun_kontrol()) mesaj( _("Some bills not prepared. Don't forget.!") ); kullanici_onayla(); init_pair(1, COLOR_WHITE, COLOR_RED); init_pair(2, COLOR_WHITE, COLOR_BLUE); baslik_goruntule(); n_choices = ARRAY_SIZE(anamenu); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) my_items[i] = new_item(anamenu[i], " "); my_menu = new_menu((ITEM **)my_items); my_menu_win = newwin(15, 50, 5, 10); keypad(my_menu_win, TRUE); set_menu_win(my_menu, my_menu_win); set_menu_sub(my_menu, derwin(my_menu_win, 10, 40, 4, 2)); set_menu_mark(my_menu, mark); box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Options", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 48); mvwaddch(my_menu_win, 2, 49, ACS_RTEE); post_menu(my_menu); wrefresh(my_menu_win); while((c = wgetch(my_menu_win)) ) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case 10: cur_item = current_item(my_menu); switch(item_index(cur_item) + 1) { case 1: /* Kasa satis */ //satis if ( haklari_kontrol_et(0)==1 ) market_ana_ekran(); else {mesaj(_("Access denied.!!!"));} baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 2: /*cari*/ //stok giris kontrolu if ( haklari_kontrol_et(0)==1 ) cari(); else {mesaj(_("Access denied.!!!"));} baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 3: /*ceksenet*/ //cari giris kontrolu if ( haklari_kontrol_et(1)==1 ) ceksenet(); else {mesaj(_("Access denied.!!!"));} baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 4: /*stok*/ //cari giris kontrolu if ( haklari_kontrol_et(1)==1 ) stok(); else {mesaj(_("Access denied.!!!"));} baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 5: /*raporlar*/ //rapor giris kontrolu if ( haklari_kontrol_et(2)==1 ) raporlar(); else {mesaj(_("Access denied.!!!"));} baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 6: /*ayarlar*/ ayarlar(); baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 7:/*yardým*/ yardim(); baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 8:/*hakkýnda*/ hakkinda(); baslik_goruntule(); touchwin(my_menu_win); wrefresh(my_menu_win); baslik_yaz(); refresh(); break; case 9:/*Kapat*/ donen_deger=onayla(cikis_onay); if (donen_deger==1) { // donen deger evet ise kapat beep(); touchwin(ana_win); wrefresh(ana_win); unpost_menu(my_menu); free_menu(my_menu); for(i = 0; i < n_choices; ++i) free_item(my_items[i]); endwin(); return; } touchwin(ana_win); wrefresh(ana_win); touchwin(my_menu_win); wrefresh(my_menu_win); break; } wrefresh(my_menu_win); pos_menu_cursor(my_menu); } wrefresh(my_menu_win); } }
/* * This is called by main.c ncurses_action everytime a popup exists. * It's used to handle characters input in forms and button pressing. */ void popup_driver(int ch) { switch (ch) { case KEY_DOWN: if (is_on_button || !popup_form) break; if (popup_form->current == popup_fields[popup_form->maxfield-1]) switch_to_buttons(); else form_driver(popup_form, REQ_NEXT_FIELD); break; case KEY_UP: if (is_on_button) { if (!popup_form) break; is_on_button = false; set_menu_fore(popup_menu, A_NORMAL); // "hide" the button } else form_driver(popup_form, REQ_PREV_FIELD); break; case KEY_LEFT: if (is_on_button) menu_driver(popup_menu, REQ_LEFT_ITEM); else form_driver(popup_form, REQ_PREV_CHAR); break; case KEY_RIGHT: if (is_on_button) menu_driver(popup_menu, REQ_RIGHT_ITEM); else form_driver(popup_form, REQ_NEXT_CHAR); break; case 10: if (is_on_button) driver_buttons(current_item(popup_menu)); else switch_to_buttons(); break; // Delete the char before cursor case KEY_BACKSPACE: case 127: if (!is_on_button) form_driver(popup_form, REQ_DEL_PREV); break; // Delete the char under the cursor case KEY_DC: if (!is_on_button) form_driver(popup_form, REQ_DEL_CHAR); break; default: if (!is_on_button) form_driver(popup_form, ch); break; } if (popup_menu) { if (is_on_button) pos_menu_cursor(popup_menu); else pos_form_cursor(popup_form); } wrefresh(win_body); }
/* returns 1 if yes, 0 if no */ int confirm(const char *title, const char *text, chtype forecolor, chtype backcolor, int def) { int retval = 0; ITEM **menu_items; MENU *confirm_menu; PANEL *my_panels[1]; WINDOW *confirm_win, *dw; ITEM *cur; int height = 7, width = 25, startx = 5, starty = 5, max_x = 0, max_y = 0; const char *choices[] = {STR_YES, STR_NO}; size_t n_choices = 2, i = 0; int ch, quit = 0; char *print_title; /* safety */ vrmr_fatal_if_null(title); vrmr_fatal_if_null(text); if (width - 4 < (int)StrLen(text)) width = (int)StrLen(text) + 4; if (width - 6 < (int)StrLen(title)) width = (int)StrLen(title) + 6; getmaxyx(stdscr, max_y, max_x); startx = (max_x - width) / 2; starty = (max_y - height) / 2; print_title = malloc(StrMemLen(title) + 3); vrmr_fatal_alloc("malloc", print_title); snprintf(print_title, StrMemLen(title) + 3, " %s ", title); menu_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); vrmr_fatal_alloc("calloc", menu_items); for (i = 0; i < n_choices; ++i) { menu_items[i] = new_item(choices[i], NULL); } menu_items[n_choices] = (ITEM *)NULL; confirm_menu = new_menu((ITEM **)menu_items); vrmr_fatal_if_null(confirm_menu); confirm_win = newwin(height, width, starty, startx); wbkgd(confirm_win, backcolor); keypad(confirm_win, TRUE); wrefresh(confirm_win); my_panels[0] = new_panel(confirm_win); set_menu_win(confirm_menu, confirm_win); dw = derwin(confirm_win, height - 4, 10, 4, (width) / 2 - 5); set_menu_sub(confirm_menu, dw); set_menu_format(confirm_menu, height - 4, 2); box(confirm_win, 0, 0); print_in_middle(confirm_win, 0, 0, width, print_title, backcolor); print_in_middle(confirm_win, 2, 0, width, text, backcolor); set_menu_back(confirm_menu, backcolor); set_menu_fore(confirm_menu, forecolor); post_menu(confirm_menu); /* set the cursor to the 'no' position */ if (!def) { menu_driver(confirm_menu, REQ_RIGHT_ITEM); } update_panels(); doupdate(); while (quit == 0) { ch = wgetch(confirm_win); switch (ch) { case KEY_DOWN: menu_driver(confirm_menu, REQ_LEFT_ITEM); break; case KEY_UP: menu_driver(confirm_menu, REQ_RIGHT_ITEM); break; case KEY_LEFT: menu_driver(confirm_menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(confirm_menu, REQ_RIGHT_ITEM); break; case 10: // enter { cur = current_item(confirm_menu); vrmr_fatal_if_null(cur); if (strcmp((char *)item_name(cur), STR_YES) == 0) { retval = 1; } quit = 1; break; } case 'y': case 'Y': retval = 1; quit = 1; break; case 'n': case 'N': retval = 0; quit = 1; break; case 27: case KEY_F(10): case 'q': case 'Q': quit = 1; break; } } unpost_menu(confirm_menu); free_menu(confirm_menu); for (i = 0; i < n_choices; ++i) free_item(menu_items[i]); free(menu_items); destroy_win(dw); del_panel(my_panels[0]); destroy_win(confirm_win); free(print_title); update_panels(); doupdate(); return (retval); }
void Menu(int *value) { ITEM **my_items; int c; MENU *my_menu; WINDOW *my_menu_win; int n_choices, i; int output = 3; // Create Menu items n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) my_items[i] = new_item(choices[i], ""); // Crate Menu my_menu = new_menu((ITEM **)my_items); // Create the window to be associated with the menu my_menu_win = newwin(15, 50, 4, 4); keypad(my_menu_win, TRUE); // Enable keyboard on that window // Set main window and sub window set_menu_win(my_menu, my_menu_win); set_menu_sub(my_menu, derwin(my_menu_win, 6, 38, 3, 1)); // Set menu mark to the string " * " set_menu_mark(my_menu, " * "); // Print a border around the main window and print a title box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 50, "THE GAME", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 48); mvwaddch(my_menu_win, 2, 39, ACS_RTEE); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); // Refres the window bool done = false; while(!done && (c = wgetch(my_menu_win)) != 'q' ) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case 10: // ENTER was pressed if(item_index(current_item(my_menu)) == 0) { // First menu item was selected (Play) output = 1; done = true; } if(item_index(current_item(my_menu)) == 1) { // Second menu item was selected (Help)) output = 2; done = true; } if(item_index(current_item(my_menu)) == 2) { // Third menu item was selected (Exit) output = 3; done = true; } break; } wrefresh(my_menu_win); // Refres the window } // Unpost and free all the memory taken up unpost_menu(my_menu); free_menu(my_menu); for(i = 0; i < n_choices; ++i) free_item(my_items[i]); endwin(); *value = output; }
int main() { ITEM **my_items; int c; MENU *my_menu; WINDOW *my_menu_win; int n_choices, i; /* Initialize curses */ initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK); /* Create items */ n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) my_items[i] = new_item(choices[i], choices[i]); /* Crate menu */ my_menu = new_menu((ITEM **)my_items); /* Set menu option not to show the description */ menu_opts_off(my_menu, O_SHOWDESC); /* Create the window to be associated with the menu */ my_menu_win = newwin(10, 40, 4, 4); keypad(my_menu_win, TRUE); /* Set main window and sub window */ set_menu_win(my_menu, my_menu_win); set_menu_sub(my_menu, derwin(my_menu_win, 6, 30, 3, 1)); set_menu_format(my_menu, 5, 3); set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); attron(COLOR_PAIR(2)); mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll"); mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)"); attroff(COLOR_PAIR(2)); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); while((c = wgetch(my_menu_win)) != KEY_F(1)) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case KEY_LEFT: menu_driver(my_menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(my_menu, REQ_RIGHT_ITEM); break; case KEY_NPAGE: menu_driver(my_menu, REQ_SCR_DPAGE); break; case KEY_PPAGE: menu_driver(my_menu, REQ_SCR_UPAGE); break; } wrefresh(my_menu_win); } /* Unpost and free all the memory taken up */ unpost_menu(my_menu); free_menu(my_menu); for(i = 0; i < n_choices; ++i) free_item(my_items[i]); endwin(); }
int main_menu(void) { MENU *my_menu = NULL; ITEM **my_items = NULL; struct tool_instance *list = NULL; const char *ret_msg = NULL; // Init at refresh for first loop. int c = 'R'; do { switch (c) { // Navigate in the menu case KEY_DOWN: case KEY_RIGHT: menu_driver(my_menu, REQ_DOWN_ITEM); break ; case KEY_UP: case KEY_LEFT: menu_driver(my_menu, REQ_UP_ITEM); break ; // Select a migration to monitor case ' ': case 13: // Enter key { if (item_count(my_menu) > 0) { ITEM *curitem = current_item(my_menu); if (view_instance(item_description(curitem)) != EXIT_SUCCESS) { ret_msg = "An error occured while trying to display the tool's data."; } } } // No break here to allow refreshing the list // when coming out of the instance display. // Refresh the list. case 'r': case 'R': if (list) clear_instance_list(list); list = get_instance_list(); if (list == NULL) mvprintw(0, 0, "cloudmig-view: No cloudmig tool is running at the moment.\n"); if (my_menu) clear_menu(my_menu, my_items); my_items = NULL; my_menu = NULL; if (fill_menu(list, &my_items, &my_menu)) return EXIT_FAILURE; post_menu(my_menu); break; // Leave the monitor case 'q': case 'Q': break ; // Ignore the others default: break ; } mvprintw(LINES - 4, 0, "Use <SPACE> or <ENTER> to select the process to monitor."); mvprintw(LINES - 3, 0, " <q> or <Q> to quit this program."); mvprintw(LINES - 2, 0, " <r> or <R> to see refresh the list manually."); if (ret_msg) { mvprintw(LINES -6, 0, "%.*s", COLS, ret_msg); ret_msg = NULL; } refresh(); } while ((c = getch()) != 'q'); return EXIT_FAILURE; }
void viewEditTeamProperties(FootBallGame::TeamNumber teamNumber) { const char *fieldBuffer; const ITEM *currentItem; const char *itemName; ITEM **items; const int window_nlines = 10, window_ncols = 90; char *choices[] = { "Ok", "Cancel", }; for(;;) { // Create new window. WINDOW *window = newwin(window_nlines, window_ncols, headerWindow_begin_y + headerWindow_nlines + 1, 0); // Set new window options. keypad(window, TRUE); // Team the team FootBallTeam *team = game.getFootBallTeam(teamNumber); // Create the form fields FIELD *field[2]; field[0] = new_field(1, 20, 2, 22, 0, 0); field[1] = NULL; // Set feild options set_field_back(field[0], A_UNDERLINE); field_opts_off(field[0], O_AUTOSKIP); set_field_type(field[0], TYPE_REGEXP, "^[A-Za-z ]*$"); if (team) { String teamName = team->getTeamName(); set_field_buffer(field[0], 0, teamName.c_str()); } // Create the form FORM *form = new_form(field); // Attach the form to the window set_form_win(form, window); set_form_sub(form, derwin(window, 4, 90, 0, 0)); // Setup the menu int nChoices = ARRAY_SIZE(choices)+1; items = new ITEM* [nChoices]; for (int i = 0; i < nChoices-1; i++) items[i] = new_item(choices[i], NULL); items[nChoices-1] = NULL; // Create the menu MENU *menu = new_menu(items); // Menu options set_menu_format(menu, 1, 2); set_menu_mark(menu, NULL); // Attach the menu to the window set_menu_win(menu, window); set_menu_sub(menu, derwin(window, 1, 20, 4, 17)); // Make window and menu visible; post_form(form); post_menu(menu); mvwprintw(window, 0, 10, "Team Properties: "); mvwprintw(window, 2, 10, "Team Name: "); wrefresh(window); refresh(); // Start user interaction loop. int c; // The restart variable is used to tell the function to rebuild the // menu by starting at the top of the for(;;) loop above. bool restart = false; while (!restart && (c = wgetch(window))) { switch(c) { case KEY_DOWN: case 0x09: // Go to next field form_driver(form, REQ_NEXT_FIELD); // GO to the end of the presend buffer // Leaves nicely at the last character form_driver(form, REQ_END_LINE); break; case KEY_UP: // Go to previous field form_driver(form, REQ_PREV_FIELD); form_driver(form, REQ_END_LINE); break; case 0x7F: form_driver(form, REQ_DEL_PREV); break; case KEY_LEFT: menu_driver(menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: menu_driver(menu, REQ_RIGHT_ITEM); break; case 10: // Enter // When the user hits enter determine the currently selected // item and do nessary actions. currentItem = current_item(menu); itemName = item_name(currentItem); if (strcmp(itemName, choices[0]) == 0) { form_driver(form, REQ_VALIDATION); fieldBuffer = field_buffer(field[0], 0); if (fieldBuffer != NULL && strcmp(fieldBuffer, "") != 0) { if (team == NULL) { team = new FootBallTeam; game.setFootBallTeam(teamNumber, team); } team->setTeamName(fieldBuffer); } } // Delete allocated data delwin(window); unpost_form(form); free_form(form); unpost_menu(menu); free_menu(menu); for (unsigned i = 0; i < ARRAY_SIZE(field); i++) free_field(field[i]); for (int i = 0; i < nChoices-1; i++) free_item(items[i]); delete[] items; return; default: // If this is a normal character, it gets printed. form_driver(form, c); break; } } } }