4 * Copyright 1993, 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
19 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(text);
31 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
33 static int tabstop = 8;
35 static int spacewidth;
36 static int prefix_offset;
38 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
39 char *dest, int *len, int width, WORD format)
41 /* Return next line of text from a string.
44 * str - string to parse into lines.
45 * count - length of str.
46 * dest - destination in which to return line.
47 * len - length of resultant line in dest in chars.
48 * width - maximum width of line in pixels.
49 * format - format type passed to DrawText.
51 * Returns pointer to next char in str after end of the line
52 * or NULL if end of str reached.
60 int wb_i = 0, wb_j = 0, wb_count = 0;
68 if (!(format & DT_SINGLELINE))
70 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
81 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
82 (format & DT_WORDBREAK))
84 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
91 if (!(format & DT_NOPREFIX) && *count > 1)
93 if (str[++i] == PREFIX)
100 dest[j++] = str[i++];
101 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
102 (format & DT_WORDBREAK))
104 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
111 if (format & DT_EXPANDTABS)
117 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
121 numspaces = (tabwidth - size.cx) / spacewidth;
122 for (k = 0; k < numspaces; k++)
124 plen += tabwidth - size.cx;
125 lasttab = wb_j + numspaces;
129 dest[j++] = str[i++];
130 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
131 (format & DT_WORDBREAK))
133 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
141 dest[j++] = str[i++];
142 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
143 (format & DT_WORDBREAK))
148 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
155 dest[j++] = str[i++];
156 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
157 (format & DT_WORDBREAK))
159 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
166 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
170 if (format & DT_WORDBREAK)
175 *count = wb_count - 1;
193 /***********************************************************************
194 * DrawText16 (USER.85)
196 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
197 LPRECT16 rect, UINT16 flags )
201 static char line[1024];
202 int len, lh, count=i_count;
206 int x = rect->left, y = rect->top;
207 int width = rect->right - rect->left;
210 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n",
211 debugstr_an (str, count), count,
212 rect->left, rect->top, rect->right, rect->bottom);
215 if (count == -1) count = strlen(str);
218 GetTextMetrics16(hdc, &tm);
219 if (flags & DT_EXTERNALLEADING)
220 lh = tm.tmHeight + tm.tmExternalLeading;
224 if (flags & DT_TABSTOP)
225 tabstop = flags >> 8;
227 if (flags & DT_EXPANDTABS)
229 GetTextExtentPoint16(hdc, " ", 1, &size);
230 spacewidth = size.cx;
231 GetTextExtentPoint16(hdc, "o", 1, &size);
232 tabwidth = size.cx * tabstop;
235 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
240 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
242 if (prefix_offset != -1)
244 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
246 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
247 prefix_end = size.cx - 1;
250 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
251 if (flags & DT_CENTER) x = (rect->left + rect->right -
253 else if (flags & DT_RIGHT) x = rect->right - size.cx;
255 if (flags & DT_SINGLELINE)
257 if (flags & DT_VCENTER) y = rect->top +
258 (rect->bottom - rect->top) / 2 - size.cy / 2;
259 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
261 if (!(flags & DT_CALCRECT))
263 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
264 rect, line, len, NULL )) return 0;
265 if (prefix_offset != -1)
267 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
268 HPEN oldPen = SelectObject( hdc, hpen );
269 MoveTo16(hdc, x + prefix_x, y + tm.tmAscent + 1 );
270 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
271 SelectObject( hdc, oldPen );
272 DeleteObject( hpen );
275 else if (size.cx > max_width)
281 if (!(flags & DT_NOCLIP))
283 if (y > rect->bottom - lh)
289 if (flags & DT_CALCRECT)
291 rect->right = rect->left + max_width;
294 return y - rect->top;
298 /***********************************************************************
299 * DrawTextA (USER32.164)
301 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count,
302 LPRECT rect, UINT flags )
308 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
309 CONV_RECT32TO16( rect, &rect16 );
310 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
311 CONV_RECT16TO32( &rect16, rect );
316 /***********************************************************************
317 * DrawTextW (USER32.167)
319 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
320 LPRECT rect, UINT flags )
322 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
323 INT ret = DrawTextA( hdc, p, count, rect, flags );
324 HeapFree( GetProcessHeap(), 0, p );
328 /***********************************************************************
329 * DrawTextExA (USER32.165)
331 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
332 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
334 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
336 FIXME("Ignores params:%d,%d,%d,%d,%d\n",dtp->cbSize,
337 dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin,
340 return DrawTextA(hdc,str,count,rect,flags);
343 /***********************************************************************
344 * DrawTextExW (USER32.166)
346 INT WINAPI DrawTextExW( HDC hdc, LPCWSTR str, INT count,
347 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
349 TRACE("(%d,%p,%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
350 FIXME("ignores extended functionality\n");
351 return DrawTextW(hdc,str,count,rect,flags);
354 /***********************************************************************
355 * ExtTextOut16 (GDI.351)
357 BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
358 const RECT16 *lprect, LPCSTR str, UINT16 count,
366 if (lpDx) lpdx32 = (LPINT)HEAP_xalloc( GetProcessHeap(), 0,
368 if (lprect) CONV_RECT16TO32(lprect,&rect32);
369 if (lpdx32) for (i=count;i--;) lpdx32[i]=lpDx[i];
370 ret = ExtTextOutA(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
371 if (lpdx32) HeapFree( GetProcessHeap(), 0, lpdx32 );
378 /***********************************************************************
379 * ExtTextOutA (GDI32.98)
381 BOOL WINAPI ExtTextOutA( HDC hdc, INT x, INT y, UINT flags,
382 const RECT *lprect, LPCSTR str, UINT count,
387 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
390 wlen = MultiByteToWideChar(codepage,0,str,count,NULL,0);
391 p = HeapAlloc( GetProcessHeap(), 0, wlen * sizeof(WCHAR) );
392 wlen = MultiByteToWideChar(codepage,0,str,count,p,wlen);
394 ret = ExtTextOutW( hdc, x, y, flags, lprect, p, wlen, lpDx );
395 HeapFree( GetProcessHeap(), 0, p );
400 /***********************************************************************
401 * ExtTextOutW (GDI32.99)
403 BOOL WINAPI ExtTextOutW( HDC hdc, INT x, INT y, UINT flags,
404 const RECT *lprect, LPCWSTR str, UINT count,
407 DC * dc = DC_GetDCPtr( hdc );
408 return dc && dc->funcs->pExtTextOut &&
409 dc->funcs->pExtTextOut(dc,x,y,flags,lprect,str,count,lpDx);
413 /***********************************************************************
416 BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
418 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
422 /***********************************************************************
423 * TextOutA (GDI32.355)
425 BOOL WINAPI TextOutA( HDC hdc, INT x, INT y, LPCSTR str, INT count )
427 return ExtTextOutA( hdc, x, y, 0, NULL, str, count, NULL );
431 /***********************************************************************
432 * TextOutW (GDI32.356)
434 BOOL WINAPI TextOutW(HDC hdc, INT x, INT y, LPCWSTR str, INT count)
436 return ExtTextOutW( hdc, x, y, 0, NULL, str, count, NULL );
440 /***********************************************************************
443 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
444 * heap and we can guarantee that the handles fit in an INT16. We have to
445 * rethink the strategy once the migration to NT handles is complete.
446 * We are going to get a lot of code-duplication once this migration is
450 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb,
451 GRAYSTRINGPROC fn, LPARAM lp, INT len,
452 INT x, INT y, INT cx, INT cy,
453 BOOL unicode, BOOL _32bit)
455 HBITMAP hbm, hbmsave;
458 HDC memdc = CreateCompatibleDC(hdc);
463 if(!hdc) return FALSE;
468 slen = lstrlenW((LPCWSTR)lp);
470 slen = lstrlenA((LPCSTR)lp);
472 slen = lstrlenA((LPCSTR)PTR_SEG_TO_LIN(lp));
475 if((cx == 0 || cy == 0) && slen != -1)
479 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
481 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
483 GetTextExtentPoint32A(hdc, (LPCSTR)PTR_SEG_TO_LIN(lp), slen, &s);
484 if(cx == 0) cx = s.cx;
485 if(cy == 0) cy = s.cy;
488 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
489 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
490 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
491 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
492 SelectObject( memdc, hbsave );
493 SetTextColor(memdc, RGB(255, 255, 255));
494 SetBkColor(memdc, RGB(0, 0, 0));
495 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
499 retval = fn(memdc, lp, slen);
501 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
504 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
506 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
508 TextOutA(memdc, 0, 0, (LPCSTR)PTR_SEG_TO_LIN(lp), slen);
510 SelectObject(memdc, hfsave);
513 * Windows doc says that the bitmap isn't grayed when len == -1 and
514 * the callback function returns FALSE. However, testing this on
515 * win95 showed otherwise...
517 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
518 if(retval || len != -1)
521 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
522 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
523 SelectObject(memdc, hbsave);
526 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
527 fg = SetTextColor(hdc, RGB(0, 0, 0));
528 bg = SetBkColor(hdc, RGB(255, 255, 255));
529 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
530 SetTextColor(hdc, fg);
532 if(hb) SelectObject(hdc, hbsave);
534 SelectObject(memdc, hbmsave);
541 /***********************************************************************
542 * GrayString16 (USER.185)
544 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
545 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
548 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch, x, y, cx, cy, FALSE, FALSE);
552 /***********************************************************************
553 * GrayStringA (USER32.315)
555 BOOL WINAPI GrayStringA( 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, FALSE, TRUE);
563 /***********************************************************************
564 * GrayStringW (USER32.316)
566 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
567 LPARAM lParam, INT cch, INT x, INT y,
570 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, TRUE, TRUE);
574 /***********************************************************************
577 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
578 * Note: this doesn't work too well for text-alignment modes other
579 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
581 LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
582 INT count, INT cTabStops, const INT16 *lpTabPos16,
583 const INT *lpTabPos32, INT nTabOrg,
593 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
599 GetTextMetrics16( hdc, &tm );
600 defWidth = 8 * tm.tmAveCharWidth;
605 for (i = 0; i < count; i++)
606 if (lpstr[i] == '\t') break;
607 extent = GetTextExtent16( hdc, lpstr, i );
610 while ((cTabStops > 0) &&
611 (nTabOrg + *lpTabPos32 <= x + LOWORD(extent)))
619 while ((cTabStops > 0) &&
620 (nTabOrg + *lpTabPos16 <= x + LOWORD(extent)))
627 tabPos = x + LOWORD(extent);
628 else if (cTabStops > 0)
629 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
631 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
638 r.bottom = y + HIWORD(extent);
639 ExtTextOutA( hdc, x, y,
640 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
641 &r, lpstr, i, NULL );
647 return MAKELONG(tabPos - start, HIWORD(extent));
651 /***********************************************************************
652 * TabbedTextOut16 (USER.196)
654 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
655 INT16 count, INT16 cTabStops,
656 const INT16 *lpTabPos, INT16 nTabOrg )
658 TRACE("%04x %d,%d '%.*s' %d\n",
659 hdc, x, y, count, lpstr, count );
660 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
661 lpTabPos, NULL, nTabOrg, TRUE );
665 /***********************************************************************
666 * TabbedTextOutA (USER32.542)
668 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr,
669 INT count, INT cTabStops,
670 const INT *lpTabPos, INT nTabOrg )
672 TRACE("%04x %d,%d '%.*s' %d\n",
673 hdc, x, y, count, lpstr, count );
674 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
675 NULL, lpTabPos, nTabOrg, TRUE );
679 /***********************************************************************
680 * TabbedTextOutW (USER32.543)
682 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str,
683 INT count, INT cTabStops,
684 const INT *lpTabPos, INT nTabOrg )
689 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
691 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
692 p = HEAP_xalloc( GetProcessHeap(), 0, acount );
693 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
694 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops,
696 HeapFree( GetProcessHeap(), 0, p );
701 /***********************************************************************
702 * GetTabbedTextExtent16 (USER.197)
704 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
705 INT16 cTabStops, const INT16 *lpTabPos )
707 TRACE("%04x '%.*s' %d\n",
708 hdc, count, lpstr, count );
709 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
710 lpTabPos, NULL, 0, FALSE );
714 /***********************************************************************
715 * GetTabbedTextExtentA (USER32.293)
717 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
718 INT cTabStops, const INT *lpTabPos )
720 TRACE("%04x '%.*s' %d\n",
721 hdc, count, lpstr, count );
722 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
723 NULL, lpTabPos, 0, FALSE );
727 /***********************************************************************
728 * GetTabbedTextExtentW (USER32.294)
730 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
731 INT cTabStops, const INT *lpTabPos )
736 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
738 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
739 p = HEAP_xalloc( GetProcessHeap(), 0, acount );
740 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
741 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
742 HeapFree( GetProcessHeap(), 0, p );
746 /***********************************************************************
747 * GetTextCharset [GDI32.226] Gets character set for font in DC
750 * Should it return a UINT32 instead of an INT32?
751 * => YES, as GetTextCharsetInfo returns UINT32
754 * Success: Character set identifier
755 * Failure: DEFAULT_CHARSET
757 UINT WINAPI GetTextCharset(
758 HDC hdc) /* [in] Handle to device context */
760 /* MSDN docs say this is equivalent */
761 return GetTextCharsetInfo(hdc, NULL, 0);
764 /***********************************************************************
765 * GetTextCharset16 [GDI.612]
767 UINT16 WINAPI GetTextCharset16(HDC16 hdc)
769 return (UINT16)GetTextCharset(hdc);
772 /***********************************************************************
773 * GetTextCharsetInfo [GDI32.381] Gets character set for font
776 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
777 * Should it return a UINT32 instead of an INT32?
778 * => YES and YES, from win32.hlp from Borland
781 * Success: Character set identifier
782 * Failure: DEFAULT_CHARSET
784 UINT WINAPI GetTextCharsetInfo(
785 HDC hdc, /* [in] Handle to device context */
786 LPFONTSIGNATURE fs, /* [out] Pointer to struct to receive data */
787 DWORD flags) /* [in] Reserved - must be 0 */
790 UINT charSet = DEFAULT_CHARSET;
794 hFont = GetCurrentObject(hdc, OBJ_FONT);
796 return(DEFAULT_CHARSET);
797 if ( GetObjectW(hFont, sizeof(LOGFONTW), &lf) != 0 )
798 charSet = lf.lfCharSet;
801 if (!TranslateCharsetInfo((LPDWORD)charSet, &csinfo, TCI_SRCCHARSET))
802 return (DEFAULT_CHARSET);
803 memcpy(fs, &csinfo.fs, sizeof(FONTSIGNATURE));
808 /***********************************************************************
809 * PolyTextOutA [GDI.402] Draw several Strings
811 BOOL WINAPI PolyTextOutA (
812 HDC hdc, /* Handle to device context */
813 PPOLYTEXTA pptxt, /* array of strings */
814 INT cStrings /* Number of strings in array */
818 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED );
824 /***********************************************************************
825 * PolyTextOutW [GDI.403] Draw several Strings
827 BOOL WINAPI PolyTextOutW (
828 HDC hdc, /* Handle to device context */
829 PPOLYTEXTW pptxt, /* array of strings */
830 INT cStrings /* Number of strings in array */
834 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED );