/** Starts all the subscreens of the game */ int engine_windows_init() { window_s w; screen_s* s = &(engine.screen); int main_x = 0; int main_y = 0; if (global.screen_center_horizontally) main_x = engine.screen.width/2 - 80/2; if (global.screen_center_vertically) main_y = engine.screen.height/2 - 24/2; /* main window, wrapper of all others */ w.width = 80; w.height = 24; w.x = main_x; w.y = main_y; w.win = newwin(w.height, w.width, w.y, w.x); if (global.screen_show_outer_border) { if (global.screen_fancy_borders) window_fancy_borders(w.win); else window_normal_borders(w.win); } wnoutrefresh(w.win); s->main = w; /* leftmost */ w.width = 6 * 2 + 2; w.height = s->main.height - 2; /* borders */ w.x = 2; w.y = 1; w.win = derwin(s->main.win, w.height, w.width, w.y, w.x); if (global.screen_fancy_borders) { window_fancy_borders(w.win); /* If the player has no hold, doesnt make sense printing these parts */ if (global.game_can_hold) { /* making the top line between hold and score windows */ mvwaddch(w.win, 5, 0, ACS_LLCORNER|COLOR_PAIR(WHITE_BLACK)); my_mvwhline(w.win, 5, 1, ACS_HLINE|COLOR_PAIR(BLACK_BLACK)|A_BOLD, w.width - 2); mvwaddch(w.win, 5, w.width - 1, ACS_LRCORNER|COLOR_PAIR(BLACK_BLACK)|A_BOLD); /* making the bottom line between hold and score windows */ mvwaddch(w.win, 6, 0, ACS_ULCORNER|COLOR_PAIR(WHITE_BLACK)|A_BOLD); my_mvwhline(w.win, 6, 1, ACS_HLINE|COLOR_PAIR(WHITE_BLACK), w.width - 2); mvwaddch(w.win, 6, w.width - 1, ACS_URCORNER|COLOR_PAIR(WHITE_BLACK)); } } else { window_normal_borders(w.win); wattrset(w.win, engine_get_color(COLOR_BLACK, COLOR_BLACK, true)); mvwhline(w.win, 5, 1, '-', w.width - 2); } wnoutrefresh(w.win); s->leftmost = w; /* middle-left */ w.width = 10 * 2 + 2; w.height = s->main.height - 2; /* borders */ w.x = s->leftmost.x + s->leftmost.width + 1; w.y = 1; w.win = derwin(s->main.win, w.height, w.width, w.y, w.x); if (global.screen_fancy_borders) window_fancy_borders(w.win); else window_normal_borders(w.win); wnoutrefresh(w.win); s->middle_left = w; /* middle-right */ w.width = 4 * 2 + 2; w.height = s->main.height - 2; /* borders */ w.x = s->middle_left.x + s->middle_left.width + 1; w.y = 1; w.win = derwin(s->main.win, w.height, w.width, w.y, w.x); if (global.screen_fancy_borders) { window_fancy_borders(w.win); /* making the top line between 1st next and the rest */ mvwaddch(w.win, 3, 0, ACS_LLCORNER|COLOR_PAIR(WHITE_BLACK)); mvwhline(w.win, 3, 1, ACS_HLINE|COLOR_PAIR(BLACK_BLACK)|A_BOLD, w.width - 2); mvwaddch(w.win, 3, w.width - 1, ACS_LRCORNER|COLOR_PAIR(BLACK_BLACK)|A_BOLD); /* making the bottom line between 1st next and the rest */ mvwaddch(w.win, 4, 0, ACS_ULCORNER|COLOR_PAIR(WHITE_BLACK)|A_BOLD); mvwhline(w.win, 4, 1, ACS_HLINE|COLOR_PAIR(WHITE_BLACK), w.width - 2); mvwaddch(w.win, 4, w.width - 1, ACS_URCORNER|COLOR_PAIR(WHITE_BLACK)); } else { window_normal_borders(w.win); wattrset(w.win, engine_get_color(COLOR_BLACK, COLOR_BLACK, true)); mvwhline(w.win, 3, 1, '-', w.width - 2); } wnoutrefresh(w.win); s->middle_right = w; /* right-most */ w.width = s->main.width - (s->middle_right.x + s->middle_right.width) - 3; w.height = s->main.height - 2; /* borders */ w.x = s->middle_right.x + s->middle_right.width + 1; w.y = 1; w.win = derwin(s->main.win, w.height, w.width, w.y, w.x); if (global.screen_fancy_borders) window_fancy_borders(w.win); else window_normal_borders(w.win); wnoutrefresh(w.win); s->rightmost = w; /* next pieces */ w.width = s->middle_right.width - 2; w.height = s->middle_right.height - 2; w.x = 1; w.y = 1; w.win = derwin(s->middle_right.win, w.height, w.width, w.y, w.x); wnoutrefresh(w.win); s->next_container = w; /* first next piece */ w.width = s->next_container.width; w.height = 2; w.x = 0; w.y = 0; w.win = derwin(s->next_container.win, w.height, w.width, w.y, w.x); wnoutrefresh(w.win); s->next[0] = w; /* the rest */ int i; int y_offset = 2; for (i = 1; i <= global.game_next_no; i++) { /* making all the next pieces 1 line lower */ if (i != 1) y_offset = 0; w.width = s->next_container.width; w.height = 2; w.x = 0; w.y = s->next[i - 1].y + s->next[i - 1].height + 1 + y_offset; w.win = derwin(s->next_container.win, w.height, w.width, w.y, w.x); wnoutrefresh(w.win); s->next[i] = w; } s->board = new_sub_win_from(s->middle_left.win, (s->middle_left.width - 2), (s->middle_left.height - 2), 1, 1); s->info = new_sub_win_from(s->rightmost.win, (s->rightmost.width - 4), (s->rightmost.height - 2), 2, 1); s->leftmost_container = new_sub_win_from(s->leftmost.win, (s->leftmost.width - 2), (s->leftmost.height - 2), 1, 1); s->hold = new_sub_win_from(s->leftmost_container.win, s->leftmost_container.width, 4, 0, 0); s->score = new_sub_win_from(s->leftmost_container.win, s->leftmost_container.width, s->leftmost_container.height - (s->hold.height) - 2, 0, s->hold.y + s->hold.height + 2); /* w.width = s->leftmost_container.width; */ /* w.height = s->leftmost_container.height - (s->hold.height) - 2; */ /* w.x = 0; */ /* w.y = s->hold.y + s->hold.height + 2; */ /* w.win = derwin(s->leftmost_container.win, w.height, w.width, w.y, w.x); */ /* wnoutrefresh(w.win); */ /* s->score = w; */ w = s->info; wattrset(w.win, engine_get_color(COLOR_WHITE, COLOR_BLACK, true)); mvwaddstr(w.win, w.height - 1, 16 , "Loading"); wnoutrefresh(w.win); return 1; }
/* Starts the program and prints the main menu */ int main(int qw) { static int mm = -1; mm++; initscr(); curs_set(0); noecho(); if(qw == 2) banner(4); else if(qw == 1) banner(3); ITEM **my_items; int c; MENU *my_menu; WINDOW *my_menu_win; int n_choices, i; ITEM *cur; /* Initialize curses */ initscr(); start_color(); init_pair(5, COLOR_RED, COLOR_BLACK); init_pair(6, COLOR_BLACK, COLOR_RED); init_pair(7, COLOR_CYAN, COLOR_BLACK); cbreak(); noecho(); keypad(stdscr, TRUE); init_pair(1, COLOR_RED, COLOR_BLACK); START: attrset(COLOR_PAIR(7)); n_choices = ARRAY_SIZE(choices_index); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) { my_items[i] = new_item(choices_index[i], NULL); if(i == 0) /* Set the user pointer */ set_item_userptr(my_items[i], mains); else if(i == 1) set_item_userptr(my_items[i], race_menu); else if(i == 2) set_item_userptr(my_items[i], exitit); } my_items[n_choices] = (ITEM *)NULL; /* Crate menu */ my_menu = new_menu((ITEM **)my_items); /* Create the window to be associated with the menu */ my_menu_win = newwin(8, 25, 15, 70); 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, 5, 19, 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_middle1(my_menu_win, 1, 0, 25, "CHOOSE A GAME", COLOR_PAIR(7)); attrset(COLOR_PAIR(7)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 24); mvwaddch(my_menu_win, 2, 24, ACS_RTEE); mvprintw(LINES - 2, 1, " Press F1 to exit"); /* Post the menu */ mvprintw(LINES - 3, 1, " Press <ENTER> to see the option selected"); mvprintw(LINES - 2, 1, " Up and Down arrow keys to navigate (F1 to Exit)"); post_menu(my_menu); if(!mm) moto(0); else moto(1); 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 10: cur = current_item(my_menu); endwin(); unpost_menu(my_menu); clear(); refresh(); void (*p)(char *); cur = current_item(my_menu); p = item_userptr(cur); p((char *)item_name(cur)); pos_menu_cursor(my_menu); initscr(); /* Post the menu */ attrset(COLOR_PAIR(7)); mvprintw(LINES - 3, 1, " Press <ENTER> to see the option selected"); mvprintw(LINES - 2, 1, " Up and Down arrow keys to naviage (F1 to Exit)"); goto START; refresh(); break; } wrefresh(my_menu_win); } /* Unpost and free all the memory taken up */ for(i = 0; i < n_choices; ++i) free_item(my_items[i]); free_menu(my_menu); endwin(); exit(1); }
// WINDOW *subpad (WINDOW *, int, int, int, int) {} WINDOW *subwin(WINDOW *w, int l, int c, int y, int x) { return derwin(w, l, c, y - w->_begy, x - w->_begx); }
/* 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 dialog_inputbox(WINDOW *main_window, const char *title, const char *prompt, const char *init, char **resultp, int *result_len) { int prompt_lines = 0; int prompt_width = 0; WINDOW *win; WINDOW *prompt_win; WINDOW *form_win; PANEL *panel; int i, x, y; int res = -1; int cursor_position = strlen(init); int cursor_form_win; char *result = *resultp; if (strlen(init)+1 > *result_len) { *result_len = strlen(init)+1; *resultp = result = realloc(result, *result_len); } /* find the widest line of msg: */ prompt_lines = get_line_no(prompt); for (i = 0; i < prompt_lines; i++) { const char *line = get_line(prompt, i); int len = get_line_length(line); prompt_width = max(prompt_width, len); } if (title) prompt_width = max(prompt_width, strlen(title)); /* place dialog in middle of screen */ y = (LINES-(prompt_lines+4))/2; x = (COLS-(prompt_width+4))/2; strncpy(result, init, *result_len); /* create the windows */ win = newwin(prompt_lines+6, prompt_width+7, y, x); prompt_win = derwin(win, prompt_lines+1, prompt_width, 2, 2); form_win = derwin(win, 1, prompt_width, prompt_lines+3, 2); keypad(form_win, TRUE); (void) wattrset(form_win, attributes[INPUT_FIELD]); (void) wattrset(win, attributes[INPUT_BOX]); box(win, 0, 0); (void) wattrset(win, attributes[INPUT_HEADING]); if (title) mvwprintw(win, 0, 3, "%s", title); /* print message */ (void) wattrset(prompt_win, attributes[INPUT_TEXT]); fill_window(prompt_win, prompt); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); cursor_form_win = min(cursor_position, prompt_width-1); mvwprintw(form_win, 0, 0, "%s", result + cursor_position-cursor_form_win); /* create panels */ panel = new_panel(win); /* show the cursor */ curs_set(1); touchwin(win); refresh_all_windows(main_window); while ((res = wgetch(form_win))) { int len = strlen(result); switch (res) { case 10: /* ENTER */ case 27: /* ESCAPE */ case KEY_F(F_HELP): case KEY_F(F_EXIT): case KEY_F(F_BACK): break; case 127: case KEY_BACKSPACE: if (cursor_position > 0) { memmove(&result[cursor_position-1], &result[cursor_position], len-cursor_position+1); cursor_position--; cursor_form_win--; len--; } break; case KEY_DC: if (cursor_position >= 0 && cursor_position < len) { memmove(&result[cursor_position], &result[cursor_position+1], len-cursor_position+1); len--; } break; case KEY_UP: case KEY_RIGHT: if (cursor_position < len) { cursor_position++; cursor_form_win++; } break; case KEY_DOWN: case KEY_LEFT: if (cursor_position > 0) { cursor_position--; cursor_form_win--; } break; case KEY_HOME: cursor_position = 0; cursor_form_win = 0; break; case KEY_END: cursor_position = len; cursor_form_win = min(cursor_position, prompt_width-1); break; default: if ((isgraph(res) || isspace(res))) { /* one for new char, one for '\0' */ if (len+2 > *result_len) { *result_len = len+2; *resultp = result = realloc(result, *result_len); } /* insert the char at the proper position */ memmove(&result[cursor_position+1], &result[cursor_position], len-cursor_position+1); result[cursor_position] = res; cursor_position++; cursor_form_win++; len++; } else { mvprintw(0, 0, "unknown key: %d\n", res); } break; } if (cursor_form_win < 0) cursor_form_win = 0; else if (cursor_form_win > prompt_width-1) cursor_form_win = prompt_width-1; wmove(form_win, 0, 0); wclrtoeol(form_win); mvwprintw(form_win, 0, 0, "%*s", prompt_width, " "); mvwprintw(form_win, 0, 0, "%s", result + cursor_position-cursor_form_win); wmove(form_win, 0, cursor_form_win); touchwin(win); refresh_all_windows(main_window); if (res == 10) { res = 0; break; } else if (res == 27 || res == KEY_F(F_BACK) || res == KEY_F(F_EXIT)) { res = KEY_EXIT; break; } else if (res == KEY_F(F_HELP)) { res = 1; break; } } /* hide the cursor */ curs_set(0); del_panel(panel); delwin(prompt_win); delwin(form_win); delwin(win); return res; }
/* * 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); }
static int test_get_wstr(int level, char **argv, WINDOW *strwin) { WINDOW *txtbox = 0; WINDOW *txtwin = 0; FILE *fp; int ch; int rc; int txt_x = 0, txt_y = 0; int base_y; int flavor = 0; int limit = getmaxx(strwin) - 5; int actual; wint_t buffer[MAX_COLS]; if (argv[level] == 0) { beep(); return FALSE; } if (level > 1) { txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level); box(txtbox, 0, 0); wnoutrefresh(txtbox); txtwin = derwin(txtbox, getmaxy(txtbox) - 2, getmaxx(txtbox) - 2, 1, 1); base_y = 0; } else { txtwin = stdscr; base_y = BASE_Y; } keypad(txtwin, TRUE); /* enable keyboard mapping */ (void) cbreak(); /* take input chars one at a time, no wait for \n */ (void) noecho(); /* don't echo input */ txt_y = base_y; txt_x = 0; wmove(txtwin, txt_y, txt_x); if ((fp = fopen(argv[level], "r")) != 0) { while ((ch = fgetc(fp)) != EOF) { if (waddch(txtwin, UChar(ch)) != OK) { break; } } fclose(fp); } else { wprintw(txtwin, "Cannot open:\n%s", argv[1]); } wmove(txtwin, txt_y, txt_x); actual = ShowFlavor(strwin, txtwin, flavor, limit); while (!Quit(ch = mvwgetch(txtwin, txt_y, txt_x))) { switch (ch) { case KEY_DOWN: case 'j': if (txt_y < getmaxy(txtwin) - 1) { MovePrompt(txtwin, actual, ++txt_y, txt_x); } else { beep(); } break; case KEY_UP: case 'k': if (txt_y > base_y) { MovePrompt(txtwin, actual, --txt_y, txt_x); } else { beep(); } break; case KEY_LEFT: case 'h': if (txt_x > 0) { MovePrompt(txtwin, actual, txt_y, --txt_x); } else { beep(); } break; case KEY_RIGHT: case 'l': if (txt_x < getmaxx(txtwin) - 1) { MovePrompt(txtwin, actual, txt_y, ++txt_x); } else { beep(); } break; case 'w': test_get_wstr(level + 1, argv, strwin); if (txtbox != 0) { touchwin(txtbox); wnoutrefresh(txtbox); } else { touchwin(txtwin); wnoutrefresh(txtwin); } break; case '-': if (limit > 0) { actual = ShowFlavor(strwin, txtwin, flavor, --limit); MovePrompt(txtwin, actual, txt_y, txt_x); } else { beep(); } break; case '+': actual = ShowFlavor(strwin, txtwin, flavor, ++limit); MovePrompt(txtwin, actual, txt_y, txt_x); break; case '<': if (flavor > 0) { actual = ShowFlavor(strwin, txtwin, --flavor, limit); MovePrompt(txtwin, actual, txt_y, txt_x); } else { beep(); } break; case '>': if (flavor + 1 < eMaxFlavor) { actual = ShowFlavor(strwin, txtwin, ++flavor, limit); MovePrompt(txtwin, actual, txt_y, txt_x); } else { beep(); } break; case ':': actual = ShowFlavor(strwin, txtwin, flavor, limit); *buffer = '\0'; rc = ERR; echo(); (void) wattrset(txtwin, A_REVERSE); switch (flavor) { case eGetStr: if (txtwin != stdscr) { wmove(txtwin, txt_y, txt_x); rc = wget_wstr(txtwin, buffer); } else { move(txt_y, txt_x); rc = get_wstr(buffer); } break; case eGetNStr: if (txtwin != stdscr) { wmove(txtwin, txt_y, txt_x); rc = wgetn_wstr(txtwin, buffer, limit); } else { move(txt_y, txt_x); rc = getn_wstr(buffer, limit); } break; case eMvGetStr: if (txtwin != stdscr) { rc = mvwget_wstr(txtwin, txt_y, txt_x, buffer); } else { rc = mvget_wstr(txt_y, txt_x, buffer); } break; case eMvGetNStr: if (txtwin != stdscr) { rc = mvwgetn_wstr(txtwin, txt_y, txt_x, buffer, limit); } else { rc = mvgetn_wstr(txt_y, txt_x, buffer, limit); } break; case eMaxFlavor: break; } noecho(); (void) wattrset(txtwin, A_NORMAL); wprintw(strwin, "%d", rc); (void) waddwstr(strwin, (wchar_t *) buffer); wnoutrefresh(strwin); break; default: beep(); break; } doupdate(); } if (level > 1) { delwin(txtwin); delwin(txtbox); } return TRUE; }
static void test_inserts(int level) { static bool first = TRUE; int ch; int limit; int row = 1; int col; int row2, col2; int length; wchar_t buffer[BUFSIZ]; WINDOW *look = 0; WINDOW *work = 0; WINDOW *show = 0; int margin = (2 * MY_TABSIZE) - 1; Options option = ((m_opt ? oMove : oDefault) | ((w_opt || (level > 0)) ? oWindow : oDefault)); if (first) { static char cmd[80]; setlocale(LC_ALL, ""); putenv(strcpy(cmd, "TABSIZE=8")); initscr(); (void) cbreak(); /* take input chars one at a time, no wait for \n */ (void) noecho(); /* don't echo input */ keypad(stdscr, TRUE); } limit = LINES - 5; if (level > 0) { look = newwin(limit, COLS - (2 * (level - 1)), 0, level - 1); work = newwin(limit - 2, COLS - (2 * level), 1, level); show = newwin(4, COLS, limit + 1, 0); box(look, 0, 0); wnoutrefresh(look); limit -= 2; } else { work = stdscr; show = derwin(stdscr, 4, COLS, limit + 1, 0); } keypad(work, TRUE); for (col = margin + 1; col < COLS; col += MY_TABSIZE) mvwvline(work, row, col, '.', limit - 2); mvwvline(work, row, margin, ACS_VLINE, limit - 2); mvwvline(work, row, margin + 1, ACS_VLINE, limit - 2); limit /= 2; mvwaddstr(work, 1, 2, "String"); mvwaddstr(work, limit + 1, 2, "Chars"); wnoutrefresh(work); buffer[length = 0] = '\0'; legend(show, level, option, buffer, length); wnoutrefresh(show); doupdate(); /* * Show the characters inserted in color, to distinguish from those that * are shifted. */ if (has_colors()) { start_color(); init_pair(1, COLOR_WHITE, COLOR_BLUE); wbkgdset(work, COLOR_PAIR(1) | ' '); } while ((ch = read_linedata(work)) != ERR && !isQUIT(ch)) { wmove(work, row, margin + 1); switch (ch) { case key_RECUR: test_inserts(level + 1); touchwin(look); touchwin(work); touchwin(show); wnoutrefresh(look); wnoutrefresh(work); wnoutrefresh(show); doupdate(); break; case key_NEWLINE: if (row < limit) { ++row; /* put the whole string in, all at once */ col2 = margin + 1; switch (option) { case oDefault: if (n_opt > 1) { for (col = 0; col < length; col += n_opt) { col2 = ColOf(buffer, col, margin); if (move(row, col2) != ERR) { InsNStr(buffer + col, LEN(col)); } } } else { if (move(row, col2) != ERR) { InsStr(buffer); } } break; case oMove: if (n_opt > 1) { for (col = 0; col < length; col += n_opt) { col2 = ColOf(buffer, col, margin); MvInsNStr(row, col2, buffer + col, LEN(col)); } } else { MvInsStr(row, col2, buffer); } break; case oWindow: if (n_opt > 1) { for (col = 0; col < length; col += n_opt) { col2 = ColOf(buffer, col, margin); if (wmove(work, row, col2) != ERR) { WInsNStr(work, buffer + col, LEN(col)); } } } else { if (wmove(work, row, col2) != ERR) { WInsStr(work, buffer); } } break; case oMoveWindow: if (n_opt > 1) { for (col = 0; col < length; col += n_opt) { col2 = ColOf(buffer, col, margin); MvWInsNStr(work, row, col2, buffer + col, LEN(col)); } } else { MvWInsStr(work, row, col2, buffer); } break; } /* do the corresponding single-character insertion */ row2 = limit + row; for (col = 0; col < length; ++col) { col2 = ColOf(buffer, col, margin); switch (option) { case oDefault: if (move(row2, col2) != ERR) { InsCh((chtype) buffer[col]); } break; case oMove: MvInsCh(row2, col2, (chtype) buffer[col]); break; case oWindow: if (wmove(work, row2, col2) != ERR) { WInsCh(work, (chtype) buffer[col]); } break; case oMoveWindow: MvWInsCh(work, row2, col2, (chtype) buffer[col]); break; } } } else { beep(); } break; default: buffer[length++] = ch; buffer[length] = '\0'; /* put the string in, one character at a time */ col = ColOf(buffer, length - 1, margin); switch (option) { case oDefault: if (move(row, col) != ERR) { InsStr(buffer + length - 1); } break; case oMove: MvInsStr(row, col, buffer + length - 1); break; case oWindow: if (wmove(work, row, col) != ERR) { WInsStr(work, buffer + length - 1); } break; case oMoveWindow: MvWInsStr(work, row, col, buffer + length - 1); break; } /* do the corresponding single-character insertion */ switch (option) { case oDefault: if (move(limit + row, col) != ERR) { InsCh(ch); } break; case oMove: MvInsCh(limit + row, col, ch); break; case oWindow: if (wmove(work, limit + row, col) != ERR) { WInsCh(work, ch); } break; case oMoveWindow: MvWInsCh(work, limit + row, col, ch); break; } wnoutrefresh(work); legend(show, level, option, buffer, length); wnoutrefresh(show); doupdate(); break; } } if (level > 0) { delwin(show); delwin(work); delwin(look); } }
static int test_inchs(int level, char **argv, WINDOW *chrwin, WINDOW *strwin) { WINDOW *txtbox = 0; WINDOW *txtwin = 0; FILE *fp; int j; int txt_x = 0, txt_y = 0; int base_y; int limit; cchar_t ch; cchar_t text[MAX_COLS]; if (argv[level] == 0) { beep(); return FALSE; } if (level > 1) { txtbox = newwin(LINES - BASE_Y, COLS - level, BASE_Y, level); box(txtbox, 0, 0); wnoutrefresh(txtbox); txtwin = derwin(txtbox, getmaxy(txtbox) - 2, getmaxx(txtbox) - 2, 1, 1); base_y = 0; } else { txtwin = stdscr; base_y = BASE_Y; } keypad(txtwin, TRUE); /* enable keyboard mapping */ (void) cbreak(); /* take input chars one at a time, no wait for \n */ (void) noecho(); /* don't echo input */ txt_y = base_y; txt_x = 0; wmove(txtwin, txt_y, txt_x); if ((fp = fopen(argv[level], "r")) != 0) { while ((j = fgetc(fp)) != EOF) { if (waddch(txtwin, UChar(j)) != OK) { break; } } fclose(fp); } else { wprintw(txtwin, "Cannot open:\n%s", argv[1]); } while (!Quit(j = mvwgetch(txtwin, txt_y, txt_x))) { switch (j) { case KEY_DOWN: case 'j': if (txt_y < getmaxy(txtwin) - 1) txt_y++; else beep(); break; case KEY_UP: case 'k': if (txt_y > base_y) txt_y--; else beep(); break; case KEY_LEFT: case 'h': if (txt_x > 0) txt_x--; else beep(); break; case KEY_RIGHT: case 'l': if (txt_x < getmaxx(txtwin) - 1) txt_x++; else beep(); break; case 'w': test_inchs(level + 1, argv, chrwin, strwin); if (txtbox != 0) { touchwin(txtbox); wnoutrefresh(txtbox); } else { touchwin(txtwin); wnoutrefresh(txtwin); } break; default: beep(); break; } mvwprintw(chrwin, 0, 0, "char:"); wclrtoeol(chrwin); if (txtwin != stdscr) { wmove(txtwin, txt_y, txt_x); if (win_wch(txtwin, &ch) != ERR) { if (wadd_wch(chrwin, &ch) != ERR) { for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { if (mvwin_wch(txtwin, txt_y, j, &ch) != ERR) { if (wadd_wch(chrwin, &ch) == ERR) { break; } } else { break; } } } } } else { move(txt_y, txt_x); if (in_wch(&ch) != ERR) { if (wadd_wch(chrwin, &ch) != ERR) { for (j = txt_x + 1; j < getmaxx(txtwin); ++j) { if (mvin_wch(txt_y, j, &ch) != ERR) { if (wadd_wch(chrwin, &ch) == ERR) { break; } } else { break; } } } } } wnoutrefresh(chrwin); mvwprintw(strwin, 0, 0, "text:"); wclrtobot(strwin); limit = getmaxx(strwin) - 5; if (txtwin != stdscr) { wmove(txtwin, txt_y, txt_x); if (win_wchstr(txtwin, text) != ERR) { mvwadd_wchstr(strwin, 0, 5, text); } wmove(txtwin, txt_y, txt_x); if (win_wchnstr(txtwin, text, limit) != ERR) { mvwadd_wchstr(strwin, 1, 5, text); } if (mvwin_wchstr(txtwin, txt_y, txt_x, text) != ERR) { mvwadd_wchstr(strwin, 2, 5, text); } if (mvwin_wchnstr(txtwin, txt_y, txt_x, text, limit) != ERR) { mvwadd_wchstr(strwin, 3, 5, text); } } else { move(txt_y, txt_x); if (in_wchstr(text) != ERR) { mvwadd_wchstr(strwin, 0, 5, text); } move(txt_y, txt_x); if (in_wchnstr(text, limit) != ERR) { mvwadd_wchstr(strwin, 1, 5, text); } if (mvin_wchstr(txt_y, txt_x, text) != ERR) { mvwadd_wchstr(strwin, 2, 5, text); } if (mvin_wchnstr(txt_y, txt_x, text, limit) != ERR) { mvwadd_wchstr(strwin, 3, 5, text); } } wnoutrefresh(strwin); } if (level > 1) { delwin(txtwin); delwin(txtbox); } return TRUE; }
int main() { FIELD *field[3]; FORM *my_form; WINDOW *my_form_win; int ch, rows, cols; /* Initialize curses */ initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); /* Initialize few color pairs */ init_pair(1, COLOR_RED, COLOR_BLACK); /* Initialize the fields */ field[0] = new_field(1, 10, 6, 1, 0, 0); field[1] = new_field(1, 10, 8, 1, 0, 0); field[2] = NULL; /* Set field options */ set_field_back(field[0], A_UNDERLINE); field_opts_off(field[0], O_AUTOSKIP); /* Don't go to next field when this */ /* Field is filled up */ set_field_back(field[1], A_UNDERLINE); field_opts_off(field[1], O_AUTOSKIP); /* Create the form and post it */ my_form = new_form(field); /* Calculate the area required for the form */ scale_form(my_form, &rows, &cols); /* Create the window to be associated with the form */ my_form_win = newwin(rows + 4, cols + 4, 4, 4); keypad(my_form_win, TRUE); /* Set main window and sub window */ set_form_win(my_form, my_form_win); set_form_sub(my_form, derwin(my_form_win, rows, cols, 2, 2)); /* Print a border around the main window and print a title */ box(my_form_win, 0, 0); print_in_middle(my_form_win, 1, 0, cols + 4, "My Form", COLOR_PAIR(1)); post_form(my_form); wrefresh(my_form_win); mvprintw(LINES - 2, 0, "Use UP, DOWN arrow keys to switch between fields"); refresh(); /* Loop through to get user requests */ while((ch = wgetch(my_form_win)) != KEY_F(1)) { switch(ch) { case KEY_DOWN: /* Go to next field */ form_driver(my_form, REQ_NEXT_FIELD); /* Go to the end of the present buffer */ /* Leaves nicely at the last character */ form_driver(my_form, REQ_END_LINE); break; case KEY_UP: /* Go to previous field */ form_driver(my_form, REQ_PREV_FIELD); form_driver(my_form, REQ_END_LINE); break; default: /* If this is a normal character, it gets */ /* Printed */ form_driver(my_form, ch); break; } } /* Un post form and free the memory */ unpost_form(my_form); free_form(my_form); free_field(field[0]); free_field(field[1]); endwin(); return 0; }
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; }