xref: /dflybsd-src/usr.bin/window/cmd5.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
1*86d7f5d3SJohn Marino /*	@(#)cmd5.c	8.1 (Berkeley) 6/6/93	*/
2*86d7f5d3SJohn Marino /*	$NetBSD: cmd5.c,v 1.7 2003/08/07 11:17:23 agc Exp $	*/
3*86d7f5d3SJohn Marino 
4*86d7f5d3SJohn Marino /*
5*86d7f5d3SJohn Marino  * Copyright (c) 1983, 1993
6*86d7f5d3SJohn Marino  *	The Regents of the University of California.  All rights reserved.
7*86d7f5d3SJohn Marino  *
8*86d7f5d3SJohn Marino  * This code is derived from software contributed to Berkeley by
9*86d7f5d3SJohn Marino  * Edward Wang at The University of California, Berkeley.
10*86d7f5d3SJohn Marino  *
11*86d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
12*86d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
13*86d7f5d3SJohn Marino  * are met:
14*86d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the above copyright
15*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
16*86d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
17*86d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
18*86d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
19*86d7f5d3SJohn Marino  * 3. Neither the name of the University nor the names of its contributors
20*86d7f5d3SJohn Marino  *    may be used to endorse or promote products derived from this software
21*86d7f5d3SJohn Marino  *    without specific prior written permission.
22*86d7f5d3SJohn Marino  *
23*86d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24*86d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25*86d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26*86d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27*86d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28*86d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29*86d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30*86d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31*86d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32*86d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33*86d7f5d3SJohn Marino  * SUCH DAMAGE.
34*86d7f5d3SJohn Marino  */
35*86d7f5d3SJohn Marino 
36*86d7f5d3SJohn Marino #include "defs.h"
37*86d7f5d3SJohn Marino 
38*86d7f5d3SJohn Marino /*
39*86d7f5d3SJohn Marino  * Window movement.
40*86d7f5d3SJohn Marino  */
41*86d7f5d3SJohn Marino 
42*86d7f5d3SJohn Marino void	getminmax(int, int, int, int, int *, int *, int *);
43*86d7f5d3SJohn Marino 
44*86d7f5d3SJohn Marino void
c_move(struct ww * w)45*86d7f5d3SJohn Marino c_move(struct ww *w)
46*86d7f5d3SJohn Marino {
47*86d7f5d3SJohn Marino 	int col, row;
48*86d7f5d3SJohn Marino 	int mincol, minrow;
49*86d7f5d3SJohn Marino 	int maxcol, maxrow;
50*86d7f5d3SJohn Marino 	int curcol, currow;
51*86d7f5d3SJohn Marino 
52*86d7f5d3SJohn Marino 	if (!terse)
53*86d7f5d3SJohn Marino 		wwputs("New window position: ", cmdwin);
54*86d7f5d3SJohn Marino 	col = w->ww_w.l;
55*86d7f5d3SJohn Marino 	row = w->ww_w.t;
56*86d7f5d3SJohn Marino 	wwadd(boxwin, framewin->ww_back);
57*86d7f5d3SJohn Marino 	for (;;) {
58*86d7f5d3SJohn Marino 		wwbox(boxwin, row - 1, col - 1, w->ww_w.nr + 2, w->ww_w.nc + 2);
59*86d7f5d3SJohn Marino 		getminmax(row, w->ww_w.nr, 1, wwnrow,
60*86d7f5d3SJohn Marino 			&currow, &minrow, &maxrow);
61*86d7f5d3SJohn Marino 		getminmax(col, w->ww_w.nc, 0, wwncol,
62*86d7f5d3SJohn Marino 			&curcol, &mincol, &maxcol);
63*86d7f5d3SJohn Marino 		wwsetcursor(currow, curcol);
64*86d7f5d3SJohn Marino 		while (wwpeekc() < 0)
65*86d7f5d3SJohn Marino 			wwiomux();
66*86d7f5d3SJohn Marino 		switch (getpos(&row, &col, minrow, mincol, maxrow, maxcol)) {
67*86d7f5d3SJohn Marino 		case 3:
68*86d7f5d3SJohn Marino 			wwunbox(boxwin);
69*86d7f5d3SJohn Marino 			wwdelete(boxwin);
70*86d7f5d3SJohn Marino 			return;
71*86d7f5d3SJohn Marino 		case 2:
72*86d7f5d3SJohn Marino 			wwunbox(boxwin);
73*86d7f5d3SJohn Marino 			break;
74*86d7f5d3SJohn Marino 		case 1:
75*86d7f5d3SJohn Marino 			wwunbox(boxwin);
76*86d7f5d3SJohn Marino 		case 0:
77*86d7f5d3SJohn Marino 			continue;
78*86d7f5d3SJohn Marino 		}
79*86d7f5d3SJohn Marino 		break;
80*86d7f5d3SJohn Marino 	}
81*86d7f5d3SJohn Marino 	wwdelete(boxwin);
82*86d7f5d3SJohn Marino 	if (!terse)
83*86d7f5d3SJohn Marino 		wwputc('\n', cmdwin);
84*86d7f5d3SJohn Marino 	wwcurtowin(cmdwin);
85*86d7f5d3SJohn Marino 	movewin(w, row, col);
86*86d7f5d3SJohn Marino }
87*86d7f5d3SJohn Marino 
88*86d7f5d3SJohn Marino void
movewin(struct ww * w,int row,int col)89*86d7f5d3SJohn Marino movewin(struct ww *w, int row, int col)
90*86d7f5d3SJohn Marino {
91*86d7f5d3SJohn Marino 	struct ww *back = w->ww_back;
92*86d7f5d3SJohn Marino 
93*86d7f5d3SJohn Marino 	w->ww_alt.t = w->ww_w.t;
94*86d7f5d3SJohn Marino 	w->ww_alt.l = w->ww_w.l;
95*86d7f5d3SJohn Marino 	wwdelete(w);
96*86d7f5d3SJohn Marino 	wwmove(w, row, col);
97*86d7f5d3SJohn Marino 	wwadd(w, back);
98*86d7f5d3SJohn Marino 	reframe();
99*86d7f5d3SJohn Marino }
100*86d7f5d3SJohn Marino 
101*86d7f5d3SJohn Marino /*
102*86d7f5d3SJohn Marino  * Weird stufff, don't ask.
103*86d7f5d3SJohn Marino  */
104*86d7f5d3SJohn Marino void
getminmax(int x,int n,int a,int b,int * curx,int * minx,int * maxx)105*86d7f5d3SJohn Marino getminmax(int x, int n, int a, int b, int *curx, int *minx, int *maxx)
106*86d7f5d3SJohn Marino {
107*86d7f5d3SJohn Marino 	if (x < 0)
108*86d7f5d3SJohn Marino 		*curx = x + n - 1;
109*86d7f5d3SJohn Marino 	else
110*86d7f5d3SJohn Marino 		*curx = x;
111*86d7f5d3SJohn Marino 
112*86d7f5d3SJohn Marino 	if (x <= a)
113*86d7f5d3SJohn Marino 		*minx = 1 - n;
114*86d7f5d3SJohn Marino 	else if (x <= b - n)
115*86d7f5d3SJohn Marino 		*minx = a;
116*86d7f5d3SJohn Marino 	else
117*86d7f5d3SJohn Marino 		*minx = b - n;
118*86d7f5d3SJohn Marino 
119*86d7f5d3SJohn Marino 	if (x >= b - n)
120*86d7f5d3SJohn Marino 		*maxx = b - 1;
121*86d7f5d3SJohn Marino 	else if (x >= a)
122*86d7f5d3SJohn Marino 		*maxx = b - n;
123*86d7f5d3SJohn Marino 	else
124*86d7f5d3SJohn Marino 		*maxx = a;
125*86d7f5d3SJohn Marino }
126