mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

557 lines
15 KiB

  1. /*
  2. * textbox.c -- implements the text box
  3. *
  4. * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5. * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
  6. * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "dialog.h"
  23. static void back_lines (int n);
  24. static void print_page (WINDOW * win, int height, int width);
  25. static void print_line (WINDOW * win, int row, int width);
  26. static char *get_line (void);
  27. static void print_position (WINDOW * win, int height, int width);
  28. static int hscroll = 0, fd, file_size, bytes_read;
  29. static int begin_reached = 1, end_reached = 0, page_length;
  30. static char *buf, *page;
  31. /*
  32. * Display text from a file in a dialog box.
  33. */
  34. int
  35. dialog_textbox (const char *title, const char *file, int height, int width)
  36. {
  37. int i, x, y, cur_x, cur_y, fpos, key = 0;
  38. int passed_end;
  39. char search_term[MAX_LEN + 1];
  40. WINDOW *dialog, *text;
  41. search_term[0] = '\0'; /* no search term entered yet */
  42. /* Open input file for reading */
  43. if ((fd = open (file, O_RDONLY)) == -1) {
  44. endwin ();
  45. fprintf (stderr,
  46. "\nCan't open input file in dialog_textbox().\n");
  47. exit (-1);
  48. }
  49. /* Get file size. Actually, 'file_size' is the real file size - 1,
  50. since it's only the last byte offset from the beginning */
  51. if ((file_size = lseek (fd, 0, SEEK_END)) == -1) {
  52. endwin ();
  53. fprintf (stderr, "\nError getting file size in dialog_textbox().\n");
  54. exit (-1);
  55. }
  56. /* Restore file pointer to beginning of file after getting file size */
  57. if (lseek (fd, 0, SEEK_SET) == -1) {
  58. endwin ();
  59. fprintf (stderr, "\nError moving file pointer in dialog_textbox().\n");
  60. exit (-1);
  61. }
  62. /* Allocate space for read buffer */
  63. if ((buf = malloc (BUF_SIZE + 1)) == NULL) {
  64. endwin ();
  65. fprintf (stderr, "\nCan't allocate memory in dialog_textbox().\n");
  66. exit (-1);
  67. }
  68. if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
  69. endwin ();
  70. fprintf (stderr, "\nError reading file in dialog_textbox().\n");
  71. exit (-1);
  72. }
  73. buf[bytes_read] = '\0'; /* mark end of valid data */
  74. page = buf; /* page is pointer to start of page to be displayed */
  75. /* center dialog box on screen */
  76. x = (COLS - width) / 2;
  77. y = (LINES - height) / 2;
  78. draw_shadow (stdscr, y, x, height, width);
  79. dialog = newwin (height, width, y, x);
  80. keypad (dialog, TRUE);
  81. /* Create window for text region, used for scrolling text */
  82. text = subwin (dialog, height - 4, width - 2, y + 1, x + 1);
  83. wattrset (text, dialog_attr);
  84. wbkgdset (text, dialog_attr & A_COLOR);
  85. keypad (text, TRUE);
  86. /* register the new window, along with its borders */
  87. draw_box (dialog, 0, 0, height, width, dialog_attr, border_attr);
  88. wattrset (dialog, border_attr);
  89. mvwaddch (dialog, height-3, 0, ACS_LTEE);
  90. for (i = 0; i < width - 2; i++)
  91. waddch (dialog, ACS_HLINE);
  92. wattrset (dialog, dialog_attr);
  93. wbkgdset (dialog, dialog_attr & A_COLOR);
  94. waddch (dialog, ACS_RTEE);
  95. if (title != NULL && strlen(title) >= width-2 ) {
  96. /* truncate long title -- mec */
  97. char * title2 = malloc(width-2+1);
  98. memcpy( title2, title, width-2 );
  99. title2[width-2] = '\0';
  100. title = title2;
  101. }
  102. if (title != NULL) {
  103. wattrset (dialog, title_attr);
  104. mvwaddch (dialog, 0, (width - strlen(title))/2 - 1, ' ');
  105. waddstr (dialog, (char *)title);
  106. waddch (dialog, ' ');
  107. }
  108. print_button (dialog, " Exit ", height - 2, width / 2 - 4, TRUE);
  109. wnoutrefresh (dialog);
  110. getyx (dialog, cur_y, cur_x); /* Save cursor position */
  111. /* Print first page of text */
  112. attr_clear (text, height - 4, width - 2, dialog_attr);
  113. print_page (text, height - 4, width - 2);
  114. print_position (dialog, height, width);
  115. wmove (dialog, cur_y, cur_x); /* Restore cursor position */
  116. wrefresh (dialog);
  117. while ((key != ESC) && (key != '\n')) {
  118. key = wgetch (dialog);
  119. switch (key) {
  120. case 'E': /* Exit */
  121. case 'e':
  122. case 'X':
  123. case 'x':
  124. delwin (dialog);
  125. free (buf);
  126. close (fd);
  127. return 0;
  128. case 'g': /* First page */
  129. case KEY_HOME:
  130. if (!begin_reached) {
  131. begin_reached = 1;
  132. /* First page not in buffer? */
  133. if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
  134. endwin ();
  135. fprintf (stderr,
  136. "\nError moving file pointer in dialog_textbox().\n");
  137. exit (-1);
  138. }
  139. if (fpos > bytes_read) { /* Yes, we have to read it in */
  140. if (lseek (fd, 0, SEEK_SET) == -1) {
  141. endwin ();
  142. fprintf (stderr, "\nError moving file pointer in "
  143. "dialog_textbox().\n");
  144. exit (-1);
  145. }
  146. if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
  147. endwin ();
  148. fprintf (stderr,
  149. "\nError reading file in dialog_textbox().\n");
  150. exit (-1);
  151. }
  152. buf[bytes_read] = '\0';
  153. }
  154. page = buf;
  155. print_page (text, height - 4, width - 2);
  156. print_position (dialog, height, width);
  157. wmove (dialog, cur_y, cur_x); /* Restore cursor position */
  158. wrefresh (dialog);
  159. }
  160. break;
  161. case 'G': /* Last page */
  162. case KEY_END:
  163. end_reached = 1;
  164. /* Last page not in buffer? */
  165. if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
  166. endwin ();
  167. fprintf (stderr,
  168. "\nError moving file pointer in dialog_textbox().\n");
  169. exit (-1);
  170. }
  171. if (fpos < file_size) { /* Yes, we have to read it in */
  172. if (lseek (fd, -BUF_SIZE, SEEK_END) == -1) {
  173. endwin ();
  174. fprintf (stderr,
  175. "\nError moving file pointer in dialog_textbox().\n");
  176. exit (-1);
  177. }
  178. if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
  179. endwin ();
  180. fprintf (stderr,
  181. "\nError reading file in dialog_textbox().\n");
  182. exit (-1);
  183. }
  184. buf[bytes_read] = '\0';
  185. }
  186. page = buf + bytes_read;
  187. back_lines (height - 4);
  188. print_page (text, height - 4, width - 2);
  189. print_position (dialog, height, width);
  190. wmove (dialog, cur_y, cur_x); /* Restore cursor position */
  191. wrefresh (dialog);
  192. break;
  193. case 'K': /* Previous line */
  194. case 'k':
  195. case KEY_UP:
  196. if (!begin_reached) {
  197. back_lines (page_length + 1);
  198. /* We don't call print_page() here but use scrolling to ensure
  199. faster screen update. However, 'end_reached' and
  200. 'page_length' should still be updated, and 'page' should
  201. point to start of next page. This is done by calling
  202. get_line() in the following 'for' loop. */
  203. scrollok (text, TRUE);
  204. wscrl (text, -1); /* Scroll text region down one line */
  205. scrollok (text, FALSE);
  206. page_length = 0;
  207. passed_end = 0;
  208. for (i = 0; i < height - 4; i++) {
  209. if (!i) {
  210. /* print first line of page */
  211. print_line (text, 0, width - 2);
  212. wnoutrefresh (text);
  213. } else
  214. /* Called to update 'end_reached' and 'page' */
  215. get_line ();
  216. if (!passed_end)
  217. page_length++;
  218. if (end_reached && !passed_end)
  219. passed_end = 1;
  220. }
  221. print_position (dialog, height, width);
  222. wmove (dialog, cur_y, cur_x); /* Restore cursor position */
  223. wrefresh (dialog);
  224. }
  225. break;
  226. case 'B': /* Previous page */
  227. case 'b':
  228. case KEY_PPAGE:
  229. if (begin_reached)
  230. break;
  231. back_lines (page_length + height - 4);
  232. print_page (text, height - 4, width - 2);
  233. print_position (dialog, height, width);
  234. wmove (dialog, cur_y, cur_x);
  235. wrefresh (dialog);
  236. break;
  237. case 'J': /* Next line */
  238. case 'j':
  239. case KEY_DOWN:
  240. if (!end_reached) {
  241. begin_reached = 0;
  242. scrollok (text, TRUE);
  243. scroll (text); /* Scroll text region up one line */
  244. scrollok (text, FALSE);
  245. print_line (text, height - 5, width - 2);
  246. wnoutrefresh (text);
  247. print_position (dialog, height, width);
  248. wmove (dialog, cur_y, cur_x); /* Restore cursor position */
  249. wrefresh (dialog);
  250. }
  251. break;
  252. case KEY_NPAGE: /* Next page */
  253. case ' ':
  254. if (end_reached)
  255. break;
  256. begin_reached = 0;
  257. print_page (text, height - 4, width - 2);
  258. print_position (dialog, height, width);
  259. wmove (dialog, cur_y, cur_x);
  260. wrefresh (dialog);
  261. break;
  262. case '0': /* Beginning of line */
  263. case 'H': /* Scroll left */
  264. case 'h':
  265. case KEY_LEFT:
  266. if (hscroll <= 0)
  267. break;
  268. if (key == '0')
  269. hscroll = 0;
  270. else
  271. hscroll--;
  272. /* Reprint current page to scroll horizontally */
  273. back_lines (page_length);
  274. print_page (text, height - 4, width - 2);
  275. wmove (dialog, cur_y, cur_x);
  276. wrefresh (dialog);
  277. break;
  278. case 'L': /* Scroll right */
  279. case 'l':
  280. case KEY_RIGHT:
  281. if (hscroll >= MAX_LEN)
  282. break;
  283. hscroll++;
  284. /* Reprint current page to scroll horizontally */
  285. back_lines (page_length);
  286. print_page (text, height - 4, width - 2);
  287. wmove (dialog, cur_y, cur_x);
  288. wrefresh (dialog);
  289. break;
  290. case ESC:
  291. break;
  292. }
  293. }
  294. delwin (dialog);
  295. free (buf);
  296. close (fd);
  297. return -1; /* ESC pressed */
  298. }
  299. /*
  300. * Go back 'n' lines in text file. Called by dialog_textbox().
  301. * 'page' will be updated to point to the desired line in 'buf'.
  302. */
  303. static void
  304. back_lines (int n)
  305. {
  306. int i, fpos;
  307. begin_reached = 0;
  308. /* We have to distinguish between end_reached and !end_reached
  309. since at end of file, the line is not ended by a '\n'.
  310. The code inside 'if' basically does a '--page' to move one
  311. character backward so as to skip '\n' of the previous line */
  312. if (!end_reached) {
  313. /* Either beginning of buffer or beginning of file reached? */
  314. if (page == buf) {
  315. if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
  316. endwin ();
  317. fprintf (stderr, "\nError moving file pointer in "
  318. "back_lines().\n");
  319. exit (-1);
  320. }
  321. if (fpos > bytes_read) { /* Not beginning of file yet */
  322. /* We've reached beginning of buffer, but not beginning of
  323. file yet, so read previous part of file into buffer.
  324. Note that we only move backward for BUF_SIZE/2 bytes,
  325. but not BUF_SIZE bytes to avoid re-reading again in
  326. print_page() later */
  327. /* Really possible to move backward BUF_SIZE/2 bytes? */
  328. if (fpos < BUF_SIZE / 2 + bytes_read) {
  329. /* No, move less then */
  330. if (lseek (fd, 0, SEEK_SET) == -1) {
  331. endwin ();
  332. fprintf (stderr, "\nError moving file pointer in "
  333. "back_lines().\n");
  334. exit (-1);
  335. }
  336. page = buf + fpos - bytes_read;
  337. } else { /* Move backward BUF_SIZE/2 bytes */
  338. if (lseek (fd, -(BUF_SIZE / 2 + bytes_read), SEEK_CUR)
  339. == -1) {
  340. endwin ();
  341. fprintf (stderr, "\nError moving file pointer "
  342. "in back_lines().\n");
  343. exit (-1);
  344. }
  345. page = buf + BUF_SIZE / 2;
  346. }
  347. if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
  348. endwin ();
  349. fprintf (stderr, "\nError reading file in back_lines().\n");
  350. exit (-1);
  351. }
  352. buf[bytes_read] = '\0';
  353. } else { /* Beginning of file reached */
  354. begin_reached = 1;
  355. return;
  356. }
  357. }
  358. if (*(--page) != '\n') { /* '--page' here */
  359. /* Something's wrong... */
  360. endwin ();
  361. fprintf (stderr, "\nInternal error in back_lines().\n");
  362. exit (-1);
  363. }
  364. }
  365. /* Go back 'n' lines */
  366. for (i = 0; i < n; i++)
  367. do {
  368. if (page == buf) {
  369. if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
  370. endwin ();
  371. fprintf (stderr,
  372. "\nError moving file pointer in back_lines().\n");
  373. exit (-1);
  374. }
  375. if (fpos > bytes_read) {
  376. /* Really possible to move backward BUF_SIZE/2 bytes? */
  377. if (fpos < BUF_SIZE / 2 + bytes_read) {
  378. /* No, move less then */
  379. if (lseek (fd, 0, SEEK_SET) == -1) {
  380. endwin ();
  381. fprintf (stderr, "\nError moving file pointer "
  382. "in back_lines().\n");
  383. exit (-1);
  384. }
  385. page = buf + fpos - bytes_read;
  386. } else { /* Move backward BUF_SIZE/2 bytes */
  387. if (lseek (fd, -(BUF_SIZE / 2 + bytes_read),
  388. SEEK_CUR) == -1) {
  389. endwin ();
  390. fprintf (stderr, "\nError moving file pointer"
  391. " in back_lines().\n");
  392. exit (-1);
  393. }
  394. page = buf + BUF_SIZE / 2;
  395. }
  396. if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
  397. endwin ();
  398. fprintf (stderr, "\nError reading file in "
  399. "back_lines().\n");
  400. exit (-1);
  401. }
  402. buf[bytes_read] = '\0';
  403. } else { /* Beginning of file reached */
  404. begin_reached = 1;
  405. return;
  406. }
  407. }
  408. } while (*(--page) != '\n');
  409. page++;
  410. }
  411. /*
  412. * Print a new page of text. Called by dialog_textbox().
  413. */
  414. static void
  415. print_page (WINDOW * win, int height, int width)
  416. {
  417. int i, passed_end = 0;
  418. page_length = 0;
  419. for (i = 0; i < height; i++) {
  420. print_line (win, i, width);
  421. if (!passed_end)
  422. page_length++;
  423. if (end_reached && !passed_end)
  424. passed_end = 1;
  425. }
  426. wnoutrefresh (win);
  427. }
  428. /*
  429. * Print a new line of text. Called by dialog_textbox() and print_page().
  430. */
  431. static void
  432. print_line (WINDOW * win, int row, int width)
  433. {
  434. int y, x;
  435. char *line;
  436. line = get_line ();
  437. line += MIN (strlen (line), hscroll); /* Scroll horizontally */
  438. wmove (win, row, 0); /* move cursor to correct line */
  439. waddch (win, ' ');
  440. waddnstr (win, line, MIN (strlen (line), width - 2));
  441. getyx (win, y, x);
  442. /* Clear 'residue' of previous line */
  443. #if OLD_NCURSES
  444. {
  445. int i;
  446. for (i = 0; i < width - x; i++)
  447. waddch (win, ' ');
  448. }
  449. #else
  450. wclrtoeol(win);
  451. #endif
  452. }
  453. /*
  454. * Return current line of text. Called by dialog_textbox() and print_line().
  455. * 'page' should point to start of current line before calling, and will be
  456. * updated to point to start of next line.
  457. */
  458. static char *
  459. get_line (void)
  460. {
  461. int i = 0, fpos;
  462. static char line[MAX_LEN + 1];
  463. end_reached = 0;
  464. while (*page != '\n') {
  465. if (*page == '\0') {
  466. /* Either end of file or end of buffer reached */
  467. if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
  468. endwin ();
  469. fprintf (stderr, "\nError moving file pointer in "
  470. "get_line().\n");
  471. exit (-1);
  472. }
  473. if (fpos < file_size) { /* Not end of file yet */
  474. /* We've reached end of buffer, but not end of file yet,
  475. so read next part of file into buffer */
  476. if ((bytes_read = read (fd, buf, BUF_SIZE)) == -1) {
  477. endwin ();
  478. fprintf (stderr, "\nError reading file in get_line().\n");
  479. exit (-1);
  480. }
  481. buf[bytes_read] = '\0';
  482. page = buf;
  483. } else {
  484. if (!end_reached)
  485. end_reached = 1;
  486. break;
  487. }
  488. } else if (i < MAX_LEN)
  489. line[i++] = *(page++);
  490. else {
  491. /* Truncate lines longer than MAX_LEN characters */
  492. if (i == MAX_LEN)
  493. line[i++] = '\0';
  494. page++;
  495. }
  496. }
  497. if (i <= MAX_LEN)
  498. line[i] = '\0';
  499. if (!end_reached)
  500. page++; /* move pass '\n' */
  501. return line;
  502. }
  503. /*
  504. * Print current position
  505. */
  506. static void
  507. print_position (WINDOW * win, int height, int width)
  508. {
  509. int fpos, percent;
  510. if ((fpos = lseek (fd, 0, SEEK_CUR)) == -1) {
  511. endwin ();
  512. fprintf (stderr, "\nError moving file pointer in print_position().\n");
  513. exit (-1);
  514. }
  515. wattrset (win, position_indicator_attr);
  516. wbkgdset (win, position_indicator_attr & A_COLOR);
  517. percent = !file_size ?
  518. 100 : ((fpos - bytes_read + page - buf) * 100) / file_size;
  519. wmove (win, height - 3, width - 9);
  520. wprintw (win, "(%3d%%)", percent);
  521. }