Release 970305
[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  *           GrayString16   (USER.185)
394  */
395 BOOL16 GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
396                      LPARAM lParam, INT16 cch, INT16 x, INT16 y,
397                      INT16 cx, INT16 cy )
398 {
399     BOOL16 ret;
400     COLORREF current_color;
401
402     if (!cch) cch = lstrlen16( (LPCSTR)PTR_SEG_TO_LIN(lParam) );
403     if (gsprc) return gsprc( hdc, lParam, cch );
404     current_color = SetTextColor32( hdc, GetSysColor32(COLOR_GRAYTEXT) );
405     ret = TextOut16( hdc, x, y, (LPCSTR)PTR_SEG_TO_LIN(lParam), cch );
406     SetTextColor32( hdc, current_color );
407     return ret;
408 }
409
410
411 /***********************************************************************
412  *           GrayString32A   (USER32.314)
413  */
414 BOOL32 GrayString32A( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
415                       LPARAM lParam, INT32 cch, INT32 x, INT32 y,
416                       INT32 cx, INT32 cy )
417 {
418     BOOL32 ret;
419     COLORREF current_color;
420
421     if (!cch) cch = lstrlen32A( (LPCSTR)lParam );
422     if (gsprc) return gsprc( hdc, lParam, cch );
423     current_color = SetTextColor32( hdc, GetSysColor32(COLOR_GRAYTEXT) );
424     ret = TextOut32A( hdc, x, y, (LPCSTR)lParam, cch );
425     SetTextColor32( hdc, current_color );
426     return ret;
427 }
428
429
430 /***********************************************************************
431  *           GrayString32W   (USER32.315)
432  */
433 BOOL32 GrayString32W( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
434                       LPARAM lParam, INT32 cch, INT32 x, INT32 y,
435                       INT32 cx, INT32 cy )
436 {
437     BOOL32 ret;
438     COLORREF current_color;
439
440     if (!cch) cch = lstrlen32W( (LPCWSTR)lParam );
441     if (gsprc) return gsprc( hdc, lParam, cch );
442     current_color = SetTextColor32( hdc, GetSysColor32(COLOR_GRAYTEXT) );
443     ret = TextOut32W( hdc, x, y, (LPCWSTR)lParam, cch );
444     SetTextColor32( hdc, current_color );
445     return ret;
446 }
447
448
449 /***********************************************************************
450  *           TEXT_TabbedTextOut
451  *
452  * Helper function for TabbedTextOut() and GetTabbedTextExtent().
453  * Note: this doesn't work too well for text-alignment modes other
454  *       than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
455  */
456 LONG TEXT_TabbedTextOut( HDC32 hdc, INT32 x, INT32 y, LPCSTR lpstr,
457                          INT32 count, INT32 cTabStops, const INT16 *lpTabPos16,
458                          const INT32 *lpTabPos32, INT32 nTabOrg,
459                          BOOL32 fDisplayText )
460 {
461     INT32 defWidth;
462     DWORD extent = 0;
463     int i, tabPos = x;
464     int start = x;
465
466     if (cTabStops == 1)
467     {
468         defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
469         cTabStops = 0;
470     }
471     else
472     {
473         TEXTMETRIC16 tm;
474         GetTextMetrics16( hdc, &tm );
475         defWidth = 8 * tm.tmAveCharWidth;
476     }
477     
478     while (count > 0)
479     {
480         for (i = 0; i < count; i++)
481             if (lpstr[i] == '\t') break;
482         extent = GetTextExtent( hdc, lpstr, i );
483         if (lpTabPos32)
484         {
485             while ((cTabStops > 0) &&
486                    (nTabOrg + *lpTabPos32 <= x + LOWORD(extent)))
487             {
488                 lpTabPos32++;
489                 cTabStops--;
490             }
491         }
492         else
493         {
494             while ((cTabStops > 0) &&
495                    (nTabOrg + *lpTabPos16 <= x + LOWORD(extent)))
496             {
497                 lpTabPos16++;
498                 cTabStops--;
499             }
500         }
501         if (i == count)
502             tabPos = x + LOWORD(extent);
503         else if (cTabStops > 0)
504             tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
505         else
506             tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
507         if (fDisplayText)
508         {
509             RECT32 r;
510             SetRect32( &r, x, y, tabPos, y+HIWORD(extent) );
511             ExtTextOut32A( hdc, x, y,
512                            GetBkMode32(hdc) == OPAQUE ? ETO_OPAQUE : 0,
513                            &r, lpstr, i, NULL );
514         }
515         x = tabPos;
516         count -= i+1;
517         lpstr += i+1;
518     }
519     return MAKELONG(tabPos - start, HIWORD(extent));
520 }
521
522
523 /***********************************************************************
524  *           TabbedTextOut16    (USER.196)
525  */
526 LONG TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr, INT16 count, 
527                       INT16 cTabStops, const INT16 *lpTabPos, INT16 nTabOrg )
528 {
529     dprintf_text( stddeb, "TabbedTextOut16: %04x %d,%d '%.*s' %d\n",
530                   hdc, x, y, count, lpstr, count );
531     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
532                                lpTabPos, NULL, nTabOrg, TRUE );
533 }
534
535
536 /***********************************************************************
537  *           TabbedTextOut32A    (USER32.541)
538  */
539 LONG TabbedTextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR lpstr, INT32 count, 
540                        INT32 cTabStops, const INT32 *lpTabPos, INT32 nTabOrg )
541 {
542     dprintf_text( stddeb, "TabbedTextOut32A: %04x %d,%d '%.*s' %d\n",
543                   hdc, x, y, count, lpstr, count );
544     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
545                                NULL, lpTabPos, nTabOrg, TRUE );
546 }
547
548
549 /***********************************************************************
550  *           TabbedTextOut32W    (USER32.542)
551  */
552 LONG TabbedTextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count, 
553                        INT32 cTabStops, const INT32 *lpTabPos, INT32 nTabOrg )
554 {
555     LONG ret;
556     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
557     lstrcpynWtoA( p, str, count + 1 );
558     ret = TabbedTextOut32A( hdc, x, y, p, count, cTabStops,
559                             lpTabPos, nTabOrg );
560     HeapFree( GetProcessHeap(), 0, p );
561     return ret;
562 }
563
564
565 /***********************************************************************
566  *           GetTabbedTextExtent16    (USER.197)
567  */
568 DWORD GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count, 
569                              INT16 cTabStops, const INT16 *lpTabPos )
570 {
571     dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%.*s' %d\n",
572                   hdc, count, lpstr, count );
573     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
574                                lpTabPos, NULL, 0, FALSE );
575 }
576
577
578 /***********************************************************************
579  *           GetTabbedTextExtent32A    (USER32.292)
580  */
581 DWORD GetTabbedTextExtent32A( HDC32 hdc, LPCSTR lpstr, INT32 count, 
582                               INT32 cTabStops, const INT32 *lpTabPos )
583 {
584     dprintf_text( stddeb, "GetTabbedTextExtent: %04x '%.*s' %d\n",
585                   hdc, count, lpstr, count );
586     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
587                                NULL, lpTabPos, 0, FALSE );
588 }
589
590
591 /***********************************************************************
592  *           GetTabbedTextExtent32W    (USER32.293)
593  */
594 DWORD GetTabbedTextExtent32W( HDC32 hdc, LPCWSTR lpstr, INT32 count, 
595                               INT32 cTabStops, const INT32 *lpTabPos )
596 {
597     LONG ret;
598     LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
599     lstrcpynWtoA( p, lpstr, count + 1 );
600     ret = GetTabbedTextExtent32A( hdc, p, count, cTabStops, lpTabPos );
601     HeapFree( GetProcessHeap(), 0, p );
602     return ret;
603 }