4 * Copyright 1993, 1994 Alexandre Julliard
11 #include "wine/winuser16.h"
19 DEFAULT_DEBUG_CHANNEL(text
)
27 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
29 static int tabstop
= 8;
31 static int spacewidth
;
32 static int prefix_offset
;
34 static const char *TEXT_NextLine( HDC16 hdc
, const char *str
, int *count
,
35 char *dest
, int *len
, int width
, WORD format
)
37 /* Return next line of text from a string.
40 * str - string to parse into lines.
41 * count - length of str.
42 * dest - destination in which to return line.
43 * len - length of resultant line in dest in chars.
44 * width - maximum width of line in pixels.
45 * format - format type passed to DrawText.
47 * Returns pointer to next char in str after end of the line
48 * or NULL if end of str reached.
56 int wb_i
= 0, wb_j
= 0, wb_count
= 0;
64 if (!(format
& DT_SINGLELINE
))
66 if ((*count
> 1) && (str
[i
] == CR
) && (str
[i
+1] == LF
))
77 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
78 (format
& DT_WORDBREAK
))
80 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
87 if (!(format
& DT_NOPREFIX
) && *count
> 1)
89 if (str
[++i
] == PREFIX
)
97 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
98 (format
& DT_WORDBREAK
))
100 if (!GetTextExtentPoint16(hdc
, &dest
[j
-1], 1, &size
))
107 if (format
& DT_EXPANDTABS
)
113 if (!GetTextExtentPoint16(hdc
, &dest
[lasttab
], j
- lasttab
,
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 (!GetTextExtentPoint16(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 (!GetTextExtentPoint16(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 (!GetTextExtentPoint16(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 i_count
,
193 LPRECT16 rect
, UINT16 flags
)
197 static char line
[1024];
198 int len
, lh
, count
=i_count
;
202 int x
= rect
->left
, y
= rect
->top
;
203 int width
= rect
->right
- rect
->left
;
206 TRACE(text
,"%s, %d , [(%d,%d),(%d,%d)]\n",
207 debugstr_an (str
, count
), count
,
208 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
210 if (count
== -1) count
= strlen(str
);
213 GetTextMetrics16(hdc
, &tm
);
214 if (flags
& DT_EXTERNALLEADING
)
215 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
219 if (flags
& DT_TABSTOP
)
220 tabstop
= flags
>> 8;
222 if (flags
& DT_EXPANDTABS
)
224 GetTextExtentPoint16(hdc
, " ", 1, &size
);
225 spacewidth
= size
.cx
;
226 GetTextExtentPoint16(hdc
, "o", 1, &size
);
227 tabwidth
= size
.cx
* tabstop
;
230 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
235 strPtr
= TEXT_NextLine(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
237 if (prefix_offset
!= -1)
239 GetTextExtentPoint16(hdc
, line
, prefix_offset
, &size
);
241 GetTextExtentPoint16(hdc
, line
, prefix_offset
+ 1, &size
);
242 prefix_end
= size
.cx
- 1;
245 if (!GetTextExtentPoint16(hdc
, line
, len
, &size
)) return 0;
246 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
248 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
250 if (flags
& DT_SINGLELINE
)
252 if (flags
& DT_VCENTER
) y
= rect
->top
+
253 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
254 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
256 if (!(flags
& DT_CALCRECT
))
258 if (!ExtTextOut16(hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
259 rect
, line
, len
, NULL
)) return 0;
260 if (prefix_offset
!= -1)
262 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
263 HPEN oldPen
= SelectObject( hdc
, hpen
);
264 MoveTo16(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1 );
265 LineTo(hdc
, x
+ prefix_end
+ 1, y
+ tm
.tmAscent
+ 1 );
266 SelectObject( hdc
, oldPen
);
267 DeleteObject( hpen
);
270 else if (size
.cx
> max_width
)
276 if (!(flags
& DT_NOCLIP
))
278 if (y
> rect
->bottom
- lh
)
284 if (flags
& DT_CALCRECT
)
286 rect
->right
= rect
->left
+ max_width
;
289 return y
- rect
->top
;
293 /***********************************************************************
294 * DrawText32A (USER32.164)
296 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
,
297 LPRECT rect
, UINT flags
)
303 return DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, NULL
, (UINT16
)flags
);
304 CONV_RECT32TO16( rect
, &rect16
);
305 ret
= DrawText16( (HDC16
)hdc
, str
, (INT16
)count
, &rect16
, (UINT16
)flags
);
306 CONV_RECT16TO32( &rect16
, rect
);
311 /***********************************************************************
312 * DrawText32W (USER32.167)
314 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
,
315 LPRECT rect
, UINT flags
)
317 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
318 INT ret
= DrawTextA( hdc
, p
, count
, rect
, flags
);
319 HeapFree( GetProcessHeap(), 0, p
);
323 /***********************************************************************
324 * DrawTextEx32A (USER32.165)
326 INT WINAPI
DrawTextExA( HDC hdc
, LPCSTR str
, INT count
,
327 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
329 TRACE(text
,"(%d,'%s',%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
331 FIXME(text
,"Ignores params:%d,%d,%d,%d,%d\n",dtp
->cbSize
,
332 dtp
->iTabLength
,dtp
->iLeftMargin
,dtp
->iRightMargin
,
335 return DrawTextA(hdc
,str
,count
,rect
,flags
);
338 /***********************************************************************
339 * DrawTextEx32W (USER32.166)
341 INT WINAPI
DrawTextExW( HDC hdc
, LPCWSTR str
, INT count
,
342 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
344 TRACE(text
,"(%d,%p,%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
345 FIXME(text
,"ignores extended functionality\n");
346 return DrawTextW(hdc
,str
,count
,rect
,flags
);
349 /***********************************************************************
350 * ExtTextOut16 (GDI.351)
352 BOOL16 WINAPI
ExtTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, UINT16 flags
,
353 const RECT16
*lprect
, LPCSTR str
, UINT16 count
,
361 if (lpDx
) lpdx32
= (LPINT
)HEAP_xalloc( GetProcessHeap(), 0,
363 if (lprect
) CONV_RECT16TO32(lprect
,&rect32
);
364 if (lpdx32
) for (i
=count
;i
--;) lpdx32
[i
]=lpDx
[i
];
365 ret
= ExtTextOutA(hdc
,x
,y
,flags
,lprect
?&rect32
:NULL
,str
,count
,lpdx32
);
366 if (lpdx32
) HeapFree( GetProcessHeap(), 0, lpdx32
);
373 /***********************************************************************
374 * ExtTextOut32A (GDI32.98)
376 BOOL WINAPI
ExtTextOutA( HDC hdc
, INT x
, INT y
, UINT flags
,
377 const RECT
*lprect
, LPCSTR str
, UINT count
,
380 DC
* dc
= DC_GetDCPtr( hdc
);
381 return dc
&& dc
->funcs
->pExtTextOut
&&
382 dc
->funcs
->pExtTextOut(dc
,x
,y
,flags
,lprect
,str
,count
,lpDx
);
386 /***********************************************************************
387 * ExtTextOut32W (GDI32.99)
389 BOOL WINAPI
ExtTextOutW( HDC hdc
, INT x
, INT y
, UINT flags
,
390 const RECT
*lprect
, LPCWSTR str
, UINT count
,
393 LPSTR p
= HEAP_strdupWtoA( GetProcessHeap(), 0, str
);
394 INT ret
= ExtTextOutA( hdc
, x
, y
, flags
, lprect
, p
, count
, lpDx
);
395 HeapFree( GetProcessHeap(), 0, p
);
400 /***********************************************************************
403 BOOL16 WINAPI
TextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR str
, INT16 count
)
405 return ExtTextOut16( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
409 /***********************************************************************
410 * TextOut32A (GDI32.355)
412 BOOL WINAPI
TextOutA( HDC hdc
, INT x
, INT y
, LPCSTR str
, INT count
)
414 return ExtTextOutA( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
418 /***********************************************************************
419 * TextOut32W (GDI32.356)
421 BOOL WINAPI
TextOutW(HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
)
423 return ExtTextOutW( hdc
, x
, y
, 0, NULL
, str
, count
, NULL
);
427 /***********************************************************************
430 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
431 * heap and we can guarantee that the handles fit in an INT16. We have to
432 * rethink the strategy once the migration to NT handles is complete.
433 * We are going to get a lot of code-duplication once this migration is
437 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
,
438 GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
439 INT x
, INT y
, INT cx
, INT cy
,
440 BOOL unicode
, BOOL _32bit
)
442 HBITMAP hbm
, hbmsave
;
445 HDC memdc
= CreateCompatibleDC(hdc
);
451 if(!hdc
) return FALSE
;
456 slen
= lstrlenW((LPCWSTR
)lp
);
458 slen
= lstrlenA((LPCSTR
)lp
);
460 slen
= lstrlenA((LPCSTR
)PTR_SEG_TO_LIN(lp
));
463 if((cx
== 0 || cy
== 0) && slen
!= -1)
467 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
469 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
471 GetTextExtentPoint32A(hdc
, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
, &s
);
472 if(cx
== 0) cx
= s
.cx
;
473 if(cy
== 0) cy
= s
.cy
;
480 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
481 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
482 FillRect(memdc
, &r
, (HBRUSH
)GetStockObject(BLACK_BRUSH
));
483 SetTextColor(memdc
, RGB(255, 255, 255));
484 SetBkColor(memdc
, RGB(0, 0, 0));
485 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
489 retval
= fn(memdc
, lp
, slen
);
491 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
494 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
496 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
498 TextOutA(memdc
, 0, 0, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
);
500 SelectObject(memdc
, hfsave
);
503 * Windows doc says that the bitmap isn't grayed when len == -1 and
504 * the callback function returns FALSE. However, testing this on
505 * win95 showed otherwise...
507 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
508 if(retval
|| len
!= -1)
511 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
512 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
513 SelectObject(memdc
, hbsave
);
516 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
517 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
518 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
519 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
520 SetTextColor(hdc
, fg
);
522 if(hb
) SelectObject(hdc
, hbsave
);
524 SelectObject(memdc
, hbmsave
);
531 /***********************************************************************
532 * GrayString16 (USER.185)
534 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
535 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
538 return TEXT_GrayString(hdc
, hbr
, (GRAYSTRINGPROC
)gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, FALSE
);
542 /***********************************************************************
543 * GrayString32A (USER32.315)
545 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
546 LPARAM lParam
, INT cch
, INT x
, INT y
,
549 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, FALSE
, TRUE
);
553 /***********************************************************************
554 * GrayString32W (USER32.316)
556 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
557 LPARAM lParam
, INT cch
, INT x
, INT y
,
560 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
, TRUE
, TRUE
);
564 /***********************************************************************
567 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
568 * Note: this doesn't work too well for text-alignment modes other
569 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
571 LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
572 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
573 const INT
*lpTabPos32
, INT nTabOrg
,
583 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
589 GetTextMetrics16( hdc
, &tm
);
590 defWidth
= 8 * tm
.tmAveCharWidth
;
595 for (i
= 0; i
< count
; i
++)
596 if (lpstr
[i
] == '\t') break;
597 extent
= GetTextExtent16( hdc
, lpstr
, i
);
600 while ((cTabStops
> 0) &&
601 (nTabOrg
+ *lpTabPos32
<= x
+ LOWORD(extent
)))
609 while ((cTabStops
> 0) &&
610 (nTabOrg
+ *lpTabPos16
<= x
+ LOWORD(extent
)))
617 tabPos
= x
+ LOWORD(extent
);
618 else if (cTabStops
> 0)
619 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
621 tabPos
= nTabOrg
+ ((x
+ LOWORD(extent
) - nTabOrg
) / defWidth
+ 1) * defWidth
;
625 SetRect( &r
, x
, y
, tabPos
, y
+HIWORD(extent
) );
626 ExtTextOutA( hdc
, x
, y
,
627 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
628 &r
, lpstr
, i
, NULL
);
634 return MAKELONG(tabPos
- start
, HIWORD(extent
));
638 /***********************************************************************
639 * TabbedTextOut16 (USER.196)
641 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
642 INT16 count
, INT16 cTabStops
,
643 const INT16
*lpTabPos
, INT16 nTabOrg
)
645 TRACE(text
, "%04x %d,%d '%.*s' %d\n",
646 hdc
, x
, y
, count
, lpstr
, count
);
647 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
648 lpTabPos
, NULL
, nTabOrg
, TRUE
);
652 /***********************************************************************
653 * TabbedTextOut32A (USER32.542)
655 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
656 INT count
, INT cTabStops
,
657 const INT
*lpTabPos
, INT nTabOrg
)
659 TRACE(text
, "%04x %d,%d '%.*s' %d\n",
660 hdc
, x
, y
, count
, lpstr
, count
);
661 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
662 NULL
, lpTabPos
, nTabOrg
, TRUE
);
666 /***********************************************************************
667 * TabbedTextOut32W (USER32.543)
669 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
,
670 INT count
, INT cTabStops
,
671 const INT
*lpTabPos
, INT nTabOrg
)
674 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
675 lstrcpynWtoA( p
, str
, count
+ 1 );
676 ret
= TabbedTextOutA( hdc
, x
, y
, p
, count
, cTabStops
,
678 HeapFree( GetProcessHeap(), 0, p
);
683 /***********************************************************************
684 * GetTabbedTextExtent16 (USER.197)
686 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
687 INT16 cTabStops
, const INT16
*lpTabPos
)
689 TRACE(text
, "%04x '%.*s' %d\n",
690 hdc
, count
, lpstr
, count
);
691 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
692 lpTabPos
, NULL
, 0, FALSE
);
696 /***********************************************************************
697 * GetTabbedTextExtent32A (USER32.293)
699 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
700 INT cTabStops
, const INT
*lpTabPos
)
702 TRACE(text
, "%04x '%.*s' %d\n",
703 hdc
, count
, lpstr
, count
);
704 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
705 NULL
, lpTabPos
, 0, FALSE
);
709 /***********************************************************************
710 * GetTabbedTextExtent32W (USER32.294)
712 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
713 INT cTabStops
, const INT
*lpTabPos
)
716 LPSTR p
= HEAP_xalloc( GetProcessHeap(), 0, count
+ 1 );
717 lstrcpynWtoA( p
, lpstr
, count
+ 1 );
718 ret
= GetTabbedTextExtentA( hdc
, p
, count
, cTabStops
, lpTabPos
);
719 HeapFree( GetProcessHeap(), 0, p
);
723 /***********************************************************************
724 * GetTextCharset32 [GDI32.226] Gets character set for font in DC
727 * Should it return a UINT32 instead of an INT32?
728 * => YES, as GetTextCharsetInfo returns UINT32
731 * Success: Character set identifier
732 * Failure: DEFAULT_CHARSET
734 UINT WINAPI
GetTextCharset(
735 HDC hdc
) /* [in] Handle to device context */
737 /* MSDN docs say this is equivalent */
738 return GetTextCharsetInfo(hdc
, NULL
, 0);
741 /***********************************************************************
742 * GetTextCharset16 [GDI.612]
744 UINT16 WINAPI
GetTextCharset16(HDC16 hdc
)
746 return (UINT16
)GetTextCharset(hdc
);
749 /***********************************************************************
750 * GetTextCharsetInfo [GDI32.381] Gets character set for font
753 * Should csi be an LPFONTSIGNATURE instead of an LPCHARSETINFO?
754 * Should it return a UINT32 instead of an INT32?
755 * => YES and YES, from win32.hlp from Borland
758 * Success: Character set identifier
759 * Failure: DEFAULT_CHARSET
761 UINT WINAPI
GetTextCharsetInfo(
762 HDC hdc
, /* [in] Handle to device context */
763 LPFONTSIGNATURE fs
, /* [out] Pointer to struct to receive data */
764 DWORD flags
) /* [in] Reserved - must be 0 */
767 UINT charSet
= DEFAULT_CHARSET
;
771 hFont
= GetCurrentObject(hdc
, OBJ_FONT
);
773 return(DEFAULT_CHARSET
);
774 if ( GetObjectW(hFont
, sizeof(LOGFONTW
), &lf
) != 0 )
775 charSet
= lf
.lfCharSet
;
778 if (!TranslateCharsetInfo((LPDWORD
)charSet
, &csinfo
, TCI_SRCCHARSET
))
779 return (DEFAULT_CHARSET
);
780 memcpy(fs
, &csinfo
.fs
, sizeof(FONTSIGNATURE
));