4 * Copyright 1993, 1994 Alexandre Julliard
18 DEFAULT_DEBUG_CHANNEL(text)
26 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
28 static int tabstop = 8;
30 static int spacewidth;
31 static int prefix_offset;
33 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
34 char *dest, int *len, int width, WORD format)
36 /* Return next line of text from a string.
39 * str - string to parse into lines.
40 * count - length of str.
41 * dest - destination in which to return line.
42 * len - length of resultant line in dest in chars.
43 * width - maximum width of line in pixels.
44 * format - format type passed to DrawText.
46 * Returns pointer to next char in str after end of the line
47 * or NULL if end of str reached.
55 int wb_i = 0, wb_j = 0, wb_count = 0;
63 if (!(format & DT_SINGLELINE))
65 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
76 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
77 (format & DT_WORDBREAK))
79 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
86 if (!(format & DT_NOPREFIX) && *count > 1)
88 if (str[++i] == PREFIX)
96 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
97 (format & DT_WORDBREAK))
99 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
106 if (format & DT_EXPANDTABS)
112 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
116 numspaces = (tabwidth - size.cx) / spacewidth;
117 for (k = 0; k < numspaces; k++)
119 plen += tabwidth - size.cx;
120 lasttab = wb_j + numspaces;
124 dest[j++] = str[i++];
125 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
126 (format & DT_WORDBREAK))
128 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
136 dest[j++] = str[i++];
137 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
138 (format & DT_WORDBREAK))
143 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
150 dest[j++] = str[i++];
151 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
152 (format & DT_WORDBREAK))
154 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
161 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
165 if (format & DT_WORDBREAK)
170 *count = wb_count - 1;
188 /***********************************************************************
189 * DrawText16 (USER.85)
191 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
192 LPRECT16 rect, UINT16 flags )
196 static char line[1024];
197 int len, lh, count=i_count;
201 int x = rect->left, y = rect->top;
202 int width = rect->right - rect->left;
205 TRACE(text,"%s, %d , [(%d,%d),(%d,%d)]\n",
206 debugstr_an (str, count), count,
207 rect->left, rect->top, rect->right, rect->bottom);
209 if (count == -1) count = strlen(str);
212 GetTextMetrics16(hdc, &tm);
213 if (flags & DT_EXTERNALLEADING)
214 lh = tm.tmHeight + tm.tmExternalLeading;
218 if (flags & DT_TABSTOP)
219 tabstop = flags >> 8;
221 if (flags & DT_EXPANDTABS)
223 GetTextExtentPoint16(hdc, " ", 1, &size);
224 spacewidth = size.cx;
225 GetTextExtentPoint16(hdc, "o", 1, &size);
226 tabwidth = size.cx * tabstop;
229 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
234 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
236 if (prefix_offset != -1)
238 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
240 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
241 prefix_end = size.cx - 1;
244 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
245 if (flags & DT_CENTER) x = (rect->left + rect->right -
247 else if (flags & DT_RIGHT) x = rect->right - size.cx;
249 if (flags & DT_SINGLELINE)
251 if (flags & DT_VCENTER) y = rect->top +
252 (rect->bottom - rect->top) / 2 - size.cy / 2;
253 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
255 if (!(flags & DT_CALCRECT))
257 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
258 rect, line, len, NULL )) return 0;
259 if (prefix_offset != -1)
261 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
262 HPEN oldPen = SelectObject( hdc, hpen );
263 MoveTo16(hdc, x + prefix_x, y + tm.tmAscent + 1 );
264 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
265 SelectObject( hdc, oldPen );
266 DeleteObject( hpen );
269 else if (size.cx > max_width)
275 if (!(flags & DT_NOCLIP))
277 if (y > rect->bottom - lh)
283 if (flags & DT_CALCRECT)
285 rect->right = rect->left + max_width;
288 return y - rect->top;
292 /***********************************************************************
293 * DrawText32A (USER32.164)
295 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count,
296 LPRECT rect, UINT flags )
302 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
303 CONV_RECT32TO16( rect, &rect16 );
304 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
305 CONV_RECT16TO32( &rect16, rect );
310 /***********************************************************************
311 * DrawText32W (USER32.167)
313 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
314 LPRECT rect, UINT flags )
316 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
317 INT ret = DrawTextA( hdc, p, count, rect, flags );
318 HeapFree( GetProcessHeap(), 0, p );
322 /***********************************************************************
323 * DrawTextEx32A (USER32.165)
325 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
326 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
328 TRACE(text,"(%d,'%s',%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
330 FIXME(text,"Ignores params:%d,%d,%d,%d,%d\n",dtp->cbSize,
331 dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin,
334 return DrawTextA(hdc,str,count,rect,flags);
337 /***********************************************************************
338 * DrawTextEx32W (USER32.166)
340 INT WINAPI DrawTextExW( HDC hdc, LPCWSTR str, INT count,
341 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
343 TRACE(text,"(%d,%p,%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
344 FIXME(text,"ignores extended functionality\n");
345 return DrawTextW(hdc,str,count,rect,flags);
348 /***********************************************************************
349 * ExtTextOut16 (GDI.351)
351 BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
352 const RECT16 *lprect, LPCSTR str, UINT16 count,
360 if (lpDx) lpdx32 = (LPINT)HEAP_xalloc( GetProcessHeap(), 0,
362 if (lprect) CONV_RECT16TO32(lprect,&rect32);
363 if (lpdx32) for (i=count;i--;) lpdx32[i]=lpDx[i];
364 ret = ExtTextOutA(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
365 if (lpdx32) HeapFree( GetProcessHeap(), 0, lpdx32 );
372 /***********************************************************************
373 * ExtTextOut32A (GDI32.98)
375 BOOL WINAPI ExtTextOutA( HDC hdc, INT x, INT y, UINT flags,
376 const RECT *lprect, LPCSTR str, UINT count,
379 DC * dc = DC_GetDCPtr( hdc );
380 return dc && dc->funcs->pExtTextOut &&
381 dc->funcs->pExtTextOut(dc,x,y,flags,lprect,str,count,lpDx);
385 /***********************************************************************
386 * ExtTextOut32W (GDI32.99)
388 BOOL WINAPI ExtTextOutW( HDC hdc, INT x, INT y, UINT flags,
389 const RECT *lprect, LPCWSTR str, UINT count,
392 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
393 INT ret = ExtTextOutA( hdc, x, y, flags, lprect, p, count, lpDx );
394 HeapFree( GetProcessHeap(), 0, p );
399 /***********************************************************************
402 BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
404 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
408 /***********************************************************************
409 * TextOut32A (GDI32.355)
411 BOOL WINAPI TextOutA( HDC hdc, INT x, INT y, LPCSTR str, INT count )
413 return ExtTextOutA( hdc, x, y, 0, NULL, str, count, NULL );
417 /***********************************************************************
418 * TextOut32W (GDI32.356)
420 BOOL WINAPI TextOutW(HDC hdc, INT x, INT y, LPCWSTR str, INT count)
422 return ExtTextOutW( hdc, x, y, 0, NULL, str, count, NULL );
426 /***********************************************************************
429 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
430 * heap and we can guarantee that the handles fit in an INT16. We have to
431 * rethink the strategy once the migration to NT handles is complete.
432 * We are going to get a lot of code-duplication once this migration is
436 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb,
437 GRAYSTRINGPROC fn, LPARAM lp, INT len,
438 INT x, INT y, INT cx, INT cy,
439 BOOL unicode, BOOL _32bit)
441 HBITMAP hbm, hbmsave;
444 HDC memdc = CreateCompatibleDC(hdc);
450 if(!hdc) return FALSE;
455 slen = lstrlenW((LPCWSTR)lp);
457 slen = lstrlenA((LPCSTR)lp);
459 slen = lstrlenA((LPCSTR)PTR_SEG_TO_LIN(lp));
462 if((cx == 0 || cy == 0) && slen != -1)
466 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
468 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
470 GetTextExtentPoint32A(hdc, (LPCSTR)PTR_SEG_TO_LIN(lp), slen, &s);
471 if(cx == 0) cx = s.cx;
472 if(cy == 0) cy = s.cy;
479 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
480 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
481 FillRect(memdc, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));
482 SetTextColor(memdc, RGB(255, 255, 255));
483 SetBkColor(memdc, RGB(0, 0, 0));
484 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
488 retval = fn(memdc, lp, slen);
490 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
493 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
495 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
497 TextOutA(memdc, 0, 0, (LPCSTR)PTR_SEG_TO_LIN(lp), slen);
499 SelectObject(memdc, hfsave);
502 * Windows doc says that the bitmap isn't grayed when len == -1 and
503 * the callback function returns FALSE. However, testing this on
504 * win95 showed otherwise...
506 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
507 if(retval || len != -1)
510 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
511 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
512 SelectObject(memdc, hbsave);
515 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
516 fg = SetTextColor(hdc, RGB(0, 0, 0));
517 bg = SetBkColor(hdc, RGB(255, 255, 255));
518 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
519 SetTextColor(hdc, fg);
521 if(hb) SelectObject(hdc, hbsave);
523 SelectObject(memdc, hbmsave);
530 /***********************************************************************
531 * GrayString16 (USER.185)
533 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
534 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
537 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch, x, y, cx, cy, FALSE, FALSE);
541 /***********************************************************************
542 * GrayString32A (USER32.315)
544 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
545 LPARAM lParam, INT cch, INT x, INT y,
548 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, FALSE, TRUE);
552 /***********************************************************************
553 * GrayString32W (USER32.316)
555 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
556 LPARAM lParam, INT cch, INT x, INT y,
559 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, TRUE, TRUE);
563 /***********************************************************************
566 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
567 * Note: this doesn't work too well for text-alignment modes other
568 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
570 LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
571 INT count, INT cTabStops, const INT16 *lpTabPos16,
572 const INT *lpTabPos32, INT nTabOrg,
582 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
588 GetTextMetrics16( hdc, &tm );
589 defWidth = 8 * tm.tmAveCharWidth;
594 for (i = 0; i < count; i++)
595 if (lpstr[i] == '\t') break;
596 extent = GetTextExtent16( hdc, lpstr, i );
599 while ((cTabStops > 0) &&
600 (nTabOrg + *lpTabPos32 <= x + LOWORD(extent)))
608 while ((cTabStops > 0) &&
609 (nTabOrg + *lpTabPos16 <= x + LOWORD(extent)))
616 tabPos = x + LOWORD(extent);
617 else if (cTabStops > 0)
618 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
620 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
624 SetRect( &r, x, y, tabPos, y+HIWORD(extent) );
625 ExtTextOutA( hdc, x, y,
626 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
627 &r, lpstr, i, NULL );
633 return MAKELONG(tabPos - start, HIWORD(extent));
637 /***********************************************************************
638 * TabbedTextOut16 (USER.196)
640 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
641 INT16 count, INT16 cTabStops,
642 const INT16 *lpTabPos, INT16 nTabOrg )
644 TRACE(text, "%04x %d,%d '%.*s' %d\n",
645 hdc, x, y, count, lpstr, count );
646 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
647 lpTabPos, NULL, nTabOrg, TRUE );
651 /***********************************************************************
652 * TabbedTextOut32A (USER32.542)
654 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr,
655 INT count, INT cTabStops,
656 const INT *lpTabPos, INT nTabOrg )
658 TRACE(text, "%04x %d,%d '%.*s' %d\n",
659 hdc, x, y, count, lpstr, count );
660 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
661 NULL, lpTabPos, nTabOrg, TRUE );
665 /***********************************************************************
666 * TabbedTextOut32W (USER32.543)
668 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str,
669 INT count, INT cTabStops,
670 const INT *lpTabPos, INT nTabOrg )
673 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
674 lstrcpynWtoA( p, str, count + 1 );
675 ret = TabbedTextOutA( hdc, x, y, p, count, cTabStops,
677 HeapFree( GetProcessHeap(), 0, p );
682 /***********************************************************************
683 * GetTabbedTextExtent16 (USER.197)
685 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
686 INT16 cTabStops, const INT16 *lpTabPos )
688 TRACE(text, "%04x '%.*s' %d\n",
689 hdc, count, lpstr, count );
690 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
691 lpTabPos, NULL, 0, FALSE );
695 /***********************************************************************
696 * GetTabbedTextExtent32A (USER32.293)
698 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
699 INT cTabStops, const INT *lpTabPos )
701 TRACE(text, "%04x '%.*s' %d\n",
702 hdc, count, lpstr, count );
703 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
704 NULL, lpTabPos, 0, FALSE );
708 /***********************************************************************
709 * GetTabbedTextExtent32W (USER32.294)
711 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
712 INT cTabStops, const INT *lpTabPos )
715 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
716 lstrcpynWtoA( p, lpstr, count + 1 );
717 ret = GetTabbedTextExtentA( hdc, p, count, cTabStops, lpTabPos );
718 HeapFree( GetProcessHeap(), 0, p );
722 /***********************************************************************
723 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
726 * Should it return a UINT32 instead of an INT32?
727 * => YES, as GetTextCharsetInfo returns UINT32
730 * Success: Character set identifier
731 * Failure: DEFAULT_CHARSET
733 UINT WINAPI GetTextCharset(
734 HDC hdc) /* [in] Handle to device context */
736 /* MSDN docs say this is equivalent */
737 return GetTextCharsetInfo(hdc, NULL, 0);
740 /***********************************************************************
741 * GetTextCharset16 [GDI.612]
743 UINT16 WINAPI GetTextCharset16(HDC16 hdc)
745 return (UINT16)GetTextCharset(hdc);
748 /***********************************************************************
749 * GetTextCharsetInfo [GDI32.381] Gets character set for font
752 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
753 * Should it return a UINT32 instead of an INT32?
754 * => YES and YES, from win32.hlp from Borland
757 * Success: Character set identifier
758 * Failure: DEFAULT_CHARSET
760 UINT WINAPI GetTextCharsetInfo(
761 HDC hdc, /* [in] Handle to device context */
762 LPFONTSIGNATURE fs, /* [out] Pointer to struct to receive data */
763 DWORD flags) /* [in] Reserved - must be 0 */
766 UINT charSet = DEFAULT_CHARSET;
770 hFont = GetCurrentObject(hdc, OBJ_FONT);
772 return(DEFAULT_CHARSET);
773 if ( GetObjectW(hFont, sizeof(LOGFONTW), &lf) != 0 )
774 charSet = lf.lfCharSet;
777 if (!TranslateCharsetInfo((LPDWORD)charSet, &csinfo, TCI_SRCCHARSET))
778 return (DEFAULT_CHARSET);
779 memcpy(fs, &csinfo.fs, sizeof(FONTSIGNATURE));