ncurses-interface.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "ncurses-interface.h"
  2. extern int start_field(void);
  3. OptionBox options;
  4. static void on_enter(const char *entry) {
  5. move(20, 0);
  6. clrtoeol();
  7. attron(COLOR_PAIR(2));
  8. if (strstr(entry, ENTRY_EXIT)) {
  9. stopprogram = true;
  10. mvprintw(LINES - 1, 0, "BYE, BYE, BYE", entry);
  11. options.engine = Exit;
  12. }
  13. ELIF(strstr(entry, ENTRY_HIGHMODE)) {
  14. mvprintw(LINES - 1, 0, "Enter to highhead mode?(enter/control+C)");
  15. options.engine = HighHead;
  16. }
  17. ELIF(strstr(entry, ENTRY_REGEXPMODE)) {
  18. mvprintw(LINES - 1, 0, "Enter to RegExp mode");
  19. options.engine = RegExp;
  20. // on_regexpmode_selected();
  21. }
  22. ELIF(strstr(entry, ENTRY_SEARCHBYTEXTMODE)) {
  23. mvprintw(LINES - 1, 0, "Enter to Search by text mode");
  24. options.engine = SearchByText;
  25. }
  26. // mvprintw(LINES - 1,0 , ITEMSELECTEDTEXT" %s", entry);
  27. attroff(COLOR_PAIR(2));
  28. refresh();
  29. if (options.engine != HighHead && !stopprogram)
  30. start_field();
  31. stopprogram = true;
  32. }
  33. void start_menu(void) {
  34. ITEM **my_items;
  35. int c;
  36. MENU *my_menu;
  37. WINDOW *my_menu_win;
  38. int n_choices, i;
  39. /* Initialize curses */
  40. initscr();
  41. start_color();
  42. cbreak();
  43. noecho();
  44. keypad(stdscr, TRUE);
  45. init_pair(1, COLOR_YELLOW, COLOR_BLACK);
  46. init_pair(2, COLOR_CYAN, COLOR_BLACK);
  47. /* Create items */
  48. n_choices = ARRAY_SIZE(choices);
  49. my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *));
  50. for (i = 0; i < n_choices; ++i) {
  51. my_items[i] = new_item(choices[i], choices[i]);
  52. set_item_userptr(my_items[i], (void (*)(char *))on_enter);
  53. }
  54. /* Crate menu */
  55. my_menu = new_menu((ITEM **)my_items);
  56. /* Set menu option not to show the description */
  57. menu_opts_off(my_menu, O_SHOWDESC);
  58. /* Create the window to be associated with the menu */
  59. my_menu_win = newwin(10, 70, 4, 4);
  60. keypad(my_menu_win, TRUE);
  61. /* Set main window and sub window */
  62. set_menu_win(my_menu, my_menu_win);
  63. set_menu_sub(my_menu, derwin(my_menu_win, 6, 68, 3, 1));
  64. // set_menu_format(my_menu, 5, 3);
  65. set_menu_mark(my_menu, " * ");
  66. /* Print a border around the main window and print a title */
  67. box(my_menu_win, 1, 0);
  68. box(my_menu_win, -1, 0);
  69. attron(COLOR_PAIR(1));
  70. mvprintw(LINES - 3, 0, "Use PageUp and PageDown to scroll");
  71. mvprintw(LINES - 2, 0, "Use Arrow Keys to navigate (F1 to Exit)");
  72. attroff(COLOR_PAIR(1));
  73. refresh();
  74. /* Post the menu */
  75. post_menu(my_menu);
  76. wrefresh(my_menu_win);
  77. while ((c = wgetch(my_menu_win)) != KEY_F(1) && !stopprogram) {
  78. switch (c) {
  79. case KEY_DOWN:
  80. menu_driver(my_menu, REQ_DOWN_ITEM);
  81. break;
  82. case KEY_UP:
  83. menu_driver(my_menu, REQ_UP_ITEM);
  84. break;
  85. case KEY_LEFT:
  86. menu_driver(my_menu, REQ_LEFT_ITEM);
  87. break;
  88. case KEY_RIGHT:
  89. menu_driver(my_menu, REQ_RIGHT_ITEM);
  90. break;
  91. case KEY_NPAGE:
  92. menu_driver(my_menu, REQ_SCR_DPAGE);
  93. break;
  94. case KEY_PPAGE:
  95. menu_driver(my_menu, REQ_SCR_UPAGE);
  96. break;
  97. case 10: /* Enter */
  98. {
  99. ITEM *cur;
  100. void (*p)(char *);
  101. cur = current_item(my_menu);
  102. p = item_userptr((const ITEM *)cur);
  103. p((char *)item_name(cur));
  104. pos_menu_cursor(my_menu);
  105. break;
  106. }
  107. }
  108. wrefresh(my_menu_win);
  109. }
  110. /* Unpost and free all the memory taken up */
  111. unpost_menu(my_menu);
  112. free_menu(my_menu);
  113. for (i = 0; i < n_choices; ++i)
  114. free_item(my_items[i]);
  115. endwin();
  116. }
  117. OptionBox getOption(void) {
  118. start_menu();
  119. return options;
  120. }