Update shell xxxAW wrapper prototypes for fixed SHLWAPI functions.
[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
578     /* For each text segment in the line */
579
580     retsize->cy = 0;
581     while (*count)
582     {
583
584         /* Skip any leading tabs */
585
586         if (str[i] == TAB && (format & DT_EXPANDTABS))
587         {
588             plen = ((plen/tabwidth)+1)*tabwidth;
589             (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
590             while (*count && str[i] == TAB)
591             {
592                 plen += tabwidth;
593                 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
594             }
595         }
596
597
598         /* Now copy as far as the next tab or cr/lf or eos */
599
600         seg_i = i;
601         seg_count = *count;
602         seg_j = j;
603
604         while (*count &&
605                (str[i] != TAB || !(format & DT_EXPANDTABS)) &&
606                ((str[i] != CR && str[i] != LF) || (format & DT_SINGLELINE)))
607         {
608             if (str[i] == PREFIX && !(format & DT_NOPREFIX) && *count > 1)
609             {
610                 (*count)--, i++; /* Throw away the prefix itself */
611                 if (str[i] == PREFIX)
612                 {
613                     /* Swallow it before we see it again */
614                     (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
615                 }
616                 else if (*pprefix_offset == -1 || *pprefix_offset >= seg_j)
617                 {
618                     *pprefix_offset = j;
619                 }
620                 /* else the previous prefix was in an earlier segment of the
621                  * line; we will leave it to the drawing code to catch this
622                  * one.
623                  */
624             }
625             else
626             {
627                 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
628             }
629         }
630
631
632         /* Measure the whole text segment and possibly WordBreak and
633          * ellipsify it
634          */
635
636         j_in_seg = j - seg_j;
637         max_seg_width = width - plen;
638         GetTextExtentExPointW (hdc, dest + seg_j, j_in_seg, max_seg_width, &num_fit, NULL, &size);
639
640         /* The Microsoft handling of various combinations of formats is weird.
641          * The following may very easily be incorrect if several formats are
642          * combined, and may differ between versions (to say nothing of the
643          * several bugs in the Microsoft versions).
644          */
645         word_broken = 0;
646         line_fits = (num_fit >= j_in_seg);
647         if (!line_fits && (format & DT_WORDBREAK))
648         {
649             const WCHAR *s;
650             int chars_used;
651             TEXT_WordBreak (hdc, dest+seg_j, maxl-seg_j, &j_in_seg,
652                             max_seg_width, format, num_fit, &chars_used, &size);
653             line_fits = (size.cx <= max_seg_width);
654             /* and correct the counts */
655             TEXT_SkipChars (count, &s, seg_count, str+seg_i, i-seg_i,
656                             chars_used, !(format & DT_NOPREFIX));
657             i = s - str;
658             word_broken = 1;
659         }
660         pellip->before = j_in_seg;
661         pellip->under = 0;
662         pellip->after = 0;
663         pellip->len = 0;
664         ellipsified = 0;
665         if (!line_fits && (format & DT_PATH_ELLIPSIS))
666         {
667             TEXT_PathEllipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
668                                 max_seg_width, &size, *p_retstr, pellip);
669             line_fits = (size.cx <= max_seg_width);
670             ellipsified = 1;
671         }
672         /* NB we may end up ellipsifying a word-broken or path_ellipsified
673          * string */
674         if ((!line_fits && (format & DT_WORD_ELLIPSIS)) ||
675             ((format & DT_END_ELLIPSIS) && 
676               ((last_line && *count) ||
677                (remainder_is_none_or_newline (*count, &str[i]) && !line_fits))))
678         {
679             int before, len_ellipsis;
680             TEXT_Ellipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
681                             max_seg_width, &size, *p_retstr, &before, &len_ellipsis);
682             if (before > pellip->before)
683             {
684                 /* We must have done a path ellipsis too */
685                 pellip->after = before - pellip->before - pellip->len;
686                 /* Leave the len as the length of the first ellipsis */
687             }
688             else
689             {
690                 /* If we are here after a path ellipsification it must be
691                  * because even the ellipsis itself didn't fit.
692                  */
693                 assert (pellip->under == 0 && pellip->after == 0);
694                 pellip->before = before;
695                 pellip->len = len_ellipsis;
696                 /* pellip->after remains as zero as does
697                  * pellip->under
698                  */
699             }
700             line_fits = (size.cx <= max_seg_width);
701             ellipsified = 1;
702         }
703         /* As an optimisation if we have ellipsified and we are expanding
704          * tabs and we haven't reached the end of the line we can skip to it
705          * now rather than going around the loop again.
706          */
707         if ((format & DT_EXPANDTABS) && ellipsified)
708         {
709             if (format & DT_SINGLELINE)
710                 *count = 0;
711             else
712             {
713                 while ((*count) && str[i] != CR && str[i] != LF)
714                 {
715                     (*count)--, i++;
716                 }
717             }
718         }
719
720         j = seg_j + j_in_seg;
721         if (*pprefix_offset >= seg_j + pellip->before)
722         {
723             *pprefix_offset = TEXT_Reprefix (str + seg_i, i - seg_i, pellip);
724             if (*pprefix_offset != -1)
725                 *pprefix_offset += seg_j;
726         }
727
728         plen += size.cx;
729         if (size.cy > retsize->cy)
730             retsize->cy = size.cy;
731
732         if (word_broken)
733             break;
734         else if (!*count)
735             break;
736         else if (str[i] == CR || str[i] == LF)
737         {
738             (*count)--, i++;
739             if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
740             {
741                 (*count)--, i++;
742             }
743             break;
744         }
745         /* else it was a Tab and we go around again */
746     }
747     
748     retsize->cx = plen;
749     *len = j;
750     if (*count)
751         return (&str[i]);
752     else
753         return NULL;
754 }
755
756
757 /***********************************************************************
758  *                      TEXT_DrawUnderscore
759  *
760  *  Draw the underline under the prefixed character
761  *
762  * Parameters
763  *   hdc        [in] The handle of the DC for drawing
764  *   x          [in] The x location of the line segment (logical coordinates)
765  *   y          [in] The y location of where the underscore should appear
766  *                   (logical coordinates)
767  *   str        [in] The text of the line segment
768  *   offset     [in] The offset of the underscored character within str
769  */
770
771 static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset)
772 {
773     int prefix_x;
774     int prefix_end;
775     SIZE size;
776     HPEN hpen;
777     HPEN oldPen;
778
779     GetTextExtentPointW (hdc, str, offset, &size);
780     prefix_x = x + size.cx;
781     GetTextExtentPointW (hdc, str, offset+1, &size);
782     prefix_end = x + size.cx - 1;
783     /* The above method may eventually be slightly wrong due to kerning etc. */
784
785     hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
786     oldPen = SelectObject (hdc, hpen);
787     MoveToEx (hdc, prefix_x, y, NULL);
788     LineTo (hdc, prefix_end, y);
789     SelectObject (hdc, oldPen);
790     DeleteObject (hpen);
791 }
792
793 /***********************************************************************
794  *           DrawTextExW    (USER32.@)
795  *
796  * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
797  * is not quite complete, especially with regard to \0.  We will assume that
798  * the returned string could have a length of up to i_count+3 and also have
799  * a trailing \0 (which would be 4 more than a not-null-terminated string but
800  * 3 more than a null-terminated string).  If this is not so then increase 
801  * the allowance in DrawTextExA.
802  */
803 #define MAX_STATIC_BUFFER 1024
804 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
805                         LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
806 {
807     SIZE size;
808     const WCHAR *strPtr;
809     WCHAR *retstr, *p_retstr;
810     size_t size_retstr;
811     static WCHAR line[MAX_STATIC_BUFFER];
812     int len, lh, count=i_count;
813     TEXTMETRICW tm;
814     int lmargin = 0, rmargin = 0;
815     int x = rect->left, y = rect->top;
816     int width = rect->right - rect->left;
817     int max_width = 0;
818     int last_line;
819     int tabwidth /* to keep gcc happy */ = 0;
820     int prefix_offset;
821     ellipsis_data ellip;
822
823     TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
824           rect->left, rect->top, rect->right, rect->bottom);
825
826    if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
827           dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
828
829     if (!str) return 0;
830     if (count == -1) count = strlenW(str);
831     if (count == 0) return 0;
832     strPtr = str;
833
834     if (flags & DT_SINGLELINE)
835         flags &= ~DT_WORDBREAK;
836
837     GetTextMetricsW(hdc, &tm);
838     if (flags & DT_EXTERNALLEADING)
839         lh = tm.tmHeight + tm.tmExternalLeading;
840     else
841         lh = tm.tmHeight;
842
843     if (dtp)
844     {
845         lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
846         rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
847         if (!(flags & (DT_CENTER | DT_RIGHT)))
848             x += lmargin;
849         dtp->uiLengthDrawn = 0;     /* This param RECEIVES number of chars processed */
850     }
851
852     if (flags & DT_EXPANDTABS)
853     {
854         int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
855         tabwidth = tm.tmAveCharWidth * tabstop;
856     }
857
858     if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
859
860     if (flags & DT_MODIFYSTRING)
861     {
862         size_retstr = (count + 4) * sizeof (WCHAR);
863         retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
864         if (!retstr) return 0;
865         memcpy (retstr, str, size_retstr);
866     }
867     else
868     {
869         size_retstr = 0;
870         retstr = NULL;
871     }
872     p_retstr = retstr;
873
874     do
875     {
876         len = MAX_STATIC_BUFFER;
877         last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
878         strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr, tabwidth, &prefix_offset, &ellip);
879
880         if (flags & DT_CENTER) x = (rect->left + rect->right -
881                                     size.cx) / 2;
882         else if (flags & DT_RIGHT) x = rect->right - size.cx;
883
884         if (flags & DT_SINGLELINE)
885         {
886             if (flags & DT_VCENTER) y = rect->top +
887                 (rect->bottom - rect->top) / 2 - size.cy / 2;
888             else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
889         }
890
891         if (!(flags & DT_CALCRECT))
892         {
893             const WCHAR *str = line;
894             int xseg = x;
895             while (len)
896             {
897                 int len_seg;
898                 SIZE size;
899                 if ((flags & DT_EXPANDTABS))
900                 {
901                     const WCHAR *p;
902                     p = str; while (p < str+len && *p != TAB) p++;
903                     len_seg = p - str;
904                     if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size)) 
905                         return 0;
906                 }
907                 else
908                     len_seg = len;
909                    
910                 if (!ExtTextOutW( hdc, xseg, y,
911                                  ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
912                                  ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
913                                  rect, str, len_seg, NULL ))  return 0;
914                 if (prefix_offset != -1 && prefix_offset < len_seg)
915                 {
916                     TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset);
917                 }
918                 len -= len_seg;
919                 str += len_seg;
920                 if (len)
921                 {
922                     assert ((flags & DT_EXPANDTABS) && *str == TAB);
923                     len--; str++; 
924                     xseg += ((size.cx/tabwidth)+1)*tabwidth;
925                     if (prefix_offset != -1)
926                     {
927                         if (prefix_offset < len_seg)
928                         {
929                             /* We have just drawn an underscore; we ought to
930                              * figure out where the next one is.  I am going
931                              * to leave it for now until I have a better model
932                              * for the line, which will make reprefixing easier.
933                              * This is where ellip would be used.
934                              */
935                             prefix_offset = -1;
936                         }
937                         else
938                             prefix_offset -= len_seg;
939                     }
940                 }
941             }
942         }
943         else if (size.cx > max_width)
944             max_width = size.cx;
945
946         y += lh;
947         if (dtp)
948             dtp->uiLengthDrawn += len;
949     }
950     while (strPtr && !last_line);
951
952     if (flags & DT_CALCRECT)
953     {
954         rect->right = rect->left + max_width;
955         rect->bottom = y;
956         if (dtp)
957             rect->right += lmargin + rmargin;
958     }
959     if (retstr)
960     {
961         memcpy (str, retstr, size_retstr);
962         HeapFree (GetProcessHeap(), 0, retstr);
963     }
964     return y - rect->top;
965 }
966
967 /***********************************************************************
968  *           DrawTextExA    (USER32.@)
969  *
970  * If DT_MODIFYSTRING is specified then there must be room for up to 
971  * 4 extra characters.  We take great care about just how much modified
972  * string we return.
973  */
974 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
975                         LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
976 {
977    WCHAR *wstr;
978    WCHAR *p;
979    INT ret = 0;
980    int i;
981    DWORD wcount;
982    DWORD wmax;
983    DWORD amax;
984
985    if (count == -1) count = strlen(str);
986    if (!count) return 0;
987    wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
988    wmax = wcount;
989    amax = count;
990    if (flags & DT_MODIFYSTRING)
991    {
992         wmax += 4;
993         amax += 4;
994    }
995    wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
996    if (wstr)
997    {
998        MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
999        if (flags & DT_MODIFYSTRING)
1000            for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
1001            /* Initialise the extra characters so that we can see which ones
1002             * change.  U+FFFE is guaranteed to be not a unicode character and
1003             * so will not be generated by DrawTextEx itself.
1004             */
1005        ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
1006        if (flags & DT_MODIFYSTRING)
1007        {
1008             /* Unfortunately the returned string may contain multiple \0s
1009              * and so we need to measure it ourselves.
1010              */
1011             for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
1012             WideCharToMultiByte( CP_ACP, 0, wstr, wcount, str, amax, NULL, NULL );
1013        }
1014        HeapFree(GetProcessHeap(), 0, wstr);
1015    }
1016    return ret;
1017 }
1018
1019 /***********************************************************************
1020  *           DrawTextW    (USER32.@)
1021  */
1022 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
1023 {
1024     DRAWTEXTPARAMS dtp;
1025
1026     memset (&dtp, 0, sizeof(dtp));
1027     if (flags & DT_TABSTOP)
1028     {
1029         dtp.iTabLength = (flags >> 8) && 0xff;
1030         flags &= 0xffff00ff;
1031     }
1032     return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1033 }
1034
1035 /***********************************************************************
1036  *           DrawTextA    (USER32.@)
1037  */
1038 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1039 {
1040     DRAWTEXTPARAMS dtp;
1041
1042     memset (&dtp, 0, sizeof(dtp));
1043     if (flags & DT_TABSTOP)
1044     {
1045         dtp.iTabLength = (flags >> 8) && 0xff;
1046         flags &= 0xffff00ff;
1047     }
1048     return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1049 }
1050
1051 /***********************************************************************
1052  *           DrawText    (USER.85)
1053  */
1054 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
1055 {
1056     INT16 ret;
1057
1058     if (rect)
1059     {
1060         RECT rect32;
1061         CONV_RECT16TO32( rect, &rect32 );
1062         ret = DrawTextA( hdc, str, count, &rect32, flags );
1063         CONV_RECT32TO16( &rect32, rect );
1064     }
1065     else ret = DrawTextA( hdc, str, count, NULL, flags);
1066     return ret;
1067 }
1068
1069
1070 /***********************************************************************
1071  *
1072  *           GrayString functions
1073  */
1074
1075 /* ### start build ### */
1076 extern WORD CALLBACK TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16,WORD,LONG,WORD);
1077 /* ### stop build ### */
1078
1079 struct gray_string_info
1080 {
1081     GRAYSTRINGPROC16 proc;
1082     LPARAM           param;
1083 };
1084
1085 /* callback for 16-bit gray string proc */
1086 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
1087 {
1088     const struct gray_string_info *info = (struct gray_string_info *)param;
1089     return TEXT_CallTo16_word_wlw( info->proc, hdc, info->param, len );
1090 }
1091
1092 /***********************************************************************
1093  *           TEXT_GrayString
1094  *
1095  * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1096  * heap and we can guarantee that the handles fit in an INT16. We have to
1097  * rethink the strategy once the migration to NT handles is complete.
1098  * We are going to get a lot of code-duplication once this migration is
1099  * completed...
1100  * 
1101  */
1102 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1103                             INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
1104 {
1105     HBITMAP hbm, hbmsave;
1106     HBRUSH hbsave;
1107     HFONT hfsave;
1108     HDC memdc;
1109     int slen = len;
1110     BOOL retval = TRUE;
1111     COLORREF fg, bg;
1112
1113     if(!hdc) return FALSE;
1114     if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1115
1116     if(len == 0)
1117     {
1118         if(unicode)
1119             slen = lstrlenW((LPCWSTR)lp);
1120         else if(_32bit)
1121             slen = strlen((LPCSTR)lp);
1122         else
1123             slen = strlen(MapSL(lp));
1124     }
1125
1126     if((cx == 0 || cy == 0) && slen != -1)
1127     {
1128         SIZE s;
1129         if(unicode)
1130             GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
1131         else if(_32bit)
1132             GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
1133         else
1134             GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
1135         if(cx == 0) cx = s.cx;
1136         if(cy == 0) cy = s.cy;
1137     }
1138
1139     hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1140     hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1141     hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1142     PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1143     SelectObject( memdc, hbsave );
1144     SetTextColor(memdc, RGB(255, 255, 255));
1145     SetBkColor(memdc, RGB(0, 0, 0));
1146     hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1147
1148     if(fn)
1149     {
1150         if(_32bit)
1151             retval = fn(memdc, lp, slen);
1152         else
1153             retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
1154     }
1155     else
1156     {
1157         if(unicode)
1158             TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
1159         else if(_32bit)
1160             TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
1161         else
1162             TextOutA(memdc, 0, 0, MapSL(lp), slen);
1163     }
1164
1165     SelectObject(memdc, hfsave);
1166
1167 /*
1168  * Windows doc says that the bitmap isn't grayed when len == -1 and
1169  * the callback function returns FALSE. However, testing this on
1170  * win95 showed otherwise...
1171 */
1172 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1173     if(retval || len != -1)
1174 #endif
1175     {
1176         hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1177         PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1178         SelectObject(memdc, hbsave);
1179     }
1180
1181     if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
1182     fg = SetTextColor(hdc, RGB(0, 0, 0));
1183     bg = SetBkColor(hdc, RGB(255, 255, 255));
1184     BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1185     SetTextColor(hdc, fg);
1186     SetBkColor(hdc, bg);
1187     if(hb) SelectObject(hdc, hbsave);
1188
1189     SelectObject(memdc, hbmsave);
1190     DeleteObject(hbm);
1191     DeleteDC(memdc);
1192     return retval;
1193 }
1194
1195
1196 /***********************************************************************
1197  *           GrayString   (USER.185)
1198  */
1199 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
1200                             LPARAM lParam, INT16 cch, INT16 x, INT16 y,
1201                             INT16 cx, INT16 cy )
1202 {
1203     struct gray_string_info info;
1204
1205     if (!gsprc) return TEXT_GrayString(hdc, hbr, NULL, lParam, cch, x, y, cx, cy, FALSE, FALSE);
1206     info.proc  = gsprc;
1207     info.param = lParam;
1208     return TEXT_GrayString( hdc, hbr, gray_string_callback, (LPARAM)&info,
1209                             cch, x, y, cx, cy, FALSE, FALSE);
1210 }
1211
1212
1213 /***********************************************************************
1214  *           GrayStringA   (USER32.@)
1215  */
1216 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1217                          LPARAM lParam, INT cch, INT x, INT y,
1218                          INT cx, INT cy )
1219 {
1220     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1221                            FALSE, TRUE);
1222 }
1223
1224
1225 /***********************************************************************
1226  *           GrayStringW   (USER32.@)
1227  */
1228 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1229                          LPARAM lParam, INT cch, INT x, INT y,
1230                          INT cx, INT cy )
1231 {
1232     return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1233                            TRUE, TRUE);
1234 }
1235
1236 /***********************************************************************
1237  *
1238  *           TabbedText functions
1239  */
1240
1241 /***********************************************************************
1242  *           TEXT_TabbedTextOut
1243  *
1244  * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1245  * Note: this doesn't work too well for text-alignment modes other
1246  *       than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1247  */
1248 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
1249                                 INT count, INT cTabStops, const INT16 *lpTabPos16,
1250                                 const INT *lpTabPos32, INT nTabOrg,
1251                                 BOOL fDisplayText )
1252 {
1253     INT defWidth;
1254     SIZE extent;
1255     int i, tabPos = x;
1256     int start = x;
1257
1258     extent.cx = 0;
1259     extent.cy = 0;
1260
1261     if (cTabStops == 1)
1262     {
1263         defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
1264         cTabStops = 0;
1265     }
1266     else
1267     {
1268         TEXTMETRICA tm;
1269         GetTextMetricsA( hdc, &tm );
1270         defWidth = 8 * tm.tmAveCharWidth;
1271     }
1272
1273     while (count > 0)
1274     {
1275         for (i = 0; i < count; i++)
1276             if (lpstr[i] == '\t') break;
1277         GetTextExtentPointA( hdc, lpstr, i, &extent );
1278         if (lpTabPos32)
1279         {
1280             while ((cTabStops > 0) &&
1281                    (nTabOrg + *lpTabPos32 <= x + extent.cx))
1282             {
1283                 lpTabPos32++;
1284                 cTabStops--;
1285             }
1286         }
1287         else
1288         {
1289             while ((cTabStops > 0) &&
1290                    (nTabOrg + *lpTabPos16 <= x + extent.cx))
1291             {
1292                 lpTabPos16++;
1293                 cTabStops--;
1294             }
1295         }
1296         if (i == count)
1297             tabPos = x + extent.cx;
1298         else if (cTabStops > 0)
1299             tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
1300         else
1301             tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
1302         if (fDisplayText)
1303         {
1304             RECT r;
1305             r.left   = x;
1306             r.top    = y;
1307             r.right  = tabPos;
1308             r.bottom = y + extent.cy;
1309             ExtTextOutA( hdc, x, y,
1310                            GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1311                            &r, lpstr, i, NULL );
1312         }
1313         x = tabPos;
1314         count -= i+1;
1315         lpstr += i+1;
1316     }
1317     return MAKELONG(tabPos - start, extent.cy);
1318 }
1319
1320
1321 /***********************************************************************
1322  *           TabbedTextOut    (USER.196)
1323  */
1324 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
1325                              INT16 count, INT16 cTabStops,
1326                              const INT16 *lpTabPos, INT16 nTabOrg )
1327 {
1328     TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1329     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1330                                lpTabPos, NULL, nTabOrg, TRUE );
1331 }
1332
1333
1334 /***********************************************************************
1335  *           TabbedTextOutA    (USER32.@)
1336  */
1337 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1338                             INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1339 {
1340     TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1341     return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1342                                NULL, lpTabPos, nTabOrg, TRUE );
1343 }
1344
1345
1346 /***********************************************************************
1347  *           TabbedTextOutW    (USER32.@)
1348  */
1349 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1350                             INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1351 {
1352     LONG ret;
1353     LPSTR p;
1354     INT acount;
1355     UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1356
1357     acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
1358     p = HeapAlloc( GetProcessHeap(), 0, acount );
1359     if(p == NULL) return 0; /* FIXME: is this the correct return on failure */ 
1360     acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
1361     ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
1362     HeapFree( GetProcessHeap(), 0, p );
1363     return ret;
1364 }
1365
1366
1367 /***********************************************************************
1368  *           GetTabbedTextExtent    (USER.197)
1369  */
1370 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
1371                                     INT16 cTabStops, const INT16 *lpTabPos )
1372 {
1373     TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1374     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1375                                lpTabPos, NULL, 0, FALSE );
1376 }
1377
1378
1379 /***********************************************************************
1380  *           GetTabbedTextExtentA    (USER32.@)
1381  */
1382 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1383                                    INT cTabStops, const INT *lpTabPos )
1384 {
1385     TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1386     return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1387                                NULL, lpTabPos, 0, FALSE );
1388 }
1389
1390
1391 /***********************************************************************
1392  *           GetTabbedTextExtentW    (USER32.@)
1393  */
1394 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1395                                    INT cTabStops, const INT *lpTabPos )
1396 {
1397     LONG ret;
1398     LPSTR p;
1399     INT acount;
1400     UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1401
1402     acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
1403     p = HeapAlloc( GetProcessHeap(), 0, acount );
1404     if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
1405     acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
1406     ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
1407     HeapFree( GetProcessHeap(), 0, p );
1408     return ret;
1409 }