4 * Copyright 1993, 1994 Alexandre Julliard
7 * 1. DrawText functions
8 * 2. GrayString functions
9 * 3. TabbedText functions
16 #include "wine/winuser16.h"
17 #include "wine/unicode.h"
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(text);
26 #define countof(a) (sizeof(a)/sizeof(a[0]))
28 /*********************************************************************
39 #define ELLIPSIS "..."
40 #define FORWARD_SLASH '/'
41 #define BACK_SLASH '\\'
43 static const WCHAR SPACEW[] = {' ', 0};
44 static const WCHAR oW[] = {'o', 0};
45 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
46 static const WCHAR FORWARD_SLASHW[] = {'/', 0};
47 static const WCHAR BACK_SLASHW[] = {'\\', 0};
51 static int spacewidth;
52 static int prefix_offset;
55 /*********************************************************************
56 * Return next line of text from a string.
59 * str - string to parse into lines.
60 * count - length of str.
61 * dest - destination in which to return line.
62 * len - dest buffer size in chars on input, copied length into dest on output.
63 * width - maximum width of line in pixels.
64 * format - format type passed to DrawText.
66 * Returns pointer to next char in str after end of the line
67 * or NULL if end of str reached.
70 * GetTextExtentPoint is used to get the width of each character,
71 * rather than GetCharABCWidth... So the whitespace between
72 * characters is ignored, and the reported len is too great.
74 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
75 WCHAR *dest, int *len, int width, WORD format)
82 int wb_i = 0, wb_j = 0, wb_count = 0;
85 while (*count && j < maxl)
91 if (!(format & DT_SINGLELINE))
93 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
103 dest[j++] = str[i++];
104 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
105 (format & DT_WORDBREAK))
107 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
114 if (!(format & DT_NOPREFIX) && *count > 1)
116 if (str[++i] == PREFIX)
123 dest[j++] = str[i++];
124 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
125 (format & DT_WORDBREAK))
127 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
134 if (format & DT_EXPANDTABS)
140 if (!GetTextExtentPointW(hdc, &dest[lasttab], j - lasttab, &size))
143 numspaces = (tabwidth - size.cx) / spacewidth;
144 for (k = 0; k < numspaces; k++)
146 plen += tabwidth - size.cx;
147 lasttab = wb_j + numspaces;
151 dest[j++] = str[i++];
152 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
153 (format & DT_WORDBREAK))
155 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
163 dest[j++] = str[i++];
164 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
165 (format & DT_WORDBREAK))
170 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
177 dest[j++] = str[i++];
178 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
179 (format & DT_WORDBREAK))
181 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
188 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
192 if (format & DT_WORDBREAK)
197 *count = wb_count - 1;
215 /***********************************************************************
216 * TEXT_DrawUnderscore
218 * Draw the underline under the prefixed character
221 * hdc [in] The handle of the DC for drawing
222 * x [in] The x location of the line segment (logical coordinates)
223 * y [in] The y location of where the underscore should appear
224 * (logical coordinates)
225 * str [in] The text of the line segment
226 * offset [in] The offset of the underscored character within str
229 static void TEXT_DrawUnderscore (HDC hdc, int x, int y, const WCHAR *str, int offset)
237 GetTextExtentPointW (hdc, str, offset, &size);
238 prefix_x = x + size.cx;
239 GetTextExtentPointW (hdc, str, offset+1, &size);
240 prefix_end = x + size.cx - 1;
241 /* The above method may eventually be slightly wrong due to kerning etc. */
243 hpen = CreatePen (PS_SOLID, 1, GetTextColor (hdc));
244 oldPen = SelectObject (hdc, hpen);
245 MoveToEx (hdc, prefix_x, y, NULL);
246 LineTo (hdc, prefix_end, y);
247 SelectObject (hdc, oldPen);
251 /***********************************************************************
252 * DrawTextExW (USER32.@)
254 #define MAX_STATIC_BUFFER 1024
255 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
256 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
260 static WCHAR line[MAX_STATIC_BUFFER];
261 int len, lh, count=i_count;
263 int lmargin = 0, rmargin = 0;
264 int x = rect->left, y = rect->top;
265 int width = rect->right - rect->left;
268 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
269 rect->left, rect->top, rect->right, rect->bottom);
271 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
272 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
275 if (count == -1) count = strlenW(str);
276 if (count == 0) return 0;
279 GetTextMetricsW(hdc, &tm);
280 if (flags & DT_EXTERNALLEADING)
281 lh = tm.tmHeight + tm.tmExternalLeading;
287 lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
288 rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
289 if (!(flags & (DT_CENTER | DT_RIGHT)))
291 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
294 tabstop = ((flags & DT_TABSTOP) && dtp) ? dtp->iTabLength : 8;
296 if (flags & DT_EXPANDTABS)
298 GetTextExtentPointW(hdc, SPACEW, 1, &size);
299 spacewidth = size.cx;
300 GetTextExtentPointW(hdc, oW, 1, &size);
301 tabwidth = size.cx * tabstop;
304 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
309 len = MAX_STATIC_BUFFER;
310 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags);
312 if (!GetTextExtentPointW(hdc, line, len, &size)) return 0;
313 if (flags & DT_CENTER) x = (rect->left + rect->right -
315 else if (flags & DT_RIGHT) x = rect->right - size.cx;
317 if (flags & DT_SINGLELINE)
319 if (flags & DT_VCENTER) y = rect->top +
320 (rect->bottom - rect->top) / 2 - size.cy / 2;
321 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
324 if ((flags & DT_SINGLELINE) && size.cx > width &&
325 (flags & (DT_PATH_ELLIPSIS | DT_END_ELLIPSIS | DT_WORD_ELLIPSIS)))
327 WCHAR swapStr[countof(line)];
328 WCHAR* fnameDelim = NULL;
329 int totalLen = i_count >= 0 ? i_count : strlenW(str);
330 int fnameLen = totalLen;
331 int old_prefix_offset = prefix_offset;
333 /* allow room for '...' */
334 count = min(totalLen+3, countof(line)-3);
336 if (flags & DT_WORD_ELLIPSIS)
337 flags |= DT_WORDBREAK;
339 if (flags & DT_PATH_ELLIPSIS)
341 WCHAR* lastBkSlash = NULL;
342 WCHAR* lastFwdSlash = NULL;
343 strncpyW(line, str, totalLen);
344 line[totalLen] = '\0';
345 lastBkSlash = strrchrW(line, BACK_SLASHW[0]);
346 lastFwdSlash = strrchrW(line, FORWARD_SLASHW[0]);
347 fnameDelim = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
350 fnameLen = &line[totalLen] - fnameDelim;
352 fnameDelim = (WCHAR*)str;
354 strcpyW(swapStr, ELLIPSISW);
355 strncpyW(swapStr+strlenW(swapStr), fnameDelim, fnameLen);
356 swapStr[fnameLen+3] = '\0';
357 strncpyW(swapStr+strlenW(swapStr), str, totalLen - fnameLen);
358 swapStr[totalLen+3] = '\0';
360 else /* DT_END_ELLIPSIS | DT_WORD_ELLIPSIS */
362 strcpyW(swapStr, ELLIPSISW);
363 strncpyW(swapStr+strlenW(swapStr), str, totalLen);
366 len = MAX_STATIC_BUFFER;
367 TEXT_NextLineW(hdc, swapStr, &count, line, &len, width, flags);
368 prefix_offset = old_prefix_offset;
370 /* if only the ELLIPSIS will fit, just let it be clipped */
372 GetTextExtentPointW(hdc, line, len, &size);
375 * NextLine uses GetTextExtentPoint for each character,
376 * rather than GetCharABCWidth... So the whitespace between
377 * characters is ignored in the width measurement, and the
378 * reported len is too great. To compensate, we must get
379 * the width of the entire line and adjust len accordingly.
381 while ((size.cx > width) && (len > 3))
384 GetTextExtentPointW(hdc, line, len, &size);
387 if (fnameLen < len-3) /* some of the path will fit */
389 /* put the ELLIPSIS between the path and filename */
390 strncpyW(swapStr, &line[fnameLen+3], len-3-fnameLen);
391 swapStr[len-3-fnameLen] = '\0';
392 strcatW(swapStr, ELLIPSISW);
393 strncpyW(swapStr+strlenW(swapStr), &line[3], fnameLen);
397 /* move the ELLIPSIS to the end */
398 strncpyW(swapStr, &line[3], len-3);
399 swapStr[len-3] = '\0';
400 strcpyW(swapStr+strlenW(swapStr), ELLIPSISW);
403 strncpyW(line, swapStr, len);
407 /* Note that really we ought to refigure the location of
408 * the underline for the prefix; it might be currently set for
409 * something we have just ellipsified out.
410 * NB We had better do it before modifying the string and
411 * loosing the ampersands
414 if (flags & DT_MODIFYSTRING)
415 strcpyW(str, swapStr);
417 if (!(flags & DT_CALCRECT))
419 if (!ExtTextOutW( hdc, x, y,
420 ((flags & DT_NOCLIP) ? 0 : ETO_CLIPPED) |
421 ((flags & DT_RTLREADING) ? ETO_RTLREADING : 0),
422 rect, line, len, NULL )) return 0;
423 if (prefix_offset != -1)
424 TEXT_DrawUnderscore (hdc, x, y + tm.tmAscent + 1, line, prefix_offset);
426 else if (size.cx > max_width)
432 if (!(flags & DT_NOCLIP))
434 if (y > rect->bottom - lh)
439 dtp->uiLengthDrawn += len;
443 if (flags & DT_CALCRECT)
445 rect->right = rect->left + max_width;
448 rect->right += lmargin + rmargin;
450 return y - rect->top;
453 /***********************************************************************
454 * DrawTextExA (USER32.@)
456 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
457 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
463 if (count == -1) count = strlen(str);
464 if (!count) return 0;
465 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
466 wstr = HeapAlloc(GetProcessHeap(), 0, wcount * sizeof(WCHAR));
469 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
470 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
471 if (flags & DT_MODIFYSTRING)
472 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, count, NULL, NULL );
473 HeapFree(GetProcessHeap(), 0, wstr);
478 /***********************************************************************
479 * DrawTextW (USER32.@)
481 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
485 memset (&dtp, 0, sizeof(dtp));
486 if (flags & DT_TABSTOP)
488 dtp.iTabLength = (flags >> 8) && 0xff;
491 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, &dtp);
494 /***********************************************************************
495 * DrawTextA (USER32.@)
497 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
501 memset (&dtp, 0, sizeof(dtp));
502 if (flags & DT_TABSTOP)
504 dtp.iTabLength = (flags >> 8) && 0xff;
507 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, &dtp );
510 /***********************************************************************
513 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
520 CONV_RECT16TO32( rect, &rect32 );
521 ret = DrawTextA( hdc, str, count, &rect32, flags );
522 CONV_RECT32TO16( &rect32, rect );
524 else ret = DrawTextA( hdc, str, count, NULL, flags);
529 /***********************************************************************
531 * GrayString functions
534 /* ### start build ### */
535 extern WORD CALLBACK TEXT_CallTo16_word_wlw(GRAYSTRINGPROC16,WORD,LONG,WORD);
536 /* ### stop build ### */
538 struct gray_string_info
540 GRAYSTRINGPROC16 proc;
544 /* callback for 16-bit gray string proc */
545 static BOOL CALLBACK gray_string_callback( HDC hdc, LPARAM param, INT len )
547 const struct gray_string_info *info = (struct gray_string_info *)param;
548 return TEXT_CallTo16_word_wlw( info->proc, hdc, info->param, len );
551 /***********************************************************************
554 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
555 * heap and we can guarantee that the handles fit in an INT16. We have to
556 * rethink the strategy once the migration to NT handles is complete.
557 * We are going to get a lot of code-duplication once this migration is
561 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
562 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
564 HBITMAP hbm, hbmsave;
572 if(!hdc) return FALSE;
573 if (!(memdc = CreateCompatibleDC(hdc))) return FALSE;
578 slen = lstrlenW((LPCWSTR)lp);
580 slen = strlen((LPCSTR)lp);
582 slen = strlen(MapSL(lp));
585 if((cx == 0 || cy == 0) && slen != -1)
589 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
591 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
593 GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
594 if(cx == 0) cx = s.cx;
595 if(cy == 0) cy = s.cy;
598 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
599 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
600 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
601 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
602 SelectObject( memdc, hbsave );
603 SetTextColor(memdc, RGB(255, 255, 255));
604 SetBkColor(memdc, RGB(0, 0, 0));
605 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
610 retval = fn(memdc, lp, slen);
612 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
617 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
619 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
621 TextOutA(memdc, 0, 0, MapSL(lp), slen);
624 SelectObject(memdc, hfsave);
627 * Windows doc says that the bitmap isn't grayed when len == -1 and
628 * the callback function returns FALSE. However, testing this on
629 * win95 showed otherwise...
631 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
632 if(retval || len != -1)
635 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
636 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
637 SelectObject(memdc, hbsave);
640 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
641 fg = SetTextColor(hdc, RGB(0, 0, 0));
642 bg = SetBkColor(hdc, RGB(255, 255, 255));
643 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
644 SetTextColor(hdc, fg);
646 if(hb) SelectObject(hdc, hbsave);
648 SelectObject(memdc, hbmsave);
655 /***********************************************************************
656 * GrayString (USER.185)
658 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
659 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
662 struct gray_string_info info;
664 if (!gsprc) return TEXT_GrayString(hdc, hbr, NULL, lParam, cch, x, y, cx, cy, FALSE, FALSE);
667 return TEXT_GrayString( hdc, hbr, gray_string_callback, (LPARAM)&info,
668 cch, x, y, cx, cy, FALSE, FALSE);
672 /***********************************************************************
673 * GrayStringA (USER32.@)
675 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
676 LPARAM lParam, INT cch, INT x, INT y,
679 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
684 /***********************************************************************
685 * GrayStringW (USER32.@)
687 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
688 LPARAM lParam, INT cch, INT x, INT y,
691 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
695 /***********************************************************************
697 * TabbedText functions
700 /***********************************************************************
703 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
704 * Note: this doesn't work too well for text-alignment modes other
705 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
707 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
708 INT count, INT cTabStops, const INT16 *lpTabPos16,
709 const INT *lpTabPos32, INT nTabOrg,
722 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
728 GetTextMetricsA( hdc, &tm );
729 defWidth = 8 * tm.tmAveCharWidth;
734 for (i = 0; i < count; i++)
735 if (lpstr[i] == '\t') break;
736 GetTextExtentPointA( hdc, lpstr, i, &extent );
739 while ((cTabStops > 0) &&
740 (nTabOrg + *lpTabPos32 <= x + extent.cx))
748 while ((cTabStops > 0) &&
749 (nTabOrg + *lpTabPos16 <= x + extent.cx))
756 tabPos = x + extent.cx;
757 else if (cTabStops > 0)
758 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
760 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
767 r.bottom = y + extent.cy;
768 ExtTextOutA( hdc, x, y,
769 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
770 &r, lpstr, i, NULL );
776 return MAKELONG(tabPos - start, extent.cy);
780 /***********************************************************************
781 * TabbedTextOut (USER.196)
783 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
784 INT16 count, INT16 cTabStops,
785 const INT16 *lpTabPos, INT16 nTabOrg )
787 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
788 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
789 lpTabPos, NULL, nTabOrg, TRUE );
793 /***********************************************************************
794 * TabbedTextOutA (USER32.@)
796 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
797 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
799 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
800 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
801 NULL, lpTabPos, nTabOrg, TRUE );
805 /***********************************************************************
806 * TabbedTextOutW (USER32.@)
808 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
809 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
814 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
816 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
817 p = HeapAlloc( GetProcessHeap(), 0, acount );
818 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
819 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
820 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
821 HeapFree( GetProcessHeap(), 0, p );
826 /***********************************************************************
827 * GetTabbedTextExtent (USER.197)
829 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
830 INT16 cTabStops, const INT16 *lpTabPos )
832 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
833 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
834 lpTabPos, NULL, 0, FALSE );
838 /***********************************************************************
839 * GetTabbedTextExtentA (USER32.@)
841 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
842 INT cTabStops, const INT *lpTabPos )
844 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
845 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
846 NULL, lpTabPos, 0, FALSE );
850 /***********************************************************************
851 * GetTabbedTextExtentW (USER32.@)
853 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
854 INT cTabStops, const INT *lpTabPos )
859 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
861 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
862 p = HeapAlloc( GetProcessHeap(), 0, acount );
863 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
864 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
865 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
866 HeapFree( GetProcessHeap(), 0, p );