xref: /openbsd/lib/libcurses/base/lib_slkrefr.c (revision 4bdff4be)
1 /* $OpenBSD: lib_slkrefr.c,v 1.6 2023/10/17 09:52:09 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright 2020,2021 Thomas E. Dickey                                     *
5  * Copyright 1998-2013,2014 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> 1992,1995               *
34  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
35  *     and: Juergen Pfeifer                         1996-on                 *
36  *     and: Thomas E. Dickey                                                *
37  ****************************************************************************/
38 
39 /*
40  *	lib_slkrefr.c
41  *	Write SLK window to the (virtual) screen.
42  */
43 #include <curses.priv.h>
44 
45 #ifndef CUR
46 #define CUR SP_TERMTYPE
47 #endif
48 
49 MODULE_ID("$Id: lib_slkrefr.c,v 1.6 2023/10/17 09:52:09 nicm Exp $")
50 
51 #ifdef USE_TERM_DRIVER
52 #define NumLabels    InfoOf(SP_PARM).numlabels
53 #else
54 #define NumLabels    num_labels
55 #endif
56 
57 /*
58  * Paint the info line for the PC style SLK emulation.
59  */
60 static void
61 slk_paint_info(WINDOW *win)
62 {
63     SCREEN *sp = _nc_screen_of(win);
64 
65     if (win && sp && (sp->slk_format == 4)) {
66 	int i;
67 
68 	(void) mvwhline(win, 0, 0, 0, getmaxx(win));
69 	wmove(win, 0, 0);
70 
71 	for (i = 0; i < sp->_slk->maxlab; i++) {
72 	    mvwprintw(win, 0, sp->_slk->ent[i].ent_x, "F%d", i + 1);
73 	}
74     }
75 }
76 
77 /*
78  * Write the soft labels to the soft-key window.
79  */
80 static void
81 slk_intern_refresh(SCREEN *sp)
82 {
83     int i;
84     int fmt;
85     SLK *slk;
86     int numlab;
87 
88     if (sp == 0)
89 	return;
90 
91     slk = sp->_slk;
92     fmt = sp->slk_format;
93     numlab = NumLabels;
94 
95     if (slk->hidden)
96 	return;
97 
98     for (i = 0; i < slk->labcnt; i++) {
99 	if (slk->dirty || slk->ent[i].dirty) {
100 	    if (slk->ent[i].visible) {
101 		if (numlab > 0 && SLK_STDFMT(fmt)) {
102 #ifdef USE_TERM_DRIVER
103 		    CallDriver_2(sp, td_hwlabel, i + 1, slk->ent[i].form_text);
104 #else
105 		    if (i < num_labels) {
106 			NCURSES_PUTP2("plab_norm",
107 				      TPARM_2(plab_norm,
108 					      i + 1,
109 					      slk->ent[i].form_text));
110 		    }
111 #endif
112 		} else {
113 		    if (fmt == 4)
114 			slk_paint_info(slk->win);
115 		    wmove(slk->win, SLK_LINES(fmt) - 1, slk->ent[i].ent_x);
116 		    (void) wattrset(slk->win, (int) AttrOf(slk->attr));
117 		    waddstr(slk->win, slk->ent[i].form_text);
118 		    /* if we simulate SLK's, it is looking much more
119 		       natural to use the current ATTRIBUTE also
120 		       for the label window */
121 		    (void) wattrset(slk->win, (int) WINDOW_ATTRS(StdScreen(sp)));
122 		}
123 	    }
124 	    slk->ent[i].dirty = FALSE;
125 	}
126     }
127     slk->dirty = FALSE;
128 
129     if (numlab > 0) {
130 #ifdef USE_TERM_DRIVER
131 	CallDriver_1(sp, td_hwlabelOnOff, slk->hidden ? FALSE : TRUE);
132 #else
133 	if (slk->hidden) {
134 	    NCURSES_PUTP2("label_off", label_off);
135 	} else {
136 	    NCURSES_PUTP2("label_on", label_on);
137 	}
138 #endif
139     }
140 }
141 
142 /*
143  * Refresh the soft labels.
144  */
145 NCURSES_EXPORT(int)
146 NCURSES_SP_NAME(slk_noutrefresh) (NCURSES_SP_DCL0)
147 {
148     T((T_CALLED("slk_noutrefresh(%p)"), (void *) SP_PARM));
149 
150     if (SP_PARM == 0 || SP_PARM->_slk == 0)
151 	returnCode(ERR);
152     if (SP_PARM->_slk->hidden)
153 	returnCode(OK);
154     slk_intern_refresh(SP_PARM);
155 
156     returnCode(wnoutrefresh(SP_PARM->_slk->win));
157 }
158 
159 #if NCURSES_SP_FUNCS
160 NCURSES_EXPORT(int)
161 slk_noutrefresh(void)
162 {
163     return NCURSES_SP_NAME(slk_noutrefresh) (CURRENT_SCREEN);
164 }
165 #endif
166 
167 /*
168  * Refresh the soft labels.
169  */
170 NCURSES_EXPORT(int)
171 NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_DCL0)
172 {
173     T((T_CALLED("slk_refresh(%p)"), (void *) SP_PARM));
174 
175     if (SP_PARM == 0 || SP_PARM->_slk == 0)
176 	returnCode(ERR);
177     if (SP_PARM->_slk->hidden)
178 	returnCode(OK);
179     slk_intern_refresh(SP_PARM);
180 
181     returnCode(wrefresh(SP_PARM->_slk->win));
182 }
183 
184 #if NCURSES_SP_FUNCS
185 NCURSES_EXPORT(int)
186 slk_refresh(void)
187 {
188     return NCURSES_SP_NAME(slk_refresh) (CURRENT_SCREEN);
189 }
190 #endif
191