addch.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. addch
  5. -----
  6. ### Synopsis
  7. int addch(const chtype ch);
  8. int waddch(WINDOW *win, const chtype ch);
  9. int mvaddch(int y, int x, const chtype ch);
  10. int mvwaddch(WINDOW *win, int y, int x, const chtype ch);
  11. int echochar(const chtype ch);
  12. int wechochar(WINDOW *win, const chtype ch);
  13. int addrawch(chtype ch);
  14. int waddrawch(WINDOW *win, chtype ch);
  15. int mvaddrawch(int y, int x, chtype ch);
  16. int mvwaddrawch(WINDOW *win, int y, int x, chtype ch);
  17. int add_wch(const cchar_t *wch);
  18. int wadd_wch(WINDOW *win, const cchar_t *wch);
  19. int mvadd_wch(int y, int x, const cchar_t *wch);
  20. int mvwadd_wch(WINDOW *win, int y, int x, const cchar_t *wch);
  21. int echo_wchar(const cchar_t *wch);
  22. int wecho_wchar(WINDOW *win, const cchar_t *wch);
  23. ### Description
  24. addch() adds the chtype ch to the default window (stdscr) at the
  25. current cursor position, and advances the cursor. Note that chtypes
  26. can convey both text (a single character) and attributes, including a
  27. color pair. add_wch() is the wide-character version of this function,
  28. taking a pointer to a cchar_t instead of a chtype.
  29. waddch() is like addch(), but also lets you specify the window. (This
  30. is in fact the core output routine.) wadd_wch() is the wide version.
  31. mvaddch() moves the cursor to the specified (y, x) position, and adds
  32. ch to stdscr. mvadd_wch() is the wide version.
  33. mvwaddch() moves the cursor to the specified position and adds ch to
  34. the specified window. mvwadd_wch() is the wide version.
  35. echochar() adds ch to stdscr at the current cursor position and calls
  36. refresh(). echo_wchar() is the wide version.
  37. wechochar() adds ch to the specified window and calls wrefresh().
  38. wecho_wchar() is the wide version.
  39. addrawch(), waddrawch(), mvaddrawch() and mvwaddrawch() are PDCurses-
  40. specific wrappers for addch() etc. that disable the translation of
  41. control characters.
  42. The following applies to all these functions:
  43. If the cursor moves on to the right margin, an automatic newline is
  44. performed. If scrollok is enabled, and a character is added to the
  45. bottom right corner of the window, the scrolling region will be
  46. scrolled up one line. If scrolling is not allowed, ERR will be
  47. returned.
  48. If ch is a tab, newline, or backspace, the cursor will be moved
  49. appropriately within the window. If ch is a newline, the clrtoeol
  50. routine is called before the cursor is moved to the beginning of the
  51. next line. If newline mapping is off, the cursor will be moved to
  52. the next line, but the x coordinate will be unchanged. If ch is a
  53. tab the cursor is moved to the next tab position within the window.
  54. If ch is another control character, it will be drawn in the ^X
  55. notation. Calling the inch() routine after adding a control
  56. character returns the representation of the control character, not
  57. the control character.
  58. Video attributes can be combined with a character by ORing them into
  59. the parameter. Text, including attributes, can be copied from one
  60. place to another by using inch() and addch().
  61. Note that in PDCurses, for now, a cchar_t and a chtype are the same.
  62. The text field is 16 bits wide, and is treated as Unicode (UCS-2)
  63. when PDCurses is built with wide-character support (define PDC_WIDE).
  64. So, in functions that take a chtype, like addch(), both the wide and
  65. narrow versions will handle Unicode. But for portability, you should
  66. use the wide functions.
  67. ### Return Value
  68. All functions return OK on success and ERR on error.
  69. ### Portability
  70. X/Open ncurses NetBSD
  71. addch Y Y Y
  72. waddch Y Y Y
  73. mvaddch Y Y Y
  74. mvwaddch Y Y Y
  75. echochar Y Y Y
  76. wechochar Y Y Y
  77. add_wch Y Y Y
  78. wadd_wch Y Y Y
  79. mvadd_wch Y Y Y
  80. mvwadd_wch Y Y Y
  81. echo_wchar Y Y Y
  82. wecho_wchar Y Y Y
  83. addrawch - - -
  84. waddrawch - - -
  85. mvaddrawch - - -
  86. mvwaddrawch - - -
  87. **man-end****************************************************************/
  88. int waddch(WINDOW *win, const chtype ch)
  89. {
  90. int x, y;
  91. chtype text, attr;
  92. bool xlat;
  93. PDC_LOG(("waddch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
  94. win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));
  95. if (!win || !SP)
  96. return ERR;
  97. x = win->_curx;
  98. y = win->_cury;
  99. if (y > win->_maxy || x > win->_maxx || y < 0 || x < 0)
  100. return ERR;
  101. xlat = !SP->raw_out && !(ch & A_ALTCHARSET);
  102. text = ch & A_CHARTEXT;
  103. attr = ch & A_ATTRIBUTES;
  104. if (xlat && (text < ' ' || text == 0x7f))
  105. {
  106. int x2;
  107. switch (text)
  108. {
  109. case '\t':
  110. for (x2 = ((x / TABSIZE) + 1) * TABSIZE; x < x2; x++)
  111. {
  112. if (waddch(win, attr | ' ') == ERR)
  113. return ERR;
  114. /* if tab to next line, exit the loop */
  115. if (!win->_curx)
  116. break;
  117. }
  118. return OK;
  119. case '\n':
  120. /* if lf -> crlf */
  121. if (!SP->raw_out)
  122. x = 0;
  123. wclrtoeol(win);
  124. if (++y > win->_bmarg)
  125. {
  126. y--;
  127. if (wscrl(win, 1) == ERR)
  128. return ERR;
  129. }
  130. break;
  131. case '\b':
  132. /* don't back over left margin */
  133. if (--x < 0)
  134. case '\r':
  135. x = 0;
  136. break;
  137. case 0x7f:
  138. if (waddch(win, attr | '^') == ERR)
  139. return ERR;
  140. return waddch(win, attr | '?');
  141. default:
  142. /* handle control chars */
  143. if (waddch(win, attr | '^') == ERR)
  144. return ERR;
  145. return waddch(win, ch + '@');
  146. }
  147. }
  148. else
  149. {
  150. /* If the incoming character doesn't have its own attribute,
  151. then use the current attributes for the window. If it has
  152. attributes but not a color component, OR the attributes to
  153. the current attributes for the window. If it has a color
  154. component, use the attributes solely from the incoming
  155. character. */
  156. if (!(attr & A_COLOR))
  157. attr |= win->_attrs;
  158. /* wrs (4/10/93): Apply the same sort of logic for the window
  159. background, in that it only takes precedence if other color
  160. attributes are not there and that the background character
  161. will only print if the printing character is blank. */
  162. if (!(attr & A_COLOR))
  163. attr |= win->_bkgd & A_ATTRIBUTES;
  164. else
  165. attr |= win->_bkgd & (A_ATTRIBUTES ^ A_COLOR);
  166. if (text == ' ')
  167. text = win->_bkgd & A_CHARTEXT;
  168. /* Add the attribute back into the character. */
  169. text |= attr;
  170. /* Only change _firstch/_lastch if the character to be added is
  171. different from the character/attribute that is already in
  172. that position in the window. */
  173. if (win->_y[y][x] != text)
  174. {
  175. if (win->_firstch[y] == _NO_CHANGE)
  176. win->_firstch[y] = win->_lastch[y] = x;
  177. else
  178. if (x < win->_firstch[y])
  179. win->_firstch[y] = x;
  180. else
  181. if (x > win->_lastch[y])
  182. win->_lastch[y] = x;
  183. win->_y[y][x] = text;
  184. }
  185. if (++x >= win->_maxx)
  186. {
  187. /* wrap around test */
  188. x = 0;
  189. if (++y > win->_bmarg)
  190. {
  191. y--;
  192. if (wscrl(win, 1) == ERR)
  193. {
  194. PDC_sync(win);
  195. return ERR;
  196. }
  197. }
  198. }
  199. }
  200. win->_curx = x;
  201. win->_cury = y;
  202. if (win->_immed)
  203. wrefresh(win);
  204. if (win->_sync)
  205. wsyncup(win);
  206. return OK;
  207. }
  208. int addch(const chtype ch)
  209. {
  210. PDC_LOG(("addch() - called: ch=%x\n", ch));
  211. return waddch(stdscr, ch);
  212. }
  213. int mvaddch(int y, int x, const chtype ch)
  214. {
  215. PDC_LOG(("mvaddch() - called: y=%d x=%d ch=%x\n", y, x, ch));
  216. if (move(y,x) == ERR)
  217. return ERR;
  218. return waddch(stdscr, ch);
  219. }
  220. int mvwaddch(WINDOW *win, int y, int x, const chtype ch)
  221. {
  222. PDC_LOG(("mvwaddch() - called: win=%p y=%d x=%d ch=%d\n", win, y, x, ch));
  223. if (wmove(win, y, x) == ERR)
  224. return ERR;
  225. return waddch(win, ch);
  226. }
  227. int echochar(const chtype ch)
  228. {
  229. PDC_LOG(("echochar() - called: ch=%x\n", ch));
  230. return wechochar(stdscr, ch);
  231. }
  232. int wechochar(WINDOW *win, const chtype ch)
  233. {
  234. PDC_LOG(("wechochar() - called: win=%p ch=%x\n", win, ch));
  235. if (waddch(win, ch) == ERR)
  236. return ERR;
  237. return wrefresh(win);
  238. }
  239. int waddrawch(WINDOW *win, chtype ch)
  240. {
  241. PDC_LOG(("waddrawch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
  242. win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));
  243. if ((ch & A_CHARTEXT) < ' ' || (ch & A_CHARTEXT) == 0x7f)
  244. ch |= A_ALTCHARSET;
  245. return waddch(win, ch);
  246. }
  247. int addrawch(chtype ch)
  248. {
  249. PDC_LOG(("addrawch() - called: ch=%x\n", ch));
  250. return waddrawch(stdscr, ch);
  251. }
  252. int mvaddrawch(int y, int x, chtype ch)
  253. {
  254. PDC_LOG(("mvaddrawch() - called: y=%d x=%d ch=%d\n", y, x, ch));
  255. if (move(y, x) == ERR)
  256. return ERR;
  257. return waddrawch(stdscr, ch);
  258. }
  259. int mvwaddrawch(WINDOW *win, int y, int x, chtype ch)
  260. {
  261. PDC_LOG(("mvwaddrawch() - called: win=%p y=%d x=%d ch=%d\n",
  262. win, y, x, ch));
  263. if (wmove(win, y, x) == ERR)
  264. return ERR;
  265. return waddrawch(win, ch);
  266. }
  267. #ifdef PDC_WIDE
  268. int wadd_wch(WINDOW *win, const cchar_t *wch)
  269. {
  270. PDC_LOG(("wadd_wch() - called: win=%p wch=%x\n", win, *wch));
  271. return wch ? waddch(win, *wch) : ERR;
  272. }
  273. int add_wch(const cchar_t *wch)
  274. {
  275. PDC_LOG(("add_wch() - called: wch=%x\n", *wch));
  276. return wadd_wch(stdscr, wch);
  277. }
  278. int mvadd_wch(int y, int x, const cchar_t *wch)
  279. {
  280. PDC_LOG(("mvaddch() - called: y=%d x=%d wch=%x\n", y, x, *wch));
  281. if (move(y,x) == ERR)
  282. return ERR;
  283. return wadd_wch(stdscr, wch);
  284. }
  285. int mvwadd_wch(WINDOW *win, int y, int x, const cchar_t *wch)
  286. {
  287. PDC_LOG(("mvwaddch() - called: win=%p y=%d x=%d wch=%d\n",
  288. win, y, x, *wch));
  289. if (wmove(win, y, x) == ERR)
  290. return ERR;
  291. return wadd_wch(win, wch);
  292. }
  293. int echo_wchar(const cchar_t *wch)
  294. {
  295. PDC_LOG(("echo_wchar() - called: wch=%x\n", *wch));
  296. return wecho_wchar(stdscr, wch);
  297. }
  298. int wecho_wchar(WINDOW *win, const cchar_t *wch)
  299. {
  300. PDC_LOG(("wecho_wchar() - called: win=%p wch=%x\n", win, *wch));
  301. if (!wch || (wadd_wch(win, wch) == ERR))
  302. return ERR;
  303. return wrefresh(win);
  304. }
  305. #endif