Release 941122
[wine] / objects / text.c
1 /*
2  * text functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  */
6
7 static char Copyright[] = "Copyright  Alexandre Julliard, 1993, 1994";
8
9 #include <stdlib.h>
10 #include <X11/Xatom.h>
11 #include "windows.h"
12 #include "gdi.h"
13 #include "metafile.h"
14 #include "stddebug.h"
15 /* #define DEBUG_TEXT /* */
16 /* #undef  DEBUG_TEXT /* */
17 #include "debug.h"
18
19 #define TAB     9
20 #define LF     10
21 #define CR     13
22 #define SPACE  32
23 #define PREFIX 38
24
25 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
26
27 static int tabstop = 8;
28 static int tabwidth;
29 static int spacewidth;
30 static int prefix_offset;
31
32
33 static char *TEXT_NextLine(HDC hdc, char *str, int *count, char *dest, 
34                            int *len, int width, WORD format)
35 {
36     /* Return next line of text from a string.
37      * 
38      * hdc - handle to DC.
39      * str - string to parse into lines.
40      * count - length of str.
41      * dest - destination in which to return line.
42      * len - length of resultant line in dest in chars.
43      * width - maximum width of line in pixels.
44      * format - format type passed to DrawText.
45      *
46      * Returns pointer to next char in str after end of the line
47      * or NULL if end of str reached.
48      */
49
50     int i = 0, j = 0, k;
51     int plen = 0;
52     int numspaces;
53     SIZE size;
54     int lasttab = 0;
55     int wb_i = 0, wb_j = 0, wb_count;
56
57     while (*count)
58     {
59         switch (str[i])
60         {
61         case CR:
62         case LF:
63             if (!(format & DT_SINGLELINE))
64             {
65                 if (str[i] == CR && str[i+1] == LF)
66                     i++;
67                 i++;
68                 *len = j;
69                 return (&str[i]);
70             }
71             dest[j++] = str[i++];
72             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
73                 (format & DT_WORDBREAK))
74             {
75                 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
76                     return NULL;
77                 plen += size.cx;
78             }
79             break;
80             
81         case PREFIX:
82             if (!(format & DT_NOPREFIX))
83             {
84                 prefix_offset = j;
85                 i++;
86             }
87             else
88             {
89                 dest[j++] = str[i++];
90                 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
91                 {
92                     if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
93                         return NULL;
94                     plen += size.cx;
95                 }
96             }
97             break;
98             
99         case TAB:
100             if (format & DT_EXPANDTABS)
101             {
102                 wb_i = ++i;
103                 wb_j = j;
104                 wb_count = *count;
105
106                 if (!GetTextExtentPoint(hdc, &dest[lasttab], j - lasttab,
107                                                                  &size))
108                     return NULL;
109
110                 numspaces = (tabwidth - size.cx) / spacewidth;
111                 for (k = 0; k < numspaces; k++)
112                     dest[j++] = SPACE;
113                 plen += tabwidth - size.cx;
114                 lasttab = wb_j + numspaces;
115             }
116             else
117             {
118                 dest[j++] = str[i++];
119                 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
120                     (format & DT_WORDBREAK))
121                 {
122                     if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
123                         return NULL;
124                     plen += size.cx;
125                 }
126             }
127             break;
128
129         case SPACE:
130             dest[j++] = str[i++];
131             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
132                 (format & DT_WORDBREAK))
133             {
134                 wb_i = i;
135                 wb_j = j - 1;
136                 wb_count = *count;
137                 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
138                     return NULL;
139                 plen += size.cx;
140             }
141             break;
142
143         default:
144             dest[j++] = str[i++];
145             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
146                 (format & DT_WORDBREAK))
147             {
148                 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
149                     return NULL;
150                 plen += size.cx;
151             }
152         }
153
154         (*count)--;
155         if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
156         {
157             if (plen > width)
158             {
159                 if (format & DT_WORDBREAK)
160                 {
161                     if (wb_j)
162                     {
163                         *len = wb_j;
164                         *count = wb_count - 1;
165                         return (&str[wb_i]);
166                     }
167                 }
168                 else
169                 {
170                     *len = j;
171                     return (&str[i]);
172                 }
173             }
174         }
175     }
176     
177     *len = j;
178     return NULL;
179 }
180
181
182 /***********************************************************************
183  *           DrawText    (USER.85)
184  */
185 int DrawText( HDC hdc, LPSTR str, int count, LPRECT rect, WORD flags )
186 {
187     SIZE size;
188     char *strPtr;
189     static char line[1024];
190     int len, lh, prefix_x, prefix_end;
191     TEXTMETRIC tm;
192     int x = rect->left, y = rect->top;
193     int width = rect->right - rect->left;
194     int max_width = 0;
195
196     dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str, count,
197            rect->left, rect->top, rect->right, rect->bottom);
198
199     if (count == -1) count = strlen(str);
200     strPtr = str;
201
202     GetTextMetrics(hdc, &tm);
203     if (flags & DT_EXTERNALLEADING)
204         lh = tm.tmHeight + tm.tmExternalLeading;
205     else
206         lh = tm.tmHeight;
207
208     if (flags & DT_TABSTOP)
209         tabstop = flags >> 8;
210
211     if (flags & DT_EXPANDTABS)
212     {
213         GetTextExtentPoint(hdc, " ", 1, &size);
214         spacewidth = size.cx;
215         GetTextExtentPoint(hdc, "o", 1, &size);
216         tabwidth = size.cx * tabstop;
217     }
218
219     do
220     {
221         prefix_offset = -1;
222         strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
223
224         if (prefix_offset != -1)
225         {
226             GetTextExtentPoint(hdc, line, prefix_offset, &size);
227             prefix_x = size.cx;
228             GetTextExtentPoint(hdc, line, prefix_offset + 1, &size);
229             prefix_end = size.cx - 1;
230         }
231
232         if (!GetTextExtentPoint(hdc, line, len, &size)) return 0;
233         if (flags & DT_CENTER) x = (rect->left + rect->right -
234                                     size.cx) / 2;
235         else if (flags & DT_RIGHT) x = rect->right - size.cx;
236
237         if (flags & DT_SINGLELINE)
238         {
239             if (flags & DT_VCENTER) y = rect->top + 
240                 (rect->bottom - rect->top) / 2 - size.cy / 2;
241             else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
242         }
243         if (!(flags & DT_CALCRECT))
244         {
245             if (!ExtTextOut( hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
246                              rect, line, len, NULL )) return 0;
247         }
248         else if (size.cx > max_width)
249             max_width = size.cx;
250
251         if (prefix_offset != -1)
252         {
253             HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
254             HPEN oldPen = SelectObject( hdc, hpen );
255             MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
256             LineTo(hdc, x + prefix_end, y + tm.tmAscent + 1 );
257             SelectObject( hdc, oldPen );
258             DeleteObject( hpen );
259         }
260
261         y += lh;
262         if (strPtr)
263         {
264             if (!(flags & DT_NOCLIP))
265             {
266                 if (y > rect->bottom - lh)
267                     break;
268             }
269         }
270     }
271     while (strPtr);
272     if (flags & DT_CALCRECT)
273     {
274         rect->right = rect->left + max_width;
275         rect->bottom = y;
276     }
277     return 1;
278 }
279
280
281 /***********************************************************************
282  *           ExtTextOut    (GDI.351)
283  */
284 BOOL ExtTextOut( HDC hdc, short x, short y, WORD flags, LPRECT lprect,
285                  LPSTR str, WORD count, LPINT lpDx )
286 {
287     int dir, ascent, descent, i;
288     XCharStruct info;
289     XFontStruct *font;
290     RECT rect;
291
292     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
293     if (!dc) 
294     {
295         dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
296         if (!dc) return FALSE;
297         MF_TextOut( dc, x, y, str, count );
298         return TRUE;
299     }
300
301     if (!DC_SetupGCForText( dc )) return TRUE;
302     font = dc->u.x.font.fstruct;
303
304     dprintf_text(stddeb,"ExtTextOut: %d,%d '%s', %d  flags=%d rect=%d,%d,%d,%d\n",
305             x, y, str, count, flags,
306             lprect->left, lprect->top, lprect->right, lprect->bottom );
307
308       /* Setup coordinates */
309
310     if (dc->w.textAlign & TA_UPDATECP)
311     {
312         x = dc->w.CursPosX;
313         y = dc->w.CursPosY;
314     }
315     x = XLPTODP( dc, x );
316     y = YLPTODP( dc, y );
317     if (flags & (ETO_OPAQUE | ETO_CLIPPED))  /* There's a rectangle */
318     {
319         rect.left   = XLPTODP( dc, lprect->left );
320         rect.right  = XLPTODP( dc, lprect->right );
321         rect.top    = YLPTODP( dc, lprect->top );
322         rect.bottom = YLPTODP( dc, lprect->bottom );
323         if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
324         if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
325     }
326
327       /* Draw the rectangle */
328
329     if (flags & ETO_OPAQUE)
330     {
331         XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
332         XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
333                         dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
334                         rect.right-rect.left, rect.bottom-rect.top );
335     }
336     if (!count) return TRUE;  /* Nothing more to do */
337
338       /* Compute text starting position */
339
340     XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
341     info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
342     if (lpDx) for (i = 0; i < count; i++) info.width += lpDx[i];
343
344     switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
345     {
346       case TA_LEFT:
347           if (dc->w.textAlign & TA_UPDATECP)
348               dc->w.CursPosX = XDPTOLP( dc, x + info.width );
349           break;
350       case TA_RIGHT:
351           x -= info.width;
352           if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
353           break;
354       case TA_CENTER:
355           x -= info.width / 2;
356           break;
357     }
358     switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
359     {
360       case TA_TOP:
361           y += font->ascent;
362           break;
363       case TA_BOTTOM:
364           y -= font->descent;
365           break;
366       case TA_BASELINE:
367           break;
368     }
369
370       /* Set the clip region */
371
372     if (flags & ETO_CLIPPED)
373     {
374         SaveVisRgn( hdc );
375         IntersectVisRect( hdc, rect.left, rect.top, rect.right, rect.bottom );
376     }
377
378       /* Draw the text background if necessary */
379
380     if (dc->w.backgroundMode != TRANSPARENT)
381     {
382           /* If rectangle is opaque and clipped, do nothing */
383         if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
384         {
385               /* Only draw if rectangle is not opaque or if some */
386               /* text is outside the rectangle */
387             if (!(flags & ETO_OPAQUE) ||
388                 (x < rect.left) ||
389                 (x + info.width >= rect.right) ||
390                 (y-font->ascent < rect.top) ||
391                 (y+font->descent >= rect.bottom))
392             {
393                 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
394                 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
395                                 dc->w.DCOrgX + x,
396                                 dc->w.DCOrgY + y - font->ascent,
397                                 info.width,
398                                 font->ascent + font->descent );
399             }
400         }
401     }
402     
403       /* Draw the text */
404
405     XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
406     if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
407     {
408         XDrawString( display, dc->u.x.drawable, dc->u.x.gc, 
409                      dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
410     }
411     else  /* Now the fun begins... */
412     {
413         XTextItem *items, *pitem;
414
415         items = malloc( count * sizeof(XTextItem) );
416         for (i = 0, pitem = items; i < count; i++, pitem++)
417         {
418             pitem->chars  = str + i;
419             pitem->nchars = 1;
420             pitem->font   = None;
421             if (i == 0)
422             {
423                 pitem->delta = 0;
424                 continue;  /* First iteration -> no delta */
425             }
426             pitem->delta = dc->w.charExtra;
427             if (str[i] == dc->u.x.font.metrics.tmBreakChar)
428                 pitem->delta += dc->w.breakExtra;
429             if (lpDx)
430             {
431                 INT width;
432                 GetCharWidth( hdc, str[i], str[i], &width );
433                 pitem->delta += lpDx[i-1] - width;
434             }
435         }
436         XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
437                    dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, count );
438         free( items );
439     }
440
441       /* Draw underline and strike-out if needed */
442
443     if (dc->u.x.font.metrics.tmUnderlined)
444     {
445         long linePos, lineWidth;       
446         if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
447             linePos = font->descent-1;
448         if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
449             lineWidth = 0;
450         else if (lineWidth == 1) lineWidth = 0;
451         XSetLineAttributes( display, dc->u.x.gc, lineWidth,
452                             LineSolid, CapRound, JoinBevel ); 
453         XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
454                    dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
455                    dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
456     }
457     if (dc->u.x.font.metrics.tmStruckOut)
458     {
459         long lineAscent, lineDescent;
460         if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
461             lineAscent = font->ascent / 3;
462         if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
463             lineDescent = -lineAscent;
464         XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
465                             LineSolid, CapRound, JoinBevel ); 
466         XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
467                    dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
468                    dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
469     }
470     if (flags & ETO_CLIPPED) RestoreVisRgn( hdc );
471     return TRUE;
472 }
473
474
475 /***********************************************************************
476  *           TextOut    (GDI.33)
477  */
478 BOOL TextOut( HDC hdc, short x, short y, LPSTR str, short count )
479 {
480     return ExtTextOut( hdc, x, y, 0, NULL, str, count, NULL );
481 }
482
483
484 /***********************************************************************
485  *              GrayString (USER.185)
486  */
487 BOOL GrayString(HDC hdc, HBRUSH hbr, FARPROC gsprc, LPARAM lParam, 
488                 INT cch, INT x, INT y, INT cx, INT cy)
489 {
490         int s, current_color;
491
492         if (gsprc) {
493                 return CallGrayStringProc(gsprc, hdc, lParam, 
494                                         cch ? cch : lstrlen((LPCSTR) lParam) );
495         } else {
496                 current_color = GetTextColor(hdc);
497                 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT) );
498                 s = TextOut(hdc, x, y, (LPSTR) lParam, 
499                                 cch ? cch : lstrlen((LPCSTR) lParam) );
500                 SetTextColor(hdc, current_color);
501                 
502                 return s;
503         }
504 }
505
506
507 /***********************************************************************
508  *                      TabbedTextOut           [USER.196]
509  */
510 LONG TabbedTextOut(HDC hDC, short x, short y, LPSTR lpStr, short nCount, 
511                 short nTabCount, LPINT lpTabPos, short nTabOrg)
512 {
513         WORD    width, height;
514         dprintf_text(stdnimp,"EMPTY STUB !!! TabbedTextOut(); ! call TextOut() for now !\n");
515         height = HIWORD(GetTextExtent(hDC, lpStr, nCount));
516         width = LOWORD(GetTextExtent(hDC, lpStr, nCount));
517         TextOut(hDC, x, y, lpStr, nCount);
518         return MAKELONG(width, height);
519 }
520
521
522 /***********************************************************************
523  *                      GetTabbedTextExtent             [USER.197]
524  */
525 DWORD GetTabbedTextExtent(HDC hDC, LPSTR lpString, int nCount, 
526         int nTabPositions, LPINT lpnTabStopPositions)
527 {
528         dprintf_text(stdnimp,"EMPTY STUB !!! GetTabbedTextExtent(); !\n");
529
530         return (18 << 16) | (nCount * 18);
531 }
532
533