4 * Copyright 1993, 1994 Alexandre Julliard
11 #include "wine/winuser16.h"
25 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
27 static int tabstop
= 8;
29 static int spacewidth
;
30 static int prefix_offset
;
32 static const char *TEXT_NextLine( HDC16 hdc
, const char *str
, int *count
,
33 char *dest
, int *len
, int width
, WORD format
)
35 /* Return next line of text from a string.
38 * str - string to parse into lines.
39 * count - length of str.
40 * dest - destination in which to return line.
41 * len - length of resultant line in dest in chars.
42 * width - maximum width of line in pixels.
43 * format - format type passed to DrawText.
45 * Returns pointer to next char in str after end of the line
46 * or NULL if end of str reached.
54 int wb_i
= 0, wb_j
= 0, wb_count
= 0;
62 if (!(format
& DT_SINGLELINE
))
64 if ((*count
> 1) && (str
[i
] == CR
) && (str
[i
+1] == LF
))
75 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
76 (format
& DT_WORDBREAK
))
78 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
85 if (!(format
& DT_NOPREFIX
) && *count
> 1)
87 if (str
[++i
] == PREFIX
)
95 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
96 (format
& DT_WORDBREAK
))
98 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
105 if (format
& DT_EXPANDTABS
)
111 if (!GetTextExtentPoint16(hdc
, &dest
[lasttab
], j
- lasttab
,
115 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
116 for (k
= 0; k
< numspaces
; k
++)
118 plen
+= tabwidth
- size
.cx
;
119 lasttab
= wb_j
+ numspaces
;
123 dest
[j
++] = str
[i
++];
124 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
125 (format
& DT_WORDBREAK
))
127 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
135 dest
[j
++] = str
[i
++];
136 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
137 (format
& DT_WORDBREAK
))
142 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
149 dest
[j
++] = str
[i
++];
150 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
151 (format
& DT_WORDBREAK
))
153 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
160 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
164 if (format
& DT_WORDBREAK
)
169 *count
= wb_count
- 1;
187 /***********************************************************************
188 * DrawText16 (USER.85)
190 INT16 WINAPI
DrawText16( HDC16 hdc
, LPCSTR str
, INT16 i_count
,
191 LPRECT16 rect
, UINT16 flags
)
195 static char line
[1024];
196 int len
, lh
, count
=i_count
;
200 int x
= rect
->left
, y
= rect
->top
;
201 int width
= rect
->right
- rect
->left
;
204 TRACE(text
,"%s, %d , [(%d,%d),(%d,%d)]\n",
205 debugstr_an (str
, count
), count
,
206 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
208 if (count
== -1) count
= strlen(str
);
211 GetTextMetrics16(hdc
, &tm
);
212 if (flags
& DT_EXTERNALLEADING
)
213 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
217 if (flags
& DT_TABSTOP
)
218 tabstop
= flags
>> 8;
220 if (flags
& DT_EXPANDTABS
)
222 GetTextExtentPoint16(hdc
, " ", 1, &size
);
223 spacewidth
= size
.cx
;
224 GetTextExtentPoint16(hdc
, "o", 1, &size
);
225 tabwidth
= size
.cx
* tabstop
;
228 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
233 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
235 if (prefix_offset
!= -1)
237 GetTextExtentPoint16(hdc
, line
, prefix_offset
, &size
);
239 GetTextExtentPoint16(hdc
, line
, prefix_offset
+ 1, &size
);
240 prefix_end
= size
.cx
- 1;
243 if (!GetTextExtentPoint16(hdc
, line
, len
, &size
)) return 0;
244 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
246 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
248 if (flags
& DT_SINGLELINE
)
250 if (flags
& DT_VCENTER
) y
= rect
->top
+
251 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
252 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
254 if (!(flags
& DT_CALCRECT
))
256 if (!ExtTextOut16(hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
257 rect
, line
, len
, NULL
)) return 0;
258 if (prefix_offset
!= -1)
260 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
261 HPEN oldPen
= SelectObject( hdc
, hpen
);
262 MoveTo16(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
263 LineTo(hdc
, x
+ prefix_end
+ 1, y
+ tm
.tmAscent
+ 1 );
264 SelectObject( hdc
, oldPen
);
265 DeleteObject( hpen
);
268 else if (size
.cx
> max_width
)
274 if (!(flags
& DT_NOCLIP
))
276 if (y
> rect
->bottom
- lh
)
282 if (flags
& DT_CALCRECT
)
284 rect
->right
= rect
->left
+ max_width
;
287 return y
- rect
->top
;
291 /***********************************************************************
292 * DrawText32A (USER32.164)
294 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
,
295 LPRECT rect
, UINT flags
)
301 return DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, NULL
, (UINT16
)flags
);
302 CONV_RECT32TO16( rect
, &rect16
);
303 ret
= DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, &rect16
, (UINT16
)flags
);
304 CONV_RECT16TO32( &rect16
, rect
);
309 /***********************************************************************
310 * DrawText32W (USER32.167)
312 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
,
313 LPRECT rect
, UINT flags
)
315 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
316 INT ret
= DrawTextA( hdc
, p
, count
, rect
, flags
);
317 HeapFree( GetProcessHeap(), 0, p
);
321 /***********************************************************************
322 * DrawTextEx32A (USER32.165)
324 INT WINAPI
DrawTextExA( HDC hdc
, LPCSTR str
, INT count
,
325 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
327 TRACE(text
,"(%d,'%s',%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
329 FIXME(text
,"Ignores params:%d,%d,%d,%d,%d\n",dtp
->cbSize
,
330 dtp
->iTabLength
,dtp
->iLeftMargin
,dtp
->iRightMargin
,
333 return DrawTextA(hdc
,str
,count
,rect
,flags
);
336 /***********************************************************************
337 * DrawTextEx32W (USER32.166)
339 INT WINAPI
DrawTextExW( HDC hdc
, LPCWSTR str
, INT count
,
340 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
342 TRACE(text
,"(%d,%p,%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
343 FIXME(text
,"ignores extended functionality\n");
344 return DrawTextW(hdc
,str
,count
,rect
,flags
);
347 /***********************************************************************
348 * ExtTextOut16 (GDI.351)
350 BOOL16 WINAPI
ExtTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, UINT16 flags
,
351 const RECT16
*lprect
, LPCSTR str
, UINT16 count
,
359 if (lpDx
) lpdx32
= (LPINT
)HEAP_xalloc( GetProcessHeap(), 0,
361 if (lprect
) CONV_RECT16TO32(lprect
,&rect32
);
362 if (lpdx32
) for (i
=count
;i
--;) lpdx32
[i
]=lpDx
[i
];
363 ret
= ExtTextOutA(hdc
,x
,y
,flags
,lprect
?&rect32
:NULL
,str
,count
,lpdx32
);
364 if (lpdx32
) HeapFree( GetProcessHeap(), 0, lpdx32
);
371 /***********************************************************************
372 * ExtTextOut32A (GDI32.98)
374 BOOL WINAPI
ExtTextOutA( HDC hdc
, INT x
, INT y
, UINT flags
,
375 const RECT
*lprect
, LPCSTR str
, UINT count
,
378 DC
* dc
= DC_GetDCPtr( hdc
);
379 return dc
&& dc
->funcs
->pExtTextOut
&&
380 dc
->funcs
->pExtTextOut(dc
,x
,y
,flags
,lprect
,str
,count
,lpDx
);
384 /***********************************************************************
385 * ExtTextOut32W (GDI32.99)
387 BOOL WINAPI
ExtTextOutW( HDC hdc
, INT x
, INT y
, UINT flags
,
388 const RECT
*lprect
, LPCWSTR str
, UINT count
,
391 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
392 INT ret
= ExtTextOutA( hdc
, x
, y
, flags
, lprect
, p
, count
, lpDx
);
393 HeapFree( GetProcessHeap(), 0, p
);
398 /***********************************************************************
401 BOOL16 WINAPI
TextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR str
, INT16 count
)
403 return ExtTextOut16( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
407 /***********************************************************************
408 * TextOut32A (GDI32.355)
410 BOOL WINAPI
TextOutA( HDC hdc
, INT x
, INT y
, LPCSTR str
, INT count
)
412 return ExtTextOutA( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
416 /***********************************************************************
417 * TextOut32W (GDI32.356)
419 BOOL WINAPI
TextOutW(HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
)
421 return ExtTextOutW( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
425 /***********************************************************************
428 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
429 * heap and we can guarantee that the handles fit in an INT16. We have to
430 * rethink the strategy once the migration to NT handles is complete.
431 * We are going to get a lot of code-duplication once this migration is
435 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
,
436 GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
437 INT x
, INT y
, INT cx
, INT cy
,
438 BOOL unicode
, BOOL _32bit
)
440 HBITMAP hbm
, hbmsave
;
443 HDC memdc
= CreateCompatibleDC(hdc
);
449 if(!hdc
) return FALSE
;
454 slen
= lstrlenW((LPCWSTR
)lp
);
456 slen
= lstrlenA((LPCSTR
)lp
);
458 slen
= lstrlenA((LPCSTR
)PTR_SEG_TO_LIN(lp
));
461 if((cx
== 0 || cy
== 0) && slen
!= -1)
465 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
467 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
469 GetTextExtentPoint32A(hdc
, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
, &s
);
470 if(cx
== 0) cx
= s
.cx
;
471 if(cy
== 0) cy
= s
.cy
;
478 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
479 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
480 FillRect(memdc
, &r
, (HBRUSH
)GetStockObject(BLACK_BRUSH
));
481 SetTextColor(memdc
, RGB(255, 255, 255));
482 SetBkColor(memdc
, RGB(0, 0, 0));
483 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
487 retval
= fn(memdc
, lp
, slen
);
489 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
492 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
494 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
496 TextOutA(memdc
, 0, 0, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
);
498 SelectObject(memdc
, hfsave
);
501 * Windows doc says that the bitmap isn't grayed when len == -1 and
502 * the callback function returns FALSE. However, testing this on
503 * win95 showed otherwise...
505 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
506 if(retval
|| len
!= -1)
509 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
510 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
511 SelectObject(memdc
, hbsave
);
514 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
515 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
516 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
517 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
518 SetTextColor(hdc
, fg
);
520 if(hb
) SelectObject(hdc
, hbsave
);
522 SelectObject(memdc
, hbmsave
);
529 /***********************************************************************
530 * GrayString16 (USER.185)
532 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
533 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
536 return TEXT_GrayString(hdc
, hbr
, (GRAYSTRINGPROC
)gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
540 /***********************************************************************
541 * GrayString32A (USER32.315)
543 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
544 LPARAM lParam
, INT cch
, INT x
, INT y
,
547 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, TRUE
);
551 /***********************************************************************
552 * GrayString32W (USER32.316)
554 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
555 LPARAM lParam
, INT cch
, INT x
, INT y
,
558 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, TRUE
, TRUE
);
562 /***********************************************************************
565 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
566 * Note: this doesn't work too well for text-alignment modes other
567 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
569 LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
570 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
571 const INT
*lpTabPos32
, INT nTabOrg
,
581 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
587 GetTextMetrics16( hdc
, &tm
);
588 defWidth
= 8 * tm
.tmAveCharWidth
;
593 for (i
= 0; i
< count
; i
++)
594 if (lpstr
[i
] == '\t') break;
595 extent
= GetTextExtent16( hdc
, lpstr
, i
);
598 while ((cTabStops
> 0) &&
599 (nTabOrg
+ *lpTabPos32
<= x
+ LOWORD(extent
)))
607 while ((cTabStops
> 0) &&
608 (nTabOrg
+ *lpTabPos16
<= x
+ LOWORD(extent
)))
615 tabPos
= x
+ LOWORD(extent
);
616 else if (cTabStops
> 0)
617 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
619 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
623 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
624 ExtTextOutA( hdc
, x
, y
,
625 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
626 &r
, lpstr
, i
, NULL
);
632 return MAKELONG(tabPos
- start
, HIWORD(extent
));
636 /***********************************************************************
637 * TabbedTextOut16 (USER.196)
639 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
640 INT16 count
, INT16 cTabStops
,
641 const INT16
*lpTabPos
, INT16 nTabOrg
)
643 TRACE(text
, "%04x %d,%d '%.*s' %d\n",
644 hdc
, x
, y
, count
, lpstr
, count
);
645 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
646 lpTabPos
, NULL
, nTabOrg
, TRUE
);
650 /***********************************************************************
651 * TabbedTextOut32A (USER32.542)
653 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
654 INT count
, INT cTabStops
,
655 const INT
*lpTabPos
, INT nTabOrg
)
657 TRACE(text
, "%04x %d,%d '%.*s' %d\n",
658 hdc
, x
, y
, count
, lpstr
, count
);
659 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
660 NULL
, lpTabPos
, nTabOrg
, TRUE
);
664 /***********************************************************************
665 * TabbedTextOut32W (USER32.543)
667 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
,
668 INT count
, INT cTabStops
,
669 const INT
*lpTabPos
, INT nTabOrg
)
672 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
673 lstrcpynWtoA( p
, str
, count
+ 1 );
674 ret
= TabbedTextOutA( hdc
, x
, y
, p
, count
, cTabStops
,
676 HeapFree( GetProcessHeap(), 0, p
);
681 /***********************************************************************
682 * GetTabbedTextExtent16 (USER.197)
684 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
685 INT16 cTabStops
, const INT16
*lpTabPos
)
687 TRACE(text
, "%04x '%.*s' %d\n",
688 hdc
, count
, lpstr
, count
);
689 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
690 lpTabPos
, NULL
, 0, FALSE
);
694 /***********************************************************************
695 * GetTabbedTextExtent32A (USER32.293)
697 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
698 INT cTabStops
, const INT
*lpTabPos
)
700 TRACE(text
, "%04x '%.*s' %d\n",
701 hdc
, count
, lpstr
, count
);
702 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
703 NULL
, lpTabPos
, 0, FALSE
);
707 /***********************************************************************
708 * GetTabbedTextExtent32W (USER32.294)
710 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
711 INT cTabStops
, const INT
*lpTabPos
)
714 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
715 lstrcpynWtoA( p
, lpstr
, count
+ 1 );
716 ret
= GetTabbedTextExtentA( hdc
, p
, count
, cTabStops
, lpTabPos
);
717 HeapFree( GetProcessHeap(), 0, p
);
721 /***********************************************************************
722 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
725 * Should it return a UINT32 instead of an INT32?
726 * => YES, as GetTextCharsetInfo returns UINT32
729 * Success: Character set identifier
730 * Failure: DEFAULT_CHARSET
732 UINT WINAPI
GetTextCharset(
733 HDC hdc
) /* [in] Handle to device context */
735 /* MSDN docs say this is equivalent */
736 return GetTextCharsetInfo(hdc
, NULL
, 0);
739 /***********************************************************************
740 * GetTextCharset16 [GDI.612]
742 UINT16 WINAPI
GetTextCharset16(HDC16 hdc
)
744 return (UINT16
)GetTextCharset(hdc
);
747 /***********************************************************************
748 * GetTextCharsetInfo [GDI32.381] Gets character set for font
751 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
752 * Should it return a UINT32 instead of an INT32?
753 * => YES and YES, from win32.hlp from Borland
756 * Success: Character set identifier
757 * Failure: DEFAULT_CHARSET
759 UINT WINAPI
GetTextCharsetInfo(
760 HDC hdc
, /* [in] Handle to device context */
761 LPFONTSIGNATURE fs
, /* [out] Pointer to struct to receive data */
762 DWORD flags
) /* [in] Reserved - must be 0 */
765 UINT charSet
= DEFAULT_CHARSET
;
769 hFont
= GetCurrentObject(hdc
, OBJ_FONT
);
771 return(DEFAULT_CHARSET
);
772 if ( GetObjectW(hFont
, sizeof(LOGFONTW
), &lf
) != 0 )
773 charSet
= lf
.lfCharSet
;
776 if (!TranslateCharsetInfo((LPDWORD
)charSet
, &csinfo
, TCI_SRCCHARSET
))
777 return (DEFAULT_CHARSET
);
778 memcpy(fs
, &csinfo
.fs
, sizeof(FONTSIGNATURE
));