int main() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; /* Initialize curses */ initscr(); cbreak(); noecho(); keypad(stdscr, TRUE); /* 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; my_menu = new_menu((ITEM **)my_items); /* Make the menu multi valued */ menu_opts_off(my_menu, O_ONEVALUE); mvprintw(LINES - 3, 0, "Use <SPACE> to select or unselect an item."); mvprintw(LINES - 2, 0, "<ENTER> to see presently selected items(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 ' ': menu_driver(my_menu, REQ_TOGGLE_ITEM); break; case 10: /* Enter */ { char temp[200]; ITEM **items; items = menu_items(my_menu); temp[0] = '\0'; for(i = 0; i < item_count(my_menu); ++i) if(item_value(items[i]) == TRUE) { strcat(temp, item_name(items[i])); strcat(temp, " "); } move(20, 0); clrtoeol(); mvprintw(20, 0, temp); refresh(); } break; } } free_item(my_items[0]); free_item(my_items[1]); free_menu(my_menu); endwin(); }
/* get the message, and buttons. * each button must be a char* * return the selected button * * this dialog is used for 2 different things: * 1) show a text box, no buttons. * 2) show a dialog, with horizontal buttons */ 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]); (void) wattrset(win, attributes[DIALOG_BOX]); box(win, 0, 0); /* print message */ (void) 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(){ WINDOW *my_win, *menu_win, *my_menu_win; ITEM **my_items; MENU *my_menu; list *FileBuffer; int height, width, startx, starty, exit = 0; int highlight = 1; int ch, c, choice = 0, i, j; char str[81]; FileBuffer = (list *)malloc(sizeof(list)); if (FileBuffer == NULL) return; InitialiseBuffer(FileBuffer); initscr(); clear(); noecho(); cbreak(); start_color(); /*Checking whether the terminal supports colors*/ if (has_colors() == FALSE){ endwin(); printf("Your terminal does not support colors\n"); } keypad(stdscr, TRUE); height = 3; width = 10; starty = (LINES - height)/2; startx = (COLS - width) / 2; refresh(); my_win = Create_NewWindow(height, width, starty, startx); mvwhline(my_win, 5, 1, ACS_HLINE, width - 1); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK); /* Create items */ my_items = (ITEM **)calloc(nchoices, sizeof(ITEM *)); for(i = 0; i < nchoices; ++i) my_items[i] = new_item(menu_options[i], menu_options[i]); /* Create 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(0, 0, 0, 0); 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, 0, 0, 0, 0)); set_menu_format(my_menu, 1, 6); set_menu_mark(my_menu, " * "); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); i = 0; mvwhline(my_menu_win, 1, 0, ACS_HLINE, COLS); mvwprintw(my_menu_win, LINES - 1, 0, "Press F3 to go to the menu, F6 to exit", c); while(1){ choice = ToggleMenu(my_menu_win, my_menu, i); i = choice; switch(choice){ case 0: MenuOpen(FileBuffer, my_menu_win); break; case 1: MenuNew(FileBuffer, my_menu_win, my_menu); break; case 2: MenuSave(FileBuffer, my_menu_win); break; case 3: MenuSaveAs(FileBuffer, my_menu_win); break; case 4: MenuEdit(FileBuffer, my_menu_win); break; case 5: MenuExit(FileBuffer, my_menu_win); exit = 1; break; default: break; } if (exit) break; } /*Assertion: the user wants to exit the program*/ /* Unpost and free all the memory taken up */ unpost_menu(my_menu); free_menu(my_menu); for(j = 0; j < nchoices; ++j) free_item(my_items[j]); clrtoeol(); refresh(); /*Ending curses mode*/ endwin(); return 0; }
// // Affiche le tableau des high-scores dans une fenêtre modale. // void displayLeaderboard() { int i, c; const int menuWidth = 10; const int winHeight = MAX_SAVED_SCORES_COUNT + 6; const int winWidth = POPUP_WINDOW_WIDTH + 20; const int colRank = 2; const int colScore = 20; const int colName = 35; const int colBoard = 55; ITEM* menuItems[2]; MENU* menu; Score scores[MAX_SAVED_SCORES_COUNT]; int n = loadScoreBoard(scores); while (true) { clear(); refresh(); WINDOW* win = newwin(winHeight, winWidth, (LINES - winHeight) / 2, (COLS - winWidth) / 2); box(win, 0, 0); mvwaddch(win, 2, 0, ACS_LTEE); mvwhline(win, 2, 1, ACS_HLINE, winWidth - 1); mvwaddch(win, 2, winWidth - 1, ACS_RTEE); mvwprintw(win, 1, colRank, "CLASSEMENT"); mvwprintw(win, 1, colScore, "SCORE"); mvwprintw(win, 1, colName, "PSEUDO"); mvwprintw(win, 1, colBoard, "PLATEAU"); mvwaddch(win, 0, colScore - 2, ACS_TTEE); mvwvline(win, 1, colScore - 2, ACS_VLINE, winHeight - 2); mvwaddch(win, winHeight - 1, colScore - 2, ACS_BTEE); mvwaddch(win, 0, colName - 2, ACS_TTEE); mvwvline(win, 1, colName - 2, ACS_VLINE, winHeight - 2); mvwaddch(win, winHeight - 1, colName - 2, ACS_BTEE); mvwaddch(win, 0, colBoard - 2, ACS_TTEE); mvwvline(win, 1, colBoard - 2, ACS_VLINE, winHeight - 2); mvwaddch(win, winHeight - 1, colBoard - 2, ACS_BTEE); for(i = 0; i < n; i++) { mvwprintw(win, i + 3, colRank, "#%d", i+1); mvwprintw(win, i + 3, colScore, "%d", scores[i].score); mvwprintw(win, i + 3, colName, "%s", scores[i].username); mvwprintw(win, i + 3, colBoard, "%s", scores[i].boardName); } menuItems[0] = new_item("<Retour>", NULL); menuItems[1] = (ITEM *) NULL; //on initialise le menu menu = new_menu((ITEM **) menuItems); //on lui précise bien que le menu fait 1 ligne et 1 colonne set_menu_format(menu, 1, 1); //on associe le menu à une fenêtre et une sous-fenêtre set_menu_win(menu, win); //fenêtre hauteur largeur x y set_menu_sub(menu, derwin(win, 1, menuWidth, winHeight - 2, (winWidth - menuWidth) / 2)); set_menu_mark(menu, ""); noecho(); curs_set(0); //et hop, on affiche le menu et on rafraîchit. post_menu(menu); wrefresh(win); refresh(); //boucle pour le menu while((c = getch())) { bool resized = false; switch(c) { case KEY_RESIZE: //si le terminal a été redimensionné, on ré-exécute la boucle d'affichage resized = true; break; case KEY_MENU_ENTER: { unpost_menu(menu); free_menu(menu); for(i = 0; i < 2; ++i) free_item(menuItems[i]); clear(); refresh(); delwin(win); curs_set(1); echo(); return; } } if(resized) break; wrefresh(win); } } }
// // Affiche une fenêtre de menu. // int displayMenu(char **choices, int nbChoices, char title[], bool logo) { if(choices == NULL || nbChoices < 1) return -1; //variables pour l'affichage du menu ITEM **menuItems = NULL; MENU *menu = NULL; WINDOW *menuWin = NULL; int i = 0, c; int winWidth = POPUP_WINDOW_WIDTH; //largeur du menu = longueur du plus grand des choix possibles int menuWidth = max_strlen(choices, nbChoices) + 2; //on alloue de la mémoire pour initialiser les éléments du menu menuItems = (ITEM **) calloc(nbChoices + 1, sizeof(ITEM *)); //on créé de nouveaux éléments à partir des choix fournis for(i = 0; i < nbChoices; i++) { menuItems[i] = new_item(choices[i], NULL); } //on met un élément nul à la fin du tableau menuItems[nbChoices] = (ITEM *) NULL; while(true) { clear(); menuWin = (logo) ? getMenuWindow(nbChoices, title) : getMenuWindowNoLogo(nbChoices, title, -1, -1); //on initialise le menu menu = new_menu((ITEM **) menuItems); //on lui précise bien que le menu fait N lignes et 1 colonne set_menu_format(menu, nbChoices, 1); menu_opts_off(menu, O_NONCYCLIC); set_menu_mark(menu, "> "); //on associe le menu à une fenêtre et une sous-fenêtre set_menu_win(menu, menuWin); //fenêtre hauteur largeur x y set_menu_sub(menu, derwin(menuWin, nbChoices, menuWidth, (logo) ? 15 : 4, (winWidth - menuWidth) / 2)); //et hop, on affiche le menu et on rafraîchit. post_menu(menu); refresh(); wrefresh(menuWin); curs_set(0); noecho(); //boucle pour le menu while((c = getch())) { bool resized = false; switch(c) { case KEY_DOWN: menu_driver(menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(menu, REQ_UP_ITEM); break; case KEY_ESC_ALT: return -1; case KEY_RESIZE: //si on a redimensionné le terminal, on ré-exécute la boucle d'affichage resized = true; break; case KEY_MENU_ENTER: { int choice = item_index(current_item(menu)); //on libère la mémoire pour le menu, les choix, la fenêtre unpost_menu(menu); free_menu(menu); for(i = 0; i < nbChoices; ++i) free_item(menuItems[i]); clear(); refresh(); delwin(menuWin); //on réactive l'affichage des caractères tapés et du curseur echo(); curs_set(1); return choice; } } if(resized) break; wrefresh(menuWin); } } return 0; }
// // Demande à l'utilisateur s'il souhaite rejouer. // bool wantsToReplay(WINDOW *win, int top) { if(win == NULL || top < 0) return false; //variables pour l'affichage du menu ITEM **menuItems = NULL; MENU *menu = NULL; int i = 0, c; int nbChoices = 2; char *choices[] = { "Menu Principal", "Quitter" }; int winWidth = POPUP_WINDOW_WIDTH; //largeur du menu = longueur du plus grand des choix possibles int menuWidth = 22; //on alloue de la mémoire pour initialiser les éléments du menu menuItems = (ITEM **) calloc(nbChoices + 1, sizeof(ITEM *)); //on créé de nouveaux éléments à partir des choix fournis for(i = 0; i < nbChoices; i++) { menuItems[i] = new_item(choices[i], NULL); } //on met un élément nul à la fin du tableau menuItems[nbChoices] = (ITEM *) NULL; //on initialise le menu menu = new_menu((ITEM **) menuItems); //on lui précise bien que le menu fait 1 ligne et 2 colonnes set_menu_format(menu, 1, 2); //on associe le menu à une fenêtre et une sous-fenêtre set_menu_win(menu, win); //fenêtre hauteur largeur x y set_menu_sub(menu, derwin(win, nbChoices, menuWidth, top, (winWidth - menuWidth) / 2)); menu_opts_off(menu, O_NONCYCLIC); set_menu_mark(menu, ""); //et hop, on affiche le menu et on rafraîchit. post_menu(menu); refresh(); wrefresh(win); curs_set(0); noecho(); //boucle pour le menu while((c = getch())) { switch(c) { case KEY_LEFT: case KEY_UP: menu_driver(menu, REQ_LEFT_ITEM); break; case KEY_RIGHT: case KEY_DOWN: menu_driver(menu, REQ_RIGHT_ITEM); break; case KEY_MENU_ENTER: { int choice = item_index(current_item(menu)); unpost_menu(menu); free_menu(menu); for(i = 0; i < nbChoices; ++i) free_item(menuItems[i]); //si l'indice est 1 on renvoie 0 et vice-versa return !choice; } } wrefresh(win); } return false; }