FreeBSD 10 clang port
[oweals/cde.git] / cde / lib / DtTerm / TermPrim / TermPrimRenderLineDraw.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #ifndef lint
24 #ifdef  VERBOSE_REV_INFO
25 static char rcs_id[] = "$XConsortium: TermPrimRenderLineDraw.c /main/1 1996/04/21 19:18:59 drk $";
26 #endif  /* VERBOSE_REV_INFO */
27 #endif  /* lint */
28
29 /*                                                                      *
30  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
31  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
32  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
33  * (c) Copyright 1993, 1994 Novell, Inc.                                *
34  */
35
36 #include <wchar.h>
37
38 #include "TermHeader.h"
39 #include "TermPrimP.h"
40 #include "TermPrimDebug.h"
41 #include "TermPrimRenderP.h"
42 #include "TermPrimRenderLineDraw.h"
43 #include "TermPrimLineDrawP.h"
44
45 static void
46 LineDrawRenderFunction(
47     Widget                w,
48     TermFont              font,
49     Pixel                 fg,
50     Pixel                 bg,
51     unsigned long         flags,
52     int                   x,
53     int                   y,
54     unsigned char        *rawString,
55     int                   len
56 )
57 {
58     DtTermPrimitiveWidget tw = (DtTermPrimitiveWidget) w;
59     struct termData *tpd = tw->term.tpd;
60     XGCValues values;
61     unsigned long valueMask;
62     LineDrawFont lineDrawFont = (LineDrawFont) font->fontInfo;
63     unsigned char *string;
64
65     if (tpd->mbCurMax > 1) {
66         /* we have a string of wide chars that need to be converted to
67          * chars...
68          */
69         wchar_t                  *wPtr;
70         unsigned char            *ptr;
71         int                       i1;
72         unsigned char             mbChar[MB_LEN_MAX];
73
74         /* we will need to convert 2 column characters to 2 spaces in
75          * order to preserve character positions...
76          */
77         string = (unsigned char *) XtMalloc(2 * len);
78         for (ptr = string, wPtr = (wchar_t *) rawString, i1 = 0; i1 < len;
79                 i1++, wPtr++) {
80             switch (wcwidth(*wPtr)) {
81             case 1:
82                 if (wctomb((char *) mbChar, *wPtr) == 1) {
83                     *ptr++ = *mbChar;
84                 } else {
85                     *ptr++ = ' ';
86                 }
87                 break;
88
89             case 2:
90                 *ptr++ = ' ';
91                 *ptr++ = ' ';
92                 break;
93
94             default:
95                 *ptr++ = ' ';
96                 break;
97             }
98         }
99
100         len = ptr - string;
101     } else {
102         string = rawString;
103     }
104
105     /* set the renderGC... */
106     valueMask = (unsigned long) 0;
107
108     /* set the foreground... */
109     if (TermIS_SECURE(flags)) {
110         if (tpd->renderGC.foreground != bg) {
111             tpd->renderGC.foreground = bg;
112             values.foreground = bg;
113             valueMask |= GCForeground;
114         }
115     } else {
116         if (tpd->renderGC.foreground != fg) {
117             tpd->renderGC.foreground = fg;
118             values.foreground = fg;
119             valueMask |= GCForeground;
120         }
121     }
122
123     /* set background... */
124     if (tpd->renderGC.background != bg) {
125         tpd->renderGC.background = bg;
126         values.background = bg;
127         valueMask |= GCBackground;
128     }
129
130     if (valueMask) {
131         (void) XChangeGC(XtDisplay(w), tpd->renderGC.gc, valueMask,
132                 &values);
133     }
134
135     /* we need a clear GC as well...
136      */
137     valueMask = (unsigned long) 0;
138     if (tpd->renderReverseGC.foreground != bg) {
139         tpd->renderReverseGC.foreground = bg;
140         values.foreground = bg;
141         valueMask |= GCForeground;
142     }
143     if (valueMask) {
144         (void) XChangeGC(XtDisplay(w),
145                 tpd->renderReverseGC.gc, valueMask, &values);
146     }
147
148     /* line draw a line of text... */
149     if (isDebugFSet('t', 1)) {
150 #ifdef  BBA
151 #pragma BBA_IGNORE
152 #endif  /*BBA*/
153         /* Fill in the text area so we can see what is going to
154          * be displayed...
155          */
156         (void) XFillRectangle(XtDisplay(w),
157                 XtWindow(w),
158                 tpd->renderGC.gc,
159                 x,
160                 y,
161                 tpd->cellWidth * len,
162                 tpd->cellHeight);
163         (void) XSync(XtDisplay(w), False);
164         (void) shortSleep(100000);
165     }
166                         
167     (void) _DtTermPrimLineDrawImageString(
168             XtDisplay(w),                       /* Display              */
169             XtWindow(w),                        /* Drawable             */
170             lineDrawFont,                       /* LineDrawFont         */
171             tpd->renderGC.gc,                   /* GC                   */
172             tpd->renderReverseGC.gc,            /* clearGC              */
173             x,                                  /* x                    */
174             y + lineDrawFont->ascent,           /* y                    */
175             string,                             /* string               */
176             len);                               /* length               */
177
178     /* we don't support overstrike for line draw... */
179
180     /* handle the underline enhancement... */
181     /* draw the underline... */
182     if (TermIS_UNDERLINE(flags)) {
183         XDrawLine(XtDisplay(w),                 /* Display              */
184                 XtWindow(w),                    /* Window               */
185                 tpd->renderGC.gc,               /* GC                   */
186                 x,                              /* X1                   */
187                 y + tpd->cellHeight - 1,                /* Y1                   */
188                 x + len * tpd->cellWidth,       /* X2                   */
189                 y + tpd->cellHeight - 1);       /* Y2                   */
190     }
191
192     if (rawString != string) {
193         (void) XtFree((char *) string);
194     }
195 }
196
197 static void
198 LineDrawDestroyFunction(
199     Widget                w,
200     TermFont              font
201 )
202 {
203     LineDrawFont lineDrawFont = (LineDrawFont) font->fontInfo;
204
205     (void) _DtTermPrimLineDrawFreeFont(lineDrawFont);
206     (void) XtFree((char *) font);
207 }
208
209 static void
210 LineDrawExtentsFunction(
211     Widget                w,
212     TermFont              font,
213     unsigned char        *string,
214     int                   len,
215     int                  *widthReturn,
216     int                  *heightReturn,
217     int                  *ascentReturn
218 )
219 {
220     LineDrawFont lineDrawFont = (LineDrawFont) font->fontInfo;
221
222     if (widthReturn) {
223         *widthReturn = len * lineDrawFont->width;
224     }
225     if (heightReturn) {
226         *heightReturn = lineDrawFont->height;
227     }
228     if (ascentReturn) {
229         *ascentReturn = lineDrawFont->ascent;
230     }
231     return;
232 }
233
234 TermFont
235 _DtTermPrimRenderLineDrawCreate(
236     Widget                w,
237     GlyphInfo             glyphInfo,
238     int                   numGlyphs,
239     int                   width,
240     int                   ascent,
241     int                   descent
242 )
243 {
244     TermFont termFont;
245
246     termFont = (TermFont) XtMalloc(sizeof(TermFontRec));
247     termFont->renderFunction = LineDrawRenderFunction;
248     termFont->destroyFunction = LineDrawDestroyFunction;
249     termFont->extentsFunction = LineDrawExtentsFunction;
250     termFont->fontInfo =
251       (XtPointer)_DtTermPrimLineDrawCreateFont(w, glyphInfo, numGlyphs,
252                                                width, ascent, descent);
253     return(termFont);
254 }