PAGEVIEW.C 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* pageview.c - Implementation of the PagedViewer() system. */
  2. #include <string.h>
  3. #include "opendoor.h"
  4. #include "pageview.h"
  5. char bTitleColor = 0x0c;
  6. char bTitleLineColor = 0x04;
  7. char bNumberColor = 0x0a;
  8. char bTextColor = 0x02;
  9. char bPromptColor = 0x0f;
  10. int PagedViewer(
  11. int nInitialLine, /* Zero-based initial line number. */
  12. int nTotalLines, /* Total line count. */
  13. void (*pDisplayCallback)(int nLine, void *pData),
  14. void *pCallbackData, /* Data to pass to callback func. */
  15. BOOL bAllowSelection, /* TRUE if selection is permitted. */
  16. char *pszTitle, /* Title string, or NULL. */
  17. int nPageSize) /* # of lines to display per page. */
  18. {
  19. int nCurrentPage = 0;
  20. int nScreenLine;
  21. int nAbsoluteLine;
  22. char chPressed;
  23. char bCanPageDown;
  24. char bCanPageUp;
  25. /* Determine current page from initial line number, if specified. */
  26. if(nInitialLine != NO_LINE)
  27. {
  28. nCurrentPage = nInitialLine / nPageSize;
  29. }
  30. /* Loop until user makes a selection, or chooses to quit. */
  31. for(;;)
  32. {
  33. /* Display the current page. */
  34. /* Clear the screen */
  35. od_printf("\n\r");
  36. od_clr_scr();
  37. /* If a title has been specified, then display it. */
  38. if(pszTitle != NULL)
  39. {
  40. od_set_attrib(bTitleColor);
  41. od_repeat(' ', (80 - strlen(pszTitle)) / 2);
  42. od_disp_str(pszTitle);
  43. od_printf("\n\r");
  44. od_set_attrib(bTitleLineColor);
  45. if(od_control.user_ansi || od_control.user_avatar)
  46. {
  47. od_repeat(196, 79);
  48. }
  49. else
  50. {
  51. od_repeat('-', 79);
  52. }
  53. od_printf("\n\r");
  54. }
  55. /* Display the lines on this page. */
  56. nAbsoluteLine = nCurrentPage * nPageSize;
  57. nScreenLine = 0;
  58. while(nScreenLine < nPageSize && nAbsoluteLine < nTotalLines)
  59. {
  60. /* If selection is permitted, display an identifier for each line. */
  61. if(bAllowSelection)
  62. {
  63. od_set_attrib(bNumberColor);
  64. if(nScreenLine < 9)
  65. {
  66. od_printf("%d. ", nScreenLine + 1);
  67. }
  68. else
  69. {
  70. od_printf("%c. ", 'A' + (nScreenLine - 9));
  71. }
  72. }
  73. /* Display the line itself. */
  74. od_set_attrib(bTextColor);
  75. (*pDisplayCallback)(nAbsoluteLine, pCallbackData);
  76. od_printf("\n\r");
  77. /* Move to next line. */
  78. nScreenLine++;
  79. nAbsoluteLine++;
  80. }
  81. /* Determine whether user can page up or down from this page. */
  82. bCanPageDown = nCurrentPage < (nTotalLines - 1) / nPageSize;
  83. bCanPageUp = nCurrentPage > 0;
  84. /* Display prompt at bottom of screen. */
  85. od_set_attrib(bPromptColor);
  86. od_printf("\n\r[Page %d of %d] ", nCurrentPage + 1,
  87. ((nTotalLines - 1) / nPageSize) + 1);
  88. if(bAllowSelection)
  89. {
  90. od_printf("Choose an option or");
  91. }
  92. else
  93. {
  94. od_printf("Available options:");
  95. }
  96. if(bCanPageDown)
  97. {
  98. od_printf(" [N]ext page,");
  99. }
  100. if(bCanPageUp)
  101. {
  102. od_printf(" [P]revious page,");
  103. }
  104. od_printf(" [Q]uit.");
  105. /* Loop until the user makes a valid choice. */
  106. for(;;)
  107. {
  108. /* Get key from user */
  109. chPressed = toupper(od_get_key(TRUE));
  110. if(chPressed == 'Q')
  111. {
  112. /* If user chooses to quit, then return without a selection. */
  113. od_printf("\n\r");
  114. return(NO_LINE);
  115. }
  116. else if(chPressed == 'P' && bCanPageUp)
  117. {
  118. /* Move to previous page and redraw screen. */
  119. --nCurrentPage;
  120. break;
  121. }
  122. else if(chPressed == 'N' && bCanPageDown)
  123. {
  124. /* Move to next page and redraw screen. */
  125. ++nCurrentPage;
  126. break;
  127. }
  128. else if(bAllowSelection
  129. && (
  130. (chPressed >= '1' && chPressed <= '9')
  131. || (chPressed >= 'A' && chPressed <= 'M')
  132. )
  133. )
  134. {
  135. /* If user pressed a possible line key, and selection is */
  136. /* enabled, try translating character to a line number. */
  137. if(chPressed >= '1' && chPressed <= '9')
  138. {
  139. nScreenLine = chPressed - '1';
  140. }
  141. else
  142. {
  143. nScreenLine = 9 + (chPressed - 'A');
  144. }
  145. /* Calculate absolute line number. */
  146. nAbsoluteLine = nScreenLine + (nCurrentPage * nPageSize);
  147. /* If selected line is within range, then return selected line */
  148. /* number. */
  149. if(nScreenLine < nPageSize && nAbsoluteLine < nTotalLines)
  150. {
  151. od_printf("\n\r");
  152. return(nAbsoluteLine);
  153. }
  154. }
  155. }
  156. }
  157. }