]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlparser.cpp
*** empty log message ***
[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 {
78 static char buffer[20];
79 wxString html;
80
81 wxLayoutStyleInfo *si = cmd.GetStyle();
82
83 int size, sizecount;
84
85 html += "<font ";
86
87 if(si->m_fg_valid)
88 {
89 html +="color=";
90 sprintf(buffer,"\"#%02X%02X%02X\"", si->m_fg.Red(),si->m_fg.Green(),si->m_fg.Blue());
91 html += buffer;
92 }
93
94 if(si->m_bg_valid)
95 {
96 html += " bgcolor=";
97 sprintf(buffer,"\"#%02X%02X%02X\"", si->m_bg.Red(),si->m_bg.Green(),si->m_bg.Blue());
98 html += buffer;
99 }
100
101 switch(si->family)
102 {
103 case wxSWISS:
104 case wxMODERN:
105 html += " face=\"Arial,Helvetica\""; break;
106 case wxROMAN:
107 html += " face=\"Times New Roman, Times\""; break;
108 case wxTELETYPE:
109 html += " face=\"Courier New, Courier\""; break;
110 default:
111 ;
112 }
113
114 size = BASE_SIZE; sizecount = 0;
115 while(size < si->size && sizecount < 5)
116 {
117 sizecount ++;
118 size = (size*12)/10;
119 }
120 while(size > si->size && sizecount > -5)
121 {
122 sizecount --;
123 size = (size*10)/12;
124 }
125 html += "size=";
126 sprintf(buffer,"%+1d", sizecount);
127 html += buffer;
128
129 html +=">";
130
131 if(styleInfo != NULL)
132 html ="</font>"+html; // terminate any previous font command
133
134 if((si->weight == wxBOLD) && ( (!styleInfo) || (styleInfo->weight != wxBOLD)))
135 html += "<b>";
136 else
137 if(si->weight != wxBOLD && ( styleInfo && (styleInfo->weight == wxBOLD)))
138 html += "</b>";
139
140 if(si->style == wxSLANT)
141 si->style = wxITALIC; // the same for html
142
143 if((si->style == wxITALIC) && ( (!styleInfo) || (styleInfo->style != wxITALIC)))
144 html += "<i>";
145 else
146 if(si->style != wxITALIC && ( styleInfo && (styleInfo->style == wxITALIC)))
147 html += "</i>";
148
149 if(si->underline && ( (!styleInfo) || ! styleInfo->underline))
150 html += "<u>";
151 else if(si->underline == false && ( styleInfo && styleInfo->underline))
152 html += "</u>";
153
154
155 *styleInfo = *si; // update last style info
156
157 return html;
158 }
159
160
161
162 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList *list)
163 {
164 m_si = list->GetDefaultStyleInfo();
165 m_line = list->GetFirstLine();
166 m_iterator = m_line->GetFirstObject();
167 }
168
169
170
171 #define WXLO_IS_TEXT(type) \
172 ( type == WXLO_TYPE_TEXT \
173 || (type == WXLO_TYPE_CMD \
174 && mode == WXLO_EXPORT_AS_HTML))
175
176
177 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
178 int mode, int flags)
179 {
180 wxASSERT(status);
181 wxLayoutExportObject * exp;
182
183 if(status->m_iterator == NULLIT) // 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 != NULLIT)
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 == NULLIT
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
217 wxString *str = new wxString();
218 // text must be concatenated
219 for(;;)
220 {
221 while(status->m_iterator == NULLIT)
222 {
223 if(flags & WXLO_EXPORT_AS_HTML)
224 *str += "<br>";
225 if(flags & WXLO_EXPORT_WITH_CRLF)
226 *str += "\r\n";
227 else
228 *str += '\n';
229
230 status->m_line = status->m_line->GetNextLine();
231 if(status->m_line)
232 status->m_iterator = status->m_line->GetFirstObject();
233 else
234 break; // end of list
235 }
236 if(! status->m_line) // reached end of list, fall through
237 break;
238 type = (** status->m_iterator).GetType();
239 if(type == WXLO_TYPE_ICON)
240 break;
241 switch(type)
242 {
243 case WXLO_TYPE_TEXT:
244 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
245 break;
246 case WXLO_TYPE_CMD:
247 if(mode == WXLO_EXPORT_AS_HTML)
248 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
249 *)*status->m_iterator, & status->m_si);
250 break;
251 default: // ignore icons
252 ;
253 }
254 status->m_iterator++;
255 }
256
257 exp->type = (mode == WXLO_EXPORT_AS_HTML)
258 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
259 exp->content.text = str;
260 return exp;
261 }
262