1/*	$OpenBSD: panel.priv.h,v 1.11 2023/10/17 09:52:10 nicm Exp $	*/
2/****************************************************************************
3 * Copyright 2020 Thomas E. Dickey                                          *
4 * Copyright 1998-2014,2017 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/* $Id: panel.priv.h,v 1.11 2023/10/17 09:52:10 nicm Exp $ */
32
33#ifndef NCURSES_PANEL_PRIV_H
34#define NCURSES_PANEL_PRIV_H 1
35/* *INDENT-OFF* */
36
37#if HAVE_CONFIG_H
38#  include <ncurses_cfg.h>
39#endif
40
41#include <stdlib.h>
42#include <string.h>
43#include <assert.h>
44
45struct screen;              /* forward declaration */
46
47#include "curses.priv.h"    /* includes nc_panel.h */
48
49#define NCURSES_OPAQUE_PANEL 0
50
51#include "panel.h"
52
53#ifdef TRACE
54   extern PANEL_EXPORT(const char *) _nc_my_visbuf (const void *, int);
55#  ifdef TRACE_TXT
56#    define USER_PTR(ptr,n) _nc_visbuf2(n, (const char *)ptr)
57#  else
58#    define USER_PTR(ptr,n) _nc_my_visbuf((const char *)ptr, n)
59#  endif
60
61#  define returnPanel(code)	TRACE_RETURN1(code,panel)
62
63   extern PANEL_EXPORT(PANEL *) _nc_retrace_panel (PANEL *);
64   extern PANEL_EXPORT(void) _nc_dPanel (const char*, const PANEL*);
65   extern PANEL_EXPORT(void) _nc_dStack (const char*, int, const PANEL*);
66   extern PANEL_EXPORT(void) _nc_Wnoutrefresh (const PANEL*);
67   extern PANEL_EXPORT(void) _nc_Touchpan (const PANEL*);
68   extern PANEL_EXPORT(void) _nc_Touchline (const PANEL*, int, int);
69
70#  define dBug(x) _tracef x
71#  define dPanel(text,pan) _nc_dPanel(text,pan)
72#  define dStack(fmt,num,pan) _nc_dStack(fmt,num,pan)
73#  define Wnoutrefresh(pan) _nc_Wnoutrefresh(pan)
74#  define Touchpan(pan) _nc_Touchpan(pan)
75#  define Touchline(pan,start,count) _nc_Touchline(pan,start,count)
76#else /* !TRACE */
77#  define returnPanel(code)	return code
78#  define dBug(x)
79#  define dPanel(text,pan)
80#  define dStack(fmt,num,pan)
81#  define Wnoutrefresh(pan) wnoutrefresh((pan)->win)
82#  define Touchpan(pan) touchwin((pan)->win)
83#  define Touchline(pan,start,count) touchline((pan)->win,start,count)
84#endif
85
86#if NCURSES_SP_FUNCS
87#define GetScreenHook(sp) \
88			struct panelhook* ph = NCURSES_SP_NAME(_nc_panelhook)(sp)
89#define GetPanelHook(pan) \
90			GetScreenHook(pan ? _nc_screen_of((pan)->win) : 0)
91#define GetWindowHook(win) \
92			SCREEN* sp = _nc_screen_of(win); \
93			GetScreenHook(sp)
94#define GetHook(pan)	SCREEN* sp = _nc_screen_of(pan->win); \
95			GetScreenHook(sp)
96
97#define _nc_stdscr_pseudo_panel ((ph)->stdscr_pseudo_panel)
98#define _nc_top_panel           ((ph)->top_panel)
99#define _nc_bottom_panel        ((ph)->bottom_panel)
100
101#else	/* !NCURSES_SP_FUNCS */
102
103#define GetScreenHook(sp) /* nothing */
104#define GetPanelHook(pan) /* nothing */
105#define GetWindowHook(win) /* nothing */
106#define GetHook(pan) /* nothing */
107
108#define _nc_stdscr_pseudo_panel _nc_panelhook()->stdscr_pseudo_panel
109#define _nc_top_panel           _nc_panelhook()->top_panel
110#define _nc_bottom_panel        _nc_panelhook()->bottom_panel
111
112#endif	/* NCURSES_SP_FUNCS */
113
114#define EMPTY_STACK() (_nc_top_panel == _nc_bottom_panel)
115#define Is_Bottom(p)  (((p) != (PANEL*)0) && !EMPTY_STACK() && (_nc_bottom_panel->above == (p)))
116#define Is_Top(p)     (((p) != (PANEL*)0) && !EMPTY_STACK() && (_nc_top_panel == (p)))
117#define Is_Pseudo(p)  (((p) != (PANEL*)0) && ((p) == _nc_bottom_panel))
118
119/*+-------------------------------------------------------------------------
120	IS_LINKED(pan) - check to see if panel is in the stack
121--------------------------------------------------------------------------*/
122/* This works! The only case where it would fail is, when the list has
123   only one element. But this could only be the pseudo panel at the bottom */
124#define IS_LINKED(p) (((p)->above || (p)->below ||((p)==_nc_bottom_panel)) ? TRUE : FALSE)
125
126#define PSTARTX(pan) ((pan)->win->_begx)
127#define PENDX(pan)   ((pan)->win->_begx + getmaxx((pan)->win) - 1)
128#define PSTARTY(pan) ((pan)->win->_begy)
129#define PENDY(pan)   ((pan)->win->_begy + getmaxy((pan)->win) - 1)
130
131/*+-------------------------------------------------------------------------
132	PANELS_OVERLAPPED(pan1,pan2) - check panel overlapped
133---------------------------------------------------------------------------*/
134#define PANELS_OVERLAPPED(pan1,pan2) \
135(( !(pan1) || !(pan2) || \
136       PSTARTY(pan1) > PENDY(pan2) || PENDY(pan1) < PSTARTY(pan2) ||\
137       PSTARTX(pan1) > PENDX(pan2) || PENDX(pan1) < PSTARTX(pan2) ) \
138     ? FALSE : TRUE)
139
140
141/*+-------------------------------------------------------------------------
142	Compute the intersection rectangle of two overlapping rectangles
143---------------------------------------------------------------------------*/
144#define COMPUTE_INTERSECTION(pan1,pan2,ix1,ix2,iy1,iy2)\
145   ix1 = (PSTARTX(pan1) < PSTARTX(pan2)) ? PSTARTX(pan2) : PSTARTX(pan1);\
146   ix2 = (PENDX(pan1)   < PENDX(pan2))   ? PENDX(pan1)   : PENDX(pan2);\
147   iy1 = (PSTARTY(pan1) < PSTARTY(pan2)) ? PSTARTY(pan2) : PSTARTY(pan1);\
148   iy2 = (PENDY(pan1)   < PENDY(pan2))   ? PENDY(pan1)   : PENDY(pan2);\
149   assert((ix1<=ix2) && (iy1<=iy2))
150
151
152/*+-------------------------------------------------------------------------
153	Walk through the panel stack starting at the given location and
154        check for intersections; overlapping panels are "touched", so they
155        are incrementally overwriting cells that should be hidden.
156        If the "touch" flag is set, the panel gets touched before it is
157        updated.
158---------------------------------------------------------------------------*/
159#define PANEL_UPDATE(pan,panstart)\
160{  PANEL* pan2 = ((panstart) ? (panstart) : _nc_bottom_panel);\
161   while(pan2 && pan2->win) {\
162      if ((pan2 != pan) && PANELS_OVERLAPPED(pan,pan2)) {\
163        int y, ix1, ix2, iy1, iy2;\
164        COMPUTE_INTERSECTION(pan, pan2, ix1, ix2, iy1, iy2);\
165	for(y = iy1; y <= iy2; y++) {\
166	  if (is_linetouched(pan->win,y - PSTARTY(pan))) {\
167            struct ldat* line = &(pan2->win->_line[y - PSTARTY(pan2)]);\
168            CHANGED_RANGE(line, ix1 - PSTARTX(pan2), ix2 - PSTARTX(pan2));\
169          }\
170	}\
171      }\
172      pan2 = pan2->above;\
173   }\
174}
175
176/*+-------------------------------------------------------------------------
177	Remove panel from stack.
178---------------------------------------------------------------------------*/
179#define PANEL_UNLINK(pan,err) \
180{  err = ERR;\
181   if (pan) {\
182     if (IS_LINKED(pan)) {\
183       if ((pan)->below)\
184         (pan)->below->above = (pan)->above;\
185       if ((pan)->above)\
186         (pan)->above->below = (pan)->below;\
187       if ((pan) == _nc_bottom_panel) \
188         _nc_bottom_panel = (pan)->above;\
189       if ((pan) == _nc_top_panel) \
190         _nc_top_panel = (pan)->below;\
191       err = OK;\
192     }\
193     (pan)->above = (pan)->below = (PANEL*)0;\
194   }\
195}
196
197#define HIDE_PANEL(pan,err,err_if_unlinked)\
198  if (IS_LINKED(pan)) {\
199    Touchpan(pan);\
200    PANEL_UPDATE(pan,(PANEL*)0);\
201    PANEL_UNLINK(pan,err);\
202  } \
203  else {\
204      err = err_if_unlinked;\
205  }
206
207#if NCURSES_SP_FUNCS
208/* These may become later renamed and part of panel.h and the public API */
209extern PANEL_EXPORT(void) NCURSES_SP_NAME(_nc_update_panels)(SCREEN*);
210#endif
211/* *INDENT-ON* */
212
213#endif /* NCURSES_PANEL_PRIV_H */
214