Don't use ".previous" for Cygwin build.
[wine/multimedia.git] / dlls / user / text.c
blobb3a4b10e181035e4c0941553d38cf0c1c70ea92e
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "config.h"
28 #include "wine/port.h"
30 #include <stdarg.h>
31 #include <string.h>
32 #include <assert.h>
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "wine/winuser16.h"
38 #include "wine/unicode.h"
39 #include "winerror.h"
40 #include "winnls.h"
41 #include "wownt32.h"
42 #include "user.h"
43 #include "controls.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(text);
48 /*********************************************************************
50 * DrawText functions
52 * Design issues
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
58 * in effect.
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.
62 * Complications.
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
70 * times.
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)
87 #define TAB 9
88 #define LF 10
89 #define CR 13
90 #define SPACE 32
91 #define PREFIX 38
93 #define FORWARD_SLASH '/'
94 #define BACK_SLASH '\\'
96 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
98 typedef struct tag_ellipsis_data
100 int before;
101 int len;
102 int under;
103 int after;
104 } 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
115 * Arguments
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,
137 WCHAR *modstr,
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. */
149 if (*len_str > 0 &&
150 GetTextExtentExPointW(hdc, str, *len_str, width, NULL, NULL, size) &&
151 size->cx > width)
153 for (lo = 0, hi = *len_str; lo < hi; )
155 mid = (lo + hi) / 2;
156 if (!GetTextExtentExPointW(hdc, str, mid, width, NULL, NULL, size))
157 break;
158 if (size->cx > width)
159 hi = mid;
160 else
161 lo = mid + 1;
163 *len_str = hi;
165 /* Now this should take only a couple iterations at most. */
166 for ( ; ; )
168 strncpyW (str + *len_str, ELLIPSISW, len_ellipsis);
170 if (!GetTextExtentExPointW (hdc, str, *len_str + len_ellipsis, width,
171 NULL, NULL, size)) break;
173 if (!*len_str || size->cx <= width) break;
175 (*len_str)--;
177 *len_ellip = len_ellipsis;
178 *len_before = *len_str;
179 *len_str += len_ellipsis;
181 if (modstr)
183 strncpyW (modstr, str, *len_str);
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
193 * flag.
195 * See Also TEXT_Ellipsify
197 * Arguments
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)
235 int len_ellipsis;
236 int len_trailing;
237 int len_under;
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 strncpyW (lastSlash, ELLIPSISW, len_ellipsis);
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
264 len_under = 0;
265 for ( ; ; )
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));
274 lastSlash--;
275 len_under++;
277 assert (*len_str);
278 (*len_str)--;
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;
286 if (modstr)
288 strncpyW (modstr, str, *len_str);
289 *(str+*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
305 * the line.
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.
312 * Arguments
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
324 * the next line.
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)
330 * Algorithm
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).
343 * Otherwise
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)
358 WCHAR *p;
359 int word_fits;
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 */
367 word_fits = TRUE;
368 if (!chars_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 */
372 else
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 */
379 if (word_fits)
381 int next_is_space;
382 /* break the line before/after that character */
383 if (!(format & (DT_RIGHT | DT_CENTER)) || *p != SPACE)
384 p++;
385 next_is_space = (p - str) < *len_str && *p == SPACE;
386 *len_str = p - str;
387 /* and if the next character is a space then discard it. */
388 *chars_used = *len_str;
389 if (next_is_space)
390 (*chars_used)++;
392 /* Suppose there was none. */
393 else
395 if ((format & (DT_EDITCONTROL | DT_WORD_ELLIPSIS | DT_PATH_ELLIPSIS)) ==
396 DT_EDITCONTROL)
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.
409 /* Otherwise */
410 else
412 /* discard any trailing space. */
413 const WCHAR *e = str + *len_str;
414 p = str + chars_fit;
415 while (p < e && *p != SPACE)
416 p++;
417 *chars_used = p - str;
418 if (p < e) /* i.e. loop failed because *p == SPACE */
419 (*chars_used)++;
421 /* include the whole word; it may be ellipsified later */
422 *len_str = p - str;
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 /*********************************************************************
433 * TEXT_SkipChars
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
438 * a trailing &)
440 * Parameters
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
449 * &c counts as one)
450 * prefix [in] Apply prefix substitution
452 * Return Values
453 * none
455 * Remarks
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
470 if (prefix)
472 const WCHAR *str_on_entry = start_str;
473 assert (max >= n);
474 max -= n;
475 while (n--)
476 if (*start_str++ == PREFIX && max--)
477 start_str++;
478 else;
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 else;
537 i++;
539 return result;
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;
553 str++;
554 if (*str != CR && *str != LF) return FALSE;
555 if (--num_chars) return FALSE;
556 return TRUE;
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)
593 int i = 0, j = 0;
594 int plen = 0;
595 SIZE size;
596 int maxl = *len;
597 int seg_i, seg_count, seg_j;
598 int max_seg_width;
599 int num_fit;
600 int word_broken;
601 int line_fits;
602 int j_in_seg;
603 int ellipsified;
604 *pprefix_offset = -1;
606 /* For each text segment in the line */
608 retsize->cy = 0;
609 while (*count)
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)
620 plen += tabwidth;
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 */
628 seg_i = i;
629 seg_count = *count;
630 seg_j = j;
632 while (*count &&
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)
646 *pprefix_offset = 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
650 * one.
653 else
655 (*count)--; if (j < maxl) dest[j++] = str[i++]; else i++;
660 /* Measure the whole text segment and possibly WordBreak and
661 * ellipsify it
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).
673 word_broken = 0;
674 line_fits = (num_fit >= j_in_seg);
675 if (!line_fits && (format & DT_WORDBREAK))
677 const WCHAR *s;
678 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));
685 i = s - str;
686 word_broken = 1;
688 pellip->before = j_in_seg;
689 pellip->under = 0;
690 pellip->after = 0;
691 pellip->len = 0;
692 ellipsified = 0;
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);
698 ellipsified = 1;
700 /* NB we may end up ellipsifying a word-broken or path_ellipsified
701 * string */
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 */
716 else
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
725 * pellip->under
728 line_fits = (size.cx <= max_seg_width);
729 ellipsified = 1;
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)
738 *count = 0;
739 else
741 while ((*count) && str[i] != CR && str[i] != LF)
743 (*count)--, i++;
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;
756 plen += size.cx;
757 if (size.cy > retsize->cy)
758 retsize->cy = size.cy;
760 if (word_broken)
761 break;
762 else if (!*count)
763 break;
764 else if (str[i] == CR || str[i] == LF)
766 (*count)--, i++;
767 if (*count && (str[i] == CR || str[i] == LF) && str[i] != str[i-1])
769 (*count)--, i++;
771 break;
773 /* else it was a Tab and we go around again */
776 retsize->cx = plen;
777 *len = j;
778 if (*count)
779 return (&str[i]);
780 else
781 return NULL;
785 /***********************************************************************
786 * TEXT_DrawUnderscore
788 * Draw the underline under the prefixed character
790 * Parameters
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)
802 int prefix_x;
803 int prefix_end;
804 SIZE size;
805 HPEN hpen;
806 HPEN oldPen;
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 */
815 if (rect){
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);
828 DeleteObject (hpen);
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_STATIC_BUFFER 1024
842 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
843 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
845 SIZE size;
846 const WCHAR *strPtr;
847 WCHAR *retstr, *p_retstr;
848 size_t size_retstr;
849 static WCHAR line[MAX_STATIC_BUFFER];
850 int len, lh, count=i_count;
851 TEXTMETRICW tm;
852 int lmargin = 0, rmargin = 0;
853 int x = rect->left, y = rect->top;
854 int width = rect->right - rect->left;
855 int max_width = 0;
856 int last_line;
857 int tabwidth /* to keep gcc happy */ = 0;
858 int prefix_offset;
859 ellipsis_data ellip;
861 TRACE("%s, %d, [(%ld,%ld),(%ld,%ld)]\n", debugstr_wn (str, count), count,
862 rect->left, rect->top, rect->right, rect->bottom);
864 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
865 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
867 if (!str) return 0;
868 if (count == -1) count = strlenW(str);
869 if (count == 0) return 0;
870 strPtr = str;
872 if (flags & DT_SINGLELINE)
873 flags &= ~DT_WORDBREAK;
875 GetTextMetricsW(hdc, &tm);
876 if (flags & DT_EXTERNALLEADING)
877 lh = tm.tmHeight + tm.tmExternalLeading;
878 else
879 lh = tm.tmHeight;
881 if (dtp)
883 lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
884 rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
885 if (!(flags & (DT_CENTER | DT_RIGHT)))
886 x += lmargin;
887 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
890 if (flags & DT_EXPANDTABS)
892 int tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
893 tabwidth = tm.tmAveCharWidth * tabstop;
896 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
898 if (flags & DT_MODIFYSTRING)
900 size_retstr = (count + 4) * sizeof (WCHAR);
901 retstr = HeapAlloc(GetProcessHeap(), 0, size_retstr);
902 if (!retstr) return 0;
903 memcpy (retstr, str, size_retstr);
905 else
907 size_retstr = 0;
908 retstr = NULL;
910 p_retstr = retstr;
914 len = MAX_STATIC_BUFFER;
915 last_line = !(flags & DT_NOCLIP) && y + ((flags & DT_EDITCONTROL) ? 2*lh-1 : lh) > rect->bottom;
916 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags, &size, last_line, &p_retstr, tabwidth, &prefix_offset, &ellip);
918 if (flags & DT_CENTER) x = (rect->left + rect->right -
919 size.cx) / 2;
920 else if (flags & DT_RIGHT) x = rect->right - size.cx;
922 if (flags & DT_SINGLELINE)
924 if (flags & DT_VCENTER) y = rect->top +
925 (rect->bottom - rect->top) / 2 - size.cy / 2;
926 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
929 if (!(flags & DT_CALCRECT))
931 const WCHAR *str = line;
932 int xseg = x;
933 while (len)
935 int len_seg;
936 SIZE size;
937 if ((flags & DT_EXPANDTABS))
939 const WCHAR *p;
940 p = str; while (p < str+len && *p != TAB) p++;
941 len_seg = p - str;
942 if (len_seg != len && !GetTextExtentPointW(hdc, str, len_seg, &size))
943 return 0;
945 else
946 len_seg = len;
948 if (!ExtTextOutW( hdc, xseg, y,
949 ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
950 ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
951 rect, str, len_seg, NULL )) return 0;
952 if (prefix_offset != -1 && prefix_offset < len_seg)
954 TEXT_DrawUnderscore (hdc, xseg, y + tm.tmAscent + 1, str, prefix_offset, (flags & DT_NOCLIP) ? NULL : rect);
956 len -= len_seg;
957 str += len_seg;
958 if (len)
960 assert ((flags & DT_EXPANDTABS) && *str == TAB);
961 len--; str++;
962 xseg += ((size.cx/tabwidth)+1)*tabwidth;
963 if (prefix_offset != -1)
965 if (prefix_offset < len_seg)
967 /* We have just drawn an underscore; we ought to
968 * figure out where the next one is. I am going
969 * to leave it for now until I have a better model
970 * for the line, which will make reprefixing easier.
971 * This is where ellip would be used.
973 prefix_offset = -1;
975 else
976 prefix_offset -= len_seg;
981 else if (size.cx > max_width)
982 max_width = size.cx;
984 y += lh;
985 if (dtp)
986 dtp->uiLengthDrawn += len;
988 while (strPtr && !last_line);
990 if (flags & DT_CALCRECT)
992 rect->right = rect->left + max_width;
993 rect->bottom = y;
994 if (dtp)
995 rect->right += lmargin + rmargin;
997 if (retstr)
999 memcpy (str, retstr, size_retstr);
1000 HeapFree (GetProcessHeap(), 0, retstr);
1002 return y - rect->top;
1005 /***********************************************************************
1006 * DrawTextExA (USER32.@)
1008 * If DT_MODIFYSTRING is specified then there must be room for up to
1009 * 4 extra characters. We take great care about just how much modified
1010 * string we return.
1012 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
1013 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
1015 WCHAR *wstr;
1016 WCHAR *p;
1017 INT ret = 0;
1018 int i;
1019 DWORD wcount;
1020 DWORD wmax;
1021 DWORD amax;
1023 if (!str) return 0;
1024 if (count == -1) count = strlen(str);
1025 if (!count) return 0;
1026 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
1027 wmax = wcount;
1028 amax = count;
1029 if (flags & DT_MODIFYSTRING)
1031 wmax += 4;
1032 amax += 4;
1034 wstr = HeapAlloc(GetProcessHeap(), 0, wmax * sizeof(WCHAR));
1035 if (wstr)
1037 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
1038 if (flags & DT_MODIFYSTRING)
1039 for (i=4, p=wstr+wcount; i--; p++) *p=0xFFFE;
1040 /* Initialise the extra characters so that we can see which ones
1041 * change. U+FFFE is guaranteed to be not a unicode character and
1042 * so will not be generated by DrawTextEx itself.
1044 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, dtp );
1045 if (flags & DT_MODIFYSTRING)
1047 /* Unfortunately the returned string may contain multiple \0s
1048 * and so we need to measure it ourselves.
1050 for (i=4, p=wstr+wcount; i-- && *p != 0xFFFE; p++) wcount++;
1051 WideCharToMultiByte( CP_ACP, 0, wstr, wcount, str, amax, NULL, NULL );
1053 HeapFree(GetProcessHeap(), 0, wstr);
1055 return ret;
1058 /***********************************************************************
1059 * DrawTextW (USER32.@)
1061 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
1063 DRAWTEXTPARAMS dtp;
1065 memset (&dtp, 0, sizeof(dtp));
1066 if (flags & DT_TABSTOP)
1068 dtp.iTabLength = (flags >> 8) && 0xff;
1069 flags &= 0xffff00ff;
1071 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
1074 /***********************************************************************
1075 * DrawTextA (USER32.@)
1077 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
1079 DRAWTEXTPARAMS dtp;
1081 memset (&dtp, 0, sizeof(dtp));
1082 if (flags & DT_TABSTOP)
1084 dtp.iTabLength = (flags >> 8) && 0xff;
1085 flags &= 0xffff00ff;
1087 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
1090 /***********************************************************************
1092 * GrayString functions
1095 /* callback for ASCII gray string proc */
1096 static BOOL CALLBACK gray_string_callbackA( HDC hdc, LPARAM param, INT len )
1098 return TextOutA( hdc, 0, 0, (LPCSTR)param, len );
1101 /* callback for Unicode gray string proc */
1102 static BOOL CALLBACK gray_string_callbackW( HDC hdc, LPARAM param, INT len )
1104 return TextOutW( hdc, 0, 0, (LPCWSTR)param, len );
1107 /***********************************************************************
1108 * TEXT_GrayString
1110 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
1111 * heap and we can guarantee that the handles fit in an INT16. We have to
1112 * rethink the strategy once the migration to NT handles is complete.
1113 * We are going to get a lot of code-duplication once this migration is
1114 * completed...
1117 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
1118 INT x, INT y, INT cx, INT cy )
1120 HBITMAP hbm, hbmsave;
1121 HBRUSH hbsave;
1122 HFONT hfsave;
1123 HDC memdc;
1124 int slen = len;
1125 BOOL retval = TRUE;
1126 COLORREF fg, bg;
1128 if(!hdc) return FALSE;
1129 if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
1131 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
1132 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
1133 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
1134 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
1135 SelectObject( memdc, hbsave );
1136 SetTextColor(memdc, RGB(255, 255, 255));
1137 SetBkColor(memdc, RGB(0, 0, 0));
1138 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1140 retval = fn(memdc, lp, slen);
1141 SelectObject(memdc, hfsave);
1144 * Windows doc says that the bitmap isn't grayed when len == -1 and
1145 * the callback function returns FALSE. However, testing this on
1146 * win95 showed otherwise...
1148 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
1149 if(retval || len != -1)
1150 #endif
1152 hbsave = (HBRUSH)SelectObject(memdc, UITOOLS_GetPattern55AABrush());
1153 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
1154 SelectObject(memdc, hbsave);
1157 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
1158 fg = SetTextColor(hdc, RGB(0, 0, 0));
1159 bg = SetBkColor(hdc, RGB(255, 255, 255));
1160 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
1161 SetTextColor(hdc, fg);
1162 SetBkColor(hdc, bg);
1163 if(hb) SelectObject(hdc, hbsave);
1165 SelectObject(memdc, hbmsave);
1166 DeleteObject(hbm);
1167 DeleteDC(memdc);
1168 return retval;
1172 /***********************************************************************
1173 * GrayStringA (USER32.@)
1175 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1176 LPARAM lParam, INT cch, INT x, INT y,
1177 INT cx, INT cy )
1179 if (!cch) cch = strlen( (LPCSTR)lParam );
1180 if ((cx == 0 || cy == 0) && cch != -1)
1182 SIZE s;
1183 GetTextExtentPoint32A( hdc, (LPCSTR)lParam, cch, &s );
1184 if (cx == 0) cx = s.cx;
1185 if (cy == 0) cy = s.cy;
1187 if (!gsprc) gsprc = gray_string_callbackA;
1188 return TEXT_GrayString( hdc, hbr, gsprc, lParam, cch, x, y, cx, cy );
1192 /***********************************************************************
1193 * GrayStringW (USER32.@)
1195 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
1196 LPARAM lParam, INT cch, INT x, INT y,
1197 INT cx, INT cy )
1199 if (!cch) cch = strlenW( (LPCWSTR)lParam );
1200 if ((cx == 0 || cy == 0) && cch != -1)
1202 SIZE s;
1203 GetTextExtentPoint32W( hdc, (LPCWSTR)lParam, cch, &s );
1204 if (cx == 0) cx = s.cx;
1205 if (cy == 0) cy = s.cy;
1207 if (!gsprc) gsprc = gray_string_callbackW;
1208 return TEXT_GrayString( hdc, hbr, gsprc, lParam, cch, x, y, cx, cy );
1212 /***********************************************************************
1213 * TEXT_TabbedTextOut
1215 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
1216 * Note: this doesn't work too well for text-alignment modes other
1217 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
1219 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCWSTR lpstr,
1220 INT count, INT cTabStops, const INT *lpTabPos, INT nTabOrg,
1221 BOOL fDisplayText )
1223 INT defWidth;
1224 SIZE extent;
1225 int i, tabPos = x;
1226 int start = x;
1228 extent.cx = 0;
1229 extent.cy = 0;
1231 if (!lpTabPos)
1232 cTabStops=0;
1234 if (cTabStops == 1 && *lpTabPos >= /* sic */ 0)
1236 defWidth = *lpTabPos;
1237 cTabStops = 0;
1239 else
1241 TEXTMETRICA tm;
1242 GetTextMetricsA( hdc, &tm );
1243 defWidth = 8 * tm.tmAveCharWidth;
1244 if (cTabStops == 1)
1245 cTabStops = 0; /* on negative *lpTabPos */
1248 while (count > 0)
1250 for (i = 0; i < count; i++)
1251 if (lpstr[i] == '\t') break;
1252 GetTextExtentPointW( hdc, lpstr, i, &extent );
1253 while ((cTabStops > 0) &&
1254 (nTabOrg + *lpTabPos <= x + extent.cx))
1256 lpTabPos++;
1257 cTabStops--;
1259 if (i == count)
1260 tabPos = x + extent.cx;
1261 else if (cTabStops > 0)
1262 tabPos = nTabOrg + *lpTabPos;
1263 else if (defWidth <= 0)
1264 tabPos = x + extent.cx;
1265 else
1266 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
1267 if (fDisplayText)
1269 RECT r;
1270 r.left = x;
1271 r.top = y;
1272 r.right = tabPos;
1273 r.bottom = y + extent.cy;
1274 ExtTextOutW( hdc, x, y, GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
1275 &r, lpstr, i, NULL );
1277 x = tabPos;
1278 count -= i+1;
1279 lpstr += i+1;
1281 return MAKELONG(tabPos - start, extent.cy);
1285 /***********************************************************************
1286 * TabbedTextOutA (USER32.@)
1288 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
1289 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1291 LONG ret;
1292 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpstr, count, NULL, 0 );
1293 LPWSTR strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1294 if (!strW) return 0;
1295 MultiByteToWideChar( CP_ACP, 0, lpstr, count, strW, len );
1296 ret = TabbedTextOutW( hdc, x, y, strW, len, cTabStops, lpTabPos, nTabOrg );
1297 HeapFree( GetProcessHeap(), 0, strW );
1298 return ret;
1302 /***********************************************************************
1303 * TabbedTextOutW (USER32.@)
1305 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
1306 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
1308 TRACE("%p %d,%d %s %d\n", hdc, x, y, debugstr_wn(str,count), count );
1309 return TEXT_TabbedTextOut( hdc, x, y, str, count, cTabStops, lpTabPos, nTabOrg, TRUE );
1313 /***********************************************************************
1314 * GetTabbedTextExtentA (USER32.@)
1316 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
1317 INT cTabStops, const INT *lpTabPos )
1319 LONG ret;
1320 DWORD len = MultiByteToWideChar( CP_ACP, 0, lpstr, count, NULL, 0 );
1321 LPWSTR strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1322 if (!strW) return 0;
1323 MultiByteToWideChar( CP_ACP, 0, lpstr, count, strW, len );
1324 ret = GetTabbedTextExtentW( hdc, strW, len, cTabStops, lpTabPos );
1325 HeapFree( GetProcessHeap(), 0, strW );
1326 return ret;
1330 /***********************************************************************
1331 * GetTabbedTextExtentW (USER32.@)
1333 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
1334 INT cTabStops, const INT *lpTabPos )
1336 TRACE("%p %s %d\n", hdc, debugstr_wn(lpstr,count), count );
1337 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops, lpTabPos, 0, FALSE );