ncursesw-morphos/ncurses/base/lib_slkset.c

158 lines
4.9 KiB
C
Raw Normal View History

1998-03-01 12:21:12 +08:00
/****************************************************************************
* Copyright (c) 1998-2007,2009 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/****************************************************************************
2005-10-10 02:41:57 +08:00
* Author: Juergen Pfeifer *
* and: Thomas E. Dickey *
1998-03-01 12:21:12 +08:00
****************************************************************************/
/*
* lib_slkset.c
* Set soft label text.
*/
#include <curses.priv.h>
#include <ctype.h>
2005-10-10 02:41:57 +08:00
#if USE_WIDEC_SUPPORT
2006-12-18 12:32:42 +08:00
#if HAVE_WCTYPE_H
2005-10-10 02:41:57 +08:00
#include <wctype.h>
#endif
2006-12-18 12:32:42 +08:00
#endif
2005-10-10 02:41:57 +08:00
MODULE_ID("$Id: lib_slkset.c,v 1.20 2009/10/24 22:12:21 tom Exp $")
1998-03-01 12:21:12 +08:00
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
NCURSES_SP_NAME(slk_set) (NCURSES_SP_DCLx int i, const char *astr, int format)
1998-03-01 12:21:12 +08:00
{
SLK *slk;
2004-02-09 10:15:26 +08:00
int offset;
2005-10-10 02:41:57 +08:00
int numchrs;
int numcols;
int limit;
2002-10-13 11:35:53 +08:00
const char *str = astr;
const char *p;
1998-03-01 12:21:12 +08:00
T((T_CALLED("slk_set(%p, %d, \"%s\", %d)"), (void *) SP_PARM, i, str, format));
1998-03-01 12:21:12 +08:00
if (SP_PARM == 0
|| (slk = SP_PARM->_slk) == 0
|| i < 1
|| i > slk->labcnt
|| format < 0
|| format > 2)
2002-10-13 11:35:53 +08:00
returnCode(ERR);
if (str == 0)
2002-10-13 11:35:53 +08:00
str = "";
2005-10-10 02:41:57 +08:00
--i; /* Adjust numbering of labels */
1998-03-01 12:21:12 +08:00
limit = MAX_SKEY_LEN(SP_PARM->slk_format);
2002-10-13 11:35:53 +08:00
while (isspace(UChar(*str)))
str++; /* skip over leading spaces */
p = str;
2005-10-10 02:41:57 +08:00
#if USE_WIDEC_SUPPORT
numcols = 0;
while (*p != 0) {
mbstate_t state;
wchar_t wc;
size_t need;
init_mb(state);
need = mbrtowc(0, p, strlen(p), &state);
if (need == (size_t) -1)
break;
mbrtowc(&wc, p, need, &state);
if (!iswprint((wint_t) wc))
break;
if (wcwidth(wc) + numcols > limit)
break;
numcols += wcwidth(wc);
p += need;
}
numchrs = (p - str);
#else
2002-10-13 11:35:53 +08:00
while (isprint(UChar(*p)))
p++; /* The first non-print stops */
1998-03-01 12:21:12 +08:00
2005-10-10 02:41:57 +08:00
numcols = (p - str);
if (numcols > limit)
numcols = limit;
numchrs = numcols;
#endif
1998-03-01 12:21:12 +08:00
2005-10-10 02:41:57 +08:00
FreeIfNeeded(slk->ent[i].ent_text);
if ((slk->ent[i].ent_text = strdup(str)) == 0)
returnCode(ERR);
slk->ent[i].ent_text[numchrs] = '\0';
if ((slk->ent[i].form_text = (char *) _nc_doalloc(slk->ent[i].form_text,
(unsigned) (limit +
numchrs + 1))
) == 0)
returnCode(ERR);
1998-03-01 12:21:12 +08:00
2002-10-13 11:35:53 +08:00
switch (format) {
2004-02-09 10:15:26 +08:00
default:
2002-10-13 11:35:53 +08:00
case 0: /* left-justified */
2004-02-09 10:15:26 +08:00
offset = 0;
2002-10-13 11:35:53 +08:00
break;
case 1: /* centered */
2005-10-10 02:41:57 +08:00
offset = (limit - numcols) / 2;
2002-10-13 11:35:53 +08:00
break;
case 2: /* right-justified */
2005-10-10 02:41:57 +08:00
offset = limit - numcols;
2002-10-13 11:35:53 +08:00
break;
}
2005-10-10 02:41:57 +08:00
if (offset <= 0)
offset = 0;
else
memset(slk->ent[i].form_text, ' ', (unsigned) offset);
2004-02-09 10:15:26 +08:00
memcpy(slk->ent[i].form_text + offset,
slk->ent[i].ent_text,
2005-10-10 02:41:57 +08:00
(unsigned) numchrs);
if (offset < limit) {
memset(slk->ent[i].form_text + offset + numchrs,
' ',
(unsigned) (limit - (offset + numcols)));
}
slk->ent[i].form_text[numchrs - numcols + limit] = 0;
2002-10-13 11:35:53 +08:00
slk->ent[i].dirty = TRUE;
returnCode(OK);
1998-03-01 12:21:12 +08:00
}
#if NCURSES_SP_FUNCS
NCURSES_EXPORT(int)
slk_set(int i, const char *astr, int format)
{
return NCURSES_SP_NAME(slk_set) (CURRENT_SCREEN, i, astr, format);
}
#endif