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