insch.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. insch
  5. -----
  6. ### Synopsis
  7. int insch(chtype ch);
  8. int winsch(WINDOW *win, chtype ch);
  9. int mvinsch(int y, int x, chtype ch);
  10. int mvwinsch(WINDOW *win, int y, int x, chtype ch);
  11. int insrawch(chtype ch);
  12. int winsrawch(WINDOW *win, chtype ch);
  13. int mvinsrawch(int y, int x, chtype ch);
  14. int mvwinsrawch(WINDOW *win, int y, int x, chtype ch);
  15. int ins_wch(const cchar_t *wch);
  16. int wins_wch(WINDOW *win, const cchar_t *wch);
  17. int mvins_wch(int y, int x, const cchar_t *wch);
  18. int mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch);
  19. ### Description
  20. The insch() functions insert a chtype into the window at the current
  21. or specified cursor position. The cursor is NOT advanced. A newline
  22. is equivalent to clrtoeol(); tabs are expanded; other control
  23. characters are converted as with unctrl().
  24. The ins_wch() functions are the wide-character equivalents, taking
  25. cchar_t pointers rather than chtypes.
  26. Video attributes can be combined with a character by ORing them into
  27. the parameter. Text, including attributes, can be copied from one
  28. place to another using inch() and insch().
  29. insrawch() etc. are PDCurses-specific wrappers for insch() etc. that
  30. disable the translation of control characters.
  31. ### Return Value
  32. All functions return OK on success and ERR on error.
  33. ### Portability
  34. X/Open ncurses NetBSD
  35. insch Y Y Y
  36. winsch Y Y Y
  37. mvinsch Y Y Y
  38. mvwinsch Y Y Y
  39. ins_wch Y Y Y
  40. wins_wch Y Y Y
  41. mvins_wch Y Y Y
  42. mvwins_wch Y Y Y
  43. insrawch - - -
  44. winsrawch - - -
  45. **man-end****************************************************************/
  46. #include <string.h>
  47. int winsch(WINDOW *win, chtype ch)
  48. {
  49. int x, y;
  50. chtype attr;
  51. bool xlat;
  52. PDC_LOG(("winsch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
  53. win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));
  54. if (!win)
  55. return ERR;
  56. x = win->_curx;
  57. y = win->_cury;
  58. if (y > win->_maxy || x > win->_maxx || y < 0 || x < 0)
  59. return ERR;
  60. xlat = !SP->raw_out && !(ch & A_ALTCHARSET);
  61. attr = ch & A_ATTRIBUTES;
  62. ch &= A_CHARTEXT;
  63. if (xlat && (ch < ' ' || ch == 0x7f))
  64. {
  65. int x2;
  66. switch (ch)
  67. {
  68. case '\t':
  69. for (x2 = ((x / TABSIZE) + 1) * TABSIZE; x < x2; x++)
  70. {
  71. if (winsch(win, attr | ' ') == ERR)
  72. return ERR;
  73. }
  74. return OK;
  75. case '\n':
  76. wclrtoeol(win);
  77. break;
  78. case 0x7f:
  79. if (winsch(win, attr | '?') == ERR)
  80. return ERR;
  81. return winsch(win, attr | '^');
  82. default:
  83. /* handle control chars */
  84. if (winsch(win, attr | (ch + '@')) == ERR)
  85. return ERR;
  86. return winsch(win, attr | '^');
  87. }
  88. }
  89. else
  90. {
  91. int maxx;
  92. chtype *temp;
  93. /* If the incoming character doesn't have its own attribute,
  94. then use the current attributes for the window. If it has
  95. attributes but not a color component, OR the attributes to
  96. the current attributes for the window. If it has a color
  97. component, use the attributes solely from the incoming
  98. character. */
  99. if (!(attr & A_COLOR))
  100. attr |= win->_attrs;
  101. /* wrs (4/10/93): Apply the same sort of logic for the window
  102. background, in that it only takes precedence if other color
  103. attributes are not there and that the background character
  104. will only print if the printing character is blank. */
  105. if (!(attr & A_COLOR))
  106. attr |= win->_bkgd & A_ATTRIBUTES;
  107. else
  108. attr |= win->_bkgd & (A_ATTRIBUTES ^ A_COLOR);
  109. if (ch == ' ')
  110. ch = win->_bkgd & A_CHARTEXT;
  111. /* Add the attribute back into the character. */
  112. ch |= attr;
  113. maxx = win->_maxx;
  114. temp = &win->_y[y][x];
  115. memmove(temp + 1, temp, (maxx - x - 1) * sizeof(chtype));
  116. win->_lastch[y] = maxx - 1;
  117. if ((win->_firstch[y] == _NO_CHANGE) || (win->_firstch[y] > x))
  118. win->_firstch[y] = x;
  119. *temp = ch;
  120. }
  121. PDC_sync(win);
  122. return OK;
  123. }
  124. int insch(chtype ch)
  125. {
  126. PDC_LOG(("insch() - called\n"));
  127. return winsch(stdscr, ch);
  128. }
  129. int mvinsch(int y, int x, chtype ch)
  130. {
  131. PDC_LOG(("mvinsch() - called\n"));
  132. if (move(y, x) == ERR)
  133. return ERR;
  134. return winsch(stdscr, ch);
  135. }
  136. int mvwinsch(WINDOW *win, int y, int x, chtype ch)
  137. {
  138. PDC_LOG(("mvwinsch() - called\n"));
  139. if (wmove(win, y, x) == ERR)
  140. return ERR;
  141. return winsch(win, ch);
  142. }
  143. int winsrawch(WINDOW *win, chtype ch)
  144. {
  145. PDC_LOG(("winsrawch() - called: win=%p ch=%x "
  146. "(char=%c attr=0x%x)\n", win, ch,
  147. ch & A_CHARTEXT, ch & A_ATTRIBUTES));
  148. if ((ch & A_CHARTEXT) < ' ' || (ch & A_CHARTEXT) == 0x7f)
  149. ch |= A_ALTCHARSET;
  150. return winsch(win, ch);
  151. }
  152. int insrawch(chtype ch)
  153. {
  154. PDC_LOG(("insrawch() - called\n"));
  155. return winsrawch(stdscr, ch);
  156. }
  157. int mvinsrawch(int y, int x, chtype ch)
  158. {
  159. PDC_LOG(("mvinsrawch() - called\n"));
  160. if (move(y, x) == ERR)
  161. return ERR;
  162. return winsrawch(stdscr, ch);
  163. }
  164. int mvwinsrawch(WINDOW *win, int y, int x, chtype ch)
  165. {
  166. PDC_LOG(("mvwinsrawch() - called\n"));
  167. if (wmove(win, y, x) == ERR)
  168. return ERR;
  169. return winsrawch(win, ch);
  170. }
  171. #ifdef PDC_WIDE
  172. int wins_wch(WINDOW *win, const cchar_t *wch)
  173. {
  174. PDC_LOG(("wins_wch() - called\n"));
  175. return wch ? winsch(win, *wch) : ERR;
  176. }
  177. int ins_wch(const cchar_t *wch)
  178. {
  179. PDC_LOG(("ins_wch() - called\n"));
  180. return wins_wch(stdscr, wch);
  181. }
  182. int mvins_wch(int y, int x, const cchar_t *wch)
  183. {
  184. PDC_LOG(("mvins_wch() - called\n"));
  185. if (move(y, x) == ERR)
  186. return ERR;
  187. return wins_wch(stdscr, wch);
  188. }
  189. int mvwins_wch(WINDOW *win, int y, int x, const cchar_t *wch)
  190. {
  191. PDC_LOG(("mvwins_wch() - called\n"));
  192. if (wmove(win, y, x) == ERR)
  193. return ERR;
  194. return wins_wch(win, wch);
  195. }
  196. #endif