bkgd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. bkgd
  5. ----
  6. ### Synopsis
  7. int bkgd(chtype ch);
  8. void bkgdset(chtype ch);
  9. chtype getbkgd(WINDOW *win);
  10. int wbkgd(WINDOW *win, chtype ch);
  11. void wbkgdset(WINDOW *win, chtype ch);
  12. int bkgrnd(const cchar_t *wch);
  13. void bkgrndset(const cchar_t *wch);
  14. int getbkgrnd(cchar_t *wch);
  15. int wbkgrnd(WINDOW *win, const cchar_t *wch);
  16. void wbkgrndset(WINDOW *win, const cchar_t *wch);
  17. int wgetbkgrnd(WINDOW *win, cchar_t *wch);
  18. ### Description
  19. bkgdset() and wbkgdset() manipulate the background of a window. The
  20. background is a chtype consisting of any combination of attributes
  21. and a character; it is combined with each chtype added or inserted to
  22. the window by waddch() or winsch(). Only the attribute part is used
  23. to set the background of non-blank characters, while both character
  24. and attributes are used for blank positions.
  25. bkgd() and wbkgd() not only change the background, but apply it
  26. immediately to every cell in the window.
  27. wbkgrnd(), wbkgrndset() and wgetbkgrnd() are the "wide-character"
  28. versions of these functions, taking a pointer to a cchar_t instead of
  29. a chtype. However, in PDCurses, cchar_t and chtype are the same.
  30. The attributes that are defined with the attrset()/attron() set of
  31. functions take precedence over the background attributes if there is
  32. a conflict (e.g., different color pairs).
  33. ### Return Value
  34. bkgd() and wbkgd() return OK, unless the window is NULL, in which
  35. case they return ERR.
  36. ### Portability
  37. X/Open ncurses NetBSD
  38. bkgd Y Y Y
  39. bkgdset Y Y Y
  40. getbkgd Y Y Y
  41. wbkgd Y Y Y
  42. wbkgdset Y Y Y
  43. bkgrnd Y Y Y
  44. bkgrndset Y Y Y
  45. getbkgrnd Y Y Y
  46. wbkgrnd Y Y Y
  47. wbkgrndset Y Y Y
  48. wgetbkgrnd Y Y Y
  49. **man-end****************************************************************/
  50. int wbkgd(WINDOW *win, chtype ch)
  51. {
  52. int x, y;
  53. chtype oldcolr, oldch, newcolr, newch, colr, attr;
  54. chtype oldattr = 0, newattr = 0;
  55. chtype *winptr;
  56. PDC_LOG(("wbkgd() - called\n"));
  57. if (!win)
  58. return ERR;
  59. if (win->_bkgd == ch)
  60. return OK;
  61. oldcolr = win->_bkgd & A_COLOR;
  62. if (oldcolr)
  63. oldattr = (win->_bkgd & A_ATTRIBUTES) ^ oldcolr;
  64. oldch = win->_bkgd & A_CHARTEXT;
  65. wbkgdset(win, ch);
  66. newcolr = win->_bkgd & A_COLOR;
  67. if (newcolr)
  68. newattr = (win->_bkgd & A_ATTRIBUTES) ^ newcolr;
  69. newch = win->_bkgd & A_CHARTEXT;
  70. /* what follows is what seems to occur in the System V
  71. implementation of this routine */
  72. for (y = 0; y < win->_maxy; y++)
  73. {
  74. for (x = 0; x < win->_maxx; x++)
  75. {
  76. winptr = win->_y[y] + x;
  77. ch = *winptr;
  78. /* determine the colors and attributes of the character read
  79. from the window */
  80. colr = ch & A_COLOR;
  81. attr = ch & (A_ATTRIBUTES ^ A_COLOR);
  82. /* if the color is the same as the old background color,
  83. then make it the new background color, otherwise leave it */
  84. if (colr == oldcolr)
  85. colr = newcolr;
  86. /* remove any attributes (non color) from the character that
  87. were part of the old background, then combine the
  88. remaining ones with the new background */
  89. attr ^= oldattr;
  90. attr |= newattr;
  91. /* change character if it is there because it was the old
  92. background character */
  93. ch &= A_CHARTEXT;
  94. if (ch == oldch)
  95. ch = newch;
  96. ch |= (attr | colr);
  97. *winptr = ch;
  98. }
  99. }
  100. touchwin(win);
  101. PDC_sync(win);
  102. return OK;
  103. }
  104. int bkgd(chtype ch)
  105. {
  106. PDC_LOG(("bkgd() - called\n"));
  107. return wbkgd(stdscr, ch);
  108. }
  109. void wbkgdset(WINDOW *win, chtype ch)
  110. {
  111. PDC_LOG(("wbkgdset() - called\n"));
  112. if (win)
  113. {
  114. if (!(ch & A_CHARTEXT))
  115. ch |= ' ';
  116. win->_bkgd = ch;
  117. }
  118. }
  119. void bkgdset(chtype ch)
  120. {
  121. PDC_LOG(("bkgdset() - called\n"));
  122. wbkgdset(stdscr, ch);
  123. }
  124. chtype getbkgd(WINDOW *win)
  125. {
  126. PDC_LOG(("getbkgd() - called\n"));
  127. return win ? win->_bkgd : (chtype)ERR;
  128. }
  129. #ifdef PDC_WIDE
  130. int wbkgrnd(WINDOW *win, const cchar_t *wch)
  131. {
  132. PDC_LOG(("wbkgrnd() - called\n"));
  133. return wch ? wbkgd(win, *wch) : ERR;
  134. }
  135. int bkgrnd(const cchar_t *wch)
  136. {
  137. PDC_LOG(("bkgrnd() - called\n"));
  138. return wbkgrnd(stdscr, wch);
  139. }
  140. void wbkgrndset(WINDOW *win, const cchar_t *wch)
  141. {
  142. PDC_LOG(("wbkgdset() - called\n"));
  143. if (wch)
  144. wbkgdset(win, *wch);
  145. }
  146. void bkgrndset(const cchar_t *wch)
  147. {
  148. PDC_LOG(("bkgrndset() - called\n"));
  149. wbkgrndset(stdscr, wch);
  150. }
  151. int wgetbkgrnd(WINDOW *win, cchar_t *wch)
  152. {
  153. PDC_LOG(("wgetbkgrnd() - called\n"));
  154. if (!win || !wch)
  155. return ERR;
  156. *wch = win->_bkgd;
  157. return OK;
  158. }
  159. int getbkgrnd(cchar_t *wch)
  160. {
  161. PDC_LOG(("getbkgrnd() - called\n"));
  162. return wgetbkgrnd(stdscr, wch);
  163. }
  164. #endif