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
;
138 unsigned int lo
, mid
, hi
;
140 len_ellipsis
= strlenW (ELLIPSISW
);
141 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
142 if (*len_str
> max_len
- len_ellipsis
)
143 *len_str
= max_len
- len_ellipsis
;
145 /* First do a quick binary search to get an upper bound for *len_str. */
147 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
150 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
153 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
155 if (size
->cx
> width
)
162 /* Now this should take only a couple iterations at most. */
165 strncpyW (str
+ *len_str
, ELLIPSISW
, len_ellipsis
);
167 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
168 NULL
, NULL
, size
)) break;
170 if (!*len_str
|| size
->cx
<= width
) break;
174 *len_ellip
= len_ellipsis
;
175 *len_before
= *len_str
;
176 *len_str
+= len_ellipsis
;
180 strncpyW (modstr
, str
, *len_str
);
181 *(str
+*len_str
) = '\0';
185 /*********************************************************************
186 * TEXT_PathEllipsify (static)
188 * Add an ellipsis to the provided string in order to make it fit within
189 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
192 * See Also TEXT_Ellipsify
195 * hdc [in] The handle to the DC that defines the font.
196 * str [in/out] The string that needs to be modified
197 * max_str [in] The dimension of str (number of WCHAR).
198 * len_str [in/out] The number of characters in str
199 * width [in] The maximum width permitted (in logical coordinates)
200 * size [out] The dimensions of the text
201 * modstr [out] The modified form of the string, to be returned to the
202 * calling program. It is assumed that the caller has
203 * made sufficient space available so we don't need to
204 * know the size of the space. This pointer may be NULL if
205 * the modified string is not required.
206 * pellip [out] The ellipsification results
208 * For now we will simply use three dots rather than worrying about whether
209 * the font contains an explicit ellipsis character.
211 * The following applies, I think to Win95. We will need to extend it for
212 * Win98 which can have both path and end ellipsis at the same time (e.g.
213 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
215 * The resulting string consists of as much as possible of the following:
216 * 1. The ellipsis itself
217 * 2. The last \ or / of the string (if any)
218 * 3. Everything after the last \ or / of the string (if any) or the whole
219 * string if there is no / or \. I believe that under Win95 this would
220 * include everything even though some might be clipped off the end whereas
221 * under Win98 that might be ellipsified too.
222 * Yet to be investigated is whether this would include wordbreaking if the
223 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
224 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
225 * broken within words).
226 * 4. All the stuff before the / or \, which is placed before the ellipsis.
228 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
229 unsigned int *len_str
, int width
, SIZE
*size
,
230 WCHAR
*modstr
, ellipsis_data
*pellip
)
235 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
237 len_ellipsis
= strlenW (ELLIPSISW
);
238 if (!max_len
) return;
239 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
240 if (*len_str
+ len_ellipsis
>= max_len
)
241 *len_str
= max_len
- len_ellipsis
-1;
242 /* Hopefully this will never happen, otherwise it would probably lose
243 * the wrong character
245 str
[*len_str
] = '\0'; /* to simplify things */
247 lastBkSlash
= strrchrW (str
, BACK_SLASH
);
248 lastFwdSlash
= strrchrW (str
, FORWARD_SLASH
);
249 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
250 if (!lastSlash
) lastSlash
= str
;
251 len_trailing
= *len_str
- (lastSlash
- str
);
253 /* overlap-safe movement to the right */
254 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
255 strncpyW (lastSlash
, ELLIPSISW
, len_ellipsis
);
256 len_trailing
+= len_ellipsis
;
257 /* From this point on lastSlash actually points to the ellipsis in front
258 * of the last slash and len_trailing includes the ellipsis
264 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
265 NULL
, NULL
, size
)) break;
267 if (lastSlash
== str
|| size
->cx
<= width
) break;
269 /* overlap-safe movement to the left */
270 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
277 pellip
->before
= lastSlash
-str
;
278 pellip
->len
= len_ellipsis
;
279 pellip
->under
= len_under
;
280 pellip
->after
= len_trailing
- len_ellipsis
;
281 *len_str
+= len_ellipsis
;
285 strncpyW (modstr
, str
, *len_str
);
286 *(str
+*len_str
) = '\0';
290 /*********************************************************************
291 * TEXT_WordBreak (static)
293 * Perform wordbreak processing on the given string
295 * Assumes that DT_WORDBREAK has been specified and not all the characters
296 * fit. Note that this function should even be called when the first character
297 * that doesn't fit is known to be a space or tab, so that it can swallow them.
299 * Note that the Windows processing has some strange properties.
300 * 1. If the text is left-justified and there is room for some of the spaces
301 * that follow the last word on the line then those that fit are included on
303 * 2. If the text is centred or right-justified and there is room for some of
304 * the spaces that follow the last word on the line then all but one of those
305 * that fit are included on the line.
306 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
307 * character of a new line it will be skipped.
310 * hdc [in] The handle to the DC that defines the font.
311 * str [in/out] The string that needs to be broken.
312 * max_str [in] The dimension of str (number of WCHAR).
313 * len_str [in/out] The number of characters in str
314 * width [in] The maximum width permitted
315 * format [in] The format flags in effect
316 * chars_fit [in] The maximum number of characters of str that are already
317 * known to fit; chars_fit+1 is known not to fit.
318 * chars_used [out] The number of characters of str that have been "used" and
319 * do not need to be included in later text. For example this will
320 * include any spaces that have been discarded from the start of
322 * size [out] The size of the returned text in logical coordinates
324 * Pedantic assumption - Assumes that the text length is monotonically
325 * increasing with number of characters (i.e. no weird kernings)
329 * Work back from the last character that did fit to either a space or the last
330 * character of a word, whichever is met first.
331 * If there was one or the first character didn't fit then
332 * If the text is centred or right justified and that one character was a
333 * space then break the line before that character
334 * Otherwise break the line after that character
335 * and if the next character is a space then discard it.
336 * Suppose there was none (and the first character did fit).
337 * If Break Within Word is permitted
338 * break the word after the last character that fits (there must be
339 * at least one; none is caught earlier).
341 * discard any trailing space.
342 * include the whole word; it may be ellipsified later
344 * Break Within Word is permitted under a set of circumstances that are not
345 * totally clear yet. Currently our best guess is:
346 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
347 * DT_PATH_ELLIPSIS is
350 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
351 unsigned int *len_str
,
352 int width
, int format
, unsigned int chars_fit
,
353 unsigned int *chars_used
, SIZE
*size
)
357 assert (format
& DT_WORDBREAK
);
358 assert (chars_fit
< *len_str
);
360 /* Work back from the last character that did fit to either a space or the
361 * last character of a word, whichever is met first.
363 p
= str
+ chars_fit
; /* The character that doesn't fit */
366 ; /* we pretend that it fits anyway */
367 else if (*p
== SPACE
) /* chars_fit < *len_str so this is valid */
368 p
--; /* the word just fitted */
371 while (p
> str
&& *(--p
) != SPACE
)
373 word_fits
= (p
!= str
|| *p
== SPACE
);
375 /* If there was one or the first character didn't fit then */
379 /* break the line before/after that character */
380 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
382 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
384 /* and if the next character is a space then discard it. */
385 *chars_used
= *len_str
;
389 /* Suppose there was none. */
392 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
395 /* break the word after the last character that fits (there must be
396 * at least one; none is caught earlier).
398 *len_str
= chars_fit
;
399 *chars_used
= chars_fit
;
401 /* FIXME - possible error. Since the next character is now removed
402 * this could make the text longer so that it no longer fits, and
403 * so we need a loop to test and shrink.
409 /* discard any trailing space. */
410 const WCHAR
*e
= str
+ *len_str
;
412 while (p
< e
&& *p
!= SPACE
)
414 *chars_used
= p
- str
;
415 if (p
< e
) /* i.e. loop failed because *p == SPACE */
418 /* include the whole word; it may be ellipsified later */
420 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
421 * so that it will be too long
425 /* Remeasure the string */
426 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
429 /*********************************************************************
432 * Skip over the given number of characters, bearing in mind prefix
433 * substitution and the fact that a character may take more than one
434 * WCHAR (Unicode surrogates are two words long) (and there may have been
438 * new_count [out] The updated count
439 * new_str [out] The updated pointer
440 * start_count [in] The count of remaining characters corresponding to the
441 * start of the string
442 * start_str [in] The starting point of the string
443 * max [in] The number of characters actually in this segment of the
444 * string (the & counts)
445 * n [in] The number of characters to skip (if prefix then
447 * prefix [in] Apply prefix substitution
453 * There must be at least n characters in the string
454 * We need max because the "line" may have ended with a & followed by a tab
455 * or newline etc. which we don't want to swallow
458 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
459 int start_count
, const WCHAR
*start_str
,
460 int max
, int n
, int prefix
)
462 /* This is specific to wide characters, MSDN doesn't say anything much
463 * about Unicode surrogates yet and it isn't clear if _wcsinc will
464 * correctly handle them so we'll just do this the easy way for now
469 const WCHAR
*str_on_entry
= start_str
;
473 if (*start_str
++ == PREFIX
&& max
--)
476 start_count
-= (start_str
- str_on_entry
);
483 *new_str
= start_str
;
484 *new_count
= start_count
;
487 /*********************************************************************
490 * Reanalyse the text to find the prefixed character. This is called when
491 * wordbreaking or ellipsification has shortened the string such that the
492 * previously noted prefixed character is no longer visible.
495 * str [in] The original string segment (including all characters)
496 * ns [in] The number of characters in str (including prefixes)
497 * pe [in] The ellipsification data
500 * The prefix offset within the new string segment (the one that contains the
501 * ellipses and does not contain the prefix characters) (-1 if none)
504 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
505 const ellipsis_data
*pe
)
509 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
515 /* Reached the path ellipsis; jump over it */
516 if (ns
< pe
->under
) break;
520 if (!pe
->after
) break; /* Nothing after the path ellipsis */
524 if (*str
++ == PREFIX
)
528 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
529 /* pe->len may be non-zero while pe_under is zero */
539 /*********************************************************************
540 * Returns true if and only if the remainder of the line is a single
541 * newline representation or nothing
544 static int remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
546 if (!num_chars
) return TRUE
;
547 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
548 if (!--num_chars
) return TRUE
;
549 if (*str
== *(str
+1)) return FALSE
;
551 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
552 if (--num_chars
) return FALSE
;
556 /*********************************************************************
557 * Return next line of text from a string.
559 * hdc - handle to DC.
560 * str - string to parse into lines.
561 * count - length of str.
562 * dest - destination in which to return line.
563 * len - dest buffer size in chars on input, copied length into dest on output.
564 * width - maximum width of line in pixels.
565 * format - format type passed to DrawText.
566 * retsize - returned size of the line in pixels.
567 * last_line - TRUE if is the last line that will be processed
568 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
569 * the return string is built.
570 * tabwidth - The width of a tab in logical coordinates
571 * pprefix_offset - Here is where we return the offset within dest of the first
572 * prefixed (underlined) character. -1 is returned if there
573 * are none. Note that there may be more; the calling code
574 * will need to use TEXT_Reprefix to find any later ones.
575 * pellip - Here is where we return the information about any ellipsification
576 * that was carried out. Note that if tabs are being expanded then
577 * this data will correspond to the last text segment actually
578 * returned in dest; by definition there would not have been any
579 * ellipsification in earlier text segments of the line.
581 * Returns pointer to next char in str after end of the line
582 * or NULL if end of str reached.
584 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
585 WCHAR
*dest
, int *len
, int width
, DWORD format
,
586 SIZE
*retsize
, int last_line
, WCHAR
**p_retstr
,
587 int tabwidth
, int *pprefix_offset
,
588 ellipsis_data
*pellip
)
594 int seg_i
, seg_count
, seg_j
;
601 *pprefix_offset
= -1;
603 /* For each text segment in the line */
609 /* Skip any leading tabs */
611 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
613 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
614 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
615 while (*count
&& str
[i
] == TAB
)
618 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
623 /* Now copy as far as the next tab or cr/lf or eos */
630 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
631 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
633 if (str
[i
] == PREFIX
&& !(format
& DT_NOPREFIX
) && *count
> 1)
635 (*count
)--, i
++; /* Throw away the prefix itself */
636 if (str
[i
] == PREFIX
)
638 /* Swallow it before we see it again */
639 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
641 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
645 /* else the previous prefix was in an earlier segment of the
646 * line; we will leave it to the drawing code to catch this
652 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
657 /* Measure the whole text segment and possibly WordBreak and
661 j_in_seg
= j
- seg_j
;
662 max_seg_width
= width
- plen
;
663 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
665 /* The Microsoft handling of various combinations of formats is weird.
666 * The following may very easily be incorrect if several formats are
667 * combined, and may differ between versions (to say nothing of the
668 * several bugs in the Microsoft versions).
671 line_fits
= (num_fit
>= j_in_seg
);
672 if (!line_fits
&& (format
& DT_WORDBREAK
))
676 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
677 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
678 line_fits
= (size
.cx
<= max_seg_width
);
679 /* and correct the counts */
680 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
681 chars_used
, !(format
& DT_NOPREFIX
));
685 pellip
->before
= j_in_seg
;
690 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
692 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
693 max_seg_width
, &size
, *p_retstr
, pellip
);
694 line_fits
= (size
.cx
<= max_seg_width
);
697 /* NB we may end up ellipsifying a word-broken or path_ellipsified
699 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
700 ((format
& DT_END_ELLIPSIS
) &&
701 ((last_line
&& *count
) ||
702 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
704 int before
, len_ellipsis
;
705 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
706 max_seg_width
, &size
, *p_retstr
, &before
, &len_ellipsis
);
707 if (before
> pellip
->before
)
709 /* We must have done a path ellipsis too */
710 pellip
->after
= before
- pellip
->before
- pellip
->len
;
711 /* Leave the len as the length of the first ellipsis */
715 /* If we are here after a path ellipsification it must be
716 * because even the ellipsis itself didn't fit.
718 assert (pellip
->under
== 0 && pellip
->after
== 0);
719 pellip
->before
= before
;
720 pellip
->len
= len_ellipsis
;
721 /* pellip->after remains as zero as does
725 line_fits
= (size
.cx
<= max_seg_width
);
728 /* As an optimisation if we have ellipsified and we are expanding
729 * tabs and we haven't reached the end of the line we can skip to it
730 * now rather than going around the loop again.
732 if ((format
& DT_EXPANDTABS
) && ellipsified
)
734 if (format
& DT_SINGLELINE
)
738 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
745 j
= seg_j
+ j_in_seg
;
746 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
748 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
749 if (*pprefix_offset
!= -1)
750 *pprefix_offset
+= seg_j
;
754 if (size
.cy
> retsize
->cy
)
755 retsize
->cy
= size
.cy
;
761 else if (str
[i
] == CR
|| str
[i
] == LF
)
764 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
770 /* else it was a Tab and we go around again */
782 /***********************************************************************
783 * TEXT_DrawUnderscore
785 * Draw the underline under the prefixed character
788 * hdc [in] The handle of the DC for drawing
789 * x [in] The x location of the line segment (logical coordinates)
790 * y [in] The y location of where the underscore should appear
791 * (logical coordinates)
792 * str [in] The text of the line segment
793 * offset [in] The offset of the underscored character within str
796 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
)
804 GetTextExtentPointW (hdc
, str
, offset
, &size
);
805 prefix_x
= x
+ size
.cx
;
806 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
807 prefix_end
= x
+ size
.cx
- 1;
808 /* The above method may eventually be slightly wrong due to kerning etc. */
810 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
811 oldPen
= SelectObject (hdc
, hpen
);
812 MoveToEx (hdc
, prefix_x
, y
, NULL
);
813 LineTo (hdc
, prefix_end
, y
);
814 SelectObject (hdc
, oldPen
);
818 /***********************************************************************
819 * DrawTextExW (USER32.@)
821 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
822 * is not quite complete, especially with regard to \0. We will assume that
823 * the returned string could have a length of up to i_count+3 and also have
824 * a trailing \0 (which would be 4 more than a not-null-terminated string but
825 * 3 more than a null-terminated string). If this is not so then increase
826 * the allowance in DrawTextExA.
828 #define MAX_STATIC_BUFFER 1024
829 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
830 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
834 WCHAR
*retstr
, *p_retstr
;
836 static WCHAR line
[MAX_STATIC_BUFFER
];
837 int len
, lh
, count
=i_count
;
839 int lmargin
= 0, rmargin
= 0;
840 int x
= rect
->left
, y
= rect
->top
;
841 int width
= rect
->right
- rect
->left
;
844 int tabwidth
/* to keep gcc happy */ = 0;
848 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str
, count
), count
,
849 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
851 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
852 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
855 if (count
== -1) count
= strlenW(str
);
856 if (count
== 0) return 0;
859 if (flags
& DT_SINGLELINE
)
860 flags
&= ~DT_WORDBREAK
;
862 GetTextMetricsW(hdc
, &tm
);
863 if (flags
& DT_EXTERNALLEADING
)
864 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
870 lmargin
= dtp
->iLeftMargin
* tm
.tmAveCharWidth
;
871 rmargin
= dtp
->iRightMargin
* tm
.tmAveCharWidth
;
872 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
874 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
877 if (flags
& DT_EXPANDTABS
)
879 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
) ? dtp
->iTabLength
: 8;
880 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
883 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
885 if (flags
& DT_MODIFYSTRING
)
887 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
888 retstr
= HeapAlloc(GetProcessHeap(), 0, size_retstr
);
889 if (!retstr
) return 0;
890 memcpy (retstr
, str
, size_retstr
);
901 len
= MAX_STATIC_BUFFER
;
902 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
903 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, &p_retstr
, tabwidth
, &prefix_offset
, &ellip
);
905 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
907 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
909 if (flags
& DT_SINGLELINE
)
911 if (flags
& DT_VCENTER
) y
= rect
->top
+
912 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
913 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
916 if (!(flags
& DT_CALCRECT
))
918 const WCHAR
*str
= line
;
924 if ((flags
& DT_EXPANDTABS
))
927 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
929 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
))
935 if (!ExtTextOutW( hdc
, xseg
, y
,
936 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
937 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
938 rect
, str
, len_seg
, NULL
)) return 0;
939 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
941 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
);
947 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
949 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
950 if (prefix_offset
!= -1)
952 if (prefix_offset
< len_seg
)
954 /* We have just drawn an underscore; we ought to
955 * figure out where the next one is. I am going
956 * to leave it for now until I have a better model
957 * for the line, which will make reprefixing easier.
958 * This is where ellip would be used.
963 prefix_offset
-= len_seg
;
968 else if (size
.cx
> max_width
)
973 dtp
->uiLengthDrawn
+= len
;
975 while (strPtr
&& !last_line
);
977 if (flags
& DT_CALCRECT
)
979 rect
->right
= rect
->left
+ max_width
;
982 rect
->right
+= lmargin
+ rmargin
;
986 memcpy (str
, retstr
, size_retstr
);
987 HeapFree (GetProcessHeap(), 0, retstr
);
989 return y
- rect
->top
;
992 /***********************************************************************
993 * DrawTextExA (USER32.@)
995 * If DT_MODIFYSTRING is specified then there must be room for up to
996 * 4 extra characters. We take great care about just how much modified
999 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1000 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1011 if (count
== -1) count
= strlen(str
);
1012 if (!count
) return 0;
1013 wcount
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
1016 if (flags
& DT_MODIFYSTRING
)
1021 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
1024 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, wcount
);
1025 if (flags
& DT_MODIFYSTRING
)
1026 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1027 /* Initialise the extra characters so that we can see which ones
1028 * change. U+FFFE is guaranteed to be not a unicode character and
1029 * so will not be generated by DrawTextEx itself.
1031 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, NULL
);
1032 if (flags
& DT_MODIFYSTRING
)
1034 /* Unfortunately the returned string may contain multiple \0s
1035 * and so we need to measure it ourselves.
1037 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1038 WideCharToMultiByte( CP_ACP
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1040 HeapFree(GetProcessHeap(), 0, wstr
);
1045 /***********************************************************************
1046 * DrawTextW (USER32.@)
1048 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1052 memset (&dtp
, 0, sizeof(dtp
));
1053 if (flags
& DT_TABSTOP
)
1055 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1056 flags
&= 0xffff00ff;
1058 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1061 /***********************************************************************
1062 * DrawTextA (USER32.@)
1064 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1068 memset (&dtp
, 0, sizeof(dtp
));
1069 if (flags
& DT_TABSTOP
)
1071 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1072 flags
&= 0xffff00ff;
1074 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1077 /***********************************************************************
1078 * DrawText (USER.85)
1080 INT16 WINAPI
DrawText16( HDC16 hdc
, LPCSTR str
, INT16 count
, LPRECT16 rect
, UINT16 flags
)
1087 CONV_RECT16TO32( rect
, &rect32
);
1088 ret
= DrawTextA( hdc
, str
, count
, &rect32
, flags
);
1089 CONV_RECT32TO16( &rect32
, rect
);
1091 else ret
= DrawTextA( hdc
, str
, count
, NULL
, flags
);
1096 /***********************************************************************
1098 * GrayString functions
1101 /* ### start build ### */
1102 extern WORD CALLBACK
TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16
,WORD
,LONG
,WORD
);
1103 /* ### stop build ### */
1105 struct gray_string_info
1107 GRAYSTRINGPROC16 proc
;
1111 /* callback for 16-bit gray string proc */
1112 static BOOL CALLBACK
gray_string_callback( HDC hdc
, LPARAM param
, INT len
)
1114 const struct gray_string_info
*info
= (struct gray_string_info
*)param
;
1115 return TEXT_CallTo16_word_wlw( info
->proc
, hdc
, info
->param
, len
);
1118 /***********************************************************************
1121 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1122 * heap and we can guarantee that the handles fit in an INT16. We have to
1123 * rethink the strategy once the migration to NT handles is complete.
1124 * We are going to get a lot of code-duplication once this migration is
1128 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1129 INT x
, INT y
, INT cx
, INT cy
, BOOL unicode
, BOOL _32bit
)
1131 HBITMAP hbm
, hbmsave
;
1139 if(!hdc
) return FALSE
;
1140 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1145 slen
= lstrlenW((LPCWSTR
)lp
);
1147 slen
= strlen((LPCSTR
)lp
);
1149 slen
= strlen(MapSL(lp
));
1152 if((cx
== 0 || cy
== 0) && slen
!= -1)
1156 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
1158 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
1160 GetTextExtentPoint32A(hdc
, MapSL(lp
), slen
, &s
);
1161 if(cx
== 0) cx
= s
.cx
;
1162 if(cy
== 0) cy
= s
.cy
;
1165 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1166 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
1167 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1168 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1169 SelectObject( memdc
, hbsave
);
1170 SetTextColor(memdc
, RGB(255, 255, 255));
1171 SetBkColor(memdc
, RGB(0, 0, 0));
1172 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1177 retval
= fn(memdc
, lp
, slen
);
1179 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
1184 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
1186 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
1188 TextOutA(memdc
, 0, 0, MapSL(lp
), slen
);
1191 SelectObject(memdc
, hfsave
);
1194 * Windows doc says that the bitmap isn't grayed when len == -1 and
1195 * the callback function returns FALSE. However, testing this on
1196 * win95 showed otherwise...
1198 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1199 if(retval
|| len
!= -1)
1202 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
1203 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1204 SelectObject(memdc
, hbsave
);
1207 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
1208 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1209 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1210 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1211 SetTextColor(hdc
, fg
);
1212 SetBkColor(hdc
, bg
);
1213 if(hb
) SelectObject(hdc
, hbsave
);
1215 SelectObject(memdc
, hbmsave
);
1222 /***********************************************************************
1223 * GrayString (USER.185)
1225 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
1226 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
1227 INT16 cx
, INT16 cy
)
1229 struct gray_string_info info
;
1231 if (!gsprc
) return TEXT_GrayString(hdc
, hbr
, NULL
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
1233 info
.param
= lParam
;
1234 return TEXT_GrayString( hdc
, hbr
, gray_string_callback
, (LPARAM
)&info
,
1235 cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
1239 /***********************************************************************
1240 * GrayStringA (USER32.@)
1242 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1243 LPARAM lParam
, INT cch
, INT x
, INT y
,
1246 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
,
1251 /***********************************************************************
1252 * GrayStringW (USER32.@)
1254 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1255 LPARAM lParam
, INT cch
, INT x
, INT y
,
1258 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
,
1262 /***********************************************************************
1264 * TabbedText functions
1267 /***********************************************************************
1268 * TEXT_TabbedTextOut
1270 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1271 * Note: this doesn't work too well for text-alignment modes other
1272 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1274 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
1275 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
1276 const INT
*lpTabPos32
, INT nTabOrg
,
1289 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
1295 GetTextMetricsA( hdc
, &tm
);
1296 defWidth
= 8 * tm
.tmAveCharWidth
;
1301 for (i
= 0; i
< count
; i
++)
1302 if (lpstr
[i
] == '\t') break;
1303 GetTextExtentPointA( hdc
, lpstr
, i
, &extent
);
1306 while ((cTabStops
> 0) &&
1307 (nTabOrg
+ *lpTabPos32
<= x
+ extent
.cx
))
1315 while ((cTabStops
> 0) &&
1316 (nTabOrg
+ *lpTabPos16
<= x
+ extent
.cx
))
1323 tabPos
= x
+ extent
.cx
;
1324 else if (cTabStops
> 0)
1325 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
1327 tabPos
= nTabOrg
+ ((x
+ extent
.cx
- nTabOrg
) / defWidth
+ 1) * defWidth
;
1334 r
.bottom
= y
+ extent
.cy
;
1335 ExtTextOutA( hdc
, x
, y
,
1336 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1337 &r
, lpstr
, i
, NULL
);
1343 return MAKELONG(tabPos
- start
, extent
.cy
);
1347 /***********************************************************************
1348 * TabbedTextOut (USER.196)
1350 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
1351 INT16 count
, INT16 cTabStops
,
1352 const INT16
*lpTabPos
, INT16 nTabOrg
)
1354 TRACE("%04x %d,%d %s %d\n", hdc
, x
, y
, debugstr_an(lpstr
,count
), count
);
1355 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
1356 lpTabPos
, NULL
, nTabOrg
, TRUE
);
1360 /***********************************************************************
1361 * TabbedTextOutA (USER32.@)
1363 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1364 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1366 TRACE("%04x %d,%d %s %d\n", hdc
, x
, y
, debugstr_an(lpstr
,count
), count
);
1367 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
1368 NULL
, lpTabPos
, nTabOrg
, TRUE
);
1372 /***********************************************************************
1373 * TabbedTextOutW (USER32.@)
1375 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1376 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1381 UINT codepage
= CP_ACP
; /* FIXME: get codepage of font charset */
1383 acount
= WideCharToMultiByte(codepage
,0,str
,count
,NULL
,0,NULL
,NULL
);
1384 p
= HeapAlloc( GetProcessHeap(), 0, acount
);
1385 if(p
== NULL
) return 0; /* FIXME: is this the correct return on failure */
1386 acount
= WideCharToMultiByte(codepage
,0,str
,count
,p
,acount
,NULL
,NULL
);
1387 ret
= TabbedTextOutA( hdc
, x
, y
, p
, acount
, cTabStops
, lpTabPos
, nTabOrg
);
1388 HeapFree( GetProcessHeap(), 0, p
);
1393 /***********************************************************************
1394 * GetTabbedTextExtent (USER.197)
1396 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
1397 INT16 cTabStops
, const INT16
*lpTabPos
)
1399 TRACE("%04x %s %d\n", hdc
, debugstr_an(lpstr
,count
), count
);
1400 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
1401 lpTabPos
, NULL
, 0, FALSE
);
1405 /***********************************************************************
1406 * GetTabbedTextExtentA (USER32.@)
1408 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1409 INT cTabStops
, const INT
*lpTabPos
)
1411 TRACE("%04x %s %d\n", hdc
, debugstr_an(lpstr
,count
), count
);
1412 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
1413 NULL
, lpTabPos
, 0, FALSE
);
1417 /***********************************************************************
1418 * GetTabbedTextExtentW (USER32.@)
1420 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1421 INT cTabStops
, const INT
*lpTabPos
)
1426 UINT codepage
= CP_ACP
; /* FIXME: get codepage of font charset */
1428 acount
= WideCharToMultiByte(codepage
,0,lpstr
,count
,NULL
,0,NULL
,NULL
);
1429 p
= HeapAlloc( GetProcessHeap(), 0, acount
);
1430 if(p
== NULL
) return 0; /* FIXME: is this the correct failure value? */
1431 acount
= WideCharToMultiByte(codepage
,0,lpstr
,count
,p
,acount
,NULL
,NULL
);
1432 ret
= GetTabbedTextExtentA( hdc
, p
, acount
, cTabStops
, lpTabPos
);
1433 HeapFree( GetProcessHeap(), 0, p
);