1: /* 2: * allocate space for and set up defaults for a new window 3: * 4: * %G% (Berkeley) %W% 5: */ 6: 7: # include "curses.ext" 8: 9: short *calloc(); 10: WINDOW *malloc(); 11: 12: static WINDOW *makenew(); 13: 14: # undef nl /* don't need it here, and it interferes */ 15: 16: WINDOW * 17: newwin(num_lines, num_cols, begy, begx) 18: int num_lines, num_cols, begy, begx; 19: { 20: reg WINDOW *win; 21: reg char *sp; 22: reg int i, by, bx, nl, nc; 23: 24: by = begy; 25: bx = begx; 26: nl = num_lines; 27: nc = num_cols; 28: 29: if (nl == 0) 30: nl = LINES - by; 31: if (nc == 0) 32: nc = COLS - bx; 33: if ((win = makenew(nl, nc, by, bx)) == NULL) 34: return ERR; 35: for (i = 0; i < nl; i++) 36: if ((win->_y[i] = (char *) calloc(nc, sizeof (char))) == NULL) { 37: reg int j; 38: 39: for (j = 0; j < i; j++) 40: cfree(win->_y[j]); 41: cfree(win->_firstch); 42: cfree(win->_lastch); 43: cfree(win->_y); 44: cfree(win); 45: return ERR; 46: } 47: else 48: for (sp = win->_y[i]; sp < win->_y[i] + nc; ) 49: *sp++ = ' '; 50: win->_nextp = win; 51: return win; 52: } 53: 54: WINDOW * 55: subwin(orig, num_lines, num_cols, begy, begx) 56: reg WINDOW *orig; 57: int num_lines, num_cols, begy, begx; { 58: 59: reg int i; 60: reg WINDOW *win; 61: reg int by, bx, nl, nc; 62: reg int j, k; 63: 64: by = begy; 65: bx = begx; 66: nl = num_lines; 67: nc = num_cols; 68: 69: /* 70: * make sure window fits inside the original one 71: */ 72: # ifdef DEBUG 73: fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx); 74: # endif 75: if (by < orig->_begy || bx < orig->_begx 76: || by + nl > orig->_maxy + orig->_begy 77: || bx + nc > orig->_maxx + orig->_begx) { 78: fprintf(stderr, "returning ERR (1)\n"); 79: fprintf(stderr, "SUBWIN(begx = %d, begy = %d,maxx = %d, maxy = %d, nl = %d, nc = %d, by = %d, bx = %d)\n", orig->_begx,orig->_begy,orig->_maxx,orig->_maxy, nl, nc, by, bx); 80: return ERR; 81: } 82: if (nl == 0) 83: nl = orig->_maxy + orig->_begy - by; 84: if (nc == 0) 85: nc = orig->_maxx + orig->_begx - bx; 86: if ((win = makenew(nl, nc, by, bx)) == NULL) { 87: fprintf(stderr, "returning ERR (2)\n"); 88: return ERR; 89: } 90: j = by - orig->_begy; 91: k = bx - orig->_begx; 92: for (i = 0; i < nl; i++) 93: win->_y[i] = &orig->_y[j++][k]; 94: win->_nextp = orig->_nextp; 95: orig->_nextp = win; 96: win->_orig = orig; 97: return win; 98: } 99: 100: /* 101: * This routine sets up a window buffer and returns a pointer to it. 102: */ 103: static WINDOW * 104: makenew(num_lines, num_cols, begy, begx) 105: int num_lines, num_cols, begy, begx; { 106: 107: reg int i; 108: reg WINDOW *win; 109: reg int by, bx, nl, nc; 110: 111: by = begy; 112: bx = begx; 113: nl = num_lines; 114: nc = num_cols; 115: 116: # ifdef DEBUG 117: fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx); 118: # endif 119: if ((win = (WINDOW *) calloc(1, sizeof (WINDOW))) == NULL) 120: return NULL; 121: # ifdef DEBUG 122: fprintf(outf, "MAKENEW: nl = %d\n", nl); 123: # endif 124: if ((win->_y = (char **) calloc(nl, sizeof (char *))) == NULL) { 125: cfree(win); 126: return NULL; 127: } 128: if ((win->_firstch = calloc(nl, sizeof (short))) == NULL) { 129: cfree(win); 130: cfree(win->_y); 131: return NULL; 132: } 133: if ((win->_lastch = calloc(nl, sizeof (short))) == NULL) { 134: cfree(win); 135: cfree(win->_y); 136: cfree(win->_firstch); 137: return NULL; 138: } 139: # ifdef DEBUG 140: fprintf(outf, "MAKENEW: nc = %d\n", nc); 141: # endif 142: win->_cury = win->_curx = 0; 143: win->_clear = (nl == LINES && nc == COLS); 144: win->_maxy = nl; 145: win->_maxx = nc; 146: win->_begy = by; 147: win->_begx = bx; 148: win->_flags = 0; 149: win->_scroll = win->_leave = FALSE; 150: for (i = 0; i < nl; i++) 151: win->_firstch[i] = win->_lastch[i] = _NOCHANGE; 152: if (bx + nc == COLS) { 153: win->_flags |= _ENDLINE; 154: if (bx == 0 && nl == LINES && by == 0) 155: win->_flags |= _FULLWIN; 156: if (by + nl == LINES) 157: win->_flags |= _SCROLLWIN; 158: } 159: # ifdef DEBUG 160: fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear); 161: fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave); 162: fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll); 163: fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags); 164: fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy); 165: fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx); 166: fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy); 167: fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx); 168: # endif 169: return win; 170: }