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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/port.h"
38 #include "wine/winuser16.h"
39 #include "wine/unicode.h"
43 #include "user_private.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(text
);
48 /*********************************************************************
53 * How many buffers to use
54 * While processing in DrawText there are potentially three different forms
55 * of the text that need to be held. How are they best held?
56 * 1. The original text is needed, of course, to see what to display.
57 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
59 * 3. The buffered text that is about to be displayed e.g. the current line.
60 * Typically this will exclude the ampersands used for prefixing etc.
63 * a. If the buffered text to be displayed includes the ampersands then
64 * we will need special measurement and draw functions that will ignore
65 * the ampersands (e.g. by copying to a buffer without the prefix and
66 * then using the normal forms). This may involve less space but may
67 * require more processing. e.g. since a line containing tabs may
68 * contain several underlined characters either we need to carry around
69 * a list of prefix locations or we may need to locate them several
71 * b. If we actually directly modify the "original text" as we go then we
72 * will need some special "caching" to handle the fact that when we
73 * ellipsify the text the ellipsis may modify the next line of text,
74 * which we have not yet processed. (e.g. ellipsification of a W at the
75 * end of a line will overwrite the W, the \n and the first character of
76 * the next line, and a \0 will overwrite the second. Try it!!)
78 * Option 1. Three separate storages. (To be implemented)
79 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
80 * the edited string in some form, either as the string itself or as some
81 * sort of "edit list" to be applied just before returning.
82 * Use a buffer that holds the ellipsified current line sans ampersands
83 * and accept the need occasionally to recalculate the prefixes (if
84 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
93 #define FORWARD_SLASH '/'
94 #define BACK_SLASH '\\'
96 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
98 typedef struct tag_ellipsis_data
106 /*********************************************************************
107 * TEXT_Ellipsify (static)
109 * Add an ellipsis to the end of the given string whilst ensuring it fits.
111 * If the ellipsis alone doesn't fit then it will be returned anyway.
113 * See Also TEXT_PathEllipsify
116 * hdc [in] The handle to the DC that defines the font.
117 * str [in/out] The string that needs to be modified.
118 * max_str [in] The dimension of str (number of WCHAR).
119 * len_str [in/out] The number of characters in str
120 * width [in] The maximum width permitted (in logical coordinates)
121 * size [out] The dimensions of the text
122 * modstr [out] The modified form of the string, to be returned to the
123 * calling program. It is assumed that the caller has
124 * made sufficient space available so we don't need to
125 * know the size of the space. This pointer may be NULL if
126 * the modified string is not required.
127 * len_before [out] The number of characters before the ellipsis.
128 * len_ellip [out] The number of characters in the ellipsis.
130 * See for example Microsoft article Q249678.
132 * For now we will simply use three dots rather than worrying about whether
133 * the font contains an explicit ellipsis character.
135 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
136 unsigned int *len_str
, int width
, SIZE
*size
,
138 int *len_before
, int *len_ellip
)
140 unsigned int len_ellipsis
;
141 unsigned int lo
, mid
, hi
;
143 len_ellipsis
= strlenW (ELLIPSISW
);
144 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
145 if (*len_str
> max_len
- len_ellipsis
)
146 *len_str
= max_len
- len_ellipsis
;
148 /* First do a quick binary search to get an upper bound for *len_str. */
150 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
153 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
156 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
158 if (size
->cx
> width
)
165 /* Now this should take only a couple iterations at most. */
168 memcpy(str
+ *len_str
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
170 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
171 NULL
, NULL
, size
)) break;
173 if (!*len_str
|| size
->cx
<= width
) break;
177 *len_ellip
= len_ellipsis
;
178 *len_before
= *len_str
;
179 *len_str
+= len_ellipsis
;
183 memcpy (modstr
, str
, *len_str
* sizeof(WCHAR
));
184 *(str
+*len_str
) = '\0';
188 /*********************************************************************
189 * TEXT_PathEllipsify (static)
191 * Add an ellipsis to the provided string in order to make it fit within
192 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
195 * See Also TEXT_Ellipsify
198 * hdc [in] The handle to the DC that defines the font.
199 * str [in/out] The string that needs to be modified
200 * max_str [in] The dimension of str (number of WCHAR).
201 * len_str [in/out] The number of characters in str
202 * width [in] The maximum width permitted (in logical coordinates)
203 * size [out] The dimensions of the text
204 * modstr [out] The modified form of the string, to be returned to the
205 * calling program. It is assumed that the caller has
206 * made sufficient space available so we don't need to
207 * know the size of the space. This pointer may be NULL if
208 * the modified string is not required.
209 * pellip [out] The ellipsification results
211 * For now we will simply use three dots rather than worrying about whether
212 * the font contains an explicit ellipsis character.
214 * The following applies, I think to Win95. We will need to extend it for
215 * Win98 which can have both path and end ellipsis at the same time (e.g.
216 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
218 * The resulting string consists of as much as possible of the following:
219 * 1. The ellipsis itself
220 * 2. The last \ or / of the string (if any)
221 * 3. Everything after the last \ or / of the string (if any) or the whole
222 * string if there is no / or \. I believe that under Win95 this would
223 * include everything even though some might be clipped off the end whereas
224 * under Win98 that might be ellipsified too.
225 * Yet to be investigated is whether this would include wordbreaking if the
226 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
227 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
228 * broken within words).
229 * 4. All the stuff before the / or \, which is placed before the ellipsis.
231 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
232 unsigned int *len_str
, int width
, SIZE
*size
,
233 WCHAR
*modstr
, ellipsis_data
*pellip
)
238 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
240 len_ellipsis
= strlenW (ELLIPSISW
);
241 if (!max_len
) return;
242 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
243 if (*len_str
+ len_ellipsis
>= max_len
)
244 *len_str
= max_len
- len_ellipsis
-1;
245 /* Hopefully this will never happen, otherwise it would probably lose
246 * the wrong character
248 str
[*len_str
] = '\0'; /* to simplify things */
250 lastBkSlash
= strrchrW (str
, BACK_SLASH
);
251 lastFwdSlash
= strrchrW (str
, FORWARD_SLASH
);
252 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
253 if (!lastSlash
) lastSlash
= str
;
254 len_trailing
= *len_str
- (lastSlash
- str
);
256 /* overlap-safe movement to the right */
257 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
258 memcpy (lastSlash
, ELLIPSISW
, len_ellipsis
*sizeof(WCHAR
));
259 len_trailing
+= len_ellipsis
;
260 /* From this point on lastSlash actually points to the ellipsis in front
261 * of the last slash and len_trailing includes the ellipsis
267 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
268 NULL
, NULL
, size
)) break;
270 if (lastSlash
== str
|| size
->cx
<= width
) break;
272 /* overlap-safe movement to the left */
273 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
280 pellip
->before
= lastSlash
-str
;
281 pellip
->len
= len_ellipsis
;
282 pellip
->under
= len_under
;
283 pellip
->after
= len_trailing
- len_ellipsis
;
284 *len_str
+= len_ellipsis
;
288 memcpy(modstr
, str
, *len_str
* sizeof(WCHAR
));
289 modstr
[*len_str
] = '\0';
293 /*********************************************************************
294 * TEXT_WordBreak (static)
296 * Perform wordbreak processing on the given string
298 * Assumes that DT_WORDBREAK has been specified and not all the characters
299 * fit. Note that this function should even be called when the first character
300 * that doesn't fit is known to be a space or tab, so that it can swallow them.
302 * Note that the Windows processing has some strange properties.
303 * 1. If the text is left-justified and there is room for some of the spaces
304 * that follow the last word on the line then those that fit are included on
306 * 2. If the text is centred or right-justified and there is room for some of
307 * the spaces that follow the last word on the line then all but one of those
308 * that fit are included on the line.
309 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
310 * character of a new line it will be skipped.
313 * hdc [in] The handle to the DC that defines the font.
314 * str [in/out] The string that needs to be broken.
315 * max_str [in] The dimension of str (number of WCHAR).
316 * len_str [in/out] The number of characters in str
317 * width [in] The maximum width permitted
318 * format [in] The format flags in effect
319 * chars_fit [in] The maximum number of characters of str that are already
320 * known to fit; chars_fit+1 is known not to fit.
321 * chars_used [out] The number of characters of str that have been "used" and
322 * do not need to be included in later text. For example this will
323 * include any spaces that have been discarded from the start of
325 * size [out] The size of the returned text in logical coordinates
327 * Pedantic assumption - Assumes that the text length is monotonically
328 * increasing with number of characters (i.e. no weird kernings)
332 * Work back from the last character that did fit to either a space or the last
333 * character of a word, whichever is met first.
334 * If there was one or the first character didn't fit then
335 * If the text is centred or right justified and that one character was a
336 * space then break the line before that character
337 * Otherwise break the line after that character
338 * and if the next character is a space then discard it.
339 * Suppose there was none (and the first character did fit).
340 * If Break Within Word is permitted
341 * break the word after the last character that fits (there must be
342 * at least one; none is caught earlier).
344 * discard any trailing space.
345 * include the whole word; it may be ellipsified later
347 * Break Within Word is permitted under a set of circumstances that are not
348 * totally clear yet. Currently our best guess is:
349 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
350 * DT_PATH_ELLIPSIS is
353 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
354 unsigned int *len_str
,
355 int width
, int format
, unsigned int chars_fit
,
356 unsigned int *chars_used
, SIZE
*size
)
360 assert (format
& DT_WORDBREAK
);
361 assert (chars_fit
< *len_str
);
363 /* Work back from the last character that did fit to either a space or the
364 * last character of a word, whichever is met first.
366 p
= str
+ chars_fit
; /* The character that doesn't fit */
369 ; /* we pretend that it fits anyway */
370 else if (*p
== SPACE
) /* chars_fit < *len_str so this is valid */
371 p
--; /* the word just fitted */
374 while (p
> str
&& *(--p
) != SPACE
)
376 word_fits
= (p
!= str
|| *p
== SPACE
);
378 /* If there was one or the first character didn't fit then */
382 /* break the line before/after that character */
383 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
385 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
387 /* and if the next character is a space then discard it. */
388 *chars_used
= *len_str
;
392 /* Suppose there was none. */
395 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
398 /* break the word after the last character that fits (there must be
399 * at least one; none is caught earlier).
401 *len_str
= chars_fit
;
402 *chars_used
= chars_fit
;
404 /* FIXME - possible error. Since the next character is now removed
405 * this could make the text longer so that it no longer fits, and
406 * so we need a loop to test and shrink.
412 /* discard any trailing space. */
413 const WCHAR
*e
= str
+ *len_str
;
415 while (p
< e
&& *p
!= SPACE
)
417 *chars_used
= p
- str
;
418 if (p
< e
) /* i.e. loop failed because *p == SPACE */
421 /* include the whole word; it may be ellipsified later */
423 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
424 * so that it will be too long
428 /* Remeasure the string */
429 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
432 /*********************************************************************
435 * Skip over the given number of characters, bearing in mind prefix
436 * substitution and the fact that a character may take more than one
437 * WCHAR (Unicode surrogates are two words long) (and there may have been
441 * new_count [out] The updated count
442 * new_str [out] The updated pointer
443 * start_count [in] The count of remaining characters corresponding to the
444 * start of the string
445 * start_str [in] The starting point of the string
446 * max [in] The number of characters actually in this segment of the
447 * string (the & counts)
448 * n [in] The number of characters to skip (if prefix then
450 * prefix [in] Apply prefix substitution
456 * There must be at least n characters in the string
457 * We need max because the "line" may have ended with a & followed by a tab
458 * or newline etc. which we don't want to swallow
461 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
462 int start_count
, const WCHAR
*start_str
,
463 int max
, int n
, int prefix
)
465 /* This is specific to wide characters, MSDN doesn't say anything much
466 * about Unicode surrogates yet and it isn't clear if _wcsinc will
467 * correctly handle them so we'll just do this the easy way for now
472 const WCHAR
*str_on_entry
= start_str
;
477 if (*start_str
++ == PREFIX
&& max
--)
480 start_count
-= (start_str
- str_on_entry
);
487 *new_str
= start_str
;
488 *new_count
= start_count
;
491 /*********************************************************************
494 * Reanalyse the text to find the prefixed character. This is called when
495 * wordbreaking or ellipsification has shortened the string such that the
496 * previously noted prefixed character is no longer visible.
499 * str [in] The original string segment (including all characters)
500 * ns [in] The number of characters in str (including prefixes)
501 * pe [in] The ellipsification data
504 * The prefix offset within the new string segment (the one that contains the
505 * ellipses and does not contain the prefix characters) (-1 if none)
508 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
509 const ellipsis_data
*pe
)
513 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
519 /* Reached the path ellipsis; jump over it */
520 if (ns
< pe
->under
) break;
524 if (!pe
->after
) break; /* Nothing after the path ellipsis */
528 if (*str
++ == PREFIX
)
532 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
533 /* pe->len may be non-zero while pe_under is zero */
542 /*********************************************************************
543 * Returns true if and only if the remainder of the line is a single
544 * newline representation or nothing
547 static int remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
549 if (!num_chars
) return TRUE
;
550 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
551 if (!--num_chars
) return TRUE
;
552 if (*str
== *(str
+1)) return FALSE
;
554 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
555 if (--num_chars
) return FALSE
;
559 /*********************************************************************
560 * Return next line of text from a string.
562 * hdc - handle to DC.
563 * str - string to parse into lines.
564 * count - length of str.
565 * dest - destination in which to return line.
566 * len - dest buffer size in chars on input, copied length into dest on output.
567 * width - maximum width of line in pixels.
568 * format - format type passed to DrawText.
569 * retsize - returned size of the line in pixels.
570 * last_line - TRUE if is the last line that will be processed
571 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
572 * the return string is built.
573 * tabwidth - The width of a tab in logical coordinates
574 * pprefix_offset - Here is where we return the offset within dest of the first
575 * prefixed (underlined) character. -1 is returned if there
576 * are none. Note that there may be more; the calling code
577 * will need to use TEXT_Reprefix to find any later ones.
578 * pellip - Here is where we return the information about any ellipsification
579 * that was carried out. Note that if tabs are being expanded then
580 * this data will correspond to the last text segment actually
581 * returned in dest; by definition there would not have been any
582 * ellipsification in earlier text segments of the line.
584 * Returns pointer to next char in str after end of the line
585 * or NULL if end of str reached.
587 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
588 WCHAR
*dest
, int *len
, int width
, DWORD format
,
589 SIZE
*retsize
, int last_line
, WCHAR
**p_retstr
,
590 int tabwidth
, int *pprefix_offset
,
591 ellipsis_data
*pellip
)
597 int seg_i
, seg_count
, seg_j
;
602 unsigned int j_in_seg
;
604 *pprefix_offset
= -1;
606 /* For each text segment in the line */
612 /* Skip any leading tabs */
614 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
616 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
617 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
618 while (*count
&& str
[i
] == TAB
)
621 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
626 /* Now copy as far as the next tab or cr/lf or eos */
633 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
634 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
636 if (str
[i
] == PREFIX
&& !(format
& DT_NOPREFIX
) && *count
> 1)
638 (*count
)--, i
++; /* Throw away the prefix itself */
639 if (str
[i
] == PREFIX
)
641 /* Swallow it before we see it again */
642 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
644 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
648 /* else the previous prefix was in an earlier segment of the
649 * line; we will leave it to the drawing code to catch this
655 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
660 /* Measure the whole text segment and possibly WordBreak and
664 j_in_seg
= j
- seg_j
;
665 max_seg_width
= width
- plen
;
666 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
668 /* The Microsoft handling of various combinations of formats is weird.
669 * The following may very easily be incorrect if several formats are
670 * combined, and may differ between versions (to say nothing of the
671 * several bugs in the Microsoft versions).
674 line_fits
= (num_fit
>= j_in_seg
);
675 if (!line_fits
&& (format
& DT_WORDBREAK
))
678 unsigned int chars_used
;
679 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
680 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
681 line_fits
= (size
.cx
<= max_seg_width
);
682 /* and correct the counts */
683 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
684 chars_used
, !(format
& DT_NOPREFIX
));
688 pellip
->before
= j_in_seg
;
693 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
695 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
696 max_seg_width
, &size
, *p_retstr
, pellip
);
697 line_fits
= (size
.cx
<= max_seg_width
);
700 /* NB we may end up ellipsifying a word-broken or path_ellipsified
702 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
703 ((format
& DT_END_ELLIPSIS
) &&
704 ((last_line
&& *count
) ||
705 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
707 int before
, len_ellipsis
;
708 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
709 max_seg_width
, &size
, *p_retstr
, &before
, &len_ellipsis
);
710 if (before
> pellip
->before
)
712 /* We must have done a path ellipsis too */
713 pellip
->after
= before
- pellip
->before
- pellip
->len
;
714 /* Leave the len as the length of the first ellipsis */
718 /* If we are here after a path ellipsification it must be
719 * because even the ellipsis itself didn't fit.
721 assert (pellip
->under
== 0 && pellip
->after
== 0);
722 pellip
->before
= before
;
723 pellip
->len
= len_ellipsis
;
724 /* pellip->after remains as zero as does
728 line_fits
= (size
.cx
<= max_seg_width
);
731 /* As an optimisation if we have ellipsified and we are expanding
732 * tabs and we haven't reached the end of the line we can skip to it
733 * now rather than going around the loop again.
735 if ((format
& DT_EXPANDTABS
) && ellipsified
)
737 if (format
& DT_SINGLELINE
)
741 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
748 j
= seg_j
+ j_in_seg
;
749 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
751 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
752 if (*pprefix_offset
!= -1)
753 *pprefix_offset
+= seg_j
;
757 if (size
.cy
> retsize
->cy
)
758 retsize
->cy
= size
.cy
;
764 else if (str
[i
] == CR
|| str
[i
] == LF
)
767 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
773 /* else it was a Tab and we go around again */
785 /***********************************************************************
786 * TEXT_DrawUnderscore
788 * Draw the underline under the prefixed character
791 * hdc [in] The handle of the DC for drawing
792 * x [in] The x location of the line segment (logical coordinates)
793 * y [in] The y location of where the underscore should appear
794 * (logical coordinates)
795 * str [in] The text of the line segment
796 * offset [in] The offset of the underscored character within str
797 * rect [in] Clipping rectangle (if not NULL)
800 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
, const RECT
*rect
)
808 GetTextExtentPointW (hdc
, str
, offset
, &size
);
809 prefix_x
= x
+ size
.cx
;
810 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
811 prefix_end
= x
+ size
.cx
- 1;
812 /* The above method may eventually be slightly wrong due to kerning etc. */
814 /* Check for clipping */
816 if (prefix_x
> rect
->right
|| prefix_end
< rect
->left
|| y
< rect
->top
|| y
> rect
->bottom
)
817 return; /* Completely outside */
818 /* Partially outside */
819 if (prefix_x
< rect
->left
) prefix_x
= rect
->left
;
820 if (prefix_end
> rect
->right
) prefix_end
= rect
->right
;
823 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
824 oldPen
= SelectObject (hdc
, hpen
);
825 MoveToEx (hdc
, prefix_x
, y
, NULL
);
826 LineTo (hdc
, prefix_end
, y
);
827 SelectObject (hdc
, oldPen
);
831 /***********************************************************************
832 * DrawTextExW (USER32.@)
834 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
835 * is not quite complete, especially with regard to \0. We will assume that
836 * the returned string could have a length of up to i_count+3 and also have
837 * a trailing \0 (which would be 4 more than a not-null-terminated string but
838 * 3 more than a null-terminated string). If this is not so then increase
839 * the allowance in DrawTextExA.
841 #define MAX_BUFFER 1024
842 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
843 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
847 WCHAR
*retstr
, *p_retstr
;
849 WCHAR line
[MAX_BUFFER
];
850 int len
, lh
, count
=i_count
;
852 int lmargin
= 0, rmargin
= 0;
853 int x
= rect
->left
, y
= rect
->top
;
854 int width
= rect
->right
- rect
->left
;
857 int tabwidth
/* to keep gcc happy */ = 0;
861 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str
, count
), count
,
862 wine_dbgstr_rect(rect
), flags
);
864 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
865 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
867 if (!str
|| count
== 0) return 0;
870 count
= strlenW(str
);
873 if( flags
& DT_CALCRECT
)
875 rect
->right
= rect
->left
;
876 rect
->bottom
= rect
->top
;
883 if (flags
& DT_SINGLELINE
)
884 flags
&= ~DT_WORDBREAK
;
886 GetTextMetricsW(hdc
, &tm
);
887 if (flags
& DT_EXTERNALLEADING
)
888 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
894 lmargin
= dtp
->iLeftMargin
* tm
.tmAveCharWidth
;
895 rmargin
= dtp
->iRightMargin
* tm
.tmAveCharWidth
;
896 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
898 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
901 if (flags
& DT_EXPANDTABS
)
903 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
) ? dtp
->iTabLength
: 8;
904 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
907 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
909 if (flags
& DT_MODIFYSTRING
)
911 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
912 retstr
= HeapAlloc(GetProcessHeap(), 0, size_retstr
);
913 if (!retstr
) return 0;
914 memcpy (retstr
, str
, size_retstr
);
925 len
= sizeof(line
)/sizeof(line
[0]);
926 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
927 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, &p_retstr
, tabwidth
, &prefix_offset
, &ellip
);
929 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
931 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
933 if (flags
& DT_SINGLELINE
)
935 if (flags
& DT_VCENTER
) y
= rect
->top
+
936 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
937 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
940 if (!(flags
& DT_CALCRECT
))
942 const WCHAR
*str
= line
;
948 if ((flags
& DT_EXPANDTABS
))
951 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
953 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
))
959 if (!ExtTextOutW( hdc
, xseg
, y
,
960 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
961 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
962 rect
, str
, len_seg
, NULL
)) return 0;
963 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
965 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
, (flags
& DT_NOCLIP
) ? NULL
: rect
);
971 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
973 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
974 if (prefix_offset
!= -1)
976 if (prefix_offset
< len_seg
)
978 /* We have just drawn an underscore; we ought to
979 * figure out where the next one is. I am going
980 * to leave it for now until I have a better model
981 * for the line, which will make reprefixing easier.
982 * This is where ellip would be used.
987 prefix_offset
-= len_seg
;
992 else if (size
.cx
> max_width
)
997 dtp
->uiLengthDrawn
+= len
;
999 while (strPtr
&& !last_line
);
1001 if (flags
& DT_CALCRECT
)
1003 rect
->right
= rect
->left
+ max_width
;
1006 rect
->right
+= lmargin
+ rmargin
;
1010 memcpy (str
, retstr
, size_retstr
);
1011 HeapFree (GetProcessHeap(), 0, retstr
);
1013 return y
- rect
->top
;
1016 /***********************************************************************
1017 * DrawTextExA (USER32.@)
1019 * If DT_MODIFYSTRING is specified then there must be room for up to
1020 * 4 extra characters. We take great care about just how much modified
1023 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1024 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1034 if (!count
) return 0;
1035 if( !str
|| ((count
== -1) && !(count
= strlen(str
))))
1037 if( flags
& DT_CALCRECT
)
1039 rect
->right
= rect
->left
;
1040 rect
->bottom
= rect
->top
;
1044 wcount
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
1047 if (flags
& DT_MODIFYSTRING
)
1052 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
1055 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, wcount
);
1056 if (flags
& DT_MODIFYSTRING
)
1057 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1058 /* Initialise the extra characters so that we can see which ones
1059 * change. U+FFFE is guaranteed to be not a unicode character and
1060 * so will not be generated by DrawTextEx itself.
1062 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, dtp
);
1063 if (flags
& DT_MODIFYSTRING
)
1065 /* Unfortunately the returned string may contain multiple \0s
1066 * and so we need to measure it ourselves.
1068 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1069 WideCharToMultiByte( CP_ACP
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1071 HeapFree(GetProcessHeap(), 0, wstr
);
1076 /***********************************************************************
1077 * DrawTextW (USER32.@)
1079 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1083 memset (&dtp
, 0, sizeof(dtp
));
1084 if (flags
& DT_TABSTOP
)
1086 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1087 flags
&= 0xffff00ff;
1089 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1092 /***********************************************************************
1093 * DrawTextA (USER32.@)
1095 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1099 memset (&dtp
, 0, sizeof(dtp
));
1100 if (flags
& DT_TABSTOP
)
1102 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1103 flags
&= 0xffff00ff;
1105 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1108 /***********************************************************************
1110 * GrayString functions
1113 /* callback for ASCII gray string proc */
1114 static BOOL CALLBACK
gray_string_callbackA( HDC hdc
, LPARAM param
, INT len
)
1116 return TextOutA( hdc
, 0, 0, (LPCSTR
)param
, len
);
1119 /* callback for Unicode gray string proc */
1120 static BOOL CALLBACK
gray_string_callbackW( HDC hdc
, LPARAM param
, INT len
)
1122 return TextOutW( hdc
, 0, 0, (LPCWSTR
)param
, len
);
1125 /***********************************************************************
1128 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1129 INT x
, INT y
, INT cx
, INT cy
)
1131 HBITMAP hbm
, hbmsave
;
1139 if(!hdc
) return FALSE
;
1140 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1142 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1143 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
1144 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1145 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1146 SelectObject( memdc
, hbsave
);
1147 SetTextColor(memdc
, RGB(255, 255, 255));
1148 SetBkColor(memdc
, RGB(0, 0, 0));
1149 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1151 retval
= fn(memdc
, lp
, slen
);
1152 SelectObject(memdc
, hfsave
);
1155 * Windows doc says that the bitmap isn't grayed when len == -1 and
1156 * the callback function returns FALSE. However, testing this on
1157 * win95 showed otherwise...
1159 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1160 if(retval
|| len
!= -1)
1163 hbsave
= (HBRUSH
)SelectObject(memdc
, SYSCOLOR_55AABrush
);
1164 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1165 SelectObject(memdc
, hbsave
);
1168 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
1169 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1170 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1171 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1172 SetTextColor(hdc
, fg
);
1173 SetBkColor(hdc
, bg
);
1174 if(hb
) SelectObject(hdc
, hbsave
);
1176 SelectObject(memdc
, hbmsave
);
1183 /***********************************************************************
1184 * GrayStringA (USER32.@)
1186 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1187 LPARAM lParam
, INT cch
, INT x
, INT y
,
1190 if (!cch
) cch
= strlen( (LPCSTR
)lParam
);
1191 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1194 GetTextExtentPoint32A( hdc
, (LPCSTR
)lParam
, cch
, &s
);
1195 if (cx
== 0) cx
= s
.cx
;
1196 if (cy
== 0) cy
= s
.cy
;
1198 if (!gsprc
) gsprc
= gray_string_callbackA
;
1199 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1203 /***********************************************************************
1204 * GrayStringW (USER32.@)
1206 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1207 LPARAM lParam
, INT cch
, INT x
, INT y
,
1210 if (!cch
) cch
= strlenW( (LPCWSTR
)lParam
);
1211 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1214 GetTextExtentPoint32W( hdc
, (LPCWSTR
)lParam
, cch
, &s
);
1215 if (cx
== 0) cx
= s
.cx
;
1216 if (cy
== 0) cy
= s
.cy
;
1218 if (!gsprc
) gsprc
= gray_string_callbackW
;
1219 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1223 /***********************************************************************
1224 * TEXT_TabbedTextOut
1226 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1227 * Note: this doesn't work too well for text-alignment modes other
1228 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1230 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCWSTR lpstr
,
1231 INT count
, INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
,
1244 defWidth
= *lpTabPos
;
1250 GetTextMetricsA( hdc
, &tm
);
1251 defWidth
= 8 * tm
.tmAveCharWidth
;
1260 /* chop the string into substrings of 0 or more <tabs>
1261 * possibly followed by 1 or more normal characters */
1262 for (i
= 0; i
< count
; i
++)
1263 if (lpstr
[i
] != '\t') break;
1264 for (j
= i
; j
< count
; j
++)
1265 if (lpstr
[j
] == '\t') break;
1266 /* get the extent of the normal character part */
1267 GetTextExtentPointW( hdc
, lpstr
+ i
, j
- i
, &extent
);
1268 /* and if there is a <tab>, calculate its position */
1270 /* get x coordinate for the drawing of this string */
1271 for (; cTabStops
> i
; lpTabPos
++, cTabStops
--)
1273 if( nTabOrg
+ abs( *lpTabPos
) > x
) {
1274 if( lpTabPos
[ i
- 1] >= 0) {
1275 /* a left aligned tab */
1276 x
= nTabOrg
+ lpTabPos
[ i
-1] + extent
.cx
;
1281 /* if tab pos is negative then text is right-aligned
1282 * to tab stop meaning that the string extends to the
1283 * left, so we must subtract the width of the string */
1284 if (nTabOrg
- lpTabPos
[ i
- 1] - extent
.cx
> x
)
1286 x
= nTabOrg
- lpTabPos
[ i
- 1];
1293 /* if we have run out of tab stops and we have a valid default tab
1294 * stop width then round x up to that width */
1295 if ((cTabStops
<= i
) && (defWidth
> 0)) {
1296 x0
= nTabOrg
+ ((x
- nTabOrg
) / defWidth
+ i
) * defWidth
;
1298 } else if ((cTabStops
<= i
) && (defWidth
< 0)) {
1299 x
= nTabOrg
+ ((x
- nTabOrg
+ extent
.cx
) / -defWidth
+ i
)
1310 r
.bottom
= y
+ extent
.cy
;
1311 ExtTextOutW( hdc
, x0
, y
, GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1312 &r
, lpstr
+ i
, j
- i
, NULL
);
1317 return MAKELONG(x
- start
, extent
.cy
);
1321 /***********************************************************************
1322 * TabbedTextOutA (USER32.@)
1324 * See TabbedTextOutW.
1326 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1327 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1330 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1331 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1332 if (!strW
) return 0;
1333 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1334 ret
= TabbedTextOutW( hdc
, x
, y
, strW
, len
, cTabStops
, lpTabPos
, nTabOrg
);
1335 HeapFree( GetProcessHeap(), 0, strW
);
1340 /***********************************************************************
1341 * TabbedTextOutW (USER32.@)
1343 * Draws tabbed text aligned using the specified tab stops.
1346 * hdc [I] Handle to device context to draw to.
1347 * x [I] X co-ordinate to start drawing the text at in logical units.
1348 * y [I] Y co-ordinate to start drawing the text at in logical units.
1349 * str [I] Pointer to the characters to draw.
1350 * count [I] Number of WCHARs pointed to by str.
1351 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1352 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1353 * nTabOrg [I] Starting position to expand tabs from in logical units.
1356 * The dimensions of the string drawn. The height is in the high-order word
1357 * and the width is in the low-order word.
1360 * The tabs stops can be negative, in which case the text is right aligned to
1361 * that tab stop and, despite what MSDN says, this is supported on
1365 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1366 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1368 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1369 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1371 TRACE("%p %d,%d %s %d\n", hdc
, x
, y
, debugstr_wn(str
,count
), count
);
1372 return TEXT_TabbedTextOut( hdc
, x
, y
, str
, count
, cTabStops
, lpTabPos
, nTabOrg
, TRUE
);
1376 /***********************************************************************
1377 * GetTabbedTextExtentA (USER32.@)
1379 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1380 INT cTabStops
, const INT
*lpTabPos
)
1383 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1384 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1385 if (!strW
) return 0;
1386 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1387 ret
= GetTabbedTextExtentW( hdc
, strW
, len
, cTabStops
, lpTabPos
);
1388 HeapFree( GetProcessHeap(), 0, strW
);
1393 /***********************************************************************
1394 * GetTabbedTextExtentW (USER32.@)
1396 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1397 INT cTabStops
, const INT
*lpTabPos
)
1399 TRACE("%p %s %d\n", hdc
, debugstr_wn(lpstr
,count
), count
);
1400 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
, lpTabPos
, 0, FALSE
);