overlay.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* PDCurses */
  2. #include <curspriv.h>
  3. /*man-start**************************************************************
  4. overlay
  5. -------
  6. ### Synopsis
  7. int overlay(const WINDOW *src_w, WINDOW *dst_w)
  8. int overwrite(const WINDOW *src_w, WINDOW *dst_w)
  9. int copywin(const WINDOW *src_w, WINDOW *dst_w, int src_tr,
  10. int src_tc, int dst_tr, int dst_tc, int dst_br,
  11. int dst_bc, int _overlay)
  12. ### Description
  13. overlay() and overwrite() copy all the text from src_w into dst_w.
  14. The windows need not be the same size. Those characters in the source
  15. window that intersect with the destination window are copied, so that
  16. the characters appear in the same physical position on the screen.
  17. The difference between the two functions is that overlay() is non-
  18. destructive (blanks are not copied) while overwrite() is destructive
  19. (blanks are copied).
  20. copywin() is similar, but doesn't require that the two windows
  21. overlap. The arguments src_tc and src_tr specify the top left corner
  22. of the region to be copied. dst_tc, dst_tr, dst_br, and dst_bc
  23. specify the region within the destination window to copy to. The
  24. argument "overlay", if TRUE, indicates that the copy is done non-
  25. destructively (as in overlay()); blanks in the source window are not
  26. copied to the destination window. When overlay is FALSE, blanks are
  27. copied.
  28. ### Return Value
  29. All functions return OK on success and ERR on error.
  30. ### Portability
  31. X/Open ncurses NetBSD
  32. overlay Y Y Y
  33. overwrite Y Y Y
  34. copywin Y Y Y
  35. **man-end****************************************************************/
  36. /* Thanks to Andreas Otte <venn@@uni-paderborn.de> for the
  37. corrected overlay()/overwrite() behavior. */
  38. static int _copy_win(const WINDOW *src_w, WINDOW *dst_w, int src_tr,
  39. int src_tc, int src_br, int src_bc, int dst_tr,
  40. int dst_tc, bool _overlay)
  41. {
  42. int col, line, y1, fc, *minchng, *maxchng;
  43. chtype *w1ptr, *w2ptr;
  44. int lc = 0;
  45. int xdiff = src_bc - src_tc;
  46. int ydiff = src_br - src_tr;
  47. if (!src_w || !dst_w)
  48. return ERR;
  49. minchng = dst_w->_firstch;
  50. maxchng = dst_w->_lastch;
  51. for (y1 = 0; y1 < dst_tr; y1++)
  52. {
  53. minchng++;
  54. maxchng++;
  55. }
  56. for (line = 0; line < ydiff; line++)
  57. {
  58. w1ptr = src_w->_y[line + src_tr] + src_tc;
  59. w2ptr = dst_w->_y[line + dst_tr] + dst_tc;
  60. fc = _NO_CHANGE;
  61. for (col = 0; col < xdiff; col++)
  62. {
  63. if ((*w1ptr) != (*w2ptr) &&
  64. !((*w1ptr & A_CHARTEXT) == ' ' && _overlay))
  65. {
  66. *w2ptr = *w1ptr;
  67. if (fc == _NO_CHANGE)
  68. fc = col + dst_tc;
  69. lc = col + dst_tc;
  70. }
  71. w1ptr++;
  72. w2ptr++;
  73. }
  74. if (*minchng == _NO_CHANGE)
  75. {
  76. *minchng = fc;
  77. *maxchng = lc;
  78. }
  79. else if (fc != _NO_CHANGE)
  80. {
  81. if (fc < *minchng)
  82. *minchng = fc;
  83. if (lc > *maxchng)
  84. *maxchng = lc;
  85. }
  86. minchng++;
  87. maxchng++;
  88. }
  89. return OK;
  90. }
  91. int _copy_overlap(const WINDOW *src_w, WINDOW *dst_w, bool overlay)
  92. {
  93. int first_line, first_col, last_line, last_col;
  94. int src_start_x, src_start_y, dst_start_x, dst_start_y;
  95. int xdiff, ydiff;
  96. if (!src_w || !dst_w)
  97. return ERR;
  98. first_col = max(dst_w->_begx, src_w->_begx);
  99. first_line = max(dst_w->_begy, src_w->_begy);
  100. last_col = min(src_w->_begx + src_w->_maxx, dst_w->_begx + dst_w->_maxx);
  101. last_line = min(src_w->_begy + src_w->_maxy, dst_w->_begy + dst_w->_maxy);
  102. /* determine the overlapping region of the two windows in real
  103. coordinates */
  104. /* if no overlapping region, do nothing */
  105. if ((last_col < first_col) || (last_line < first_line))
  106. return OK;
  107. /* size of overlapping region */
  108. xdiff = last_col - first_col;
  109. ydiff = last_line - first_line;
  110. if (src_w->_begx <= dst_w->_begx)
  111. {
  112. src_start_x = dst_w->_begx - src_w->_begx;
  113. dst_start_x = 0;
  114. }
  115. else
  116. {
  117. dst_start_x = src_w->_begx - dst_w->_begx;
  118. src_start_x = 0;
  119. }
  120. if (src_w->_begy <= dst_w->_begy)
  121. {
  122. src_start_y = dst_w->_begy - src_w->_begy;
  123. dst_start_y = 0;
  124. }
  125. else
  126. {
  127. dst_start_y = src_w->_begy - dst_w->_begy;
  128. src_start_y = 0;
  129. }
  130. return _copy_win(src_w, dst_w, src_start_y, src_start_x,
  131. src_start_y + ydiff, src_start_x + xdiff,
  132. dst_start_y, dst_start_x, overlay);
  133. }
  134. int overlay(const WINDOW *src_w, WINDOW *dst_w)
  135. {
  136. PDC_LOG(("overlay() - called\n"));
  137. return _copy_overlap(src_w, dst_w, TRUE);
  138. }
  139. int overwrite(const WINDOW *src_w, WINDOW *dst_w)
  140. {
  141. PDC_LOG(("overwrite() - called\n"));
  142. return _copy_overlap(src_w, dst_w, FALSE);
  143. }
  144. int copywin(const WINDOW *src_w, WINDOW *dst_w, int src_tr, int src_tc,
  145. int dst_tr, int dst_tc, int dst_br, int dst_bc, int _overlay)
  146. {
  147. int src_end_x, src_end_y;
  148. int src_rows, src_cols, dst_rows, dst_cols;
  149. int min_rows, min_cols;
  150. PDC_LOG(("copywin() - called\n"));
  151. if (!src_w || !dst_w || dst_w == curscr || dst_br >= dst_w->_maxy
  152. || dst_bc >= dst_w->_maxx || dst_tr < 0 || dst_tc < 0)
  153. return ERR;
  154. src_rows = src_w->_maxy - src_tr;
  155. src_cols = src_w->_maxx - src_tc;
  156. dst_rows = dst_br - dst_tr + 1;
  157. dst_cols = dst_bc - dst_tc + 1;
  158. min_rows = min(src_rows, dst_rows);
  159. min_cols = min(src_cols, dst_cols);
  160. src_end_y = src_tr + min_rows;
  161. src_end_x = src_tc + min_cols;
  162. return _copy_win(src_w, dst_w, src_tr, src_tc, src_end_y, src_end_x,
  163. dst_tr, dst_tc, _overlay);
  164. }