1/****************************************************************************
2 * Copyright (c) 2004,2006 Free Software Foundation, Inc.                   *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/*
30**	lib_add_wch.c
31**
32**	The routine wadd_wch().
33**
34*/
35
36#include <curses.priv.h>
37
38MODULE_ID("$Id: lib_add_wch.c,v 1.6 2006/12/02 21:19:17 tom Exp $")
39
40NCURSES_EXPORT(int)
41wadd_wch(WINDOW *win, const cchar_t *wch)
42{
43    PUTC_DATA;
44    int n;
45    int code = ERR;
46
47    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"), win,
48				      _tracech_t(wch)));
49
50    if (win != 0) {
51	PUTC_INIT;
52	for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
53	    attr_t attrs = (wch->attr & A_ATTRIBUTES);
54
55	    if ((PUTC_ch = wch->chars[PUTC_i]) == L'\0')
56		break;
57	    if ((PUTC_n = wcrtomb(PUTC_buf, PUTC_ch, &PUT_st)) <= 0) {
58		code = ERR;
59		if (is8bits(PUTC_ch))
60		    code = waddch(win, UChar(PUTC_ch) | attrs);
61		break;
62	    }
63	    for (n = 0; n < PUTC_n; n++) {
64		if ((code = waddch(win, UChar(PUTC_buf[n]) | attrs)) == ERR) {
65		    break;
66		}
67	    }
68	    if (code == ERR)
69		break;
70	}
71    }
72
73    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
74    return (code);
75}
76
77NCURSES_EXPORT(int)
78wecho_wchar(WINDOW *win, const cchar_t *wch)
79{
80    PUTC_DATA;
81    int n;
82    int code = ERR;
83
84    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wecho_wchar(%p, %s)"), win,
85				      _tracech_t(wch)));
86
87    if (win != 0) {
88	PUTC_INIT;
89	for (PUTC_i = 0; PUTC_i < CCHARW_MAX; ++PUTC_i) {
90	    attr_t attrs = (wch->attr & A_ATTRIBUTES);
91
92	    if ((PUTC_ch = wch->chars[PUTC_i]) == L'\0')
93		break;
94	    if ((PUTC_n = wcrtomb(PUTC_buf, PUTC_ch, &PUT_st)) <= 0) {
95		code = ERR;
96		if (is8bits(PUTC_ch))
97		    code = waddch(win, UChar(PUTC_ch) | attrs);
98		break;
99	    }
100	    for (n = 0; n < PUTC_n; n++) {
101		if ((code = waddch(win, UChar(PUTC_buf[n]) | attrs)) == ERR) {
102		    break;
103		}
104	    }
105	    if (code == ERR)
106		break;
107	}
108	wrefresh(win);
109    }
110
111    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
112    return (code);
113}
114