1 /* $NetBSD: win.c,v 1.13 2003/08/07 11:17:34 agc Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)win.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: win.c,v 1.13 2003/08/07 11:17:34 agc Exp $");
47 #include "window_string.h"
50 * Higher level routines for dealing with windows.
52 * There are two types of windows: user window, and information window.
53 * User windows are the ones with a pty and shell. Information windows
54 * are for displaying error messages, and other information.
56 * The windows are doubly linked in overlapping order and divided into
57 * two groups: foreground and normal. Information
58 * windows are always foreground. User windows can be either.
59 * Addwin() adds a window to the list at the top of one of the two groups.
60 * Deletewin() deletes a window. Front() moves a window to the front
61 * of its group. Wwopen(), wwadd(), and wwdelete() should never be called
69 openwin(int id
, int row
, int col
, int nrow
, int ncol
, int nline
, char *label
, int type
, int uflags
, char *shf
, char **sh
)
73 if (id
< 0 && (id
= findid()) < 0)
75 if (row
+ nrow
<= 0 || row
> wwnrow
- 1
76 || col
+ ncol
<= 0 || col
> wwncol
- 1) {
77 error("Illegal window position.");
80 w
= wwopen(type
, 0, nrow
, ncol
, row
, col
, nline
);
82 error("Can't open window: %s.", wwerror());
87 CLR(w
->ww_uflags
, WWU_ALLFLAGS
);
88 SET(w
->ww_uflags
, uflags
);
90 if (label
!= 0 && setlabel(w
, label
) < 0)
91 error("No memory for label.");
94 * We have to do this little maneuver to make sure
95 * addwin() puts w at the top, so we don't waste an
96 * insert and delete operation.
98 setselwin((struct ww
*)0);
101 if (wwspawn(w
, shf
, sh
) < 0) {
102 error("Can't execute %s: %s.", shf
, wwerror());
114 for (i
= 0; i
< NWINDOW
&& window
[i
] != 0; i
++)
117 error("Too many windows.");
126 struct ww
*w
, *s
= 0;
129 for (i
= 0; i
< NWINDOW
; i
++)
130 if ((w
= window
[i
]) != 0 && w
!= selwin
&&
132 (!isfg(w
) && (w
->ww_order
< s
->ww_order
|| isfg(s
)))))
138 * Close a user window. Close all if w == 0.
141 closewin(struct ww
*w
)
150 for (i
= 0; i
< NWINDOW
; i
++) {
151 if ((w
= window
[i
]) == 0)
158 if (lastselwin
!= 0) {
159 setselwin(lastselwin
);
161 } else if ((w
= findselwin()))
164 if (lastselwin
== 0 && selwin
)
165 if ((w
= findselwin()))
172 * Open an information (display) window.
175 openiwin(int nrow
, const char *label
)
179 if ((w
= wwopen(WWT_INTERNAL
, 0, nrow
, wwncol
, 2, 0, 0)) == 0)
181 SET(w
->ww_wflags
, WWW_MAPNL
| WWW_NOINTR
| WWW_NOUPDATE
| WWW_UNCTRL
);
182 SET(w
->ww_uflags
, WWU_HASFRAME
| WWU_CENTER
);
184 (void) setlabel(w
, label
);
191 * Close an information window.
194 closeiwin(struct ww
*w
)
201 closewin1(struct ww
*w
)
207 if (w
->ww_id
>= 0 && w
->ww_id
< NWINDOW
)
208 window
[w
->ww_id
] = 0;
210 str_free(w
->ww_label
);
216 * Move the window to the top of its group.
217 * Don't do it if already fully visible.
218 * Wwvisible() doesn't work for tinted windows.
219 * But anything to make it faster.
220 * Always reframe() if doreframe is true.
223 front(struct ww
*w
, char doreframe
)
225 if (w
->ww_back
!= (isfg(w
) ? framewin
: fgwin
) && !wwvisible(w
)) {
235 * Add a window at the top of normal windows or foreground windows.
236 * For normal windows, we put it behind the current window.
239 addwin(struct ww
*w
, char fg
)
243 if (fgwin
== framewin
)
246 wwadd(w
, selwin
!= 0 && selwin
!= w
&& !isfg(selwin
)
254 deletewin(struct ww
*w
)
267 for (w
= wwhead
.ww_back
; w
!= &wwhead
; w
= w
->ww_back
)
268 if (ISSET(w
->ww_uflags
, WWU_HASFRAME
)) {
269 wwframe(w
, framewin
);
275 labelwin(struct ww
*w
)
277 int mode
= w
== selwin
? WWM_REV
: 0;
279 if (!ISSET(w
->ww_uflags
, WWU_HASFRAME
))
284 buf
[0] = w
->ww_id
+ '1';
286 wwlabel(w
, framewin
, 1, buf
, mode
);
291 if (ISSET(w
->ww_uflags
, WWU_CENTER
)) {
292 col
= (w
->ww_w
.nc
- strlen(w
->ww_label
)) / 2;
296 wwlabel(w
, framewin
, col
, w
->ww_label
, mode
);
301 stopwin(struct ww
*w
)
303 if (w
->ww_pty
>= 0 && w
->ww_type
== WWT_PTY
&& wwstoptty(w
->ww_pty
) < 0)
304 error("Can't stop output: %s.", wwerror());
306 SET(w
->ww_pflags
, WWP_STOPPED
);
310 startwin(struct ww
*w
)
312 if (w
->ww_pty
>= 0 && w
->ww_type
== WWT_PTY
&&
313 wwstarttty(w
->ww_pty
) < 0)
314 error("Can't start output: %s.", wwerror());
316 CLR(w
->ww_pflags
, WWP_STOPPED
);
320 sizewin(struct ww
*w
, int nrow
, int ncol
)
322 struct ww
*back
= w
->ww_back
;
324 w
->ww_alt
.nr
= w
->ww_w
.nr
;
325 w
->ww_alt
.nc
= w
->ww_w
.nc
;
327 if (wwsize(w
, nrow
, ncol
) < 0)
328 error("Can't resize window: %s.", wwerror());
336 (void) waitnl1(w
, "[Type any key to continue]");
340 more(struct ww
*w
, char always
)
343 int uc
= ISSET(w
->ww_wflags
, WWW_UNCTRL
);
345 if (!always
&& w
->ww_cur
.r
< w
->ww_w
.b
- 2)
347 c
= waitnl1(w
, "[Type escape to abort, any other key to continue]");
348 CLR(w
->ww_wflags
, WWW_UNCTRL
);
350 SET(w
->ww_wflags
, uc
);
351 return c
== ctrl('[') ? 2 : 1;
355 waitnl1(struct ww
*w
, const char *prompt
)
357 int uc
= ISSET(w
->ww_wflags
, WWW_UNCTRL
);
359 CLR(w
->ww_wflags
, WWW_UNCTRL
);
361 wwprintf(w
, "\033Y%c%c\033sA%s\033rA ",
362 w
->ww_w
.nr
- 1 + ' ', ' ', prompt
); /* print on last line */
364 while (wwpeekc() < 0)
366 SET(w
->ww_wflags
, uc
);