1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2014,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /* $Id: panel.priv.h,v 1.28 2020/02/02 23:34:34 tom Exp $ */
31 
32 #ifndef NCURSES_PANEL_PRIV_H
33 #define NCURSES_PANEL_PRIV_H 1
34 /* *INDENT-OFF* */
35 
36 #include <ncurses_cfg.h>
37 
38 #include <stdlib.h>
39 #include <string.h>
40 #include <assert.h>
41 
42 struct screen;              /* forward declaration */
43 
44 #include "curses.priv.h"    /* includes nc_panel.h */
45 
46 #define NCURSES_OPAQUE_PANEL 0
47 
48 #include "panel.h"
49 
50 #ifdef TRACE
51    extern NCURSES_EXPORT(const char *) _nc_my_visbuf (const void *);
52 #  ifdef TRACE_TXT
53 #    define USER_PTR(ptr) _nc_visbuf((const char *)ptr)
54 #  else
55 #    define USER_PTR(ptr) _nc_my_visbuf((const char *)ptr)
56 #  endif
57 
58 #  define returnPanel(code)	TRACE_RETURN1(code,panel)
59 
60    extern NCURSES_EXPORT(PANEL *) _nc_retrace_panel (PANEL *);
61    extern NCURSES_EXPORT(void) _nc_dPanel (const char*, const PANEL*);
62    extern NCURSES_EXPORT(void) _nc_dStack (const char*, int, const PANEL*);
63    extern NCURSES_EXPORT(void) _nc_Wnoutrefresh (const PANEL*);
64    extern NCURSES_EXPORT(void) _nc_Touchpan (const PANEL*);
65    extern NCURSES_EXPORT(void) _nc_Touchline (const PANEL*, int, int);
66 
67 #  define dBug(x) _tracef x
68 #  define dPanel(text,pan) _nc_dPanel(text,pan)
69 #  define dStack(fmt,num,pan) _nc_dStack(fmt,num,pan)
70 #  define Wnoutrefresh(pan) _nc_Wnoutrefresh(pan)
71 #  define Touchpan(pan) _nc_Touchpan(pan)
72 #  define Touchline(pan,start,count) _nc_Touchline(pan,start,count)
73 #else /* !TRACE */
74 #  define returnPanel(code)	return code
75 #  define dBug(x)
76 #  define dPanel(text,pan)
77 #  define dStack(fmt,num,pan)
78 #  define Wnoutrefresh(pan) wnoutrefresh((pan)->win)
79 #  define Touchpan(pan) touchwin((pan)->win)
80 #  define Touchline(pan,start,count) touchline((pan)->win,start,count)
81 #endif
82 
83 #if NCURSES_SP_FUNCS
84 #define GetScreenHook(sp) \
85 			struct panelhook* ph = NCURSES_SP_NAME(_nc_panelhook)(sp)
86 #define GetPanelHook(pan) \
87 			GetScreenHook(pan ? _nc_screen_of((pan)->win) : 0)
88 #define GetWindowHook(win) \
89 			SCREEN* sp = _nc_screen_of(win); \
90 			GetScreenHook(sp)
91 #define GetHook(pan)	SCREEN* sp = _nc_screen_of(pan->win); \
92 			GetScreenHook(sp)
93 
94 #define _nc_stdscr_pseudo_panel ((ph)->stdscr_pseudo_panel)
95 #define _nc_top_panel           ((ph)->top_panel)
96 #define _nc_bottom_panel        ((ph)->bottom_panel)
97 
98 #else	/* !NCURSES_SP_FUNCS */
99 
100 #define GetScreenHook(sp) /* nothing */
101 #define GetPanelHook(pan) /* nothing */
102 #define GetWindowHook(win) /* nothing */
103 #define GetHook(pan) /* nothing */
104 
105 #define _nc_stdscr_pseudo_panel _nc_panelhook()->stdscr_pseudo_panel
106 #define _nc_top_panel           _nc_panelhook()->top_panel
107 #define _nc_bottom_panel        _nc_panelhook()->bottom_panel
108 
109 #endif	/* NCURSES_SP_FUNCS */
110 
111 #define EMPTY_STACK() (_nc_top_panel == _nc_bottom_panel)
112 #define Is_Bottom(p)  (((p) != (PANEL*)0) && !EMPTY_STACK() && (_nc_bottom_panel->above == (p)))
113 #define Is_Top(p)     (((p) != (PANEL*)0) && !EMPTY_STACK() && (_nc_top_panel == (p)))
114 #define Is_Pseudo(p)  (((p) != (PANEL*)0) && ((p) == _nc_bottom_panel))
115 
116 /*+-------------------------------------------------------------------------
117 	IS_LINKED(pan) - check to see if panel is in the stack
118 --------------------------------------------------------------------------*/
119 /* This works! The only case where it would fail is, when the list has
120    only one element. But this could only be the pseudo panel at the bottom */
121 #define IS_LINKED(p) (((p)->above || (p)->below ||((p)==_nc_bottom_panel)) ? TRUE : FALSE)
122 
123 #define PSTARTX(pan) ((pan)->win->_begx)
124 #define PENDX(pan)   ((pan)->win->_begx + getmaxx((pan)->win) - 1)
125 #define PSTARTY(pan) ((pan)->win->_begy)
126 #define PENDY(pan)   ((pan)->win->_begy + getmaxy((pan)->win) - 1)
127 
128 /*+-------------------------------------------------------------------------
129 	PANELS_OVERLAPPED(pan1,pan2) - check panel overlapped
130 ---------------------------------------------------------------------------*/
131 #define PANELS_OVERLAPPED(pan1,pan2) \
132 (( !(pan1) || !(pan2) || \
133        PSTARTY(pan1) > PENDY(pan2) || PENDY(pan1) < PSTARTY(pan2) ||\
134        PSTARTX(pan1) > PENDX(pan2) || PENDX(pan1) < PSTARTX(pan2) ) \
135      ? FALSE : TRUE)
136 
137 
138 /*+-------------------------------------------------------------------------
139 	Compute the intersection rectangle of two overlapping rectangles
140 ---------------------------------------------------------------------------*/
141 #define COMPUTE_INTERSECTION(pan1,pan2,ix1,ix2,iy1,iy2)\
142    ix1 = (PSTARTX(pan1) < PSTARTX(pan2)) ? PSTARTX(pan2) : PSTARTX(pan1);\
143    ix2 = (PENDX(pan1)   < PENDX(pan2))   ? PENDX(pan1)   : PENDX(pan2);\
144    iy1 = (PSTARTY(pan1) < PSTARTY(pan2)) ? PSTARTY(pan2) : PSTARTY(pan1);\
145    iy2 = (PENDY(pan1)   < PENDY(pan2))   ? PENDY(pan1)   : PENDY(pan2);\
146    assert((ix1<=ix2) && (iy1<=iy2))
147 
148 
149 /*+-------------------------------------------------------------------------
150 	Walk through the panel stack starting at the given location and
151         check for intersections; overlapping panels are "touched", so they
152         are incrementally overwriting cells that should be hidden.
153         If the "touch" flag is set, the panel gets touched before it is
154         updated.
155 ---------------------------------------------------------------------------*/
156 #define PANEL_UPDATE(pan,panstart)\
157 {  PANEL* pan2 = ((panstart) ? (panstart) : _nc_bottom_panel);\
158    while(pan2 && pan2->win) {\
159       if ((pan2 != pan) && PANELS_OVERLAPPED(pan,pan2)) {\
160         int y, ix1, ix2, iy1, iy2;\
161         COMPUTE_INTERSECTION(pan, pan2, ix1, ix2, iy1, iy2);\
162 	for(y = iy1; y <= iy2; y++) {\
163 	  if (is_linetouched(pan->win,y - PSTARTY(pan))) {\
164             struct ldat* line = &(pan2->win->_line[y - PSTARTY(pan2)]);\
165             CHANGED_RANGE(line, ix1 - PSTARTX(pan2), ix2 - PSTARTX(pan2));\
166           }\
167 	}\
168       }\
169       pan2 = pan2->above;\
170    }\
171 }
172 
173 /*+-------------------------------------------------------------------------
174 	Remove panel from stack.
175 ---------------------------------------------------------------------------*/
176 #define PANEL_UNLINK(pan,err) \
177 {  err = ERR;\
178    if (pan) {\
179      if (IS_LINKED(pan)) {\
180        if ((pan)->below)\
181          (pan)->below->above = (pan)->above;\
182        if ((pan)->above)\
183          (pan)->above->below = (pan)->below;\
184        if ((pan) == _nc_bottom_panel) \
185          _nc_bottom_panel = (pan)->above;\
186        if ((pan) == _nc_top_panel) \
187          _nc_top_panel = (pan)->below;\
188        err = OK;\
189      }\
190      (pan)->above = (pan)->below = (PANEL*)0;\
191    }\
192 }
193 
194 #define HIDE_PANEL(pan,err,err_if_unlinked)\
195   if (IS_LINKED(pan)) {\
196     Touchpan(pan);\
197     PANEL_UPDATE(pan,(PANEL*)0);\
198     PANEL_UNLINK(pan,err);\
199   } \
200   else {\
201       err = err_if_unlinked;\
202   }
203 
204 #if NCURSES_SP_FUNCS
205 /* These may become later renamed and part of panel.h and the public API */
206 extern NCURSES_EXPORT(void) NCURSES_SP_NAME(_nc_update_panels)(SCREEN*);
207 #endif
208 /* *INDENT-ON* */
209 
210 #endif /* NCURSES_PANEL_PRIV_H */
211