You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

windows.c 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include <curses.h>
  2. #include <stddef.h>
  3. #include <stdlib.h>
  4. #include "mucurses.h"
  5. /** @file
  6. *
  7. * MuCurses windows instance functions
  8. *
  9. */
  10. FILE_LICENCE ( GPL2_OR_LATER );
  11. /**
  12. * Delete a window
  13. *
  14. * @v *win pointer to window being deleted
  15. * @ret rc return status code
  16. */
  17. int delwin ( WINDOW *win ) {
  18. if ( win == NULL )
  19. return ERR;
  20. /* I think we should blank the region covered by the window -
  21. ncurses doesn't do this, but they have a buffer, so they
  22. may just be deleting from an offscreen context whereas we
  23. are guaranteed to be deleting something onscreen */
  24. wmove( win, 0, 0 );
  25. chtype killch = (chtype)' ';
  26. do {
  27. _wputch( win, killch, WRAP );
  28. } while ( win->curs_x + win->curs_y );
  29. free( win );
  30. wmove ( stdscr, 0, 0 );
  31. return OK;
  32. }
  33. /**
  34. * Create a new derived window
  35. *
  36. * @v parent parent window
  37. * @v nlines window height
  38. * @v ncols window width
  39. * @v begin_y window y origin (relative to parent)
  40. * @v begin_x window x origin (relative to parent)
  41. * @ret ptr return pointer to child window
  42. */
  43. WINDOW *derwin ( WINDOW *parent, int nlines, int ncols,
  44. int begin_y, int begin_x ) {
  45. WINDOW *child;
  46. if ( parent == NULL )
  47. return NULL;
  48. if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
  49. return NULL;
  50. if ( ( (unsigned)ncols > parent->width ) ||
  51. ( (unsigned)nlines > parent->height ) )
  52. return NULL;
  53. child->ori_y = parent->ori_y + begin_y;
  54. child->ori_x = parent->ori_x + begin_x;
  55. child->height = nlines;
  56. child->width = ncols;
  57. child->parent = parent;
  58. child->scr = parent->scr;
  59. return child;
  60. }
  61. /**
  62. * Create a duplicate of the specified window
  63. *
  64. * @v orig original window
  65. * @ret ptr pointer to duplicate window
  66. */
  67. WINDOW *dupwin ( WINDOW *orig ) {
  68. WINDOW *copy;
  69. if ( orig == NULL )
  70. return NULL;
  71. if ( ( copy = malloc( sizeof( WINDOW ) ) ) == NULL )
  72. return NULL;
  73. copy->scr = orig->scr;
  74. copy->attrs = orig->attrs;
  75. copy->ori_y = orig->ori_y;
  76. copy->ori_x = orig->ori_x;
  77. copy->curs_y = orig->curs_y;
  78. copy->curs_x = orig->curs_x;
  79. copy->height = orig->height;
  80. copy->width = orig->width;
  81. return copy;
  82. }
  83. /**
  84. * Move window origin to specified coordinates
  85. *
  86. * @v *win window to move
  87. * @v y Y position
  88. * @v x X position
  89. * @ret rc return status code
  90. */
  91. int mvwin ( WINDOW *win, int y, int x ) {
  92. if ( win == NULL )
  93. return ERR;
  94. if ( ( ( (unsigned)y + win->height ) > LINES ) ||
  95. ( ( (unsigned)x + win->width ) > COLS ) )
  96. return ERR;
  97. win->ori_y = y;
  98. win->ori_x = x;
  99. return OK;
  100. }
  101. /**
  102. * Create new WINDOW
  103. *
  104. * @v nlines number of lines
  105. * @v ncols number of columns
  106. * @v begin_y column origin
  107. * @v begin_x line origin
  108. * @ret *win return pointer to new window
  109. */
  110. WINDOW *newwin ( int nlines, int ncols, int begin_y, int begin_x ) {
  111. WINDOW *win;
  112. if ( ( win = malloc( sizeof(WINDOW) ) ) == NULL )
  113. return NULL;
  114. if ( ( (unsigned)( begin_y + nlines ) > stdscr->height ) &&
  115. ( (unsigned)( begin_x + ncols ) > stdscr->width ) )
  116. return NULL;
  117. win->ori_y = begin_y;
  118. win->ori_x = begin_x;
  119. win->height = nlines;
  120. win->width = ncols;
  121. win->scr = stdscr->scr;
  122. win->parent = stdscr;
  123. return win;
  124. }
  125. /**
  126. * Create a new sub-window
  127. *
  128. * @v orig parent window
  129. * @v nlines window height
  130. * @v ncols window width
  131. * @v begin_y window y origin (absolute)
  132. * @v begin_x window x origin (absolute)
  133. * @ret ptr return pointer to child window
  134. */
  135. WINDOW *subwin ( WINDOW *parent, int nlines, int ncols,
  136. int begin_y, int begin_x ) {
  137. WINDOW *child;
  138. if ( parent == NULL )
  139. return NULL;
  140. if ( ( child = malloc( sizeof( WINDOW ) ) ) == NULL )
  141. return NULL;
  142. child = newwin( nlines, ncols, begin_y, begin_x );
  143. child->parent = parent;
  144. child->scr = parent->scr;
  145. return child;
  146. }