xref: /openbsd/lib/libpanel/p_new.c (revision c7ef0cfc)
1 /* $OpenBSD: p_new.c,v 1.6 2023/10/17 09:52:10 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020,2021 Thomas E. Dickey                                     *
5  * Copyright 1998-2009,2010 Free Software Foundation, Inc.                  *
6  *                                                                          *
7  * Permission is hereby granted, free of charge, to any person obtaining a  *
8  * copy of this software and associated documentation files (the            *
9  * "Software"), to deal in the Software without restriction, including      *
10  * without limitation the rights to use, copy, modify, merge, publish,      *
11  * distribute, distribute with modifications, sublicense, and/or sell       *
12  * copies of the Software, and to permit persons to whom the Software is    *
13  * furnished to do so, subject to the following conditions:                 *
14  *                                                                          *
15  * The above copyright notice and this permission notice shall be included  *
16  * in all copies or substantial portions of the Software.                   *
17  *                                                                          *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
21  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
24  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
25  *                                                                          *
26  * Except as contained in this notice, the name(s) of the above copyright   *
27  * holders shall not be used in advertising or otherwise to promote the     *
28  * sale, use or other dealings in this Software without prior written       *
29  * authorization.                                                           *
30  ****************************************************************************/
31 
32 /****************************************************************************
33  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1995                    *
34  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
35  *     and: Juergen Pfeifer                         1997-1999               *
36  *     and: Thomas E. Dickey                        2000-on                 *
37  ****************************************************************************/
38 
39 /* p_new.c
40  * Creation of a new panel
41  */
42 #include "panel.priv.h"
43 
44 MODULE_ID("$Id: p_new.c,v 1.6 2023/10/17 09:52:10 nicm Exp $")
45 
46 #ifdef TRACE
47 static char *stdscr_id;
48 static char *new_id;
49 
50 static PANEL *
AllocPanel(const char * name)51 AllocPanel(const char *name)
52 {
53   PANEL *result = typeMalloc(PANEL, 1);
54 
55   _tracef("create :%s %p", name, (void *)result);
56   return result;
57 }
58 #define InitUser(name) \
59 	if (!name ## _id) \
60 	    name ## _id = strdup(#name); \
61 	pan->user = name ## _id; \
62 	_tracef("create :user_ptr %p", pan->user)
63 #else
64 #define AllocPanel(name) typeMalloc(PANEL, 1)
65 #define InitUser(name) \
66 	  pan->user = (void *)0
67 #endif
68 
69 /*+-------------------------------------------------------------------------
70   Get root (i.e. stdscr's) panel.
71   Establish the pseudo panel for stdscr if necessary.
72 --------------------------------------------------------------------------*/
73 static PANEL *
root_panel(NCURSES_SP_DCL0)74 root_panel(NCURSES_SP_DCL0)
75 {
76 #if NCURSES_SP_FUNCS
77   struct panelhook *ph = NCURSES_SP_NAME(_nc_panelhook) (sp);
78 
79 #elif NO_LEAKS
80   struct panelhook *ph = _nc_panelhook();
81 #endif
82 
83   if (_nc_stdscr_pseudo_panel == (PANEL *)0)
84     {
85 
86       assert(SP_PARM && SP_PARM->_stdscr && !_nc_bottom_panel && !_nc_top_panel);
87 #if NO_LEAKS
88       ph->destroy = del_panel;
89 #endif
90       _nc_stdscr_pseudo_panel = AllocPanel("root_panel");
91       if (_nc_stdscr_pseudo_panel != 0)
92 	{
93 	  PANEL *pan = _nc_stdscr_pseudo_panel;
94 	  WINDOW *win = SP_PARM->_stdscr;
95 
96 	  pan->win = win;
97 	  pan->below = (PANEL *)0;
98 	  pan->above = (PANEL *)0;
99 	  InitUser(stdscr);
100 	  _nc_bottom_panel = _nc_top_panel = pan;
101 	}
102     }
103   return _nc_stdscr_pseudo_panel;
104 }
105 
106 PANEL_EXPORT(PANEL *)
new_panel(WINDOW * win)107 new_panel(WINDOW *win)
108 {
109   PANEL *pan = (PANEL *)0;
110 
111   GetWindowHook(win);
112 
113   T((T_CALLED("new_panel(%p)"), (void *)win));
114 
115   if (!win)
116     returnPanel(pan);
117 
118   if (!_nc_stdscr_pseudo_panel)
119     (void)root_panel(NCURSES_SP_ARG);
120   assert(_nc_stdscr_pseudo_panel);
121 
122   if ((pan = AllocPanel("new_panel")) != NULL)
123     {
124       pan->win = win;
125       pan->above = (PANEL *)0;
126       pan->below = (PANEL *)0;
127       InitUser(new);
128       (void)show_panel(pan);
129     }
130   returnPanel(pan);
131 }
132