1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 7*0Sstevel@tonic-gate /* All Rights Reserved */ 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gate /* 10*0Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 11*0Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 12*0Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 13*0Sstevel@tonic-gate */ 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate /*LINTLIBRARY*/ 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate #ifndef lint 20*0Sstevel@tonic-gate static char 21*0Sstevel@tonic-gate sccsid[] = "@(#)newwin.c 1.7 89/07/13 SMI"; /* from UCB 5.1 85/06/07 */ 22*0Sstevel@tonic-gate #endif /* not lint */ 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gate /* 25*0Sstevel@tonic-gate * allocate space for and set up defaults for a new window 26*0Sstevel@tonic-gate * 27*0Sstevel@tonic-gate */ 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gate #include "curses.ext" 30*0Sstevel@tonic-gate #include <malloc.h> 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate #define SMALLOC (short *)malloc 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate /* forward declaration */ 35*0Sstevel@tonic-gate static WINDOW *makenew(int, int, int, int); 36*0Sstevel@tonic-gate 37*0Sstevel@tonic-gate #undef nl /* don't need it here, and it interferes */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate WINDOW * 40*0Sstevel@tonic-gate newwin(int num_lines, int num_cols, int begy, int begx) 41*0Sstevel@tonic-gate { 42*0Sstevel@tonic-gate WINDOW *win; 43*0Sstevel@tonic-gate char *sp; 44*0Sstevel@tonic-gate int i, by, bx, nl, nc; 45*0Sstevel@tonic-gate int j; 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate by = begy; 48*0Sstevel@tonic-gate bx = begx; 49*0Sstevel@tonic-gate nl = num_lines; 50*0Sstevel@tonic-gate nc = num_cols; 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate if (nl == 0) 53*0Sstevel@tonic-gate nl = LINES - by; 54*0Sstevel@tonic-gate if (nc == 0) 55*0Sstevel@tonic-gate nc = COLS - bx; 56*0Sstevel@tonic-gate if ((win = makenew(nl, nc, by, bx)) == NULL) 57*0Sstevel@tonic-gate return (ERR); 58*0Sstevel@tonic-gate if ((win->_firstch = SMALLOC(nl * sizeof (win->_firstch[0]))) == NULL) { 59*0Sstevel@tonic-gate free(win->_y); 60*0Sstevel@tonic-gate free(win); 61*0Sstevel@tonic-gate return (NULL); 62*0Sstevel@tonic-gate } 63*0Sstevel@tonic-gate if ((win->_lastch = SMALLOC(nl * sizeof (win->_lastch[0]))) == NULL) { 64*0Sstevel@tonic-gate free(win->_y); 65*0Sstevel@tonic-gate free(win->_firstch); 66*0Sstevel@tonic-gate free(win); 67*0Sstevel@tonic-gate return (NULL); 68*0Sstevel@tonic-gate } 69*0Sstevel@tonic-gate win->_nextp = win; 70*0Sstevel@tonic-gate for (i = 0; i < nl; i++) { 71*0Sstevel@tonic-gate win->_firstch[i] = _NOCHANGE; 72*0Sstevel@tonic-gate win->_lastch[i] = _NOCHANGE; 73*0Sstevel@tonic-gate } 74*0Sstevel@tonic-gate for (i = 0; i < nl; i++) 75*0Sstevel@tonic-gate if ((win->_y[i] = malloc(nc * sizeof (win->_y[0]))) == NULL) { 76*0Sstevel@tonic-gate for (j = 0; j < i; j++) 77*0Sstevel@tonic-gate free(win->_y[j]); 78*0Sstevel@tonic-gate free(win->_firstch); 79*0Sstevel@tonic-gate free(win->_lastch); 80*0Sstevel@tonic-gate free(win->_y); 81*0Sstevel@tonic-gate free(win); 82*0Sstevel@tonic-gate return (ERR); 83*0Sstevel@tonic-gate } 84*0Sstevel@tonic-gate else 85*0Sstevel@tonic-gate for (sp = win->_y[i]; sp < win->_y[i] + nc; ) 86*0Sstevel@tonic-gate *sp++ = ' '; 87*0Sstevel@tonic-gate win->_ch_off = 0; 88*0Sstevel@tonic-gate #ifdef DEBUG 89*0Sstevel@tonic-gate fprintf(outf, "NEWWIN: win->_ch_off = %d\n", win->_ch_off); 90*0Sstevel@tonic-gate #endif 91*0Sstevel@tonic-gate return (win); 92*0Sstevel@tonic-gate } 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate WINDOW * 95*0Sstevel@tonic-gate subwin(WINDOW *orig, int num_lines, int num_cols, int begy, int begx) 96*0Sstevel@tonic-gate { 97*0Sstevel@tonic-gate WINDOW *win; 98*0Sstevel@tonic-gate int by, bx, nl, nc; 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate by = begy; 101*0Sstevel@tonic-gate bx = begx; 102*0Sstevel@tonic-gate nl = num_lines; 103*0Sstevel@tonic-gate nc = num_cols; 104*0Sstevel@tonic-gate 105*0Sstevel@tonic-gate /* 106*0Sstevel@tonic-gate * make sure window fits inside the original one 107*0Sstevel@tonic-gate */ 108*0Sstevel@tonic-gate #ifdef DEBUG 109*0Sstevel@tonic-gate fprintf(outf, "SUBWIN(%0.2o, %d, %d, %d, %d)\n", orig, nl, nc, by, bx); 110*0Sstevel@tonic-gate #endif 111*0Sstevel@tonic-gate if (by < orig->_begy || bx < orig->_begx || 112*0Sstevel@tonic-gate by + nl > orig->_maxy + orig->_begy || 113*0Sstevel@tonic-gate bx + nc > orig->_maxx + orig->_begx) 114*0Sstevel@tonic-gate return (ERR); 115*0Sstevel@tonic-gate if (nl == 0) 116*0Sstevel@tonic-gate nl = orig->_maxy + orig->_begy - by; 117*0Sstevel@tonic-gate if (nc == 0) 118*0Sstevel@tonic-gate nc = orig->_maxx + orig->_begx - bx; 119*0Sstevel@tonic-gate if ((win = makenew(nl, nc, by, bx)) == NULL) 120*0Sstevel@tonic-gate return (ERR); 121*0Sstevel@tonic-gate win->_nextp = orig->_nextp; 122*0Sstevel@tonic-gate orig->_nextp = win; 123*0Sstevel@tonic-gate win->_orig = orig; 124*0Sstevel@tonic-gate _set_subwin_(orig, win); 125*0Sstevel@tonic-gate return (win); 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate /* 129*0Sstevel@tonic-gate * this code is shared with mvwin() 130*0Sstevel@tonic-gate */ 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate void 133*0Sstevel@tonic-gate _set_subwin_(WINDOW *orig, WINDOW *win) 134*0Sstevel@tonic-gate { 135*0Sstevel@tonic-gate int i, j, k; 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate j = win->_begy - orig->_begy; 138*0Sstevel@tonic-gate k = win->_begx - orig->_begx; 139*0Sstevel@tonic-gate win->_ch_off = (short)k; 140*0Sstevel@tonic-gate #ifdef DEBUG 141*0Sstevel@tonic-gate fprintf(outf, "_SET_SUBWIN_: win->_ch_off = %d\n", win->_ch_off); 142*0Sstevel@tonic-gate #endif 143*0Sstevel@tonic-gate win->_firstch = &orig->_firstch[j]; 144*0Sstevel@tonic-gate win->_lastch = &orig->_lastch[j]; 145*0Sstevel@tonic-gate for (i = 0; i < win->_maxy; i++, j++) 146*0Sstevel@tonic-gate win->_y[i] = &orig->_y[j][k]; 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate /* 151*0Sstevel@tonic-gate * This routine sets up a window buffer and returns a pointer to it. 152*0Sstevel@tonic-gate */ 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate static WINDOW * 155*0Sstevel@tonic-gate makenew(int num_lines, int num_cols, int begy, int begx) 156*0Sstevel@tonic-gate { 157*0Sstevel@tonic-gate WINDOW *win; 158*0Sstevel@tonic-gate int by, bx, nl, nc; 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate by = begy; 161*0Sstevel@tonic-gate bx = begx; 162*0Sstevel@tonic-gate nl = num_lines; 163*0Sstevel@tonic-gate nc = num_cols; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate #ifdef DEBUG 166*0Sstevel@tonic-gate fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n", nl, nc, by, bx); 167*0Sstevel@tonic-gate #endif 168*0Sstevel@tonic-gate if ((win = (WINDOW *) malloc(sizeof (*win))) == NULL) 169*0Sstevel@tonic-gate return (NULL); 170*0Sstevel@tonic-gate #ifdef DEBUG 171*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: nl = %d\n", nl); 172*0Sstevel@tonic-gate #endif 173*0Sstevel@tonic-gate if ((win->_y = (char **)malloc(nl * sizeof (win->_y[0]))) == NULL) { 174*0Sstevel@tonic-gate free(win); 175*0Sstevel@tonic-gate return (NULL); 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate #ifdef DEBUG 178*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: nc = %d\n", nc); 179*0Sstevel@tonic-gate #endif 180*0Sstevel@tonic-gate win->_cury = win->_curx = 0; 181*0Sstevel@tonic-gate win->_clear = FALSE; 182*0Sstevel@tonic-gate win->_maxy = (short)nl; 183*0Sstevel@tonic-gate win->_maxx = (short)nc; 184*0Sstevel@tonic-gate win->_begy = (short)by; 185*0Sstevel@tonic-gate win->_begx = (short)bx; 186*0Sstevel@tonic-gate win->_flags = 0; 187*0Sstevel@tonic-gate win->_scroll = win->_leave = FALSE; 188*0Sstevel@tonic-gate _swflags_(win); 189*0Sstevel@tonic-gate win->_orig = NULL; 190*0Sstevel@tonic-gate #ifdef DEBUG 191*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear); 192*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_leave = %d\n", win->_leave); 193*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_scroll = %d\n", win->_scroll); 194*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags); 195*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy); 196*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx); 197*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy); 198*0Sstevel@tonic-gate fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx); 199*0Sstevel@tonic-gate #endif 200*0Sstevel@tonic-gate return (win); 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate void 204*0Sstevel@tonic-gate _swflags_(WINDOW *win) 205*0Sstevel@tonic-gate { 206*0Sstevel@tonic-gate win->_flags &= ~(_ENDLINE|_FULLLINE|_FULLWIN|_SCROLLWIN); 207*0Sstevel@tonic-gate if (win->_begx + win->_maxx == COLS) { 208*0Sstevel@tonic-gate win->_flags |= _ENDLINE; 209*0Sstevel@tonic-gate if (win->_begx == 0) { 210*0Sstevel@tonic-gate if (AL && DL) 211*0Sstevel@tonic-gate win->_flags |= _FULLLINE; 212*0Sstevel@tonic-gate if (win->_maxy == LINES && win->_begy == 0) 213*0Sstevel@tonic-gate win->_flags |= _FULLWIN; 214*0Sstevel@tonic-gate } 215*0Sstevel@tonic-gate if (win->_begy + win->_maxy == LINES) 216*0Sstevel@tonic-gate win->_flags |= _SCROLLWIN; 217*0Sstevel@tonic-gate } 218*0Sstevel@tonic-gate } 219