1 | #include <stdlib.h>
|
---|
2 | #include <curses.h>
|
---|
3 | #include "curspriv.h"
|
---|
4 |
|
---|
5 | /****************************************************************/
|
---|
6 | /* Makenew() allocates all data for a new window except the */
|
---|
7 | /* Actual lines themselves. */
|
---|
8 | /****************************************************************/
|
---|
9 |
|
---|
10 | _PROTOTYPE(static WINDOW *makenew, (int nlines, int ncols, int begy,int begx));
|
---|
11 |
|
---|
12 | static WINDOW *makenew(num_lines, num_columns, begy, begx)
|
---|
13 | int num_lines, num_columns, begy, begx;
|
---|
14 | {
|
---|
15 | int i;
|
---|
16 | WINDOW *win;
|
---|
17 |
|
---|
18 | /* Allocate the window structure itself */
|
---|
19 | if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == NULL)
|
---|
20 | return((WINDOW *) ERR);
|
---|
21 |
|
---|
22 | /* Allocate the line pointer array */
|
---|
23 | if ((win->_line = (int **) calloc(num_lines, sizeof(int *))) == NULL) {
|
---|
24 | free(win);
|
---|
25 | return((WINDOW *) ERR);
|
---|
26 | }
|
---|
27 |
|
---|
28 | /* Allocate the minchng and maxchng arrays */
|
---|
29 | if ((win->_minchng = (int *) calloc(num_lines, sizeof(int))) == NULL) {
|
---|
30 | free(win);
|
---|
31 | free(win->_line);
|
---|
32 | return((WINDOW *) ERR);
|
---|
33 | }
|
---|
34 | if ((win->_maxchng = (int *) calloc(num_lines, sizeof(int))) == NULL) {
|
---|
35 | free(win);
|
---|
36 | free(win->_line);
|
---|
37 | free(win->_minchng);
|
---|
38 | return((WINDOW *) ERR);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /* Initialize window variables */
|
---|
42 | win->_curx = 0;
|
---|
43 | win->_cury = 0;
|
---|
44 | win->_maxy = num_lines - 1;
|
---|
45 | win->_maxx = num_columns - 1;
|
---|
46 | win->_begy = begy;
|
---|
47 | win->_begx = begx;
|
---|
48 | win->_flags = 0;
|
---|
49 | win->_attrs = ATR_NRM;
|
---|
50 | win->_tabsize = 8;
|
---|
51 | win->_clear = FALSE;
|
---|
52 | win->_leave = FALSE;
|
---|
53 | win->_scroll = FALSE;
|
---|
54 | win->_nodelay = FALSE;
|
---|
55 | win->_keypad = FALSE;
|
---|
56 | win->_regtop = 0;
|
---|
57 | win->_regbottom = num_lines - 1;
|
---|
58 |
|
---|
59 | /* Init to say window unchanged */
|
---|
60 | for (i = 0; i < num_lines; i++) {
|
---|
61 | win->_minchng[i] = 0;
|
---|
62 | win->_maxchng[i] = num_columns - 1;
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Set flags for window properties */
|
---|
66 | if ((begy + num_lines) == LINES) {
|
---|
67 | win->_flags |= _ENDLINE;
|
---|
68 | if ((begx == 0) && (num_columns == COLS) && (begy == 0))
|
---|
69 | win->_flags |= _FULLWIN;
|
---|
70 | } /* if */
|
---|
71 | if (((begy + num_lines) == LINES) && ((begx + num_columns) == COLS))
|
---|
72 | win->_flags |= _SCROLLWIN;
|
---|
73 | return(win);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /****************************************************************/
|
---|
78 | /* Newwin() creates a new window with size num_lines * num_co- */
|
---|
79 | /* Lumns, and origin begx,begy relative to the SCREEN. Special */
|
---|
80 | /* Case: if num_lines and/or num_columns is 0, the remainder of */
|
---|
81 | /* The screen is used. */
|
---|
82 | /****************************************************************/
|
---|
83 | WINDOW *newwin(num_lines, num_columns, begy, begx)
|
---|
84 | int num_lines, num_columns, begy, begx;
|
---|
85 | {
|
---|
86 | WINDOW *win;
|
---|
87 | int *ptr;
|
---|
88 | int i, j;
|
---|
89 |
|
---|
90 | if (num_lines == 0) num_lines = LINES - begy;
|
---|
91 | if (num_columns == 0) num_columns = COLS - begx;
|
---|
92 | if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
|
---|
93 | return((WINDOW *) ERR);
|
---|
94 | for (i = 0; i < num_lines; i++) { /* make and clear the lines */
|
---|
95 | if ((win->_line[i] = (int *)calloc(num_columns, sizeof(int))) == NULL){
|
---|
96 | for (j = 0; j < i; j++) /* if error, free all the data */
|
---|
97 | free(win->_line[j]);
|
---|
98 | free(win->_minchng);
|
---|
99 | free(win->_maxchng);
|
---|
100 | free(win->_line);
|
---|
101 | free(win);
|
---|
102 | return((WINDOW *) ERR);
|
---|
103 | } else {
|
---|
104 | for (ptr = win->_line[i]; ptr < win->_line[i] + num_columns;)
|
---|
105 | *ptr++ = ' ' | ATR_NRM;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return(win);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | /****************************************************************/
|
---|
113 | /* Subwin() creates a sub-window in the 'orig' window, with */
|
---|
114 | /* Size num_lines * num_columns, and with origin begx, begy */
|
---|
115 | /* Relative to the SCREEN. Special case: if num_lines and/or */
|
---|
116 | /* Num_columns is 0, the remainder of the original window is */
|
---|
117 | /* Used. The subwindow uses the original window's line buffers */
|
---|
118 | /* To store it's own lines. */
|
---|
119 | /****************************************************************/
|
---|
120 | WINDOW *subwin(orig, num_lines, num_columns, begy, begx)
|
---|
121 | WINDOW *orig;
|
---|
122 | int num_lines, num_columns, begy, begx;
|
---|
123 | {
|
---|
124 | WINDOW *win;
|
---|
125 | int i, j, k;
|
---|
126 |
|
---|
127 | /* Make sure window fits inside the original one */
|
---|
128 | if (begy < orig->_begy || begx < orig->_begx ||
|
---|
129 | (begy + num_lines) > (orig->_begy + orig->_maxy) ||
|
---|
130 | (begx + num_columns) > (orig->_begx + orig->_maxx) )
|
---|
131 | return((WINDOW *) ERR);
|
---|
132 |
|
---|
133 | if (num_lines == 0) num_lines = orig->_maxy - (begy - orig->_begy);
|
---|
134 | if (num_columns == 0) num_columns = orig->_maxx - (begx - orig->_begx);
|
---|
135 | if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
|
---|
136 | return((WINDOW *) ERR);
|
---|
137 |
|
---|
138 | /* Set line pointers the same as in the original window */
|
---|
139 | j = begy - orig->_begy;
|
---|
140 | k = begx - orig->_begx;
|
---|
141 | for (i = 0; i < num_lines; i++) win->_line[i] = (orig->_line[j++]) + k;
|
---|
142 | win->_flags |= _SUBWIN;
|
---|
143 | return(win);
|
---|
144 | }
|
---|