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
28 #include "wine/port.h"
35 #include "wine/winuser16.h"
36 #include "wine/unicode.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(text
);
45 /*********************************************************************
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
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.
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
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!!)
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)
90 #define FORWARD_SLASH '/'
91 #define BACK_SLASH '\\'
93 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
95 typedef struct tag_ellipsis_data
103 /*********************************************************************
104 * TEXT_Ellipsify (static)
106 * Add an ellipsis to the end of the given string whilst ensuring it fits.
108 * If the ellipsis alone doesn't fit then it will be returned anyway.
110 * See Also TEXT_PathEllipsify
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.
127 * See for example Microsoft article Q249678.
129 * For now we will simply use three dots rather than worrying about whether
130 * the font contains an explicit ellipsis character.
132 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
133 unsigned int *len_str
, int width
, SIZE
*size
,
135 int *len_before
, int *len_ellip
)
137 unsigned int len_ellipsis
;
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
;
146 strncpyW (str
+ *len_str
, ELLIPSISW
, len_ellipsis
);
148 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
149 NULL
, NULL
, size
)) break;
151 if (!*len_str
|| size
->cx
<= width
) break;
155 *len_ellip
= len_ellipsis
;
156 *len_before
= *len_str
;
157 *len_str
+= len_ellipsis
;
161 strncpyW (modstr
, str
, *len_str
);
162 *(str
+*len_str
) = '\0';
166 /*********************************************************************
167 * TEXT_PathEllipsify (static)
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
173 * See Also TEXT_Ellipsify
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
189 * For now we will simply use three dots rather than worrying about whether
190 * the font contains an explicit ellipsis character.
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...)
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.
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
)
216 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
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
226 str
[*len_str
] = '\0'; /* to simplify things */
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
);
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
245 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
246 NULL
, NULL
, size
)) break;
248 if (lastSlash
== str
|| size
->cx
<= width
) break;
250 /* overlap-safe movement to the left */
251 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
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
;
266 strncpyW (modstr
, str
, *len_str
);
267 *(str
+*len_str
) = '\0';
271 /*********************************************************************
272 * TEXT_WordBreak (static)
274 * Perform wordbreak processing on the given string
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.
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
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.
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
303 * size [out] The size of the returned text in logical coordinates
305 * Pedantic assumption - Assumes that the text length is monotonically
306 * increasing with number of characters (i.e. no weird kernings)
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).
322 * discard any trailing space.
323 * include the whole word; it may be ellipsified later
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
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
)
338 assert (format
& DT_WORDBREAK
);
339 assert (chars_fit
< *len_str
);
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.
344 p
= str
+ chars_fit
; /* The character that doesn't 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 */
352 while (p
> str
&& *(--p
) != SPACE
)
354 word_fits
= (p
!= str
|| *p
== SPACE
);
356 /* If there was one or the first character didn't fit then */
360 /* break the line before/after that character */
361 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
363 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
365 /* and if the next character is a space then discard it. */
366 *chars_used
= *len_str
;
370 /* Suppose there was none. */
373 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
376 /* break the word after the last character that fits (there must be
377 * at least one; none is caught earlier).
379 *len_str
= chars_fit
;
380 *chars_used
= chars_fit
;
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.
390 /* discard any trailing space. */
391 const WCHAR
*e
= str
+ *len_str
;
393 while (p
< e
&& *p
!= SPACE
)
395 *chars_used
= p
- str
;
396 if (p
< e
) /* i.e. loop failed because *p == SPACE */
399 /* include the whole word; it may be ellipsified later */
401 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
402 * so that it will be too long
406 /* Remeasure the string */
407 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
410 /*********************************************************************
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
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
428 * prefix [in] Apply prefix substitution
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
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
)
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
450 const WCHAR
*str_on_entry
= start_str
;
454 if (*start_str
++ == PREFIX
&& max
--)
457 start_count
-= (start_str
- str_on_entry
);
464 *new_str
= start_str
;
465 *new_count
= start_count
;
468 /*********************************************************************
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.
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
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)
485 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
486 const ellipsis_data
*pe
)
490 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
496 /* Reached the path ellipsis; jump over it */
497 if (ns
< pe
->under
) break;
501 if (!pe
->after
) break; /* Nothing after the path ellipsis */
505 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 */
520 /*********************************************************************
521 * Returns true if and only if the remainder of the line is a single
522 * newline representation or nothing
525 static int remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
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
;
532 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
533 if (--num_chars
) return FALSE
;
537 /*********************************************************************
538 * Return next line of text from a string.
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.
562 * Returns pointer to next char in str after end of the line
563 * or NULL if end of str reached.
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
)
575 int seg_i
, seg_count
, seg_j
;
582 *pprefix_offset
= -1;
584 /* For each text segment in the line */
590 /* Skip any leading tabs */
592 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
594 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
595 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
596 while (*count
&& str
[i
] == TAB
)
599 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
604 /* Now copy as far as the next tab or cr/lf or eos */
611 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
612 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
614 if (str
[i
] == PREFIX
&& !(format
& DT_NOPREFIX
) && *count
> 1)
616 (*count
)--, i
++; /* Throw away the prefix itself */
617 if (str
[i
] == PREFIX
)
619 /* Swallow it before we see it again */
620 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
622 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
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
633 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
638 /* Measure the whole text segment and possibly WordBreak and
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
);
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).
652 line_fits
= (num_fit
>= j_in_seg
);
653 if (!line_fits
&& (format
& DT_WORDBREAK
))
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
));
666 pellip
->before
= j_in_seg
;
671 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
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
);
678 /* NB we may end up ellipsifying a word-broken or path_ellipsified
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
))))
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
)
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 */
696 /* If we are here after a path ellipsification it must be
697 * because even the ellipsis itself didn't fit.
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
706 line_fits
= (size
.cx
<= max_seg_width
);
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.
713 if ((format
& DT_EXPANDTABS
) && ellipsified
)
715 if (format
& DT_SINGLELINE
)
719 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
726 j
= seg_j
+ j_in_seg
;
727 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
729 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
730 if (*pprefix_offset
!= -1)
731 *pprefix_offset
+= seg_j
;
735 if (size
.cy
> retsize
->cy
)
736 retsize
->cy
= size
.cy
;
742 else if (str
[i
] == CR
|| str
[i
] == LF
)
745 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
751 /* else it was a Tab and we go around again */
763 /***********************************************************************
764 * TEXT_DrawUnderscore
766 * Draw the underline under the prefixed character
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
777 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
)
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. */
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
);
799 /***********************************************************************
800 * DrawTextExW (USER32.@)
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.
809 #define MAX_STATIC_BUFFER 1024
810 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
811 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
815 WCHAR
*retstr
, *p_retstr
;
817 static WCHAR line
[MAX_STATIC_BUFFER
];
818 int len
, lh
, count
=i_count
;
820 int lmargin
= 0, rmargin
= 0;
821 int x
= rect
->left
, y
= rect
->top
;
822 int width
= rect
->right
- rect
->left
;
825 int tabwidth
/* to keep gcc happy */ = 0;
829 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str
, count
), count
,
830 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
832 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
833 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
836 if (count
== -1) count
= strlenW(str
);
837 if (count
== 0) return 0;
840 if (flags
& DT_SINGLELINE
)
841 flags
&= ~DT_WORDBREAK
;
843 GetTextMetricsW(hdc
, &tm
);
844 if (flags
& DT_EXTERNALLEADING
)
845 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
851 lmargin
= dtp
->iLeftMargin
* tm
.tmAveCharWidth
;
852 rmargin
= dtp
->iRightMargin
* tm
.tmAveCharWidth
;
853 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
855 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
858 if (flags
& DT_EXPANDTABS
)
860 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
) ? dtp
->iTabLength
: 8;
861 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
864 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
866 if (flags
& DT_MODIFYSTRING
)
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
);
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
);
886 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
888 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
890 if (flags
& DT_SINGLELINE
)
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
;
897 if (!(flags
& DT_CALCRECT
))
899 const WCHAR
*str
= line
;
905 if ((flags
& DT_EXPANDTABS
))
908 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
910 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
))
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
)
922 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
);
928 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
930 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
931 if (prefix_offset
!= -1)
933 if (prefix_offset
< len_seg
)
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.
944 prefix_offset
-= len_seg
;
949 else if (size
.cx
> max_width
)
954 dtp
->uiLengthDrawn
+= len
;
956 while (strPtr
&& !last_line
);
958 if (flags
& DT_CALCRECT
)
960 rect
->right
= rect
->left
+ max_width
;
963 rect
->right
+= lmargin
+ rmargin
;
967 memcpy (str
, retstr
, size_retstr
);
968 HeapFree (GetProcessHeap(), 0, retstr
);
970 return y
- rect
->top
;
973 /***********************************************************************
974 * DrawTextExA (USER32.@)
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
980 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
981 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
992 if (count
== -1) count
= strlen(str
);
993 if (!count
) return 0;
994 wcount
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
997 if (flags
& DT_MODIFYSTRING
)
1002 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
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.
1012 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, NULL
);
1013 if (flags
& DT_MODIFYSTRING
)
1015 /* Unfortunately the returned string may contain multiple \0s
1016 * and so we need to measure it ourselves.
1018 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1019 WideCharToMultiByte( CP_ACP
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1021 HeapFree(GetProcessHeap(), 0, wstr
);
1026 /***********************************************************************
1027 * DrawTextW (USER32.@)
1029 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1033 memset (&dtp
, 0, sizeof(dtp
));
1034 if (flags
& DT_TABSTOP
)
1036 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1037 flags
&= 0xffff00ff;
1039 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1042 /***********************************************************************
1043 * DrawTextA (USER32.@)
1045 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1049 memset (&dtp
, 0, sizeof(dtp
));
1050 if (flags
& DT_TABSTOP
)
1052 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1053 flags
&= 0xffff00ff;
1055 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1058 /***********************************************************************
1059 * DrawText (USER.85)
1061 INT16 WINAPI
DrawText16( HDC16 hdc
, LPCSTR str
, INT16 count
, LPRECT16 rect
, UINT16 flags
)
1068 CONV_RECT16TO32( rect
, &rect32
);
1069 ret
= DrawTextA( hdc
, str
, count
, &rect32
, flags
);
1070 CONV_RECT32TO16( &rect32
, rect
);
1072 else ret
= DrawTextA( hdc
, str
, count
, NULL
, flags
);
1077 /***********************************************************************
1079 * GrayString functions
1082 /* ### start build ### */
1083 extern WORD CALLBACK
TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16
,WORD
,LONG
,WORD
);
1084 /* ### stop build ### */
1086 struct gray_string_info
1088 GRAYSTRINGPROC16 proc
;
1092 /* callback for 16-bit gray string proc */
1093 static BOOL CALLBACK
gray_string_callback( HDC hdc
, LPARAM param
, INT len
)
1095 const struct gray_string_info
*info
= (struct gray_string_info
*)param
;
1096 return TEXT_CallTo16_word_wlw( info
->proc
, hdc
, info
->param
, len
);
1099 /***********************************************************************
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
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
)
1112 HBITMAP hbm
, hbmsave
;
1120 if(!hdc
) return FALSE
;
1121 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1126 slen
= lstrlenW((LPCWSTR
)lp
);
1128 slen
= strlen((LPCSTR
)lp
);
1130 slen
= strlen(MapSL(lp
));
1133 if((cx
== 0 || cy
== 0) && slen
!= -1)
1137 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
1139 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
1141 GetTextExtentPoint32A(hdc
, MapSL(lp
), slen
, &s
);
1142 if(cx
== 0) cx
= s
.cx
;
1143 if(cy
== 0) cy
= s
.cy
;
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
));
1158 retval
= fn(memdc
, lp
, slen
);
1160 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
1165 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
1167 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
1169 TextOutA(memdc
, 0, 0, MapSL(lp
), slen
);
1172 SelectObject(memdc
, hfsave
);
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...
1179 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1180 if(retval
|| len
!= -1)
1183 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
1184 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1185 SelectObject(memdc
, hbsave
);
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
);
1196 SelectObject(memdc
, hbmsave
);
1203 /***********************************************************************
1204 * GrayString (USER.185)
1206 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
1207 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
1208 INT16 cx
, INT16 cy
)
1210 struct gray_string_info info
;
1212 if (!gsprc
) return TEXT_GrayString(hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
1214 info
.param
= lParam
;
1215 return TEXT_GrayString( hdc
, hbr
, gray_string_callback
, (LPARAM
)&info
,
1216 cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
1220 /***********************************************************************
1221 * GrayStringA (USER32.@)
1223 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1224 LPARAM lParam
, INT cch
, INT x
, INT y
,
1227 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
,
1232 /***********************************************************************
1233 * GrayStringW (USER32.@)
1235 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1236 LPARAM lParam
, INT cch
, INT x
, INT y
,
1239 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
,
1243 /***********************************************************************
1245 * TabbedText functions
1248 /***********************************************************************
1249 * TEXT_TabbedTextOut
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 :-)
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
,
1270 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
1276 GetTextMetricsA( hdc
, &tm
);
1277 defWidth
= 8 * tm
.tmAveCharWidth
;
1282 for (i
= 0; i
< count
; i
++)
1283 if (lpstr
[i
] == '\t') break;
1284 GetTextExtentPointA( hdc
, lpstr
, i
, &extent
);
1287 while ((cTabStops
> 0) &&
1288 (nTabOrg
+ *lpTabPos32
<= x
+ extent
.cx
))
1296 while ((cTabStops
> 0) &&
1297 (nTabOrg
+ *lpTabPos16
<= x
+ extent
.cx
))
1304 tabPos
= x
+ extent
.cx
;
1305 else if (cTabStops
> 0)
1306 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
1308 tabPos
= nTabOrg
+ ((x
+ extent
.cx
- nTabOrg
) / defWidth
+ 1) * defWidth
;
1315 r
.bottom
= y
+ extent
.cy
;
1316 ExtTextOutA( hdc
, x
, y
,
1317 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1318 &r
, lpstr
, i
, NULL
);
1324 return MAKELONG(tabPos
- start
, extent
.cy
);
1328 /***********************************************************************
1329 * TabbedTextOut (USER.196)
1331 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
1332 INT16 count
, INT16 cTabStops
,
1333 const INT16
*lpTabPos
, INT16 nTabOrg
)
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
);
1341 /***********************************************************************
1342 * TabbedTextOutA (USER32.@)
1344 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1345 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
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
);
1353 /***********************************************************************
1354 * TabbedTextOutW (USER32.@)
1356 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1357 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1362 UINT codepage
= CP_ACP
; /* FIXME: get codepage of font charset */
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
);
1374 /***********************************************************************
1375 * GetTabbedTextExtent (USER.197)
1377 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
1378 INT16 cTabStops
, const INT16
*lpTabPos
)
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
);
1386 /***********************************************************************
1387 * GetTabbedTextExtentA (USER32.@)
1389 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1390 INT cTabStops
, const INT
*lpTabPos
)
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
);
1398 /***********************************************************************
1399 * GetTabbedTextExtentW (USER32.@)
1401 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1402 INT cTabStops
, const INT
*lpTabPos
)
1407 UINT codepage
= CP_ACP
; /* FIXME: get codepage of font charset */
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
);