4 * Copyright 1993, 1994 Alexandre Julliard
22 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
24 static int tabstop = 8;
26 static int spacewidth;
27 static int prefix_offset;
29 static const char *TEXT_NextLine( HDC16 hdc, const char *str, int *count,
30 char *dest, int *len, int width, WORD format)
32 /* Return next line of text from a string.
35 * str - string to parse into lines.
36 * count - length of str.
37 * dest - destination in which to return line.
38 * len - length of resultant line in dest in chars.
39 * width - maximum width of line in pixels.
40 * format - format type passed to DrawText.
42 * Returns pointer to next char in str after end of the line
43 * or NULL if end of str reached.
51 int wb_i = 0, wb_j = 0, wb_count = 0;
59 if (!(format & DT_SINGLELINE))
61 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
72 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
73 (format & DT_WORDBREAK))
75 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
82 if (!(format & DT_NOPREFIX) && *count > 1)
84 if (str[++i] == PREFIX)
92 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
93 (format & DT_WORDBREAK))
95 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
102 if (format & DT_EXPANDTABS)
108 if (!GetTextExtentPoint16(hdc, &dest[lasttab], j - lasttab,
112 numspaces = (tabwidth - size.cx) / spacewidth;
113 for (k = 0; k < numspaces; k++)
115 plen += tabwidth - size.cx;
116 lasttab = wb_j + numspaces;
120 dest[j++] = str[i++];
121 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
122 (format & DT_WORDBREAK))
124 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
132 dest[j++] = str[i++];
133 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
134 (format & DT_WORDBREAK))
139 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
146 dest[j++] = str[i++];
147 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
148 (format & DT_WORDBREAK))
150 if (!GetTextExtentPoint16(hdc, &dest[j-1], 1, &size))
157 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
161 if (format & DT_WORDBREAK)
166 *count = wb_count - 1;
184 /***********************************************************************
185 * DrawText16 (USER.85)
187 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 i_count,
188 LPRECT16 rect, UINT16 flags )
192 static char line[1024];
193 int len, lh, count=i_count;
197 int x = rect->left, y = rect->top;
198 int width = rect->right - rect->left;
201 dprintf_info(text,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str,
202 count, rect->left, rect->top, rect->right, rect->bottom);
204 if (count == -1) count = strlen(str);
207 GetTextMetrics16(hdc, &tm);
208 if (flags & DT_EXTERNALLEADING)
209 lh = tm.tmHeight + tm.tmExternalLeading;
213 if (flags & DT_TABSTOP)
214 tabstop = flags >> 8;
216 if (flags & DT_EXPANDTABS)
218 GetTextExtentPoint16(hdc, " ", 1, &size);
219 spacewidth = size.cx;
220 GetTextExtentPoint16(hdc, "o", 1, &size);
221 tabwidth = size.cx * tabstop;
224 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
229 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
231 if (prefix_offset != -1)
233 GetTextExtentPoint16(hdc, line, prefix_offset, &size);
235 GetTextExtentPoint16(hdc, line, prefix_offset + 1, &size);
236 prefix_end = size.cx - 1;
239 if (!GetTextExtentPoint16(hdc, line, len, &size)) return 0;
240 if (flags & DT_CENTER) x = (rect->left + rect->right -
242 else if (flags & DT_RIGHT) x = rect->right - size.cx;
244 if (flags & DT_SINGLELINE)
246 if (flags & DT_VCENTER) y = rect->top +
247 (rect->bottom - rect->top) / 2 - size.cy / 2;
248 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
250 if (!(flags & DT_CALCRECT))
252 if (!ExtTextOut16(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
253 rect, line, len, NULL )) return 0;
254 if (prefix_offset != -1)
256 HPEN32 hpen = CreatePen32( PS_SOLID, 1, GetTextColor32(hdc) );
257 HPEN32 oldPen = SelectObject32( hdc, hpen );
258 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
259 LineTo32(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
260 SelectObject32( hdc, oldPen );
261 DeleteObject32( hpen );
264 else if (size.cx > max_width)
270 if (!(flags & DT_NOCLIP))
272 if (y > rect->bottom - lh)
278 if (flags & DT_CALCRECT)
280 rect->right = rect->left + max_width;
283 return y - rect->top;
287 /***********************************************************************
288 * DrawText32A (USER32.163)
290 INT32 WINAPI DrawText32A( HDC32 hdc, LPCSTR str, INT32 count,
291 LPRECT32 rect, UINT32 flags )
297 return DrawText16( (HDC16)hdc, str, (INT16)count, NULL, (UINT16)flags);
298 CONV_RECT32TO16( rect, &rect16 );
299 ret = DrawText16( (HDC16)hdc, str, (INT16)count, &rect16, (UINT16)flags );
300 CONV_RECT16TO32( &rect16, rect );
305 /***********************************************************************
306 * DrawText32W (USER32.166)
308 INT32 WINAPI DrawText32W( HDC32 hdc, LPCWSTR str, INT32 count,
309 LPRECT32 rect, UINT32 flags )
311 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
312 INT32 ret = DrawText32A( hdc, p, count, rect, flags );
313 HeapFree( GetProcessHeap(), 0, p );
317 /***********************************************************************
318 * DrawTextEx32A (USER32.164)
320 INT32 DrawTextEx32A( HDC32 hdc, LPCSTR str, INT32 count,
321 LPRECT32 rect, UINT32 flags, LPDRAWTEXTPARAMS dtp )
323 fprintf(stderr,"DrawTextEx32A(%d,'%s',%d,%p,0x%08x,%p)\n",
324 hdc,str,count,rect,flags,dtp
326 /*FIXME: ignores extended functionality ... */
327 return DrawText32A(hdc,str,count,rect,flags);
330 /***********************************************************************
331 * DrawTextEx32W (USER32.165)
333 INT32 DrawTextEx32W( HDC32 hdc, LPCWSTR str, INT32 count,
334 LPRECT32 rect, UINT32 flags, LPDRAWTEXTPARAMS dtp )
336 fprintf(stderr,"DrawTextEx32A(%d,%p,%d,%p,0x%08x,%p)\n",
337 hdc,str,count,rect,flags,dtp
339 /*FIXME: ignores extended functionality ... */
340 return DrawText32W(hdc,str,count,rect,flags);
343 /***********************************************************************
344 * ExtTextOut16 (GDI.351)
346 BOOL16 WINAPI ExtTextOut16( HDC16 hdc, INT16 x, INT16 y, UINT16 flags,
347 const RECT16 *lprect, LPCSTR str, UINT16 count,
353 LPINT32 lpdx32 = NULL;
355 if (lpDx) lpdx32 = (LPINT32)HEAP_xalloc( GetProcessHeap(), 0,
356 sizeof(INT32)*count );
357 if (lprect) CONV_RECT16TO32(lprect,&rect32);
358 if (lpdx32) for (i=count;i--;) lpdx32[i]=lpDx[i];
359 ret = ExtTextOut32A(hdc,x,y,flags,lprect?&rect32:NULL,str,count,lpdx32);
360 if (lpdx32) HeapFree( GetProcessHeap(), 0, lpdx32 );
367 /***********************************************************************
368 * ExtTextOut32A (GDI32.98)
370 BOOL32 WINAPI ExtTextOut32A( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
371 const RECT32 *lprect, LPCSTR str, UINT32 count,
374 DC * dc = DC_GetDCPtr( hdc );
375 return dc && dc->funcs->pExtTextOut &&
376 dc->funcs->pExtTextOut(dc,x,y,flags,lprect,str,count,lpDx);
380 /***********************************************************************
381 * ExtTextOut32W (GDI32.99)
383 BOOL32 WINAPI ExtTextOut32W( HDC32 hdc, INT32 x, INT32 y, UINT32 flags,
384 const RECT32 *lprect, LPCWSTR str, UINT32 count,
387 LPSTR p = HEAP_strdupWtoA( GetProcessHeap(), 0, str );
388 INT32 ret = ExtTextOut32A( hdc, x, y, flags, lprect, p, count, lpDx );
389 HeapFree( GetProcessHeap(), 0, p );
394 /***********************************************************************
397 BOOL16 WINAPI TextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR str, INT16 count )
399 return ExtTextOut16( hdc, x, y, 0, NULL, str, count, NULL );
403 /***********************************************************************
404 * TextOut32A (GDI32.355)
406 BOOL32 WINAPI TextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR str, INT32 count )
408 return ExtTextOut32A( hdc, x, y, 0, NULL, str, count, NULL );
412 /***********************************************************************
413 * TextOut32W (GDI32.356)
415 BOOL32 WINAPI TextOut32W(HDC32 hdc, INT32 x, INT32 y, LPCWSTR str, INT32 count)
417 return ExtTextOut32W( hdc, x, y, 0, NULL, str, count, NULL );
421 /***********************************************************************
424 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
425 * heap and we can guarantee that the handles fit in an INT16. We have to
426 * rethink the strategy once the migration to NT handles is complete.
427 * We are going to get a lot of code-duplication once this migration is
431 static BOOL32 TEXT_GrayString(HDC32 hdc, HBRUSH32 hb,
432 GRAYSTRINGPROC32 fn, LPARAM lp, INT32 len,
433 INT32 x, INT32 y, INT32 cx, INT32 cy,
434 BOOL32 unicode, BOOL32 _32bit)
436 HBITMAP32 hbm, hbmsave;
439 HDC32 memdc = CreateCompatibleDC32(hdc);
441 BOOL32 retval = TRUE;
445 if(!hdc) return FALSE;
450 slen = lstrlen32W((LPCWSTR)lp);
452 slen = lstrlen32A((LPCSTR)lp);
454 slen = lstrlen32A((LPCSTR)PTR_SEG_TO_LIN(lp));
457 if((cx == 0 || cy == 0) && slen != -1)
461 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
463 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
465 GetTextExtentPoint32A(hdc, (LPCSTR)PTR_SEG_TO_LIN(lp), slen, &s);
466 if(cx == 0) cx = s.cx;
467 if(cy == 0) cy = s.cy;
474 hbm = CreateBitmap32(cx, cy, 1, 1, NULL);
475 hbmsave = (HBITMAP32)SelectObject32(memdc, hbm);
476 FillRect32(memdc, &r, (HBRUSH32)GetStockObject32(BLACK_BRUSH));
477 SetTextColor32(memdc, RGB(255, 255, 255));
478 SetBkColor32(memdc, RGB(0, 0, 0));
479 hfsave = (HFONT32)SelectObject32(memdc, GetCurrentObject(hdc, OBJ_FONT));
483 retval = fn(memdc, lp, slen);
485 retval = (BOOL32)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
488 TextOut32W(memdc, 0, 0, (LPCWSTR)lp, slen);
490 TextOut32A(memdc, 0, 0, (LPCSTR)lp, slen);
492 TextOut32A(memdc, 0, 0, (LPCSTR)PTR_SEG_TO_LIN(lp), slen);
494 SelectObject32(memdc, hfsave);
497 * Windows doc says that the bitmap isn't grayed when len == -1 and
498 * the callback function returns FALSE. However, testing this on
499 * win95 showed otherwise...
501 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
502 if(retval || len != -1)
505 hbsave = (HBRUSH32)SelectObject32(memdc, CACHE_GetPattern55AABrush());
506 PatBlt32(memdc, 0, 0, cx, cy, 0x000A0329);
507 SelectObject32(memdc, hbsave);
510 if(hb) hbsave = (HBRUSH32)SelectObject32(hdc, hb);
511 fg = SetTextColor32(hdc, RGB(0, 0, 0));
512 bg = SetBkColor32(hdc, RGB(255, 255, 255));
513 BitBlt32(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
514 SetTextColor32(hdc, fg);
515 SetBkColor32(hdc, bg);
516 if(hb) SelectObject32(hdc, hbsave);
518 SelectObject32(memdc, hbmsave);
525 /***********************************************************************
526 * GrayString16 (USER.185)
528 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
529 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
532 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC32)gsprc, lParam, cch, x, y, cx, cy, FALSE, FALSE);
536 /***********************************************************************
537 * GrayString32A (USER32.314)
539 BOOL32 WINAPI GrayString32A( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
540 LPARAM lParam, INT32 cch, INT32 x, INT32 y,
543 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, FALSE, TRUE);
547 /***********************************************************************
548 * GrayString32W (USER32.315)
550 BOOL32 WINAPI GrayString32W( HDC32 hdc, HBRUSH32 hbr, GRAYSTRINGPROC32 gsprc,
551 LPARAM lParam, INT32 cch, INT32 x, INT32 y,
554 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, TRUE, TRUE);
558 /***********************************************************************
561 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
562 * Note: this doesn't work too well for text-alignment modes other
563 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
565 LONG TEXT_TabbedTextOut( HDC32 hdc, INT32 x, INT32 y, LPCSTR lpstr,
566 INT32 count, INT32 cTabStops, const INT16 *lpTabPos16,
567 const INT32 *lpTabPos32, INT32 nTabOrg,
568 BOOL32 fDisplayText )
577 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
583 GetTextMetrics16( hdc, &tm );
584 defWidth = 8 * tm.tmAveCharWidth;
589 for (i = 0; i < count; i++)
590 if (lpstr[i] == '\t') break;
591 extent = GetTextExtent( hdc, lpstr, i );
594 while ((cTabStops > 0) &&
595 (nTabOrg + *lpTabPos32 <= x + LOWORD(extent)))
603 while ((cTabStops > 0) &&
604 (nTabOrg + *lpTabPos16 <= x + LOWORD(extent)))
611 tabPos = x + LOWORD(extent);
612 else if (cTabStops > 0)
613 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
615 tabPos = nTabOrg + ((x + LOWORD(extent) - nTabOrg) / defWidth + 1) * defWidth;
619 SetRect32( &r, x, y, tabPos, y+HIWORD(extent) );
620 ExtTextOut32A( hdc, x, y,
621 GetBkMode32(hdc) == OPAQUE ? ETO_OPAQUE : 0,
622 &r, lpstr, i, NULL );
628 return MAKELONG(tabPos - start, HIWORD(extent));
632 /***********************************************************************
633 * TabbedTextOut16 (USER.196)
635 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
636 INT16 count, INT16 cTabStops,
637 const INT16 *lpTabPos, INT16 nTabOrg )
639 dprintf_info(text, "TabbedTextOut16: %04x %d,%d '%.*s' %d\n",
640 hdc, x, y, count, lpstr, count );
641 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
642 lpTabPos, NULL, nTabOrg, TRUE );
646 /***********************************************************************
647 * TabbedTextOut32A (USER32.541)
649 LONG WINAPI TabbedTextOut32A( HDC32 hdc, INT32 x, INT32 y, LPCSTR lpstr,
650 INT32 count, INT32 cTabStops,
651 const INT32 *lpTabPos, INT32 nTabOrg )
653 dprintf_info(text, "TabbedTextOut32A: %04x %d,%d '%.*s' %d\n",
654 hdc, x, y, count, lpstr, count );
655 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
656 NULL, lpTabPos, nTabOrg, TRUE );
660 /***********************************************************************
661 * TabbedTextOut32W (USER32.542)
663 LONG WINAPI TabbedTextOut32W( HDC32 hdc, INT32 x, INT32 y, LPCWSTR str,
664 INT32 count, INT32 cTabStops,
665 const INT32 *lpTabPos, INT32 nTabOrg )
668 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
669 lstrcpynWtoA( p, str, count + 1 );
670 ret = TabbedTextOut32A( hdc, x, y, p, count, cTabStops,
672 HeapFree( GetProcessHeap(), 0, p );
677 /***********************************************************************
678 * GetTabbedTextExtent16 (USER.197)
680 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
681 INT16 cTabStops, const INT16 *lpTabPos )
683 dprintf_info(text, "GetTabbedTextExtent: %04x '%.*s' %d\n",
684 hdc, count, lpstr, count );
685 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
686 lpTabPos, NULL, 0, FALSE );
690 /***********************************************************************
691 * GetTabbedTextExtent32A (USER32.292)
693 DWORD WINAPI GetTabbedTextExtent32A( HDC32 hdc, LPCSTR lpstr, INT32 count,
694 INT32 cTabStops, const INT32 *lpTabPos )
696 dprintf_info(text, "GetTabbedTextExtent: %04x '%.*s' %d\n",
697 hdc, count, lpstr, count );
698 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
699 NULL, lpTabPos, 0, FALSE );
703 /***********************************************************************
704 * GetTabbedTextExtent32W (USER32.293)
706 DWORD WINAPI GetTabbedTextExtent32W( HDC32 hdc, LPCWSTR lpstr, INT32 count,
707 INT32 cTabStops, const INT32 *lpTabPos )
710 LPSTR p = HEAP_xalloc( GetProcessHeap(), 0, count + 1 );
711 lstrcpynWtoA( p, lpstr, count + 1 );
712 ret = GetTabbedTextExtent32A( hdc, p, count, cTabStops, lpTabPos );
713 HeapFree( GetProcessHeap(), 0, p );
717 /***********************************************************************
718 * GetTextCharset (GDI32.226) (GDI.612)
720 INT32 WINAPI GetTextCharset32(HDC32 hdc)
722 fprintf(stdnimp,"GetTextCharset(0x%x)\n",hdc);
723 return DEFAULT_CHARSET; /* FIXME */
726 INT16 WINAPI GetTextCharset16(HDC16 hdc)
728 return GetTextCharset32(hdc);
731 /***********************************************************************
732 * GetTextCharsetInfo (GDI32.381)
734 INT32 WINAPI GetTextCharsetInfo(HDC32 hdc,LPCHARSETINFO csi,DWORD flags)
736 fprintf(stdnimp,"GetTextCharsetInfo(0x%x,%p,%08lx), stub!\n",hdc,csi,flags);
738 csi->ciCharset = DEFAULT_CHARSET;
739 csi->ciACP = GetACP();
741 /* ... fill fontstruct too ... */
742 return DEFAULT_CHARSET;