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