150276Speter/****************************************************************************
2178866Srafan * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
3050276Speter *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3150276Speter *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
3250276Speter ****************************************************************************/
3350276Speter
3450276Speter/*
3550276Speter**	lib_delwin.c
3650276Speter**
3750276Speter**	The routine delwin().
3850276Speter**
3950276Speter*/
4050276Speter
4150276Speter#include <curses.priv.h>
4250276Speter
43184989SrafanMODULE_ID("$Id: lib_delwin.c,v 1.17 2008/06/07 14:10:56 tom Exp $")
4450276Speter
4576726Speterstatic bool
4676726Spetercannot_delete(WINDOW *win)
4750276Speter{
4876726Speter    WINDOWLIST *p;
4976726Speter    bool result = TRUE;
5076726Speter
51178866Srafan    for (each_window(p)) {
5297049Speter	if (&(p->win) == win) {
5376726Speter	    result = FALSE;
5497049Speter	} else if ((p->win._flags & _SUBWIN) != 0
5597049Speter		   && p->win._parent == win) {
5676726Speter	    result = TRUE;
5776726Speter	    break;
5850276Speter	}
5976726Speter    }
6076726Speter    return result;
6150276Speter}
6250276Speter
6376726SpeterNCURSES_EXPORT(int)
6476726Speterdelwin(WINDOW *win)
6550276Speter{
66174993Srafan    int result = ERR;
67174993Srafan
6876726Speter    T((T_CALLED("delwin(%p)"), win));
6950276Speter
70184989Srafan    if (_nc_try_global(curses) == 0) {
71174993Srafan	if (win == 0
72174993Srafan	    || cannot_delete(win)) {
73174993Srafan	    result = ERR;
74174993Srafan	} else {
7550276Speter
76174993Srafan	    if (win->_flags & _SUBWIN)
77174993Srafan		touchwin(win->_parent);
78174993Srafan	    else if (curscr != 0)
79174993Srafan		touchwin(curscr);
8050276Speter
81174993Srafan	    result = _nc_freewin(win);
82174993Srafan	}
83184989Srafan	_nc_unlock_global(curses);
84174993Srafan    }
85174993Srafan    returnCode(result);
8650276Speter}
87