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