Nuke #pragma implementation/interface's
[wxWidgets.git] / samples / richedit / wxlparser.cpp
1 /*-*- c++ -*-********************************************************
2 * wxlparser.h : parsers, import/export for wxLayoutList *
3 * *
4 * (C) 1998,1999 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$
7 *******************************************************************/
8
9 #include "wx/wxprec.h"
10
11 #ifdef __BORLANDC__
12 # pragma hdrstop
13 #endif
14
15 #include "Mpch.h"
16
17 #ifdef M_PREFIX
18 # include "gui/wxllist.h"
19 # include "gui/wxlparser.h"
20 #else
21 # include "wxllist.h"
22 # include "wxlparser.h"
23 #endif
24
25 #define BASE_SIZE 12
26
27 inline static bool IsEndOfLine(const wxChar *p)
28 {
29 // the end of line is either just '\n' or "\r\n" - we understand both (even
30 // though the second is used only under DOS/Windows) to be able to import
31 // DOS text files even under Unix
32 return (*p == '\n') || ((*p == '\r') && (*(p + 1) == '\n'));
33 }
34
35 void wxLayoutImportText(wxLayoutList *list, wxString const &str)
36 {
37 if ( !str )
38 return;
39
40 // we change the string only temporarily inside this function
41 // VZ: I still don't like it... the string data may be shared...
42 wxChar * cptr = (wxChar *)str.c_str(); // const_cast
43 const wxChar * begin = cptr;
44 wxUnusedVar(begin);
45 wxChar backup;
46
47 for(;;)
48 {
49 begin = cptr;
50 while( *cptr && !IsEndOfLine(cptr) )
51 cptr++;
52 backup = *cptr;
53 *cptr = '\0';
54 list->Insert(begin);
55 *cptr = backup;
56
57 // check if it's the end of this line
58 if ( IsEndOfLine(cptr) )
59 {
60 // if it was "\r\n", skip the following '\n'
61 if ( *cptr == '\r' )
62 cptr++;
63 list->LineBreak();
64 }
65 else if(backup == '\0') // reached end of string
66 break;
67 cptr++;
68 }
69 }
70
71 static
72 wxString wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
73 wxLayoutStyleInfo *styleInfo,
74 bool firstTime)
75 {
76 static wxChar buffer[20];
77 wxString html;
78
79 wxLayoutStyleInfo *si = cmd.GetStyle();
80
81 int size, sizecount;
82
83 html += _T("<font ");
84
85 if(si->m_fg_valid)
86 {
87 html += _T("color=");
88 wxSprintf(buffer,_T("\"#%02X%02X%02X\""), si->m_fg.Red(),si->m_fg.Green(),si->m_fg.Blue());
89 html += buffer;
90 }
91
92 if(si->m_bg_valid)
93 {
94 html += _T(" bgcolor=");
95 wxSprintf(buffer,_T("\"#%02X%02X%02X\""), si->m_bg.Red(),si->m_bg.Green(),si->m_bg.Blue());
96 html += buffer;
97 }
98
99 switch(si->family)
100 {
101 case wxSWISS:
102 case wxMODERN:
103 html += _T(" face=\"Arial,Helvetica\""); break;
104 case wxROMAN:
105 html += _T(" face=\"Times New Roman, Times\""); break;
106 case wxTELETYPE:
107 html += _T(" face=\"Courier New, Courier\""); break;
108 default:
109 ;
110 }
111
112 size = BASE_SIZE; sizecount = 0;
113 while(size < si->size && sizecount < 5)
114 {
115 sizecount ++;
116 size = (size*12)/10;
117 }
118 while(size > si->size && sizecount > -5)
119 {
120 sizecount --;
121 size = (size*10)/12;
122 }
123 html += _T("size=");
124 wxSprintf(buffer,_T("%+1d"), sizecount);
125 html += buffer;
126
127 html += _T(">");
128
129 if(styleInfo != NULL && ! firstTime)
130 html = _T("</font>")+html; // terminate any previous font command
131
132 if((si->weight == wxBOLD) && ( (!styleInfo) || (styleInfo->weight != wxBOLD)))
133 html += _T("<b>");
134 else
135 if(si->weight != wxBOLD && ( styleInfo && (styleInfo->weight == wxBOLD)))
136 html += _T("</b>");
137
138 if(si->style == wxSLANT)
139 si->style = wxITALIC; // the same for html
140
141 if((si->style == wxITALIC) && ( (!styleInfo) || (styleInfo->style != wxITALIC)))
142 html += _T("<i>");
143 else
144 if(si->style != wxITALIC && ( styleInfo && (styleInfo->style == wxITALIC)))
145 html += _T("</i>");
146
147 if(si->underline && ( (!styleInfo) || ! styleInfo->underline))
148 html += _T("<u>");
149 else if(si->underline == false && ( styleInfo && styleInfo->underline))
150 html += _T("</u>");
151
152
153 *styleInfo = *si; // update last style info
154
155 return html;
156 }
157
158
159
160 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList *list)
161 {
162 m_si = list->GetDefaultStyleInfo();
163 m_line = list->GetFirstLine();
164 m_iterator = m_line->GetFirstObject();
165 m_FirstTime = true;
166 }
167
168
169
170 #define WXLO_IS_TEXT(type) \
171 ( type == WXLO_TYPE_TEXT \
172 || (type == WXLO_TYPE_CMD \
173 && mode == WXLO_EXPORT_AS_HTML))
174
175
176 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
177 int mode, int flags)
178 {
179 wxLayoutObjectList::iterator nulled(NULL);
180 wxASSERT(status);
181 wxLayoutExportObject * exp;
182
183 if(status->m_iterator == nulled) // end of line
184 {
185 if(!status->m_line || status->m_line->GetNextLine() == NULL)
186 // reached end of list
187 return NULL;
188 }
189 exp = new wxLayoutExportObject();
190 wxLayoutObjectType type;
191 if(status->m_iterator != nulled)
192 {
193 type = (** status->m_iterator).GetType();
194 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
195 {
196 exp->type = WXLO_EXPORT_OBJECT;
197 exp->content.object = *status->m_iterator;
198 status->m_iterator++;
199 return exp;
200 }
201 }
202 else
203 { // iterator == nulled
204 if(mode == WXLO_EXPORT_AS_OBJECTS)
205 {
206 exp->type = WXLO_EXPORT_EMPTYLINE;
207 exp->content.object = NULL; //empty line
208 status->m_line = status->m_line->GetNextLine();
209 if(status->m_line)
210 status->m_iterator = status->m_line->GetFirstObject();
211 return exp;
212 }
213 else
214 type = WXLO_TYPE_TEXT;
215 }
216 wxUnusedVar(type);
217
218 wxString *str = new wxString();
219 // text must be concatenated
220 for(;;)
221 {
222 while(status->m_iterator == nulled)
223 {
224 if(mode & WXLO_EXPORT_AS_HTML)
225 *str += _T("<br>");
226 if(flags & WXLO_EXPORT_WITH_CRLF)
227 *str += _T("\r\n");
228 else
229 *str += '\n';
230
231 status->m_line = status->m_line->GetNextLine();
232 if(status->m_line)
233 status->m_iterator = status->m_line->GetFirstObject();
234 else
235 break; // end of list
236 }
237 if(! status->m_line) // reached end of list, fall through
238 break;
239 type = (** status->m_iterator).GetType();
240 if(type == WXLO_TYPE_ICON)
241 break;
242 switch(type)
243 {
244 case WXLO_TYPE_TEXT:
245 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
246 break;
247 case WXLO_TYPE_CMD:
248 if(mode == WXLO_EXPORT_AS_HTML)
249 *str += wxLayoutExportCmdAsHTML(
250 *(wxLayoutObjectCmd const *)*status->m_iterator,
251 & status->m_si, status->m_FirstTime);
252 status->m_FirstTime = false;
253 break;
254 default: // ignore icons
255 ;
256 }
257 status->m_iterator++;
258 }
259 exp->type = (mode == WXLO_EXPORT_AS_HTML)
260 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
261 exp->content.text = str;
262 return exp;
263 }
264