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 - length of resultant line in dest in chars.
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;
80 if (!(format & DT_SINGLELINE))
82 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
93 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
94 (format & DT_WORDBREAK))
96 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
103 if (!(format & DT_NOPREFIX) && *count > 1)
105 if (str[++i] == PREFIX)
112 dest[j++] = str[i++];
113 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
114 (format & DT_WORDBREAK))
116 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
123 if (format & DT_EXPANDTABS)
129 if (!GetTextExtentPointW(hdc, &dest[lasttab], j - lasttab, &size))
132 numspaces = (tabwidth - size.cx) / spacewidth;
133 for (k = 0; k < numspaces; k++)
135 plen += tabwidth - size.cx;
136 lasttab = wb_j + numspaces;
140 dest[j++] = str[i++];
141 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
142 (format & DT_WORDBREAK))
144 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
152 dest[j++] = str[i++];
153 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
154 (format & DT_WORDBREAK))
159 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
166 dest[j++] = str[i++];
167 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
168 (format & DT_WORDBREAK))
170 if (!GetTextExtentPointW(hdc, &dest[j-1], 1, &size))
177 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
181 if (format & DT_WORDBREAK)
186 *count = wb_count - 1;
204 /***********************************************************************
205 * DrawText16 (USER.85)
207 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
214 CONV_RECT16TO32( rect, &rect32 );
215 ret = DrawTextA( hdc, str, count, &rect32, flags );
216 CONV_RECT32TO16( &rect32, rect );
218 else ret = DrawTextA( hdc, str, count, NULL, flags);
223 /***********************************************************************
224 * DrawTextExW (USER32.166)
226 INT WINAPI DrawTextExW( HDC hdc, LPCWSTR str, INT i_count,
227 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
231 static WCHAR line[1024];
232 int len, lh, count=i_count;
236 int x = rect->left, y = rect->top;
237 int width = rect->right - rect->left;
240 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str, count), count,
241 rect->left, rect->top, rect->right, rect->bottom);
244 FIXME("Ignores params:%d,%d,%d,%d\n", dtp->cbSize,
245 dtp->iTabLength, dtp->iLeftMargin, dtp->iRightMargin);
249 if (count == -1) count = strlenW(str);
250 if (count == 0) return 0;
253 GetTextMetricsW(hdc, &tm);
254 if (flags & DT_EXTERNALLEADING)
255 lh = tm.tmHeight + tm.tmExternalLeading;
259 if (flags & DT_TABSTOP)
260 tabstop = flags >> 8;
262 if (flags & DT_EXPANDTABS)
264 GetTextExtentPointW(hdc, SPACEW, 1, &size);
265 spacewidth = size.cx;
266 GetTextExtentPointW(hdc, oW, 1, &size);
267 tabwidth = size.cx * tabstop;
270 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
275 strPtr = TEXT_NextLineW(hdc, strPtr, &count, line, &len, width, flags);
277 if (prefix_offset != -1)
279 GetTextExtentPointW(hdc, line, prefix_offset, &size);
281 GetTextExtentPointW(hdc, line, prefix_offset + 1, &size);
282 prefix_end = size.cx - 1;
285 if (!GetTextExtentPointW(hdc, line, len, &size)) return 0;
286 if (flags & DT_CENTER) x = (rect->left + rect->right -
288 else if (flags & DT_RIGHT) x = rect->right - size.cx;
290 if (flags & DT_SINGLELINE)
292 if (flags & DT_VCENTER) y = rect->top +
293 (rect->bottom - rect->top) / 2 - size.cy / 2;
294 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
296 if (flags & (DT_PATH_ELLIPSIS | DT_END_ELLIPSIS | DT_WORD_ELLIPSIS))
298 WCHAR swapStr[sizeof(line)];
299 WCHAR* fnameDelim = NULL;
300 int totalLen = i_count >= 0 ? i_count : strlenW(str);
304 int fnameLen = totalLen;
306 /* allow room for '...' */
307 count = min(totalLen+3, sizeof(line)-3);
309 if (flags & DT_WORD_ELLIPSIS)
310 flags |= DT_WORDBREAK;
312 if (flags & DT_PATH_ELLIPSIS)
314 WCHAR* lastBkSlash = NULL;
315 WCHAR* lastFwdSlash = NULL;
316 strncpyW(line, str, totalLen);
317 line[totalLen] = '\0';
318 lastBkSlash = strrchrW(line, BACK_SLASHW[0]);
319 lastFwdSlash = strrchrW(line, FORWARD_SLASHW[0]);
320 fnameDelim = lastFwdSlash ? lastFwdSlash : lastBkSlash;
321 if (lastBkSlash && lastFwdSlash) /* which is last? */
322 if (lastBkSlash > lastFwdSlash)
323 fnameDelim = lastBkSlash;
326 fnameLen = &line[totalLen] - fnameDelim;
328 fnameDelim = (WCHAR*)str;
330 strcpyW(swapStr, ELLIPSISW);
331 strncpyW(swapStr+strlenW(swapStr), fnameDelim, fnameLen);
332 swapStr[fnameLen+3] = '\0';
333 strncpyW(swapStr+strlenW(swapStr), str, totalLen - fnameLen);
334 swapStr[totalLen+3] = '\0';
336 else /* DT_END_ELLIPSIS | DT_WORD_ELLIPSIS */
338 strcpyW(swapStr, ELLIPSISW);
339 strncpyW(swapStr+strlenW(swapStr), str, totalLen);
342 TEXT_NextLineW(hdc, swapStr, &count, line, &len, width, flags);
344 /* if only the ELLIPSIS will fit, just let it be clipped */
346 GetTextExtentPointW(hdc, line, len, &size);
349 * NextLine uses GetTextExtentPoint for each character,
350 * rather than GetCharABCWidth... So the whitespace between
351 * characters is ignored in the width measurement, and the
352 * reported len is too great. To compensate, we must get
353 * the width of the entire line and adjust len accordingly.
355 while ((size.cx > width) && (len > 3))
358 GetTextExtentPointW(hdc, line, len, &size);
361 if (fnameLen < len-3) /* some of the path will fit */
363 /* put the ELLIPSIS between the path and filename */
364 strncpyW(swapStr, &line[fnameLen+3], len-3-fnameLen);
365 swapStr[len-3-fnameLen] = '\0';
366 strcatW(swapStr, ELLIPSISW);
367 strncpyW(swapStr+strlenW(swapStr), &line[3], fnameLen);
371 /* move the ELLIPSIS to the end */
372 strncpyW(swapStr, &line[3], len-3);
373 swapStr[len-3] = '\0';
374 strcpyW(swapStr+strlenW(swapStr), ELLIPSISW);
377 strncpyW(line, swapStr, len);
383 if (!(flags & DT_CALCRECT))
385 if (!ExtTextOutW(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
386 rect, line, len, NULL )) return 0;
387 if (prefix_offset != -1)
389 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
390 HPEN oldPen = SelectObject( hdc, hpen );
391 MoveToEx(hdc, x + prefix_x, y + tm.tmAscent + 1, NULL );
392 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
393 SelectObject( hdc, oldPen );
394 DeleteObject( hpen );
397 else if (size.cx > max_width)
403 if (!(flags & DT_NOCLIP))
405 if (y > rect->bottom - lh)
411 if (flags & DT_CALCRECT)
413 rect->right = rect->left + max_width;
416 return y - rect->top;
419 /***********************************************************************
420 * DrawTextA (USER32.164)
422 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count, LPRECT rect, UINT flags )
428 if (count == -1) count = strlen(str);
429 if (!count) return 0;
430 wcount = MultiByteToWideChar( CP_ACP, 0, str, count, NULL, 0 );
431 wstr = HeapAlloc(GetProcessHeap(), 0, wcount * sizeof(WCHAR));
434 MultiByteToWideChar( CP_ACP, 0, str, count, wstr, wcount );
435 ret = DrawTextExW( hdc, wstr, wcount, rect, flags, NULL );
436 HeapFree(GetProcessHeap(), 0, wstr);
441 /***********************************************************************
442 * DrawTextW (USER32.167)
444 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
445 LPRECT rect, UINT flags )
447 return DrawTextExW(hdc, str, count, rect, flags, NULL);
450 /***********************************************************************
451 * DrawTextExA (USER32.165)
453 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
454 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
456 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
458 FIXME("Ignores params:%d,%d,%d,%d\n",dtp->cbSize,
459 dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin);
461 return DrawTextA(hdc,str,count,rect,flags);
464 /***********************************************************************
467 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
468 * heap and we can guarantee that the handles fit in an INT16. We have to
469 * rethink the strategy once the migration to NT handles is complete.
470 * We are going to get a lot of code-duplication once this migration is
474 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
475 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
477 HBITMAP hbm, hbmsave;
480 HDC memdc = CreateCompatibleDC(hdc);
485 if(!hdc) return FALSE;
490 slen = lstrlenW((LPCWSTR)lp);
492 slen = strlen((LPCSTR)lp);
494 slen = strlen(MapSL(lp));
497 if((cx == 0 || cy == 0) && slen != -1)
501 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
503 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
505 GetTextExtentPoint32A(hdc, MapSL(lp), slen, &s);
506 if(cx == 0) cx = s.cx;
507 if(cy == 0) cy = s.cy;
510 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
511 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
512 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
513 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
514 SelectObject( memdc, hbsave );
515 SetTextColor(memdc, RGB(255, 255, 255));
516 SetBkColor(memdc, RGB(0, 0, 0));
517 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
522 retval = fn(memdc, lp, slen);
524 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
529 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
531 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
533 TextOutA(memdc, 0, 0, MapSL(lp), slen);
536 SelectObject(memdc, hfsave);
539 * Windows doc says that the bitmap isn't grayed when len == -1 and
540 * the callback function returns FALSE. However, testing this on
541 * win95 showed otherwise...
543 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
544 if(retval || len != -1)
547 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
548 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
549 SelectObject(memdc, hbsave);
552 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
553 fg = SetTextColor(hdc, RGB(0, 0, 0));
554 bg = SetBkColor(hdc, RGB(255, 255, 255));
555 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
556 SetTextColor(hdc, fg);
558 if(hb) SelectObject(hdc, hbsave);
560 SelectObject(memdc, hbmsave);
567 /***********************************************************************
568 * GrayString16 (USER.185)
570 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
571 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
574 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch,
575 x, y, cx, cy, FALSE, FALSE);
579 /***********************************************************************
580 * GrayStringA (USER32.315)
582 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
583 LPARAM lParam, INT cch, INT x, INT y,
586 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
591 /***********************************************************************
592 * GrayStringW (USER32.316)
594 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
595 LPARAM lParam, INT cch, INT x, INT y,
598 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy,
602 /***********************************************************************
605 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
606 * Note: this doesn't work too well for text-alignment modes other
607 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
609 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
610 INT count, INT cTabStops, const INT16 *lpTabPos16,
611 const INT *lpTabPos32, INT nTabOrg,
624 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
630 GetTextMetricsA( hdc, &tm );
631 defWidth = 8 * tm.tmAveCharWidth;
636 for (i = 0; i < count; i++)
637 if (lpstr[i] == '\t') break;
638 GetTextExtentPointA( hdc, lpstr, i, &extent );
641 while ((cTabStops > 0) &&
642 (nTabOrg + *lpTabPos32 <= x + extent.cx))
650 while ((cTabStops > 0) &&
651 (nTabOrg + *lpTabPos16 <= x + extent.cx))
658 tabPos = x + extent.cx;
659 else if (cTabStops > 0)
660 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
662 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
669 r.bottom = y + extent.cy;
670 ExtTextOutA( hdc, x, y,
671 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
672 &r, lpstr, i, NULL );
678 return MAKELONG(tabPos - start, extent.cy);
682 /***********************************************************************
683 * TabbedTextOut16 (USER.196)
685 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
686 INT16 count, INT16 cTabStops,
687 const INT16 *lpTabPos, INT16 nTabOrg )
689 TRACE("%04x %d,%d '%.*s' %d\n", hdc, x, y, count, lpstr, count );
690 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
691 lpTabPos, NULL, nTabOrg, TRUE );
695 /***********************************************************************
696 * TabbedTextOutA (USER32.542)
698 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr, INT count,
699 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
701 TRACE("%04x %d,%d '%.*s' %d\n", hdc, x, y, count, lpstr, count );
702 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
703 NULL, lpTabPos, nTabOrg, TRUE );
707 /***********************************************************************
708 * TabbedTextOutW (USER32.543)
710 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str, INT count,
711 INT cTabStops, const INT *lpTabPos, INT nTabOrg )
716 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
718 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
719 p = HeapAlloc( GetProcessHeap(), 0, acount );
720 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
721 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
722 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops, lpTabPos, nTabOrg );
723 HeapFree( GetProcessHeap(), 0, p );
728 /***********************************************************************
729 * GetTabbedTextExtent16 (USER.197)
731 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
732 INT16 cTabStops, const INT16 *lpTabPos )
734 TRACE("%04x '%.*s' %d\n", hdc, count, lpstr, count );
735 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
736 lpTabPos, NULL, 0, FALSE );
740 /***********************************************************************
741 * GetTabbedTextExtentA (USER32.293)
743 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
744 INT cTabStops, const INT *lpTabPos )
746 TRACE("%04x '%.*s' %d\n", hdc, count, lpstr, count );
747 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
748 NULL, lpTabPos, 0, FALSE );
752 /***********************************************************************
753 * GetTabbedTextExtentW (USER32.294)
755 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
756 INT cTabStops, const INT *lpTabPos )
761 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
763 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
764 p = HeapAlloc( GetProcessHeap(), 0, acount );
765 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
766 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
767 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
768 HeapFree( GetProcessHeap(), 0, p );