4 * Copyright 1993, 1994 Alexandre Julliard
12 #include "wine/winuser16.h"
13 #include "wine/unicode.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(text
);
29 #define ELLIPSIS "..."
30 #define FORWARD_SLASH '/'
31 #define BACK_SLASH '\\'
33 static const WCHAR SPACEW
[] = {' ', 0};
34 static const WCHAR oW
[] = {'o', 0};
35 static const WCHAR ELLIPSISW
[] = {'.','.','.', 0};
36 static const WCHAR FORWARD_SLASHW
[] = {'/', 0};
37 static const WCHAR BACK_SLASHW
[] = {'\\', 0};
39 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
41 static int tabstop
= 8;
43 static int spacewidth
;
44 static int prefix_offset
;
46 /*********************************************************************
47 * Return next line of text from a string.
50 * str - string to parse into lines.
51 * count - length of str.
52 * dest - destination in which to return line.
53 * len - length of resultant line in dest in chars.
54 * width - maximum width of line in pixels.
55 * format - format type passed to DrawText.
57 * Returns pointer to next char in str after end of the line
58 * or NULL if end of str reached.
61 * GetTextExtentPoint is used to get the width of each character,
62 * rather than GetCharABCWidth... So the whitespace between
63 * characters is ignored, and the reported len is too great.
65 static const WCHAR
*TEXT_NextLineW( HDC hdc
, const WCHAR
*str
, int *count
,
66 WCHAR
*dest
, int *len
, int width
, WORD format
)
73 int wb_i
= 0, wb_j
= 0, wb_count
= 0;
81 if (!(format
& DT_SINGLELINE
))
83 if ((*count
> 1) && (str
[i
] == CR
) && (str
[i
+1] == LF
))
94 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
95 (format
& DT_WORDBREAK
))
97 if (!GetTextExtentPointW(hdc
, &dest
[j
-1], 1, &size
))
104 if (!(format
& DT_NOPREFIX
) && *count
> 1)
106 if (str
[++i
] == PREFIX
)
113 dest
[j
++] = str
[i
++];
114 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
115 (format
& DT_WORDBREAK
))
117 if (!GetTextExtentPointW(hdc
, &dest
[j
-1], 1, &size
))
124 if (format
& DT_EXPANDTABS
)
130 if (!GetTextExtentPointW(hdc
, &dest
[lasttab
], j
- lasttab
, &size
))
133 numspaces
= (tabwidth
- size
.cx
) / spacewidth
;
134 for (k
= 0; k
< numspaces
; k
++)
136 plen
+= tabwidth
- size
.cx
;
137 lasttab
= wb_j
+ numspaces
;
141 dest
[j
++] = str
[i
++];
142 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
143 (format
& DT_WORDBREAK
))
145 if (!GetTextExtentPointW(hdc
, &dest
[j
-1], 1, &size
))
153 dest
[j
++] = str
[i
++];
154 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
155 (format
& DT_WORDBREAK
))
160 if (!GetTextExtentPointW(hdc
, &dest
[j
-1], 1, &size
))
167 dest
[j
++] = str
[i
++];
168 if (!(format
& DT_NOCLIP
) || !(format
& DT_NOPREFIX
) ||
169 (format
& DT_WORDBREAK
))
171 if (!GetTextExtentPointW(hdc
, &dest
[j
-1], 1, &size
))
178 if (!(format
& DT_NOCLIP
) || (format
& DT_WORDBREAK
))
182 if (format
& DT_WORDBREAK
)
187 *count
= wb_count
- 1;
205 /***********************************************************************
206 * DrawText16 (USER.85)
208 INT16 WINAPI
DrawText16( HDC16 hdc
, LPCSTR str
, INT16 count
, LPRECT16 rect
, UINT16 flags
)
215 CONV_RECT16TO32( rect
, &rect32
);
216 ret
= DrawTextA( hdc
, str
, count
, &rect32
, flags
);
217 CONV_RECT32TO16( &rect32
, rect
);
219 else ret
= DrawTextA( hdc
, str
, count
, NULL
, flags
);
224 /***********************************************************************
225 * DrawTextExW (USER32.166)
227 INT WINAPI
DrawTextExW( HDC hdc
, LPCWSTR str
, INT i_count
,
228 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
232 static WCHAR line
[1024];
233 int len
, lh
, count
=i_count
;
237 int x
= rect
->left
, y
= rect
->top
;
238 int width
= rect
->right
- rect
->left
;
241 TRACE("%s, %d , [(%d,%d),(%d,%d)]\n", debugstr_wn (str
, count
), count
,
242 rect
->left
, rect
->top
, rect
->right
, rect
->bottom
);
245 FIXME("Ignores params:%d,%d,%d,%d\n", dtp
->cbSize
,
246 dtp
->iTabLength
, dtp
->iLeftMargin
, dtp
->iRightMargin
);
250 if (count
== -1) count
= strlenW(str
);
251 if (count
== 0) return 0;
254 GetTextMetricsW(hdc
, &tm
);
255 if (flags
& DT_EXTERNALLEADING
)
256 lh
= tm
.tmHeight
+ tm
.tmExternalLeading
;
260 if (flags
& DT_TABSTOP
)
261 tabstop
= flags
>> 8;
263 if (flags
& DT_EXPANDTABS
)
265 GetTextExtentPointW(hdc
, SPACEW
, 1, &size
);
266 spacewidth
= size
.cx
;
267 GetTextExtentPointW(hdc
, oW
, 1, &size
);
268 tabwidth
= size
.cx
* tabstop
;
271 if (flags
& DT_CALCRECT
) flags
|= DT_NOCLIP
;
276 strPtr
= TEXT_NextLineW(hdc
, strPtr
, &count
, line
, &len
, width
, flags
);
278 if (prefix_offset
!= -1)
280 GetTextExtentPointW(hdc
, line
, prefix_offset
, &size
);
282 GetTextExtentPointW(hdc
, line
, prefix_offset
+ 1, &size
);
283 prefix_end
= size
.cx
- 1;
286 if (!GetTextExtentPointW(hdc
, line
, len
, &size
)) return 0;
287 if (flags
& DT_CENTER
) x
= (rect
->left
+ rect
->right
-
289 else if (flags
& DT_RIGHT
) x
= rect
->right
- size
.cx
;
291 if (flags
& DT_SINGLELINE
)
293 if (flags
& DT_VCENTER
) y
= rect
->top
+
294 (rect
->bottom
- rect
->top
) / 2 - size
.cy
/ 2;
295 else if (flags
& DT_BOTTOM
) y
= rect
->bottom
- size
.cy
;
297 if (flags
& (DT_PATH_ELLIPSIS
| DT_END_ELLIPSIS
| DT_WORD_ELLIPSIS
))
299 WCHAR swapStr
[sizeof(line
)];
300 WCHAR
* fnameDelim
= NULL
;
301 int totalLen
= i_count
>= 0 ? i_count
: strlenW(str
);
305 int fnameLen
= totalLen
;
307 /* allow room for '...' */
308 count
= min(totalLen
+3, sizeof(line
)-3);
310 if (flags
& DT_WORD_ELLIPSIS
)
311 flags
|= DT_WORDBREAK
;
313 if (flags
& DT_PATH_ELLIPSIS
)
315 WCHAR
* lastBkSlash
= NULL
;
316 WCHAR
* lastFwdSlash
= NULL
;
317 strncpyW(line
, str
, totalLen
);
318 line
[totalLen
] = '\0';
319 lastBkSlash
= strrchrW(line
, BACK_SLASHW
[0]);
320 lastFwdSlash
= strrchrW(line
, FORWARD_SLASHW
[0]);
321 fnameDelim
= lastFwdSlash
? lastFwdSlash
: lastBkSlash
;
322 if (lastBkSlash
&& lastFwdSlash
) /* which is last? */
323 if (lastBkSlash
> lastFwdSlash
)
324 fnameDelim
= lastBkSlash
;
327 fnameLen
= &line
[totalLen
] - fnameDelim
;
329 fnameDelim
= (WCHAR
*)str
;
331 strcpyW(swapStr
, ELLIPSISW
);
332 strncpyW(swapStr
+strlenW(swapStr
), fnameDelim
, fnameLen
);
333 swapStr
[fnameLen
+3] = '\0';
334 strncpyW(swapStr
+strlenW(swapStr
), str
, totalLen
- fnameLen
);
335 swapStr
[totalLen
+3] = '\0';
337 else /* DT_END_ELLIPSIS | DT_WORD_ELLIPSIS */
339 strcpyW(swapStr
, ELLIPSISW
);
340 strncpyW(swapStr
+strlenW(swapStr
), str
, totalLen
);
343 TEXT_NextLineW(hdc
, swapStr
, &count
, line
, &len
, width
, flags
);
345 /* if only the ELLIPSIS will fit, just let it be clipped */
347 GetTextExtentPointW(hdc
, line
, len
, &size
);
350 * NextLine uses GetTextExtentPoint for each character,
351 * rather than GetCharABCWidth... So the whitespace between
352 * characters is ignored in the width measurement, and the
353 * reported len is too great. To compensate, we must get
354 * the width of the entire line and adjust len accordingly.
356 while ((size
.cx
> width
) && (len
> 3))
359 GetTextExtentPointW(hdc
, line
, len
, &size
);
362 if (fnameLen
< len
-3) /* some of the path will fit */
364 /* put the ELLIPSIS between the path and filename */
365 strncpyW(swapStr
, &line
[fnameLen
+3], len
-3-fnameLen
);
366 swapStr
[len
-3-fnameLen
] = '\0';
367 strcatW(swapStr
, ELLIPSISW
);
368 strncpyW(swapStr
+strlenW(swapStr
), &line
[3], fnameLen
);
372 /* move the ELLIPSIS to the end */
373 strncpyW(swapStr
, &line
[3], len
-3);
374 swapStr
[len
-3] = '\0';
375 strcpyW(swapStr
+strlenW(swapStr
), ELLIPSISW
);
378 strncpyW(line
, swapStr
, len
);
384 if (!(flags
& DT_CALCRECT
))
386 if (!ExtTextOutW(hdc
, x
, y
, (flags
& DT_NOCLIP
) ? 0 : ETO_CLIPPED
,
387 rect
, line
, len
, NULL
)) return 0;
388 if (prefix_offset
!= -1)
390 HPEN hpen
= CreatePen( PS_SOLID
, 1, GetTextColor(hdc
) );
391 HPEN oldPen
= SelectObject( hdc
, hpen
);
392 MoveToEx(hdc
, x
+ prefix_x
, y
+ tm
.tmAscent
+ 1, NULL
);
393 LineTo(hdc
, x
+ prefix_end
+ 1, y
+ tm
.tmAscent
+ 1 );
394 SelectObject( hdc
, oldPen
);
395 DeleteObject( hpen
);
398 else if (size
.cx
> max_width
)
404 if (!(flags
& DT_NOCLIP
))
406 if (y
> rect
->bottom
- lh
)
412 if (flags
& DT_CALCRECT
)
414 rect
->right
= rect
->left
+ max_width
;
417 return y
- rect
->top
;
420 /***********************************************************************
421 * DrawTextA (USER32.164)
423 INT WINAPI
DrawTextA( HDC hdc
, LPCSTR str
, INT count
, LPRECT rect
, UINT flags
)
429 if (count
== -1) count
= strlen(str
);
430 if (!count
) return 0;
431 wcount
= MultiByteToWideChar( CP_ACP
, 0, str
, count
, NULL
, 0 );
432 wstr
= HeapAlloc(GetProcessHeap(), 0, wcount
* sizeof(WCHAR
));
435 MultiByteToWideChar( CP_ACP
, 0, str
, count
, wstr
, wcount
);
436 ret
= DrawTextExW( hdc
, wstr
, wcount
, rect
, flags
, NULL
);
437 HeapFree(GetProcessHeap(), 0, wstr
);
442 /***********************************************************************
443 * DrawTextW (USER32.167)
445 INT WINAPI
DrawTextW( HDC hdc
, LPCWSTR str
, INT count
,
446 LPRECT rect
, UINT flags
)
448 return DrawTextExW(hdc
, str
, count
, rect
, flags
, NULL
);
451 /***********************************************************************
452 * DrawTextExA (USER32.165)
454 INT WINAPI
DrawTextExA( HDC hdc
, LPCSTR str
, INT count
,
455 LPRECT rect
, UINT flags
, LPDRAWTEXTPARAMS dtp
)
457 TRACE("(%d,'%s',%d,%p,0x%08x,%p)\n",hdc
,str
,count
,rect
,flags
,dtp
);
459 FIXME("Ignores params:%d,%d,%d,%d\n",dtp
->cbSize
,
460 dtp
->iTabLength
,dtp
->iLeftMargin
,dtp
->iRightMargin
);
462 return DrawTextA(hdc
,str
,count
,rect
,flags
);
465 /***********************************************************************
468 * FIXME: The call to 16-bit code only works because the wine GDI is a 16-bit
469 * heap and we can guarantee that the handles fit in an INT16. We have to
470 * rethink the strategy once the migration to NT handles is complete.
471 * We are going to get a lot of code-duplication once this migration is
475 static BOOL
TEXT_GrayString(HDC hdc
, HBRUSH hb
, GRAYSTRINGPROC fn
, LPARAM lp
, INT len
,
476 INT x
, INT y
, INT cx
, INT cy
, BOOL unicode
, BOOL _32bit
)
478 HBITMAP hbm
, hbmsave
;
481 HDC memdc
= CreateCompatibleDC(hdc
);
486 if(!hdc
) return FALSE
;
491 slen
= lstrlenW((LPCWSTR
)lp
);
493 slen
= strlen((LPCSTR
)lp
);
495 slen
= strlen((LPCSTR
)PTR_SEG_TO_LIN(lp
));
498 if((cx
== 0 || cy
== 0) && slen
!= -1)
502 GetTextExtentPoint32W(hdc
, (LPCWSTR
)lp
, slen
, &s
);
504 GetTextExtentPoint32A(hdc
, (LPCSTR
)lp
, slen
, &s
);
506 GetTextExtentPoint32A(hdc
, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
, &s
);
507 if(cx
== 0) cx
= s
.cx
;
508 if(cy
== 0) cy
= s
.cy
;
511 hbm
= CreateBitmap(cx
, cy
, 1, 1, NULL
);
512 hbmsave
= (HBITMAP
)SelectObject(memdc
, hbm
);
513 hbsave
= SelectObject( memdc
, GetStockObject(BLACK_BRUSH
) );
514 PatBlt( memdc
, 0, 0, cx
, cy
, PATCOPY
);
515 SelectObject( memdc
, hbsave
);
516 SetTextColor(memdc
, RGB(255, 255, 255));
517 SetBkColor(memdc
, RGB(0, 0, 0));
518 hfsave
= (HFONT
)SelectObject(memdc
, GetCurrentObject(hdc
, OBJ_FONT
));
523 retval
= fn(memdc
, lp
, slen
);
525 retval
= (BOOL
)((BOOL16
)((GRAYSTRINGPROC16
)fn
)((HDC16
)memdc
, lp
, (INT16
)slen
));
530 TextOutW(memdc
, 0, 0, (LPCWSTR
)lp
, slen
);
532 TextOutA(memdc
, 0, 0, (LPCSTR
)lp
, slen
);
534 TextOutA(memdc
, 0, 0, (LPCSTR
)PTR_SEG_TO_LIN(lp
), slen
);
537 SelectObject(memdc
, hfsave
);
540 * Windows doc says that the bitmap isn't grayed when len == -1 and
541 * the callback function returns FALSE. However, testing this on
542 * win95 showed otherwise...
544 #ifdef GRAYSTRING_USING_DOCUMENTED_BEHAVIOUR
545 if(retval
|| len
!= -1)
548 hbsave
= (HBRUSH
)SelectObject(memdc
, CACHE_GetPattern55AABrush());
549 PatBlt(memdc
, 0, 0, cx
, cy
, 0x000A0329);
550 SelectObject(memdc
, hbsave
);
553 if(hb
) hbsave
= (HBRUSH
)SelectObject(hdc
, hb
);
554 fg
= SetTextColor(hdc
, RGB(0, 0, 0));
555 bg
= SetBkColor(hdc
, RGB(255, 255, 255));
556 BitBlt(hdc
, x
, y
, cx
, cy
, memdc
, 0, 0, 0x00E20746);
557 SetTextColor(hdc
, fg
);
559 if(hb
) SelectObject(hdc
, hbsave
);
561 SelectObject(memdc
, hbmsave
);
568 /***********************************************************************
569 * GrayString16 (USER.185)
571 BOOL16 WINAPI
GrayString16( HDC16 hdc
, HBRUSH16 hbr
, GRAYSTRINGPROC16 gsprc
,
572 LPARAM lParam
, INT16 cch
, INT16 x
, INT16 y
,
575 return TEXT_GrayString(hdc
, hbr
, (GRAYSTRINGPROC
)gsprc
, lParam
, cch
,
576 x
, y
, cx
, cy
, FALSE
, FALSE
);
580 /***********************************************************************
581 * GrayStringA (USER32.315)
583 BOOL WINAPI
GrayStringA( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
584 LPARAM lParam
, INT cch
, INT x
, INT y
,
587 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
,
592 /***********************************************************************
593 * GrayStringW (USER32.316)
595 BOOL WINAPI
GrayStringW( HDC hdc
, HBRUSH hbr
, GRAYSTRINGPROC gsprc
,
596 LPARAM lParam
, INT cch
, INT x
, INT y
,
599 return TEXT_GrayString(hdc
, hbr
, gsprc
, lParam
, cch
, x
, y
, cx
, cy
,
603 /***********************************************************************
606 * Helper function for TabbedTextOut() and GetTabbedTextExtent().
607 * Note: this doesn't work too well for text-alignment modes other
608 * than TA_LEFT|TA_TOP. But we want bug-for-bug compatibility :-)
610 static LONG
TEXT_TabbedTextOut( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
,
611 INT count
, INT cTabStops
, const INT16
*lpTabPos16
,
612 const INT
*lpTabPos32
, INT nTabOrg
,
625 defWidth
= lpTabPos32
? *lpTabPos32
: *lpTabPos16
;
631 GetTextMetricsA( hdc
, &tm
);
632 defWidth
= 8 * tm
.tmAveCharWidth
;
637 for (i
= 0; i
< count
; i
++)
638 if (lpstr
[i
] == '\t') break;
639 GetTextExtentPointA( hdc
, lpstr
, i
, &extent
);
642 while ((cTabStops
> 0) &&
643 (nTabOrg
+ *lpTabPos32
<= x
+ extent
.cx
))
651 while ((cTabStops
> 0) &&
652 (nTabOrg
+ *lpTabPos16
<= x
+ extent
.cx
))
659 tabPos
= x
+ extent
.cx
;
660 else if (cTabStops
> 0)
661 tabPos
= nTabOrg
+ (lpTabPos32
? *lpTabPos32
: *lpTabPos16
);
663 tabPos
= nTabOrg
+ ((x
+ extent
.cx
- nTabOrg
) / defWidth
+ 1) * defWidth
;
670 r
.bottom
= y
+ extent
.cy
;
671 ExtTextOutA( hdc
, x
, y
,
672 GetBkMode(hdc
) == OPAQUE
? ETO_OPAQUE
: 0,
673 &r
, lpstr
, i
, NULL
);
679 return MAKELONG(tabPos
- start
, extent
.cy
);
683 /***********************************************************************
684 * TabbedTextOut16 (USER.196)
686 LONG WINAPI
TabbedTextOut16( HDC16 hdc
, INT16 x
, INT16 y
, LPCSTR lpstr
,
687 INT16 count
, INT16 cTabStops
,
688 const INT16
*lpTabPos
, INT16 nTabOrg
)
690 TRACE("%04x %d,%d '%.*s' %d\n", hdc
, x
, y
, count
, lpstr
, count
);
691 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
692 lpTabPos
, NULL
, nTabOrg
, TRUE
);
696 /***********************************************************************
697 * TabbedTextOutA (USER32.542)
699 LONG WINAPI
TabbedTextOutA( HDC hdc
, INT x
, INT y
, LPCSTR lpstr
, INT count
,
700 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
702 TRACE("%04x %d,%d '%.*s' %d\n", hdc
, x
, y
, count
, lpstr
, count
);
703 return TEXT_TabbedTextOut( hdc
, x
, y
, lpstr
, count
, cTabStops
,
704 NULL
, lpTabPos
, nTabOrg
, TRUE
);
708 /***********************************************************************
709 * TabbedTextOutW (USER32.543)
711 LONG WINAPI
TabbedTextOutW( HDC hdc
, INT x
, INT y
, LPCWSTR str
, INT count
,
712 INT cTabStops
, const INT
*lpTabPos
, INT nTabOrg
)
717 UINT codepage
= CP_ACP
; /* FIXME: get codepage of font charset */
719 acount
= WideCharToMultiByte(codepage
,0,str
,count
,NULL
,0,NULL
,NULL
);
720 p
= HeapAlloc( GetProcessHeap(), 0, acount
);
721 if(p
== NULL
) return 0; /* FIXME: is this the correct return on failure */
722 acount
= WideCharToMultiByte(codepage
,0,str
,count
,p
,acount
,NULL
,NULL
);
723 ret
= TabbedTextOutA( hdc
, x
, y
, p
, acount
, cTabStops
, lpTabPos
, nTabOrg
);
724 HeapFree( GetProcessHeap(), 0, p
);
729 /***********************************************************************
730 * GetTabbedTextExtent16 (USER.197)
732 DWORD WINAPI
GetTabbedTextExtent16( HDC16 hdc
, LPCSTR lpstr
, INT16 count
,
733 INT16 cTabStops
, const INT16
*lpTabPos
)
735 TRACE("%04x '%.*s' %d\n", hdc
, count
, lpstr
, count
);
736 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
737 lpTabPos
, NULL
, 0, FALSE
);
741 /***********************************************************************
742 * GetTabbedTextExtentA (USER32.293)
744 DWORD WINAPI
GetTabbedTextExtentA( HDC hdc
, LPCSTR lpstr
, INT count
,
745 INT cTabStops
, const INT
*lpTabPos
)
747 TRACE("%04x '%.*s' %d\n", hdc
, count
, lpstr
, count
);
748 return TEXT_TabbedTextOut( hdc
, 0, 0, lpstr
, count
, cTabStops
,
749 NULL
, lpTabPos
, 0, FALSE
);
753 /***********************************************************************
754 * GetTabbedTextExtentW (USER32.294)
756 DWORD WINAPI
GetTabbedTextExtentW( HDC hdc
, LPCWSTR lpstr
, INT count
,
757 INT cTabStops
, const INT
*lpTabPos
)
762 UINT codepage
= CP_ACP
; /* FIXME: get codepage of font charset */
764 acount
= WideCharToMultiByte(codepage
,0,lpstr
,count
,NULL
,0,NULL
,NULL
);
765 p
= HeapAlloc( GetProcessHeap(), 0, acount
);
766 if(p
== NULL
) return 0; /* FIXME: is this the correct failure value? */
767 acount
= WideCharToMultiByte(codepage
,0,lpstr
,count
,p
,acount
,NULL
,NULL
);
768 ret
= GetTabbedTextExtentA( hdc
, p
, acount
, cTabStops
, lpTabPos
);
769 HeapFree( GetProcessHeap(), 0, p
);