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 * ExtTextOut32A (GDI32.98)
379 BOOL WINAPI
ExtTextOutA( HDC hdc
, INT x
, INT y
, UINT flags
,
380 const RECT
*lprect
, LPCSTR str
, UINT count
,
383 DC
* dc
= DC_GetDCPtr( hdc
);
384 return dc
&& dc
->funcs
->pExtTextOut
&&
385 dc
->funcs
->pExtTextOut(dc
,x
,y
,flags
,lprect
,str
,count
,lpDx
);
389 /***********************************************************************
390 * ExtTextOut32W (GDI32.99)
392 BOOL WINAPI
ExtTextOutW( HDC hdc
, INT x
, INT y
, UINT flags
,
393 const RECT
*lprect
, LPCWSTR str
, UINT count
,
396 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
397 INT ret
= ExtTextOutA( hdc
, x
, y
, flags
, lprect
, p
, count
, lpDx
);
398 HeapFree( GetProcessHeap(), 0, p
);
403 /***********************************************************************
406 BOOL16 WINAPI
TextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR str
, INT16 count
)
408 return ExtTextOut16( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
412 /***********************************************************************
413 * TextOut32A (GDI32.355)
415 BOOL WINAPI
TextOutA( HDC hdc
, INT x
, INT y
, LPCSTR str
, INT count
)
417 return ExtTextOutA( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
421 /***********************************************************************
422 * TextOut32W (GDI32.356)
424 BOOL WINAPI
TextOutW(HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
)
426 return ExtTextOutW( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
430 /***********************************************************************
433 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
434 * heap and we can guarantee that the handles fit in an INT16. We have to
435 * rethink the strategy once the migration to NT handles is complete.
436 * We are going to get a lot of code-duplication once this migration is
440 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
,
441 GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
442 INT x
, INT y
, INT cx
, INT cy
,
443 BOOL unicode
, BOOL _32bit
)
445 HBITMAP hbm
, hbmsave
;
448 HDC memdc
= CreateCompatibleDC(hdc
);
454 if(!hdc
) return FALSE
;
459 slen
= lstrlenW((LPCWSTR
)lp
);
461 slen
= lstrlenA((LPCSTR
)lp
);
463 slen
= lstrlenA((LPCSTR
)PTR_SEG_TO_LIN(lp
));
466 if((cx
== 0 || cy
== 0) && slen
!= -1)
470 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
472 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
474 GetTextExtentPoint32A(hdc
, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
, &s
);
475 if(cx
== 0) cx
= s
.cx
;
476 if(cy
== 0) cy
= s
.cy
;
483 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
484 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
485 FillRect(memdc
, &r
, (HBRUSH
)GetStockObject(BLACK_BRUSH
));
486 SetTextColor(memdc
, RGB(255, 255, 255));
487 SetBkColor(memdc
, RGB(0, 0, 0));
488 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
492 retval
= fn(memdc
, lp
, slen
);
494 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
497 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
499 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
501 TextOutA(memdc
, 0, 0, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
);
503 SelectObject(memdc
, hfsave
);
506 * Windows doc says that the bitmap isn't grayed when len == -1 and
507 * the callback function returns FALSE. However, testing this on
508 * win95 showed otherwise...
510 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
511 if(retval
|| len
!= -1)
514 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
515 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
516 SelectObject(memdc
, hbsave
);
519 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
520 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
521 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
522 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
523 SetTextColor(hdc
, fg
);
525 if(hb
) SelectObject(hdc
, hbsave
);
527 SelectObject(memdc
, hbmsave
);
534 /***********************************************************************
535 * GrayString16 (USER.185)
537 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
538 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
541 return TEXT_GrayString(hdc
, hbr
, (GRAYSTRINGPROC
)gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
545 /***********************************************************************
546 * GrayString32A (USER32.315)
548 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
549 LPARAM lParam
, INT cch
, INT x
, INT y
,
552 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, TRUE
);
556 /***********************************************************************
557 * GrayString32W (USER32.316)
559 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
560 LPARAM lParam
, INT cch
, INT x
, INT y
,
563 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, TRUE
, TRUE
);
567 /***********************************************************************
570 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
571 * Note: this doesn't work too well for text-alignment modes other
572 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
574 LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
575 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
576 const INT
*lpTabPos32
, INT nTabOrg
,
586 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
592 GetTextMetrics16( hdc
, &tm
);
593 defWidth
= 8 * tm
.tmAveCharWidth
;
598 for (i
= 0; i
< count
; i
++)
599 if (lpstr
[i
] == '\t') break;
600 extent
= GetTextExtent16( hdc
, lpstr
, i
);
603 while ((cTabStops
> 0) &&
604 (nTabOrg
+ *lpTabPos32
<= x
+ LOWORD(extent
)))
612 while ((cTabStops
> 0) &&
613 (nTabOrg
+ *lpTabPos16
<= x
+ LOWORD(extent
)))
620 tabPos
= x
+ LOWORD(extent
);
621 else if (cTabStops
> 0)
622 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
624 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
628 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
629 ExtTextOutA( hdc
, x
, y
,
630 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
631 &r
, lpstr
, i
, NULL
);
637 return MAKELONG(tabPos
- start
, HIWORD(extent
));
641 /***********************************************************************
642 * TabbedTextOut16 (USER.196)
644 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
645 INT16 count
, INT16 cTabStops
,
646 const INT16
*lpTabPos
, INT16 nTabOrg
)
648 TRACE("%04x %d,%d '%.*s' %d\n",
649 hdc
, x
, y
, count
, lpstr
, count
);
650 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
651 lpTabPos
, NULL
, nTabOrg
, TRUE
);
655 /***********************************************************************
656 * TabbedTextOut32A (USER32.542)
658 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
659 INT count
, INT cTabStops
,
660 const INT
*lpTabPos
, INT nTabOrg
)
662 TRACE("%04x %d,%d '%.*s' %d\n",
663 hdc
, x
, y
, count
, lpstr
, count
);
664 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
665 NULL
, lpTabPos
, nTabOrg
, TRUE
);
669 /***********************************************************************
670 * TabbedTextOut32W (USER32.543)
672 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
,
673 INT count
, INT cTabStops
,
674 const INT
*lpTabPos
, INT nTabOrg
)
677 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
678 lstrcpynWtoA( p
, str
, count
+ 1 );
679 ret
= TabbedTextOutA( hdc
, x
, y
, p
, count
, cTabStops
,
681 HeapFree( GetProcessHeap(), 0, p
);
686 /***********************************************************************
687 * GetTabbedTextExtent16 (USER.197)
689 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
690 INT16 cTabStops
, const INT16
*lpTabPos
)
692 TRACE("%04x '%.*s' %d\n",
693 hdc
, count
, lpstr
, count
);
694 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
695 lpTabPos
, NULL
, 0, FALSE
);
699 /***********************************************************************
700 * GetTabbedTextExtent32A (USER32.293)
702 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
703 INT cTabStops
, const INT
*lpTabPos
)
705 TRACE("%04x '%.*s' %d\n",
706 hdc
, count
, lpstr
, count
);
707 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
708 NULL
, lpTabPos
, 0, FALSE
);
712 /***********************************************************************
713 * GetTabbedTextExtent32W (USER32.294)
715 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
716 INT cTabStops
, const INT
*lpTabPos
)
719 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
720 lstrcpynWtoA( p
, lpstr
, count
+ 1 );
721 ret
= GetTabbedTextExtentA( hdc
, p
, count
, cTabStops
, lpTabPos
);
722 HeapFree( GetProcessHeap(), 0, p
);
726 /***********************************************************************
727 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
730 * Should it return a UINT32 instead of an INT32?
731 * => YES, as GetTextCharsetInfo returns UINT32
734 * Success: Character set identifier
735 * Failure: DEFAULT_CHARSET
737 UINT WINAPI
GetTextCharset(
738 HDC hdc
) /* [in] Handle to device context */
740 /* MSDN docs say this is equivalent */
741 return GetTextCharsetInfo(hdc
, NULL
, 0);
744 /***********************************************************************
745 * GetTextCharset16 [GDI.612]
747 UINT16 WINAPI
GetTextCharset16(HDC16 hdc
)
749 return (UINT16
)GetTextCharset(hdc
);
752 /***********************************************************************
753 * GetTextCharsetInfo [GDI32.381] Gets character set for font
756 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
757 * Should it return a UINT32 instead of an INT32?
758 * => YES and YES, from win32.hlp from Borland
761 * Success: Character set identifier
762 * Failure: DEFAULT_CHARSET
764 UINT WINAPI
GetTextCharsetInfo(
765 HDC hdc
, /* [in] Handle to device context */
766 LPFONTSIGNATURE fs
, /* [out] Pointer to struct to receive data */
767 DWORD flags
) /* [in] Reserved - must be 0 */
770 UINT charSet
= DEFAULT_CHARSET
;
774 hFont
= GetCurrentObject(hdc
, OBJ_FONT
);
776 return(DEFAULT_CHARSET
);
777 if ( GetObjectW(hFont
, sizeof(LOGFONTW
), &lf
) != 0 )
778 charSet
= lf
.lfCharSet
;
781 if (!TranslateCharsetInfo((LPDWORD
)charSet
, &csinfo
, TCI_SRCCHARSET
))
782 return (DEFAULT_CHARSET
);
783 memcpy(fs
, &csinfo
.fs
, sizeof(FONTSIGNATURE
));
788 /***********************************************************************
789 * PolyTextOutA [GDI.402] Draw several Strings
791 BOOL WINAPI
PolyTextOutA (
792 HDC hdc
, /* Handle to device context */
793 PPOLYTEXTA pptxt
, /* array of strings */
794 INT cStrings
/* Number of strings in array */
798 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED
);
804 /***********************************************************************
805 * PolyTextOutW [GDI.403] Draw several Strings
807 BOOL WINAPI
PolyTextOutW (
808 HDC hdc
, /* Handle to device context */
809 PPOLYTEXTW pptxt
, /* array of strings */
810 INT cStrings
/* Number of strings in array */
814 SetLastError ( ERROR_CALL_NOT_IMPLEMENTED
);