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, LPCWSTR 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 x = rect->left, y = rect->top;
239 int width = rect->right - rect->left;
242 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
243 rect->left, rect->top, rect->right, rect->bottom);
246 FIXME("Ignores params:%d,%d,%d,%d\n", dtp->cbSize,
247 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
251 if (count == -1) count = strlenW(str);
252 if (count == 0) return 0;
255 GetTextMetricsW(hdc, &tm);
256 if (flags & DT_EXTERNALLEADING)
257 lh = tm.tmHeight + tm.tmExternalLeading;
261 if (flags & DT_TABSTOP)
262 tabstop = flags >> 8;
264 if (flags & DT_EXPANDTABS)
266 GetTextExtentPointW(hdc, SPACEW, 1, &size);
267 spacewidth = size.cx;
268 GetTextExtentPointW(hdc, oW, 1, &size);
269 tabwidth = size.cx * tabstop;
272 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
277 len = MAX_STATIC_BUFFER;
278 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags);
280 if (prefix_offset != -1)
282 GetTextExtentPointW(hdc, line, prefix_offset, &size);
284 GetTextExtentPointW(hdc, line, prefix_offset + 1, &size);
285 prefix_end = size.cx - 1;
288 if (!GetTextExtentPointW(hdc, line, len, &size)) return 0;
289 if (flags & DT_CENTER) x = (rect->left + rect->right -
291 else if (flags & DT_RIGHT) x = rect->right - size.cx;
293 if (flags & DT_SINGLELINE)
295 if (flags & DT_VCENTER) y = rect->top +
296 (rect->bottom - rect->top) / 2 - size.cy / 2;
297 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
299 if (flags & (DT_PATH_ELLIPSIS | DT_END_ELLIPSIS | DT_WORD_ELLIPSIS))
301 WCHAR swapStr[sizeof(line)];
302 WCHAR* fnameDelim = NULL;
303 int totalLen = i_count >= 0 ? i_count : strlenW(str);
307 int fnameLen = totalLen;
309 /* allow room for '...' */
310 count = min(totalLen+3, sizeof(line)-3);
312 if (flags & DT_WORD_ELLIPSIS)
313 flags |= DT_WORDBREAK;
315 if (flags & DT_PATH_ELLIPSIS)
317 WCHAR* lastBkSlash = NULL;
318 WCHAR* lastFwdSlash = NULL;
319 strncpyW(line, str, totalLen);
320 line[totalLen] = '\0';
321 lastBkSlash = strrchrW(line, BACK_SLASHW[0]);
322 lastFwdSlash = strrchrW(line, FORWARD_SLASHW[0]);
323 fnameDelim = lastFwdSlash ? lastFwdSlash : lastBkSlash;
324 if (lastBkSlash && lastFwdSlash) /* which is last? */
325 if (lastBkSlash > lastFwdSlash)
326 fnameDelim = lastBkSlash;
329 fnameLen = &line[totalLen] - fnameDelim;
331 fnameDelim = (WCHAR*)str;
333 strcpyW(swapStr, ELLIPSISW);
334 strncpyW(swapStr+strlenW(swapStr), fnameDelim, fnameLen);
335 swapStr[fnameLen+3] = '\0';
336 strncpyW(swapStr+strlenW(swapStr), str, totalLen - fnameLen);
337 swapStr[totalLen+3] = '\0';
339 else /* DT_END_ELLIPSIS | DT_WORD_ELLIPSIS */
341 strcpyW(swapStr, ELLIPSISW);
342 strncpyW(swapStr+strlenW(swapStr), str, totalLen);
345 len = MAX_STATIC_BUFFER;
346 TEXT_NextLineW(hdc, swapStr, &count, line, &len, width, flags);
348 /* if only the ELLIPSIS will fit, just let it be clipped */
350 GetTextExtentPointW(hdc, line, len, &size);
353 * NextLine uses GetTextExtentPoint for each character,
354 * rather than GetCharABCWidth... So the whitespace between
355 * characters is ignored in the width measurement, and the
356 * reported len is too great. To compensate, we must get
357 * the width of the entire line and adjust len accordingly.
359 while ((size.cx > width) && (len > 3))
362 GetTextExtentPointW(hdc, line, len, &size);
365 if (fnameLen < len-3) /* some of the path will fit */
367 /* put the ELLIPSIS between the path and filename */
368 strncpyW(swapStr, &line[fnameLen+3], len-3-fnameLen);
369 swapStr[len-3-fnameLen] = '\0';
370 strcatW(swapStr, ELLIPSISW);
371 strncpyW(swapStr+strlenW(swapStr), &line[3], fnameLen);
375 /* move the ELLIPSIS to the end */
376 strncpyW(swapStr, &line[3], len-3);
377 swapStr[len-3] = '\0';
378 strcpyW(swapStr+strlenW(swapStr), ELLIPSISW);
381 strncpyW(line, swapStr, len);
387 if (!(flags & DT_CALCRECT))
389 if (!ExtTextOutW(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
390 rect, line, len, NULL )) return 0;
391 if (prefix_offset != -1)
393 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
394 HPEN oldPen = SelectObject( hdc, hpen );
395 MoveToEx(hdc, x + prefix_x, y + tm.tmAscent + 1, NULL );
396 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
397 SelectObject( hdc, oldPen );
398 DeleteObject( hpen );
401 else if (size.cx > max_width)
407 if (!(flags & DT_NOCLIP))
409 if (y > rect->bottom - lh)
415 if (flags & DT_CALCRECT)
417 rect->right = rect->left + max_width;
420 return y - rect->top;
423 /***********************************************************************
424 * DrawTextA (USER32.@)
426 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
432 if (count == -1) count = strlen(str);
433 if (!count) return 0;
434 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
435 wstr = HeapAlloc(GetProcessHeap(), 0, wcount * sizeof(WCHAR));
438 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
439 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
440 HeapFree(GetProcessHeap(), 0, wstr);
445 /***********************************************************************
446 * DrawTextW (USER32.@)
448 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
449 LPRECT rect, UINT flags )
451 return DrawTextExW(hdc, str, count, rect, flags, NULL);
454 /***********************************************************************
455 * DrawTextExA (USER32.@)
457 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
458 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
460 TRACE("(%d,%s,%d,%p,0x%08x,%p)\n",hdc,debugstr_an(str,count),count,rect,flags,dtp);
462 FIXME("Ignores params:%d,%d,%d,%d\n",dtp->cbSize,
463 dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin);
465 return DrawTextA(hdc,str,count,rect,flags);
468 /***********************************************************************
471 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
472 * heap and we can guarantee that the handles fit in an INT16. We have to
473 * rethink the strategy once the migration to NT handles is complete.
474 * We are going to get a lot of code-duplication once this migration is
478 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
479 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
481 HBITMAP hbm, hbmsave;
484 HDC memdc = CreateCompatibleDC(hdc);
489 if(!hdc) return FALSE;
494 slen = lstrlenW((LPCWSTR)lp);
496 slen = strlen((LPCSTR)lp);
498 slen = strlen(MapSL(lp));
501 if((cx == 0 || cy == 0) && slen != -1)
505 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
507 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
509 GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
510 if(cx == 0) cx = s.cx;
511 if(cy == 0) cy = s.cy;
514 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
515 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
516 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
517 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
518 SelectObject( memdc, hbsave );
519 SetTextColor(memdc, RGB(255, 255, 255));
520 SetBkColor(memdc, RGB(0, 0, 0));
521 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
526 retval = fn(memdc, lp, slen);
528 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
533 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
535 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
537 TextOutA(memdc, 0, 0, MapSL(lp), slen);
540 SelectObject(memdc, hfsave);
543 * Windows doc says that the bitmap isn't grayed when len == -1 and
544 * the callback function returns FALSE. However, testing this on
545 * win95 showed otherwise...
547 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
548 if(retval || len != -1)
551 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
552 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
553 SelectObject(memdc, hbsave);
556 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
557 fg = SetTextColor(hdc, RGB(0, 0, 0));
558 bg = SetBkColor(hdc, RGB(255, 255, 255));
559 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
560 SetTextColor(hdc, fg);
562 if(hb) SelectObject(hdc, hbsave);
564 SelectObject(memdc, hbmsave);
571 /***********************************************************************
572 * GrayString16 (USER.185)
574 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
575 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
578 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch,
579 x, y, cx, cy, FALSE, FALSE);
583 /***********************************************************************
584 * GrayStringA (USER32.@)
586 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
587 LPARAM lParam, INT cch, INT x, INT y,
590 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
595 /***********************************************************************
596 * GrayStringW (USER32.@)
598 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
599 LPARAM lParam, INT cch, INT x, INT y,
602 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
606 /***********************************************************************
609 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
610 * Note: this doesn't work too well for text-alignment modes other
611 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
613 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
614 INT count, INT cTabStops, const INT16 *lpTabPos16,
615 const INT *lpTabPos32, INT nTabOrg,
628 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
634 GetTextMetricsA( hdc, &tm );
635 defWidth = 8 * tm.tmAveCharWidth;
640 for (i = 0; i < count; i++)
641 if (lpstr[i] == '\t') break;
642 GetTextExtentPointA( hdc, lpstr, i, &extent );
645 while ((cTabStops > 0) &&
646 (nTabOrg + *lpTabPos32 <= x + extent.cx))
654 while ((cTabStops > 0) &&
655 (nTabOrg + *lpTabPos16 <= x + extent.cx))
662 tabPos = x + extent.cx;
663 else if (cTabStops > 0)
664 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
666 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
673 r.bottom = y + extent.cy;
674 ExtTextOutA( hdc, x, y,
675 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
676 &r, lpstr, i, NULL );
682 return MAKELONG(tabPos - start, extent.cy);
686 /***********************************************************************
687 * TabbedTextOut (USER.196)
689 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
690 INT16 count, INT16 cTabStops,
691 const INT16 *lpTabPos, INT16 nTabOrg )
693 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
694 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
695 lpTabPos, NULL, nTabOrg, TRUE );
699 /***********************************************************************
700 * TabbedTextOutA (USER32.@)
702 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
703 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
705 TRACE("%04x %d,%d %s %d\n", hdc, x, y, debugstr_an(lpstr,count), count );
706 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
707 NULL, lpTabPos, nTabOrg, TRUE );
711 /***********************************************************************
712 * TabbedTextOutW (USER32.@)
714 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
715 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
720 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
722 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
723 p = HeapAlloc( GetProcessHeap(), 0, acount );
724 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
725 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
726 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
727 HeapFree( GetProcessHeap(), 0, p );
732 /***********************************************************************
733 * GetTabbedTextExtent (USER.197)
735 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
736 INT16 cTabStops, const INT16 *lpTabPos )
738 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
739 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
740 lpTabPos, NULL, 0, FALSE );
744 /***********************************************************************
745 * GetTabbedTextExtentA (USER32.@)
747 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
748 INT cTabStops, const INT *lpTabPos )
750 TRACE("%04x %s %d\n", hdc, debugstr_an(lpstr,count), count );
751 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
752 NULL, lpTabPos, 0, FALSE );
756 /***********************************************************************
757 * GetTabbedTextExtentW (USER32.@)
759 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
760 INT cTabStops, const INT *lpTabPos )
765 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
767 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
768 p = HeapAlloc( GetProcessHeap(), 0, acount );
769 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
770 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
771 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
772 HeapFree( GetProcessHeap(), 0, p );