1/**************************************************************************** 2 * Copyright (c) 1998-2006,2007 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 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 * 31 * and: Eric S. Raymond <esr@snark.thyrsus.com> * 32 * * 33 * Rewritten 2001-2004 to support wide-characters by * 34 * Sven Verdoolaege * 35 * Thomas Dickey * 36 ****************************************************************************/ 37 38/* 39** lib_addstr.c 40* 41** The routines waddnstr(), waddchnstr(). 42** 43*/ 44 45#include <curses.priv.h> 46 47MODULE_ID("$Id: lib_addstr.c,v 1.48 2007/10/13 19:56:57 tom Exp $") 48 49NCURSES_EXPORT(int) 50waddnstr(WINDOW *win, const char *astr, int n) 51{ 52 const char *str = astr; 53 int code = ERR; 54 55 T((T_CALLED("waddnstr(%p,%s,%d)"), win, _nc_visbufn(astr, n), n)); 56 57 if (win && (str != 0)) { 58 TR(TRACE_VIRTPUT | TRACE_ATTRS, 59 ("... current %s", _traceattr(WINDOW_ATTRS(win)))); 60 code = OK; 61 if (n < 0) 62 n = (int) strlen(astr); 63 64 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n)); 65 while ((n-- > 0) && (*str != '\0')) { 66 NCURSES_CH_T ch; 67 TR(TRACE_VIRTPUT, ("*str = %#o", UChar(*str))); 68 SetChar(ch, UChar(*str++), A_NORMAL); 69 if (_nc_waddch_nosync(win, ch) == ERR) { 70 code = ERR; 71 break; 72 } 73 } 74 _nc_synchook(win); 75 } 76 TR(TRACE_VIRTPUT, ("waddnstr returns %d", code)); 77 returnCode(code); 78} 79 80NCURSES_EXPORT(int) 81waddchnstr(WINDOW *win, const chtype *astr, int n) 82{ 83 NCURSES_SIZE_T y, x; 84 int code = OK; 85 int i; 86 struct ldat *line; 87 88 T((T_CALLED("waddchnstr(%p,%p,%d)"), win, astr, n)); 89 90 if (!win) 91 returnCode(ERR); 92 93 y = win->_cury; 94 x = win->_curx; 95 if (n < 0) { 96 const chtype *str; 97 n = 0; 98 for (str = (const chtype *) astr; *str != 0; str++) 99 n++; 100 } 101 if (n > win->_maxx - x + 1) 102 n = win->_maxx - x + 1; 103 if (n == 0) 104 returnCode(code); 105 106 line = &(win->_line[y]); 107 for (i = 0; i < n && ChCharOf(astr[i]) != '\0'; ++i) { 108 SetChar2(line->text[i + x], astr[i]); 109 } 110 CHANGED_RANGE(line, x, x + n - 1); 111 112 _nc_synchook(win); 113 returnCode(code); 114} 115 116#if USE_WIDEC_SUPPORT 117 118NCURSES_EXPORT(int) 119_nc_wchstrlen(const cchar_t *s) 120{ 121 int result = 0; 122 while (CharOf(s[result]) != L'\0') { 123 result++; 124 } 125 return result; 126} 127 128NCURSES_EXPORT(int) 129wadd_wchnstr(WINDOW *win, const cchar_t *astr, int n) 130{ 131 static const NCURSES_CH_T blank = NewChar(BLANK_TEXT); 132 NCURSES_SIZE_T y; 133 NCURSES_SIZE_T x; 134 int code = OK; 135 struct ldat *line; 136 int i, j, start, len, end; 137 138 T((T_CALLED("wadd_wchnstr(%p,%s,%d)"), win, _nc_viscbuf(astr, n), n)); 139 140 if (!win) 141 returnCode(ERR); 142 143 y = win->_cury; 144 x = win->_curx; 145 if (n < 0) { 146 n = _nc_wchstrlen(astr); 147 } 148 if (n > win->_maxx - x + 1) 149 n = win->_maxx - x + 1; 150 if (n == 0) 151 returnCode(code); 152 153 line = &(win->_line[y]); 154 start = x; 155 end = x + n - 1; 156 157 /* 158 * Reset orphaned cells of multi-column characters that extend up to the 159 * new string's location to blanks. 160 */ 161 if (x > 0 && isWidecExt(line->text[x])) { 162 for (i = 0; i <= x; ++i) { 163 if (!isWidecExt(line->text[x - i])) { 164 /* must be isWidecBase() */ 165 start -= i; 166 while (i > 0) { 167 line->text[x - i--] = _nc_render(win, blank); 168 } 169 break; 170 } 171 } 172 } 173 174 /* 175 * Copy the new string to the window. 176 */ 177 for (i = 0; i < n && CharOf(astr[i]) != L'\0' && x <= win->_maxx; ++i) { 178 if (isWidecExt(astr[i])) 179 continue; 180 181 len = wcwidth(CharOf(astr[i])); 182 183 if (x + len - 1 <= win->_maxx) { 184 line->text[x] = _nc_render(win, astr[i]); 185 if (len > 1) { 186 for (j = 0; j < len; ++j) { 187 if (j != 0) { 188 line->text[x + j] = line->text[x]; 189 } 190 SetWidecExt(line->text[x + j], j); 191 } 192 } 193 x += len; 194 end += len - 1; 195 } else { 196 break; 197 } 198 } 199 200 /* 201 * Set orphaned cells of multi-column characters which lie after the new 202 * string to blanks. 203 */ 204 while (x <= win->_maxx && isWidecExt(line->text[x])) { 205 line->text[x] = _nc_render(win, blank); 206 ++end; 207 ++x; 208 } 209 CHANGED_RANGE(line, start, end); 210 211 _nc_synchook(win); 212 returnCode(code); 213} 214 215NCURSES_EXPORT(int) 216waddnwstr(WINDOW *win, const wchar_t *str, int n) 217{ 218 int code = ERR; 219 220 T((T_CALLED("waddnwstr(%p,%s,%d)"), win, _nc_viswbufn(str, n), n)); 221 222 if (win && (str != 0)) { 223 TR(TRACE_VIRTPUT | TRACE_ATTRS, 224 ("... current %s", _traceattr(WINDOW_ATTRS(win)))); 225 code = OK; 226 if (n < 0) 227 n = (int) wcslen(str); 228 229 TR(TRACE_VIRTPUT, ("str is not null, length = %d", n)); 230 while ((n-- > 0) && (*str != L('\0'))) { 231 NCURSES_CH_T ch; 232 TR(TRACE_VIRTPUT, ("*str[0] = %#lx", (unsigned long) *str)); 233 SetChar(ch, *str++, A_NORMAL); 234 if (wadd_wch(win, &ch) == ERR) { 235 code = ERR; 236 break; 237 } 238 } 239 _nc_synchook(win); 240 } 241 TR(TRACE_VIRTPUT, ("waddnwstr returns %d", code)); 242 returnCode(code); 243} 244 245#endif 246