4 * Copyright 1993, 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
13 #include "wine/unicode.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(text);
28 #define ELLIPSIS "..."
29 #define FORWARD_SLASH '/'
30 #define BACK_SLASH '\\'
32 static const WCHAR SPACEW[] = {' ', 0};
33 static const WCHAR oW[] = {'o', 0};
34 static const WCHAR ELLIPSISW[] = {'.','.','.', 0};
35 static const WCHAR FORWARD_SLASHW[] = {'/', 0};
36 static const WCHAR BACK_SLASHW[] = {'\\', 0};
38 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
40 static int tabstop = 8;
42 static int spacewidth;
43 static int prefix_offset;
45 /*********************************************************************
46 * Return next line of text from a string.
49 * str - string to parse into lines.
50 * count - length of str.
51 * dest - destination in which to return line.
52 * len - dest buffer size in chars on input, copied length into dest on output.
53 * width - maximum width of line in pixels.
54 * format - format type passed to DrawText.
56 * Returns pointer to next char in str after end of the line
57 * or NULL if end of str reached.
60 * GetTextExtentPoint is used to get the width of each character,
61 * rather than GetCharABCWidth... So the whitespace between
62 * characters is ignored, and the reported len is too great.
64 static const WCHAR *TEXT_NextLineW( HDC hdc, const WCHAR *str, int *count,
65 WCHAR *dest, int *len, int width, WORD format)
72 int wb_i = 0, wb_j = 0, wb_count = 0;
75 while (*count && j < maxl)
81 if (!(format & DT_SINGLELINE))
83 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
94 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
95 (format & DT_WORDBREAK))
97 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
104 if (!(format & DT_NOPREFIX) && *count > 1)
106 if (str[++i] == PREFIX)
113 dest[j++] = str[i++];
114 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
115 (format & DT_WORDBREAK))
117 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
124 if (format & DT_EXPANDTABS)
130 if (!GetTextExtentPointW(hdc, &dest[lasttab], j - lasttab, &size))
133 numspaces = (tabwidth - size.cx) / spacewidth;
134 for (k = 0; k < numspaces; k++)
136 plen += tabwidth - size.cx;
137 lasttab = wb_j + numspaces;
141 dest[j++] = str[i++];
142 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
143 (format & DT_WORDBREAK))
145 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
153 dest[j++] = str[i++];
154 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
155 (format & DT_WORDBREAK))
160 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
167 dest[j++] = str[i++];
168 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
169 (format & DT_WORDBREAK))
171 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
178 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
182 if (format & DT_WORDBREAK)
187 *count = wb_count - 1;
205 /***********************************************************************
208 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
215 CONV_RECT16TO32( rect, &rect32 );
216 ret = DrawTextA( hdc, str, count, &rect32, flags );
217 CONV_RECT32TO16( &rect32, rect );
219 else ret = DrawTextA( hdc, str, count, NULL, flags);
224 /***********************************************************************
225 * DrawTextExW (USER32.@)
227 #define MAX_STATIC_BUFFER 1024
228 INT WINAPI DrawTextExW( HDC hdc, LPWSTR str, INT i_count,
229 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
233 static WCHAR line[MAX_STATIC_BUFFER];
234 int len, lh, count=i_count;
238 int lmargin = 0, rmargin = 0;
239 int x = rect->left, y = rect->top;
240 int width = rect->right - rect->left;
243 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
244 rect->left, rect->top, rect->right, rect->bottom);
246 if (dtp) TRACE("Params: iTabLength=%d, iLeftMargin=%d, iRightMargin=%d\n",
247 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
250 if (count == -1) count = strlenW(str);
251 if (count == 0) return 0;
254 GetTextMetricsW(hdc, &tm);
255 if (flags & DT_EXTERNALLEADING)
256 lh = tm.tmHeight + tm.tmExternalLeading;
262 lmargin = dtp->iLeftMargin * tm.tmAveCharWidth;
263 rmargin = dtp->iRightMargin * tm.tmAveCharWidth;
264 if (!(flags & (DT_CENTER | DT_RIGHT)))
266 dtp->uiLengthDrawn = 0; /* This param RECEIVES number of chars processed */
269 if (flags & DT_TABSTOP)
270 tabstop = dtp ? dtp->iTabLength : flags >> 8;
272 if (flags & DT_EXPANDTABS)
274 GetTextExtentPointW(hdc, SPACEW, 1, &size);
275 spacewidth = size.cx;
276 GetTextExtentPointW(hdc, oW, 1, &size);
277 tabwidth = size.cx * tabstop;
280 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
285 len = MAX_STATIC_BUFFER;
286 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags);
288 if (prefix_offset != -1)
290 GetTextExtentPointW(hdc, line, prefix_offset, &size);
292 GetTextExtentPointW(hdc, line, prefix_offset + 1, &size);
293 prefix_end = size.cx - 1;
296 if (!GetTextExtentPointW(hdc, line, len, &size)) return 0;
297 if (flags & DT_CENTER) x = (rect->left + rect->right -
299 else if (flags & DT_RIGHT) x = rect->right - size.cx;
301 if (flags & DT_SINGLELINE)
303 if (flags & DT_VCENTER) y = rect->top +
304 (rect->bottom - rect->top) / 2 - size.cy / 2;
305 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
307 if (flags & (DT_PATH_ELLIPSIS | DT_END_ELLIPSIS | DT_WORD_ELLIPSIS))
309 WCHAR swapStr[sizeof(line)];
310 WCHAR* fnameDelim = NULL;
311 int totalLen = i_count >= 0 ? i_count : strlenW(str);
315 int fnameLen = totalLen;
317 /* allow room for '...' */
318 count = min(totalLen+3, sizeof(line)-3);
320 if (flags & DT_WORD_ELLIPSIS)
321 flags |= DT_WORDBREAK;
323 if (flags & DT_PATH_ELLIPSIS)
325 WCHAR* lastBkSlash = NULL;
326 WCHAR* lastFwdSlash = NULL;
327 strncpyW(line, str, totalLen);
328 line[totalLen] = '\0';
329 lastBkSlash = strrchrW(line, BACK_SLASHW[0]);
330 lastFwdSlash = strrchrW(line, FORWARD_SLASHW[0]);
331 fnameDelim = lastBkSlash > lastFwdSlash ? lastBkSlash : lastFwdSlash;
334 fnameLen = &line[totalLen] - fnameDelim;
336 fnameDelim = (WCHAR*)str;
338 strcpyW(swapStr, ELLIPSISW);
339 strncpyW(swapStr+strlenW(swapStr), fnameDelim, fnameLen);
340 swapStr[fnameLen+3] = '\0';
341 strncpyW(swapStr+strlenW(swapStr), str, totalLen - fnameLen);
342 swapStr[totalLen+3] = '\0';
344 else /* DT_END_ELLIPSIS | DT_WORD_ELLIPSIS */
346 strcpyW(swapStr, ELLIPSISW);
347 strncpyW(swapStr+strlenW(swapStr), str, totalLen);
350 len = MAX_STATIC_BUFFER;
351 TEXT_NextLineW(hdc, swapStr, &count, line, &len, width, flags);
353 /* if only the ELLIPSIS will fit, just let it be clipped */
355 GetTextExtentPointW(hdc, line, len, &size);
358 * NextLine uses GetTextExtentPoint for each character,
359 * rather than GetCharABCWidth... So the whitespace between
360 * characters is ignored in the width measurement, and the
361 * reported len is too great. To compensate, we must get
362 * the width of the entire line and adjust len accordingly.
364 while ((size.cx > width) && (len > 3))
367 GetTextExtentPointW(hdc, line, len, &size);
370 if (fnameLen < len-3) /* some of the path will fit */
372 /* put the ELLIPSIS between the path and filename */
373 strncpyW(swapStr, &line[fnameLen+3], len-3-fnameLen);
374 swapStr[len-3-fnameLen] = '\0';
375 strcatW(swapStr, ELLIPSISW);
376 strncpyW(swapStr+strlenW(swapStr), &line[3], fnameLen);
380 /* move the ELLIPSIS to the end */
381 strncpyW(swapStr, &line[3], len-3);
382 swapStr[len-3] = '\0';
383 strcpyW(swapStr+strlenW(swapStr), ELLIPSISW);
386 strncpyW(line, swapStr, len);
390 if (flags & DT_MODIFYSTRING)
391 strcpyW(str, swapStr);
394 if (!(flags & DT_CALCRECT))
396 if (!ExtTextOutW( hdc, x, y,
397 ( (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED ) | ( (flags & DT_RTLREADING) ? 0 : ETO_RTLREADING ),
398 rect, line, len, NULL )) return 0;
399 if (prefix_offset != -1)
401 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
402 HPEN oldPen = SelectObject( hdc, hpen );
403 MoveToEx(hdc, x + prefix_x, y + tm.tmAscent + 1, NULL );
404 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
405 SelectObject( hdc, oldPen );
406 DeleteObject( hpen );
409 else if (size.cx > max_width)
415 if (!(flags & DT_NOCLIP))
417 if (y > rect->bottom - lh)
422 dtp->uiLengthDrawn += len;
426 if (flags & DT_CALCRECT)
428 rect->right = rect->left + max_width;
431 rect->right += lmargin + rmargin;
433 return y - rect->top;
436 /***********************************************************************
437 * DrawTextExA (USER32.@)
439 INT WINAPI DrawTextExA( HDC hdc, LPSTR str, INT count,
440 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
446 if (count == -1) count = strlen(str);
447 if (!count) return 0;
448 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
449 wstr = HeapAlloc(GetProcessHeap(), 0, wcount * sizeof(WCHAR));
452 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
453 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
454 if (flags & DT_MODIFYSTRING)
455 WideCharToMultiByte( CP_ACP, 0, wstr, -1, str, count, NULL, NULL );
456 HeapFree(GetProcessHeap(), 0, wstr);
461 /***********************************************************************
462 * DrawTextW (USER32.@)
464 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count, LPRECT rect, UINT flags )
466 return DrawTextExW(hdc, (LPWSTR)str, count, rect, flags, NULL);
469 /***********************************************************************
470 * DrawTextA (USER32.@)
472 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
474 return DrawTextExA( hdc, (LPSTR)str, count, rect, flags, NULL );
477 /***********************************************************************
480 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
481 * heap and we can guarantee that the handles fit in an INT16. We have to
482 * rethink the strategy once the migration to NT handles is complete.
483 * We are going to get a lot of code-duplication once this migration is
487 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
488 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
490 HBITMAP hbm, hbmsave;
493 HDC memdc = CreateCompatibleDC(hdc);
498 if(!hdc) return FALSE;
503 slen = lstrlenW((LPCWSTR)lp);
505 slen = strlen((LPCSTR)lp);
507 slen = strlen(MapSL(lp));
510 if((cx == 0 || cy == 0) && slen != -1)
514 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
516 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
518 GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
519 if(cx == 0) cx = s.cx;
520 if(cy == 0) cy = s.cy;
523 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
524 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
525 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
526 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
527 SelectObject( memdc, hbsave );
528 SetTextColor(memdc, RGB(255, 255, 255));
529 SetBkColor(memdc, RGB(0, 0, 0));
530 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
535 retval = fn(memdc, lp, slen);
537 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
542 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
544 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
546 TextOutA(memdc, 0, 0, MapSL(lp), slen);
549 SelectObject(memdc, hfsave);
552 * Windows doc says that the bitmap isn't grayed when len == -1 and
553 * the callback function returns FALSE. However, testing this on
554 * win95 showed otherwise...
556 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
557 if(retval || len != -1)
560 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
561 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
562 SelectObject(memdc, hbsave);
565 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
566 fg = SetTextColor(hdc, RGB(0, 0, 0));
567 bg = SetBkColor(hdc, RGB(255, 255, 255));
568 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
569 SetTextColor(hdc, fg);
571 if(hb) SelectObject(hdc, hbsave);
573 SelectObject(memdc, hbmsave);
580 /***********************************************************************
581 * GrayString16 (USER.185)
583 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
584 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
587 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch,
588 x, y, cx, cy, FALSE, FALSE);
592 /***********************************************************************
593 * GrayStringA (USER32.@)
595 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
596 LPARAM lParam, INT cch, INT x, INT y,
599 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
604 /***********************************************************************
605 * GrayStringW (USER32.@)
607 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
608 LPARAM lParam, INT cch, INT x, INT y,
611 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
615 /***********************************************************************
618 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
619 * Note: this doesn't work too well for text-alignment modes other
620 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
622 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
623 INT count, INT cTabStops, const INT16 *lpTabPos16,
624 const INT *lpTabPos32, INT nTabOrg,
637 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
643 GetTextMetricsA( hdc, &tm );
644 defWidth = 8 * tm.tmAveCharWidth;
649 for (i = 0; i < count; i++)
650 if (lpstr[i] == '\t') break;
651 GetTextExtentPointA( hdc, lpstr, i, &extent );
654 while ((cTabStops > 0) &&
655 (nTabOrg + *lpTabPos32 <= x + extent.cx))
663 while ((cTabStops > 0) &&
664 (nTabOrg + *lpTabPos16 <= x + extent.cx))
671 tabPos = x + extent.cx;
672 else if (cTabStops > 0)
673 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
675 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
682 r.bottom = y + extent.cy;
683 ExtTextOutA( hdc, x, y,
684 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
685 &r, lpstr, i, NULL );
691 return MAKELONG(tabPos - start, extent.cy);
695 /***********************************************************************
696 * TabbedTextOut (USER.196)
698 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
699 INT16 count, INT16 cTabStops,
700 const INT16 *lpTabPos, INT16 nTabOrg )
702 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
703 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
704 lpTabPos, NULL, nTabOrg, TRUE );
708 /***********************************************************************
709 * TabbedTextOutA (USER32.@)
711 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
712 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
714 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
715 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
716 NULL, lpTabPos, nTabOrg, TRUE );
720 /***********************************************************************
721 * TabbedTextOutW (USER32.@)
723 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
724 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
729 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
731 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
732 p = HeapAlloc( GetProcessHeap(), 0, acount );
733 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
734 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
735 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
736 HeapFree( GetProcessHeap(), 0, p );
741 /***********************************************************************
742 * GetTabbedTextExtent (USER.197)
744 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
745 INT16 cTabStops, const INT16 *lpTabPos )
747 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
748 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
749 lpTabPos, NULL, 0, FALSE );
753 /***********************************************************************
754 * GetTabbedTextExtentA (USER32.@)
756 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
757 INT cTabStops, const INT *lpTabPos )
759 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
760 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
761 NULL, lpTabPos, 0, FALSE );
765 /***********************************************************************
766 * GetTabbedTextExtentW (USER32.@)
768 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
769 INT cTabStops, const INT *lpTabPos )
774 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
776 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
777 p = HeapAlloc( GetProcessHeap(), 0, acount );
778 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
779 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
780 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
781 HeapFree( GetProcessHeap(), 0, p );