Add support for more than one sound card.
[wine] / dlls / riched20 / writer.c
1 /*
2  * RichEdit - RTF writer module
3  *
4  * Copyright 2005 by Phil Krylov
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "editor.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
24
25
26 static BOOL
27 ME_StreamOutRTFText(ME_TextEditor *editor, WCHAR *text, LONG nChars);
28
29
30 static void
31 ME_StreamOutInit(ME_TextEditor *editor, EDITSTREAM *stream)
32 {
33   editor->pStream = ALLOC_OBJ(ME_OutStream);
34   editor->pStream->stream = stream;
35   editor->pStream->pos = 0;
36   editor->pStream->written = 0;
37   editor->pStream->nFontTblLen = 0;
38   editor->pStream->nColorTblLen = 1;
39 }
40
41
42 static BOOL
43 ME_StreamOutFlush(ME_TextEditor *editor)
44 {
45   LONG nStart = 0;
46   LONG nWritten = 0;
47   EDITSTREAM *stream = editor->pStream->stream;
48
49   do {
50     stream->dwError = stream->pfnCallback(stream->dwCookie, editor->pStream->buffer + nStart,
51                                           editor->pStream->pos - nStart, &nWritten);
52     if (nWritten == 0 || stream->dwError)
53       return FALSE;
54     editor->pStream->written += nWritten;
55     nStart += nWritten;
56   } while (nStart < editor->pStream->pos);
57   editor->pStream->pos = 0;
58   return TRUE;
59 }
60
61
62 static LONG
63 ME_StreamOutFree(ME_TextEditor *editor)
64 {
65   LONG written = editor->pStream->written;
66
67   FREE_OBJ(editor->pStream);
68   editor->pStream = NULL;
69   return written;
70 }
71
72
73 static BOOL
74 ME_StreamOutMove(ME_TextEditor *editor, BYTE *buffer, int len)
75 {
76   ME_OutStream *pStream = editor->pStream;
77   
78   while (len) {
79     int space = STREAMOUT_BUFFER_SIZE - pStream->pos;
80     int fit = min(space, len);
81
82     TRACE("%u:%u:%.*s\n", pStream->pos, fit, fit, buffer);
83     memmove(pStream->buffer + pStream->pos, buffer, fit);
84     len -= fit;
85     buffer += fit;
86     pStream->pos += fit;
87     if (pStream->pos == STREAMOUT_BUFFER_SIZE) {
88       if (!ME_StreamOutFlush(editor))
89         return FALSE;
90     }
91   }
92   return TRUE;
93 }
94
95
96 static BOOL
97 ME_StreamOutPrint(ME_TextEditor *editor, char *format, ...)
98 {
99   char string[STREAMOUT_BUFFER_SIZE]; /* This is going to be enough */
100   int len;
101   va_list valist;
102
103   va_start(valist, format);
104   len = vsnprintf(string, sizeof(string), format, valist);
105   va_end(valist);
106   
107   return ME_StreamOutMove(editor, string, len);
108 }
109
110
111 static BOOL
112 ME_StreamOutRTFHeader(ME_TextEditor *editor, int dwFormat)
113 {
114   char *cCharSet = NULL;
115   UINT nCodePage;
116   LANGID language;
117   BOOL success;
118   
119   if (dwFormat & SF_USECODEPAGE) {
120     CPINFOEXW info;
121     
122     switch (HIWORD(dwFormat)) {
123       case CP_ACP:
124         cCharSet = "ansi";
125         nCodePage = GetACP();
126         break;
127       case CP_OEMCP:
128         nCodePage = GetOEMCP();
129         if (nCodePage == 437)
130           cCharSet = "pc";
131         else if (nCodePage == 850)
132           cCharSet = "pca";
133         else
134           cCharSet = "ansi";
135         break;
136       case CP_UTF8:
137         nCodePage = CP_UTF8;
138         break;
139       default:
140         if (HIWORD(dwFormat) == CP_MACCP) {
141           cCharSet = "mac";
142           nCodePage = 10000; /* MacRoman */
143         } else {
144           cCharSet = "ansi";
145           nCodePage = 1252; /* Latin-1 */
146         }
147         if (GetCPInfoExW(HIWORD(dwFormat), 0, &info))
148           nCodePage = info.CodePage;
149     }
150   } else {
151     cCharSet = "ansi";
152     nCodePage = GetACP();
153   }
154   if (nCodePage == CP_UTF8)
155     success = ME_StreamOutPrint(editor, "{\\urtf");
156   else
157     success = ME_StreamOutPrint(editor, "{\\rtf1\\%s\\ansicpg%u\\uc1", cCharSet, nCodePage);
158
159   if (!success)
160     return FALSE;
161
162   editor->pStream->nCodePage = nCodePage;
163   
164   /* FIXME: This should be a document property */
165   /* TODO: handle SFF_PLAINRTF */
166   language = GetUserDefaultLangID(); 
167   if (!ME_StreamOutPrint(editor, "\\deff0\\deflang%u\\deflangfe%u", language, language))
168     return FALSE;
169
170   /* FIXME: This should be a document property */
171   editor->pStream->nDefaultFont = 0;
172
173   return TRUE;
174 }
175
176
177 static BOOL
178 ME_StreamOutRTFFontAndColorTbl(ME_TextEditor *editor, ME_DisplayItem *pFirstRun, ME_DisplayItem *pLastRun)
179 {
180   ME_DisplayItem *item = pFirstRun;
181   ME_FontTableItem *table = editor->pStream->fonttbl;
182   int i;
183   
184   do {
185     CHARFORMAT2W *fmt = &item->member.run.style->fmt;
186     COLORREF crColor;
187
188     if (fmt->dwMask & CFM_FACE) {
189       WCHAR *face = fmt->szFaceName;
190       BYTE bCharSet = fmt->bCharSet;
191   
192       for (i = 0; i < editor->pStream->nFontTblLen; i++)
193         if (table[i].bCharSet == bCharSet
194             && (table[i].szFaceName == face || !lstrcmpW(table[i].szFaceName, face)))
195           break;
196       if (i == editor->pStream->nFontTblLen) {
197         table[i].bCharSet = bCharSet;
198         table[i].szFaceName = face;
199         editor->pStream->nFontTblLen++;
200       }
201     }
202     
203     if (fmt->dwMask & CFM_COLOR && !(fmt->dwEffects & CFE_AUTOCOLOR)) {
204       crColor = fmt->crTextColor;
205       for (i = 1; i < editor->pStream->nColorTblLen; i++)
206         if (editor->pStream->colortbl[i] == crColor)
207           break;
208       if (i == editor->pStream->nColorTblLen) {
209         editor->pStream->colortbl[i] = crColor;
210         editor->pStream->nColorTblLen++;
211       }
212     }
213     if (fmt->dwMask & CFM_BACKCOLOR && !(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
214       crColor = fmt->crBackColor;
215       for (i = 1; i < editor->pStream->nColorTblLen; i++)
216         if (editor->pStream->colortbl[i] == crColor)
217           break;
218       if (i == editor->pStream->nColorTblLen) {
219         editor->pStream->colortbl[i] = crColor;
220         editor->pStream->nColorTblLen++;
221       }
222     }
223
224     if (item == pLastRun)
225       break;
226     item = ME_FindItemFwd(item, diRun);
227   } while (item);
228         
229   if (!ME_StreamOutPrint(editor, "{\\fonttbl"))
230     return FALSE;
231   
232   for (i = 0; i < editor->pStream->nFontTblLen; i++) {
233     if (table[i].bCharSet) {
234       if (!ME_StreamOutPrint(editor, "{\\f%u\\fcharset%u ", i, table[i].bCharSet))
235         return FALSE;
236     } else {
237       if (!ME_StreamOutPrint(editor, "{\\f%u ", i))
238         return FALSE;
239     }
240     if (!ME_StreamOutRTFText(editor, table[i].szFaceName, -1))
241       return FALSE;
242     if (!ME_StreamOutPrint(editor, ";}\r\n"))
243       return FALSE;
244   }
245   if (!ME_StreamOutPrint(editor, "}"))
246     return FALSE;
247
248   /* Output colors table if not empty */
249   if (editor->pStream->nColorTblLen > 1) {
250     if (!ME_StreamOutPrint(editor, "{\\colortbl;"))
251       return FALSE;
252     for (i = 1; i < editor->pStream->nColorTblLen; i++) {
253       if (!ME_StreamOutPrint(editor, "\\red%u\\green%u\\blue%u;",
254                              editor->pStream->colortbl[i] & 0xFF,
255                              (editor->pStream->colortbl[i] >> 8) & 0xFF,
256                              (editor->pStream->colortbl[i] >> 16) & 0xFF))
257         return FALSE;
258     }
259     if (!ME_StreamOutPrint(editor, "}"))
260       return FALSE;
261   }
262
263   return TRUE;
264 }
265
266
267 static BOOL
268 ME_StreamOutRTFParaProps(ME_TextEditor *editor, ME_DisplayItem *para)
269 {
270   PARAFORMAT2 *fmt = para->member.para.pFmt;
271   char props[STREAMOUT_BUFFER_SIZE] = "";
272   int i;
273
274   /* TODO: Don't emit anything if the last PARAFORMAT2 is inherited */
275   if (!ME_StreamOutPrint(editor, "\\pard"))
276     return FALSE;
277   
278   /* TODO: PFM_BORDER. M$ does not emit any keywords for these properties, and
279    * when streaming border keywords in, PFM_BORDER is set, but wBorder field is
280    * set very different from the documentation.
281    * (Tested with RichEdit 5.50.25.0601) */
282   
283   if (fmt->dwMask & PFM_ALIGNMENT) {
284     switch (fmt->wAlignment) {
285       case PFA_LEFT:
286         /* Default alignment: not emitted */
287         break;
288       case PFA_RIGHT:
289         strcat(props, "\\qr");
290         break;
291       case PFA_CENTER:
292         strcat(props, "\\qc");
293         break;
294       case PFA_JUSTIFY:
295         strcat(props, "\\qj");
296         break;
297     }
298   }
299   
300   if (fmt->dwMask & PFM_LINESPACING) {
301     /* FIXME: MSDN says that the bLineSpacingRule field is controlled by the
302      * PFM_SPACEAFTER flag. Is that true? I don't believe so. */
303     switch (fmt->bLineSpacingRule) {
304       case 0: /* Single spacing */
305         strcat(props, "\\sl-240\\slmult1");
306         break;
307       case 1: /* 1.5 spacing */
308         strcat(props, "\\sl-360\\slmult1");
309         break;
310       case 2: /* Double spacing */
311         strcat(props, "\\sl-480\\slmult1");
312         break;
313       case 3:
314         sprintf(props + strlen(props), "\\sl%ld\\slmult0", fmt->dyLineSpacing);
315         break;
316       case 4:
317         sprintf(props + strlen(props), "\\sl-%ld\\slmult0", fmt->dyLineSpacing);
318         break;
319       case 5:
320         sprintf(props + strlen(props), "\\sl-%ld\\slmult1", fmt->dyLineSpacing * 240 / 20);
321         break;
322     }
323   }
324
325   if (fmt->dwMask & PFM_DONOTHYPHEN && fmt->wEffects & PFE_DONOTHYPHEN)
326     strcat(props, "\\hyph0");
327   if (fmt->dwMask & PFM_KEEP && fmt->wEffects & PFE_KEEP)
328     strcat(props, "\\keep");
329   if (fmt->dwMask & PFM_KEEPNEXT && fmt->wEffects & PFE_KEEPNEXT)
330     strcat(props, "\\keepn");
331   if (fmt->dwMask & PFM_NOLINENUMBER && fmt->wEffects & PFE_NOLINENUMBER)
332     strcat(props, "\\noline");
333   if (fmt->dwMask & PFM_NOWIDOWCONTROL && fmt->wEffects & PFE_NOWIDOWCONTROL)
334     strcat(props, "\\nowidctlpar");
335   if (fmt->dwMask & PFM_PAGEBREAKBEFORE && fmt->wEffects & PFE_PAGEBREAKBEFORE)
336     strcat(props, "\\pagebb");
337   if (fmt->dwMask & PFM_RTLPARA && fmt->wEffects & PFE_RTLPARA)
338     strcat(props, "\\rtlpar");
339   if (fmt->dwMask & PFM_SIDEBYSIDE && fmt->wEffects & PFE_SIDEBYSIDE)
340     strcat(props, "\\sbys");
341   if (fmt->dwMask & PFM_TABLE && fmt->dwMask & PFE_TABLE)
342     strcat(props, "\\intbl");
343   
344   if (fmt->dwMask & PFM_OFFSET)
345     sprintf(props + strlen(props), "\\li%ld", fmt->dxOffset);
346   if (fmt->dwMask & PFM_OFFSETINDENT || fmt->dwMask & PFM_STARTINDENT)
347     sprintf(props + strlen(props), "\\fi%ld", fmt->dxStartIndent);
348   if (fmt->dwMask & PFM_RIGHTINDENT)
349     sprintf(props + strlen(props), "\\ri%ld", fmt->dxRightIndent);
350   if (fmt->dwMask & PFM_SPACEAFTER)
351     sprintf(props + strlen(props), "\\sa%ld", fmt->dySpaceAfter);
352   if (fmt->dwMask & PFM_SPACEBEFORE)
353     sprintf(props + strlen(props), "\\sb%ld", fmt->dySpaceBefore);
354   if (fmt->dwMask & PFM_STYLE)
355     sprintf(props + strlen(props), "\\s%d", fmt->sStyle);
356
357   if (fmt->dwMask & PFM_TABSTOPS) {
358     static const char *leader[6] = { "", "\\tldot", "\\tlhyph", "\\tlul", "\\tlth", "\\tleq" };
359     
360     for (i = 0; i < fmt->cTabCount; i++) {
361       switch ((fmt->rgxTabs[i] >> 24) & 0xF) {
362         case 1:
363           strcat(props, "\\tqc");
364           break;
365         case 2:
366           strcat(props, "\\tqr");
367           break;
368         case 3:
369           strcat(props, "\\tqdec");
370           break;
371         case 4:
372           /* Word bar tab (vertical bar). Handled below */
373           break;
374       }
375       if (fmt->rgxTabs[i] >> 28 <= 5)
376         strcat(props, leader[fmt->rgxTabs[i] >> 28]);
377     }
378   }
379     
380   
381   if (fmt->dwMask & PFM_SHADING) {
382     static const char *style[16] = { "", "\\bgdkhoriz", "\\bgdkvert", "\\bgdkfdiag",
383                                      "\\bgdkbdiag", "\\bgdkcross", "\\bgdkdcross",
384                                      "\\bghoriz", "\\bgvert", "\\bgfdiag",
385                                      "\\bgbdiag", "\\bgcross", "\\bgdcross",
386                                      "", "", "" };
387     if (fmt->wShadingWeight)
388       sprintf(props + strlen(props), "\\shading%d", fmt->wShadingWeight);
389     if (fmt->wShadingStyle & 0xF)
390       strcat(props, style[fmt->wShadingStyle & 0xF]);
391     sprintf(props + strlen(props), "\\cfpat%d\\cbpat%d",
392             (fmt->wShadingStyle >> 4) & 0xF, (fmt->wShadingStyle >> 8) & 0xF);
393   }
394   
395   if (*props && !ME_StreamOutPrint(editor, props))
396     return FALSE;
397
398   return TRUE;
399 }
400
401
402 static BOOL
403 ME_StreamOutRTFCharProps(ME_TextEditor *editor, CHARFORMAT2W *fmt)
404 {
405   char props[STREAMOUT_BUFFER_SIZE] = "";
406   int i;
407
408   if (fmt->dwMask & CFM_ALLCAPS && fmt->dwEffects & CFE_ALLCAPS)
409     strcat(props, "\\caps");
410   if (fmt->dwMask & CFM_ANIMATION)
411     sprintf(props + strlen(props), "\\animtext%u", fmt->bAnimation);
412   if (fmt->dwMask & CFM_BACKCOLOR) {
413     if (!(fmt->dwEffects & CFE_AUTOBACKCOLOR)) {
414       for (i = 1; i < editor->pStream->nColorTblLen; i++)
415         if (editor->pStream->colortbl[i] == fmt->crBackColor) {
416           sprintf(props + strlen(props), "\\cb%u", i);
417           break;
418         }
419     }
420   }
421   if (fmt->dwMask & CFM_BOLD && fmt->dwEffects & CFE_BOLD)
422     strcat(props, "\\b");
423   if (fmt->dwMask & CFM_COLOR) {
424     if (!(fmt->dwEffects & CFE_AUTOCOLOR)) {
425       for (i = 1; i < editor->pStream->nColorTblLen; i++)
426         if (editor->pStream->colortbl[i] == fmt->crTextColor) {
427           sprintf(props + strlen(props), "\\cf%u", i);
428           break;
429         }
430     }
431   }
432   /* TODO: CFM_DISABLED */
433   if (fmt->dwMask & CFM_EMBOSS && fmt->dwEffects & CFE_EMBOSS)
434     strcat(props, "\\embo");
435   if (fmt->dwMask & CFM_HIDDEN && fmt->dwEffects & CFE_HIDDEN)
436     strcat(props, "\\v");
437   if (fmt->dwMask & CFM_IMPRINT && fmt->dwEffects & CFE_IMPRINT)
438     strcat(props, "\\impr");
439   if (fmt->dwMask & CFM_ITALIC && fmt->dwEffects & CFE_ITALIC)
440     strcat(props, "\\i");
441   if (fmt->dwMask & CFM_KERNING)
442     sprintf(props + strlen(props), "\\kerning%u", fmt->wKerning);
443   if (fmt->dwMask & CFM_LCID) {
444     /* TODO: handle SFF_PLAINRTF */
445     if (LOWORD(fmt->lcid) == 1024)
446       strcat(props, "\\noproof\\lang1024\\langnp1024\\langfe1024\\langfenp1024");
447     else
448       sprintf(props + strlen(props), "\\lang%u", LOWORD(fmt->lcid));
449   }
450   /* CFM_LINK is not streamed out by M$ */
451   if (fmt->dwMask & CFM_OFFSET) {
452     if (fmt->yOffset >= 0)
453       sprintf(props + strlen(props), "\\up%ld", fmt->yOffset);
454     else
455       sprintf(props + strlen(props), "\\dn%ld", -fmt->yOffset);
456   }
457   if (fmt->dwMask & CFM_OUTLINE && fmt->dwEffects & CFE_OUTLINE)
458     strcat(props, "\\outl");
459   if (fmt->dwMask & CFM_PROTECTED && fmt->dwEffects & CFE_PROTECTED)
460     strcat(props, "\\protect");
461   /* TODO: CFM_REVISED CFM_REVAUTHOR - probably using rsidtbl? */
462   if (fmt->dwMask & CFM_SHADOW && fmt->dwEffects & CFE_SHADOW)
463     strcat(props, "\\shad");
464   if (fmt->dwMask & CFM_SIZE)
465     sprintf(props + strlen(props), "\\fs%ld", fmt->yHeight / 10);
466   if (fmt->dwMask & CFM_SMALLCAPS && fmt->dwEffects & CFE_SMALLCAPS)
467     strcat(props, "\\scaps");
468   if (fmt->dwMask & CFM_SPACING)
469     sprintf(props + strlen(props), "\\expnd%u\\expndtw%u", fmt->sSpacing / 5, fmt->sSpacing);
470   if (fmt->dwMask & CFM_STRIKEOUT && fmt->dwEffects & CFE_STRIKEOUT)
471     strcat(props, "\\strike");
472   if (fmt->dwMask & CFM_STYLE) {
473     sprintf(props + strlen(props), "\\cs%u", fmt->sStyle);
474     /* TODO: emit style contents here */
475   }
476   if (fmt->dwMask & (CFM_SUBSCRIPT | CFM_SUPERSCRIPT)) {
477     if (fmt->dwEffects & CFE_SUBSCRIPT)
478       strcat(props, "\\sub");
479     else if (fmt->dwEffects & CFE_SUPERSCRIPT)
480       strcat(props, "\\super");
481   }
482   if (fmt->dwMask & CFM_UNDERLINE || fmt->dwMask & CFM_UNDERLINETYPE) {
483     if (fmt->dwMask & CFM_UNDERLINETYPE)
484       switch (fmt->bUnderlineType) {
485         case CFU_CF1UNDERLINE:
486         case CFU_UNDERLINE:
487           strcat(props, "\\ul");
488           break;
489         case CFU_UNDERLINEDOTTED:
490           strcat(props, "\\uld");
491           break;
492         case CFU_UNDERLINEDOUBLE:
493           strcat(props, "\\uldb");
494           break;
495         case CFU_UNDERLINEWORD:
496           strcat(props, "\\ulw");
497           break;
498         case CFU_UNDERLINENONE:
499         default:
500           strcat(props, "\\ul0");
501           break;
502       }
503     else if (fmt->dwEffects & CFE_UNDERLINE)
504       strcat(props, "\\ul");
505   }
506   /* FIXME: How to emit CFM_WEIGHT? */
507   
508   if (fmt->dwMask & CFM_FACE || fmt->dwMask & CFM_CHARSET) {
509     WCHAR *szFaceName;
510     
511     if (fmt->dwMask & CFM_FACE)
512       szFaceName = fmt->szFaceName;
513     else
514       szFaceName = editor->pStream->fonttbl[0].szFaceName;
515     for (i = 0; i < editor->pStream->nFontTblLen; i++) {
516       if (szFaceName == editor->pStream->fonttbl[i].szFaceName
517           || !lstrcmpW(szFaceName, editor->pStream->fonttbl[i].szFaceName))
518         if (!(fmt->dwMask & CFM_CHARSET)
519             || fmt->bCharSet == editor->pStream->fonttbl[i].bCharSet)
520           break;
521     }
522     if (i < editor->pStream->nFontTblLen && i != editor->pStream->nDefaultFont)
523       sprintf(props + strlen(props), "\\f%u", i);
524   }
525   if (*props)
526     strcat(props, " ");
527   if (!ME_StreamOutPrint(editor, props))
528     return FALSE;
529   return TRUE;
530 }
531
532
533 static BOOL
534 ME_StreamOutRTFText(ME_TextEditor *editor, WCHAR *text, LONG nChars)
535 {
536   char buffer[STREAMOUT_BUFFER_SIZE];
537   int pos = 0;
538   int fit, i;
539
540   if (nChars == -1)
541     nChars = lstrlenW(text);
542   
543   while (nChars) {
544     if (editor->pStream->nCodePage == CP_UTF8) {
545       /* 6 is the maximum character length in UTF-8 */
546       fit = min(nChars, STREAMOUT_BUFFER_SIZE / 6);
547       WideCharToMultiByte(CP_UTF8, 0, text, fit, buffer, STREAMOUT_BUFFER_SIZE,
548                           NULL, NULL);
549       nChars -= fit;
550       text += fit;
551       for (i = 0; buffer[i]; i++)
552         if (buffer[i] == '{' || buffer[i] == '}' || buffer[i] == '\\') {
553           if (!ME_StreamOutPrint(editor, "%.*s\\", i - pos, buffer + pos))
554             return FALSE;
555           pos = i;
556         }
557       if (!pos)
558         if (!ME_StreamOutPrint(editor, "%s", buffer + pos))
559           return FALSE;
560       pos = 0;
561     } else if (*text < 128) {
562       if (*text == '{' || *text == '}' || *text == '\\')
563         buffer[pos++] = '\\';
564       buffer[pos++] = (char)(*text++);
565       nChars--;
566     } else {
567       BOOL unknown;
568       BYTE letter[2];
569       
570       WideCharToMultiByte(editor->pStream->nCodePage, 0, text, 1, letter, 2,
571                           NULL, &unknown);
572       if (unknown)
573         pos += sprintf(buffer + pos, "\\u%d?", (int)*text);
574       else if (*letter < 128) {
575         if (*letter == '{' || *letter == '}' || *letter == '\\')
576           buffer[pos++] = '\\';
577         buffer[pos++] = *letter;
578       } else
579         pos += sprintf(buffer + pos, "\\'%02x", *letter);
580       text++;
581       nChars--;
582     }
583     if (pos >= STREAMOUT_BUFFER_SIZE - 10) {
584       if (!ME_StreamOutMove(editor, buffer, pos))
585         return FALSE;
586       pos = 0;
587     }
588   }
589   return ME_StreamOutMove(editor, buffer, pos);
590 }
591
592
593 static BOOL
594 ME_StreamOutRTF(ME_TextEditor *editor, int nStart, int nChars, int dwFormat)
595 {
596   ME_DisplayItem *p, *pEnd;
597   int nOffset, nEndLen;
598   ME_RunOfsFromCharOfs(editor, nStart, &p, &nOffset);
599   ME_RunOfsFromCharOfs(editor, nStart+nChars, &pEnd, &nEndLen);
600   
601   if (!ME_StreamOutRTFHeader(editor, dwFormat))
602     return FALSE;
603
604   if (!ME_StreamOutRTFFontAndColorTbl(editor, p, pEnd))
605     return FALSE;
606   
607   /* TODO: stylesheet table */
608   
609   /* FIXME: maybe emit something smarter for the generator? */
610   if (!ME_StreamOutPrint(editor, "{\\*\\generator Wine Riched20 2.0.????;}"))
611     return FALSE;
612
613   /* TODO: information group */
614
615   /* TODO: document formatting properties */
616
617   /* FIXME: We have only one document section */
618
619   /* TODO: section formatting properties */
620
621   if (!ME_StreamOutRTFParaProps(editor, ME_GetParagraph(p)))
622     return FALSE;
623
624   while(1)
625   {
626     switch(p->type)
627     {
628       case diParagraph:
629         if (!ME_StreamOutRTFParaProps(editor, p))
630           return FALSE;
631         break;
632       case diRun:
633         if (p == pEnd && !nEndLen)
634           break;
635         TRACE("flags %xh\n", p->member.run.nFlags);
636         /* TODO: emit embedded objects */
637         if (p->member.run.nFlags & MERF_GRAPHICS)
638           FIXME("embedded objects are not handled\n");
639         if (p->member.run.nFlags & MERF_ENDPARA) {
640           if (!ME_StreamOutPrint(editor, "\r\n\\par"))
641             return FALSE;
642           nChars--;
643         } else {
644           int nEnd;
645           
646           if (!ME_StreamOutPrint(editor, "{"))
647             return FALSE;
648           TRACE("style %p\n", p->member.run.style);
649           if (!ME_StreamOutRTFCharProps(editor, &p->member.run.style->fmt))
650             return FALSE;
651         
652           nEnd = (p == pEnd) ? nEndLen : ME_StrLen(p->member.run.strText);
653           if (!ME_StreamOutRTFText(editor, p->member.run.strText->szData + nOffset, nEnd - nOffset))
654             return FALSE;
655           nOffset = 0;
656           if (!ME_StreamOutPrint(editor, "}"))
657             return FALSE;
658         }
659         break;
660       default: /* we missed the last item */
661         assert(0);
662     }
663     if (p == pEnd)
664       break;
665     p = ME_FindItemFwd(p, diRunOrParagraphOrEnd);
666   }
667   if (!ME_StreamOutPrint(editor, "}"))
668     return FALSE;
669   return TRUE;
670 }
671
672
673 static BOOL
674 ME_StreamOutText(ME_TextEditor *editor, int nStart, int nChars, DWORD dwFormat)
675 {
676   /* FIXME: use ME_RunOfsFromCharOfs */
677   ME_DisplayItem *item = ME_FindItemAtOffset(editor, diRun, nStart, &nStart);
678   int nLen;
679   UINT nCodePage = CP_ACP;
680   BYTE *buffer = NULL;
681   int nBufLen = 0;
682   BOOL success = TRUE;
683
684   if (!item)
685     return FALSE;
686    
687   if (dwFormat & SF_USECODEPAGE)
688     nCodePage = HIWORD(dwFormat);
689
690   /* TODO: Handle SF_TEXTIZED */
691   
692   while (success && nChars && item) {
693     nLen = ME_StrLen(item->member.run.strText) - nStart;
694     if (nLen > nChars)
695       nLen = nChars;
696
697     if (item->member.run.nFlags & MERF_ENDPARA) {
698       WCHAR szEOL[] = { '\r', '\n' };
699       
700       if (dwFormat & SF_UNICODE)
701         success = ME_StreamOutMove(editor, (BYTE *)szEOL, 4);
702       else
703         success = ME_StreamOutMove(editor, "\r\n", 2);
704     } else {
705       if (dwFormat & SF_UNICODE)
706         success = ME_StreamOutMove(editor, (BYTE *)(item->member.run.strText->szData + nStart),
707                                    sizeof(WCHAR) * nLen);
708       else {
709         int nSize;
710
711         nSize = WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
712                                     nLen, NULL, 0, NULL, NULL);
713         if (nSize > nBufLen) {
714           if (buffer)
715             FREE_OBJ(buffer);
716           buffer = ALLOC_N_OBJ(BYTE, nSize);
717           nBufLen = nSize;
718         }
719         WideCharToMultiByte(nCodePage, 0, item->member.run.strText->szData + nStart,
720                             nLen, buffer, nSize, NULL, NULL);
721         success = ME_StreamOutMove(editor, buffer, nSize - 1);
722       }
723     }
724     
725     nChars -= nLen;
726     nStart = 0;
727     item = ME_FindItemFwd(item, diRun);
728   }
729   
730   if (buffer)
731     FREE_OBJ(buffer);
732   return success;
733 }
734
735
736 LRESULT
737 ME_StreamOut(ME_TextEditor *editor, DWORD dwFormat, EDITSTREAM *stream)
738 {
739   int nStart, nTo;
740   
741   ME_StreamOutInit(editor, stream);
742
743   if (dwFormat & SFF_SELECTION)
744     ME_GetSelection(editor, &nStart, &nTo);
745   else {
746     nStart = 0;
747     nTo = -1;
748   }
749   if (nTo == -1)
750     nTo = ME_GetTextLength(editor);
751   TRACE("from %d to %d\n", nStart, nTo);
752   
753   if (dwFormat & SF_RTF || dwFormat & SF_RTFNOOBJS)
754     ME_StreamOutRTF(editor, nStart, nTo - nStart, dwFormat);
755   else if (dwFormat & SF_TEXT || dwFormat & SF_TEXTIZED)
756     ME_StreamOutText(editor, nStart, nTo - nStart, dwFormat);
757   
758   ME_StreamOutFlush(editor);
759   return ME_StreamOutFree(editor);
760 }