Release 941017
[wine/gsoc-2012-control.git] / objects / text.c
blob95b2eb7814e00857bba787c3af99c75a7f6a811e
1 /*
2 * text functions
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993, 1994";
9 #include <stdlib.h>
10 #include <X11/Xatom.h>
11 #include "windows.h"
12 #include "gdi.h"
13 #include "metafile.h"
14 #include "stddebug.h"
15 /* #define DEBUG_TEXT /* */
16 /* #undef DEBUG_TEXT /* */
17 #include "debug.h"
19 #define TAB 9
20 #define LF 10
21 #define CR 13
22 #define SPACE 32
23 #define PREFIX 38
25 #define SWAP_INT(a,b) { int t = a; a = b; b = t; }
27 static int tabstop = 8;
28 static int tabwidth;
29 static int spacewidth;
30 static int prefix_offset;
33 static char *TEXT_NextLine(HDC hdc, char *str, int *count, char *dest,
34 int *len, int width, WORD format)
36 /* Return next line of text from a string.
38 * hdc - handle to DC.
39 * str - string to parse into lines.
40 * count - length of str.
41 * dest - destination in which to return line.
42 * len - length of resultant line in dest in chars.
43 * width - maximum width of line in pixels.
44 * format - format type passed to DrawText.
46 * Returns pointer to next char in str after end of the line
47 * or NULL if end of str reached.
50 int i = 0, j = 0, k;
51 int plen = 0;
52 int numspaces;
53 SIZE size;
54 int lasttab = 0;
55 int wb_i = 0, wb_j = 0, wb_count;
57 while (*count)
59 switch (str[i])
61 case CR:
62 case LF:
63 if (!(format & DT_SINGLELINE))
65 i++;
66 if (str[i] == CR || str[i] == LF)
67 i++;
68 *len = j;
69 return (&str[i]);
71 dest[j++] = str[i++];
72 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
73 (format & DT_WORDBREAK))
75 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
76 return NULL;
77 plen += size.cx;
79 break;
81 case PREFIX:
82 if (!(format & DT_NOPREFIX))
84 prefix_offset = j;
85 i++;
87 else
89 dest[j++] = str[i++];
90 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
92 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
93 return NULL;
94 plen += size.cx;
97 break;
99 case TAB:
100 if (format & DT_EXPANDTABS)
102 wb_i = ++i;
103 wb_j = j;
104 wb_count = *count;
106 if (!GetTextExtentPoint(hdc, &dest[lasttab], j - lasttab,
107 &size))
108 return NULL;
110 numspaces = (tabwidth - size.cx) / spacewidth;
111 for (k = 0; k < numspaces; k++)
112 dest[j++] = SPACE;
113 plen += tabwidth - size.cx;
114 lasttab = wb_j + numspaces;
116 else
118 dest[j++] = str[i++];
119 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
120 (format & DT_WORDBREAK))
122 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
123 return NULL;
124 plen += size.cx;
127 break;
129 case SPACE:
130 dest[j++] = str[i++];
131 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
132 (format & DT_WORDBREAK))
134 wb_i = i;
135 wb_j = j - 1;
136 wb_count = *count;
137 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
138 return NULL;
139 plen += size.cx;
141 break;
143 default:
144 dest[j++] = str[i++];
145 if (!(format & DT_NOCLIP) || !(format & DT_NOPREFIX) ||
146 (format & DT_WORDBREAK))
148 if (!GetTextExtentPoint(hdc, &dest[j-1], 1, &size))
149 return NULL;
150 plen += size.cx;
154 (*count)--;
155 if (!(format & DT_NOCLIP) || (format & DT_WORDBREAK))
157 if (plen > width)
159 if (format & DT_WORDBREAK)
161 *len = wb_j;
162 *count = wb_count - 1;
163 return (&str[wb_i]);
165 else
167 *len = j;
168 return (&str[i]);
174 *len = j;
175 return NULL;
179 /***********************************************************************
180 * DrawText (USER.85)
182 int DrawText( HDC hdc, LPSTR str, int count, LPRECT rect, WORD flags )
184 SIZE size;
185 char *strPtr;
186 static char line[1024];
187 int len, lh, prefix_x, prefix_end;
188 TEXTMETRIC tm;
189 int x = rect->left, y = rect->top;
190 int width = rect->right - rect->left;
192 dprintf_text(stddeb,"DrawText: '%s', %d , [(%d,%d),(%d,%d)]\n", str, count,
193 rect->left, rect->top, rect->right, rect->bottom);
195 if (count == -1) count = strlen(str);
196 strPtr = str;
198 GetTextMetrics(hdc, &tm);
199 if (flags & DT_EXTERNALLEADING)
200 lh = tm.tmHeight + tm.tmExternalLeading;
201 else
202 lh = tm.tmHeight;
204 if (flags & DT_TABSTOP)
205 tabstop = flags >> 8;
207 if (flags & DT_EXPANDTABS)
209 GetTextExtentPoint(hdc, " ", 1, &size);
210 spacewidth = size.cx;
211 GetTextExtentPoint(hdc, "o", 1, &size);
212 tabwidth = size.cx * tabstop;
217 prefix_offset = -1;
218 strPtr = TEXT_NextLine(hdc, strPtr, &count, line, &len, width, flags);
220 if (prefix_offset != -1)
222 GetTextExtentPoint(hdc, line, prefix_offset, &size);
223 prefix_x = size.cx;
224 GetTextExtentPoint(hdc, line, prefix_offset + 1, &size);
225 prefix_end = size.cx - 1;
228 if (!GetTextExtentPoint(hdc, line, len, &size)) return 0;
229 if (flags & DT_CENTER) x = (rect->left + rect->right -
230 size.cx) / 2;
231 else if (flags & DT_RIGHT) x = rect->right - size.cx;
233 if (flags & DT_SINGLELINE)
235 if (flags & DT_VCENTER) y = rect->top +
236 (rect->bottom - rect->top) / 2 - size.cy / 2;
237 else if (flags & DT_BOTTOM) y = rect->bottom - size.cy;
239 if (!(flags & DT_CALCRECT))
240 if (!ExtTextOut( hdc, x, y, (flags & DT_NOCLIP) ? 0 : ETO_CLIPPED,
241 rect, line, len, NULL )) return 0;
243 if (prefix_offset != -1)
245 HPEN hpen = CreatePen( PS_SOLID, 1, GetTextColor(hdc) );
246 HPEN oldPen = SelectObject( hdc, hpen );
247 MoveTo(hdc, x + prefix_x, y + tm.tmAscent + 1 );
248 LineTo(hdc, x + prefix_end, y + tm.tmAscent + 1 );
249 SelectObject( hdc, oldPen );
250 DeleteObject( hpen );
253 if (strPtr)
255 y += lh;
256 if (!(flags & DT_NOCLIP))
258 if (y > rect->bottom - lh)
259 break;
263 while (strPtr);
264 if (flags & DT_CALCRECT) rect->bottom = y;
265 return 1;
269 /***********************************************************************
270 * ExtTextOut (GDI.351)
272 BOOL ExtTextOut( HDC hdc, short x, short y, WORD flags, LPRECT lprect,
273 LPSTR str, WORD count, LPINT lpDx )
275 int dir, ascent, descent, i;
276 XCharStruct info;
277 XFontStruct *font;
278 RECT rect;
280 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
281 if (!dc)
283 dc = (DC *)GDI_GetObjPtr( hdc, METAFILE_DC_MAGIC );
284 if (!dc) return FALSE;
285 MF_TextOut( dc, x, y, str, count );
286 return TRUE;
289 if (!DC_SetupGCForText( dc )) return TRUE;
290 font = dc->u.x.font.fstruct;
292 dprintf_text(stddeb,"ExtTextOut: %d,%d '%s', %d flags=%d rect=%d,%d,%d,%d\n",
293 x, y, str, count, flags,
294 lprect->left, lprect->top, lprect->right, lprect->bottom );
296 /* Setup coordinates */
298 if (dc->w.textAlign & TA_UPDATECP)
300 x = dc->w.CursPosX;
301 y = dc->w.CursPosY;
303 x = XLPTODP( dc, x );
304 y = YLPTODP( dc, y );
305 if (flags & (ETO_OPAQUE | ETO_CLIPPED)) /* There's a rectangle */
307 rect.left = XLPTODP( dc, lprect->left );
308 rect.right = XLPTODP( dc, lprect->right );
309 rect.top = YLPTODP( dc, lprect->top );
310 rect.bottom = YLPTODP( dc, lprect->bottom );
311 if (rect.right < rect.left) SWAP_INT( rect.left, rect.right );
312 if (rect.bottom < rect.top) SWAP_INT( rect.top, rect.bottom );
315 /* Draw the rectangle */
317 if (flags & ETO_OPAQUE)
319 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
320 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
321 dc->w.DCOrgX + rect.left, dc->w.DCOrgY + rect.top,
322 rect.right-rect.left, rect.bottom-rect.top );
324 if (!count) return TRUE; /* Nothing more to do */
326 /* Compute text starting position */
328 XTextExtents( font, str, count, &dir, &ascent, &descent, &info );
329 info.width += count*dc->w.charExtra + dc->w.breakExtra*dc->w.breakCount;
330 if (lpDx) for (i = 0; i < count; i++) info.width += lpDx[i];
332 switch( dc->w.textAlign & (TA_LEFT | TA_RIGHT | TA_CENTER) )
334 case TA_LEFT:
335 if (dc->w.textAlign & TA_UPDATECP)
336 dc->w.CursPosX = XDPTOLP( dc, x + info.width );
337 break;
338 case TA_RIGHT:
339 x -= info.width;
340 if (dc->w.textAlign & TA_UPDATECP) dc->w.CursPosX = XDPTOLP( dc, x );
341 break;
342 case TA_CENTER:
343 x -= info.width / 2;
344 break;
346 switch( dc->w.textAlign & (TA_TOP | TA_BOTTOM | TA_BASELINE) )
348 case TA_TOP:
349 y += font->ascent;
350 break;
351 case TA_BOTTOM:
352 y -= font->descent;
353 break;
354 case TA_BASELINE:
355 break;
358 /* Set the clip region */
360 if (flags & ETO_CLIPPED)
362 SaveVisRgn( hdc );
363 IntersectVisRect( hdc, rect.left, rect.top, rect.right, rect.bottom );
366 /* Draw the text background if necessary */
368 if (dc->w.backgroundMode != TRANSPARENT)
370 /* If rectangle is opaque and clipped, do nothing */
371 if (!(flags & ETO_CLIPPED) || !(flags & ETO_OPAQUE))
373 /* Only draw if rectangle is not opaque or if some */
374 /* text is outside the rectangle */
375 if (!(flags & ETO_OPAQUE) ||
376 (x < rect.left) ||
377 (x + info.width >= rect.right) ||
378 (y-font->ascent < rect.top) ||
379 (y+font->descent >= rect.bottom))
381 XSetForeground( display, dc->u.x.gc, dc->w.backgroundPixel );
382 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
383 dc->w.DCOrgX + x,
384 dc->w.DCOrgY + y - font->ascent,
385 info.width,
386 font->ascent + font->descent );
391 /* Draw the text */
393 XSetForeground( display, dc->u.x.gc, dc->w.textPixel );
394 if (!dc->w.charExtra && !dc->w.breakExtra && !lpDx)
396 XDrawString( display, dc->u.x.drawable, dc->u.x.gc,
397 dc->w.DCOrgX + x, dc->w.DCOrgY + y, str, count );
399 else /* Now the fun begins... */
401 XTextItem *items, *pitem;
403 items = malloc( count * sizeof(XTextItem) );
404 for (i = 0, pitem = items; i < count; i++, pitem++)
406 pitem->chars = str + i;
407 pitem->nchars = 1;
408 pitem->font = None;
409 if (i == 0)
411 pitem->delta = 0;
412 continue; /* First iteration -> no delta */
414 pitem->delta = dc->w.charExtra;
415 if (str[i] == dc->u.x.font.metrics.tmBreakChar)
416 pitem->delta += dc->w.breakExtra;
417 if (lpDx)
419 INT width;
420 GetCharWidth( hdc, str[i], str[i], &width );
421 pitem->delta += lpDx[i-1] - width;
424 XDrawText( display, dc->u.x.drawable, dc->u.x.gc,
425 dc->w.DCOrgX + x, dc->w.DCOrgY + y, items, count );
426 free( items );
429 /* Draw underline and strike-out if needed */
431 if (dc->u.x.font.metrics.tmUnderlined)
433 long linePos, lineWidth;
434 if (!XGetFontProperty( font, XA_UNDERLINE_POSITION, &linePos ))
435 linePos = font->descent-1;
436 if (!XGetFontProperty( font, XA_UNDERLINE_THICKNESS, &lineWidth ))
437 lineWidth = 0;
438 else if (lineWidth == 1) lineWidth = 0;
439 XSetLineAttributes( display, dc->u.x.gc, lineWidth,
440 LineSolid, CapRound, JoinBevel );
441 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
442 dc->w.DCOrgX + x, dc->w.DCOrgY + y + linePos,
443 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y + linePos );
445 if (dc->u.x.font.metrics.tmStruckOut)
447 long lineAscent, lineDescent;
448 if (!XGetFontProperty( font, XA_STRIKEOUT_ASCENT, &lineAscent ))
449 lineAscent = font->ascent / 3;
450 if (!XGetFontProperty( font, XA_STRIKEOUT_DESCENT, &lineDescent ))
451 lineDescent = -lineAscent;
452 XSetLineAttributes( display, dc->u.x.gc, lineAscent + lineDescent,
453 LineSolid, CapRound, JoinBevel );
454 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
455 dc->w.DCOrgX + x, dc->w.DCOrgY + y - lineAscent,
456 dc->w.DCOrgX + x + info.width, dc->w.DCOrgY + y - lineAscent );
458 if (flags & ETO_CLIPPED) RestoreVisRgn( hdc );
459 return TRUE;
463 /***********************************************************************
464 * TextOut (GDI.33)
466 BOOL TextOut( HDC hdc, short x, short y, LPSTR str, short count )
468 return ExtTextOut( hdc, x, y, 0, NULL, str, count, NULL );
472 /***********************************************************************
473 * GrayString (USER.185)
475 BOOL GrayString(HDC hdc, HBRUSH hbr, FARPROC gsprc, LPARAM lParam,
476 INT cch, INT x, INT y, INT cx, INT cy)
478 int s, current_color;
480 if (gsprc) {
481 return CallGrayStringProc(gsprc, hdc, lParam,
482 cch ? cch : lstrlen((LPCSTR) lParam) );
483 } else {
484 current_color = GetTextColor(hdc);
485 SetTextColor(hdc, GetSysColor(COLOR_GRAYTEXT) );
486 s = TextOut(hdc, x, y, (LPSTR) lParam,
487 cch ? cch : lstrlen((LPCSTR) lParam) );
488 SetTextColor(hdc, current_color);
490 return s;
495 /***********************************************************************
496 * TabbedTextOut [USER.196]
498 LONG TabbedTextOut(HDC hDC, short x, short y, LPSTR lpStr, short nCount,
499 short nTabCount, LPINT lpTabPos, short nTabOrg)
501 WORD width, height;
502 dprintf_text(stdnimp,"EMPTY STUB !!! TabbedTextOut(); ! call TextOut() for now !\n");
503 height = HIWORD(GetTextExtent(hDC, lpStr, nCount));
504 width = LOWORD(GetTextExtent(hDC, lpStr, nCount));
505 TextOut(hDC, x, y, lpStr, nCount);
506 return MAKELONG(width, height);
510 /***********************************************************************
511 * GetTabbedTextExtent [USER.197]
513 DWORD GetTabbedTextExtent(HDC hDC, LPSTR lpString, int nCount,
514 int nTabPositions, LPINT lpnTabStopPositions)
516 dprintf_text(stdnimp,"EMPTY STUB !!! GetTabbedTextExtent(); !\n");
518 return (18 << 16) | (nCount * 18);