197049Speter/****************************************************************************
2166124Srafan * Copyright (c) 2002-2003,2005 Free Software Foundation, Inc.              *
397049Speter *                                                                          *
497049Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
597049Speter * copy of this software and associated documentation files (the            *
697049Speter * "Software"), to deal in the Software without restriction, including      *
797049Speter * without limitation the rights to use, copy, modify, merge, publish,      *
897049Speter * distribute, distribute with modifications, sublicense, and/or sell       *
997049Speter * copies of the Software, and to permit persons to whom the Software is    *
1097049Speter * furnished to do so, subject to the following conditions:                 *
1197049Speter *                                                                          *
1297049Speter * The above copyright notice and this permission notice shall be included  *
1397049Speter * in all copies or substantial portions of the Software.                   *
1497049Speter *                                                                          *
1597049Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1697049Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1797049Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1897049Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1997049Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2097049Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2197049Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2297049Speter *                                                                          *
2397049Speter * Except as contained in this notice, the name(s) of the above copyright   *
2497049Speter * holders shall not be used in advertising or otherwise to promote the     *
2597049Speter * sale, use or other dealings in this Software without prior written       *
2697049Speter * authorization.                                                           *
2797049Speter ****************************************************************************/
2897049Speter
2997049Speter/****************************************************************************
3097049Speter *  Author: Thomas Dickey 2002                                              *
3197049Speter ****************************************************************************/
3297049Speter
3397049Speter/*
3497049Speter**	lib_ins_wch.c
3597049Speter**
3697049Speter**	The routine wins_wch().
3797049Speter**
3897049Speter*/
3997049Speter
4097049Speter#include <curses.priv.h>
4197049Speter
42166124SrafanMODULE_ID("$Id: lib_ins_wch.c,v 1.8 2005/12/03 20:24:19 tom Exp $")
4397049Speter
44166124Srafan/*
45166124Srafan * Insert the given character, updating the current location to simplify
46166124Srafan * inserting a string.
47166124Srafan */
48166124Srafanstatic int
49166124Srafan_nc_insert_wch(WINDOW *win, const cchar_t *wch)
5097049Speter{
51166124Srafan    int cells = wcwidth(CharOf(CHDEREF(wch)));
52166124Srafan    int cell;
5397049Speter
54166124Srafan    if (cells <= 0)
55166124Srafan	cells = 1;
5697049Speter
57166124Srafan    if (win->_curx <= win->_maxx) {
5897049Speter	struct ldat *line = &(win->_line[win->_cury]);
5997049Speter	NCURSES_CH_T *end = &(line->text[win->_curx]);
6097049Speter	NCURSES_CH_T *temp1 = &(line->text[win->_maxx]);
61166124Srafan	NCURSES_CH_T *temp2 = temp1 - cells;
6297049Speter
6397049Speter	CHANGED_TO_EOL(line, win->_curx, win->_maxx);
6497049Speter	while (temp1 > end)
6597049Speter	    *temp1-- = *temp2--;
6697049Speter
6797049Speter	*temp1 = _nc_render(win, *wch);
68166124Srafan	for (cell = 1; cell < cells; ++cell) {
69166124Srafan	    SetWidecExt(temp1[cell], cell);
70166124Srafan	}
71166124Srafan
72166124Srafan	win->_curx++;
73166124Srafan    }
74166124Srafan    return OK;
75166124Srafan}
76166124Srafan
77166124SrafanNCURSES_EXPORT(int)
78166124Srafanwins_wch(WINDOW *win, const cchar_t *wch)
79166124Srafan{
80166124Srafan    NCURSES_SIZE_T oy;
81166124Srafan    NCURSES_SIZE_T ox;
82166124Srafan    int code = ERR;
83166124Srafan
84166124Srafan    T((T_CALLED("wins_wch(%p, %s)"), win, _tracecchar_t(wch)));
85166124Srafan
86166124Srafan    if (win != 0) {
87166124Srafan	oy = win->_cury;
88166124Srafan	ox = win->_curx;
89166124Srafan
90166124Srafan	code = _nc_insert_wch(win, wch);
91166124Srafan
92166124Srafan	win->_curx = ox;
93166124Srafan	win->_cury = oy;
94166124Srafan	_nc_synchook(win);
95166124Srafan    }
96166124Srafan    returnCode(code);
97166124Srafan}
98166124Srafan
99166124SrafanNCURSES_EXPORT(int)
100166124Srafanwins_nwstr(WINDOW *win, const wchar_t *wstr, int n)
101166124Srafan{
102166124Srafan    int code = ERR;
103166124Srafan    NCURSES_SIZE_T oy;
104166124Srafan    NCURSES_SIZE_T ox;
105166124Srafan    const wchar_t *cp;
106166124Srafan
107166124Srafan    T((T_CALLED("wins_nwstr(%p,%s,%d)"), win, _nc_viswbufn(wstr, n), n));
108166124Srafan
109166124Srafan    if (win != 0
110166124Srafan	&& wstr != 0) {
111166124Srafan	if (n < 1)
112166124Srafan	    n = wcslen(wstr);
11397049Speter	code = OK;
114166124Srafan	if (n > 0) {
115166124Srafan	    oy = win->_cury;
116166124Srafan	    ox = win->_curx;
117166124Srafan	    for (cp = wstr; *cp && ((cp - wstr) < n); cp++) {
118166124Srafan		int len = wcwidth(*cp);
119166124Srafan
120166124Srafan		if (len != 1 || !is8bits(*cp)) {
121166124Srafan		    cchar_t tmp_cchar;
122166124Srafan		    wchar_t tmp_wchar = *cp;
123166124Srafan		    memset(&tmp_cchar, 0, sizeof(tmp_cchar));
124166124Srafan		    (void) setcchar(&tmp_cchar,
125166124Srafan				    &tmp_wchar,
126166124Srafan				    WA_NORMAL,
127166124Srafan				    0,
128166124Srafan				    (void *) 0);
129166124Srafan		    code = _nc_insert_wch(win, &tmp_cchar);
130166124Srafan		} else {
131166124Srafan		    /* tabs, other ASCII stuff */
132166124Srafan		    code = _nc_insert_ch(win, (chtype) (*cp));
133166124Srafan		}
134166124Srafan		if (code != OK)
135166124Srafan		    break;
136166124Srafan	    }
137166124Srafan
138166124Srafan	    win->_curx = ox;
139166124Srafan	    win->_cury = oy;
140166124Srafan	    _nc_synchook(win);
141166124Srafan	}
14297049Speter    }
14397049Speter    returnCode(code);
14497049Speter}
145