]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/wxlparser.cpp
Added wxStandardPaths
[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 wxChar *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 wxChar * cptr = (wxChar *)str.c_str(); // const_cast
47 const wxChar * begin = cptr;
48 wxUnusedVar(begin);
49 wxChar backup;
50
51 for(;;)
52 {
53 begin = cptr;
54 while( *cptr && !IsEndOfLine(cptr) )
55 cptr++;
56 backup = *cptr;
57 *cptr = '\0';
58 list->Insert(begin);
59 *cptr = backup;
60
61 // check if it's the end of this line
62 if ( IsEndOfLine(cptr) )
63 {
64 // if it was "\r\n", skip the following '\n'
65 if ( *cptr == '\r' )
66 cptr++;
67 list->LineBreak();
68 }
69 else if(backup == '\0') // reached end of string
70 break;
71 cptr++;
72 }
73 }
74
75 static
76 wxString wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
77 wxLayoutStyleInfo *styleInfo,
78 bool firstTime)
79 {
80 static wxChar buffer[20];
81 wxString html;
82
83 wxLayoutStyleInfo *si = cmd.GetStyle();
84
85 int size, sizecount;
86
87 html += _T("<font ");
88
89 if(si->m_fg_valid)
90 {
91 html += _T("color=");
92 wxSprintf(buffer,_T("\"#%02X%02X%02X\""), si->m_fg.Red(),si->m_fg.Green(),si->m_fg.Blue());
93 html += buffer;
94 }
95
96 if(si->m_bg_valid)
97 {
98 html += _T(" bgcolor=");
99 wxSprintf(buffer,_T("\"#%02X%02X%02X\""), si->m_bg.Red(),si->m_bg.Green(),si->m_bg.Blue());
100 html += buffer;
101 }
102
103 switch(si->family)
104 {
105 case wxSWISS:
106 case wxMODERN:
107 html += _T(" face=\"Arial,Helvetica\""); break;
108 case wxROMAN:
109 html += _T(" face=\"Times New Roman, Times\""); break;
110 case wxTELETYPE:
111 html += _T(" face=\"Courier New, Courier\""); break;
112 default:
113 ;
114 }
115
116 size = BASE_SIZE; sizecount = 0;
117 while(size < si->size && sizecount < 5)
118 {
119 sizecount ++;
120 size = (size*12)/10;
121 }
122 while(size > si->size && sizecount > -5)
123 {
124 sizecount --;
125 size = (size*10)/12;
126 }
127 html += _T("size=");
128 wxSprintf(buffer,_T("%+1d"), sizecount);
129 html += buffer;
130
131 html += _T(">");
132
133 if(styleInfo != NULL && ! firstTime)
134 html = _T("</font>")+html; // terminate any previous font command
135
136 if((si->weight == wxBOLD) && ( (!styleInfo) || (styleInfo->weight != wxBOLD)))
137 html += _T("<b>");
138 else
139 if(si->weight != wxBOLD && ( styleInfo && (styleInfo->weight == wxBOLD)))
140 html += _T("</b>");
141
142 if(si->style == wxSLANT)
143 si->style = wxITALIC; // the same for html
144
145 if((si->style == wxITALIC) && ( (!styleInfo) || (styleInfo->style != wxITALIC)))
146 html += _T("<i>");
147 else
148 if(si->style != wxITALIC && ( styleInfo && (styleInfo->style == wxITALIC)))
149 html += _T("</i>");
150
151 if(si->underline && ( (!styleInfo) || ! styleInfo->underline))
152 html += _T("<u>");
153 else if(si->underline == false && ( styleInfo && styleInfo->underline))
154 html += _T("</u>");
155
156
157 *styleInfo = *si; // update last style info
158
159 return html;
160 }
161
162
163
164 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList *list)
165 {
166 m_si = list->GetDefaultStyleInfo();
167 m_line = list->GetFirstLine();
168 m_iterator = m_line->GetFirstObject();
169 m_FirstTime = true;
170 }
171
172
173
174 #define WXLO_IS_TEXT(type) \
175 ( type == WXLO_TYPE_TEXT \
176 || (type == WXLO_TYPE_CMD \
177 && mode == WXLO_EXPORT_AS_HTML))
178
179
180 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
181 int mode, int flags)
182 {
183 wxLayoutObjectList::iterator nulled(NULL);
184 wxASSERT(status);
185 wxLayoutExportObject * exp;
186
187 if(status->m_iterator == nulled) // end of line
188 {
189 if(!status->m_line || status->m_line->GetNextLine() == NULL)
190 // reached end of list
191 return NULL;
192 }
193 exp = new wxLayoutExportObject();
194 wxLayoutObjectType type;
195 if(status->m_iterator != nulled)
196 {
197 type = (** status->m_iterator).GetType();
198 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
199 {
200 exp->type = WXLO_EXPORT_OBJECT;
201 exp->content.object = *status->m_iterator;
202 status->m_iterator++;
203 return exp;
204 }
205 }
206 else
207 { // iterator == nulled
208 if(mode == WXLO_EXPORT_AS_OBJECTS)
209 {
210 exp->type = WXLO_EXPORT_EMPTYLINE;
211 exp->content.object = NULL; //empty line
212 status->m_line = status->m_line->GetNextLine();
213 if(status->m_line)
214 status->m_iterator = status->m_line->GetFirstObject();
215 return exp;
216 }
217 else
218 type = WXLO_TYPE_TEXT;
219 }
220 wxUnusedVar(type);
221
222 wxString *str = new wxString();
223 // text must be concatenated
224 for(;;)
225 {
226 while(status->m_iterator == nulled)
227 {
228 if(mode & WXLO_EXPORT_AS_HTML)
229 *str += _T("<br>");
230 if(flags & WXLO_EXPORT_WITH_CRLF)
231 *str += _T("\r\n");
232 else
233 *str += '\n';
234
235 status->m_line = status->m_line->GetNextLine();
236 if(status->m_line)
237 status->m_iterator = status->m_line->GetFirstObject();
238 else
239 break; // end of list
240 }
241 if(! status->m_line) // reached end of list, fall through
242 break;
243 type = (** status->m_iterator).GetType();
244 if(type == WXLO_TYPE_ICON)
245 break;
246 switch(type)
247 {
248 case WXLO_TYPE_TEXT:
249 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
250 break;
251 case WXLO_TYPE_CMD:
252 if(mode == WXLO_EXPORT_AS_HTML)
253 *str += wxLayoutExportCmdAsHTML(
254 *(wxLayoutObjectCmd const *)*status->m_iterator,
255 & status->m_si, status->m_FirstTime);
256 status->m_FirstTime = false;
257 break;
258 default: // ignore icons
259 ;
260 }
261 status->m_iterator++;
262 }
263 exp->type = (mode == WXLO_EXPORT_AS_HTML)
264 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
265 exp->content.text = str;
266 return exp;
267 }
268