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
);
212 if (count
== -1) count
= strlen(str
);
215 GetTextMetrics16(hdc
, &tm
);
216 if (flags
& DT_EXTERNALLEADING
)
217 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
221 if (flags
& DT_TABSTOP
)
222 tabstop
= flags
>> 8;
224 if (flags
& DT_EXPANDTABS
)
226 GetTextExtentPoint16(hdc
, " ", 1, &size
);
227 spacewidth
= size
.cx
;
228 GetTextExtentPoint16(hdc
, "o", 1, &size
);
229 tabwidth
= size
.cx
* tabstop
;
232 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
237 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
239 if (prefix_offset
!= -1)
241 GetTextExtentPoint16(hdc
, line
, prefix_offset
, &size
);
243 GetTextExtentPoint16(hdc
, line
, prefix_offset
+ 1, &size
);
244 prefix_end
= size
.cx
- 1;
247 if (!GetTextExtentPoint16(hdc
, line
, len
, &size
)) return 0;
248 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
250 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
252 if (flags
& DT_SINGLELINE
)
254 if (flags
& DT_VCENTER
) y
= rect
->top
+
255 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
256 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
258 if (!(flags
& DT_CALCRECT
))
260 if (!ExtTextOut16(hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
261 rect
, line
, len
, NULL
)) return 0;
262 if (prefix_offset
!= -1)
264 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
265 HPEN oldPen
= SelectObject( hdc
, hpen
);
266 MoveTo16(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
267 LineTo(hdc
, x
+ prefix_end
+ 1, y
+ tm
.tmAscent
+ 1 );
268 SelectObject( hdc
, oldPen
);
269 DeleteObject( hpen
);
272 else if (size
.cx
> max_width
)
278 if (!(flags
& DT_NOCLIP
))
280 if (y
> rect
->bottom
- lh
)
286 if (flags
& DT_CALCRECT
)
288 rect
->right
= rect
->left
+ max_width
;
291 return y
- rect
->top
;
295 /***********************************************************************
296 * DrawText32A (USER32.164)
298 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
,
299 LPRECT rect
, UINT flags
)
305 return DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, NULL
, (UINT16
)flags
);
306 CONV_RECT32TO16( rect
, &rect16
);
307 ret
= DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, &rect16
, (UINT16
)flags
);
308 CONV_RECT16TO32( &rect16
, rect
);
313 /***********************************************************************
314 * DrawText32W (USER32.167)
316 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
,
317 LPRECT rect
, UINT flags
)
319 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
320 INT ret
= DrawTextA( hdc
, p
, count
, rect
, flags
);
321 HeapFree( GetProcessHeap(), 0, p
);
325 /***********************************************************************
326 * DrawTextEx32A (USER32.165)
328 INT WINAPI
DrawTextExA( HDC hdc
, LPCSTR str
, INT count
,
329 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
331 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
333 FIXME("Ignores params:%d,%d,%d,%d,%d\n",dtp
->cbSize
,
334 dtp
->iTabLength
,dtp
->iLeftMargin
,dtp
->iRightMargin
,
337 return DrawTextA(hdc
,str
,count
,rect
,flags
);
340 /***********************************************************************
341 * DrawTextEx32W (USER32.166)
343 INT WINAPI
DrawTextExW( HDC hdc
, LPCWSTR str
, INT count
,
344 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
346 TRACE("(%d,%p,%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
347 FIXME("ignores extended functionality\n");
348 return DrawTextW(hdc
,str
,count
,rect
,flags
);
351 /***********************************************************************
352 * ExtTextOut16 (GDI.351)
354 BOOL16 WINAPI
ExtTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, UINT16 flags
,
355 const RECT16
*lprect
, LPCSTR str
, UINT16 count
,
363 if (lpDx
) lpdx32
= (LPINT
)HEAP_xalloc( GetProcessHeap(), 0,
365 if (lprect
) CONV_RECT16TO32(lprect
,&rect32
);
366 if (lpdx32
) for (i
=count
;i
--;) lpdx32
[i
]=lpDx
[i
];
367 ret
= ExtTextOutA(hdc
,x
,y
,flags
,lprect
?&rect32
:NULL
,str
,count
,lpdx32
);
368 if (lpdx32
) HeapFree( GetProcessHeap(), 0, lpdx32
);
375 /***********************************************************************
376 * ExtTextOut32A (GDI32.98)
378 BOOL WINAPI
ExtTextOutA( HDC hdc
, INT x
, INT y
, UINT flags
,
379 const RECT
*lprect
, LPCSTR str
, UINT count
,
382 DC
* dc
= DC_GetDCPtr( hdc
);
383 return dc
&& dc
->funcs
->pExtTextOut
&&
384 dc
->funcs
->pExtTextOut(dc
,x
,y
,flags
,lprect
,str
,count
,lpDx
);
388 /***********************************************************************
389 * ExtTextOut32W (GDI32.99)
391 BOOL WINAPI
ExtTextOutW( HDC hdc
, INT x
, INT y
, UINT flags
,
392 const RECT
*lprect
, LPCWSTR str
, UINT count
,
395 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
396 INT ret
= ExtTextOutA( hdc
, x
, y
, flags
, lprect
, p
, count
, lpDx
);
397 HeapFree( GetProcessHeap(), 0, p
);
402 /***********************************************************************
405 BOOL16 WINAPI
TextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR str
, INT16 count
)
407 return ExtTextOut16( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
411 /***********************************************************************
412 * TextOut32A (GDI32.355)
414 BOOL WINAPI
TextOutA( HDC hdc
, INT x
, INT y
, LPCSTR str
, INT count
)
416 return ExtTextOutA( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
420 /***********************************************************************
421 * TextOut32W (GDI32.356)
423 BOOL WINAPI
TextOutW(HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
)
425 return ExtTextOutW( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
429 /***********************************************************************
432 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
433 * heap and we can guarantee that the handles fit in an INT16. We have to
434 * rethink the strategy once the migration to NT handles is complete.
435 * We are going to get a lot of code-duplication once this migration is
439 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
,
440 GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
441 INT x
, INT y
, INT cx
, INT cy
,
442 BOOL unicode
, BOOL _32bit
)
444 HBITMAP hbm
, hbmsave
;
447 HDC memdc
= CreateCompatibleDC(hdc
);
453 if(!hdc
) return FALSE
;
458 slen
= lstrlenW((LPCWSTR
)lp
);
460 slen
= lstrlenA((LPCSTR
)lp
);
462 slen
= lstrlenA((LPCSTR
)PTR_SEG_TO_LIN(lp
));
465 if((cx
== 0 || cy
== 0) && slen
!= -1)
469 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
471 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
473 GetTextExtentPoint32A(hdc
, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
, &s
);
474 if(cx
== 0) cx
= s
.cx
;
475 if(cy
== 0) cy
= s
.cy
;
482 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
483 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
484 FillRect(memdc
, &r
, (HBRUSH
)GetStockObject(BLACK_BRUSH
));
485 SetTextColor(memdc
, RGB(255, 255, 255));
486 SetBkColor(memdc
, RGB(0, 0, 0));
487 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
491 retval
= fn(memdc
, lp
, slen
);
493 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
496 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
498 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
500 TextOutA(memdc
, 0, 0, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
);
502 SelectObject(memdc
, hfsave
);
505 * Windows doc says that the bitmap isn't grayed when len == -1 and
506 * the callback function returns FALSE. However, testing this on
507 * win95 showed otherwise...
509 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
510 if(retval
|| len
!= -1)
513 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
514 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
515 SelectObject(memdc
, hbsave
);
518 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
519 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
520 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
521 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
522 SetTextColor(hdc
, fg
);
524 if(hb
) SelectObject(hdc
, hbsave
);
526 SelectObject(memdc
, hbmsave
);
533 /***********************************************************************
534 * GrayString16 (USER.185)
536 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
537 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
540 return TEXT_GrayString(hdc
, hbr
, (GRAYSTRINGPROC
)gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
544 /***********************************************************************
545 * GrayString32A (USER32.315)
547 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
548 LPARAM lParam
, INT cch
, INT x
, INT y
,
551 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, TRUE
);
555 /***********************************************************************
556 * GrayString32W (USER32.316)
558 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
559 LPARAM lParam
, INT cch
, INT x
, INT y
,
562 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, TRUE
, TRUE
);
566 /***********************************************************************
569 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
570 * Note: this doesn't work too well for text-alignment modes other
571 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
573 LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
574 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
575 const INT
*lpTabPos32
, INT nTabOrg
,
585 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
591 GetTextMetrics16( hdc
, &tm
);
592 defWidth
= 8 * tm
.tmAveCharWidth
;
597 for (i
= 0; i
< count
; i
++)
598 if (lpstr
[i
] == '\t') break;
599 extent
= GetTextExtent16( hdc
, lpstr
, i
);
602 while ((cTabStops
> 0) &&
603 (nTabOrg
+ *lpTabPos32
<= x
+ LOWORD(extent
)))
611 while ((cTabStops
> 0) &&
612 (nTabOrg
+ *lpTabPos16
<= x
+ LOWORD(extent
)))
619 tabPos
= x
+ LOWORD(extent
);
620 else if (cTabStops
> 0)
621 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
623 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
627 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
628 ExtTextOutA( hdc
, x
, y
,
629 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
630 &r
, lpstr
, i
, NULL
);
636 return MAKELONG(tabPos
- start
, HIWORD(extent
));
640 /***********************************************************************
641 * TabbedTextOut16 (USER.196)
643 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
644 INT16 count
, INT16 cTabStops
,
645 const INT16
*lpTabPos
, INT16 nTabOrg
)
647 TRACE("%04x %d,%d '%.*s' %d\n",
648 hdc
, x
, y
, count
, lpstr
, count
);
649 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
650 lpTabPos
, NULL
, nTabOrg
, TRUE
);
654 /***********************************************************************
655 * TabbedTextOut32A (USER32.542)
657 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
658 INT count
, INT cTabStops
,
659 const INT
*lpTabPos
, INT nTabOrg
)
661 TRACE("%04x %d,%d '%.*s' %d\n",
662 hdc
, x
, y
, count
, lpstr
, count
);
663 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
664 NULL
, lpTabPos
, nTabOrg
, TRUE
);
668 /***********************************************************************
669 * TabbedTextOut32W (USER32.543)
671 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
,
672 INT count
, INT cTabStops
,
673 const INT
*lpTabPos
, INT nTabOrg
)
676 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
677 lstrcpynWtoA( p
, str
, count
+ 1 );
678 ret
= TabbedTextOutA( hdc
, x
, y
, p
, count
, cTabStops
,
680 HeapFree( GetProcessHeap(), 0, p
);
685 /***********************************************************************
686 * GetTabbedTextExtent16 (USER.197)
688 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
689 INT16 cTabStops
, const INT16
*lpTabPos
)
691 TRACE("%04x '%.*s' %d\n",
692 hdc
, count
, lpstr
, count
);
693 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
694 lpTabPos
, NULL
, 0, FALSE
);
698 /***********************************************************************
699 * GetTabbedTextExtent32A (USER32.293)
701 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
702 INT cTabStops
, const INT
*lpTabPos
)
704 TRACE("%04x '%.*s' %d\n",
705 hdc
, count
, lpstr
, count
);
706 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
707 NULL
, lpTabPos
, 0, FALSE
);
711 /***********************************************************************
712 * GetTabbedTextExtent32W (USER32.294)
714 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
715 INT cTabStops
, const INT
*lpTabPos
)
718 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
719 lstrcpynWtoA( p
, lpstr
, count
+ 1 );
720 ret
= GetTabbedTextExtentA( hdc
, p
, count
, cTabStops
, lpTabPos
);
721 HeapFree( GetProcessHeap(), 0, p
);
725 /***********************************************************************
726 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
729 * Should it return a UINT32 instead of an INT32?
730 * => YES, as GetTextCharsetInfo returns UINT32
733 * Success: Character set identifier
734 * Failure: DEFAULT_CHARSET
736 UINT WINAPI
GetTextCharset(
737 HDC hdc
) /* [in] Handle to device context */
739 /* MSDN docs say this is equivalent */
740 return GetTextCharsetInfo(hdc
, NULL
, 0);
743 /***********************************************************************
744 * GetTextCharset16 [GDI.612]
746 UINT16 WINAPI
GetTextCharset16(HDC16 hdc
)
748 return (UINT16
)GetTextCharset(hdc
);
751 /***********************************************************************
752 * GetTextCharsetInfo [GDI32.381] Gets character set for font
755 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
756 * Should it return a UINT32 instead of an INT32?
757 * => YES and YES, from win32.hlp from Borland
760 * Success: Character set identifier
761 * Failure: DEFAULT_CHARSET
763 UINT WINAPI
GetTextCharsetInfo(
764 HDC hdc
, /* [in] Handle to device context */
765 LPFONTSIGNATURE fs
, /* [out] Pointer to struct to receive data */
766 DWORD flags
) /* [in] Reserved - must be 0 */
769 UINT charSet
= DEFAULT_CHARSET
;
773 hFont
= GetCurrentObject(hdc
, OBJ_FONT
);
775 return(DEFAULT_CHARSET
);
776 if ( GetObjectW(hFont
, sizeof(LOGFONTW
), &lf
) != 0 )
777 charSet
= lf
.lfCharSet
;
780 if (!TranslateCharsetInfo((LPDWORD
)charSet
, &csinfo
, TCI_SRCCHARSET
))
781 return (DEFAULT_CHARSET
);
782 memcpy(fs
, &csinfo
.fs
, sizeof(FONTSIGNATURE
));
787 /***********************************************************************
788 * PolyTextOutA [GDI.402] Draw several Strings
790 BOOL WINAPI
PolyTextOutA (
791 HDC hdc
, /* Handle to device context */
792 PPOLYTEXTA pptxt
, /* array of strings */
793 INT cStrings
/* Number of strings in array */
797 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED
);
803 /***********************************************************************
804 * PolyTextOutW [GDI.403] Draw several Strings
806 BOOL WINAPI
PolyTextOutW (
807 HDC hdc
, /* Handle to device context */
808 PPOLYTEXTW pptxt
, /* array of strings */
809 INT cStrings
/* Number of strings in array */
813 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED
);