4 * Copyright 1993, 1994 Alexandre Julliard
10 #include "wine/winuser16.h"
17 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(text)
29 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
31 static int tabstop = 8;
33 static int spacewidth;
34 static int prefix_offset;
36 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
37 char *dest, int *len, int width, WORD format)
39 /* Return next line of text from a string.
42 * str - string to parse into lines.
43 * count - length of str.
44 * dest - destination in which to return line.
45 * len - length of resultant line in dest in chars.
46 * width - maximum width of line in pixels.
47 * format - format type passed to DrawText.
49 * Returns pointer to next char in str after end of the line
50 * or NULL if end of str reached.
58 int wb_i = 0, wb_j = 0, wb_count = 0;
66 if (!(format & DT_SINGLELINE))
68 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
79 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
80 (format & DT_WORDBREAK))
82 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
89 if (!(format & DT_NOPREFIX) && *count > 1)
91 if (str[++i] == PREFIX)
99 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
100 (format & DT_WORDBREAK))
102 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
109 if (format & DT_EXPANDTABS)
115 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
119 numspaces = (tabwidth - size.cx) / spacewidth;
120 for (k = 0; k < numspaces; k++)
122 plen += tabwidth - size.cx;
123 lasttab = wb_j + numspaces;
127 dest[j++] = str[i++];
128 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
129 (format & DT_WORDBREAK))
131 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
139 dest[j++] = str[i++];
140 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
141 (format & DT_WORDBREAK))
146 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
153 dest[j++] = str[i++];
154 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
155 (format & DT_WORDBREAK))
157 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
164 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
168 if (format & DT_WORDBREAK)
173 *count = wb_count - 1;
191 /***********************************************************************
192 * DrawText16 (USER.85)
194 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
195 LPRECT16 rect, UINT16 flags )
199 static char line[1024];
200 int len, lh, count=i_count;
204 int x = rect->left, y = rect->top;
205 int width = rect->right - rect->left;
208 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n",
209 debugstr_an (str, count), count,
210 rect->left, rect->top, rect->right, rect->bottom);
213 if (count == -1) count = strlen(str);
216 GetTextMetrics16(hdc, &tm);
217 if (flags & DT_EXTERNALLEADING)
218 lh = tm.tmHeight + tm.tmExternalLeading;
222 if (flags & DT_TABSTOP)
223 tabstop = flags >> 8;
225 if (flags & DT_EXPANDTABS)
227 GetTextExtentPoint16(hdc, " ", 1, &size);
228 spacewidth = size.cx;
229 GetTextExtentPoint16(hdc, "o", 1, &size);
230 tabwidth = size.cx * tabstop;
233 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
238 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
240 if (prefix_offset != -1)
242 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
244 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
245 prefix_end = size.cx - 1;
248 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
249 if (flags & DT_CENTER) x = (rect->left + rect->right -
251 else if (flags & DT_RIGHT) x = rect->right - size.cx;
253 if (flags & DT_SINGLELINE)
255 if (flags & DT_VCENTER) y = rect->top +
256 (rect->bottom - rect->top) / 2 - size.cy / 2;
257 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
259 if (!(flags & DT_CALCRECT))
261 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
262 rect, line, len, NULL )) return 0;
263 if (prefix_offset != -1)
265 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
266 HPEN oldPen = SelectObject( hdc, hpen );
267 MoveTo16(hdc, x + prefix_x, y + tm.tmAscent + 1 );
268 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
269 SelectObject( hdc, oldPen );
270 DeleteObject( hpen );
273 else if (size.cx > max_width)
279 if (!(flags & DT_NOCLIP))
281 if (y > rect->bottom - lh)
287 if (flags & DT_CALCRECT)
289 rect->right = rect->left + max_width;
292 return y - rect->top;
296 /***********************************************************************
297 * DrawText32A (USER32.164)
299 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT count,
300 LPRECT rect, UINT flags )
306 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
307 CONV_RECT32TO16( rect, &rect16 );
308 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
309 CONV_RECT16TO32( &rect16, rect );
314 /***********************************************************************
315 * DrawText32W (USER32.167)
317 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
318 LPRECT rect, UINT flags )
320 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
321 INT ret = DrawTextA( hdc, p, count, rect, flags );
322 HeapFree( GetProcessHeap(), 0, p );
326 /***********************************************************************
327 * DrawTextEx32A (USER32.165)
329 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
330 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
332 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
334 FIXME("Ignores params:%d,%d,%d,%d,%d\n",dtp->cbSize,
335 dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin,
338 return DrawTextA(hdc,str,count,rect,flags);
341 /***********************************************************************
342 * DrawTextEx32W (USER32.166)
344 INT WINAPI DrawTextExW( HDC hdc, LPCWSTR str, INT count,
345 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
347 TRACE("(%d,%p,%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
348 FIXME("ignores extended functionality\n");
349 return DrawTextW(hdc,str,count,rect,flags);
352 /***********************************************************************
353 * ExtTextOut16 (GDI.351)
355 BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
356 const RECT16 *lprect, LPCSTR str, UINT16 count,
364 if (lpDx) lpdx32 = (LPINT)HEAP_xalloc( GetProcessHeap(), 0,
366 if (lprect) CONV_RECT16TO32(lprect,&rect32);
367 if (lpdx32) for (i=count;i--;) lpdx32[i]=lpDx[i];
368 ret = ExtTextOutA(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
369 if (lpdx32) HeapFree( GetProcessHeap(), 0, lpdx32 );
376 /***********************************************************************
377 * ExtTextOutA (GDI32.98)
379 BOOL WINAPI ExtTextOutA( HDC hdc, INT x, INT y, UINT flags,
380 const RECT *lprect, LPCSTR str, UINT count,
383 /* str need not be \0 terminated but lstrcpynAtoW adds \0 so we allocate one
385 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, (count+1) * sizeof(WCHAR) );
387 lstrcpynAtoW( p, str, count+1 );
388 ret = ExtTextOutW( hdc, x, y, flags, lprect, p, count, lpDx );
389 HeapFree( GetProcessHeap(), 0, p );
394 /***********************************************************************
395 * ExtTextOutW (GDI32.99)
397 BOOL WINAPI ExtTextOutW( HDC hdc, INT x, INT y, UINT flags,
398 const RECT *lprect, LPCWSTR str, UINT count,
401 DC * dc = DC_GetDCPtr( hdc );
402 return dc && dc->funcs->pExtTextOut &&
403 dc->funcs->pExtTextOut(dc,x,y,flags,lprect,str,count,lpDx);
407 /***********************************************************************
410 BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
412 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
416 /***********************************************************************
417 * TextOut32A (GDI32.355)
419 BOOL WINAPI TextOutA( HDC hdc, INT x, INT y, LPCSTR str, INT count )
421 return ExtTextOutA( hdc, x, y, 0, NULL, str, count, NULL );
425 /***********************************************************************
426 * TextOut32W (GDI32.356)
428 BOOL WINAPI TextOutW(HDC hdc, INT x, INT y, LPCWSTR str, INT count)
430 return ExtTextOutW( hdc, x, y, 0, NULL, str, count, NULL );
434 /***********************************************************************
437 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
438 * heap and we can guarantee that the handles fit in an INT16. We have to
439 * rethink the strategy once the migration to NT handles is complete.
440 * We are going to get a lot of code-duplication once this migration is
444 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb,
445 GRAYSTRINGPROC fn, LPARAM lp, INT len,
446 INT x, INT y, INT cx, INT cy,
447 BOOL unicode, BOOL _32bit)
449 HBITMAP hbm, hbmsave;
452 HDC memdc = CreateCompatibleDC(hdc);
458 if(!hdc) return FALSE;
463 slen = lstrlenW((LPCWSTR)lp);
465 slen = lstrlenA((LPCSTR)lp);
467 slen = lstrlenA((LPCSTR)PTR_SEG_TO_LIN(lp));
470 if((cx == 0 || cy == 0) && slen != -1)
474 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
476 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
478 GetTextExtentPoint32A(hdc, (LPCSTR)PTR_SEG_TO_LIN(lp), slen, &s);
479 if(cx == 0) cx = s.cx;
480 if(cy == 0) cy = s.cy;
487 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
488 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
489 FillRect(memdc, &r, (HBRUSH)GetStockObject(BLACK_BRUSH));
490 SetTextColor(memdc, RGB(255, 255, 255));
491 SetBkColor(memdc, RGB(0, 0, 0));
492 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
496 retval = fn(memdc, lp, slen);
498 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
501 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
503 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
505 TextOutA(memdc, 0, 0, (LPCSTR)PTR_SEG_TO_LIN(lp), slen);
507 SelectObject(memdc, hfsave);
510 * Windows doc says that the bitmap isn't grayed when len == -1 and
511 * the callback function returns FALSE. However, testing this on
512 * win95 showed otherwise...
514 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
515 if(retval || len != -1)
518 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
519 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
520 SelectObject(memdc, hbsave);
523 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
524 fg = SetTextColor(hdc, RGB(0, 0, 0));
525 bg = SetBkColor(hdc, RGB(255, 255, 255));
526 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
527 SetTextColor(hdc, fg);
529 if(hb) SelectObject(hdc, hbsave);
531 SelectObject(memdc, hbmsave);
538 /***********************************************************************
539 * GrayString16 (USER.185)
541 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
542 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
545 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch, x, y, cx, cy, FALSE, FALSE);
549 /***********************************************************************
550 * GrayString32A (USER32.315)
552 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
553 LPARAM lParam, INT cch, INT x, INT y,
556 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, FALSE, TRUE);
560 /***********************************************************************
561 * GrayString32W (USER32.316)
563 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
564 LPARAM lParam, INT cch, INT x, INT y,
567 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, TRUE, TRUE);
571 /***********************************************************************
574 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
575 * Note: this doesn't work too well for text-alignment modes other
576 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
578 LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
579 INT count, INT cTabStops, const INT16 *lpTabPos16,
580 const INT *lpTabPos32, INT nTabOrg,
590 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
596 GetTextMetrics16( hdc, &tm );
597 defWidth = 8 * tm.tmAveCharWidth;
602 for (i = 0; i < count; i++)
603 if (lpstr[i] == '\t') break;
604 extent = GetTextExtent16( hdc, lpstr, i );
607 while ((cTabStops > 0) &&
608 (nTabOrg + *lpTabPos32 <= x + LOWORD(extent)))
616 while ((cTabStops > 0) &&
617 (nTabOrg + *lpTabPos16 <= x + LOWORD(extent)))
624 tabPos = x + LOWORD(extent);
625 else if (cTabStops > 0)
626 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
628 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
632 SetRect( &r, x, y, tabPos, y+HIWORD(extent) );
633 ExtTextOutA( hdc, x, y,
634 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
635 &r, lpstr, i, NULL );
641 return MAKELONG(tabPos - start, HIWORD(extent));
645 /***********************************************************************
646 * TabbedTextOut16 (USER.196)
648 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
649 INT16 count, INT16 cTabStops,
650 const INT16 *lpTabPos, INT16 nTabOrg )
652 TRACE("%04x %d,%d '%.*s' %d\n",
653 hdc, x, y, count, lpstr, count );
654 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
655 lpTabPos, NULL, nTabOrg, TRUE );
659 /***********************************************************************
660 * TabbedTextOut32A (USER32.542)
662 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr,
663 INT count, INT cTabStops,
664 const INT *lpTabPos, INT nTabOrg )
666 TRACE("%04x %d,%d '%.*s' %d\n",
667 hdc, x, y, count, lpstr, count );
668 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
669 NULL, lpTabPos, nTabOrg, TRUE );
673 /***********************************************************************
674 * TabbedTextOut32W (USER32.543)
676 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str,
677 INT count, INT cTabStops,
678 const INT *lpTabPos, INT nTabOrg )
681 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
682 lstrcpynWtoA( p, str, count + 1 );
683 ret = TabbedTextOutA( hdc, x, y, p, count, cTabStops,
685 HeapFree( GetProcessHeap(), 0, p );
690 /***********************************************************************
691 * GetTabbedTextExtent16 (USER.197)
693 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
694 INT16 cTabStops, const INT16 *lpTabPos )
696 TRACE("%04x '%.*s' %d\n",
697 hdc, count, lpstr, count );
698 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
699 lpTabPos, NULL, 0, FALSE );
703 /***********************************************************************
704 * GetTabbedTextExtent32A (USER32.293)
706 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
707 INT cTabStops, const INT *lpTabPos )
709 TRACE("%04x '%.*s' %d\n",
710 hdc, count, lpstr, count );
711 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
712 NULL, lpTabPos, 0, FALSE );
716 /***********************************************************************
717 * GetTabbedTextExtent32W (USER32.294)
719 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
720 INT cTabStops, const INT *lpTabPos )
723 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
724 lstrcpynWtoA( p, lpstr, count + 1 );
725 ret = GetTabbedTextExtentA( hdc, p, count, cTabStops, lpTabPos );
726 HeapFree( GetProcessHeap(), 0, p );
730 /***********************************************************************
731 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
734 * Should it return a UINT32 instead of an INT32?
735 * => YES, as GetTextCharsetInfo returns UINT32
738 * Success: Character set identifier
739 * Failure: DEFAULT_CHARSET
741 UINT WINAPI GetTextCharset(
742 HDC hdc) /* [in] Handle to device context */
744 /* MSDN docs say this is equivalent */
745 return GetTextCharsetInfo(hdc, NULL, 0);
748 /***********************************************************************
749 * GetTextCharset16 [GDI.612]
751 UINT16 WINAPI GetTextCharset16(HDC16 hdc)
753 return (UINT16)GetTextCharset(hdc);
756 /***********************************************************************
757 * GetTextCharsetInfo [GDI32.381] Gets character set for font
760 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
761 * Should it return a UINT32 instead of an INT32?
762 * => YES and YES, from win32.hlp from Borland
765 * Success: Character set identifier
766 * Failure: DEFAULT_CHARSET
768 UINT WINAPI GetTextCharsetInfo(
769 HDC hdc, /* [in] Handle to device context */
770 LPFONTSIGNATURE fs, /* [out] Pointer to struct to receive data */
771 DWORD flags) /* [in] Reserved - must be 0 */
774 UINT charSet = DEFAULT_CHARSET;
778 hFont = GetCurrentObject(hdc, OBJ_FONT);
780 return(DEFAULT_CHARSET);
781 if ( GetObjectW(hFont, sizeof(LOGFONTW), &lf) != 0 )
782 charSet = lf.lfCharSet;
785 if (!TranslateCharsetInfo((LPDWORD)charSet, &csinfo, TCI_SRCCHARSET))
786 return (DEFAULT_CHARSET);
787 memcpy(fs, &csinfo.fs, sizeof(FONTSIGNATURE));
792 /***********************************************************************
793 * PolyTextOutA [GDI.402] Draw several Strings
795 BOOL WINAPI PolyTextOutA (
796 HDC hdc, /* Handle to device context */
797 PPOLYTEXTA pptxt, /* array of strings */
798 INT cStrings /* Number of strings in array */
802 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED );
808 /***********************************************************************
809 * PolyTextOutW [GDI.403] Draw several Strings
811 BOOL WINAPI PolyTextOutW (
812 HDC hdc, /* Handle to device context */
813 PPOLYTEXTW pptxt, /* array of strings */
814 INT cStrings /* Number of strings in array */
818 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED );