Advertisement
Guest User

Untitled

a guest
Mar 15th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "text.txt"
  5.  
  6.  
  7. int barmenu(const char **array, const int row, const int col, const int arraylength, const int width, int menulength, int selection);
  8.  
  9. int main(void)
  10. {
  11.  
  12. int selection,row=1, col=10, arraylength=20, width=5, menulength=5;
  13. // const char *testarray[]={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
  14.  
  15.  
  16. initscr();
  17. noecho();
  18. keypad(stdscr,TRUE);
  19.  
  20. selection=barmenu(testarray,row,col,arraylength,width,menulength,3);
  21.  
  22. mvprintw(15,0,"Selection= %d",selection);
  23. refresh();
  24. getch();
  25.  
  26. endwin();
  27.  
  28. // char i;
  29.  
  30. // printf("New value : ");
  31. // scanf("%s", &testarray[i]);
  32. // printf("%s", &testarray[i]);
  33.  
  34.  
  35.  
  36. // Char arrays are declared like so:
  37. // const char *testarray[]={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
  38.  
  39. // Open a file for writing.
  40. // (This will replace any existing file. Use "w+" for appending)
  41. FILE *file = fopen("filename", "a");
  42.  
  43. int results = fputs(testarray, file);
  44. if (results == EOF) {
  45. // Failed to write do error code here.
  46. }
  47. fclose(file);
  48.  
  49. return 0;
  50. }
  51.  
  52. int barmenu(const char **array,const int row, const int col, const int arraylength, const int width, int menulength, int selection)
  53. {
  54. int counter,offset=0,ky=0;
  55. char formatstring[7];
  56. curs_set(0);
  57.  
  58. if (arraylength < menulength)
  59. menulength=arraylength;
  60.  
  61. if (selection > menulength)
  62. offset=selection-menulength+1;
  63.  
  64. sprintf(formatstring,"%%-%ds",width); // remove - sign to right-justify the menu items
  65.  
  66. while(ky != 27)
  67. {
  68. for (counter=0; counter < menulength; counter++)
  69. {
  70. if (counter+offset==selection)
  71. attron(A_REVERSE);
  72. mvprintw(row+counter,col,formatstring,array[counter+offset]);
  73. attroff(A_REVERSE);
  74. }
  75.  
  76. ky=getch();
  77.  
  78. switch(ky)
  79. {
  80. case KEY_UP:
  81. if (selection)
  82. {
  83. selection--;
  84. if (selection < offset)
  85. offset--;
  86. }
  87. break;
  88. case KEY_DOWN:
  89. if (selection < arraylength-1)
  90. {
  91. selection++;
  92. if (selection > offset+menulength-1)
  93. offset++;
  94. }
  95. break;
  96. case KEY_HOME:
  97. selection=0;
  98. offset=0;
  99. break;
  100. case KEY_END:
  101. selection=arraylength-1;
  102. offset=arraylength-menulength;
  103. break;
  104. case KEY_PPAGE:
  105. selection-=menulength;
  106. if (selection < 0)
  107. selection=0;
  108. offset-=menulength;
  109. if (offset < 0)
  110. offset=0;
  111. break;
  112. case KEY_NPAGE:
  113. selection+=menulength;
  114. if (selection > arraylength-1)
  115. selection=arraylength-1;
  116. offset+=menulength;
  117. if (offset > arraylength-menulength)
  118. offset=arraylength-menulength;
  119. break;
  120.  
  121. case 10: //enter
  122. return selection;
  123. break;
  124. case KEY_F(1): // function key 1
  125. return -1;
  126. case 27: //esc
  127. // esc twice to get out, otherwise eat the chars that don't work
  128. //from home or end on the keypad
  129. ky=getch();
  130. if (ky == 27)
  131. {
  132. curs_set(0);
  133. mvaddstr(9,77," ");
  134. return -1;
  135. }
  136. else
  137. if (ky=='[')
  138. {
  139. getch();
  140. getch();
  141. }
  142. else
  143. ungetch(ky);
  144. }
  145. }
  146. return -1;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement