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

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