Back to index

tetex-bin  3.0
Functions
lib_slk_wset.c File Reference
#include <curses.priv.h>

Go to the source code of this file.

Functions

 slk_wset (int i, const wchar_t *astr, int format)

Function Documentation

slk_wset ( int  i,
const wchar_t *  astr,
int  format 
)

Definition at line 46 of file lib_slk_wset.c.

{
    static wchar_t empty[] =
    {L'\0'};
    int result = ERR;
    SLK *slk = SP->_slk;
    int offset;
    size_t arglen;
    const wchar_t *p;

    T((T_CALLED("slk_wset(%d, %s, %d)"), i, _nc_viswbuf(astr), format));

    if (astr == 0)
       astr = empty;
    arglen = wcslen(astr);
    while (iswspace(*astr)) {
       --arglen;
       ++astr;                     /* skip over leading spaces  */
    }
    p = astr;
    while (iswprint(*p))
       p++;                 /* The first non-print stops */

    arglen = (size_t) (p - astr);

    if (slk != NULL &&
       i >= 1 &&
       i <= slk->labcnt &&
       format >= 0 &&
       format <= 2) {
       char *new_text;
       size_t n;
       size_t used = 0;
       int mycols;
       mbstate_t state;

       --i;                 /* Adjust numbering of labels */

       /*
        * Reduce the actual number of columns to fit in the label field.
        */
       while (arglen != 0 && wcswidth(astr, arglen) > slk->maxlen) {
           --arglen;
       }
       mycols = wcswidth(astr, arglen);

       /*
        * translate the wide-character string to multibyte form.
        */
       memset(&state, 0, sizeof(state));

       if ((new_text = (char *) _nc_doalloc(0, MB_LEN_MAX * arglen)) == 0)
           returnCode(ERR);

       for (n = 0; n < arglen; ++n) {
           used += wcrtomb(new_text + used, astr[n], &state);
       }
       new_text[used] = '\0';

       if (used >= (size_t) (slk->maxlen + 1)) {
           if ((slk->ent[i].ent_text = (char *) _nc_doalloc(slk->ent[i].ent_text,
                                                      used + 1)) == 0)
              returnCode(ERR);
           if ((slk->ent[i].form_text = (char *) _nc_doalloc(slk->ent[i].form_text,
                                                       used + 1)) == 0)
              returnCode(ERR);
       }

       (void) strcpy(slk->ent[i].ent_text, new_text);
       free(new_text);

       sprintf(slk->ent[i].form_text, "%*s", (size_t) slk->maxlen, " ");

       switch (format) {
       default:
       case 0:              /* left-aligned */
           offset = 0;
           break;
       case 1:              /* centered */
           offset = (slk->maxlen - mycols) / 2;
           break;
       case 2:              /* right-aligned */
           offset = slk->maxlen - mycols;
           break;
       }
       if (offset < 0)
           offset = 0;
       strcpy(slk->ent[i].form_text + offset,
              slk->ent[i].ent_text);
       /*
        * Pad the display with blanks on the right, unless it is already
        * right-aligned.
        */
       if (format != 2 && mycols < slk->maxlen) {
           sprintf(slk->ent[i].form_text + offset + used,
                  "%*s",
                  slk->maxlen - (mycols - offset),
                  " ");
       }
       slk->ent[i].dirty = TRUE;
       result = OK;
    }
    returnCode(result);
}

Here is the call graph for this function: