MENU* menu_create(const char *choices[], const char *desc[], int nchoices) { MENU *menu; ITEM **items; int i; items=(ITEM **)calloc(5+1, sizeof(ITEM *)); if(!items) { set_error(ERR_NOMEM); lpr_error("create_menu"); } for(i=0; i<5; ++i) { items[i]=new_item(choices[i], desc[i]); if(choices[i]==SEP) item_opts_off(items[i], O_SELECTABLE); } items[5] = (ITEM *)NULL; menu=new_menu((ITEM **)items); return menu; }
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); } }
static VALUE rbncurs_c_item_opts_off(VALUE rb_item, VALUE opts) { ITEM *item = get_item(rb_item); return INT2NUM(item_opts_off(item, NUM2INT(opts))); }
/* * create the internal menu for the files */ static void wdg_file_menu_create(struct wdg_object *wo) { WDG_WO_EXT(struct wdg_file_handle, ww); int mrows, mcols; int i; size_t c = wdg_get_ncols(wo); size_t x = wdg_get_begin_x(wo); size_t y = wdg_get_begin_y(wo); struct stat buf; /* the menu is already posted */ if (ww->nitems) return; WDG_DEBUG_MSG("wdg_file_menu_create"); /* get the working directory */ getcwd(ww->curpath, PATH_MAX); /* scan the directory */ ww->nlist = scandir(".", &ww->namelist, 0, alphasort); /* on error display the message in the box */ if (ww->nlist <= 0) { ww->nitems = 2; WDG_SAFE_REALLOC(ww->items, ww->nitems * sizeof(ITEM *)); ww->items[ww->nitems - 2] = new_item("/", "root"); ww->items[ww->nitems - 1] = new_item("Cannot open the directory", ""); item_opts_off(ww->items[ww->nitems - 1], O_SELECTABLE); } else { /* for each directory in the directory */ for (i = 0; i < ww->nlist; i++) { /* * transform the current dir into the root. * useful to exit from a path whose parent is not readable */ if (!strcmp(ww->namelist[i]->d_name, ".")) { strncpy(ww->namelist[i]->d_name, "/", 1); ww->nitems++; WDG_SAFE_REALLOC(ww->items, ww->nitems * sizeof(ITEM *)); ww->items[ww->nitems - 1] = new_item(ww->namelist[i]->d_name, "root"); continue; } /* get the file properties */ stat(ww->namelist[i]->d_name, &buf); if (S_ISDIR(buf.st_mode)) { ww->nitems++; WDG_SAFE_REALLOC(ww->items, ww->nitems * sizeof(ITEM *)); ww->items[ww->nitems - 1] = new_item(ww->namelist[i]->d_name, "[...]"); } // if not readable //item_opts_off(ww->items[ww->nitems - 1], O_SELECTABLE); } /* and now add the files */ for (i = 0; i < ww->nlist; i++) { /* get the file properties */ stat(ww->namelist[i]->d_name, &buf); if (!S_ISDIR(buf.st_mode)) { ww->nitems++; WDG_SAFE_REALLOC(ww->items, ww->nitems * sizeof(ITEM *)); ww->items[ww->nitems - 1] = new_item(ww->namelist[i]->d_name, ""); } } } /* null terminate the array */ WDG_SAFE_REALLOC(ww->items, (ww->nitems + 1) * sizeof(ITEM *)); ww->items[ww->nitems] = NULL; /* create the menu */ ww->m = new_menu(ww->items); /* set the dimensions */ set_menu_format(ww->m, ww->y - 2, 1); set_menu_spacing(ww->m, 2, 0, 0); /* get the geometry to make a window */ scale_menu(ww->m, &mrows, &mcols); /* * if the menu is larger than the main window * adapt to the new dimensions */ if (mcols > (int)c - 4) { ww->x = mcols + 4; wdg_file_redraw(wo); return; } /* create the window for the menu */ ww->mwin = newwin(mrows, MAX(mcols, (int)c - 4), y + 1, x + 2); /* set the color */ wbkgd(ww->mwin, COLOR_PAIR(wo->window_color)); keypad(ww->mwin, TRUE); /* associate with the menu */ set_menu_win(ww->m, ww->mwin); /* the subwin for the menu */ set_menu_sub(ww->m, derwin(ww->mwin, mrows + 1, mcols, 1, 1)); /* menu attributes */ set_menu_mark(ww->m, ""); set_menu_grey(ww->m, COLOR_PAIR(wo->window_color)); set_menu_back(ww->m, COLOR_PAIR(wo->window_color)); set_menu_fore(ww->m, COLOR_PAIR(wo->window_color) | A_REVERSE | A_BOLD); /* display the menu */ post_menu(ww->m); wnoutrefresh(ww->mwin); }
int main() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; /* Initialize curses */ initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_MAGENTA, COLOR_BLACK); /* Initialize items */ 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; item_opts_off(my_items[3], O_SELECTABLE); item_opts_off(my_items[6], O_SELECTABLE); /* Create menu */ my_menu = new_menu((ITEM **)my_items); /* Set fore ground and back ground of the menu */ set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE); set_menu_back(my_menu, COLOR_PAIR(2)); set_menu_grey(my_menu, COLOR_PAIR(3)); /* Post the menu */ mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (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; case 10: /* Enter */ move(20, 0); clrtoeol(); mvprintw(20, 0, "Item selected is : %s", item_name(current_item(my_menu))); pos_menu_cursor(my_menu); break; } } unpost_menu(my_menu); for(i = 0; i < n_choices; ++i) free_item(my_items[i]); free_menu(my_menu); endwin(); }