Release 970112
[wine] / objects / text.c
1 /*
2  * text functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  *
6  */
7
8 #include <stdlib.h>
9 #include "windows.h"
10 #include "dc.h"
11 #include "gdi.h"
12 #include "heap.h"
13 #include "stddebug.h"
14 /* #define DEBUG_TEXT */
15 #include "debug.h"
16 #include "xmalloc.h"
17
18 #define TAB     9
19 #define LF     10
20 #define CR     13
21 #define SPACE  32
22 #define PREFIX 38
23
24 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
25
26 static int tabstop = 8;
27 static int tabwidth;
28 static int spacewidth;
29 static int prefix_offset;
30
31 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
32                                   char *dest, int *len, int width, WORD format)
33 {
34     /* Return next line of text from a string.
35      * 
36      * hdc - handle to DC.
37      * str - string to parse into lines.
38      * count - length of str.
39      * dest - destination in which to return line.
40      * len - length of resultant line in dest in chars.
41      * width - maximum width of line in pixels.
42      * format - format type passed to DrawText.
43      *
44      * Returns pointer to next char in str after end of the line
45      * or NULL if end of str reached.
46      */
47
48     int i = 0, j = 0, k;
49     int plen = 0;
50     int numspaces;
51     SIZE16 size;
52     int lasttab = 0;
53     int wb_i = 0, wb_j = 0, wb_count = 0;
54
55     while (*count)
56     {
57         switch (str[i])
58         {
59         case CR:
60         case LF:
61             if (!(format & DT_SINGLELINE))
62             {
63                 if (str[i] == CR && str[i+1] == LF)
64                     i++;
65                 i++;
66                 *len = j;
67                 (*count)--;
68                 return (&str[i]);
69             }
70             dest[j++] = str[i++];
71             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
72                 (format & DT_WORDBREAK))
73             {
74                 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
75                     return NULL;
76                 plen += size.cx;
77             }
78             break;
79             
80         case PREFIX:
81             if (!(format & DT_NOPREFIX))
82             {
83                 if (str[++i] != PREFIX)
84                 {
85                     prefix_offset = j;
86                     break;
87                 }
88             }
89             dest[j++] = str[i++];
90             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
91                 (format & DT_WORDBREAK))
92             {
93                 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
94                     return NULL;
95                 plen += size.cx;
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 (!GetTextExtentPoint16(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 (!GetTextExtentPoint16(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 (!GetTextExtentPoint16(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 (!GetTextExtentPoint16(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  *           DrawText16    (USER.85)
184  */
185 INT16 DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
186                   LPRECT16 rect, UINT16 flags )
187 {
188     SIZE16 size;
189     const char *strPtr;
190     static char line[1024];
191     int len, lh, count=i_count;
192     int prefix_x = 0;
193     int prefix_end = 0;
194     TEXTMETRIC16 tm;
195     int x = rect->left, y = rect->top;
196     int width = rect->right - rect->left;
197     int max_width = 0;
198
199     dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str,
200                  count, rect->left, rect->top, rect->right, rect->bottom);
201     
202     if (count == -1) count = strlen(str);
203     strPtr = str;
204
205     GetTextMetrics16(hdc, &tm);
206     if (flags & DT_EXTERNALLEADING)
207         lh = tm.tmHeight + tm.tmExternalLeading;
208     else
209         lh = tm.tmHeight;
210
211     if (flags & DT_TABSTOP)
212         tabstop = flags >> 8;
213
214     if (flags & DT_EXPANDTABS)
215     {
216         GetTextExtentPoint16(hdc, " ", 1, &size);
217         spacewidth = size.cx;
218         GetTextExtentPoint16(hdc, "o", 1, &size);
219         tabwidth = size.cx * tabstop;
220     }
221
222     if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
223
224     do
225     {
226         prefix_offset = -1;
227         strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
228
229         if (prefix_offset != -1)
230         {
231             GetTextExtentPoint16(hdc, line, prefix_offset, &size);
232             prefix_x = size.cx;
233             GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
234             prefix_end = size.cx - 1;
235         }
236
237         if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
238         if (flags & DT_CENTER) x = (rect->left + rect->right -
239                                     size.cx) / 2;
240         else if (flags & DT_RIGHT) x = rect->right - size.cx;
241
242         if (flags & DT_SINGLELINE)
243         {
244             if (flags & DT_VCENTER) y = rect->top + 
245                 (rect->bottom - rect->top) / 2 - size.cy / 2;
246             else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
247         }
248         if (!(flags & DT_CALCRECT))
249         {
250             if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
251                               rect, line, len, NULL )) return 0;
252             if (prefix_offset != -1)
253             {
254                 HPEN32 hpen = CreatePen32( PS_SOLID, 1, GetTextColor32(hdc) );
255                 HPEN32 oldPen = SelectObject32( hdc, hpen );
256                 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
257                 LineTo32(hdc, x + prefix_end, y + tm.tmAscent + 1 );
258                 SelectObject32( hdc, oldPen );
259                 DeleteObject32( hpen );
260             }
261         }
262         else if (size.cx > max_width)
263             max_width = size.cx;
264
265         y += lh;
266         if (strPtr)
267         {
268             if (!(flags & DT_NOCLIP))
269             {
270                 if (y > rect->bottom - lh)
271                     break;
272             }
273         }
274     }
275     while (strPtr);
276     if (flags & DT_CALCRECT)
277     {
278         rect->right = rect->left + max_width;
279         rect->bottom = y;
280     }
281     return y - rect->top;
282 }
283
284
285 /***********************************************************************
286  *           DrawText32A    (USER32.163)
287  */
288 INT32 DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
289                    LPRECT32 rect, UINT32 flags )
290 {
291     RECT16 rect16;
292     INT16 ret;
293
294     if (!rect)
295         return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
296     CONV_RECT32TO16( rect, &rect16 );
297     ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
298     CONV_RECT16TO32( &rect16, rect );
299     return ret;
300 }
301
302
303 /***********************************************************************
304  *           DrawText32W    (USER32.166)
305  */
306 INT32 DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
307                    LPRECT32 rect, UINT32 flags )
308 {
309     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
310     INT32 ret = DrawText32A( hdc, p, count, rect, flags );
311     HeapFree( GetProcessHeap(), 0, p );
312     return ret;
313 }
314
315
316 /***********************************************************************
317  *           ExtTextOut16    (GDI.351)
318  */
319 BOOL16 ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
320                      const RECT16 *lprect, LPCSTR str, UINT16 count,
321                      const INT16 *lpDx )
322 {
323     BOOL32      ret;
324     int         i;
325     RECT32      rect32;
326     LPINT32     lpdx32 = lpDx?(LPINT32)xmalloc(sizeof(INT32)*count):NULL;
327
328     if (lprect) CONV_RECT16TO32(lprect,&rect32);
329     if (lpdx32) for (i=count;i--;) lpdx32[i]=lpDx[i];
330     ret = ExtTextOut32A(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
331     if (lpdx32) free(lpdx32);
332     return ret;
333
334
335 }
336
337
338 /***********************************************************************
339  *           ExtTextOut32A    (GDI32.98)
340  */
341 BOOL32 ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
342                       const RECT32 *lprect, LPCSTR str, UINT32 count,
343                       const INT32 *lpDx )
344 {
345     DC * dc = DC_GetDCPtr( hdc );
346     return dc && dc->funcs->pExtTextOut && 
347            dc->funcs->pExtTextOut(dc,x,y,flags,lprect,str,count,lpDx);
348 }
349
350
351 /***********************************************************************
352  *           ExtTextOut32W    (GDI32.99)
353  */
354 BOOL32 ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
355                       const RECT32 *lprect, LPCWSTR str, UINT32 count,
356                       const INT32 *lpDx )
357 {
358     LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
359     INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
360     HeapFree( GetProcessHeap(), 0, p );
361     return ret;
362 }
363
364
365 /***********************************************************************
366  *           TextOut16    (GDI.33)
367  */
368 BOOL16 TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
369 {
370     return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
371 }
372
373
374 /***********************************************************************
375  *           TextOut32A    (GDI32.355)
376  */
377 BOOL32 TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
378 {
379     return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
380 }
381
382
383 /***********************************************************************
384  *           TextOut32W    (GDI32.356)
385  */
386 BOOL32 TextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count )
387 {
388     return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
389 }
390
391
392 /***********************************************************************
393  *           GrayString   (USER.185)
394  */
395 BOOL GrayString(HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc, LPARAM lParam, 
396                 INT cch, INT x, INT y, INT cx, INT cy)
397 {
398     BOOL ret;
399     COLORREF current_color;
400
401     if (!cch) cch = lstrlen16( (LPCSTR)PTR_SEG_TO_LIN(lParam) );
402     if (gsprc) return gsprc( hdc, lParam, cch );
403     current_color = GetTextColor32( hdc );
404     SetTextColor( hdc, GetSysColor(COLOR_GRAYTEXT) );
405     ret = TextOut16( hdc, x, y, (LPCSTR)PTR_SEG_TO_LIN(lParam), cch );
406     SetTextColor( hdc, current_color );
407     return ret;
408 }
409
410
411 /***********************************************************************
412  *           TEXT_TabbedTextOut
413  *
414  * Helper function for TabbedTextOut() and GetTabbedTextExtent().
415  * Note: this doesn't work too well for text-alignment modes other
416  *       than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
417  */
418 LONG TEXT_TabbedTextOut( HDC16 hdc, int x, int y, LPSTR lpstr, int count, 
419                          int cTabStops, LPINT16 lpTabPos, int nTabOrg,
420                          BOOL fDisplayText)
421 {
422     WORD defWidth;
423     DWORD extent = 0;
424     int i, tabPos = x;
425     int start = x;
426
427     if (cTabStops == 1)
428     {
429         defWidth = *lpTabPos;
430         cTabStops = 0;
431     }
432     else
433     {
434         TEXTMETRIC16 tm;
435         GetTextMetrics16( hdc, &tm );
436         defWidth = 8 * tm.tmAveCharWidth;
437     }
438     
439     while (count > 0)
440     {
441         for (i = 0; i < count; i++)
442             if (lpstr[i] == '\t') break;
443         extent = GetTextExtent( hdc, lpstr, i );
444         while ((cTabStops > 0) && (nTabOrg + *lpTabPos <= x + LOWORD(extent)))
445         {
446             lpTabPos++;
447             cTabStops--;
448         }
449         if (i == count)
450             tabPos = x + LOWORD(extent);
451         else if (cTabStops > 0)
452             tabPos = nTabOrg + *lpTabPos;
453         else
454             tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
455         if (fDisplayText)
456         {
457             RECT16 r;
458             SetRect16( &r, x, y, tabPos, y+HIWORD(extent) );
459             ExtTextOut16( hdc, x, y,
460                           GetBkMode32(hdc) == OPAQUE ? ETO_OPAQUE : 0,
461                           &r, lpstr, i, NULL );
462         }
463         x = tabPos;
464         count -= i+1;
465         lpstr += i+1;
466     }
467     return MAKELONG(tabPos - start, HIWORD(extent));
468 }
469
470
471 /***********************************************************************
472  *           TabbedTextOut    (USER.196)
473  */
474 LONG TabbedTextOut( HDC16 hdc, short x, short y, LPSTR lpstr, short count, 
475                     short cTabStops, LPINT16 lpTabPos, short nTabOrg )
476 {
477     dprintf_text( stddeb, "TabbedTextOut: %04x %d,%d '%*.*s' %d\n",
478                   hdc, x, y, count, count, lpstr, count );
479     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
480                                lpTabPos, nTabOrg, TRUE );
481 }
482
483
484 /***********************************************************************
485  *           GetTabbedTextExtent    (USER.197)
486  */
487 DWORD GetTabbedTextExtent( HDC16 hdc, LPSTR lpstr, int count, 
488                           int cTabStops, LPINT16 lpTabPos )
489 {
490     dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%*.*s' %d\n",
491                   hdc, count, count, lpstr, count );
492     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
493                                lpTabPos, 0, FALSE );
494 }