xref: /openbsd-src/lib/libpanel/panel.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: panel.c,v 1.6 1998/11/19 01:14:21 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1995                    *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /* panel.c -- implementation of panels library, some core routines */
37 #include "panel.priv.h"
38 
39 MODULE_ID("$From: panel.c,v 1.16 1998/09/19 21:26:31 Todd.Miller Exp $")
40 
41 #ifdef TRACE
42 #ifndef TRACE_TXT
43 const char *_nc_my_visbuf(const void *ptr)
44 {
45 	char temp[32];
46 	if (ptr != 0)
47 		snprintf(temp, sizeof(temp), "ptr:%p", ptr);
48 	else
49 		strcpy(temp, "<null>");
50 	return _nc_visbuf(temp);
51 }
52 #endif
53 #endif
54 
55 
56 /*+-------------------------------------------------------------------------
57 	dPanel(text,pan)
58 --------------------------------------------------------------------------*/
59 #ifdef TRACE
60 void
61 _nc_dPanel(const char *text, const PANEL *pan)
62 {
63 	_tracef("%s id=%s b=%s a=%s y=%d x=%d",
64 		text, USER_PTR(pan->user),
65 		(pan->below) ?  USER_PTR(pan->below->user) : "--",
66 		(pan->above) ?  USER_PTR(pan->above->user) : "--",
67 		pan->wstarty, pan->wstartx);
68 }
69 #endif
70 
71 /*+-------------------------------------------------------------------------
72 	dStack(fmt,num,pan)
73 --------------------------------------------------------------------------*/
74 #ifdef TRACE
75 void
76 _nc_dStack(const char *fmt, int num, const PANEL *pan)
77 {
78   char s80[80];
79 
80   snprintf(s80,sizeof(s80),fmt,num,pan);
81   _tracef("%s b=%s t=%s",s80,
82 	  (_nc_bottom_panel) ?  USER_PTR(_nc_bottom_panel->user) : "--",
83 	  (_nc_top_panel)    ?  USER_PTR(_nc_top_panel->user)    : "--");
84   if(pan)
85     _tracef("pan id=%s", USER_PTR(pan->user));
86   pan = _nc_bottom_panel;
87   while(pan)
88     {
89       dPanel("stk",pan);
90       pan = pan->above;
91     }
92 }
93 #endif
94 
95 /*+-------------------------------------------------------------------------
96 	Wnoutrefresh(pan) - debugging hook for wnoutrefresh
97 --------------------------------------------------------------------------*/
98 #ifdef TRACE
99 void
100 _nc_Wnoutrefresh(const PANEL *pan)
101 {
102   dPanel("wnoutrefresh",pan);
103   wnoutrefresh(pan->win);
104 }
105 #endif
106 
107 /*+-------------------------------------------------------------------------
108 	Touchpan(pan)
109 --------------------------------------------------------------------------*/
110 #ifdef TRACE
111 void
112 _nc_Touchpan(const PANEL *pan)
113 {
114   dPanel("Touchpan",pan);
115   touchwin(pan->win);
116 }
117 #endif
118 
119 /*+-------------------------------------------------------------------------
120 	Touchline(pan,start,count)
121 --------------------------------------------------------------------------*/
122 #ifdef TRACE
123 void
124 _nc_Touchline(const PANEL *pan, int start, int count)
125 {
126   char s80[80];
127   snprintf(s80,sizeof(s80),"Touchline s=%d c=%d",start,count);
128   dPanel(s80,pan);
129   touchline(pan->win,start,count);
130 }
131 #endif
132 
133 /*+-------------------------------------------------------------------------
134 	__panels_overlapped(pan1,pan2) - check panel overlapped
135 --------------------------------------------------------------------------*/
136 static INLINE bool
137 __panels_overlapped(register const PANEL *pan1, register const PANEL *pan2)
138 {
139   if(!pan1 || !pan2)
140     return(FALSE);
141 
142   dBug(("__panels_overlapped %s %s", USER_PTR(pan1->user), USER_PTR(pan2->user)));
143   /* pan1 intersects with pan2 ? */
144   if( (((pan1->wstarty >= pan2->wstarty) && (pan1->wstarty < pan2->wendy)) ||
145        ((pan2->wstarty >= pan1->wstarty) && (pan2->wstarty < pan1->wendy))) &&
146       (((pan1->wstartx >= pan2->wstartx) && (pan1->wstartx < pan2->wendx)) ||
147        ((pan2->wstartx >= pan1->wstartx) && (pan2->wstartx < pan1->wendx)))
148       ) return(TRUE);
149   else {
150     dBug(("  no"));
151     return(FALSE);
152   }
153 }
154 
155 /*+-------------------------------------------------------------------------
156 	_nc_free_obscure(pan)
157 --------------------------------------------------------------------------*/
158 void
159 _nc_free_obscure(PANEL *pan)
160 {
161   PANELCONS *tobs = pan->obscure;			/* "this" one */
162   PANELCONS *nobs;					/* "next" one */
163 
164   while(tobs)
165     {
166       nobs = tobs->above;
167       free((char *)tobs);
168       tobs = nobs;
169     }
170   pan->obscure = (PANELCONS *)0;
171 }
172 
173 /*+-------------------------------------------------------------------------
174 	__override(pan,show)
175 --------------------------------------------------------------------------*/
176 void
177 _nc_override(const PANEL *pan, int show)
178 {
179   int y;
180   PANEL *pan2;
181   PANELCONS *tobs = pan->obscure;			   /* "this" one */
182 
183   dBug(("_nc_override %s,%d", USER_PTR(pan->user),show));
184 
185   switch (show)
186     {
187     case P_TOUCH:
188       Touchpan(pan);
189       /* The following while loop will now mark all panel window lines
190        * obscured by use or obscuring us as touched, so they will be
191        * updated.
192        */
193       break;
194     case P_UPDATE:
195       while(tobs && (tobs->pan != pan))
196 	tobs = tobs->above;
197       /* The next loop will now only go through the panels obscuring pan;
198        * it updates all the lines in the obscuring panels in sync. with
199        * the lines touched in pan itself. This is called in update_panels()
200        * in a loop from the bottom_panel to the top_panel, resulting in
201        * the desired update effect.
202        */
203       break;
204     default:
205       return;
206     }
207 
208   while(tobs)
209     {
210       if((pan2 = tobs->pan) != pan) {
211 	dBug(("test obs pan=%s pan2=%s", USER_PTR(pan->user), USER_PTR(pan2->user)));
212 	for(y = pan->wstarty; y < pan->wendy; y++) {
213 	  if( (y >= pan2->wstarty) && (y < pan2->wendy) &&
214 	      ((is_linetouched(pan->win,y - pan->wstarty) == TRUE)) )
215 	    Touchline(pan2,y - pan2->wstarty,1);
216 	}
217       }
218       tobs = tobs->above;
219     }
220 }
221 
222 /*+-------------------------------------------------------------------------
223 	__calculate_obscure()
224 --------------------------------------------------------------------------*/
225 void
226 _nc_calculate_obscure(void)
227 {
228   PANEL *pan;
229   PANEL *pan2;
230   PANELCONS *tobs;			/* "this" one */
231   PANELCONS *lobs = (PANELCONS *)0;	/* last one */
232 
233   pan = _nc_bottom_panel;
234   while(pan)
235     {
236       if(pan->obscure)
237 	_nc_free_obscure(pan);
238       dBug(("--> __calculate_obscure %s", USER_PTR(pan->user)));
239       lobs = (PANELCONS *)0;		/* last one */
240       pan2 = _nc_bottom_panel;
241       /* This loop builds a list of panels obsured by pan or obscuring
242 	 pan; pan itself is in the list; all panels before pan are
243 	 obscured by pan, all panels after pan are obscuring pan. */
244       while(pan2)
245 	{
246 	  if(__panels_overlapped(pan,pan2))
247 	    {
248 	      if(!(tobs = (PANELCONS *)malloc(sizeof(PANELCONS))))
249 		return;
250 	      tobs->pan = pan2;
251 	      dPanel("obscured",pan2);
252 	      tobs->above = (PANELCONS *)0;
253 	      if(lobs)
254 		lobs->above = tobs;
255 	      else
256 		pan->obscure = tobs;
257 	      lobs  = tobs;
258 	    }
259 	  pan2 = pan2->above;
260 	}
261       _nc_override(pan,P_TOUCH);
262       pan = pan->above;
263     }
264 }
265 
266 /*+-------------------------------------------------------------------------
267 	_nc_panel_is_linked(pan) - check to see if panel is in the stack
268 --------------------------------------------------------------------------*/
269 bool
270 _nc_panel_is_linked(const PANEL *pan)
271 {
272   /* This works! The only case where it would fail is, when the list has
273      only one element. But this could only be the pseudo panel at the bottom */
274   return ( ((pan->above!=(PANEL *)0) ||
275 	    (pan->below!=(PANEL *)0) ||
276 	    (pan==_nc_bottom_panel)) ? TRUE : FALSE );
277 }
278 
279 
280 /*+-------------------------------------------------------------------------
281 	__panel_link_bottom(pan) - link panel into stack at bottom
282 --------------------------------------------------------------------------*/
283 void
284 _nc_panel_link_bottom(PANEL *pan)
285 {
286 #ifdef TRACE
287   dStack("<lb%d>",1,pan);
288   if(_nc_panel_is_linked(pan))
289     return;
290 #endif
291 
292   pan->above = (PANEL *)0;
293   pan->below = (PANEL *)0;
294   if(_nc_bottom_panel)
295     { /* the stdscr pseudo panel always stays real bottom;
296          so we insert after bottom panel*/
297       pan->below = _nc_bottom_panel;
298       pan->above = _nc_bottom_panel->above;
299       if (pan->above)
300 	pan->above->below = pan;
301       _nc_bottom_panel->above = pan;
302     }
303   else
304     _nc_bottom_panel = pan;
305   if(!_nc_top_panel)
306     _nc_top_panel = pan;
307   assert(_nc_bottom_panel == _nc_stdscr_pseudo_panel);
308   _nc_calculate_obscure();
309   dStack("<lb%d>",9,pan);
310 }
311