Back to index

tetex-bin  3.0
lib_slk_wset.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 2003,2004 Free Software Foundation, Inc.                   *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey, 2003                                          *
00031  ****************************************************************************/
00032 
00033 /*
00034  *     lib_slk_wset.c
00035  *      Set soft label text.
00036  */
00037 #include <curses.priv.h>
00038 
00039 #if HAVE_WCTYPE_H
00040 #include <wctype.h>
00041 #endif
00042 
00043 MODULE_ID("$Id: lib_slk_wset.c,v 1.7 2004/05/08 16:52:10 tom Exp $")
00044 
00045 NCURSES_EXPORT(int)
00046 slk_wset(int i, const wchar_t * astr, int format)
00047 {
00048     static wchar_t empty[] =
00049     {L'\0'};
00050     int result = ERR;
00051     SLK *slk = SP->_slk;
00052     int offset;
00053     size_t arglen;
00054     const wchar_t *p;
00055 
00056     T((T_CALLED("slk_wset(%d, %s, %d)"), i, _nc_viswbuf(astr), format));
00057 
00058     if (astr == 0)
00059        astr = empty;
00060     arglen = wcslen(astr);
00061     while (iswspace(*astr)) {
00062        --arglen;
00063        ++astr;                     /* skip over leading spaces  */
00064     }
00065     p = astr;
00066     while (iswprint(*p))
00067        p++;                 /* The first non-print stops */
00068 
00069     arglen = (size_t) (p - astr);
00070 
00071     if (slk != NULL &&
00072        i >= 1 &&
00073        i <= slk->labcnt &&
00074        format >= 0 &&
00075        format <= 2) {
00076        char *new_text;
00077        size_t n;
00078        size_t used = 0;
00079        int mycols;
00080        mbstate_t state;
00081 
00082        --i;                 /* Adjust numbering of labels */
00083 
00084        /*
00085         * Reduce the actual number of columns to fit in the label field.
00086         */
00087        while (arglen != 0 && wcswidth(astr, arglen) > slk->maxlen) {
00088            --arglen;
00089        }
00090        mycols = wcswidth(astr, arglen);
00091 
00092        /*
00093         * translate the wide-character string to multibyte form.
00094         */
00095        memset(&state, 0, sizeof(state));
00096 
00097        if ((new_text = (char *) _nc_doalloc(0, MB_LEN_MAX * arglen)) == 0)
00098            returnCode(ERR);
00099 
00100        for (n = 0; n < arglen; ++n) {
00101            used += wcrtomb(new_text + used, astr[n], &state);
00102        }
00103        new_text[used] = '\0';
00104 
00105        if (used >= (size_t) (slk->maxlen + 1)) {
00106            if ((slk->ent[i].ent_text = (char *) _nc_doalloc(slk->ent[i].ent_text,
00107                                                       used + 1)) == 0)
00108               returnCode(ERR);
00109            if ((slk->ent[i].form_text = (char *) _nc_doalloc(slk->ent[i].form_text,
00110                                                        used + 1)) == 0)
00111               returnCode(ERR);
00112        }
00113 
00114        (void) strcpy(slk->ent[i].ent_text, new_text);
00115        free(new_text);
00116 
00117        sprintf(slk->ent[i].form_text, "%*s", (size_t) slk->maxlen, " ");
00118 
00119        switch (format) {
00120        default:
00121        case 0:              /* left-aligned */
00122            offset = 0;
00123            break;
00124        case 1:              /* centered */
00125            offset = (slk->maxlen - mycols) / 2;
00126            break;
00127        case 2:              /* right-aligned */
00128            offset = slk->maxlen - mycols;
00129            break;
00130        }
00131        if (offset < 0)
00132            offset = 0;
00133        strcpy(slk->ent[i].form_text + offset,
00134               slk->ent[i].ent_text);
00135        /*
00136         * Pad the display with blanks on the right, unless it is already
00137         * right-aligned.
00138         */
00139        if (format != 2 && mycols < slk->maxlen) {
00140            sprintf(slk->ent[i].form_text + offset + used,
00141                   "%*s",
00142                   slk->maxlen - (mycols - offset),
00143                   " ");
00144        }
00145        slk->ent[i].dirty = TRUE;
00146        result = OK;
00147     }
00148     returnCode(result);
00149 }