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