Moved some more 16-bit functions to user16.c.
[wine] / dlls / user / text.c
1 /*
2  * USER text functions
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  * Copyright 2002 Bill Medland
6  *
7  * Contains
8  *   1.  DrawText functions
9  *   2.  GrayString functions
10  *   3.  TabbedText functions
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "config.h"
28 #include "wine/port.h"
29
30 #include <string.h>
31 #include <assert.h>
32
33 #include "windef.h"
34 #include "wingdi.h"
35 #include "wine/winuser16.h"
36 #include "wine/unicode.h"
37 #include "winbase.h"
38 #include "winerror.h"
39 #include "winnls.h"
40 #include "wownt32.h"
41 #include "user.h"
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(text);
45
46 /*********************************************************************
47  *
48  *            DrawText functions
49  *
50  * Design issues
51  *   How many buffers to use
52  *     While processing in DrawText there are potentially three different forms
53  *     of the text that need to be held.  How are they best held?
54  *     1. The original text is needed, of course, to see what to display.
55  *     2. The text that will be returned to the user if the DT_MODIFYSTRING is
56  *        in effect.
57  *     3. The buffered text that is about to be displayed e.g. the current line.
58  *        Typically this will exclude the ampersands used for prefixing etc.
59  *
60  *     Complications.
61  *     a. If the buffered text to be displayed includes the ampersands then
62  *        we will need special measurement and draw functions that will ignore
63  *        the ampersands (e.g. by copying to a buffer without the prefix and
64  *        then using the normal forms).  This may involve less space but may
65  *        require more processing.  e.g. since a line containing tabs may
66  *        contain several underlined characters either we need to carry around
67  *        a list of prefix locations or we may need to locate them several
68  *        times.
69  *     b. If we actually directly modify the "original text" as we go then we
70  *        will need some special "caching" to handle the fact that when we
71  *        ellipsify the text the ellipsis may modify the next line of text,
72  *        which we have not yet processed.  (e.g. ellipsification of a W at the
73  *        end of a line will overwrite the W, the \n and the first character of
74  *        the next line, and a \0 will overwrite the second.  Try it!!)
75  *
76  *     Option 1.  Three separate storages. (To be implemented)
77  *       If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
78  *       the edited string in some form, either as the string itself or as some
79  *       sort of "edit list" to be applied just before returning.
80  *       Use a buffer that holds the ellipsified current line sans ampersands
81  *       and accept the need occasionally to recalculate the prefixes (if
82  *       DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
83  */
84
85 #define TAB     9
86 #define LF     10
87 #define CR     13
88 #define SPACE  32
89 #define PREFIX 38
90
91 #define FORWARD_SLASH '/'
92 #define BACK_SLASH '\\'
93
94 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
95
96 typedef struct tag_ellipsis_data
97 {
98     int before;
99     int len;
100     int under;
101     int after;
102 } ellipsis_data;
103
104 /*********************************************************************
105  *                      TEXT_Ellipsify (static)
106  *
107  *  Add an ellipsis to the end of the given string whilst ensuring it fits.
108  *
109  * If the ellipsis alone doesn't fit then it will be returned anyway.
110  *
111  * See Also TEXT_PathEllipsify
112  *
113  * Arguments
114  *   hdc        [in] The handle to the DC that defines the font.
115  *   str        [in/out] The string that needs to be modified.
116  *   max_str    [in] The dimension of str (number of WCHAR).
117  *   len_str    [in/out] The number of characters in str
118  *   width      [in] The maximum width permitted (in logical coordinates)
119  *   size       [out] The dimensions of the text
120  *   modstr     [out] The modified form of the string, to be returned to the
121  *                    calling program.  It is assumed that the caller has
122  *                    made sufficient space available so we don't need to
123  *                    know the size of the space.  This pointer may be NULL if
124  *                    the modified string is not required.
125  *   len_before [out] The number of characters before the ellipsis.
126  *   len_ellip  [out] The number of characters in the ellipsis.
127  *
128  * See for example Microsoft article Q249678.
129  *
130  * For now we will simply use three dots rather than worrying about whether
131  * the font contains an explicit ellipsis character.
132  */
133 static void TEXT_Ellipsify (HDC hdc, WCHAR *str, unsigned int max_len,
134                             unsigned int *len_str, int width, SIZE *size,
135                             WCHAR *modstr,
136                             int *len_before, int *len_ellip)
137 {
138     unsigned int len_ellipsis;
139     unsigned int lo, mid, hi;
140
141     len_ellipsis = strlenW (ELLIPSISW);
142     if (len_ellipsis > max_len) len_ellipsis = max_len;
143     if (*len_str > max_len - len_ellipsis)
144         *len_str = max_len - len_ellipsis;
145
146     /* First do a quick binary search to get an upper bound for *len_str. */
147     if (*len_str > 0 &&
148         GetTextExtentExPointW(hdc, str, *len_str, width, NULL, NULL, size) &&
149         size->cx > width)
150     {
151         for (lo = 0, hi = *len_str; lo < hi; )
152         {
153             mid = (lo + hi) / 2;
154             if (!GetTextExtentExPointW(hdc, str, mid, width, NULL, NULL, size))
155                 break;
156             if (size->cx > width)
157                 hi = mid;
158             else
159                 lo = mid + 1;
160         }
161         *len_str = hi;
162     }
163     /* Now this should take only a couple iterations at most. */
164     for ( ; ; )
165     {
166         strncpyW (str + *len_str, ELLIPSISW, len_ellipsis);
167
168         if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
169                                     NULL, NULL, size)) break;
170
171         if (!*len_str || size->cx <= width) break;
172
173         (*len_str)--;
174     }
175     *len_ellip = len_ellipsis;
176     *len_before = *len_str;
177     *len_str += len_ellipsis;
178
179     if (modstr)
180     {
181         strncpyW (modstr, str, *len_str);
182         *(str+*len_str) = '\0';
183     }
184 }
185
186 /*********************************************************************
187  *                      TEXT_PathEllipsify (static)
188  *
189  * Add an ellipsis to the provided string in order to make it fit within
190  * the width.  The ellipsis is added as specified for the DT_PATH_ELLIPSIS
191  * flag.
192  *
193  * See Also TEXT_Ellipsify
194  *
195  * Arguments
196  *   hdc        [in] The handle to the DC that defines the font.
197  *   str        [in/out] The string that needs to be modified
198  *   max_str    [in] The dimension of str (number of WCHAR).
199  *   len_str    [in/out] The number of characters in str
200  *   width      [in] The maximum width permitted (in logical coordinates)
201  *   size       [out] The dimensions of the text
202  *   modstr     [out] The modified form of the string, to be returned to the
203  *                    calling program.  It is assumed that the caller has
204  *                    made sufficient space available so we don't need to
205  *                    know the size of the space.  This pointer may be NULL if
206  *                    the modified string is not required.
207  *   pellip     [out] The ellipsification results
208  *
209  * For now we will simply use three dots rather than worrying about whether
210  * the font contains an explicit ellipsis character.
211  *
212  * The following applies, I think to Win95.  We will need to extend it for
213  * Win98 which can have both path and end ellipsis at the same time (e.g.
214  *  C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
215  *
216  * The resulting string consists of as much as possible of the following:
217  * 1. The ellipsis itself
218  * 2. The last \ or / of the string (if any)
219  * 3. Everything after the last \ or / of the string (if any) or the whole
220  *    string if there is no / or \.  I believe that under Win95 this would
221  *    include everything even though some might be clipped off the end whereas
222  *    under Win98 that might be ellipsified too.
223  *    Yet to be investigated is whether this would include wordbreaking if the
224  *    filename is more than 1 word and splitting if DT_EDITCONTROL was in
225  *    effect.  (If DT_EDITCONTROL is in effect then on occasions text will be
226  *    broken within words).
227  * 4. All the stuff before the / or \, which is placed before the ellipsis.
228  */
229 static void TEXT_PathEllipsify (HDC hdc, WCHAR *str, unsigned int max_len,
230                                 unsigned int *len_str, int width, SIZE *size,
231                                 WCHAR *modstr, ellipsis_data *pellip)
232 {
233     int len_ellipsis;
234     int len_trailing;
235     int len_under;
236     WCHAR *lastBkSlash, *lastFwdSlash, *lastSlash;
237
238     len_ellipsis = strlenW (ELLIPSISW);
239     if (!max_len) return;
240     if (len_ellipsis >= max_len) len_ellipsis = max_len - 1;
241     if (*len_str + len_ellipsis >= max_len)
242         *len_str = max_len - len_ellipsis-1;
243         /* Hopefully this will never happen, otherwise it would probably lose
244          * the wrong character
245          */
246     str[*len_str] = '\0'; /* to simplify things */
247
248     lastBkSlash  = strrchrW (str, BACK_SLASH);
249     lastFwdSlash = strrchrW (str, FORWARD_SLASH);
250     lastSlash = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
251     if (!lastSlash) lastSlash = str;
252     len_trailing = *len_str - (lastSlash - str);
253
254     /* overlap-safe movement to the right */
255     memmove (lastSlash+len_ellipsis, lastSlash, len_trailing * sizeof(WCHAR));
256     strncpyW (lastSlash, ELLIPSISW, len_ellipsis);
257     len_trailing += len_ellipsis;
258     /* From this point on lastSlash actually points to the ellipsis in front
259      * of the last slash and len_trailing includes the ellipsis
260      */
261
262     len_under = 0;
263     for ( ; ; )
264     {
265         if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
266                                     NULL, NULL, size)) break;
267
268         if (lastSlash == str || size->cx <= width) break;
269
270         /* overlap-safe movement to the left */
271         memmove (lastSlash-1, lastSlash, len_trailing * sizeof(WCHAR));
272         lastSlash--;
273         len_under++;
274
275         assert (*len_str);
276         (*len_str)--;
277     }
278     pellip->before = lastSlash-str;
279     pellip->len = len_ellipsis;
280     pellip->under = len_under;
281     pellip->after = len_trailing - len_ellipsis;
282     *len_str += len_ellipsis;
283
284     if (modstr)
285     {
286         strncpyW (modstr, str, *len_str);
287         *(str+*len_str) = '\0';
288     }
289 }
290
291 /*********************************************************************
292  *                      TEXT_WordBreak (static)
293  *
294  *  Perform wordbreak processing on the given string
295  *
296  * Assumes that DT_WORDBREAK has been specified and not all the characters
297  * fit.  Note that this function should even be called when the first character
298  * that doesn't fit is known to be a space or tab, so that it can swallow them.
299  *
300  * Note that the Windows processing has some strange properties.
301  * 1. If the text is left-justified and there is room for some of the spaces
302  *    that follow the last word on the line then those that fit are included on
303  *    the line.
304  * 2. If the text is centred or right-justified and there is room for some of
305  *    the spaces that follow the last word on the line then all but one of those
306  *    that fit are included on the line.
307  * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
308  *    character of a new line it will be skipped.
309  *
310  * Arguments
311  *   hdc        [in] The handle to the DC that defines the font.
312  *   str        [in/out] The string that needs to be broken.
313  *   max_str    [in] The dimension of str (number of WCHAR).
314  *   len_str    [in/out] The number of characters in str
315  *   width      [in] The maximum width permitted
316  *   format     [in] The format flags in effect
317  *   chars_fit  [in] The maximum number of characters of str that are already
318  *              known to fit; chars_fit+1 is known not to fit.
319  *   chars_used [out] The number of characters of str that have been "used" and
320  *              do not need to be included in later text.  For example this will
321  *              include any spaces that have been discarded from the start of
322  *              the next line.
323  *   size       [out] The size of the returned text in logical coordinates
324  *
325  * Pedantic assumption - Assumes that the text length is monotonically
326  * increasing with number of characters (i.e. no weird kernings)
327  *
328  * Algorithm
329  *
330  * Work back from the last character that did fit to either a space or the last
331  * character of a word, whichever is met first.
332  * If there was one or the first character didn't fit then
333  *     If the text is centred or right justified and that one character was a
334  *     space then break the line before that character
335  *     Otherwise break the line after that character
336  *     and if the next character is a space then discard it.
337  * Suppose there was none (and the first character did fit).
338  *     If Break Within Word is permitted
339  *         break the word after the last character that fits (there must be
340  *         at least one; none is caught earlier).
341  *     Otherwise
342  *         discard any trailing space.
343  *         include the whole word; it may be ellipsified later
344  *
345  * Break Within Word is permitted under a set of circumstances that are not
346  * totally clear yet.  Currently our best guess is:
347  *     If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
348  *     DT_PATH_ELLIPSIS is
349  */
350
351 static void TEXT_WordBreak (HDC hdc, WCHAR *str, unsigned int max_str,
352                             unsigned int *len_str,
353                             int width, int format, unsigned int chars_fit,
354                             unsigned int *chars_used, SIZE *size)
355 {
356     WCHAR *p;
357     int word_fits;
358     assert (format & DT_WORDBREAK);
359     assert (chars_fit < *len_str);
360
361     /* Work back from the last character that did fit to either a space or the
362      * last character of a word, whichever is met first.
363      */
364     p = str + chars_fit; /* The character that doesn't fit */
365     word_fits = TRUE;
366     if (!chars_fit)
367         ; /* we pretend that it fits anyway */
368     else if (*p == SPACE) /* chars_fit < *len_str so this is valid */
369         p--; /* the word just fitted */
370     else
371     {
372         while (p > str && *(--p) != SPACE)
373             ;
374         word_fits = (p != str || *p == SPACE);
375     }
376     /* If there was one or the first character didn't fit then */
377     if (word_fits)
378     {
379         int next_is_space;
380         /* break the line before/after that character */
381         if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
382             p++;
383         next_is_space = (p - str) < *len_str && *p == SPACE;
384         *len_str = p - str;
385         /* and if the next character is a space then discard it. */
386         *chars_used = *len_str;
387         if (next_is_space)
388             (*chars_used)++;
389     }
390     /* Suppose there was none. */
391     else
392     {
393         if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
394             DT_EDITCONTROL)
395         {
396             /* break the word after the last character that fits (there must be
397              * at least one; none is caught earlier).
398              */
399             *len_str = chars_fit;
400             *chars_used = chars_fit;
401
402             /* FIXME - possible error.  Since the next character is now removed
403              * this could make the text longer so that it no longer fits, and
404              * so we need a loop to test and shrink.
405              */
406         }
407         /* Otherwise */
408         else
409         {
410             /* discard any trailing space. */
411             const WCHAR *e = str + *len_str;
412             p = str + chars_fit;
413             while (p < e && *p != SPACE)
414                 p++;
415             *chars_used = p - str;
416             if (p < e) /* i.e. loop failed because *p == SPACE */
417                 (*chars_used)++;
418
419             /* include the whole word; it may be ellipsified later */
420             *len_str = p - str;
421             /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
422              * so that it will be too long
423              */
424         }
425     }
426     /* Remeasure the string */
427     GetTextExtentExPointW (hdc, str, *len_str, 0, NULL, NULL, size);
428 }
429
430 /*********************************************************************
431  *                      TEXT_SkipChars
432  *
433  *  Skip over the given number of characters, bearing in mind prefix
434  *  substitution and the fact that a character may take more than one
435  *  WCHAR (Unicode surrogates are two words long) (and there may have been
436  *  a trailing &)
437  *
438  * Parameters
439  *   new_count  [out] The updated count
440  *   new_str    [out] The updated pointer
441  *   start_count [in] The count of remaining characters corresponding to the
442  *                    start of the string
443  *   start_str  [in] The starting point of the string
444  *   max        [in] The number of characters actually in this segment of the
445  *                   string (the & counts)
446  *   n          [in] The number of characters to skip (if prefix then
447  *                   &c counts as one)
448  *   prefix     [in] Apply prefix substitution
449  *
450  * Return Values
451  *   none
452  *
453  * Remarks
454  *   There must be at least n characters in the string
455  *   We need max because the "line" may have ended with a & followed by a tab
456  *   or newline etc. which we don't want to swallow
457  */
458
459 static void TEXT_SkipChars (int *new_count, const WCHAR **new_str,
460                             int start_count, const WCHAR *start_str,
461                             int max, int n, int prefix)
462 {
463     /* This is specific to wide characters, MSDN doesn't say anything much
464      * about Unicode surrogates yet and it isn't clear if _wcsinc will
465      * correctly handle them so we'll just do this the easy way for now
466      */
467
468     if (prefix)
469     {
470         const WCHAR *str_on_entry = start_str;
471         assert (max >= n);
472         max -= n;
473         while (n--)
474             if (*start_str++ == PREFIX && max--)
475                 start_str++;
476             else;
477         start_count -= (start_str - str_on_entry);
478     }
479     else
480     {
481         start_str += n;
482         start_count -= n;
483     }
484     *new_str = start_str;
485     *new_count = start_count;
486 }
487
488 /*********************************************************************
489  *                      TEXT_Reprefix
490  *
491  *  Reanalyse the text to find the prefixed character.  This is called when
492  *  wordbreaking or ellipsification has shortened the string such that the
493  *  previously noted prefixed character is no longer visible.
494  *
495  * Parameters
496  *   str        [in] The original string segment (including all characters)
497  *   ns         [in] The number of characters in str (including prefixes)
498  *   pe         [in] The ellipsification data
499  *
500  * Return Values
501  *   The prefix offset within the new string segment (the one that contains the
502  *   ellipses and does not contain the prefix characters) (-1 if none)
503  */
504
505 static int TEXT_Reprefix (const WCHAR *str, unsigned int ns,
506                           const ellipsis_data *pe)
507 {
508     int result = -1;
509     unsigned int i = 0;
510     unsigned int n = pe->before + pe->under + pe->after;
511     assert (n <= ns);
512     while (i < n)
513     {
514         if (i == pe->before)
515         {
516             /* Reached the path ellipsis; jump over it */
517             if (ns < pe->under) break;
518             str += pe->under;
519             ns -= pe->under;
520             i += pe->under;
521             if (!pe->after) break; /* Nothing after the path ellipsis */
522         }
523         if (!ns) break;
524         ns--;
525         if (*str++ == PREFIX)
526         {
527             if (!ns) break;
528             if (*str != PREFIX)
529                 result = (i < pe->before || pe->under == 0) ? i : i - pe->under + pe->len;
530                 /* pe->len may be non-zero while pe_under is zero */
531             str++;
532             ns--;
533         }
534         else;
535         i++;
536     }
537     return result;
538 }
539
540 /*********************************************************************
541  *  Returns true if and only if the remainder of the line is a single
542  *  newline representation or nothing
543  */
544
545 static int remainder_is_none_or_newline (int num_chars, const WCHAR *str)
546 {
547     if (!num_chars) return TRUE;
548     if (*str != LF && *str != CR) return FALSE;
549     if (!--num_chars) return TRUE;
550     if (*str == *(str+1)) return FALSE;
551     str++;
552     if (*str != CR && *str != LF) return FALSE;
553     if (--num_chars) return FALSE;
554     return TRUE;
555 }
556
557 /*********************************************************************
558  *  Return next line of text from a string.
559  *
560  * hdc - handle to DC.
561  * str - string to parse into lines.
562  * count - length of str.
563  * dest - destination in which to return line.
564  * len - dest buffer size in chars on input, copied length into dest on output.
565  * width - maximum width of line in pixels.
566  * format - format type passed to DrawText.
567  * retsize - returned size of the line in pixels.
568  * last_line - TRUE if is the last line that will be processed
569  * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
570  *            the return string is built.
571  * tabwidth - The width of a tab in logical coordinates
572  * pprefix_offset - Here is where we return the offset within dest of the first
573  *                  prefixed (underlined) character.  -1 is returned if there
574  *                  are none.  Note that there may be more; the calling code
575  *                  will need to use TEXT_Reprefix to find any later ones.
576  * pellip - Here is where we return the information about any ellipsification
577  *          that was carried out.  Note that if tabs are being expanded then
578  *          this data will correspond to the last text segment actually
579  *          returned in dest; by definition there would not have been any
580  *          ellipsification in earlier text segments of the line.
581  *
582  * Returns pointer to next char in str after end of the line
583  * or NULL if end of str reached.
584  */
585 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
586                                  WCHAR *dest, int *len, int width, DWORD format,
587                                  SIZE *retsize, int last_line, WCHAR **p_retstr,
588                                  int tabwidth, int *pprefix_offset,
589                                  ellipsis_data *pellip)
590 {
591     int i = 0, j = 0;
592     int plen = 0;
593     SIZE size;
594     int maxl = *len;
595     int seg_i, seg_count, seg_j;
596     int max_seg_width;
597     int num_fit;
598     int word_broken;
599     int line_fits;
600     int j_in_seg;
601     int ellipsified;
602     *pprefix_offset = -1;
603
604     /* For each text segment in the line */
605
606     retsize->cy = 0;
607     while (*count)
608     {
609
610         /* Skip any leading tabs */
611
612         if (str[i] == TAB && (format & DT_EXPANDTABS))
613         {
614             plen = ((plen/tabwidth)+1)*tabwidth;
615             (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
616             while (*count && str[i] == TAB)
617             {
618                 plen += tabwidth;
619                 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
620             }
621         }
622
623
624         /* Now copy as far as the next tab or cr/lf or eos */
625
626         seg_i = i;
627         seg_count = *count;
628         seg_j = j;
629
630         while (*count &&
631                (str[i] != TAB || !(format & DT_EXPANDTABS)) &&
632                ((str[i] != CR && str[i] != LF) || (format & DT_SINGLELINE)))
633         {
634             if (str[i] == PREFIX && !(format & DT_NOPREFIX) && *count > 1)
635             {
636                 (*count)--, i++; /* Throw away the prefix itself */
637                 if (str[i] == PREFIX)
638                 {
639                     /* Swallow it before we see it again */
640                     (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
641                 }
642                 else if (*pprefix_offset == -1 || *pprefix_offset >= seg_j)
643                 {
644                     *pprefix_offset = j;
645                 }
646                 /* else the previous prefix was in an earlier segment of the
647                  * line; we will leave it to the drawing code to catch this
648                  * one.
649                  */
650             }
651             else
652             {
653                 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
654             }
655         }
656
657
658         /* Measure the whole text segment and possibly WordBreak and
659          * ellipsify it
660          */
661
662         j_in_seg = j - seg_j;
663         max_seg_width = width - plen;
664         GetTextExtentExPointW (hdc, dest + seg_j, j_in_seg, max_seg_width, &num_fit, NULL, &size);
665
666         /* The Microsoft handling of various combinations of formats is weird.
667          * The following may very easily be incorrect if several formats are
668          * combined, and may differ between versions (to say nothing of the
669          * several bugs in the Microsoft versions).
670          */
671         word_broken = 0;
672         line_fits = (num_fit >= j_in_seg);
673         if (!line_fits && (format & DT_WORDBREAK))
674         {
675             const WCHAR *s;
676             int chars_used;
677             TEXT_WordBreak (hdc, dest+seg_j, maxl-seg_j, &j_in_seg,
678                             max_seg_width, format, num_fit, &chars_used, &size);
679             line_fits = (size.cx <= max_seg_width);
680             /* and correct the counts */
681             TEXT_SkipChars (count, &s, seg_count, str+seg_i, i-seg_i,
682                             chars_used, !(format & DT_NOPREFIX));
683             i = s - str;
684             word_broken = 1;
685         }
686         pellip->before = j_in_seg;
687         pellip->under = 0;
688         pellip->after = 0;
689         pellip->len = 0;
690         ellipsified = 0;
691         if (!line_fits && (format & DT_PATH_ELLIPSIS))
692         {
693             TEXT_PathEllipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
694                                 max_seg_width, &size, *p_retstr, pellip);
695             line_fits = (size.cx <= max_seg_width);
696             ellipsified = 1;
697         }
698         /* NB we may end up ellipsifying a word-broken or path_ellipsified
699          * string */
700         if ((!line_fits && (format & DT_WORD_ELLIPSIS)) ||
701             ((format & DT_END_ELLIPSIS) &&
702               ((last_line && *count) ||
703                (remainder_is_none_or_newline (*count, &str[i]) && !line_fits))))
704         {
705             int before, len_ellipsis;
706             TEXT_Ellipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
707                             max_seg_width, &size, *p_retstr, &before, &len_ellipsis);
708             if (before > pellip->before)
709             {
710                 /* We must have done a path ellipsis too */
711                 pellip->after = before - pellip->before - pellip->len;
712                 /* Leave the len as the length of the first ellipsis */
713             }
714             else
715             {
716                 /* If we are here after a path ellipsification it must be
717                  * because even the ellipsis itself didn't fit.
718                  */
719                 assert (pellip->under == 0 && pellip->after == 0);
720                 pellip->before = before;
721                 pellip->len = len_ellipsis;
722                 /* pellip->after remains as zero as does
723                  * pellip->under
724                  */
725             }
726             line_fits = (size.cx <= max_seg_width);
727             ellipsified = 1;
728         }
729         /* As an optimisation if we have ellipsified and we are expanding
730          * tabs and we haven't reached the end of the line we can skip to it
731          * now rather than going around the loop again.
732          */
733         if ((format & DT_EXPANDTABS) && ellipsified)
734         {
735             if (format & DT_SINGLELINE)
736                 *count = 0;
737             else
738             {
739                 while ((*count) && str[i] != CR && str[i] != LF)
740                 {
741                     (*count)--, i++;
742                 }
743             }
744         }
745
746         j = seg_j + j_in_seg;
747         if (*pprefix_offset >= seg_j + pellip->before)
748         {
749             *pprefix_offset = TEXT_Reprefix (str + seg_i, i - seg_i, pellip);
750             if (*pprefix_offset != -1)
751                 *pprefix_offset += seg_j;
752         }
753
754         plen += size.cx;
755         if (size.cy > retsize->cy)
756             retsize->cy = size.cy;
757
758         if (word_broken)
759             break;
760         else if (!*count)
761             break;
762         else if (str[i] == CR || str[i] == LF)
763         {
764             (*count)--, i++;
765             if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
766             {
767                 (*count)--, i++;
768             }
769             break;
770         }
771         /* else it was a Tab and we go around again */
772     }
773
774     retsize->cx = plen;
775     *len = j;
776     if (*count)
777         return (&str[i]);
778     else
779         return NULL;
780 }
781
782
783 /***********************************************************************
784  *                      TEXT_DrawUnderscore
785  *
786  *  Draw the underline under the prefixed character
787  *
788  * Parameters
789  *   hdc        [in] The handle of the DC for drawing
790  *   x          [in] The x location of the line segment (logical coordinates)
791  *   y          [in] The y location of where the underscore should appear
792  *                   (logical coordinates)
793  *   str        [in] The text of the line segment
794  *   offset     [in] The offset of the underscored character within str
795  */
796
797 static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset)
798 {
799     int prefix_x;
800     int prefix_end;
801     SIZE size;
802     HPEN hpen;
803     HPEN oldPen;
804
805     GetTextExtentPointW (hdc, str, offset, &size);
806     prefix_x = x + size.cx;
807     GetTextExtentPointW (hdc, str, offset+1, &size);
808     prefix_end = x + size.cx - 1;
809     /* The above method may eventually be slightly wrong due to kerning etc. */
810
811     hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
812     oldPen = SelectObject (hdc, hpen);
813     MoveToEx (hdc, prefix_x, y, NULL);
814     LineTo (hdc, prefix_end, y);
815     SelectObject (hdc, oldPen);
816     DeleteObject (hpen);
817 }
818
819 /***********************************************************************
820  *           DrawTextExW    (USER32.@)
821  *
822  * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
823  * is not quite complete, especially with regard to \0.  We will assume that
824  * the returned string could have a length of up to i_count+3 and also have
825  * a trailing \0 (which would be 4 more than a not-null-terminated string but
826  * 3 more than a null-terminated string).  If this is not so then increase
827  * the allowance in DrawTextExA.
828  */
829 #define MAX_STATIC_BUFFER 1024
830 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
831                         LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
832 {
833     SIZE size;
834     const WCHAR *strPtr;
835     WCHAR *retstr, *p_retstr;
836     size_t size_retstr;
837     static WCHAR line[MAX_STATIC_BUFFER];
838     int len, lh, count=i_count;
839     TEXTMETRICW tm;
840     int lmargin = 0, rmargin = 0;
841     int x = rect->left, y = rect->top;
842     int width = rect->right - rect->left;
843     int max_width = 0;
844     int last_line;
845     int tabwidth /* to keep gcc happy */ = 0;
846     int prefix_offset;
847     ellipsis_data ellip;
848
849     TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
850           rect->left, rect->top, rect->right, rect->bottom);
851
852    if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
853           dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
854
855     if (!str) return 0;
856     if (count == -1) count = strlenW(str);
857     if (count == 0) return 0;
858     strPtr = str;
859
860     if (flags & DT_SINGLELINE)
861         flags &= ~DT_WORDBREAK;
862
863     GetTextMetricsW(hdc, &tm);
864     if (flags & DT_EXTERNALLEADING)
865         lh = tm.tmHeight + tm.tmExternalLeading;
866     else
867         lh = tm.tmHeight;
868
869     if (dtp)
870     {
871         lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
872         rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
873         if (!(flags & (DT_CENTER | DT_RIGHT)))
874             x += lmargin;
875         dtp->uiLengthDrawn = 0;     /* This param RECEIVES number of chars processed */
876     }
877
878     if (flags & DT_EXPANDTABS)
879     {
880         int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
881         tabwidth = tm.tmAveCharWidth * tabstop;
882     }
883
884     if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
885
886     if (flags & DT_MODIFYSTRING)
887     {
888         size_retstr = (count + 4) * sizeof (WCHAR);
889         retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
890         if (!retstr) return 0;
891         memcpy (retstr, str, size_retstr);
892     }
893     else
894     {
895         size_retstr = 0;
896         retstr = NULL;
897     }
898     p_retstr = retstr;
899
900     do
901     {
902         len = MAX_STATIC_BUFFER;
903         last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
904         strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr, tabwidth, &prefix_offset, &ellip);
905
906         if (flags & DT_CENTER) x = (rect->left + rect->right -
907                                     size.cx) / 2;
908         else if (flags & DT_RIGHT) x = rect->right - size.cx;
909
910         if (flags & DT_SINGLELINE)
911         {
912             if (flags & DT_VCENTER) y = rect->top +
913                 (rect->bottom - rect->top) / 2 - size.cy / 2;
914             else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
915         }
916
917         if (!(flags & DT_CALCRECT))
918         {
919             const WCHAR *str = line;
920             int xseg = x;
921             while (len)
922             {
923                 int len_seg;
924                 SIZE size;
925                 if ((flags & DT_EXPANDTABS))
926                 {
927                     const WCHAR *p;
928                     p = str; while (p < str+len && *p != TAB) p++;
929                     len_seg = p - str;
930                     if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size))
931                         return 0;
932                 }
933                 else
934                     len_seg = len;
935
936                 if (!ExtTextOutW( hdc, xseg, y,
937                                  ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
938                                  ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
939                                  rect, str, len_seg, NULL ))  return 0;
940                 if (prefix_offset != -1 && prefix_offset < len_seg)
941                 {
942                     TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset);
943                 }
944                 len -= len_seg;
945                 str += len_seg;
946                 if (len)
947                 {
948                     assert ((flags & DT_EXPANDTABS) && *str == TAB);
949                     len--; str++;
950                     xseg += ((size.cx/tabwidth)+1)*tabwidth;
951                     if (prefix_offset != -1)
952                     {
953                         if (prefix_offset < len_seg)
954                         {
955                             /* We have just drawn an underscore; we ought to
956                              * figure out where the next one is.  I am going
957                              * to leave it for now until I have a better model
958                              * for the line, which will make reprefixing easier.
959                              * This is where ellip would be used.
960                              */
961                             prefix_offset = -1;
962                         }
963                         else
964                             prefix_offset -= len_seg;
965                     }
966                 }
967             }
968         }
969         else if (size.cx > max_width)
970             max_width = size.cx;
971
972         y += lh;
973         if (dtp)
974             dtp->uiLengthDrawn += len;
975     }
976     while (strPtr && !last_line);
977
978     if (flags & DT_CALCRECT)
979     {
980         rect->right = rect->left + max_width;
981         rect->bottom = y;
982         if (dtp)
983             rect->right += lmargin + rmargin;
984     }
985     if (retstr)
986     {
987         memcpy (str, retstr, size_retstr);
988         HeapFree (GetProcessHeap(), 0, retstr);
989     }
990     return y - rect->top;
991 }
992
993 /***********************************************************************
994  *           DrawTextExA    (USER32.@)
995  *
996  * If DT_MODIFYSTRING is specified then there must be room for up to
997  * 4 extra characters.  We take great care about just how much modified
998  * string we return.
999  */
1000 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
1001                         LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
1002 {
1003    WCHAR *wstr;
1004    WCHAR *p;
1005    INT ret = 0;
1006    int i;
1007    DWORD wcount;
1008    DWORD wmax;
1009    DWORD amax;
1010
1011    if (!str) return 0;
1012    if (count == -1) count = strlen(str);
1013    if (!count) return 0;
1014    wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
1015    wmax = wcount;
1016    amax = count;
1017    if (flags & DT_MODIFYSTRING)
1018    {
1019         wmax += 4;
1020         amax += 4;
1021    }
1022    wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
1023    if (wstr)
1024    {
1025        MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
1026        if (flags & DT_MODIFYSTRING)
1027            for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
1028            /* Initialise the extra characters so that we can see which ones
1029             * change.  U+FFFE is guaranteed to be not a unicode character and
1030             * so will not be generated by DrawTextEx itself.
1031             */
1032        ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
1033        if (flags & DT_MODIFYSTRING)
1034        {
1035             /* Unfortunately the returned string may contain multiple \0s
1036              * and so we need to measure it ourselves.
1037              */
1038             for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
1039             WideCharToMultiByte( CP_ACP, 0, wstr, wcount, str, amax, NULL, NULL );
1040        }
1041        HeapFree(GetProcessHeap(), 0, wstr);
1042    }
1043    return ret;
1044 }
1045
1046 /***********************************************************************
1047  *           DrawTextW    (USER32.@)
1048  */
1049 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
1050 {
1051     DRAWTEXTPARAMS dtp;
1052
1053     memset (&dtp, 0, sizeof(dtp));
1054     if (flags & DT_TABSTOP)
1055     {
1056         dtp.iTabLength = (flags >> 8) && 0xff;
1057         flags &= 0xffff00ff;
1058     }
1059     return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1060 }
1061
1062 /***********************************************************************
1063  *           DrawTextA    (USER32.@)
1064  */
1065 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1066 {
1067     DRAWTEXTPARAMS dtp;
1068
1069     memset (&dtp, 0, sizeof(dtp));
1070     if (flags & DT_TABSTOP)
1071     {
1072         dtp.iTabLength = (flags >> 8) && 0xff;
1073         flags &= 0xffff00ff;
1074     }
1075     return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1076 }
1077
1078 /***********************************************************************
1079  *
1080  *           GrayString functions
1081  */
1082
1083 /* ### start build ### */
1084 extern WORD CALLBACK TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16,WORD,LONG,WORD);
1085 /* ### stop build ### */
1086
1087 struct gray_string_info
1088 {
1089     GRAYSTRINGPROC16 proc;
1090     LPARAM           param;
1091 };
1092
1093 /* callback for 16-bit gray string proc */
1094 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
1095 {
1096     const struct gray_string_info *info = (struct gray_string_info *)param;
1097     return TEXT_CallTo16_word_wlw( info->proc, HDC_16(hdc), info->param, len );
1098 }
1099
1100 /***********************************************************************
1101  *           TEXT_GrayString
1102  *
1103  * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1104  * heap and we can guarantee that the handles fit in an INT16. We have to
1105  * rethink the strategy once the migration to NT handles is complete.
1106  * We are going to get a lot of code-duplication once this migration is
1107  * completed...
1108  *
1109  */
1110 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1111                             INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
1112 {
1113     HBITMAP hbm, hbmsave;
1114     HBRUSH hbsave;
1115     HFONT hfsave;
1116     HDC memdc;
1117     int slen = len;
1118     BOOL retval = TRUE;
1119     COLORREF fg, bg;
1120
1121     if(!hdc) return FALSE;
1122     if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1123
1124     if(len == 0)
1125     {
1126         if(unicode)
1127             slen = lstrlenW((LPCWSTR)lp);
1128         else if(_32bit)
1129             slen = strlen((LPCSTR)lp);
1130         else
1131             slen = strlen(MapSL(lp));
1132     }
1133
1134     if((cx == 0 || cy == 0) && slen != -1)
1135     {
1136         SIZE s;
1137         if(unicode)
1138             GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
1139         else if(_32bit)
1140             GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
1141         else
1142             GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
1143         if(cx == 0) cx = s.cx;
1144         if(cy == 0) cy = s.cy;
1145     }
1146
1147     hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1148     hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1149     hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1150     PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1151     SelectObject( memdc, hbsave );
1152     SetTextColor(memdc, RGB(255, 255, 255));
1153     SetBkColor(memdc, RGB(0, 0, 0));
1154     hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1155
1156     if(fn)
1157     {
1158         if(_32bit)
1159             retval = fn(memdc, lp, slen);
1160         else
1161             retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)(HDC_16(memdc), lp, (INT16)slen));
1162     }
1163     else
1164     {
1165         if(unicode)
1166             TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
1167         else if(_32bit)
1168             TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
1169         else
1170             TextOutA(memdc, 0, 0, MapSL(lp), slen);
1171     }
1172
1173     SelectObject(memdc, hfsave);
1174
1175 /*
1176  * Windows doc says that the bitmap isn't grayed when len == -1 and
1177  * the callback function returns FALSE. However, testing this on
1178  * win95 showed otherwise...
1179 */
1180 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1181     if(retval || len != -1)
1182 #endif
1183     {
1184         hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1185         PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1186         SelectObject(memdc, hbsave);
1187     }
1188
1189     if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
1190     fg = SetTextColor(hdc, RGB(0, 0, 0));
1191     bg = SetBkColor(hdc, RGB(255, 255, 255));
1192     BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1193     SetTextColor(hdc, fg);
1194     SetBkColor(hdc, bg);
1195     if(hb) SelectObject(hdc, hbsave);
1196
1197     SelectObject(memdc, hbmsave);
1198     DeleteObject(hbm);
1199     DeleteDC(memdc);
1200     return retval;
1201 }
1202
1203
1204 /***********************************************************************
1205  *           GrayString   (USER.185)
1206  */
1207 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
1208                             LPARAM lParam, INT16 cch, INT16 x, INT16 y,
1209                             INT16 cx, INT16 cy )
1210 {
1211     struct gray_string_info info;
1212
1213     if (!gsprc) return TEXT_GrayString(hdc, hbr, NULL, lParam, cch, x, y, cx, cy, FALSE, FALSE);
1214     info.proc  = gsprc;
1215     info.param = lParam;
1216     return TEXT_GrayString( hdc, hbr, gray_string_callback, (LPARAM)&info,
1217                             cch, x, y, cx, cy, FALSE, FALSE);
1218 }
1219
1220
1221 /***********************************************************************
1222  *           GrayStringA   (USER32.@)
1223  */
1224 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1225                          LPARAM lParam, INT cch, INT x, INT y,
1226                          INT cx, INT cy )
1227 {
1228     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1229                            FALSE, TRUE);
1230 }
1231
1232
1233 /***********************************************************************
1234  *           GrayStringW   (USER32.@)
1235  */
1236 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1237                          LPARAM lParam, INT cch, INT x, INT y,
1238                          INT cx, INT cy )
1239 {
1240     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1241                            TRUE, TRUE);
1242 }
1243
1244 /***********************************************************************
1245  *
1246  *           TabbedText functions
1247  */
1248
1249 /***********************************************************************
1250  *           TEXT_TabbedTextOut
1251  *
1252  * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1253  * Note: this doesn't work too well for text-alignment modes other
1254  *       than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1255  */
1256 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
1257                                 INT count, INT cTabStops, const INT16 *lpTabPos16,
1258                                 const INT *lpTabPos32, INT nTabOrg,
1259                                 BOOL fDisplayText )
1260 {
1261     INT defWidth;
1262     SIZE extent;
1263     int i, tabPos = x;
1264     int start = x;
1265
1266     extent.cx = 0;
1267     extent.cy = 0;
1268
1269     if (cTabStops == 1)
1270     {
1271         defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
1272         cTabStops = 0;
1273     }
1274     else
1275     {
1276         TEXTMETRICA tm;
1277         GetTextMetricsA( hdc, &tm );
1278         defWidth = 8 * tm.tmAveCharWidth;
1279     }
1280
1281     while (count > 0)
1282     {
1283         for (i = 0; i < count; i++)
1284             if (lpstr[i] == '\t') break;
1285         GetTextExtentPointA( hdc, lpstr, i, &extent );
1286         if (lpTabPos32)
1287         {
1288             while ((cTabStops > 0) &&
1289                    (nTabOrg + *lpTabPos32 <= x + extent.cx))
1290             {
1291                 lpTabPos32++;
1292                 cTabStops--;
1293             }
1294         }
1295         else
1296         {
1297             while ((cTabStops > 0) &&
1298                    (nTabOrg + *lpTabPos16 <= x + extent.cx))
1299             {
1300                 lpTabPos16++;
1301                 cTabStops--;
1302             }
1303         }
1304         if (i == count)
1305             tabPos = x + extent.cx;
1306         else if (cTabStops > 0)
1307             tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
1308         else
1309             tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
1310         if (fDisplayText)
1311         {
1312             RECT r;
1313             r.left   = x;
1314             r.top    = y;
1315             r.right  = tabPos;
1316             r.bottom = y + extent.cy;
1317             ExtTextOutA( hdc, x, y,
1318                            GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1319                            &r, lpstr, i, NULL );
1320         }
1321         x = tabPos;
1322         count -= i+1;
1323         lpstr += i+1;
1324     }
1325     return MAKELONG(tabPos - start, extent.cy);
1326 }
1327
1328
1329 /***********************************************************************
1330  *           TabbedTextOut    (USER.196)
1331  */
1332 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
1333                              INT16 count, INT16 cTabStops,
1334                              const INT16 *lpTabPos, INT16 nTabOrg )
1335 {
1336     TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1337     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1338                                lpTabPos, NULL, nTabOrg, TRUE );
1339 }
1340
1341
1342 /***********************************************************************
1343  *           TabbedTextOutA    (USER32.@)
1344  */
1345 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1346                             INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1347 {
1348     TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1349     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1350                                NULL, lpTabPos, nTabOrg, TRUE );
1351 }
1352
1353
1354 /***********************************************************************
1355  *           TabbedTextOutW    (USER32.@)
1356  */
1357 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1358                             INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1359 {
1360     LONG ret;
1361     LPSTR p;
1362     INT acount;
1363     UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1364
1365     acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
1366     p = HeapAlloc( GetProcessHeap(), 0, acount );
1367     if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
1368     acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
1369     ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
1370     HeapFree( GetProcessHeap(), 0, p );
1371     return ret;
1372 }
1373
1374
1375 /***********************************************************************
1376  *           GetTabbedTextExtent    (USER.197)
1377  */
1378 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
1379                                     INT16 cTabStops, const INT16 *lpTabPos )
1380 {
1381     TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1382     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1383                                lpTabPos, NULL, 0, FALSE );
1384 }
1385
1386
1387 /***********************************************************************
1388  *           GetTabbedTextExtentA    (USER32.@)
1389  */
1390 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1391                                    INT cTabStops, const INT *lpTabPos )
1392 {
1393     TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1394     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1395                                NULL, lpTabPos, 0, FALSE );
1396 }
1397
1398
1399 /***********************************************************************
1400  *           GetTabbedTextExtentW    (USER32.@)
1401  */
1402 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1403                                    INT cTabStops, const INT *lpTabPos )
1404 {
1405     LONG ret;
1406     LPSTR p;
1407     INT acount;
1408     UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1409
1410     acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
1411     p = HeapAlloc( GetProcessHeap(), 0, acount );
1412     if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
1413     acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
1414     ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
1415     HeapFree( GetProcessHeap(), 0, p );
1416     return ret;
1417 }