4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 2002 Bill Medland
8 * 1. DrawText functions
9 * 2. GrayString functions
10 * 3. TabbedText functions
18 #include "wine/winuser16.h"
19 #include "wine/unicode.h"
24 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(text);
28 /*********************************************************************
33 * How many buffers to use
34 * While processing in DrawText there are potentially three different forms
35 * of the text that need to be held. How are they best held?
36 * 1. The original text is needed, of course, to see what to display.
37 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
39 * 3. The buffered text that is about to be displayed e.g. the current line.
40 * Typically this will exclude the ampersands used for prefixing etc.
43 * a. If the buffered text to be displayed includes the ampersands then
44 * we will need special measurement and draw functions that will ignore
45 * the ampersands (e.g. by copying to a buffer without the prefix and
46 * then using the normal forms). This may involve less space but may
47 * require more processing. e.g. since a line containing tabs may
48 * contain several underlined characters either we need to carry around
49 * a list of prefix locations or we may need to locate them several
51 * b. If we actually directly modify the "original text" as we go then we
52 * will need some special "caching" to handle the fact that when we
53 * ellipsify the text the ellipsis may modify the next line of text,
54 * which we have not yet processed. (e.g. ellipsification of a W at the
55 * end of a line will overwrite the W, the \n and the first character of
56 * the next line, and a \0 will overwrite the second. Try it!!)
58 * Option 1. Three separate storages. (To be implemented)
59 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
60 * the edited string in some form, either as the string itself or as some
61 * sort of "edit list" to be applied just before returning.
62 * Use a buffer that holds the ellipsified current line sans ampersands
63 * and accept the need occasionally to recalculate the prefixes (if
64 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
73 #define FORWARD_SLASH '/'
74 #define BACK_SLASH '\\'
76 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
78 /* These will have to go into a structure to be passed around rather than
79 * sitting in static memory
82 static int prefix_offset;
83 static int len_before_ellipsis, len_ellipsis, len_under_ellipsis,
86 /*********************************************************************
87 * TEXT_Ellipsify (static)
89 * Add an ellipsis to the end of the given string whilst ensuring it fits.
91 * If the ellipsis alone doesn't fit then it will be returned anyway.
93 * See Also TEXT_PathEllipsify
96 * hdc [in] The handle to the DC that defines the font.
97 * str [in/out] The string that needs to be modified.
98 * max_str [in] The dimension of str (number of WCHAR).
99 * len_str [in/out] The number of characters in str
100 * width [in] The maximum width permitted (in logical coordinates)
101 * size [out] The dimensions of the text
102 * modstr [out] The modified form of the string, to be returned to the
103 * calling program. It is assumed that the caller has
104 * made sufficient space available so we don't need to
105 * know the size of the space. This pointer may be NULL if
106 * the modified string is not required.
107 * len_before [out] The number of characters before the ellipsis.
108 * len_ellip [out] The number of characters in the ellipsis.
110 * See for example Microsoft article Q249678.
112 * For now we will simply use three dots rather than worrying about whether
113 * the font contains an explicit ellipsis character.
115 static void TEXT_Ellipsify (HDC hdc, WCHAR *str, unsigned int max_len,
116 unsigned int *len_str, int width, SIZE *size,
118 int *len_before, int *len_ellip)
120 unsigned int len_ellipsis;
122 len_ellipsis = strlenW (ELLIPSISW);
123 if (len_ellipsis > max_len) len_ellipsis = max_len;
124 if (*len_str > max_len - len_ellipsis)
125 *len_str = max_len - len_ellipsis;
129 strncpyW (str + *len_str, ELLIPSISW, len_ellipsis);
131 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
132 NULL, NULL, size)) break;
134 if (!*len_str || size->cx <= width) break;
138 *len_ellip = len_ellipsis;
139 *len_before = *len_str;
140 *len_str += len_ellipsis;
144 strncpyW (modstr, str, *len_str);
145 *(str+*len_str) = '\0';
149 /*********************************************************************
150 * TEXT_PathEllipsify (static)
152 * Add an ellipsis to the provided string in order to make it fit within
153 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
156 * See Also TEXT_Ellipsify
159 * hdc [in] The handle to the DC that defines the font.
160 * str [in/out] The string that needs to be modified
161 * max_str [in] The dimension of str (number of WCHAR).
162 * len_str [in/out] The number of characters in str
163 * width [in] The maximum width permitted (in logical coordinates)
164 * size [out] The dimensions of the text
165 * modstr [out] The modified form of the string, to be returned to the
166 * calling program. It is assumed that the caller has
167 * made sufficient space available so we don't need to
168 * know the size of the space. This pointer may be NULL if
169 * the modified string is not required.
170 * len_before [out] The number of characters before the ellipsis.
171 * len_ellip [out] The number of characters in the ellipsis.
172 * len_under [out] The number of characters replaced by the ellipsis.
173 * len_after [out] The number of characters after the ellipsis.
175 * For now we will simply use three dots rather than worrying about whether
176 * the font contains an explicit ellipsis character.
178 * The following applies, I think to Win95. We will need to extend it for
179 * Win98 which can have both path and end ellipsis at the same time (e.g.
180 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
182 * The resulting string consists of as much as possible of the following:
183 * 1. The ellipsis itself
184 * 2. The last \ or / of the string (if any)
185 * 3. Everything after the last \ or / of the string (if any) or the whole
186 * string if there is no / or \. I believe that under Win95 this would
187 * include everything even though some might be clipped off the end whereas
188 * under Win98 that might be ellipsified too. (Not yet implemented). Yet
189 * to be investigated is whether this would include wordbreaking if the
190 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
191 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
192 * broken within words).
193 * 4. All the stuff before the / or \, which is placed before the ellipsis.
195 static void TEXT_PathEllipsify (HDC hdc, WCHAR *str, unsigned int max_len,
196 unsigned int *len_str, int width, SIZE *size,
198 int *len_before, int *len_ellip, int *len_under,
203 WCHAR *lastBkSlash, *lastFwdSlash, *lastSlash;
205 len_ellipsis = strlenW (ELLIPSISW);
206 if (!max_len) return;
207 if (len_ellipsis >= max_len) len_ellipsis = max_len - 1;
208 if (*len_str + len_ellipsis >= max_len)
209 *len_str = max_len - len_ellipsis-1;
210 /* Hopefully this will never happen, otherwise it would probably lose
211 * the wrong character
213 str[*len_str] = '\0'; /* to simplify things */
215 lastBkSlash = strrchrW (str, BACK_SLASH);
216 lastFwdSlash = strrchrW (str, FORWARD_SLASH);
217 lastSlash = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
218 if (!lastSlash) lastSlash = str;
219 len_trailing = *len_str - (lastSlash - str);
221 /* overlap-safe movement to the right */
222 memmove (lastSlash+len_ellipsis, lastSlash, len_trailing * sizeof(WCHAR));
223 strncpyW (lastSlash, ELLIPSISW, len_ellipsis);
224 len_trailing += len_ellipsis;
225 /* From this point on lastSlash actually points to the ellipsis in front
226 * of the last slash and len_trailing includes the ellipsis
232 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
233 NULL, NULL, size)) break;
235 if (lastSlash == str || size->cx <= width) break;
237 /* overlap-safe movement to the left */
238 memmove (lastSlash-1, lastSlash, len_trailing * sizeof(WCHAR));
245 *len_before = lastSlash-str;
246 *len_ellip = len_ellipsis;
247 *len_after = len_trailing - len_ellipsis;
248 *len_str += len_ellipsis;
252 strncpyW (modstr, str, *len_str);
253 *(str+*len_str) = '\0';
257 /*********************************************************************
258 * TEXT_WordBreak (static)
260 * Perform wordbreak processing on the given string
262 * Assumes that DT_WORDBREAK has been specified and not all the characters
263 * fit. Note that this function should even be called when the first character
264 * that doesn't fit is known to be a space or tab, so that it can swallow them.
266 * Note that the Windows processing has some strange properties.
267 * 1. If the text is left-justified and there is room for some of the spaces
268 * that follow the last word on the line then those that fit are included on
270 * 2. If the text is centred or right-justified and there is room for some of
271 * the spaces that follow the last word on the line then all but one of those
272 * that fit are included on the line.
273 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
274 * character of a new line it will be skipped.
277 * hdc [in] The handle to the DC that defines the font.
278 * str [in/out] The string that needs to be broken.
279 * max_str [in] The dimension of str (number of WCHAR).
280 * len_str [in/out] The number of characters in str
281 * width [in] The maximum width permitted
282 * format [in] The format flags in effect
283 * chars_fit [in] The maximum number of characters of str that are already
284 * known to fit; chars_fit+1 is known not to fit.
285 * chars_used [out] The number of characters of str that have been "used" and
286 * do not need to be included in later text. For example this will
287 * include any spaces that have been discarded from the start of
289 * size [out] The size of the returned text in logical coordinates
291 * Pedantic assumption - Assumes that the text length is monotonically
292 * increasing with number of characters (i.e. no weird kernings)
296 * Work back from the last character that did fit to either a space or the last
297 * character of a word, whichever is met first.
298 * If there was one or the first character didn't fit then
299 * If the text is centred or right justified and that one character was a
300 * space then break the line before that character
301 * Otherwise break the line after that character
302 * and if the next character is a space then discard it.
303 * Suppose there was none (and the first character did fit).
304 * If Break Within Word is permitted
305 * break the word after the last character that fits (there must be
306 * at least one; none is caught earlier).
308 * discard any trailing space.
309 * include the whole word; it may be ellipsified later
311 * Break Within Word is permitted under a set of circumstances that are not
312 * totally clear yet. Currently our best guess is:
313 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
314 * DT_PATH_ELLIPSIS is
317 static void TEXT_WordBreak (HDC hdc, WCHAR *str, unsigned int max_str,
318 unsigned int *len_str,
319 int width, int format, unsigned int chars_fit,
320 unsigned int *chars_used, SIZE *size)
324 assert (format & DT_WORDBREAK);
325 assert (chars_fit < *len_str);
327 /* Work back from the last character that did fit to either a space or the
328 * last character of a word, whichever is met first.
330 p = str + chars_fit; /* The character that doesn't fit */
333 ; /* we pretend that it fits anyway */
334 else if (*p == SPACE) /* chars_fit < *len_str so this is valid */
335 p--; /* the word just fitted */
338 while (p > str && *(--p) != SPACE)
340 word_fits = (p != str || *p == SPACE);
342 /* If there was one or the first character didn't fit then */
345 /* break the line before/after that character */
346 if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
349 /* and if the next character is a space then discard it. */
350 *chars_used = *len_str;
354 /* Suppose there was none. */
357 if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
360 /* break the word after the last character that fits (there must be
361 * at least one; none is caught earlier).
363 *len_str = chars_fit;
364 *chars_used = chars_fit;
366 /* FIXME - possible error. Since the next character is now removed
367 * this could make the text longer so that it no longer fits, and
368 * so we need a loop to test and shrink.
374 /* discard any trailing space. */
375 const WCHAR *e = str + *len_str;
377 while (p < e && *p != SPACE)
379 *chars_used = p - str;
380 if (p < e) /* i.e. loop failed because *p == SPACE */
383 /* include the whole word; it may be ellipsified later */
385 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
386 * so that it will be too long
390 /* Remeasure the string */
391 GetTextExtentExPointW (hdc, str, *len_str, 0, NULL, NULL, size);
394 /*********************************************************************
397 * Skip over the given number of characters, bearing in mind prefix
398 * substitution and the fact that a character may take more than one
399 * WCHAR (Unicode surrogates are two words long) (and there may have been
403 * new_count [out] The updated count
404 * new_str [out] The updated pointer
405 * start_count [in] The count of remaining characters corresponding to the
406 * start of the string
407 * start_str [in] The starting point of the string
408 * max [in] The number of characters actually in this segment of the
409 * string (the & counts)
410 * n [in] The number of characters to skip (if prefix then
412 * prefix [in] Apply prefix substitution
418 * There must be at least n characters in the string
419 * We need max because the "line" may have ended with a & followed by a tab
420 * or newline etc. which we don't want to swallow
423 static void TEXT_SkipChars (int *new_count, const WCHAR **new_str,
424 int start_count, const WCHAR *start_str,
425 int max, int n, int prefix)
427 /* This is specific to wide characters, MSDN doesn't say anything much
428 * about Unicode surrogates yet and it isn't clear if _wcsinc will
429 * correctly handle them so we'll just do this the easy way for now
434 const WCHAR *str_on_entry = start_str;
438 if (*start_str++ == PREFIX && max--)
441 start_count -= (start_str - str_on_entry);
448 *new_str = start_str;
449 *new_count = start_count;
452 /*********************************************************************
455 * Reanalyse the text to find the prefixed character. This is called when
456 * wordbreaking or ellipsification has shortened the string such that the
457 * previously noted prefixed character is no longer visible.
460 * str [in] The original string segment (including all characters)
461 * n1 [in] The number of characters visible before the path ellipsis
462 * n2 [in] The number of characters replaced by the path ellipsis
463 * ne [in] The number of characters in the path ellipsis, ignored if
465 * n3 [in] The number of characters visible after the path ellipsis
468 * The prefix offset within the new string segment (the one that contains the
469 * ellipses and does not contain the prefix characters) (-1 if none)
472 * We know that n1+n2+n3 must be strictly less than the length of the segment
473 * (because otherwise there would be no need to call this function)
476 static int TEXT_Reprefix (const WCHAR *str, unsigned int n1, unsigned int n2,
477 unsigned int ne, unsigned int n3)
481 unsigned int n = n1 + n2 + n3;
487 /* Reached the path ellipsis; jump over it */
490 if (!n3) break; /* Nothing after the path ellipsis */
492 if (*str++ == PREFIX)
494 result = (i < n1) ? i : i - n2 + ne;
503 /*********************************************************************
504 * Returns true if and only if the remainder of the line is a single
505 * newline representation or nothing
508 static int remainder_is_none_or_newline (int num_chars, const WCHAR *str)
510 if (!num_chars) return TRUE;
511 if (*str != LF && *str != CR) return FALSE;
512 if (!--num_chars) return TRUE;
513 if (*str == *(str+1)) return FALSE;
515 if (*str != CR && *str != LF) return FALSE;
516 if (--num_chars) return FALSE;
520 /*********************************************************************
521 * Return next line of text from a string.
523 * hdc - handle to DC.
524 * str - string to parse into lines.
525 * count - length of str.
526 * dest - destination in which to return line.
527 * len - dest buffer size in chars on input, copied length into dest on output.
528 * width - maximum width of line in pixels.
529 * format - format type passed to DrawText.
530 * retsize - returned size of the line in pixels.
531 * last_line - TRUE if is the last line that will be processed
532 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
533 * the return string is built.
535 * Returns pointer to next char in str after end of the line
536 * or NULL if end of str reached.
538 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
539 WCHAR *dest, int *len, int width, DWORD format,
540 SIZE *retsize, int last_line, WCHAR **p_retstr)
546 int seg_i, seg_count, seg_j;
554 /* For each text segment in the line */
560 /* Skip any leading tabs */
562 if (str[i] == TAB && (format & DT_EXPANDTABS))
564 plen = ((plen/tabwidth)+1)*tabwidth;
565 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
566 while (*count && str[i] == TAB)
569 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
574 /* Now copy as far as the next tab or cr/lf or eos */
581 (str[i] != TAB || !(format & DT_EXPANDTABS)) &&
582 ((str[i] != CR && str[i] != LF) || (format & DT_SINGLELINE)))
584 if (str[i] == PREFIX && !(format & DT_NOPREFIX) && *count > 1)
586 (*count)--, i++; /* Throw away the prefix itself */
587 if (str[i] == PREFIX)
589 /* Swallow it before we see it again */
590 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
592 else if (prefix_offset == -1 || prefix_offset >= seg_j)
596 /* else the previous prefix was in an earlier segment of the
597 * line; we will leave it to the drawing code to catch this
603 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
608 /* Measure the whole text segment and possibly WordBreak and
612 j_in_seg = j - seg_j;
613 max_seg_width = width - plen;
614 GetTextExtentExPointW (hdc, dest + seg_j, j_in_seg, max_seg_width, &num_fit, NULL, &size);
616 /* The Microsoft handling of various combinations of formats is weird.
617 * The following may very easily be incorrect if several formats are
618 * combined, and may differ between versions (to say nothing of the
619 * several bugs in the Microsoft versions).
622 line_fits = (num_fit >= j_in_seg);
623 if (!line_fits && (format & DT_WORDBREAK))
627 TEXT_WordBreak (hdc, dest+seg_j, maxl-seg_j, &j_in_seg,
628 max_seg_width, format, num_fit, &chars_used, &size);
629 line_fits = (size.cx <= max_seg_width);
630 /* and correct the counts */
631 TEXT_SkipChars (count, &s, seg_count, str+seg_i, i-seg_i,
632 chars_used, !(format & DT_NOPREFIX));
636 len_before_ellipsis = j_in_seg;
637 len_under_ellipsis = 0;
638 len_after_ellipsis = 0;
641 if (!line_fits && (format & DT_PATH_ELLIPSIS))
643 TEXT_PathEllipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
644 max_seg_width, &size, *p_retstr, &len_before_ellipsis, &len_ellipsis, &len_under_ellipsis, &len_after_ellipsis);
645 line_fits = (size.cx <= max_seg_width);
648 /* NB we may end up ellipsifying a word-broken or path_ellipsified
650 if ((!line_fits && (format & DT_WORD_ELLIPSIS)) ||
651 ((format & DT_END_ELLIPSIS) &&
652 ((last_line && *count) ||
653 (remainder_is_none_or_newline (*count, &str[i]) && !line_fits))))
656 TEXT_Ellipsify (hdc, dest + seg_j, maxl-seg_j, &j_in_seg,
657 max_seg_width, &size, *p_retstr, &before, &len_ellipsis);
658 if (before > len_before_ellipsis)
660 /* We must have done a path ellipsis too */
661 len_after_ellipsis = before - len_before_ellipsis - len_ellipsis;
665 len_before_ellipsis = before;
666 /* len_after_ellipsis remains as zero as does
667 * len_under_ellsipsis
670 line_fits = (size.cx <= max_seg_width);
673 /* As an optimisation if we have ellipsified and we are expanding
674 * tabs and we haven't reached the end of the line we can skip to it
675 * now rather than going around the loop again.
677 if ((format & DT_EXPANDTABS) && ellipsified)
679 if (format & DT_SINGLELINE)
683 while ((*count) && str[i] != CR && str[i] != LF)
690 j = seg_j + j_in_seg;
691 if (prefix_offset >= seg_j + len_before_ellipsis)
693 prefix_offset = TEXT_Reprefix (str + seg_i, len_before_ellipsis,
694 len_under_ellipsis, len_ellipsis,
696 if (prefix_offset != -1)
697 prefix_offset += seg_j;
701 if (size.cy > retsize->cy)
702 retsize->cy = size.cy;
708 else if (str[i] == CR || str[i] == LF)
711 if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
717 /* else it was a Tab and we go around again */
729 /***********************************************************************
730 * TEXT_DrawUnderscore
732 * Draw the underline under the prefixed character
735 * hdc [in] The handle of the DC for drawing
736 * x [in] The x location of the line segment (logical coordinates)
737 * y [in] The y location of where the underscore should appear
738 * (logical coordinates)
739 * str [in] The text of the line segment
740 * offset [in] The offset of the underscored character within str
743 static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset)
751 GetTextExtentPointW (hdc, str, offset, &size);
752 prefix_x = x + size.cx;
753 GetTextExtentPointW (hdc, str, offset+1, &size);
754 prefix_end = x + size.cx - 1;
755 /* The above method may eventually be slightly wrong due to kerning etc. */
757 hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
758 oldPen = SelectObject (hdc, hpen);
759 MoveToEx (hdc, prefix_x, y, NULL);
760 LineTo (hdc, prefix_end, y);
761 SelectObject (hdc, oldPen);
765 /***********************************************************************
766 * DrawTextExW (USER32.@)
768 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
769 * is not quite complete, especially with regard to \0. We will assume that
770 * the returned string could have a length of up to i_count+3 and also have
771 * a trailing \0 (which would be 4 more than a not-null-terminated string but
772 * 3 more than a null-terminated string). If this is not so then increase
773 * the allowance in DrawTextExA.
775 #define MAX_STATIC_BUFFER 1024
776 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
777 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
781 WCHAR *retstr, *p_retstr;
783 static WCHAR line[MAX_STATIC_BUFFER];
784 int len, lh, count=i_count;
786 int lmargin = 0, rmargin = 0;
787 int x = rect->left, y = rect->top;
788 int width = rect->right - rect->left;
792 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
793 rect->left, rect->top, rect->right, rect->bottom);
795 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
796 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
799 if (count == -1) count = strlenW(str);
800 if (count == 0) return 0;
803 if (flags & DT_SINGLELINE)
804 flags &= ~DT_WORDBREAK;
806 GetTextMetricsW(hdc, &tm);
807 if (flags & DT_EXTERNALLEADING)
808 lh = tm.tmHeight + tm.tmExternalLeading;
814 lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
815 rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
816 if (!(flags & (DT_CENTER | DT_RIGHT)))
818 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
821 if (flags & DT_EXPANDTABS)
823 int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
824 tabwidth = tm.tmAveCharWidth * tabstop;
827 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
829 if (flags & DT_MODIFYSTRING)
831 size_retstr = (count + 4) * sizeof (WCHAR);
832 retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
833 if (!retstr) return 0;
834 memcpy (retstr, str, size_retstr);
846 len = MAX_STATIC_BUFFER;
847 last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
848 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr);
850 if (flags & DT_CENTER) x = (rect->left + rect->right -
852 else if (flags & DT_RIGHT) x = rect->right - size.cx;
854 if (flags & DT_SINGLELINE)
856 if (flags & DT_VCENTER) y = rect->top +
857 (rect->bottom - rect->top) / 2 - size.cy / 2;
858 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
861 if (!(flags & DT_CALCRECT))
863 const WCHAR *str = line;
869 if ((flags & DT_EXPANDTABS))
872 p = str; while (p < str+len && *p != TAB) p++;
874 if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size))
880 if (!ExtTextOutW( hdc, xseg, y,
881 ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
882 ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
883 rect, str, len_seg, NULL )) return 0;
884 if (prefix_offset != -1 && prefix_offset < len_seg)
886 TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset);
892 assert ((flags & DT_EXPANDTABS) && *str == TAB);
894 xseg += ((size.cx/tabwidth)+1)*tabwidth;
895 if (prefix_offset != -1)
897 if (prefix_offset < len_seg)
899 /* We have just drawn an underscore; we ought to
900 * figure out where the next one is. I am going
901 * to leave it for now until I have a better model
902 * for the line, which will make reprefixing easier
907 prefix_offset -= len_seg;
912 else if (size.cx > max_width)
917 dtp->uiLengthDrawn += len;
919 while (strPtr && !last_line);
921 if (flags & DT_CALCRECT)
923 rect->right = rect->left + max_width;
926 rect->right += lmargin + rmargin;
930 memcpy (str, retstr, size_retstr);
931 HeapFree (GetProcessHeap(), 0, retstr);
933 return y - rect->top;
936 /***********************************************************************
937 * DrawTextExA (USER32.@)
939 * If DT_MODIFYSTRING is specified then there must be room for up to
940 * 4 extra characters. We take great care about just how much modified
943 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
944 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
954 if (count == -1) count = strlen(str);
955 if (!count) return 0;
956 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
959 if (flags & DT_MODIFYSTRING)
964 wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
967 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
968 if (flags & DT_MODIFYSTRING)
969 for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
970 /* Initialise the extra characters so that we can see which ones
971 * change. U+FFFE is guaranteed to be not a unicode character and
972 * so will not be generated by DrawTextEx itself.
974 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
975 if (flags & DT_MODIFYSTRING)
977 /* Unfortunately the returned string may contain multiple \0s
978 * and so we need to measure it ourselves.
980 for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
981 WideCharToMultiByte( CP_ACP, 0, wstr, wcount, str, amax, NULL, NULL );
983 HeapFree(GetProcessHeap(), 0, wstr);
988 /***********************************************************************
989 * DrawTextW (USER32.@)
991 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
995 memset (&dtp, 0, sizeof(dtp));
996 if (flags & DT_TABSTOP)
998 dtp.iTabLength = (flags >> 8) && 0xff;
1001 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1004 /***********************************************************************
1005 * DrawTextA (USER32.@)
1007 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1011 memset (&dtp, 0, sizeof(dtp));
1012 if (flags & DT_TABSTOP)
1014 dtp.iTabLength = (flags >> 8) && 0xff;
1015 flags &= 0xffff00ff;
1017 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1020 /***********************************************************************
1021 * DrawText (USER.85)
1023 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
1030 CONV_RECT16TO32( rect, &rect32 );
1031 ret = DrawTextA( hdc, str, count, &rect32, flags );
1032 CONV_RECT32TO16( &rect32, rect );
1034 else ret = DrawTextA( hdc, str, count, NULL, flags);
1039 /***********************************************************************
1041 * GrayString functions
1044 /* ### start build ### */
1045 extern WORD CALLBACK TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16,WORD,LONG,WORD);
1046 /* ### stop build ### */
1048 struct gray_string_info
1050 GRAYSTRINGPROC16 proc;
1054 /* callback for 16-bit gray string proc */
1055 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
1057 const struct gray_string_info *info = (struct gray_string_info *)param;
1058 return TEXT_CallTo16_word_wlw( info->proc, hdc, info->param, len );
1061 /***********************************************************************
1064 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1065 * heap and we can guarantee that the handles fit in an INT16. We have to
1066 * rethink the strategy once the migration to NT handles is complete.
1067 * We are going to get a lot of code-duplication once this migration is
1071 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1072 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
1074 HBITMAP hbm, hbmsave;
1082 if(!hdc) return FALSE;
1083 if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1088 slen = lstrlenW((LPCWSTR)lp);
1090 slen = strlen((LPCSTR)lp);
1092 slen = strlen(MapSL(lp));
1095 if((cx == 0 || cy == 0) && slen != -1)
1099 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
1101 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
1103 GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
1104 if(cx == 0) cx = s.cx;
1105 if(cy == 0) cy = s.cy;
1108 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1109 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1110 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1111 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1112 SelectObject( memdc, hbsave );
1113 SetTextColor(memdc, RGB(255, 255, 255));
1114 SetBkColor(memdc, RGB(0, 0, 0));
1115 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1120 retval = fn(memdc, lp, slen);
1122 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
1127 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
1129 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
1131 TextOutA(memdc, 0, 0, MapSL(lp), slen);
1134 SelectObject(memdc, hfsave);
1137 * Windows doc says that the bitmap isn't grayed when len == -1 and
1138 * the callback function returns FALSE. However, testing this on
1139 * win95 showed otherwise...
1141 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1142 if(retval || len != -1)
1145 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
1146 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1147 SelectObject(memdc, hbsave);
1150 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
1151 fg = SetTextColor(hdc, RGB(0, 0, 0));
1152 bg = SetBkColor(hdc, RGB(255, 255, 255));
1153 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1154 SetTextColor(hdc, fg);
1155 SetBkColor(hdc, bg);
1156 if(hb) SelectObject(hdc, hbsave);
1158 SelectObject(memdc, hbmsave);
1165 /***********************************************************************
1166 * GrayString (USER.185)
1168 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
1169 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
1170 INT16 cx, INT16 cy )
1172 struct gray_string_info info;
1174 if (!gsprc) return TEXT_GrayString(hdc, hbr, NULL, lParam, cch, x, y, cx, cy, FALSE, FALSE);
1176 info.param = lParam;
1177 return TEXT_GrayString( hdc, hbr, gray_string_callback, (LPARAM)&info,
1178 cch, x, y, cx, cy, FALSE, FALSE);
1182 /***********************************************************************
1183 * GrayStringA (USER32.@)
1185 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1186 LPARAM lParam, INT cch, INT x, INT y,
1189 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1194 /***********************************************************************
1195 * GrayStringW (USER32.@)
1197 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1198 LPARAM lParam, INT cch, INT x, INT y,
1201 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
1205 /***********************************************************************
1207 * TabbedText functions
1210 /***********************************************************************
1211 * TEXT_TabbedTextOut
1213 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1214 * Note: this doesn't work too well for text-alignment modes other
1215 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1217 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
1218 INT count, INT cTabStops, const INT16 *lpTabPos16,
1219 const INT *lpTabPos32, INT nTabOrg,
1232 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
1238 GetTextMetricsA( hdc, &tm );
1239 defWidth = 8 * tm.tmAveCharWidth;
1244 for (i = 0; i < count; i++)
1245 if (lpstr[i] == '\t') break;
1246 GetTextExtentPointA( hdc, lpstr, i, &extent );
1249 while ((cTabStops > 0) &&
1250 (nTabOrg + *lpTabPos32 <= x + extent.cx))
1258 while ((cTabStops > 0) &&
1259 (nTabOrg + *lpTabPos16 <= x + extent.cx))
1266 tabPos = x + extent.cx;
1267 else if (cTabStops > 0)
1268 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
1270 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
1277 r.bottom = y + extent.cy;
1278 ExtTextOutA( hdc, x, y,
1279 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1280 &r, lpstr, i, NULL );
1286 return MAKELONG(tabPos - start, extent.cy);
1290 /***********************************************************************
1291 * TabbedTextOut (USER.196)
1293 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
1294 INT16 count, INT16 cTabStops,
1295 const INT16 *lpTabPos, INT16 nTabOrg )
1297 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1298 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1299 lpTabPos, NULL, nTabOrg, TRUE );
1303 /***********************************************************************
1304 * TabbedTextOutA (USER32.@)
1306 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1307 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1309 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
1310 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
1311 NULL, lpTabPos, nTabOrg, TRUE );
1315 /***********************************************************************
1316 * TabbedTextOutW (USER32.@)
1318 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1319 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1324 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1326 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
1327 p = HeapAlloc( GetProcessHeap(), 0, acount );
1328 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
1329 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
1330 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
1331 HeapFree( GetProcessHeap(), 0, p );
1336 /***********************************************************************
1337 * GetTabbedTextExtent (USER.197)
1339 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
1340 INT16 cTabStops, const INT16 *lpTabPos )
1342 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1343 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1344 lpTabPos, NULL, 0, FALSE );
1348 /***********************************************************************
1349 * GetTabbedTextExtentA (USER32.@)
1351 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1352 INT cTabStops, const INT *lpTabPos )
1354 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
1355 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
1356 NULL, lpTabPos, 0, FALSE );
1360 /***********************************************************************
1361 * GetTabbedTextExtentW (USER32.@)
1363 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1364 INT cTabStops, const INT *lpTabPos )
1369 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
1371 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
1372 p = HeapAlloc( GetProcessHeap(), 0, acount );
1373 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
1374 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
1375 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
1376 HeapFree( GetProcessHeap(), 0, p );