]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/widechar/lib_slk_wset.c
ncurses 5.4
[ncurses.git] / ncurses / widechar / lib_slk_wset.c
1 /****************************************************************************
2  * Copyright (c) 2003,2004 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: Thomas E. Dickey, 2003                                          *
31  ****************************************************************************/
32
33 /*
34  *      lib_slk_wset.c
35  *      Set soft label text.
36  */
37 #include <curses.priv.h>
38
39 #ifdef HAVE_WCTYPE_H
40 #include <wctype.h>
41 #endif
42
43 MODULE_ID("$Id: lib_slk_wset.c,v 1.6 2004/01/03 21:14:03 tom Exp $")
44
45 NCURSES_EXPORT(int)
46 slk_wset(int i, const wchar_t * astr, int format)
47 {
48     static wchar_t empty[] =
49     {L'\0'};
50     int result = ERR;
51     SLK *slk = SP->_slk;
52     int offset;
53     size_t arglen;
54     const wchar_t *p;
55
56     T((T_CALLED("slk_wset(%d, %s, %d)"), i, _nc_viswbuf(astr), format));
57
58     if (astr == 0)
59         astr = empty;
60     arglen = wcslen(astr);
61     while (iswspace(*astr)) {
62         --arglen;
63         ++astr;                 /* skip over leading spaces  */
64     }
65     p = astr;
66     while (iswprint(*p))
67         p++;                    /* The first non-print stops */
68
69     arglen = (size_t) (p - astr);
70
71     if (slk != NULL &&
72         i >= 1 &&
73         i <= slk->labcnt &&
74         format >= 0 &&
75         format <= 2) {
76         char *new_text;
77         size_t n;
78         size_t used = 0;
79         int mycols;
80         mbstate_t state;
81
82         --i;                    /* Adjust numbering of labels */
83
84         /*
85          * Reduce the actual number of columns to fit in the label field.
86          */
87         while (arglen != 0 && wcswidth(astr, arglen) > slk->maxlen) {
88             --arglen;
89         }
90         mycols = wcswidth(astr, arglen);
91
92         /*
93          * translate the wide-character string to multibyte form.
94          */
95         memset(&state, 0, sizeof(state));
96
97         if ((new_text = (char *) _nc_doalloc(0, MB_LEN_MAX * arglen)) == 0)
98             returnCode(ERR);
99
100         for (n = 0; n < arglen; ++n) {
101             used += wcrtomb(new_text + used, astr[n], &state);
102         }
103         new_text[used] = '\0';
104
105         if (used >= (size_t) (slk->maxlen + 1)) {
106             if ((slk->ent[i].ent_text = (char *) _nc_doalloc(slk->ent[i].ent_text,
107                                                              used + 1)) == 0)
108                 returnCode(ERR);
109             if ((slk->ent[i].form_text = (char *) _nc_doalloc(slk->ent[i].form_text,
110                                                               used + 1)) == 0)
111                 returnCode(ERR);
112         }
113
114         (void) strcpy(slk->ent[i].ent_text, new_text);
115         free(new_text);
116
117         sprintf(slk->ent[i].form_text, "%*s", (size_t) slk->maxlen, " ");
118
119         switch (format) {
120         default:
121         case 0:         /* left-aligned */
122             offset = 0;
123             break;
124         case 1:         /* centered */
125             offset = (slk->maxlen - mycols) / 2;
126             break;
127         case 2:         /* right-aligned */
128             offset = slk->maxlen - mycols;
129             break;
130         }
131         if (offset < 0)
132             offset = 0;
133         strcpy(slk->ent[i].form_text + offset,
134                slk->ent[i].ent_text);
135         /*
136          * Pad the display with blanks on the right, unless it is already
137          * right-aligned.
138          */
139         if (format != 2 && mycols < slk->maxlen) {
140             sprintf(slk->ent[i].form_text + offset + used,
141                     "%*s",
142                     slk->maxlen - (mycols - offset),
143                     " ");
144         }
145         slk->ent[i].dirty = TRUE;
146         result = OK;
147     }
148     returnCode(result);
149 }