4 * Copyright 1993, 1994 Alexandre Julliard
10 #include "wine/winuser16.h"
16 #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( HDC16 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 (!GetTextExtentPoint16(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 (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
108 if (format
& DT_EXPANDTABS
)
114 if (!GetTextExtentPoint16(hdc
, &dest
[lasttab
], j
- lasttab
,
118 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
119 for (k
= 0; k
< numspaces
; k
++)
121 plen
+= tabwidth
- size
.cx
;
122 lasttab
= wb_j
+ numspaces
;
126 dest
[j
++] = str
[i
++];
127 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
128 (format
& DT_WORDBREAK
))
130 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
138 dest
[j
++] = str
[i
++];
139 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
140 (format
& DT_WORDBREAK
))
145 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
152 dest
[j
++] = str
[i
++];
153 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
154 (format
& DT_WORDBREAK
))
156 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
163 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
167 if (format
& DT_WORDBREAK
)
172 *count
= wb_count
- 1;
190 /***********************************************************************
191 * DrawText16 (USER.85)
193 INT16 WINAPI
DrawText16( HDC16 hdc
, LPCSTR str
, INT16 i_count
,
194 LPRECT16 rect
, UINT16 flags
)
198 static char line
[1024];
199 int len
, lh
, count
=i_count
;
203 int x
= rect
->left
, y
= rect
->top
;
204 int width
= rect
->right
- rect
->left
;
207 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n",
208 debugstr_an (str
, count
), count
,
209 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
211 if (count
== -1) count
= strlen(str
);
214 GetTextMetrics16(hdc
, &tm
);
215 if (flags
& DT_EXTERNALLEADING
)
216 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
220 if (flags
& DT_TABSTOP
)
221 tabstop
= flags
>> 8;
223 if (flags
& DT_EXPANDTABS
)
225 GetTextExtentPoint16(hdc
, " ", 1, &size
);
226 spacewidth
= size
.cx
;
227 GetTextExtentPoint16(hdc
, "o", 1, &size
);
228 tabwidth
= size
.cx
* tabstop
;
231 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
236 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
238 if (prefix_offset
!= -1)
240 GetTextExtentPoint16(hdc
, line
, prefix_offset
, &size
);
242 GetTextExtentPoint16(hdc
, line
, prefix_offset
+ 1, &size
);
243 prefix_end
= size
.cx
- 1;
246 if (!GetTextExtentPoint16(hdc
, line
, len
, &size
)) return 0;
247 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
249 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
251 if (flags
& DT_SINGLELINE
)
253 if (flags
& DT_VCENTER
) y
= rect
->top
+
254 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
255 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
257 if (!(flags
& DT_CALCRECT
))
259 if (!ExtTextOut16(hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
260 rect
, line
, len
, NULL
)) return 0;
261 if (prefix_offset
!= -1)
263 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
264 HPEN oldPen
= SelectObject( hdc
, hpen
);
265 MoveTo16(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
266 LineTo(hdc
, x
+ prefix_end
+ 1, y
+ tm
.tmAscent
+ 1 );
267 SelectObject( hdc
, oldPen
);
268 DeleteObject( hpen
);
271 else if (size
.cx
> max_width
)
277 if (!(flags
& DT_NOCLIP
))
279 if (y
> rect
->bottom
- lh
)
285 if (flags
& DT_CALCRECT
)
287 rect
->right
= rect
->left
+ max_width
;
290 return y
- rect
->top
;
294 /***********************************************************************
295 * DrawText32A (USER32.164)
297 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
,
298 LPRECT rect
, UINT flags
)
304 return DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, NULL
, (UINT16
)flags
);
305 CONV_RECT32TO16( rect
, &rect16
);
306 ret
= DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, &rect16
, (UINT16
)flags
);
307 CONV_RECT16TO32( &rect16
, rect
);
312 /***********************************************************************
313 * DrawText32W (USER32.167)
315 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
,
316 LPRECT rect
, UINT flags
)
318 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
319 INT ret
= DrawTextA( hdc
, p
, count
, rect
, flags
);
320 HeapFree( GetProcessHeap(), 0, p
);
324 /***********************************************************************
325 * DrawTextEx32A (USER32.165)
327 INT WINAPI
DrawTextExA( HDC hdc
, LPCSTR str
, INT count
,
328 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
330 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
332 FIXME("Ignores params:%d,%d,%d,%d,%d\n",dtp
->cbSize
,
333 dtp
->iTabLength
,dtp
->iLeftMargin
,dtp
->iRightMargin
,
336 return DrawTextA(hdc
,str
,count
,rect
,flags
);
339 /***********************************************************************
340 * DrawTextEx32W (USER32.166)
342 INT WINAPI
DrawTextExW( HDC hdc
, LPCWSTR str
, INT count
,
343 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
345 TRACE("(%d,%p,%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
346 FIXME("ignores extended functionality\n");
347 return DrawTextW(hdc
,str
,count
,rect
,flags
);
350 /***********************************************************************
351 * ExtTextOut16 (GDI.351)
353 BOOL16 WINAPI
ExtTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, UINT16 flags
,
354 const RECT16
*lprect
, LPCSTR str
, UINT16 count
,
362 if (lpDx
) lpdx32
= (LPINT
)HEAP_xalloc( GetProcessHeap(), 0,
364 if (lprect
) CONV_RECT16TO32(lprect
,&rect32
);
365 if (lpdx32
) for (i
=count
;i
--;) lpdx32
[i
]=lpDx
[i
];
366 ret
= ExtTextOutA(hdc
,x
,y
,flags
,lprect
?&rect32
:NULL
,str
,count
,lpdx32
);
367 if (lpdx32
) HeapFree( GetProcessHeap(), 0, lpdx32
);
374 /***********************************************************************
375 * ExtTextOut32A (GDI32.98)
377 BOOL WINAPI
ExtTextOutA( HDC hdc
, INT x
, INT y
, UINT flags
,
378 const RECT
*lprect
, LPCSTR str
, UINT count
,
381 DC
* dc
= DC_GetDCPtr( hdc
);
382 return dc
&& dc
->funcs
->pExtTextOut
&&
383 dc
->funcs
->pExtTextOut(dc
,x
,y
,flags
,lprect
,str
,count
,lpDx
);
387 /***********************************************************************
388 * ExtTextOut32W (GDI32.99)
390 BOOL WINAPI
ExtTextOutW( HDC hdc
, INT x
, INT y
, UINT flags
,
391 const RECT
*lprect
, LPCWSTR str
, UINT count
,
394 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
395 INT ret
= ExtTextOutA( hdc
, x
, y
, flags
, lprect
, p
, count
, lpDx
);
396 HeapFree( GetProcessHeap(), 0, p
);
401 /***********************************************************************
404 BOOL16 WINAPI
TextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR str
, INT16 count
)
406 return ExtTextOut16( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
410 /***********************************************************************
411 * TextOut32A (GDI32.355)
413 BOOL WINAPI
TextOutA( HDC hdc
, INT x
, INT y
, LPCSTR str
, INT count
)
415 return ExtTextOutA( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
419 /***********************************************************************
420 * TextOut32W (GDI32.356)
422 BOOL WINAPI
TextOutW(HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
)
424 return ExtTextOutW( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
428 /***********************************************************************
431 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
432 * heap and we can guarantee that the handles fit in an INT16. We have to
433 * rethink the strategy once the migration to NT handles is complete.
434 * We are going to get a lot of code-duplication once this migration is
438 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
,
439 GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
440 INT x
, INT y
, INT cx
, INT cy
,
441 BOOL unicode
, BOOL _32bit
)
443 HBITMAP hbm
, hbmsave
;
446 HDC memdc
= CreateCompatibleDC(hdc
);
452 if(!hdc
) return FALSE
;
457 slen
= lstrlenW((LPCWSTR
)lp
);
459 slen
= lstrlenA((LPCSTR
)lp
);
461 slen
= lstrlenA((LPCSTR
)PTR_SEG_TO_LIN(lp
));
464 if((cx
== 0 || cy
== 0) && slen
!= -1)
468 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
470 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
472 GetTextExtentPoint32A(hdc
, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
, &s
);
473 if(cx
== 0) cx
= s
.cx
;
474 if(cy
== 0) cy
= s
.cy
;
481 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
482 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
483 FillRect(memdc
, &r
, (HBRUSH
)GetStockObject(BLACK_BRUSH
));
484 SetTextColor(memdc
, RGB(255, 255, 255));
485 SetBkColor(memdc
, RGB(0, 0, 0));
486 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
490 retval
= fn(memdc
, lp
, slen
);
492 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
495 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
497 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
499 TextOutA(memdc
, 0, 0, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
);
501 SelectObject(memdc
, hfsave
);
504 * Windows doc says that the bitmap isn't grayed when len == -1 and
505 * the callback function returns FALSE. However, testing this on
506 * win95 showed otherwise...
508 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
509 if(retval
|| len
!= -1)
512 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
513 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
514 SelectObject(memdc
, hbsave
);
517 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
518 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
519 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
520 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
521 SetTextColor(hdc
, fg
);
523 if(hb
) SelectObject(hdc
, hbsave
);
525 SelectObject(memdc
, hbmsave
);
532 /***********************************************************************
533 * GrayString16 (USER.185)
535 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
536 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
539 return TEXT_GrayString(hdc
, hbr
, (GRAYSTRINGPROC
)gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
543 /***********************************************************************
544 * GrayString32A (USER32.315)
546 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
547 LPARAM lParam
, INT cch
, INT x
, INT y
,
550 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, TRUE
);
554 /***********************************************************************
555 * GrayString32W (USER32.316)
557 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
558 LPARAM lParam
, INT cch
, INT x
, INT y
,
561 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, TRUE
, TRUE
);
565 /***********************************************************************
568 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
569 * Note: this doesn't work too well for text-alignment modes other
570 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
572 LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
573 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
574 const INT
*lpTabPos32
, INT nTabOrg
,
584 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
590 GetTextMetrics16( hdc
, &tm
);
591 defWidth
= 8 * tm
.tmAveCharWidth
;
596 for (i
= 0; i
< count
; i
++)
597 if (lpstr
[i
] == '\t') break;
598 extent
= GetTextExtent16( hdc
, lpstr
, i
);
601 while ((cTabStops
> 0) &&
602 (nTabOrg
+ *lpTabPos32
<= x
+ LOWORD(extent
)))
610 while ((cTabStops
> 0) &&
611 (nTabOrg
+ *lpTabPos16
<= x
+ LOWORD(extent
)))
618 tabPos
= x
+ LOWORD(extent
);
619 else if (cTabStops
> 0)
620 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
622 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
626 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
627 ExtTextOutA( hdc
, x
, y
,
628 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
629 &r
, lpstr
, i
, NULL
);
635 return MAKELONG(tabPos
- start
, HIWORD(extent
));
639 /***********************************************************************
640 * TabbedTextOut16 (USER.196)
642 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
643 INT16 count
, INT16 cTabStops
,
644 const INT16
*lpTabPos
, INT16 nTabOrg
)
646 TRACE("%04x %d,%d '%.*s' %d\n",
647 hdc
, x
, y
, count
, lpstr
, count
);
648 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
649 lpTabPos
, NULL
, nTabOrg
, TRUE
);
653 /***********************************************************************
654 * TabbedTextOut32A (USER32.542)
656 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
657 INT count
, INT cTabStops
,
658 const INT
*lpTabPos
, INT nTabOrg
)
660 TRACE("%04x %d,%d '%.*s' %d\n",
661 hdc
, x
, y
, count
, lpstr
, count
);
662 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
663 NULL
, lpTabPos
, nTabOrg
, TRUE
);
667 /***********************************************************************
668 * TabbedTextOut32W (USER32.543)
670 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
,
671 INT count
, INT cTabStops
,
672 const INT
*lpTabPos
, INT nTabOrg
)
675 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
676 lstrcpynWtoA( p
, str
, count
+ 1 );
677 ret
= TabbedTextOutA( hdc
, x
, y
, p
, count
, cTabStops
,
679 HeapFree( GetProcessHeap(), 0, p
);
684 /***********************************************************************
685 * GetTabbedTextExtent16 (USER.197)
687 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
688 INT16 cTabStops
, const INT16
*lpTabPos
)
690 TRACE("%04x '%.*s' %d\n",
691 hdc
, count
, lpstr
, count
);
692 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
693 lpTabPos
, NULL
, 0, FALSE
);
697 /***********************************************************************
698 * GetTabbedTextExtent32A (USER32.293)
700 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
701 INT cTabStops
, const INT
*lpTabPos
)
703 TRACE("%04x '%.*s' %d\n",
704 hdc
, count
, lpstr
, count
);
705 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
706 NULL
, lpTabPos
, 0, FALSE
);
710 /***********************************************************************
711 * GetTabbedTextExtent32W (USER32.294)
713 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
714 INT cTabStops
, const INT
*lpTabPos
)
717 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
718 lstrcpynWtoA( p
, lpstr
, count
+ 1 );
719 ret
= GetTabbedTextExtentA( hdc
, p
, count
, cTabStops
, lpTabPos
);
720 HeapFree( GetProcessHeap(), 0, p
);
724 /***********************************************************************
725 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
728 * Should it return a UINT32 instead of an INT32?
729 * => YES, as GetTextCharsetInfo returns UINT32
732 * Success: Character set identifier
733 * Failure: DEFAULT_CHARSET
735 UINT WINAPI
GetTextCharset(
736 HDC hdc
) /* [in] Handle to device context */
738 /* MSDN docs say this is equivalent */
739 return GetTextCharsetInfo(hdc
, NULL
, 0);
742 /***********************************************************************
743 * GetTextCharset16 [GDI.612]
745 UINT16 WINAPI
GetTextCharset16(HDC16 hdc
)
747 return (UINT16
)GetTextCharset(hdc
);
750 /***********************************************************************
751 * GetTextCharsetInfo [GDI32.381] Gets character set for font
754 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
755 * Should it return a UINT32 instead of an INT32?
756 * => YES and YES, from win32.hlp from Borland
759 * Success: Character set identifier
760 * Failure: DEFAULT_CHARSET
762 UINT WINAPI
GetTextCharsetInfo(
763 HDC hdc
, /* [in] Handle to device context */
764 LPFONTSIGNATURE fs
, /* [out] Pointer to struct to receive data */
765 DWORD flags
) /* [in] Reserved - must be 0 */
768 UINT charSet
= DEFAULT_CHARSET
;
772 hFont
= GetCurrentObject(hdc
, OBJ_FONT
);
774 return(DEFAULT_CHARSET
);
775 if ( GetObjectW(hFont
, sizeof(LOGFONTW
), &lf
) != 0 )
776 charSet
= lf
.lfCharSet
;
779 if (!TranslateCharsetInfo((LPDWORD
)charSet
, &csinfo
, TCI_SRCCHARSET
))
780 return (DEFAULT_CHARSET
);
781 memcpy(fs
, &csinfo
.fs
, sizeof(FONTSIGNATURE
));