1 /* @(#)win.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: win.c,v 1.14 2009/04/14 08:50:06 lukem Exp $ */
5 * Copyright (c) 1983, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include "window_string.h"
42 * Higher level routines for dealing with windows.
44 * There are two types of windows: user window, and information window.
45 * User windows are the ones with a pty and shell. Information windows
46 * are for displaying error messages, and other information.
48 * The windows are doubly linked in overlapping order and divided into
49 * two groups: foreground and normal. Information
50 * windows are always foreground. User windows can be either.
51 * Addwin() adds a window to the list at the top of one of the two groups.
52 * Deletewin() deletes a window. Front() moves a window to the front
53 * of its group. Wwopen(), wwadd(), and wwdelete() should never be called
61 openwin(int id, int row, int col, int nrow, int ncol, int nline, char *label, int type, int uflags, char *shf, char **sh)
65 if (id < 0 && (id = findid()) < 0)
67 if (row + nrow <= 0 || row > wwnrow - 1
68 || col + ncol <= 0 || col > wwncol - 1) {
69 error("Illegal window position.");
72 w = wwopen(type, 0, nrow, ncol, row, col, nline);
74 error("Can't open window: %s.", wwerror());
79 CLR(w->ww_uflags, WWU_ALLFLAGS);
80 SET(w->ww_uflags, uflags);
82 if (label != NULL && setlabel(w, label) < 0)
83 error("No memory for label.");
86 * We have to do this little maneuver to make sure
87 * addwin() puts w at the top, so we don't waste an
88 * insert and delete operation.
90 setselwin((struct ww *)0);
93 if (wwspawn(w, shf, sh) < 0) {
94 error("Can't execute %s: %s.", shf, wwerror());
106 for (i = 0; i < NWINDOW && window[i] != 0; i++)
109 error("Too many windows.");
118 struct ww *w, *s = NULL;
121 for (i = 0; i < NWINDOW; i++)
122 if ((w = window[i]) != NULL && w != selwin &&
124 (!isfg(w) && (w->ww_order < s->ww_order || isfg(s)))))
130 * Close a user window. Close all if w == 0.
133 closewin(struct ww *w)
142 for (i = 0; i < NWINDOW; i++) {
143 if ((w = window[i]) == NULL)
150 if (lastselwin != 0) {
151 setselwin(lastselwin);
153 } else if ((w = findselwin()))
156 if (lastselwin == 0 && selwin)
157 if ((w = findselwin()))
164 * Open an information (display) window.
167 openiwin(int nrow, const char *label)
171 if ((w = wwopen(WWT_INTERNAL, 0, nrow, wwncol, 2, 0, 0)) == NULL)
173 SET(w->ww_wflags, WWW_MAPNL | WWW_NOINTR | WWW_NOUPDATE | WWW_UNCTRL);
174 SET(w->ww_uflags, WWU_HASFRAME | WWU_CENTER);
176 (void) setlabel(w, label);
183 * Close an information window.
186 closeiwin(struct ww *w)
193 closewin1(struct ww *w)
199 if (w->ww_id >= 0 && w->ww_id < NWINDOW)
200 window[w->ww_id] = 0;
202 str_free(w->ww_label);
208 * Move the window to the top of its group.
209 * Don't do it if already fully visible.
210 * Wwvisible() doesn't work for tinted windows.
211 * But anything to make it faster.
212 * Always reframe() if doreframe is true.
215 front(struct ww *w, char doreframe)
217 if (w->ww_back != (isfg(w) ? framewin : fgwin) && !wwvisible(w)) {
227 * Add a window at the top of normal windows or foreground windows.
228 * For normal windows, we put it behind the current window.
231 addwin(struct ww *w, char fg)
235 if (fgwin == framewin)
238 wwadd(w, selwin != 0 && selwin != w && !isfg(selwin)
246 deletewin(struct ww *w)
259 for (w = wwhead.ww_back; w != &wwhead; w = w->ww_back)
260 if (ISSET(w->ww_uflags, WWU_HASFRAME)) {
261 wwframe(w, framewin);
267 labelwin(struct ww *w)
269 int mode = w == selwin ? WWM_REV : 0;
271 if (!ISSET(w->ww_uflags, WWU_HASFRAME))
276 buf[0] = w->ww_id + '1';
278 wwlabel(w, framewin, 1, buf, mode);
283 if (ISSET(w->ww_uflags, WWU_CENTER)) {
284 col = (w->ww_w.nc - strlen(w->ww_label)) / 2;
288 wwlabel(w, framewin, col, w->ww_label, mode);
293 stopwin(struct ww *w)
295 if (w->ww_pty >= 0 && w->ww_type == WWT_PTY && wwstoptty(w->ww_pty) < 0)
296 error("Can't stop output: %s.", wwerror());
298 SET(w->ww_pflags, WWP_STOPPED);
302 startwin(struct ww *w)
304 if (w->ww_pty >= 0 && w->ww_type == WWT_PTY &&
305 wwstarttty(w->ww_pty) < 0)
306 error("Can't start output: %s.", wwerror());
308 CLR(w->ww_pflags, WWP_STOPPED);
312 sizewin(struct ww *w, int nrow, int ncol)
314 struct ww *back = w->ww_back;
316 w->ww_alt.nr = w->ww_w.nr;
317 w->ww_alt.nc = w->ww_w.nc;
319 if (wwsize(w, nrow, ncol) < 0)
320 error("Can't resize window: %s.", wwerror());
328 (void) waitnl1(w, "[Type any key to continue]");
332 more(struct ww *w, char always)
335 int uc = ISSET(w->ww_wflags, WWW_UNCTRL);
337 if (!always && w->ww_cur.r < w->ww_w.b - 2)
339 c = waitnl1(w, "[Type escape to abort, any other key to continue]");
340 CLR(w->ww_wflags, WWW_UNCTRL);
342 SET(w->ww_wflags, uc);
343 return c == ctrl('[') ? 2 : 1;
347 waitnl1(struct ww *w, const char *prompt)
349 int uc = ISSET(w->ww_wflags, WWW_UNCTRL);
351 CLR(w->ww_wflags, WWW_UNCTRL);
353 wwprintf(w, "\033Y%c%c\033sA%s\033rA ",
354 w->ww_w.nr - 1 + ' ', ' ', prompt); /* print on last line */
356 while (wwpeekc() < 0)
358 SET(w->ww_wflags, uc);