4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 2002 Bill Medland
8 * 1. DrawText functions
9 * 2. GrayString functions
10 * 3. TabbedText functions
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.
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.
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
32 #include "wine/winuser16.h"
33 #include "wine/unicode.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(text);
42 /*********************************************************************
47 * How many buffers to use
48 * While processing in DrawText there are potentially three different forms
49 * of the text that need to be held. How are they best held?
50 * 1. The original text is needed, of course, to see what to display.
51 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
53 * 3. The buffered text that is about to be displayed e.g. the current line.
54 * Typically this will exclude the ampersands used for prefixing etc.
57 * a. If the buffered text to be displayed includes the ampersands then
58 * we will need special measurement and draw functions that will ignore
59 * the ampersands (e.g. by copying to a buffer without the prefix and
60 * then using the normal forms). This may involve less space but may
61 * require more processing. e.g. since a line containing tabs may
62 * contain several underlined characters either we need to carry around
63 * a list of prefix locations or we may need to locate them several
65 * b. If we actually directly modify the "original text" as we go then we
66 * will need some special "caching" to handle the fact that when we
67 * ellipsify the text the ellipsis may modify the next line of text,
68 * which we have not yet processed. (e.g. ellipsification of a W at the
69 * end of a line will overwrite the W, the \n and the first character of
70 * the next line, and a \0 will overwrite the second. Try it!!)
72 * Option 1. Three separate storages. (To be implemented)
73 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
74 * the edited string in some form, either as the string itself or as some
75 * sort of "edit list" to be applied just before returning.
76 * Use a buffer that holds the ellipsified current line sans ampersands
77 * and accept the need occasionally to recalculate the prefixes (if
78 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
87 #define FORWARD_SLASH '/'
88 #define BACK_SLASH '\\'
90 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
92 typedef struct tag_ellipsis_data
100 /*********************************************************************
101 * TEXT_Ellipsify (static)
103 * Add an ellipsis to the end of the given string whilst ensuring it fits.
105 * If the ellipsis alone doesn't fit then it will be returned anyway.
107 * See Also TEXT_PathEllipsify
110 * hdc [in] The handle to the DC that defines the font.
111 * str [in/out] The string that needs to be modified.
112 * max_str [in] The dimension of str (number of WCHAR).
113 * len_str [in/out] The number of characters in str
114 * width [in] The maximum width permitted (in logical coordinates)
115 * size [out] The dimensions of the text
116 * modstr [out] The modified form of the string, to be returned to the
117 * calling program. It is assumed that the caller has
118 * made sufficient space available so we don't need to
119 * know the size of the space. This pointer may be NULL if
120 * the modified string is not required.
121 * len_before [out] The number of characters before the ellipsis.
122 * len_ellip [out] The number of characters in the ellipsis.
124 * See for example Microsoft article Q249678.
126 * For now we will simply use three dots rather than worrying about whether
127 * the font contains an explicit ellipsis character.
129 static void TEXT_Ellipsify (HDC hdc, WCHAR *str, unsigned int max_len,
130 unsigned int *len_str, int width, SIZE *size,
132 int *len_before, int *len_ellip)
134 unsigned int len_ellipsis;
136 len_ellipsis = strlenW (ELLIPSISW);
137 if (len_ellipsis > max_len) len_ellipsis = max_len;
138 if (*len_str > max_len - len_ellipsis)
139 *len_str = max_len - len_ellipsis;
143 strncpyW (str + *len_str, ELLIPSISW, len_ellipsis);
145 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
146 NULL, NULL, size)) break;
148 if (!*len_str || size->cx <= width) break;
152 *len_ellip = len_ellipsis;
153 *len_before = *len_str;
154 *len_str += len_ellipsis;
158 strncpyW (modstr, str, *len_str);
159 *(str+*len_str) = '\0';
163 /*********************************************************************
164 * TEXT_PathEllipsify (static)
166 * Add an ellipsis to the provided string in order to make it fit within
167 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
170 * See Also TEXT_Ellipsify
173 * hdc [in] The handle to the DC that defines the font.
174 * str [in/out] The string that needs to be modified
175 * max_str [in] The dimension of str (number of WCHAR).
176 * len_str [in/out] The number of characters in str
177 * width [in] The maximum width permitted (in logical coordinates)
178 * size [out] The dimensions of the text
179 * modstr [out] The modified form of the string, to be returned to the
180 * calling program. It is assumed that the caller has
181 * made sufficient space available so we don't need to
182 * know the size of the space. This pointer may be NULL if
183 * the modified string is not required.
184 * pellip [out] The ellipsification results
186 * For now we will simply use three dots rather than worrying about whether
187 * the font contains an explicit ellipsis character.
189 * The following applies, I think to Win95. We will need to extend it for
190 * Win98 which can have both path and end ellipsis at the same time (e.g.
191 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
193 * The resulting string consists of as much as possible of the following:
194 * 1. The ellipsis itself
195 * 2. The last \ or / of the string (if any)
196 * 3. Everything after the last \ or / of the string (if any) or the whole
197 * string if there is no / or \. I believe that under Win95 this would
198 * include everything even though some might be clipped off the end whereas
199 * under Win98 that might be ellipsified too.
200 * Yet to be investigated is whether this would include wordbreaking if the
201 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
202 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
203 * broken within words).
204 * 4. All the stuff before the / or \, which is placed before the ellipsis.
206 static void TEXT_PathEllipsify (HDC hdc, WCHAR *str, unsigned int max_len,
207 unsigned int *len_str, int width, SIZE *size,
208 WCHAR *modstr, ellipsis_data *pellip)
213 WCHAR *lastBkSlash, *lastFwdSlash, *lastSlash;
215 len_ellipsis = strlenW (ELLIPSISW);
216 if (!max_len) return;
217 if (len_ellipsis >= max_len) len_ellipsis = max_len - 1;
218 if (*len_str + len_ellipsis >= max_len)
219 *len_str = max_len - len_ellipsis-1;
220 /* Hopefully this will never happen, otherwise it would probably lose
221 * the wrong character
223 str[*len_str] = '\0'; /* to simplify things */
225 lastBkSlash = strrchrW (str, BACK_SLASH);
226 lastFwdSlash = strrchrW (str, FORWARD_SLASH);
227 lastSlash = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
228 if (!lastSlash) lastSlash = str;
229 len_trailing = *len_str - (lastSlash - str);
231 /* overlap-safe movement to the right */
232 memmove (lastSlash+len_ellipsis, lastSlash, len_trailing * sizeof(WCHAR));
233 strncpyW (lastSlash, ELLIPSISW, len_ellipsis);
234 len_trailing += len_ellipsis;
235 /* From this point on lastSlash actually points to the ellipsis in front
236 * of the last slash and len_trailing includes the ellipsis
242 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
243 NULL, NULL, size)) break;
245 if (lastSlash == str || size->cx <= width) break;
247 /* overlap-safe movement to the left */
248 memmove (lastSlash-1, lastSlash, len_trailing * sizeof(WCHAR));
255 pellip->before = lastSlash-str;
256 pellip->len = len_ellipsis;
257 pellip->under = len_under;
258 pellip->after = len_trailing - len_ellipsis;
259 *len_str += len_ellipsis;
263 strncpyW (modstr, str, *len_str);
264 *(str+*len_str) = '\0';
268 /*********************************************************************
269 * TEXT_WordBreak (static)
271 * Perform wordbreak processing on the given string
273 * Assumes that DT_WORDBREAK has been specified and not all the characters
274 * fit. Note that this function should even be called when the first character
275 * that doesn't fit is known to be a space or tab, so that it can swallow them.
277 * Note that the Windows processing has some strange properties.
278 * 1. If the text is left-justified and there is room for some of the spaces
279 * that follow the last word on the line then those that fit are included on
281 * 2. If the text is centred or right-justified and there is room for some of
282 * the spaces that follow the last word on the line then all but one of those
283 * that fit are included on the line.
284 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
285 * character of a new line it will be skipped.
288 * hdc [in] The handle to the DC that defines the font.
289 * str [in/out] The string that needs to be broken.
290 * max_str [in] The dimension of str (number of WCHAR).
291 * len_str [in/out] The number of characters in str
292 * width [in] The maximum width permitted
293 * format [in] The format flags in effect
294 * chars_fit [in] The maximum number of characters of str that are already
295 * known to fit; chars_fit+1 is known not to fit.
296 * chars_used [out] The number of characters of str that have been "used" and
297 * do not need to be included in later text. For example this will
298 * include any spaces that have been discarded from the start of
300 * size [out] The size of the returned text in logical coordinates
302 * Pedantic assumption - Assumes that the text length is monotonically
303 * increasing with number of characters (i.e. no weird kernings)
307 * Work back from the last character that did fit to either a space or the last
308 * character of a word, whichever is met first.
309 * If there was one or the first character didn't fit then
310 * If the text is centred or right justified and that one character was a
311 * space then break the line before that character
312 * Otherwise break the line after that character
313 * and if the next character is a space then discard it.
314 * Suppose there was none (and the first character did fit).
315 * If Break Within Word is permitted
316 * break the word after the last character that fits (there must be
317 * at least one; none is caught earlier).
319 * discard any trailing space.
320 * include the whole word; it may be ellipsified later
322 * Break Within Word is permitted under a set of circumstances that are not
323 * totally clear yet. Currently our best guess is:
324 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
325 * DT_PATH_ELLIPSIS is
328 static void TEXT_WordBreak (HDC hdc, WCHAR *str, unsigned int max_str,
329 unsigned int *len_str,
330 int width, int format, unsigned int chars_fit,
331 unsigned int *chars_used, SIZE *size)
335 assert (format & DT_WORDBREAK);
336 assert (chars_fit < *len_str);
338 /* Work back from the last character that did fit to either a space or the
339 * last character of a word, whichever is met first.
341 p = str + chars_fit; /* The character that doesn't fit */
344 ; /* we pretend that it fits anyway */
345 else if (*p == SPACE) /* chars_fit < *len_str so this is valid */
346 p--; /* the word just fitted */
349 while (p > str && *(--p) != SPACE)
351 word_fits = (p != str || *p == SPACE);
353 /* If there was one or the first character didn't fit then */
356 /* break the line before/after that character */
357 if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
360 /* and if the next character is a space then discard it. */
361 *chars_used = *len_str;
365 /* Suppose there was none. */
368 if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
371 /* break the word after the last character that fits (there must be
372 * at least one; none is caught earlier).
374 *len_str = chars_fit;
375 *chars_used = chars_fit;
377 /* FIXME - possible error. Since the next character is now removed
378 * this could make the text longer so that it no longer fits, and
379 * so we need a loop to test and shrink.
385 /* discard any trailing space. */
386 const WCHAR *e = str + *len_str;
388 while (p < e && *p != SPACE)
390 *chars_used = p - str;
391 if (p < e) /* i.e. loop failed because *p == SPACE */
394 /* include the whole word; it may be ellipsified later */
396 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
397 * so that it will be too long
401 /* Remeasure the string */
402 GetTextExtentExPointW (hdc, str, *len_str, 0, NULL, NULL, size);
405 /*********************************************************************
408 * Skip over the given number of characters, bearing in mind prefix
409 * substitution and the fact that a character may take more than one
410 * WCHAR (Unicode surrogates are two words long) (and there may have been
414 * new_count [out] The updated count
415 * new_str [out] The updated pointer
416 * start_count [in] The count of remaining characters corresponding to the
417 * start of the string
418 * start_str [in] The starting point of the string
419 * max [in] The number of characters actually in this segment of the
420 * string (the & counts)
421 * n [in] The number of characters to skip (if prefix then
423 * prefix [in] Apply prefix substitution
429 * There must be at least n characters in the string
430 * We need max because the "line" may have ended with a & followed by a tab
431 * or newline etc. which we don't want to swallow
434 static void TEXT_SkipChars (int *new_count, const WCHAR **new_str,
435 int start_count, const WCHAR *start_str,
436 int max, int n, int prefix)
438 /* This is specific to wide characters, MSDN doesn't say anything much
439 * about Unicode surrogates yet and it isn't clear if _wcsinc will
440 * correctly handle them so we'll just do this the easy way for now
445 const WCHAR *str_on_entry = start_str;
449 if (*start_str++ == PREFIX && max--)
452 start_count -= (start_str - str_on_entry);
459 *new_str = start_str;
460 *new_count = start_count;
463 /*********************************************************************
466 * Reanalyse the text to find the prefixed character. This is called when
467 * wordbreaking or ellipsification has shortened the string such that the
468 * previously noted prefixed character is no longer visible.
471 * str [in] The original string segment (including all characters)
472 * ns [in] The number of characters in str (including prefixes)
473 * pe [in] The ellipsification data
476 * The prefix offset within the new string segment (the one that contains the
477 * ellipses and does not contain the prefix characters) (-1 if none)
480 static int TEXT_Reprefix (const WCHAR *str, unsigned int ns,
481 const ellipsis_data *pe)
485 unsigned int n = pe->before + pe->under + pe->after;
491 /* Reached the path ellipsis; jump over it */
492 if (ns < pe->under) break;
496 if (!pe->after) break; /* Nothing after the path ellipsis */
500 if (*str++ == PREFIX)
504 result = (i < pe->before || pe->under == 0) ? i : i - pe->under + pe->len;
505 /* pe->len may be non-zero while pe_under is zero */
515 /*********************************************************************
516 * Returns true if and only if the remainder of the line is a single
517 * newline representation or nothing
520 static int remainder_is_none_or_newline (int num_chars, const WCHAR *str)
522 if (!num_chars) return TRUE;
523 if (*str != LF && *str != CR) return FALSE;
524 if (!--num_chars) return TRUE;
525 if (*str == *(str+1)) return FALSE;
527 if (*str != CR && *str != LF) return FALSE;
528 if (--num_chars) return FALSE;
532 /*********************************************************************
533 * Return next line of text from a string.
535 * hdc - handle to DC.
536 * str - string to parse into lines.
537 * count - length of str.
538 * dest - destination in which to return line.
539 * len - dest buffer size in chars on input, copied length into dest on output.
540 * width - maximum width of line in pixels.
541 * format - format type passed to DrawText.
542 * retsize - returned size of the line in pixels.
543 * last_line - TRUE if is the last line that will be processed
544 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
545 * the return string is built.
546 * tabwidth - The width of a tab in logical coordinates
547 * pprefix_offset - Here is where we return the offset within dest of the first
548 * prefixed (underlined) character. -1 is returned if there
549 * are none. Note that there may be more; the calling code
550 * will need to use TEXT_Reprefix to find any later ones.
551 * pellip - Here is where we return the information about any ellipsification
552 * that was carried out. Note that if tabs are being expanded then
553 * this data will correspond to the last text segment actually
554 * returned in dest; by definition there would not have been any
555 * ellipsification in earlier text segments of the line.
557 * Returns pointer to next char in str after end of the line
558 * or NULL if end of str reached.
560 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
561 WCHAR *dest, int *len, int width, DWORD format,
562 SIZE *retsize, int last_line, WCHAR **p_retstr,
563 int tabwidth, int *pprefix_offset,
564 ellipsis_data *pellip)
570 int seg_i, seg_count, seg_j;
578 /* For each text segment in the line */
584 /* Skip any leading tabs */
586 if (str[i] == TAB && (format & DT_EXPANDTABS))
588 plen = ((plen/tabwidth)+1)*tabwidth;
589 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
590 while (*count && str[i] == TAB)
593 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
598 /* Now copy as far as the next tab or cr/lf or eos */
605 (str[i] != TAB || !(format & DT_EXPANDTABS)) &&
606 ((str[i] != CR && str[i] != LF) || (format & DT_SINGLELINE)))
608 if (str[i] == PREFIX && !(format & DT_NOPREFIX) && *count > 1)
610 (*count)--, i++; /* Throw away the prefix itself */
611 if (str[i] == PREFIX)
613 /* Swallow it before we see it again */
614 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
616 else if (*pprefix_offset == -1 || *pprefix_offset >= seg_j)
620 /* else the previous prefix was in an earlier segment of the
621 * line; we will leave it to the drawing code to catch this
627 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
632 /* Measure the whole text segment and possibly WordBreak and
636 j_in_seg = j - seg_j;
637 max_seg_width = width - plen;
638 GetTextExtentExPointW (hdc, dest + seg_j, j_in_seg, max_seg_width, &num_fit, NULL, &size);
640 /* The Microsoft handling of various combinations of formats is weird.
641 * The following may very easily be incorrect if several formats are
642 * combined, and may differ between versions (to say nothing of the
643 * several bugs in the Microsoft versions).
646 line_fits = (num_fit >= j_in_seg);
647 if (!line_fits && (format & DT_WORDBREAK))
651 TEXT_WordBreak (hdc, dest+seg_j, maxl-seg_j, &j_in_seg,
652 max_seg_width, format, num_fit, &chars_used, &size);
653 line_fits = (size.cx <= max_seg_width);
654 /* and correct the counts */
655 TEXT_SkipChars (count, &s, seg_count, str+seg_i, i-seg_i,
656 chars_used, !(format & DT_NOPREFIX));
660 pellip->before = j_in_seg;
665 if (!line_fits && (format & DT_PATH_ELLIPSIS))
667 TEXT_PathEllipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
668 max_seg_width, &size, *p_retstr, pellip);
669 line_fits = (size.cx <= max_seg_width);
672 /* NB we may end up ellipsifying a word-broken or path_ellipsified
674 if ((!line_fits && (format & DT_WORD_ELLIPSIS)) ||
675 ((format & DT_END_ELLIPSIS) &&
676 ((last_line && *count) ||
677 (remainder_is_none_or_newline (*count, &str[i]) && !line_fits))))
679 int before, len_ellipsis;
680 TEXT_Ellipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
681 max_seg_width, &size, *p_retstr, &before, &len_ellipsis);
682 if (before > pellip->before)
684 /* We must have done a path ellipsis too */
685 pellip->after = before - pellip->before - pellip->len;
686 /* Leave the len as the length of the first ellipsis */
690 /* If we are here after a path ellipsification it must be
691 * because even the ellipsis itself didn't fit.
693 assert (pellip->under == 0 && pellip->after == 0);
694 pellip->before = before;
695 pellip->len = len_ellipsis;
696 /* pellip->after remains as zero as does
700 line_fits = (size.cx <= max_seg_width);
703 /* As an optimisation if we have ellipsified and we are expanding
704 * tabs and we haven't reached the end of the line we can skip to it
705 * now rather than going around the loop again.
707 if ((format & DT_EXPANDTABS) && ellipsified)
709 if (format & DT_SINGLELINE)
713 while ((*count) && str[i] != CR && str[i] != LF)
720 j = seg_j + j_in_seg;
721 if (*pprefix_offset >= seg_j + pellip->before)
723 *pprefix_offset = TEXT_Reprefix (str + seg_i, i - seg_i, pellip);
724 if (*pprefix_offset != -1)
725 *pprefix_offset += seg_j;
729 if (size.cy > retsize->cy)
730 retsize->cy = size.cy;
736 else if (str[i] == CR || str[i] == LF)
739 if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
745 /* else it was a Tab and we go around again */
757 /***********************************************************************
758 * TEXT_DrawUnderscore
760 * Draw the underline under the prefixed character
763 * hdc [in] The handle of the DC for drawing
764 * x [in] The x location of the line segment (logical coordinates)
765 * y [in] The y location of where the underscore should appear
766 * (logical coordinates)
767 * str [in] The text of the line segment
768 * offset [in] The offset of the underscored character within str
771 static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset)
779 GetTextExtentPointW (hdc, str, offset, &size);
780 prefix_x = x + size.cx;
781 GetTextExtentPointW (hdc, str, offset+1, &size);
782 prefix_end = x + size.cx - 1;
783 /* The above method may eventually be slightly wrong due to kerning etc. */
785 hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
786 oldPen = SelectObject (hdc, hpen);
787 MoveToEx (hdc, prefix_x, y, NULL);
788 LineTo (hdc, prefix_end, y);
789 SelectObject (hdc, oldPen);
793 /***********************************************************************
794 * DrawTextExW (USER32.@)
796 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
797 * is not quite complete, especially with regard to \0. We will assume that
798 * the returned string could have a length of up to i_count+3 and also have
799 * a trailing \0 (which would be 4 more than a not-null-terminated string but
800 * 3 more than a null-terminated string). If this is not so then increase
801 * the allowance in DrawTextExA.
803 #define MAX_STATIC_BUFFER 1024
804 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
805 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
809 WCHAR *retstr, *p_retstr;
811 static WCHAR line[MAX_STATIC_BUFFER];
812 int len, lh, count=i_count;
814 int lmargin = 0, rmargin = 0;
815 int x = rect->left, y = rect->top;
816 int width = rect->right - rect->left;
819 int tabwidth /* to keep gcc happy */ = 0;
823 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
824 rect->left, rect->top, rect->right, rect->bottom);
826 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
827 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
830 if (count == -1) count = strlenW(str);
831 if (count == 0) return 0;
834 if (flags & DT_SINGLELINE)
835 flags &= ~DT_WORDBREAK;
837 GetTextMetricsW(hdc, &tm);
838 if (flags & DT_EXTERNALLEADING)
839 lh = tm.tmHeight + tm.tmExternalLeading;
845 lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
846 rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
847 if (!(flags & (DT_CENTER | DT_RIGHT)))
849 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
852 if (flags & DT_EXPANDTABS)
854 int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
855 tabwidth = tm.tmAveCharWidth * tabstop;
858 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
860 if (flags & DT_MODIFYSTRING)
862 size_retstr = (count + 4) * sizeof (WCHAR);
863 retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
864 if (!retstr) return 0;
865 memcpy (retstr, str, size_retstr);
876 len = MAX_STATIC_BUFFER;
877 last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
878 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr, tabwidth, &prefix_offset, &ellip);
880 if (flags & DT_CENTER) x = (rect->left + rect->right -
882 else if (flags & DT_RIGHT) x = rect->right - size.cx;
884 if (flags & DT_SINGLELINE)
886 if (flags & DT_VCENTER) y = rect->top +
887 (rect->bottom - rect->top) / 2 - size.cy / 2;
888 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
891 if (!(flags & DT_CALCRECT))
893 const WCHAR *str = line;
899 if ((flags & DT_EXPANDTABS))
902 p = str; while (p < str+len && *p != TAB) p++;
904 if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size))
910 if (!ExtTextOutW( hdc, xseg, y,
911 ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
912 ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
913 rect, str, len_seg, NULL )) return 0;
914 if (prefix_offset != -1 && prefix_offset < len_seg)
916 TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset);
922 assert ((flags & DT_EXPANDTABS) && *str == TAB);
924 xseg += ((size.cx/tabwidth)+1)*tabwidth;
925 if (prefix_offset != -1)
927 if (prefix_offset < len_seg)
929 /* We have just drawn an underscore; we ought to
930 * figure out where the next one is. I am going
931 * to leave it for now until I have a better model
932 * for the line, which will make reprefixing easier.
933 * This is where ellip would be used.
938 prefix_offset -= len_seg;
943 else if (size.cx > max_width)
948 dtp->uiLengthDrawn += len;
950 while (strPtr && !last_line);
952 if (flags & DT_CALCRECT)
954 rect->right = rect->left + max_width;
957 rect->right += lmargin + rmargin;
961 memcpy (str, retstr, size_retstr);
962 HeapFree (GetProcessHeap(), 0, retstr);
964 return y - rect->top;
967 /***********************************************************************
968 * DrawTextExA (USER32.@)
970 * If DT_MODIFYSTRING is specified then there must be room for up to
971 * 4 extra characters. We take great care about just how much modified
974 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
975 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
985 if (count == -1) count = strlen(str);
986 if (!count) return 0;
987 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
990 if (flags & DT_MODIFYSTRING)
995 wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
998 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
999 if (flags & DT_MODIFYSTRING)
1000 for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
1001 /* Initialise the extra characters so that we can see which ones
1002 * change. U+FFFE is guaranteed to be not a unicode character and
1003 * so will not be generated by DrawTextEx itself.
1005 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
1006 if (flags & DT_MODIFYSTRING)
1008 /* Unfortunately the returned string may contain multiple \0s
1009 * and so we need to measure it ourselves.
1011 for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
1012 WideCharToMultiByte( CP_ACP, 0, wstr, wcount, str, amax, NULL, NULL );
1014 HeapFree(GetProcessHeap(), 0, wstr);
1019 /***********************************************************************
1020 * DrawTextW (USER32.@)
1022 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
1026 memset (&dtp, 0, sizeof(dtp));
1027 if (flags & DT_TABSTOP)
1029 dtp.iTabLength = (flags >> 8) && 0xff;
1030 flags &= 0xffff00ff;
1032 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1035 /***********************************************************************
1036 * DrawTextA (USER32.@)
1038 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1042 memset (&dtp, 0, sizeof(dtp));
1043 if (flags & DT_TABSTOP)
1045 dtp.iTabLength = (flags >> 8) && 0xff;
1046 flags &= 0xffff00ff;
1048 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1051 /***********************************************************************
1052 * DrawText (USER.85)
1054 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
1061 CONV_RECT16TO32( rect, &rect32 );
1062 ret = DrawTextA( hdc, str, count, &rect32, flags );
1063 CONV_RECT32TO16( &rect32, rect );
1065 else ret = DrawTextA( hdc, str, count, NULL, flags);
1070 /***********************************************************************
1072 * GrayString functions
1075 /* ### start build ### */
1076 extern WORD CALLBACK TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16,WORD,LONG,WORD);
1077 /* ### stop build ### */
1079 struct gray_string_info
1081 GRAYSTRINGPROC16 proc;
1085 /* callback for 16-bit gray string proc */
1086 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
1088 const struct gray_string_info *info = (struct gray_string_info *)param;
1089 return TEXT_CallTo16_word_wlw( info->proc, hdc, info->param, len );
1092 /***********************************************************************
1095 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1096 * heap and we can guarantee that the handles fit in an INT16. We have to
1097 * rethink the strategy once the migration to NT handles is complete.
1098 * We are going to get a lot of code-duplication once this migration is
1102 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1103 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
1105 HBITMAP hbm, hbmsave;
1113 if(!hdc) return FALSE;
1114 if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1119 slen = lstrlenW((LPCWSTR)lp);
1121 slen = strlen((LPCSTR)lp);
1123 slen = strlen(MapSL(lp));
1126 if((cx == 0 || cy == 0) && slen != -1)
1130 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
1132 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
1134 GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
1135 if(cx == 0) cx = s.cx;
1136 if(cy == 0) cy = s.cy;
1139 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1140 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1141 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1142 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1143 SelectObject( memdc, hbsave );
1144 SetTextColor(memdc, RGB(255, 255, 255));
1145 SetBkColor(memdc, RGB(0, 0, 0));
1146 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1151 retval = fn(memdc, lp, slen);
1153 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
1158 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
1160 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
1162 TextOutA(memdc, 0, 0, MapSL(lp), slen);
1165 SelectObject(memdc, hfsave);
1168 * Windows doc says that the bitmap isn't grayed when len == -1 and
1169 * the callback function returns FALSE. However, testing this on
1170 * win95 showed otherwise...
1172 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1173 if(retval || len != -1)
1176 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1177 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1178 SelectObject(memdc, hbsave);
1181 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
1182 fg = SetTextColor(hdc, RGB(0, 0, 0));
1183 bg = SetBkColor(hdc, RGB(255, 255, 255));
1184 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1185 SetTextColor(hdc, fg);
1186 SetBkColor(hdc, bg);
1187 if(hb) SelectObject(hdc, hbsave);
1189 SelectObject(memdc, hbmsave);
1196 /***********************************************************************
1197 * GrayString (USER.185)
1199 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
1200 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
1201 INT16 cx, INT16 cy )
1203 struct gray_string_info info;
1205 if (!gsprc) return TEXT_GrayString(hdc, hbr, NULL, lParam, cch, x, y, cx, cy, FALSE, FALSE);
1207 info.param = lParam;
1208 return TEXT_GrayString( hdc, hbr, gray_string_callback, (LPARAM)&info,
1209 cch, x, y, cx, cy, FALSE, FALSE);
1213 /***********************************************************************
1214 * GrayStringA (USER32.@)
1216 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1217 LPARAM lParam, INT cch, INT x, INT y,
1220 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1225 /***********************************************************************
1226 * GrayStringW (USER32.@)
1228 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1229 LPARAM lParam, INT cch, INT x, INT y,
1232 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1236 /***********************************************************************
1238 * TabbedText functions
1241 /***********************************************************************
1242 * TEXT_TabbedTextOut
1244 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1245 * Note: this doesn't work too well for text-alignment modes other
1246 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1248 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
1249 INT count, INT cTabStops, const INT16 *lpTabPos16,
1250 const INT *lpTabPos32, INT nTabOrg,
1263 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
1269 GetTextMetricsA( hdc, &tm );
1270 defWidth = 8 * tm.tmAveCharWidth;
1275 for (i = 0; i < count; i++)
1276 if (lpstr[i] == '\t') break;
1277 GetTextExtentPointA( hdc, lpstr, i, &extent );
1280 while ((cTabStops > 0) &&
1281 (nTabOrg + *lpTabPos32 <= x + extent.cx))
1289 while ((cTabStops > 0) &&
1290 (nTabOrg + *lpTabPos16 <= x + extent.cx))
1297 tabPos = x + extent.cx;
1298 else if (cTabStops > 0)
1299 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
1301 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
1308 r.bottom = y + extent.cy;
1309 ExtTextOutA( hdc, x, y,
1310 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1311 &r, lpstr, i, NULL );
1317 return MAKELONG(tabPos - start, extent.cy);
1321 /***********************************************************************
1322 * TabbedTextOut (USER.196)
1324 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
1325 INT16 count, INT16 cTabStops,
1326 const INT16 *lpTabPos, INT16 nTabOrg )
1328 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1329 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1330 lpTabPos, NULL, nTabOrg, TRUE );
1334 /***********************************************************************
1335 * TabbedTextOutA (USER32.@)
1337 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1338 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1340 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1341 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1342 NULL, lpTabPos, nTabOrg, TRUE );
1346 /***********************************************************************
1347 * TabbedTextOutW (USER32.@)
1349 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1350 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1355 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1357 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
1358 p = HeapAlloc( GetProcessHeap(), 0, acount );
1359 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
1360 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
1361 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
1362 HeapFree( GetProcessHeap(), 0, p );
1367 /***********************************************************************
1368 * GetTabbedTextExtent (USER.197)
1370 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
1371 INT16 cTabStops, const INT16 *lpTabPos )
1373 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1374 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1375 lpTabPos, NULL, 0, FALSE );
1379 /***********************************************************************
1380 * GetTabbedTextExtentA (USER32.@)
1382 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1383 INT cTabStops, const INT *lpTabPos )
1385 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1386 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1387 NULL, lpTabPos, 0, FALSE );
1391 /***********************************************************************
1392 * GetTabbedTextExtentW (USER32.@)
1394 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1395 INT cTabStops, const INT *lpTabPos )
1400 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1402 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
1403 p = HeapAlloc( GetProcessHeap(), 0, acount );
1404 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
1405 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
1406 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
1407 HeapFree( GetProcessHeap(), 0, p );