crypt32: Add a few tests for decoded message parameters.
[wine/wine-kai.git] / dlls / user32 / text.c
blobe6d21ca04683d9d00e390f120caee9e3f31311fb
1 /*
2 * USER text functions
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 2002 Bill Medland
7 * Contains
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
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wingdi.h"
38 #include "wine/winuser16.h"
39 #include "wine/unicode.h"
40 #include "winnls.h"
41 #include "controls.h"
42 #include "user_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(text);
47 /*********************************************************************
49 * DrawText functions
51 * Design issues
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
57 * in effect.
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.
61 * Complications.
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
69 * times.
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)
86 #define TAB 9
87 #define LF 10
88 #define CR 13
89 #define SPACE 32
90 #define PREFIX 38
92 #define FORWARD_SLASH '/'
93 #define BACK_SLASH '\\'
95 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
97 typedef struct tag_ellipsis_data
99 int before;
100 int len;
101 int under;
102 int after;
103 } 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
114 * Arguments
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,
136 WCHAR *modstr,
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. */
148 if (*len_str > 0 &&
149 GetTextExtentExPointW(hdc, str, *len_str, width, NULL, NULL, size) &&
150 size->cx > width)
152 for (lo = 0, hi = *len_str; lo < hi; )
154 mid = (lo + hi) / 2;
155 if (!GetTextExtentExPointW(hdc, str, mid, width, NULL, NULL, size))
156 break;
157 if (size->cx > width)
158 hi = mid;
159 else
160 lo = mid + 1;
162 *len_str = hi;
164 /* Now this should take only a couple iterations at most. */
165 for ( ; ; )
167 memcpy(str + *len_str, ELLIPSISW, len_ellipsis*sizeof(WCHAR));
169 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
170 NULL, NULL, size)) break;
172 if (!*len_str || size->cx <= width) break;
174 (*len_str)--;
176 *len_ellip = len_ellipsis;
177 *len_before = *len_str;
178 *len_str += len_ellipsis;
180 if (modstr)
182 memcpy (modstr, str, *len_str * sizeof(WCHAR));
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
192 * flag.
194 * See Also TEXT_Ellipsify
196 * Arguments
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)
234 int len_ellipsis;
235 int len_trailing;
236 int len_under;
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 memcpy (lastSlash, ELLIPSISW, len_ellipsis*sizeof(WCHAR));
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
263 len_under = 0;
264 for ( ; ; )
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));
273 lastSlash--;
274 len_under++;
276 assert (*len_str);
277 (*len_str)--;
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;
285 if (modstr)
287 memcpy(modstr, str, *len_str * sizeof(WCHAR));
288 modstr[*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
304 * the line.
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.
311 * Arguments
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
323 * the next line.
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)
329 * Algorithm
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).
342 * Otherwise
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)
357 WCHAR *p;
358 int word_fits;
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 */
366 word_fits = TRUE;
367 if (!chars_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 */
371 else
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 */
378 if (word_fits)
380 int next_is_space;
381 /* break the line before/after that character */
382 if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
383 p++;
384 next_is_space = (p - str) < *len_str && *p == SPACE;
385 *len_str = p - str;
386 /* and if the next character is a space then discard it. */
387 *chars_used = *len_str;
388 if (next_is_space)
389 (*chars_used)++;
391 /* Suppose there was none. */
392 else
394 if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
395 DT_EDITCONTROL)
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.
408 /* Otherwise */
409 else
411 /* discard any trailing space. */
412 const WCHAR *e = str + *len_str;
413 p = str + chars_fit;
414 while (p < e && *p != SPACE)
415 p++;
416 *chars_used = p - str;
417 if (p < e) /* i.e. loop failed because *p == SPACE */
418 (*chars_used)++;
420 /* include the whole word; it may be ellipsified later */
421 *len_str = p - str;
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 /*********************************************************************
432 * TEXT_SkipChars
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
437 * a trailing &)
439 * Parameters
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
448 * &c counts as one)
449 * prefix [in] Apply prefix substitution
451 * Return Values
452 * none
454 * Remarks
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
469 if (prefix)
471 const WCHAR *str_on_entry = start_str;
472 assert (max >= n);
473 max -= n;
474 while (n--)
476 if (*start_str++ == PREFIX && max--)
477 start_str++;
479 start_count -= (start_str - str_on_entry);
481 else
483 start_str += n;
484 start_count -= n;
486 *new_str = start_str;
487 *new_count = start_count;
490 /*********************************************************************
491 * TEXT_Reprefix
493 * Reanalyse the text to find the prefixed character. This is called when
494 * wordbreaking or ellipsification has shortened the string such that the
495 * previously noted prefixed character is no longer visible.
497 * Parameters
498 * str [in] The original string segment (including all characters)
499 * ns [in] The number of characters in str (including prefixes)
500 * pe [in] The ellipsification data
502 * Return Values
503 * The prefix offset within the new string segment (the one that contains the
504 * ellipses and does not contain the prefix characters) (-1 if none)
507 static int TEXT_Reprefix (const WCHAR *str, unsigned int ns,
508 const ellipsis_data *pe)
510 int result = -1;
511 unsigned int i = 0;
512 unsigned int n = pe->before + pe->under + pe->after;
513 assert (n <= ns);
514 while (i < n)
516 if (i == pe->before)
518 /* Reached the path ellipsis; jump over it */
519 if (ns < pe->under) break;
520 str += pe->under;
521 ns -= pe->under;
522 i += pe->under;
523 if (!pe->after) break; /* Nothing after the path ellipsis */
525 if (!ns) break;
526 ns--;
527 if (*str++ == PREFIX)
529 if (!ns) break;
530 if (*str != PREFIX)
531 result = (i < pe->before || pe->under == 0) ? i : i - pe->under + pe->len;
532 /* pe->len may be non-zero while pe_under is zero */
533 str++;
534 ns--;
536 i++;
538 return result;
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;
552 str++;
553 if (*str != CR && *str != LF) return FALSE;
554 if (--num_chars) return FALSE;
555 return TRUE;
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)
592 int i = 0, j = 0;
593 int plen = 0;
594 SIZE size;
595 int maxl = *len;
596 int seg_i, seg_count, seg_j;
597 int max_seg_width;
598 int num_fit;
599 int word_broken;
600 int line_fits;
601 unsigned int j_in_seg;
602 int ellipsified;
603 *pprefix_offset = -1;
605 /* For each text segment in the line */
607 retsize->cy = 0;
608 while (*count)
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)
619 plen += tabwidth;
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 */
627 seg_i = i;
628 seg_count = *count;
629 seg_j = j;
631 while (*count &&
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)
645 *pprefix_offset = 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
649 * one.
652 else
654 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
659 /* Measure the whole text segment and possibly WordBreak and
660 * ellipsify it
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).
672 word_broken = 0;
673 line_fits = (num_fit >= j_in_seg);
674 if (!line_fits && (format & DT_WORDBREAK))
676 const WCHAR *s;
677 unsigned int chars_used;
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));
684 i = s - str;
685 word_broken = 1;
687 pellip->before = j_in_seg;
688 pellip->under = 0;
689 pellip->after = 0;
690 pellip->len = 0;
691 ellipsified = 0;
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);
697 ellipsified = 1;
699 /* NB we may end up ellipsifying a word-broken or path_ellipsified
700 * string */
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 */
715 else
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
724 * pellip->under
727 line_fits = (size.cx <= max_seg_width);
728 ellipsified = 1;
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)
737 *count = 0;
738 else
740 while ((*count) && str[i] != CR && str[i] != LF)
742 (*count)--, i++;
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;
755 plen += size.cx;
756 if (size.cy > retsize->cy)
757 retsize->cy = size.cy;
759 if (word_broken)
760 break;
761 else if (!*count)
762 break;
763 else if (str[i] == CR || str[i] == LF)
765 (*count)--, i++;
766 if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
768 (*count)--, i++;
770 break;
772 /* else it was a Tab and we go around again */
775 retsize->cx = plen;
776 *len = j;
777 if (*count)
778 return (&str[i]);
779 else
780 return NULL;
784 /***********************************************************************
785 * TEXT_DrawUnderscore
787 * Draw the underline under the prefixed character
789 * Parameters
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)
801 int prefix_x;
802 int prefix_end;
803 SIZE size;
804 HPEN hpen;
805 HPEN oldPen;
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 */
814 if (rect){
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);
827 DeleteObject (hpen);
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_BUFFER 1024
841 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
842 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
844 SIZE size;
845 const WCHAR *strPtr;
846 WCHAR *retstr, *p_retstr;
847 size_t size_retstr;
848 WCHAR line[MAX_BUFFER];
849 int len, lh, count=i_count;
850 TEXTMETRICW tm;
851 int lmargin = 0, rmargin = 0;
852 int x = rect->left, y = rect->top;
853 int width = rect->right - rect->left;
854 int max_width = 0;
855 int last_line;
856 int tabwidth /* to keep gcc happy */ = 0;
857 int prefix_offset;
858 ellipsis_data ellip;
860 TRACE("%s, %d, [%s] %08x\n", debugstr_wn (str, count), count,
861 wine_dbgstr_rect(rect), flags);
863 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
864 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
866 if (!str || count == 0) return 0;
867 if (count == -1)
869 count = strlenW(str);
870 if (count == 0)
872 if( flags & DT_CALCRECT)
874 rect->right = rect->left;
875 rect->bottom = rect->top;
877 return 0;
880 strPtr = str;
882 if (flags & DT_SINGLELINE)
883 flags &= ~DT_WORDBREAK;
885 GetTextMetricsW(hdc, &tm);
886 if (flags & DT_EXTERNALLEADING)
887 lh = tm.tmHeight + tm.tmExternalLeading;
888 else
889 lh = tm.tmHeight;
891 if (dtp)
893 lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
894 rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
895 if (!(flags & (DT_CENTER | DT_RIGHT)))
896 x += lmargin;
897 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
900 if (flags & DT_EXPANDTABS)
902 int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
903 tabwidth = tm.tmAveCharWidth * tabstop;
906 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
908 if (flags & DT_MODIFYSTRING)
910 size_retstr = (count + 4) * sizeof (WCHAR);
911 retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
912 if (!retstr) return 0;
913 memcpy (retstr, str, size_retstr);
915 else
917 size_retstr = 0;
918 retstr = NULL;
920 p_retstr = retstr;
924 len = sizeof(line)/sizeof(line[0]);
925 last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
926 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr, tabwidth, &prefix_offset, &ellip);
928 if (flags & DT_CENTER) x = (rect->left + rect->right -
929 size.cx) / 2;
930 else if (flags & DT_RIGHT) x = rect->right - size.cx;
932 if (flags & DT_SINGLELINE)
934 if (flags & DT_VCENTER) y = rect->top +
935 (rect->bottom - rect->top) / 2 - size.cy / 2;
936 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
939 if (!(flags & DT_CALCRECT))
941 const WCHAR *str = line;
942 int xseg = x;
943 while (len)
945 int len_seg;
946 SIZE size;
947 if ((flags & DT_EXPANDTABS))
949 const WCHAR *p;
950 p = str; while (p < str+len && *p != TAB) p++;
951 len_seg = p - str;
952 if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size))
953 return 0;
955 else
956 len_seg = len;
958 if (!ExtTextOutW( hdc, xseg, y,
959 ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
960 ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
961 rect, str, len_seg, NULL )) return 0;
962 if (prefix_offset != -1 && prefix_offset < len_seg)
964 TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset, (flags & DT_NOCLIP) ? NULL : rect);
966 len -= len_seg;
967 str += len_seg;
968 if (len)
970 assert ((flags & DT_EXPANDTABS) && *str == TAB);
971 len--; str++;
972 xseg += ((size.cx/tabwidth)+1)*tabwidth;
973 if (prefix_offset != -1)
975 if (prefix_offset < len_seg)
977 /* We have just drawn an underscore; we ought to
978 * figure out where the next one is. I am going
979 * to leave it for now until I have a better model
980 * for the line, which will make reprefixing easier.
981 * This is where ellip would be used.
983 prefix_offset = -1;
985 else
986 prefix_offset -= len_seg;
991 else if (size.cx > max_width)
992 max_width = size.cx;
994 y += lh;
995 if (dtp)
996 dtp->uiLengthDrawn += len;
998 while (strPtr && !last_line);
1000 if (flags & DT_CALCRECT)
1002 rect->right = rect->left + max_width;
1003 rect->bottom = y;
1004 if (dtp)
1005 rect->right += lmargin + rmargin;
1007 if (retstr)
1009 memcpy (str, retstr, size_retstr);
1010 HeapFree (GetProcessHeap(), 0, retstr);
1012 return y - rect->top;
1015 /***********************************************************************
1016 * DrawTextExA (USER32.@)
1018 * If DT_MODIFYSTRING is specified then there must be room for up to
1019 * 4 extra characters. We take great care about just how much modified
1020 * string we return.
1022 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
1023 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
1025 WCHAR *wstr;
1026 WCHAR *p;
1027 INT ret = 0;
1028 int i;
1029 DWORD wcount;
1030 DWORD wmax;
1031 DWORD amax;
1032 UINT cp;
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;
1042 return 0;
1044 cp = GdiGetCodePage( hdc );
1045 wcount = MultiByteToWideChar( cp, 0, str, count, NULL, 0 );
1046 wmax = wcount;
1047 amax = count;
1048 if (flags & DT_MODIFYSTRING)
1050 wmax += 4;
1051 amax += 4;
1053 wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
1054 if (wstr)
1056 MultiByteToWideChar( cp, 0, str, count, wstr, wcount );
1057 if (flags & DT_MODIFYSTRING)
1058 for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
1059 /* Initialise the extra characters so that we can see which ones
1060 * change. U+FFFE is guaranteed to be not a unicode character and
1061 * so will not be generated by DrawTextEx itself.
1063 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, dtp );
1064 if (flags & DT_MODIFYSTRING)
1066 /* Unfortunately the returned string may contain multiple \0s
1067 * and so we need to measure it ourselves.
1069 for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
1070 WideCharToMultiByte( cp, 0, wstr, wcount, str, amax, NULL, NULL );
1072 HeapFree(GetProcessHeap(), 0, wstr);
1074 return ret;
1077 /***********************************************************************
1078 * DrawTextW (USER32.@)
1080 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
1082 DRAWTEXTPARAMS dtp;
1084 memset (&dtp, 0, sizeof(dtp));
1085 if (flags & DT_TABSTOP)
1087 dtp.iTabLength = (flags >> 8) & 0xff;
1088 flags &= 0xffff00ff;
1090 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1093 /***********************************************************************
1094 * DrawTextA (USER32.@)
1096 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1098 DRAWTEXTPARAMS dtp;
1100 memset (&dtp, 0, sizeof(dtp));
1101 if (flags & DT_TABSTOP)
1103 dtp.iTabLength = (flags >> 8) & 0xff;
1104 flags &= 0xffff00ff;
1106 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1109 /***********************************************************************
1111 * GrayString functions
1114 /* callback for ASCII gray string proc */
1115 static BOOL CALLBACK gray_string_callbackA( HDC hdc, LPARAM param, INT len )
1117 return TextOutA( hdc, 0, 0, (LPCSTR)param, len );
1120 /* callback for Unicode gray string proc */
1121 static BOOL CALLBACK gray_string_callbackW( HDC hdc, LPARAM param, INT len )
1123 return TextOutW( hdc, 0, 0, (LPCWSTR)param, len );
1126 /***********************************************************************
1127 * TEXT_GrayString
1129 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1130 INT x, INT y, INT cx, INT cy )
1132 HBITMAP hbm, hbmsave;
1133 HBRUSH hbsave;
1134 HFONT hfsave;
1135 HDC memdc;
1136 int slen = len;
1137 BOOL retval = TRUE;
1138 COLORREF fg, bg;
1140 if(!hdc) return FALSE;
1141 if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1143 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1144 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1145 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1146 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1147 SelectObject( memdc, hbsave );
1148 SetTextColor(memdc, RGB(255, 255, 255));
1149 SetBkColor(memdc, RGB(0, 0, 0));
1150 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1152 retval = fn(memdc, lp, slen);
1153 SelectObject(memdc, hfsave);
1156 * Windows doc says that the bitmap isn't grayed when len == -1 and
1157 * the callback function returns FALSE. However, testing this on
1158 * win95 showed otherwise...
1160 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1161 if(retval || len != -1)
1162 #endif
1164 hbsave = (HBRUSH)SelectObject(memdc, SYSCOLOR_55AABrush);
1165 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1166 SelectObject(memdc, hbsave);
1169 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
1170 fg = SetTextColor(hdc, RGB(0, 0, 0));
1171 bg = SetBkColor(hdc, RGB(255, 255, 255));
1172 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1173 SetTextColor(hdc, fg);
1174 SetBkColor(hdc, bg);
1175 if(hb) SelectObject(hdc, hbsave);
1177 SelectObject(memdc, hbmsave);
1178 DeleteObject(hbm);
1179 DeleteDC(memdc);
1180 return retval;
1184 /***********************************************************************
1185 * GrayStringA (USER32.@)
1187 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1188 LPARAM lParam, INT cch, INT x, INT y,
1189 INT cx, INT cy )
1191 if (!cch) cch = strlen( (LPCSTR)lParam );
1192 if ((cx == 0 || cy == 0) && cch != -1)
1194 SIZE s;
1195 GetTextExtentPoint32A( hdc, (LPCSTR)lParam, cch, &s );
1196 if (cx == 0) cx = s.cx;
1197 if (cy == 0) cy = s.cy;
1199 if (!gsprc) gsprc = gray_string_callbackA;
1200 return TEXT_GrayString( hdc, hbr, gsprc, lParam, cch, x, y, cx, cy );
1204 /***********************************************************************
1205 * GrayStringW (USER32.@)
1207 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1208 LPARAM lParam, INT cch, INT x, INT y,
1209 INT cx, INT cy )
1211 if (!cch) cch = strlenW( (LPCWSTR)lParam );
1212 if ((cx == 0 || cy == 0) && cch != -1)
1214 SIZE s;
1215 GetTextExtentPoint32W( hdc, (LPCWSTR)lParam, cch, &s );
1216 if (cx == 0) cx = s.cx;
1217 if (cy == 0) cy = s.cy;
1219 if (!gsprc) gsprc = gray_string_callbackW;
1220 return TEXT_GrayString( hdc, hbr, gsprc, lParam, cch, x, y, cx, cy );
1224 /***********************************************************************
1225 * TEXT_TabbedTextOut
1227 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1228 * Note: this doesn't work too well for text-alignment modes other
1229 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1231 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCWSTR lpstr,
1232 INT count, INT cTabStops, const INT *lpTabPos, INT nTabOrg,
1233 BOOL fDisplayText )
1235 INT defWidth;
1236 SIZE extent;
1237 int i, j;
1238 int start = x;
1240 if (!lpTabPos)
1241 cTabStops=0;
1243 if (cTabStops == 1)
1245 defWidth = *lpTabPos;
1246 cTabStops = 0;
1248 else
1250 TEXTMETRICW tm;
1251 GetTextMetricsW( hdc, &tm );
1252 defWidth = 8 * tm.tmAveCharWidth;
1255 while (count > 0)
1257 RECT r;
1258 INT x0;
1259 x0 = x;
1260 r.left = x0;
1261 /* chop the string into substrings of 0 or more <tabs>
1262 * possibly followed by 1 or more normal characters */
1263 for (i = 0; i < count; i++)
1264 if (lpstr[i] != '\t') break;
1265 for (j = i; j < count; j++)
1266 if (lpstr[j] == '\t') break;
1267 /* get the extent of the normal character part */
1268 GetTextExtentPointW( hdc, lpstr + i, j - i , &extent );
1269 /* and if there is a <tab>, calculate its position */
1270 if( i) {
1271 /* get x coordinate for the drawing of this string */
1272 for (; cTabStops >= i; lpTabPos++, cTabStops--)
1274 if( nTabOrg + abs( *lpTabPos) > x) {
1275 if( lpTabPos[ i - 1] >= 0) {
1276 /* a left aligned tab */
1277 x0 = nTabOrg + lpTabPos[i-1];
1278 x = x0 + extent.cx;
1279 break;
1281 else
1283 /* if tab pos is negative then text is right-aligned
1284 * to tab stop meaning that the string extends to the
1285 * left, so we must subtract the width of the string */
1286 if (nTabOrg - lpTabPos[ i - 1] - extent.cx > x)
1288 x = nTabOrg - lpTabPos[ i - 1];
1289 x0 = x - extent.cx;
1290 break;
1295 /* if we have run out of tab stops and we have a valid default tab
1296 * stop width then round x up to that width */
1297 if ((cTabStops < i) && (defWidth > 0)) {
1298 x0 = nTabOrg + ((x - nTabOrg) / defWidth + i) * defWidth;
1299 x = x0 + extent.cx;
1300 } else if ((cTabStops < i) && (defWidth < 0)) {
1301 x = nTabOrg + ((x - nTabOrg + extent.cx) / -defWidth + i)
1302 * -defWidth;
1303 x0 = x - extent.cx;
1305 } else
1306 x += extent.cx;
1308 if (fDisplayText)
1310 r.top = y;
1311 r.right = x;
1312 r.bottom = y + extent.cy;
1313 ExtTextOutW( hdc, x0, y, GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1314 &r, lpstr + i, j - i, NULL );
1316 count -= j;
1317 lpstr += j;
1319 return MAKELONG(x - start, extent.cy);
1323 /***********************************************************************
1324 * TabbedTextOutA (USER32.@)
1326 * See TabbedTextOutW.
1328 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1329 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1331 LONG ret;
1332 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpstr, count, NULL, 0 );
1333 LPWSTR strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1334 if (!strW) return 0;
1335 MultiByteToWideChar( CP_ACP, 0, lpstr, count, strW, len );
1336 ret = TabbedTextOutW( hdc, x, y, strW, len, cTabStops, lpTabPos, nTabOrg );
1337 HeapFree( GetProcessHeap(), 0, strW );
1338 return ret;
1342 /***********************************************************************
1343 * TabbedTextOutW (USER32.@)
1345 * Draws tabbed text aligned using the specified tab stops.
1347 * PARAMS
1348 * hdc [I] Handle to device context to draw to.
1349 * x [I] X co-ordinate to start drawing the text at in logical units.
1350 * y [I] Y co-ordinate to start drawing the text at in logical units.
1351 * str [I] Pointer to the characters to draw.
1352 * count [I] Number of WCHARs pointed to by str.
1353 * cTabStops [I] Number of tab stops pointed to by lpTabPos.
1354 * lpTabPos [I] Tab stops in logical units. Should be sorted in ascending order.
1355 * nTabOrg [I] Starting position to expand tabs from in logical units.
1357 * RETURNS
1358 * The dimensions of the string drawn. The height is in the high-order word
1359 * and the width is in the low-order word.
1361 * NOTES
1362 * The tabs stops can be negative, in which case the text is right aligned to
1363 * that tab stop and, despite what MSDN says, this is supported on
1364 * Windows XP SP2.
1366 * BUGS
1367 * MSDN says that the TA_UPDATECP from GetTextAlign causes this function to
1368 * ignore the x and y co-ordinates, but this is unimplemented at the moment.
1370 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1371 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1373 TRACE("%p %d,%d %s %d\n", hdc, x, y, debugstr_wn(str,count), count );
1374 return TEXT_TabbedTextOut( hdc, x, y, str, count, cTabStops, lpTabPos, nTabOrg, TRUE );
1378 /***********************************************************************
1379 * GetTabbedTextExtentA (USER32.@)
1381 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1382 INT cTabStops, const INT *lpTabPos )
1384 LONG ret;
1385 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpstr, count, NULL, 0 );
1386 LPWSTR strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1387 if (!strW) return 0;
1388 MultiByteToWideChar( CP_ACP, 0, lpstr, count, strW, len );
1389 ret = GetTabbedTextExtentW( hdc, strW, len, cTabStops, lpTabPos );
1390 HeapFree( GetProcessHeap(), 0, strW );
1391 return ret;
1395 /***********************************************************************
1396 * GetTabbedTextExtentW (USER32.@)
1398 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1399 INT cTabStops, const INT *lpTabPos )
1401 TRACE("%p %s %d\n", hdc, debugstr_wn(lpstr,count), count );
1402 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops, lpTabPos, 0, FALSE );