When including 'wine/port.h', include it first.
[wine] / dlls / user / text.c
1 /*
2  * USER text functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  *
6  */
7
8 #include <string.h>
9
10 #include "windef.h"
11 #include "wingdi.h"
12 #include "wine/winuser16.h"
13 #include "wine/unicode.h"
14 #include "winbase.h"
15 #include "winerror.h"
16 #include "winnls.h"
17 #include "user.h"
18 #include "debugtools.h"
19
20 DEFAULT_DEBUG_CHANNEL(text);
21
22 #define TAB     9
23 #define LF     10
24 #define CR     13
25 #define SPACE  32
26 #define PREFIX 38
27
28 #define ELLIPSIS "..."
29 #define FORWARD_SLASH '/'
30 #define BACK_SLASH '\\'
31
32 static const WCHAR SPACEW[] = {' ', 0};
33 static const WCHAR oW[] = {'o', 0};
34 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
35 static const WCHAR FORWARD_SLASHW[] = {'/', 0};
36 static const WCHAR BACK_SLASHW[] = {'\\', 0};
37
38 #define SWAP_INT(a,b)  { int t = a; a = b; b = t; }
39
40 static int tabstop = 8;
41 static int tabwidth;
42 static int spacewidth;
43 static int prefix_offset;
44
45 /*********************************************************************
46  *  Return next line of text from a string.
47  * 
48  * hdc - handle to DC.
49  * str - string to parse into lines.
50  * count - length of str.
51  * dest - destination in which to return line.
52  * len - dest buffer size in chars on input, copied length into dest on output.
53  * width - maximum width of line in pixels.
54  * format - format type passed to DrawText.
55  *
56  * Returns pointer to next char in str after end of the line
57  * or NULL if end of str reached.
58  *
59  * FIXME:
60  * GetTextExtentPoint is used to get the width of each character, 
61  * rather than GetCharABCWidth...  So the whitespace between
62  * characters is ignored, and the reported len is too great.
63  */
64 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
65                                   WCHAR *dest, int *len, int width, WORD format)
66 {
67     int i = 0, j = 0, k;
68     int plen = 0;
69     int numspaces;
70     SIZE size;
71     int lasttab = 0;
72     int wb_i = 0, wb_j = 0, wb_count = 0;
73     int maxl = *len;
74
75     while (*count && j < maxl)
76     {
77         switch (str[i])
78         {
79         case CR:
80         case LF:
81             if (!(format & DT_SINGLELINE))
82             {
83                 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
84                 {
85                     (*count)--;
86                     i++;
87                 }
88                 i++;
89                 *len = j;
90                 (*count)--;
91                 return (&str[i]);
92             }
93             dest[j++] = str[i++];
94             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
95                 (format & DT_WORDBREAK))
96             {
97                 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
98                     return NULL;
99                 plen += size.cx;
100             }
101             break;
102             
103         case PREFIX:
104             if (!(format & DT_NOPREFIX) && *count > 1)
105                 {
106                 if (str[++i] == PREFIX)
107                     (*count)--;
108                 else {
109                     prefix_offset = j;
110                     break;
111                 }
112             }
113             dest[j++] = str[i++];
114             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
115                 (format & DT_WORDBREAK))
116             {
117                 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
118                     return NULL;
119                 plen += size.cx;
120             }
121             break;
122             
123         case TAB:
124             if (format & DT_EXPANDTABS)
125             {
126                 wb_i = ++i;
127                 wb_j = j;
128                 wb_count = *count;
129
130                 if (!GetTextExtentPointW(hdc, &dest[lasttab], j - lasttab, &size))
131                     return NULL;
132
133                 numspaces = (tabwidth - size.cx) / spacewidth;
134                 for (k = 0; k < numspaces; k++)
135                     dest[j++] = SPACE;
136                 plen += tabwidth - size.cx;
137                 lasttab = wb_j + numspaces;
138             }
139             else
140             {
141                 dest[j++] = str[i++];
142                 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
143                     (format & DT_WORDBREAK))
144                 {
145                     if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
146                         return NULL;
147                     plen += size.cx;
148                 }
149             }
150             break;
151
152         case SPACE:
153             dest[j++] = str[i++];
154             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
155                 (format & DT_WORDBREAK))
156             {
157                 wb_i = i;
158                 wb_j = j - 1;
159                 wb_count = *count;
160                 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
161                     return NULL;
162                 plen += size.cx;
163             }
164             break;
165
166         default:
167             dest[j++] = str[i++];
168             if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
169                 (format & DT_WORDBREAK))
170             {
171                 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
172                     return NULL;
173                 plen += size.cx;
174             }
175         }
176
177         (*count)--;
178         if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
179         {
180             if (plen > width)
181             {
182                 if (format & DT_WORDBREAK)
183                 {
184                     if (wb_j)
185                     {
186                         *len = wb_j;
187                         *count = wb_count - 1;
188                         return (&str[wb_i]);
189                     }
190                 }
191                 else
192                 {
193                     *len = j;
194                     return (&str[i]);
195                 }
196             }
197         }
198     }
199     
200     *len = j;
201     return NULL;
202 }
203
204
205 /***********************************************************************
206  *           DrawText    (USER.85)
207  */
208 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
209 {
210     INT16 ret;
211
212     if (rect)
213     {
214         RECT rect32;
215         CONV_RECT16TO32( rect, &rect32 );
216         ret = DrawTextA( hdc, str, count, &rect32, flags );
217         CONV_RECT32TO16( &rect32, rect );
218     }
219     else ret = DrawTextA( hdc, str, count, NULL, flags);
220     return ret;
221 }
222
223
224 /***********************************************************************
225  *           DrawTextExW    (USER32.@)
226  */
227 #define MAX_STATIC_BUFFER 1024
228 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
229                         LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
230 {
231     SIZE size;
232     const WCHAR *strPtr;
233     static WCHAR line[MAX_STATIC_BUFFER];
234     int len, lh, count=i_count;
235     int prefix_x = 0;
236     int prefix_end = 0;
237     TEXTMETRICW tm;
238     int lmargin = 0, rmargin = 0;
239     int x = rect->left, y = rect->top;
240     int width = rect->right - rect->left;
241     int max_width = 0;
242
243     TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
244           rect->left, rect->top, rect->right, rect->bottom);
245
246    if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
247           dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
248
249     if (!str) return 0;
250     if (count == -1) count = strlenW(str);
251     if (count == 0) return 0;
252     strPtr = str;
253
254     GetTextMetricsW(hdc, &tm);
255     if (flags & DT_EXTERNALLEADING)
256         lh = tm.tmHeight + tm.tmExternalLeading;
257     else
258         lh = tm.tmHeight;
259
260     if (dtp)
261     {
262         lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
263         rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
264         if (!(flags & (DT_CENTER | DT_RIGHT)))
265             x += lmargin;
266         dtp->uiLengthDrawn = 0;     /* This param RECEIVES number of chars processed */
267     }
268
269     if (flags & DT_TABSTOP)
270         tabstop = dtp ? dtp->iTabLength : flags >> 8;
271
272     if (flags & DT_EXPANDTABS)
273     {
274         GetTextExtentPointW(hdc, SPACEW, 1, &size);
275         spacewidth = size.cx;
276         GetTextExtentPointW(hdc, oW, 1, &size);
277         tabwidth = size.cx * tabstop;
278     }
279
280     if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
281
282     do
283     {
284         prefix_offset = -1;
285         len = MAX_STATIC_BUFFER;
286         strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags);
287
288         if (prefix_offset != -1)
289         {
290             GetTextExtentPointW(hdc, line, prefix_offset, &size);
291             prefix_x = size.cx;
292             GetTextExtentPointW(hdc, line, prefix_offset + 1, &size);
293             prefix_end = size.cx - 1;
294         }
295
296         if (!GetTextExtentPointW(hdc, line, len, &size)) return 0;
297         if (flags & DT_CENTER) x = (rect->left + rect->right -
298                                     size.cx) / 2;
299         else if (flags & DT_RIGHT) x = rect->right - size.cx;
300
301         if (flags & DT_SINGLELINE)
302         {
303             if (flags & DT_VCENTER) y = rect->top +
304                 (rect->bottom - rect->top) / 2 - size.cy / 2;
305             else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
306
307             if (flags & (DT_PATH_ELLIPSIS | DT_END_ELLIPSIS | DT_WORD_ELLIPSIS))
308             {
309                 WCHAR swapStr[sizeof(line)];
310                 WCHAR* fnameDelim = NULL;
311                 int totalLen = i_count >= 0 ? i_count : strlenW(str);
312
313                 if (size.cx > width)
314                 {
315                     int fnameLen = totalLen;
316
317                     /* allow room for '...' */
318                     count = min(totalLen+3, sizeof(line)-3);
319
320                     if (flags & DT_WORD_ELLIPSIS)
321                         flags |= DT_WORDBREAK;
322
323                     if (flags & DT_PATH_ELLIPSIS)
324                     {
325                         WCHAR* lastBkSlash = NULL;
326                         WCHAR* lastFwdSlash = NULL;
327                         strncpyW(line, str, totalLen);
328                         line[totalLen] = '\0';
329                         lastBkSlash = strrchrW(line, BACK_SLASHW[0]);
330                         lastFwdSlash = strrchrW(line, FORWARD_SLASHW[0]);
331                         fnameDelim = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
332
333                         if (fnameDelim)
334                             fnameLen = &line[totalLen] - fnameDelim;
335                         else
336                             fnameDelim = (WCHAR*)str;
337
338                         strcpyW(swapStr, ELLIPSISW);
339                         strncpyW(swapStr+strlenW(swapStr), fnameDelim, fnameLen);
340                         swapStr[fnameLen+3] = '\0';
341                         strncpyW(swapStr+strlenW(swapStr), str, totalLen - fnameLen);
342                         swapStr[totalLen+3] = '\0';
343                     }
344                     else  /* DT_END_ELLIPSIS | DT_WORD_ELLIPSIS */
345                     {
346                         strcpyW(swapStr, ELLIPSISW);
347                         strncpyW(swapStr+strlenW(swapStr), str, totalLen);
348                     }
349
350                     len = MAX_STATIC_BUFFER;
351                     TEXT_NextLineW(hdc, swapStr, &count, line, &len, width, flags);
352
353                     /* if only the ELLIPSIS will fit, just let it be clipped */
354                     len = max(3, len);
355                     GetTextExtentPointW(hdc, line, len, &size);
356
357                     /* FIXME:
358                      * NextLine uses GetTextExtentPoint for each character,
359                      * rather than GetCharABCWidth...  So the whitespace between
360                      * characters is ignored in the width measurement, and the
361                      * reported len is too great.  To compensate, we must get
362                      * the width of the entire line and adjust len accordingly.
363                     */
364                     while ((size.cx > width) && (len > 3))
365                     {
366                         line[--len] = '\0';
367                         GetTextExtentPointW(hdc, line, len, &size);
368                     }
369
370                     if (fnameLen < len-3) /* some of the path will fit */
371                     {
372                         /* put the ELLIPSIS between the path and filename */
373                         strncpyW(swapStr, &line[fnameLen+3], len-3-fnameLen);
374                         swapStr[len-3-fnameLen] = '\0';
375                         strcatW(swapStr, ELLIPSISW);
376                         strncpyW(swapStr+strlenW(swapStr), &line[3], fnameLen);
377                     }
378                     else
379                     {
380                         /* move the ELLIPSIS to the end */
381                         strncpyW(swapStr, &line[3], len-3);
382                         swapStr[len-3] = '\0';
383                         strcpyW(swapStr+strlenW(swapStr), ELLIPSISW);
384                     }
385
386                     strncpyW(line, swapStr, len);
387                     line[len] = '\0';
388                     strPtr = NULL;
389                 }
390                if (flags & DT_MODIFYSTRING)
391                     strcpyW(str, swapStr);
392             }
393         }
394         if (!(flags & DT_CALCRECT))
395         {
396            if (!ExtTextOutW( hdc, x, y,
397                              ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
398                              ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
399                     rect, line, len, NULL ))  return 0;
400             if (prefix_offset != -1)
401             {
402                 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
403                 HPEN oldPen = SelectObject( hdc, hpen );
404                 MoveToEx(hdc, x + prefix_x, y + tm.tmAscent + 1, NULL );
405                 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
406                 SelectObject( hdc, oldPen );
407                 DeleteObject( hpen );
408             }
409         }
410         else if (size.cx > max_width)
411             max_width = size.cx;
412
413         y += lh;
414         if (strPtr)
415         {
416             if (!(flags & DT_NOCLIP))
417             {
418                 if (y > rect->bottom - lh)
419                     break;
420             }
421         }
422         if (dtp)
423             dtp->uiLengthDrawn += len;
424     }
425     while (strPtr);
426
427     if (flags & DT_CALCRECT)
428     {
429         rect->right = rect->left + max_width;
430         rect->bottom = y;
431         if (dtp)
432             rect->right += lmargin + rmargin;
433     }
434     return y - rect->top;
435 }
436
437 /***********************************************************************
438  *           DrawTextExA    (USER32.@)
439  */
440 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
441                         LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
442 {
443    WCHAR *wstr;
444    INT ret = 0;
445    DWORD wcount;
446
447    if (count == -1) count = strlen(str);
448    if (!count) return 0;
449    wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
450    wstr = HeapAlloc(GetProcessHeap(), 0, wcount * sizeof(WCHAR));
451    if (wstr)
452    {
453        MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
454        ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
455        if (flags & DT_MODIFYSTRING)
456             WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, count, NULL, NULL );
457        HeapFree(GetProcessHeap(), 0, wstr);
458    }
459    return ret;
460 }
461
462 /***********************************************************************
463  *           DrawTextW    (USER32.@)
464  */
465 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
466 {
467     return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, NULL);
468 }
469
470 /***********************************************************************
471  *           DrawTextA    (USER32.@)
472  */
473 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
474 {
475     return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, NULL );
476 }
477
478 /***********************************************************************
479  *           TEXT_GrayString
480  *
481  * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
482  * heap and we can guarantee that the handles fit in an INT16. We have to
483  * rethink the strategy once the migration to NT handles is complete.
484  * We are going to get a lot of code-duplication once this migration is
485  * completed...
486  * 
487  */
488 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
489                             INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
490 {
491     HBITMAP hbm, hbmsave;
492     HBRUSH hbsave;
493     HFONT hfsave;
494     HDC memdc = CreateCompatibleDC(hdc);
495     int slen = len;
496     BOOL retval = TRUE;
497     COLORREF fg, bg;
498
499     if(!hdc) return FALSE;
500     
501     if(len == 0)
502     {
503         if(unicode)
504             slen = lstrlenW((LPCWSTR)lp);
505         else if(_32bit)
506             slen = strlen((LPCSTR)lp);
507         else
508             slen = strlen(MapSL(lp));
509     }
510
511     if((cx == 0 || cy == 0) && slen != -1)
512     {
513         SIZE s;
514         if(unicode)
515             GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
516         else if(_32bit)
517             GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
518         else
519             GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
520         if(cx == 0) cx = s.cx;
521         if(cy == 0) cy = s.cy;
522     }
523
524     hbm = CreateBitmap(cx, cy, 1, 1, NULL);
525     hbmsave = (HBITMAP)SelectObject(memdc, hbm);
526     hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
527     PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
528     SelectObject( memdc, hbsave );
529     SetTextColor(memdc, RGB(255, 255, 255));
530     SetBkColor(memdc, RGB(0, 0, 0));
531     hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
532
533     if(fn)
534     {
535         if(_32bit)
536             retval = fn(memdc, lp, slen);
537         else
538             retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
539     }
540     else
541     {
542         if(unicode)
543             TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
544         else if(_32bit)
545             TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
546         else
547             TextOutA(memdc, 0, 0, MapSL(lp), slen);
548     }
549
550     SelectObject(memdc, hfsave);
551
552 /*
553  * Windows doc says that the bitmap isn't grayed when len == -1 and
554  * the callback function returns FALSE. However, testing this on
555  * win95 showed otherwise...
556 */
557 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
558     if(retval || len != -1)
559 #endif
560     {
561         hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
562         PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
563         SelectObject(memdc, hbsave);
564     }
565
566     if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
567     fg = SetTextColor(hdc, RGB(0, 0, 0));
568     bg = SetBkColor(hdc, RGB(255, 255, 255));
569     BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
570     SetTextColor(hdc, fg);
571     SetBkColor(hdc, bg);
572     if(hb) SelectObject(hdc, hbsave);
573
574     SelectObject(memdc, hbmsave);
575     DeleteObject(hbm);
576     DeleteDC(memdc);
577     return retval;
578 }
579
580
581 /***********************************************************************
582  *           GrayString16   (USER.185)
583  */
584 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
585                             LPARAM lParam, INT16 cch, INT16 x, INT16 y,
586                             INT16 cx, INT16 cy )
587 {
588     return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch,
589                            x, y, cx, cy, FALSE, FALSE);
590 }
591
592
593 /***********************************************************************
594  *           GrayStringA   (USER32.@)
595  */
596 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
597                          LPARAM lParam, INT cch, INT x, INT y,
598                          INT cx, INT cy )
599 {
600     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
601                            FALSE, TRUE);
602 }
603
604
605 /***********************************************************************
606  *           GrayStringW   (USER32.@)
607  */
608 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
609                          LPARAM lParam, INT cch, INT x, INT y,
610                          INT cx, INT cy )
611 {
612     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
613                            TRUE, TRUE);
614 }
615
616 /***********************************************************************
617  *           TEXT_TabbedTextOut
618  *
619  * Helper function for TabbedTextOut() and GetTabbedTextExtent().
620  * Note: this doesn't work too well for text-alignment modes other
621  *       than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
622  */
623 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
624                                 INT count, INT cTabStops, const INT16 *lpTabPos16,
625                                 const INT *lpTabPos32, INT nTabOrg,
626                                 BOOL fDisplayText )
627 {
628     INT defWidth;
629     SIZE extent;
630     int i, tabPos = x;
631     int start = x;
632
633     extent.cx = 0;
634     extent.cy = 0;
635
636     if (cTabStops == 1)
637     {
638         defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
639         cTabStops = 0;
640     }
641     else
642     {
643         TEXTMETRICA tm;
644         GetTextMetricsA( hdc, &tm );
645         defWidth = 8 * tm.tmAveCharWidth;
646     }
647
648     while (count > 0)
649     {
650         for (i = 0; i < count; i++)
651             if (lpstr[i] == '\t') break;
652         GetTextExtentPointA( hdc, lpstr, i, &extent );
653         if (lpTabPos32)
654         {
655             while ((cTabStops > 0) &&
656                    (nTabOrg + *lpTabPos32 <= x + extent.cx))
657             {
658                 lpTabPos32++;
659                 cTabStops--;
660             }
661         }
662         else
663         {
664             while ((cTabStops > 0) &&
665                    (nTabOrg + *lpTabPos16 <= x + extent.cx))
666             {
667                 lpTabPos16++;
668                 cTabStops--;
669             }
670         }
671         if (i == count)
672             tabPos = x + extent.cx;
673         else if (cTabStops > 0)
674             tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
675         else
676             tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
677         if (fDisplayText)
678         {
679             RECT r;
680             r.left   = x;
681             r.top    = y;
682             r.right  = tabPos;
683             r.bottom = y + extent.cy;
684             ExtTextOutA( hdc, x, y,
685                            GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
686                            &r, lpstr, i, NULL );
687         }
688         x = tabPos;
689         count -= i+1;
690         lpstr += i+1;
691     }
692     return MAKELONG(tabPos - start, extent.cy);
693 }
694
695
696 /***********************************************************************
697  *           TabbedTextOut    (USER.196)
698  */
699 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
700                              INT16 count, INT16 cTabStops,
701                              const INT16 *lpTabPos, INT16 nTabOrg )
702 {
703     TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
704     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
705                                lpTabPos, NULL, nTabOrg, TRUE );
706 }
707
708
709 /***********************************************************************
710  *           TabbedTextOutA    (USER32.@)
711  */
712 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
713                             INT cTabStops, const INT *lpTabPos, INT nTabOrg )
714 {
715     TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
716     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
717                                NULL, lpTabPos, nTabOrg, TRUE );
718 }
719
720
721 /***********************************************************************
722  *           TabbedTextOutW    (USER32.@)
723  */
724 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
725                             INT cTabStops, const INT *lpTabPos, INT nTabOrg )
726 {
727     LONG ret;
728     LPSTR p;
729     INT acount;
730     UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
731
732     acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
733     p = HeapAlloc( GetProcessHeap(), 0, acount );
734     if(p == NULL) return 0; /* FIXME: is this the correct return on failure */ 
735     acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
736     ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
737     HeapFree( GetProcessHeap(), 0, p );
738     return ret;
739 }
740
741
742 /***********************************************************************
743  *           GetTabbedTextExtent    (USER.197)
744  */
745 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
746                                     INT16 cTabStops, const INT16 *lpTabPos )
747 {
748     TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
749     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
750                                lpTabPos, NULL, 0, FALSE );
751 }
752
753
754 /***********************************************************************
755  *           GetTabbedTextExtentA    (USER32.@)
756  */
757 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
758                                    INT cTabStops, const INT *lpTabPos )
759 {
760     TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
761     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
762                                NULL, lpTabPos, 0, FALSE );
763 }
764
765
766 /***********************************************************************
767  *           GetTabbedTextExtentW    (USER32.@)
768  */
769 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
770                                    INT cTabStops, const INT *lpTabPos )
771 {
772     LONG ret;
773     LPSTR p;
774     INT acount;
775     UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
776
777     acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
778     p = HeapAlloc( GetProcessHeap(), 0, acount );
779     if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
780     acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
781     ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
782     HeapFree( GetProcessHeap(), 0, p );
783     return ret;
784 }