1 /* $OpenBSD: lib_ins_wch.c,v 1.1 2010/09/06 17:26:17 nicm Exp $ */
2 
3 /****************************************************************************
4  * Copyright (c) 2002-2003,2005 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: Thomas Dickey 2002                                              *
33  ****************************************************************************/
34 
35 /*
36 **	lib_ins_wch.c
37 **
38 **	The routine wins_wch().
39 **
40 */
41 
42 #include <curses.priv.h>
43 
44 MODULE_ID("$Id: lib_ins_wch.c,v 1.1 2010/09/06 17:26:17 nicm Exp $")
45 
46 /*
47  * Insert the given character, updating the current location to simplify
48  * inserting a string.
49  */
50 static int
51 _nc_insert_wch(WINDOW *win, const cchar_t *wch)
52 {
53     int cells = wcwidth(CharOf(CHDEREF(wch)));
54     int cell;
55 
56     if (cells <= 0)
57 	cells = 1;
58 
59     if (win->_curx <= win->_maxx) {
60 	struct ldat *line = &(win->_line[win->_cury]);
61 	NCURSES_CH_T *end = &(line->text[win->_curx]);
62 	NCURSES_CH_T *temp1 = &(line->text[win->_maxx]);
63 	NCURSES_CH_T *temp2 = temp1 - cells;
64 
65 	CHANGED_TO_EOL(line, win->_curx, win->_maxx);
66 	while (temp1 > end)
67 	    *temp1-- = *temp2--;
68 
69 	*temp1 = _nc_render(win, *wch);
70 	for (cell = 1; cell < cells; ++cell) {
71 	    SetWidecExt(temp1[cell], cell);
72 	}
73 
74 	win->_curx++;
75     }
76     return OK;
77 }
78 
79 NCURSES_EXPORT(int)
80 wins_wch(WINDOW *win, const cchar_t *wch)
81 {
82     NCURSES_SIZE_T oy;
83     NCURSES_SIZE_T ox;
84     int code = ERR;
85 
86     T((T_CALLED("wins_wch(%p, %s)"), win, _tracecchar_t(wch)));
87 
88     if (win != 0) {
89 	oy = win->_cury;
90 	ox = win->_curx;
91 
92 	code = _nc_insert_wch(win, wch);
93 
94 	win->_curx = ox;
95 	win->_cury = oy;
96 	_nc_synchook(win);
97     }
98     returnCode(code);
99 }
100 
101 NCURSES_EXPORT(int)
102 wins_nwstr(WINDOW *win, const wchar_t *wstr, int n)
103 {
104     int code = ERR;
105     NCURSES_SIZE_T oy;
106     NCURSES_SIZE_T ox;
107     const wchar_t *cp;
108 
109     T((T_CALLED("wins_nwstr(%p,%s,%d)"), win, _nc_viswbufn(wstr, n), n));
110 
111     if (win != 0
112 	&& wstr != 0) {
113 	if (n < 1)
114 	    n = wcslen(wstr);
115 	code = OK;
116 	if (n > 0) {
117 	    oy = win->_cury;
118 	    ox = win->_curx;
119 	    for (cp = wstr; *cp && ((cp - wstr) < n); cp++) {
120 		int len = wcwidth(*cp);
121 
122 		if (len != 1 || !is8bits(*cp)) {
123 		    cchar_t tmp_cchar;
124 		    wchar_t tmp_wchar = *cp;
125 		    memset(&tmp_cchar, 0, sizeof(tmp_cchar));
126 		    (void) setcchar(&tmp_cchar,
127 				    &tmp_wchar,
128 				    WA_NORMAL,
129 				    0,
130 				    (void *) 0);
131 		    code = _nc_insert_wch(win, &tmp_cchar);
132 		} else {
133 		    /* tabs, other ASCII stuff */
134 		    code = _nc_insert_ch(win, (chtype) (*cp));
135 		}
136 		if (code != OK)
137 		    break;
138 	    }
139 
140 	    win->_curx = ox;
141 	    win->_cury = oy;
142 	    _nc_synchook(win);
143 	}
144     }
145     returnCode(code);
146 }
147