4 * Copyright 1993, 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(text);
28 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
30 static int tabstop = 8;
32 static int spacewidth;
33 static int prefix_offset;
35 static const char *TEXT_NextLine( HDC hdc, const char *str, int *count,
36 char *dest, int *len, int width, WORD format)
38 /* Return next line of text from a string.
41 * str - string to parse into lines.
42 * count - length of str.
43 * dest - destination in which to return line.
44 * len - length of resultant line in dest in chars.
45 * width - maximum width of line in pixels.
46 * format - format type passed to DrawText.
48 * Returns pointer to next char in str after end of the line
49 * or NULL if end of str reached.
57 int wb_i = 0, wb_j = 0, wb_count = 0;
65 if (!(format & DT_SINGLELINE))
67 if ((*count > 1) && (str[i] == CR) && (str[i+1] == LF))
78 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
79 (format & DT_WORDBREAK))
81 if (!GetTextExtentPointA(hdc, &dest[j-1], 1, &size))
88 if (!(format & DT_NOPREFIX) && *count > 1)
90 if (str[++i] == PREFIX)
98 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
99 (format & DT_WORDBREAK))
101 if (!GetTextExtentPointA(hdc, &dest[j-1], 1, &size))
108 if (format & DT_EXPANDTABS)
114 if (!GetTextExtentPointA(hdc, &dest[lasttab], j - lasttab, &size))
117 numspaces = (tabwidth - size.cx) / spacewidth;
118 for (k = 0; k < numspaces; k++)
120 plen += tabwidth - size.cx;
121 lasttab = wb_j + numspaces;
125 dest[j++] = str[i++];
126 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
127 (format & DT_WORDBREAK))
129 if (!GetTextExtentPointA(hdc, &dest[j-1], 1, &size))
137 dest[j++] = str[i++];
138 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
139 (format & DT_WORDBREAK))
144 if (!GetTextExtentPointA(hdc, &dest[j-1], 1, &size))
151 dest[j++] = str[i++];
152 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
153 (format & DT_WORDBREAK))
155 if (!GetTextExtentPointA(hdc, &dest[j-1], 1, &size))
162 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
166 if (format & DT_WORDBREAK)
171 *count = wb_count - 1;
189 /***********************************************************************
190 * DrawText16 (USER.85)
192 INT16 WINAPI DrawText16( HDC16 hdc, LPCSTR str, INT16 count, LPRECT16 rect, UINT16 flags )
199 CONV_RECT16TO32( rect, &rect32 );
200 ret = DrawTextA( hdc, str, count, &rect32, flags );
201 CONV_RECT32TO16( &rect32, rect );
203 else ret = DrawTextA( hdc, str, count, NULL, flags);
208 /***********************************************************************
209 * DrawTextA (USER32.164)
211 INT WINAPI DrawTextA( HDC hdc, LPCSTR str, INT i_count, LPRECT rect, UINT flags )
215 static char line[1024];
216 int len, lh, count=i_count;
220 int x = rect->left, y = rect->top;
221 int width = rect->right - rect->left;
224 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n",
225 debugstr_an (str, count), count,
226 rect->left, rect->top, rect->right, rect->bottom);
229 if (count == -1) count = strlen(str);
232 GetTextMetricsA(hdc, &tm);
233 if (flags & DT_EXTERNALLEADING)
234 lh = tm.tmHeight + tm.tmExternalLeading;
238 if (flags & DT_TABSTOP)
239 tabstop = flags >> 8;
241 if (flags & DT_EXPANDTABS)
243 GetTextExtentPointA(hdc, " ", 1, &size);
244 spacewidth = size.cx;
245 GetTextExtentPointA(hdc, "o", 1, &size);
246 tabwidth = size.cx * tabstop;
249 if (flags & DT_CALCRECT) flags |= DT_NOCLIP;
254 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
256 if (prefix_offset != -1)
258 GetTextExtentPointA(hdc, line, prefix_offset, &size);
260 GetTextExtentPointA(hdc, line, prefix_offset + 1, &size);
261 prefix_end = size.cx - 1;
264 if (!GetTextExtentPointA(hdc, line, len, &size)) return 0;
265 if (flags & DT_CENTER) x = (rect->left + rect->right -
267 else if (flags & DT_RIGHT) x = rect->right - size.cx;
269 if (flags & DT_SINGLELINE)
271 if (flags & DT_VCENTER) y = rect->top +
272 (rect->bottom - rect->top) / 2 - size.cy / 2;
273 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
275 if (!(flags & DT_CALCRECT))
277 if (!ExtTextOutA(hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
278 rect, line, len, NULL )) return 0;
279 if (prefix_offset != -1)
281 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
282 HPEN oldPen = SelectObject( hdc, hpen );
283 MoveToEx(hdc, x + prefix_x, y + tm.tmAscent + 1, NULL );
284 LineTo(hdc, x + prefix_end + 1, y + tm.tmAscent + 1 );
285 SelectObject( hdc, oldPen );
286 DeleteObject( hpen );
289 else if (size.cx > max_width)
295 if (!(flags & DT_NOCLIP))
297 if (y > rect->bottom - lh)
303 if (flags & DT_CALCRECT)
305 rect->right = rect->left + max_width;
308 return y - rect->top;
312 /***********************************************************************
313 * DrawTextW (USER32.167)
315 INT WINAPI DrawTextW( HDC hdc, LPCWSTR str, INT count,
316 LPRECT rect, UINT flags )
321 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
323 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
324 p = HeapAlloc( GetProcessHeap(), 0, acount );
325 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
326 if (count == -1) acount = -1;
327 ret = DrawTextA( hdc, p, acount, rect, flags );
329 HeapFree( GetProcessHeap(), 0, p );
333 /***********************************************************************
334 * DrawTextExA (USER32.165)
336 INT WINAPI DrawTextExA( HDC hdc, LPCSTR str, INT count,
337 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
339 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
341 FIXME("Ignores params:%d,%d,%d,%d\n",dtp->cbSize,
342 dtp->iTabLength,dtp->iLeftMargin,dtp->iRightMargin);
344 return DrawTextA(hdc,str,count,rect,flags);
347 /***********************************************************************
348 * DrawTextExW (USER32.166)
350 INT WINAPI DrawTextExW( HDC hdc, LPCWSTR str, INT count,
351 LPRECT rect, UINT flags, LPDRAWTEXTPARAMS dtp )
353 TRACE("(%d,%p,%d,%p,0x%08x,%p)\n",hdc,str,count,rect,flags,dtp);
354 FIXME("ignores extended functionality\n");
355 return DrawTextW(hdc,str,count,rect,flags);
357 /***********************************************************************
360 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
361 * heap and we can guarantee that the handles fit in an INT16. We have to
362 * rethink the strategy once the migration to NT handles is complete.
363 * We are going to get a lot of code-duplication once this migration is
367 static BOOL TEXT_GrayString(HDC hdc, HBRUSH hb, GRAYSTRINGPROC fn, LPARAM lp, INT len,
368 INT x, INT y, INT cx, INT cy, BOOL unicode, BOOL _32bit)
370 HBITMAP hbm, hbmsave;
373 HDC memdc = CreateCompatibleDC(hdc);
378 if(!hdc) return FALSE;
383 slen = lstrlenW((LPCWSTR)lp);
385 slen = strlen((LPCSTR)lp);
387 slen = strlen((LPCSTR)PTR_SEG_TO_LIN(lp));
390 if((cx == 0 || cy == 0) && slen != -1)
394 GetTextExtentPoint32W(hdc, (LPCWSTR)lp, slen, &s);
396 GetTextExtentPoint32A(hdc, (LPCSTR)lp, slen, &s);
398 GetTextExtentPoint32A(hdc, (LPCSTR)PTR_SEG_TO_LIN(lp), slen, &s);
399 if(cx == 0) cx = s.cx;
400 if(cy == 0) cy = s.cy;
403 hbm = CreateBitmap(cx, cy, 1, 1, NULL);
404 hbmsave = (HBITMAP)SelectObject(memdc, hbm);
405 hbsave = SelectObject( memdc, GetStockObject(BLACK_BRUSH) );
406 PatBlt( memdc, 0, 0, cx, cy, PATCOPY );
407 SelectObject( memdc, hbsave );
408 SetTextColor(memdc, RGB(255, 255, 255));
409 SetBkColor(memdc, RGB(0, 0, 0));
410 hfsave = (HFONT)SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
414 retval = fn(memdc, lp, slen);
416 retval = (BOOL)((BOOL16)((GRAYSTRINGPROC16)fn)((HDC16)memdc, lp, (INT16)slen));
419 TextOutW(memdc, 0, 0, (LPCWSTR)lp, slen);
421 TextOutA(memdc, 0, 0, (LPCSTR)lp, slen);
423 TextOutA(memdc, 0, 0, (LPCSTR)PTR_SEG_TO_LIN(lp), slen);
425 SelectObject(memdc, hfsave);
428 * Windows doc says that the bitmap isn't grayed when len == -1 and
429 * the callback function returns FALSE. However, testing this on
430 * win95 showed otherwise...
432 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
433 if(retval || len != -1)
436 hbsave = (HBRUSH)SelectObject(memdc, CACHE_GetPattern55AABrush());
437 PatBlt(memdc, 0, 0, cx, cy, 0x000A0329);
438 SelectObject(memdc, hbsave);
441 if(hb) hbsave = (HBRUSH)SelectObject(hdc, hb);
442 fg = SetTextColor(hdc, RGB(0, 0, 0));
443 bg = SetBkColor(hdc, RGB(255, 255, 255));
444 BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00E20746);
445 SetTextColor(hdc, fg);
447 if(hb) SelectObject(hdc, hbsave);
449 SelectObject(memdc, hbmsave);
456 /***********************************************************************
457 * GrayString16 (USER.185)
459 BOOL16 WINAPI GrayString16( HDC16 hdc, HBRUSH16 hbr, GRAYSTRINGPROC16 gsprc,
460 LPARAM lParam, INT16 cch, INT16 x, INT16 y,
463 return TEXT_GrayString(hdc, hbr, (GRAYSTRINGPROC)gsprc, lParam, cch, x, y, cx, cy, FALSE, FALSE);
467 /***********************************************************************
468 * GrayStringA (USER32.315)
470 BOOL WINAPI GrayStringA( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
471 LPARAM lParam, INT cch, INT x, INT y,
474 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, FALSE, TRUE);
478 /***********************************************************************
479 * GrayStringW (USER32.316)
481 BOOL WINAPI GrayStringW( HDC hdc, HBRUSH hbr, GRAYSTRINGPROC gsprc,
482 LPARAM lParam, INT cch, INT x, INT y,
485 return TEXT_GrayString(hdc, hbr, gsprc, lParam, cch, x, y, cx, cy, TRUE, TRUE);
488 /***********************************************************************
491 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
492 * Note: this doesn't work too well for text-alignment modes other
493 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
495 static LONG TEXT_TabbedTextOut( HDC hdc, INT x, INT y, LPCSTR lpstr,
496 INT count, INT cTabStops, const INT16 *lpTabPos16,
497 const INT *lpTabPos32, INT nTabOrg,
510 defWidth = lpTabPos32 ? *lpTabPos32 : *lpTabPos16;
516 GetTextMetricsA( hdc, &tm );
517 defWidth = 8 * tm.tmAveCharWidth;
522 for (i = 0; i < count; i++)
523 if (lpstr[i] == '\t') break;
524 GetTextExtentPointA( hdc, lpstr, i, &extent );
527 while ((cTabStops > 0) &&
528 (nTabOrg + *lpTabPos32 <= x + extent.cx))
536 while ((cTabStops > 0) &&
537 (nTabOrg + *lpTabPos16 <= x + extent.cx))
544 tabPos = x + extent.cx;
545 else if (cTabStops > 0)
546 tabPos = nTabOrg + (lpTabPos32 ? *lpTabPos32 : *lpTabPos16);
548 tabPos = nTabOrg + ((x + extent.cx - nTabOrg) / defWidth + 1) * defWidth;
555 r.bottom = y + extent.cy;
556 ExtTextOutA( hdc, x, y,
557 GetBkMode(hdc) == OPAQUE ? ETO_OPAQUE : 0,
558 &r, lpstr, i, NULL );
564 return MAKELONG(tabPos - start, extent.cy);
568 /***********************************************************************
569 * TabbedTextOut16 (USER.196)
571 LONG WINAPI TabbedTextOut16( HDC16 hdc, INT16 x, INT16 y, LPCSTR lpstr,
572 INT16 count, INT16 cTabStops,
573 const INT16 *lpTabPos, INT16 nTabOrg )
575 TRACE("%04x %d,%d '%.*s' %d\n",
576 hdc, x, y, count, lpstr, count );
577 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
578 lpTabPos, NULL, nTabOrg, TRUE );
582 /***********************************************************************
583 * TabbedTextOutA (USER32.542)
585 LONG WINAPI TabbedTextOutA( HDC hdc, INT x, INT y, LPCSTR lpstr,
586 INT count, INT cTabStops,
587 const INT *lpTabPos, INT nTabOrg )
589 TRACE("%04x %d,%d '%.*s' %d\n",
590 hdc, x, y, count, lpstr, count );
591 return TEXT_TabbedTextOut( hdc, x, y, lpstr, count, cTabStops,
592 NULL, lpTabPos, nTabOrg, TRUE );
596 /***********************************************************************
597 * TabbedTextOutW (USER32.543)
599 LONG WINAPI TabbedTextOutW( HDC hdc, INT x, INT y, LPCWSTR str,
600 INT count, INT cTabStops,
601 const INT *lpTabPos, INT nTabOrg )
606 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
608 acount = WideCharToMultiByte(codepage,0,str,count,NULL,0,NULL,NULL);
609 p = HeapAlloc( GetProcessHeap(), 0, acount );
610 if(p == NULL) return 0; /* FIXME: is this the correct return on failure */
611 acount = WideCharToMultiByte(codepage,0,str,count,p,acount,NULL,NULL);
612 ret = TabbedTextOutA( hdc, x, y, p, acount, cTabStops,
614 HeapFree( GetProcessHeap(), 0, p );
619 /***********************************************************************
620 * GetTabbedTextExtent16 (USER.197)
622 DWORD WINAPI GetTabbedTextExtent16( HDC16 hdc, LPCSTR lpstr, INT16 count,
623 INT16 cTabStops, const INT16 *lpTabPos )
625 TRACE("%04x '%.*s' %d\n",
626 hdc, count, lpstr, count );
627 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
628 lpTabPos, NULL, 0, FALSE );
632 /***********************************************************************
633 * GetTabbedTextExtentA (USER32.293)
635 DWORD WINAPI GetTabbedTextExtentA( HDC hdc, LPCSTR lpstr, INT count,
636 INT cTabStops, const INT *lpTabPos )
638 TRACE("%04x '%.*s' %d\n",
639 hdc, count, lpstr, count );
640 return TEXT_TabbedTextOut( hdc, 0, 0, lpstr, count, cTabStops,
641 NULL, lpTabPos, 0, FALSE );
645 /***********************************************************************
646 * GetTabbedTextExtentW (USER32.294)
648 DWORD WINAPI GetTabbedTextExtentW( HDC hdc, LPCWSTR lpstr, INT count,
649 INT cTabStops, const INT *lpTabPos )
654 UINT codepage = CP_ACP; /* FIXME: get codepage of font charset */
656 acount = WideCharToMultiByte(codepage,0,lpstr,count,NULL,0,NULL,NULL);
657 p = HeapAlloc( GetProcessHeap(), 0, acount );
658 if(p == NULL) return 0; /* FIXME: is this the correct failure value? */
659 acount = WideCharToMultiByte(codepage,0,lpstr,count,p,acount,NULL,NULL);
660 ret = GetTabbedTextExtentA( hdc, p, acount, cTabStops, lpTabPos );
661 HeapFree( GetProcessHeap(), 0, p );