1
0

pad.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. pad
  5. ---
  6. ### Synopsis
  7. WINDOW *newpad(int nlines, int ncols);
  8. WINDOW *subpad(WINDOW *orig, int nlines, int ncols,
  9. int begy, int begx);
  10. int prefresh(WINDOW *win, int py, int px, int sy1, int sx1,
  11. int sy2, int sx2);
  12. int pnoutrefresh(WINDOW *w, int py, int px, int sy1, int sx1,
  13. int sy2, int sx2);
  14. int pechochar(WINDOW *pad, chtype ch);
  15. int pecho_wchar(WINDOW *pad, const cchar_t *wch);
  16. bool is_pad(const WINDOW *pad);
  17. ### Description
  18. A pad is a special kind of window, which is not restricted by the
  19. screen size, and is not necessarily associated with a particular part
  20. of the screen. You can use a pad when you need a large window, and
  21. only a part of the window will be on the screen at one time. Pads are
  22. not refreshed automatically (e.g., from scrolling or echoing of
  23. input). You can't call wrefresh() with a pad as an argument; use
  24. prefresh() or pnoutrefresh() instead. Note that these routines
  25. require additional parameters to specify the part of the pad to be
  26. displayed, and the location to use on the screen.
  27. newpad() creates a new pad data structure.
  28. subpad() creates a new sub-pad within a pad, at position (begy,
  29. begx), with dimensions of nlines lines and ncols columns. This
  30. position is relative to the pad, and not to the screen as with
  31. subwin. Changes to either the parent pad or sub-pad will affect both.
  32. When using sub-pads, you may need to call touchwin() before calling
  33. prefresh().
  34. pnoutrefresh() copies the specified pad to the virtual screen.
  35. prefresh() calls pnoutrefresh(), followed by doupdate().
  36. These routines are analogous to wnoutrefresh() and wrefresh(). (py,
  37. px) specifies the upper left corner of the part of the pad to be
  38. displayed; (sy1, sx1) and (sy2, sx2) describe the screen rectangle
  39. that will contain the selected part of the pad.
  40. pechochar() is functionally equivalent to addch() followed by a call
  41. to prefresh(), with the last-used coordinates and dimensions.
  42. pecho_wchar() is the wide-character version.
  43. is_pad() reports whether the specified window is a pad.
  44. ### Return Value
  45. All functions except is_pad() return OK on success and ERR on error.
  46. ### Portability
  47. X/Open ncurses NetBSD
  48. newpad Y Y Y
  49. subpad Y Y Y
  50. prefresh Y Y Y
  51. pnoutrefresh Y Y Y
  52. pechochar Y Y Y
  53. pecho_wchar Y Y Y
  54. is_pad - Y Y
  55. **man-end****************************************************************/
  56. #include <string.h>
  57. /* save values for pechochar() */
  58. static int save_pminrow, save_pmincol;
  59. static int save_sminrow, save_smincol, save_smaxrow, save_smaxcol;
  60. WINDOW *newpad(int nlines, int ncols)
  61. {
  62. WINDOW *win;
  63. PDC_LOG(("newpad() - called: lines=%d cols=%d\n", nlines, ncols));
  64. win = PDC_makenew(nlines, ncols, 0, 0);
  65. if (win)
  66. win = PDC_makelines(win);
  67. if (!win)
  68. return (WINDOW *)NULL;
  69. werase(win);
  70. win->_flags = _PAD;
  71. /* save default values in case pechochar() is the first call to
  72. prefresh(). */
  73. save_pminrow = 0;
  74. save_pmincol = 0;
  75. save_sminrow = 0;
  76. save_smincol = 0;
  77. save_smaxrow = min(LINES, nlines) - 1;
  78. save_smaxcol = min(COLS, ncols) - 1;
  79. return win;
  80. }
  81. WINDOW *subpad(WINDOW *orig, int nlines, int ncols, int begy, int begx)
  82. {
  83. WINDOW *win;
  84. int i;
  85. PDC_LOG(("subpad() - called: lines=%d cols=%d begy=%d begx=%d\n",
  86. nlines, ncols, begy, begx));
  87. if (!orig || !(orig->_flags & _PAD))
  88. return (WINDOW *)NULL;
  89. /* make sure window fits inside the original one */
  90. if (begy < 0 || begx < 0 ||
  91. (begy + nlines) > orig->_maxy ||
  92. (begx + ncols) > orig->_maxx)
  93. return (WINDOW *)NULL;
  94. if (!nlines)
  95. nlines = orig->_maxy - begy;
  96. if (!ncols)
  97. ncols = orig->_maxx - begx;
  98. win = PDC_makenew(nlines, ncols, begy, begx);
  99. if (!win)
  100. return (WINDOW *)NULL;
  101. /* initialize window variables */
  102. win->_attrs = orig->_attrs;
  103. win->_leaveit = orig->_leaveit;
  104. win->_scroll = orig->_scroll;
  105. win->_nodelay = orig->_nodelay;
  106. win->_use_keypad = orig->_use_keypad;
  107. win->_parent = orig;
  108. for (i = 0; i < nlines; i++)
  109. win->_y[i] = orig->_y[begy + i] + begx;
  110. win->_flags = _SUBPAD;
  111. /* save default values in case pechochar() is the first call
  112. to prefresh(). */
  113. save_pminrow = 0;
  114. save_pmincol = 0;
  115. save_sminrow = 0;
  116. save_smincol = 0;
  117. save_smaxrow = min(LINES, nlines) - 1;
  118. save_smaxcol = min(COLS, ncols) - 1;
  119. return win;
  120. }
  121. int prefresh(WINDOW *win, int py, int px, int sy1, int sx1, int sy2, int sx2)
  122. {
  123. PDC_LOG(("prefresh() - called\n"));
  124. if (pnoutrefresh(win, py, px, sy1, sx1, sy2, sx2) == ERR)
  125. return ERR;
  126. doupdate();
  127. return OK;
  128. }
  129. int pnoutrefresh(WINDOW *w, int py, int px, int sy1, int sx1, int sy2, int sx2)
  130. {
  131. int num_cols;
  132. int sline;
  133. int pline;
  134. PDC_LOG(("pnoutrefresh() - called\n"));
  135. if (py < 0)
  136. py = 0;
  137. if (px < 0)
  138. px = 0;
  139. if (sy1 < 0)
  140. sy1 = 0;
  141. if (sx1 < 0)
  142. sx1 = 0;
  143. if ((!w || !(w->_flags & (_PAD|_SUBPAD)) ||
  144. (sy2 >= LINES) || (sx2 >= COLS)) ||
  145. (sy2 < sy1) || (sx2 < sx1))
  146. return ERR;
  147. sline = sy1;
  148. pline = py;
  149. num_cols = min((sx2 - sx1 + 1), (w->_maxx - px));
  150. while (sline <= sy2)
  151. {
  152. if (pline < w->_maxy)
  153. {
  154. memcpy(curscr->_y[sline] + sx1, w->_y[pline] + px,
  155. num_cols * sizeof(chtype));
  156. if ((curscr->_firstch[sline] == _NO_CHANGE)
  157. || (curscr->_firstch[sline] > sx1))
  158. curscr->_firstch[sline] = sx1;
  159. if (sx2 > curscr->_lastch[sline])
  160. curscr->_lastch[sline] = sx2;
  161. w->_firstch[pline] = _NO_CHANGE; /* updated now */
  162. w->_lastch[pline] = _NO_CHANGE; /* updated now */
  163. }
  164. sline++;
  165. pline++;
  166. }
  167. if (w->_clear)
  168. {
  169. w->_clear = FALSE;
  170. curscr->_clear = TRUE;
  171. }
  172. /* position the cursor to the pad's current position if possible --
  173. is the pad current position going to end up displayed? if not,
  174. then don't move the cursor; if so, move it to the correct place */
  175. if (!w->_leaveit && w->_cury >= py && w->_curx >= px &&
  176. w->_cury <= py + (sy2 - sy1) && w->_curx <= px + (sx2 - sx1))
  177. {
  178. curscr->_cury = (w->_cury - py) + sy1;
  179. curscr->_curx = (w->_curx - px) + sx1;
  180. }
  181. return OK;
  182. }
  183. int pechochar(WINDOW *pad, chtype ch)
  184. {
  185. PDC_LOG(("pechochar() - called\n"));
  186. if (waddch(pad, ch) == ERR)
  187. return ERR;
  188. return prefresh(pad, save_pminrow, save_pmincol, save_sminrow,
  189. save_smincol, save_smaxrow, save_smaxcol);
  190. }
  191. #ifdef PDC_WIDE
  192. int pecho_wchar(WINDOW *pad, const cchar_t *wch)
  193. {
  194. PDC_LOG(("pecho_wchar() - called\n"));
  195. if (!wch || (waddch(pad, *wch) == ERR))
  196. return ERR;
  197. return prefresh(pad, save_pminrow, save_pmincol, save_sminrow,
  198. save_smincol, save_smaxrow, save_smaxcol);
  199. }
  200. #endif
  201. bool is_pad(const WINDOW *pad)
  202. {
  203. PDC_LOG(("is_pad() - called\n"));
  204. if (!pad)
  205. return FALSE;
  206. return (pad->_flags & _PAD) ? TRUE : FALSE;
  207. }