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"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(text
);
47 /*********************************************************************
52 * How many buffers to use
53 * While processing in DrawText there are potentially three different forms
54 * of the text that need to be held. How are they best held?
55 * 1. The original text is needed, of course, to see what to display.
56 * 2. The text that will be returned to the user if the DT_MODIFYSTRING is
58 * 3. The buffered text that is about to be displayed e.g. the current line.
59 * Typically this will exclude the ampersands used for prefixing etc.
62 * a. If the buffered text to be displayed includes the ampersands then
63 * we will need special measurement and draw functions that will ignore
64 * the ampersands (e.g. by copying to a buffer without the prefix and
65 * then using the normal forms). This may involve less space but may
66 * require more processing. e.g. since a line containing tabs may
67 * contain several underlined characters either we need to carry around
68 * a list of prefix locations or we may need to locate them several
70 * b. If we actually directly modify the "original text" as we go then we
71 * will need some special "caching" to handle the fact that when we
72 * ellipsify the text the ellipsis may modify the next line of text,
73 * which we have not yet processed. (e.g. ellipsification of a W at the
74 * end of a line will overwrite the W, the \n and the first character of
75 * the next line, and a \0 will overwrite the second. Try it!!)
77 * Option 1. Three separate storages. (To be implemented)
78 * If DT_MODIFYSTRING is in effect then allocate an extra buffer to hold
79 * the edited string in some form, either as the string itself or as some
80 * sort of "edit list" to be applied just before returning.
81 * Use a buffer that holds the ellipsified current line sans ampersands
82 * and accept the need occasionally to recalculate the prefixes (if
83 * DT_EXPANDTABS and not DT_NOPREFIX and not DT_HIDEPREFIX)
92 #define FORWARD_SLASH '/'
93 #define BACK_SLASH '\\'
95 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
97 typedef struct tag_ellipsis_data
105 /*********************************************************************
106 * TEXT_Ellipsify (static)
108 * Add an ellipsis to the end of the given string whilst ensuring it fits.
110 * If the ellipsis alone doesn't fit then it will be returned anyway.
112 * See Also TEXT_PathEllipsify
115 * hdc [in] The handle to the DC that defines the font.
116 * str [in/out] The string that needs to be modified.
117 * max_str [in] The dimension of str (number of WCHAR).
118 * len_str [in/out] The number of characters in str
119 * width [in] The maximum width permitted (in logical coordinates)
120 * size [out] The dimensions of the text
121 * modstr [out] The modified form of the string, to be returned to the
122 * calling program. It is assumed that the caller has
123 * made sufficient space available so we don't need to
124 * know the size of the space. This pointer may be NULL if
125 * the modified string is not required.
126 * len_before [out] The number of characters before the ellipsis.
127 * len_ellip [out] The number of characters in the ellipsis.
129 * See for example Microsoft article Q249678.
131 * For now we will simply use three dots rather than worrying about whether
132 * the font contains an explicit ellipsis character.
134 static void TEXT_Ellipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
135 unsigned int *len_str
, int width
, SIZE
*size
,
137 int *len_before
, int *len_ellip
)
139 unsigned int len_ellipsis
;
140 unsigned int lo
, mid
, hi
;
142 len_ellipsis
= strlenW (ELLIPSISW
);
143 if (len_ellipsis
> max_len
) len_ellipsis
= max_len
;
144 if (*len_str
> max_len
- len_ellipsis
)
145 *len_str
= max_len
- len_ellipsis
;
147 /* First do a quick binary search to get an upper bound for *len_str. */
149 GetTextExtentExPointW(hdc
, str
, *len_str
, width
, NULL
, NULL
, size
) &&
152 for (lo
= 0, hi
= *len_str
; lo
< hi
; )
155 if (!GetTextExtentExPointW(hdc
, str
, mid
, width
, NULL
, NULL
, size
))
157 if (size
->cx
> width
)
164 /* Now this should take only a couple iterations at most. */
167 strncpyW (str
+ *len_str
, ELLIPSISW
, len_ellipsis
);
169 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
170 NULL
, NULL
, size
)) break;
172 if (!*len_str
|| size
->cx
<= width
) break;
176 *len_ellip
= len_ellipsis
;
177 *len_before
= *len_str
;
178 *len_str
+= len_ellipsis
;
182 strncpyW (modstr
, str
, *len_str
);
183 *(str
+*len_str
) = '\0';
187 /*********************************************************************
188 * TEXT_PathEllipsify (static)
190 * Add an ellipsis to the provided string in order to make it fit within
191 * the width. The ellipsis is added as specified for the DT_PATH_ELLIPSIS
194 * See Also TEXT_Ellipsify
197 * hdc [in] The handle to the DC that defines the font.
198 * str [in/out] The string that needs to be modified
199 * max_str [in] The dimension of str (number of WCHAR).
200 * len_str [in/out] The number of characters in str
201 * width [in] The maximum width permitted (in logical coordinates)
202 * size [out] The dimensions of the text
203 * modstr [out] The modified form of the string, to be returned to the
204 * calling program. It is assumed that the caller has
205 * made sufficient space available so we don't need to
206 * know the size of the space. This pointer may be NULL if
207 * the modified string is not required.
208 * pellip [out] The ellipsification results
210 * For now we will simply use three dots rather than worrying about whether
211 * the font contains an explicit ellipsis character.
213 * The following applies, I think to Win95. We will need to extend it for
214 * Win98 which can have both path and end ellipsis at the same time (e.g.
215 * C:\MyLongFileName.Txt becomes ...\MyLongFileN...)
217 * The resulting string consists of as much as possible of the following:
218 * 1. The ellipsis itself
219 * 2. The last \ or / of the string (if any)
220 * 3. Everything after the last \ or / of the string (if any) or the whole
221 * string if there is no / or \. I believe that under Win95 this would
222 * include everything even though some might be clipped off the end whereas
223 * under Win98 that might be ellipsified too.
224 * Yet to be investigated is whether this would include wordbreaking if the
225 * filename is more than 1 word and splitting if DT_EDITCONTROL was in
226 * effect. (If DT_EDITCONTROL is in effect then on occasions text will be
227 * broken within words).
228 * 4. All the stuff before the / or \, which is placed before the ellipsis.
230 static void TEXT_PathEllipsify (HDC hdc
, WCHAR
*str
, unsigned int max_len
,
231 unsigned int *len_str
, int width
, SIZE
*size
,
232 WCHAR
*modstr
, ellipsis_data
*pellip
)
237 WCHAR
*lastBkSlash
, *lastFwdSlash
, *lastSlash
;
239 len_ellipsis
= strlenW (ELLIPSISW
);
240 if (!max_len
) return;
241 if (len_ellipsis
>= max_len
) len_ellipsis
= max_len
- 1;
242 if (*len_str
+ len_ellipsis
>= max_len
)
243 *len_str
= max_len
- len_ellipsis
-1;
244 /* Hopefully this will never happen, otherwise it would probably lose
245 * the wrong character
247 str
[*len_str
] = '\0'; /* to simplify things */
249 lastBkSlash
= strrchrW (str
, BACK_SLASH
);
250 lastFwdSlash
= strrchrW (str
, FORWARD_SLASH
);
251 lastSlash
= lastBkSlash
> lastFwdSlash
? lastBkSlash
: lastFwdSlash
;
252 if (!lastSlash
) lastSlash
= str
;
253 len_trailing
= *len_str
- (lastSlash
- str
);
255 /* overlap-safe movement to the right */
256 memmove (lastSlash
+len_ellipsis
, lastSlash
, len_trailing
* sizeof(WCHAR
));
257 strncpyW (lastSlash
, ELLIPSISW
, len_ellipsis
);
258 len_trailing
+= len_ellipsis
;
259 /* From this point on lastSlash actually points to the ellipsis in front
260 * of the last slash and len_trailing includes the ellipsis
266 if (!GetTextExtentExPointW (hdc
, str
, *len_str
+ len_ellipsis
, width
,
267 NULL
, NULL
, size
)) break;
269 if (lastSlash
== str
|| size
->cx
<= width
) break;
271 /* overlap-safe movement to the left */
272 memmove (lastSlash
-1, lastSlash
, len_trailing
* sizeof(WCHAR
));
279 pellip
->before
= lastSlash
-str
;
280 pellip
->len
= len_ellipsis
;
281 pellip
->under
= len_under
;
282 pellip
->after
= len_trailing
- len_ellipsis
;
283 *len_str
+= len_ellipsis
;
287 strncpyW (modstr
, str
, *len_str
);
288 *(str
+*len_str
) = '\0';
292 /*********************************************************************
293 * TEXT_WordBreak (static)
295 * Perform wordbreak processing on the given string
297 * Assumes that DT_WORDBREAK has been specified and not all the characters
298 * fit. Note that this function should even be called when the first character
299 * that doesn't fit is known to be a space or tab, so that it can swallow them.
301 * Note that the Windows processing has some strange properties.
302 * 1. If the text is left-justified and there is room for some of the spaces
303 * that follow the last word on the line then those that fit are included on
305 * 2. If the text is centred or right-justified and there is room for some of
306 * the spaces that follow the last word on the line then all but one of those
307 * that fit are included on the line.
308 * 3. (Reasonable behaviour) If the word breaking causes a space to be the first
309 * character of a new line it will be skipped.
312 * hdc [in] The handle to the DC that defines the font.
313 * str [in/out] The string that needs to be broken.
314 * max_str [in] The dimension of str (number of WCHAR).
315 * len_str [in/out] The number of characters in str
316 * width [in] The maximum width permitted
317 * format [in] The format flags in effect
318 * chars_fit [in] The maximum number of characters of str that are already
319 * known to fit; chars_fit+1 is known not to fit.
320 * chars_used [out] The number of characters of str that have been "used" and
321 * do not need to be included in later text. For example this will
322 * include any spaces that have been discarded from the start of
324 * size [out] The size of the returned text in logical coordinates
326 * Pedantic assumption - Assumes that the text length is monotonically
327 * increasing with number of characters (i.e. no weird kernings)
331 * Work back from the last character that did fit to either a space or the last
332 * character of a word, whichever is met first.
333 * If there was one or the first character didn't fit then
334 * If the text is centred or right justified and that one character was a
335 * space then break the line before that character
336 * Otherwise break the line after that character
337 * and if the next character is a space then discard it.
338 * Suppose there was none (and the first character did fit).
339 * If Break Within Word is permitted
340 * break the word after the last character that fits (there must be
341 * at least one; none is caught earlier).
343 * discard any trailing space.
344 * include the whole word; it may be ellipsified later
346 * Break Within Word is permitted under a set of circumstances that are not
347 * totally clear yet. Currently our best guess is:
348 * If DT_EDITCONTROL is in effect and neither DT_WORD_ELLIPSIS nor
349 * DT_PATH_ELLIPSIS is
352 static void TEXT_WordBreak (HDC hdc
, WCHAR
*str
, unsigned int max_str
,
353 unsigned int *len_str
,
354 int width
, int format
, unsigned int chars_fit
,
355 unsigned int *chars_used
, SIZE
*size
)
359 assert (format
& DT_WORDBREAK
);
360 assert (chars_fit
< *len_str
);
362 /* Work back from the last character that did fit to either a space or the
363 * last character of a word, whichever is met first.
365 p
= str
+ chars_fit
; /* The character that doesn't fit */
368 ; /* we pretend that it fits anyway */
369 else if (*p
== SPACE
) /* chars_fit < *len_str so this is valid */
370 p
--; /* the word just fitted */
373 while (p
> str
&& *(--p
) != SPACE
)
375 word_fits
= (p
!= str
|| *p
== SPACE
);
377 /* If there was one or the first character didn't fit then */
381 /* break the line before/after that character */
382 if (!(format
& (DT_RIGHT
| DT_CENTER
)) || *p
!= SPACE
)
384 next_is_space
= (p
- str
) < *len_str
&& *p
== SPACE
;
386 /* and if the next character is a space then discard it. */
387 *chars_used
= *len_str
;
391 /* Suppose there was none. */
394 if ((format
& (DT_EDITCONTROL
| DT_WORD_ELLIPSIS
| DT_PATH_ELLIPSIS
)) ==
397 /* break the word after the last character that fits (there must be
398 * at least one; none is caught earlier).
400 *len_str
= chars_fit
;
401 *chars_used
= chars_fit
;
403 /* FIXME - possible error. Since the next character is now removed
404 * this could make the text longer so that it no longer fits, and
405 * so we need a loop to test and shrink.
411 /* discard any trailing space. */
412 const WCHAR
*e
= str
+ *len_str
;
414 while (p
< e
&& *p
!= SPACE
)
416 *chars_used
= p
- str
;
417 if (p
< e
) /* i.e. loop failed because *p == SPACE */
420 /* include the whole word; it may be ellipsified later */
422 /* Possible optimisation; if DT_WORD_ELLIPSIS only use chars_fit+1
423 * so that it will be too long
427 /* Remeasure the string */
428 GetTextExtentExPointW (hdc
, str
, *len_str
, 0, NULL
, NULL
, size
);
431 /*********************************************************************
434 * Skip over the given number of characters, bearing in mind prefix
435 * substitution and the fact that a character may take more than one
436 * WCHAR (Unicode surrogates are two words long) (and there may have been
440 * new_count [out] The updated count
441 * new_str [out] The updated pointer
442 * start_count [in] The count of remaining characters corresponding to the
443 * start of the string
444 * start_str [in] The starting point of the string
445 * max [in] The number of characters actually in this segment of the
446 * string (the & counts)
447 * n [in] The number of characters to skip (if prefix then
449 * prefix [in] Apply prefix substitution
455 * There must be at least n characters in the string
456 * We need max because the "line" may have ended with a & followed by a tab
457 * or newline etc. which we don't want to swallow
460 static void TEXT_SkipChars (int *new_count
, const WCHAR
**new_str
,
461 int start_count
, const WCHAR
*start_str
,
462 int max
, int n
, int prefix
)
464 /* This is specific to wide characters, MSDN doesn't say anything much
465 * about Unicode surrogates yet and it isn't clear if _wcsinc will
466 * correctly handle them so we'll just do this the easy way for now
471 const WCHAR
*str_on_entry
= start_str
;
475 if (*start_str
++ == PREFIX
&& max
--)
478 start_count
-= (start_str
- str_on_entry
);
485 *new_str
= start_str
;
486 *new_count
= start_count
;
489 /*********************************************************************
492 * Reanalyse the text to find the prefixed character. This is called when
493 * wordbreaking or ellipsification has shortened the string such that the
494 * previously noted prefixed character is no longer visible.
497 * str [in] The original string segment (including all characters)
498 * ns [in] The number of characters in str (including prefixes)
499 * pe [in] The ellipsification data
502 * The prefix offset within the new string segment (the one that contains the
503 * ellipses and does not contain the prefix characters) (-1 if none)
506 static int TEXT_Reprefix (const WCHAR
*str
, unsigned int ns
,
507 const ellipsis_data
*pe
)
511 unsigned int n
= pe
->before
+ pe
->under
+ pe
->after
;
517 /* Reached the path ellipsis; jump over it */
518 if (ns
< pe
->under
) break;
522 if (!pe
->after
) break; /* Nothing after the path ellipsis */
526 if (*str
++ == PREFIX
)
530 result
= (i
< pe
->before
|| pe
->under
== 0) ? i
: i
- pe
->under
+ pe
->len
;
531 /* pe->len may be non-zero while pe_under is zero */
541 /*********************************************************************
542 * Returns true if and only if the remainder of the line is a single
543 * newline representation or nothing
546 static int remainder_is_none_or_newline (int num_chars
, const WCHAR
*str
)
548 if (!num_chars
) return TRUE
;
549 if (*str
!= LF
&& *str
!= CR
) return FALSE
;
550 if (!--num_chars
) return TRUE
;
551 if (*str
== *(str
+1)) return FALSE
;
553 if (*str
!= CR
&& *str
!= LF
) return FALSE
;
554 if (--num_chars
) return FALSE
;
558 /*********************************************************************
559 * Return next line of text from a string.
561 * hdc - handle to DC.
562 * str - string to parse into lines.
563 * count - length of str.
564 * dest - destination in which to return line.
565 * len - dest buffer size in chars on input, copied length into dest on output.
566 * width - maximum width of line in pixels.
567 * format - format type passed to DrawText.
568 * retsize - returned size of the line in pixels.
569 * last_line - TRUE if is the last line that will be processed
570 * p_retstr - If DT_MODIFYSTRING this points to a cursor in the buffer in which
571 * the return string is built.
572 * tabwidth - The width of a tab in logical coordinates
573 * pprefix_offset - Here is where we return the offset within dest of the first
574 * prefixed (underlined) character. -1 is returned if there
575 * are none. Note that there may be more; the calling code
576 * will need to use TEXT_Reprefix to find any later ones.
577 * pellip - Here is where we return the information about any ellipsification
578 * that was carried out. Note that if tabs are being expanded then
579 * this data will correspond to the last text segment actually
580 * returned in dest; by definition there would not have been any
581 * ellipsification in earlier text segments of the line.
583 * Returns pointer to next char in str after end of the line
584 * or NULL if end of str reached.
586 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
587 WCHAR
*dest
, int *len
, int width
, DWORD format
,
588 SIZE
*retsize
, int last_line
, WCHAR
**p_retstr
,
589 int tabwidth
, int *pprefix_offset
,
590 ellipsis_data
*pellip
)
596 int seg_i
, seg_count
, seg_j
;
603 *pprefix_offset
= -1;
605 /* For each text segment in the line */
611 /* Skip any leading tabs */
613 if (str
[i
] == TAB
&& (format
& DT_EXPANDTABS
))
615 plen
= ((plen
/tabwidth
)+1)*tabwidth
;
616 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
617 while (*count
&& str
[i
] == TAB
)
620 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
625 /* Now copy as far as the next tab or cr/lf or eos */
632 (str
[i
] != TAB
|| !(format
& DT_EXPANDTABS
)) &&
633 ((str
[i
] != CR
&& str
[i
] != LF
) || (format
& DT_SINGLELINE
)))
635 if (str
[i
] == PREFIX
&& !(format
& DT_NOPREFIX
) && *count
> 1)
637 (*count
)--, i
++; /* Throw away the prefix itself */
638 if (str
[i
] == PREFIX
)
640 /* Swallow it before we see it again */
641 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
643 else if (*pprefix_offset
== -1 || *pprefix_offset
>= seg_j
)
647 /* else the previous prefix was in an earlier segment of the
648 * line; we will leave it to the drawing code to catch this
654 (*count
)--; if (j
< maxl
) dest
[j
++] = str
[i
++]; else i
++;
659 /* Measure the whole text segment and possibly WordBreak and
663 j_in_seg
= j
- seg_j
;
664 max_seg_width
= width
- plen
;
665 GetTextExtentExPointW (hdc
, dest
+ seg_j
, j_in_seg
, max_seg_width
, &num_fit
, NULL
, &size
);
667 /* The Microsoft handling of various combinations of formats is weird.
668 * The following may very easily be incorrect if several formats are
669 * combined, and may differ between versions (to say nothing of the
670 * several bugs in the Microsoft versions).
673 line_fits
= (num_fit
>= j_in_seg
);
674 if (!line_fits
&& (format
& DT_WORDBREAK
))
678 TEXT_WordBreak (hdc
, dest
+seg_j
, maxl
-seg_j
, &j_in_seg
,
679 max_seg_width
, format
, num_fit
, &chars_used
, &size
);
680 line_fits
= (size
.cx
<= max_seg_width
);
681 /* and correct the counts */
682 TEXT_SkipChars (count
, &s
, seg_count
, str
+seg_i
, i
-seg_i
,
683 chars_used
, !(format
& DT_NOPREFIX
));
687 pellip
->before
= j_in_seg
;
692 if (!line_fits
&& (format
& DT_PATH_ELLIPSIS
))
694 TEXT_PathEllipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
695 max_seg_width
, &size
, *p_retstr
, pellip
);
696 line_fits
= (size
.cx
<= max_seg_width
);
699 /* NB we may end up ellipsifying a word-broken or path_ellipsified
701 if ((!line_fits
&& (format
& DT_WORD_ELLIPSIS
)) ||
702 ((format
& DT_END_ELLIPSIS
) &&
703 ((last_line
&& *count
) ||
704 (remainder_is_none_or_newline (*count
, &str
[i
]) && !line_fits
))))
706 int before
, len_ellipsis
;
707 TEXT_Ellipsify (hdc
, dest
+ seg_j
, maxl
-seg_j
, &j_in_seg
,
708 max_seg_width
, &size
, *p_retstr
, &before
, &len_ellipsis
);
709 if (before
> pellip
->before
)
711 /* We must have done a path ellipsis too */
712 pellip
->after
= before
- pellip
->before
- pellip
->len
;
713 /* Leave the len as the length of the first ellipsis */
717 /* If we are here after a path ellipsification it must be
718 * because even the ellipsis itself didn't fit.
720 assert (pellip
->under
== 0 && pellip
->after
== 0);
721 pellip
->before
= before
;
722 pellip
->len
= len_ellipsis
;
723 /* pellip->after remains as zero as does
727 line_fits
= (size
.cx
<= max_seg_width
);
730 /* As an optimisation if we have ellipsified and we are expanding
731 * tabs and we haven't reached the end of the line we can skip to it
732 * now rather than going around the loop again.
734 if ((format
& DT_EXPANDTABS
) && ellipsified
)
736 if (format
& DT_SINGLELINE
)
740 while ((*count
) && str
[i
] != CR
&& str
[i
] != LF
)
747 j
= seg_j
+ j_in_seg
;
748 if (*pprefix_offset
>= seg_j
+ pellip
->before
)
750 *pprefix_offset
= TEXT_Reprefix (str
+ seg_i
, i
- seg_i
, pellip
);
751 if (*pprefix_offset
!= -1)
752 *pprefix_offset
+= seg_j
;
756 if (size
.cy
> retsize
->cy
)
757 retsize
->cy
= size
.cy
;
763 else if (str
[i
] == CR
|| str
[i
] == LF
)
766 if (*count
&& (str
[i
] == CR
|| str
[i
] == LF
) && str
[i
] != str
[i
-1])
772 /* else it was a Tab and we go around again */
784 /***********************************************************************
785 * TEXT_DrawUnderscore
787 * Draw the underline under the prefixed character
790 * hdc [in] The handle of the DC for drawing
791 * x [in] The x location of the line segment (logical coordinates)
792 * y [in] The y location of where the underscore should appear
793 * (logical coordinates)
794 * str [in] The text of the line segment
795 * offset [in] The offset of the underscored character within str
796 * rect [in] Clipping rectangle (if not NULL)
799 static void TEXT_DrawUnderscore (HDC hdc
, int x
, int y
, const WCHAR
*str
, int offset
, const RECT
*rect
)
807 GetTextExtentPointW (hdc
, str
, offset
, &size
);
808 prefix_x
= x
+ size
.cx
;
809 GetTextExtentPointW (hdc
, str
, offset
+1, &size
);
810 prefix_end
= x
+ size
.cx
- 1;
811 /* The above method may eventually be slightly wrong due to kerning etc. */
813 /* Check for clipping */
815 if (prefix_x
> rect
->right
|| prefix_end
< rect
->left
|| y
< rect
->top
|| y
> rect
->bottom
)
816 return; /* Completely outside */
817 /* Partially outside */
818 if (prefix_x
< rect
->left
) prefix_x
= rect
->left
;
819 if (prefix_end
> rect
->right
) prefix_end
= rect
->right
;
822 hpen
= CreatePen (PS_SOLID
, 1, GetTextColor (hdc
));
823 oldPen
= SelectObject (hdc
, hpen
);
824 MoveToEx (hdc
, prefix_x
, y
, NULL
);
825 LineTo (hdc
, prefix_end
, y
);
826 SelectObject (hdc
, oldPen
);
830 /***********************************************************************
831 * DrawTextExW (USER32.@)
833 * The documentation on the extra space required for DT_MODIFYSTRING at MSDN
834 * is not quite complete, especially with regard to \0. We will assume that
835 * the returned string could have a length of up to i_count+3 and also have
836 * a trailing \0 (which would be 4 more than a not-null-terminated string but
837 * 3 more than a null-terminated string). If this is not so then increase
838 * the allowance in DrawTextExA.
840 #define MAX_STATIC_BUFFER 1024
841 INT WINAPI
DrawTextExW( HDC hdc
, LPWSTR str
, INT i_count
,
842 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
846 WCHAR
*retstr
, *p_retstr
;
848 static WCHAR line
[MAX_STATIC_BUFFER
];
849 int len
, lh
, count
=i_count
;
851 int lmargin
= 0, rmargin
= 0;
852 int x
= rect
->left
, y
= rect
->top
;
853 int width
= rect
->right
- rect
->left
;
856 int tabwidth
/* to keep gcc happy */ = 0;
860 TRACE("%s, %d, [(%ld,%ld),(%ld,%ld)]\n", debugstr_wn (str
, count
), count
,
861 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
863 if (dtp
) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
864 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
867 if (count
== -1) count
= strlenW(str
);
868 if (count
== 0) return 0;
871 if (flags
& DT_SINGLELINE
)
872 flags
&= ~DT_WORDBREAK
;
874 GetTextMetricsW(hdc
, &tm
);
875 if (flags
& DT_EXTERNALLEADING
)
876 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
882 lmargin
= dtp
->iLeftMargin
* tm
.tmAveCharWidth
;
883 rmargin
= dtp
->iRightMargin
* tm
.tmAveCharWidth
;
884 if (!(flags
& (DT_CENTER
| DT_RIGHT
)))
886 dtp
->uiLengthDrawn
= 0; /* This param RECEIVES number of chars processed */
889 if (flags
& DT_EXPANDTABS
)
891 int tabstop
= ((flags
& DT_TABSTOP
) && dtp
) ? dtp
->iTabLength
: 8;
892 tabwidth
= tm
.tmAveCharWidth
* tabstop
;
895 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
897 if (flags
& DT_MODIFYSTRING
)
899 size_retstr
= (count
+ 4) * sizeof (WCHAR
);
900 retstr
= HeapAlloc(GetProcessHeap(), 0, size_retstr
);
901 if (!retstr
) return 0;
902 memcpy (retstr
, str
, size_retstr
);
913 len
= MAX_STATIC_BUFFER
;
914 last_line
= !(flags
& DT_NOCLIP
) && y
+ ((flags
& DT_EDITCONTROL
) ? 2*lh
-1 : lh
) > rect
->bottom
;
915 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
, &size
, last_line
, &p_retstr
, tabwidth
, &prefix_offset
, &ellip
);
917 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
919 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
921 if (flags
& DT_SINGLELINE
)
923 if (flags
& DT_VCENTER
) y
= rect
->top
+
924 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
925 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
928 if (!(flags
& DT_CALCRECT
))
930 const WCHAR
*str
= line
;
936 if ((flags
& DT_EXPANDTABS
))
939 p
= str
; while (p
< str
+len
&& *p
!= TAB
) p
++;
941 if (len_seg
!= len
&& !GetTextExtentPointW(hdc
, str
, len_seg
, &size
))
947 if (!ExtTextOutW( hdc
, xseg
, y
,
948 ((flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
) |
949 ((flags
& DT_RTLREADING
) ? ETO_RTLREADING
: 0),
950 rect
, str
, len_seg
, NULL
)) return 0;
951 if (prefix_offset
!= -1 && prefix_offset
< len_seg
)
953 TEXT_DrawUnderscore (hdc
, xseg
, y
+ tm
.tmAscent
+ 1, str
, prefix_offset
, (flags
& DT_NOCLIP
) ? NULL
: rect
);
959 assert ((flags
& DT_EXPANDTABS
) && *str
== TAB
);
961 xseg
+= ((size
.cx
/tabwidth
)+1)*tabwidth
;
962 if (prefix_offset
!= -1)
964 if (prefix_offset
< len_seg
)
966 /* We have just drawn an underscore; we ought to
967 * figure out where the next one is. I am going
968 * to leave it for now until I have a better model
969 * for the line, which will make reprefixing easier.
970 * This is where ellip would be used.
975 prefix_offset
-= len_seg
;
980 else if (size
.cx
> max_width
)
985 dtp
->uiLengthDrawn
+= len
;
987 while (strPtr
&& !last_line
);
989 if (flags
& DT_CALCRECT
)
991 rect
->right
= rect
->left
+ max_width
;
994 rect
->right
+= lmargin
+ rmargin
;
998 memcpy (str
, retstr
, size_retstr
);
999 HeapFree (GetProcessHeap(), 0, retstr
);
1001 return y
- rect
->top
;
1004 /***********************************************************************
1005 * DrawTextExA (USER32.@)
1007 * If DT_MODIFYSTRING is specified then there must be room for up to
1008 * 4 extra characters. We take great care about just how much modified
1011 INT WINAPI
DrawTextExA( HDC hdc
, LPSTR str
, INT count
,
1012 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
1023 if (count
== -1) count
= strlen(str
);
1024 if (!count
) return 0;
1025 wcount
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
1028 if (flags
& DT_MODIFYSTRING
)
1033 wstr
= HeapAlloc(GetProcessHeap(), 0, wmax
* sizeof(WCHAR
));
1036 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, wcount
);
1037 if (flags
& DT_MODIFYSTRING
)
1038 for (i
=4, p
=wstr
+wcount
; i
--; p
++) *p
=0xFFFE;
1039 /* Initialise the extra characters so that we can see which ones
1040 * change. U+FFFE is guaranteed to be not a unicode character and
1041 * so will not be generated by DrawTextEx itself.
1043 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, dtp
);
1044 if (flags
& DT_MODIFYSTRING
)
1046 /* Unfortunately the returned string may contain multiple \0s
1047 * and so we need to measure it ourselves.
1049 for (i
=4, p
=wstr
+wcount
; i
-- && *p
!= 0xFFFE; p
++) wcount
++;
1050 WideCharToMultiByte( CP_ACP
, 0, wstr
, wcount
, str
, amax
, NULL
, NULL
);
1052 HeapFree(GetProcessHeap(), 0, wstr
);
1057 /***********************************************************************
1058 * DrawTextW (USER32.@)
1060 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
, LPRECT rect
, UINT flags
)
1064 memset (&dtp
, 0, sizeof(dtp
));
1065 if (flags
& DT_TABSTOP
)
1067 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1068 flags
&= 0xffff00ff;
1070 return DrawTextExW(hdc
, (LPWSTR
)str
, count
, rect
, flags
, &dtp
);
1073 /***********************************************************************
1074 * DrawTextA (USER32.@)
1076 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
1080 memset (&dtp
, 0, sizeof(dtp
));
1081 if (flags
& DT_TABSTOP
)
1083 dtp
.iTabLength
= (flags
>> 8) && 0xff;
1084 flags
&= 0xffff00ff;
1086 return DrawTextExA( hdc
, (LPSTR
)str
, count
, rect
, flags
, &dtp
);
1089 /***********************************************************************
1091 * GrayString functions
1094 /* callback for ASCII gray string proc */
1095 static BOOL CALLBACK
gray_string_callbackA( HDC hdc
, LPARAM param
, INT len
)
1097 return TextOutA( hdc
, 0, 0, (LPCSTR
)param
, len
);
1100 /* callback for Unicode gray string proc */
1101 static BOOL CALLBACK
gray_string_callbackW( HDC hdc
, LPARAM param
, INT len
)
1103 return TextOutW( hdc
, 0, 0, (LPCWSTR
)param
, len
);
1106 /***********************************************************************
1109 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1110 * heap and we can guarantee that the handles fit in an INT16. We have to
1111 * rethink the strategy once the migration to NT handles is complete.
1112 * We are going to get a lot of code-duplication once this migration is
1116 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
1117 INT x
, INT y
, INT cx
, INT cy
)
1119 HBITMAP hbm
, hbmsave
;
1127 if(!hdc
) return FALSE
;
1128 if (!(memdc
= CreateCompatibleDC(hdc
))) return FALSE
;
1130 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
1131 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
1132 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
1133 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
1134 SelectObject( memdc
, hbsave
);
1135 SetTextColor(memdc
, RGB(255, 255, 255));
1136 SetBkColor(memdc
, RGB(0, 0, 0));
1137 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
1139 retval
= fn(memdc
, lp
, slen
);
1140 SelectObject(memdc
, hfsave
);
1143 * Windows doc says that the bitmap isn't grayed when len == -1 and
1144 * the callback function returns FALSE. However, testing this on
1145 * win95 showed otherwise...
1147 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1148 if(retval
|| len
!= -1)
1151 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
1152 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
1153 SelectObject(memdc
, hbsave
);
1156 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
1157 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
1158 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
1159 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
1160 SetTextColor(hdc
, fg
);
1161 SetBkColor(hdc
, bg
);
1162 if(hb
) SelectObject(hdc
, hbsave
);
1164 SelectObject(memdc
, hbmsave
);
1171 /***********************************************************************
1172 * GrayStringA (USER32.@)
1174 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1175 LPARAM lParam
, INT cch
, INT x
, INT y
,
1178 if (!cch
) cch
= strlen( (LPCSTR
)lParam
);
1179 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1182 GetTextExtentPoint32A( hdc
, (LPCSTR
)lParam
, cch
, &s
);
1183 if (cx
== 0) cx
= s
.cx
;
1184 if (cy
== 0) cy
= s
.cy
;
1186 if (!gsprc
) gsprc
= gray_string_callbackA
;
1187 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1191 /***********************************************************************
1192 * GrayStringW (USER32.@)
1194 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
1195 LPARAM lParam
, INT cch
, INT x
, INT y
,
1198 if (!cch
) cch
= strlenW( (LPCWSTR
)lParam
);
1199 if ((cx
== 0 || cy
== 0) && cch
!= -1)
1202 GetTextExtentPoint32W( hdc
, (LPCWSTR
)lParam
, cch
, &s
);
1203 if (cx
== 0) cx
= s
.cx
;
1204 if (cy
== 0) cy
= s
.cy
;
1206 if (!gsprc
) gsprc
= gray_string_callbackW
;
1207 return TEXT_GrayString( hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
);
1211 /***********************************************************************
1212 * TEXT_TabbedTextOut
1214 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1215 * Note: this doesn't work too well for text-alignment modes other
1216 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1218 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCWSTR lpstr
,
1219 INT count
, INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
,
1233 if (cTabStops
== 1 && *lpTabPos
>= /* sic */ 0)
1235 defWidth
= *lpTabPos
;
1241 GetTextMetricsA( hdc
, &tm
);
1242 defWidth
= 8 * tm
.tmAveCharWidth
;
1244 cTabStops
= 0; /* on negative *lpTabPos */
1249 for (i
= 0; i
< count
; i
++)
1250 if (lpstr
[i
] == '\t') break;
1251 GetTextExtentPointW( hdc
, lpstr
, i
, &extent
);
1252 while ((cTabStops
> 0) &&
1253 (nTabOrg
+ *lpTabPos
<= x
+ extent
.cx
))
1259 tabPos
= x
+ extent
.cx
;
1260 else if (cTabStops
> 0)
1261 tabPos
= nTabOrg
+ *lpTabPos
;
1262 else if (defWidth
<= 0)
1263 tabPos
= x
+ extent
.cx
;
1265 tabPos
= nTabOrg
+ ((x
+ extent
.cx
- nTabOrg
) / defWidth
+ 1) * defWidth
;
1272 r
.bottom
= y
+ extent
.cy
;
1273 ExtTextOutW( hdc
, x
, y
, GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
1274 &r
, lpstr
, i
, NULL
);
1280 return MAKELONG(tabPos
- start
, extent
.cy
);
1284 /***********************************************************************
1285 * TabbedTextOutA (USER32.@)
1287 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
1288 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1291 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1292 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1293 if (!strW
) return 0;
1294 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1295 ret
= TabbedTextOutW( hdc
, x
, y
, strW
, len
, cTabStops
, lpTabPos
, nTabOrg
);
1296 HeapFree( GetProcessHeap(), 0, strW
);
1301 /***********************************************************************
1302 * TabbedTextOutW (USER32.@)
1304 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
1305 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
1307 TRACE("%p %d,%d %s %d\n", hdc
, x
, y
, debugstr_wn(str
,count
), count
);
1308 return TEXT_TabbedTextOut( hdc
, x
, y
, str
, count
, cTabStops
, lpTabPos
, nTabOrg
, TRUE
);
1312 /***********************************************************************
1313 * GetTabbedTextExtentA (USER32.@)
1315 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
1316 INT cTabStops
, const INT
*lpTabPos
)
1319 DWORD len
= MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, NULL
, 0 );
1320 LPWSTR strW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
1321 if (!strW
) return 0;
1322 MultiByteToWideChar( CP_ACP
, 0, lpstr
, count
, strW
, len
);
1323 ret
= GetTabbedTextExtentW( hdc
, strW
, len
, cTabStops
, lpTabPos
);
1324 HeapFree( GetProcessHeap(), 0, strW
);
1329 /***********************************************************************
1330 * GetTabbedTextExtentW (USER32.@)
1332 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
1333 INT cTabStops
, const INT
*lpTabPos
)
1335 TRACE("%p %s %d\n", hdc
, debugstr_wn(lpstr
,count
), count
);
1336 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
, lpTabPos
, 0, FALSE
);