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