]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
wxTextFile::Close() implemented
[wxWidgets.git] / user / wxLayout / 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 "Mpch.h"
14 #ifdef M_PREFIX
15 # include "gui/wxllist.h"
16 # include "gui/wxlparser.h"
17 #else
18 # include "wxllist.h"
19 # include "wxlparser.h"
20 #endif
21
22 #define BASE_SIZE 12
23
24 inline static bool IsEndOfLine(const char *p, int mode)
25 {
26 // in addition to Unix EOL convention we also (but not instead) understand
27 // the DOS one under Windows
28 return
29 ((mode & WXLO_EXPORT_WITH_MASK) == WXLO_EXPORT_WITH_CRLF) ?
30 ((*p == '\r') && (*(p + 1) == '\n'))
31 :
32 (((*p == '\r') && (*(p + 1) == '\n'))||(*p == '\n'));
33 }
34
35 void wxLayoutImportText(wxLayoutList *list, wxString const &str, int withflag)
36 {
37 char * cptr = (char *)str.c_str(); // string gets changed only temporarily
38 const char * begin = cptr;
39 char backup;
40
41 for(;;)
42 {
43 begin = cptr;
44 while( *cptr && !IsEndOfLine(cptr, withflag) )
45 cptr++;
46 backup = *cptr;
47 *cptr = '\0';
48 list->Insert(begin);
49 *cptr = backup;
50
51 // check if it's the end of this line
52 if ( IsEndOfLine(cptr, withflag) )
53 {
54 // if it was "\r\n", skip the following '\n'
55 if ( *cptr == '\r' )
56 cptr++;
57 list->LineBreak();
58 }
59 else if(backup == '\0') // reached end of string
60 break;
61 cptr++;
62 }
63 }
64
65 static
66 wxString wxLayoutExportCmdAsHTML(wxLayoutObjectCmd const & cmd,
67 wxLayoutStyleInfo *styleInfo)
68 {
69 static char buffer[20];
70 wxString html;
71
72 wxLayoutStyleInfo si;
73 cmd.GetStyle(&si);
74
75 int size, sizecount;
76
77 html += "<font ";
78
79 html +="color=";
80 sprintf(buffer,"\"#%02X%02X%02X\"", si.fg_red,si.fg_green,si.fg_blue);
81 html += buffer;
82
83
84 html += " bgcolor=";
85 sprintf(buffer,"\"#%02X%02X%02X\"", si.bg_red,si.bg_green,si.bg_blue);
86 html += buffer;
87
88 switch(si.family)
89 {
90 case wxSWISS:
91 case wxMODERN:
92 html += " face=\"Arial,Helvetica\""; break;
93 case wxROMAN:
94 html += " face=\"Times New Roman, Times\""; break;
95 case wxTELETYPE:
96 html += " face=\"Courier New, Courier\""; break;
97 default:
98 ;
99 }
100
101 size = BASE_SIZE; sizecount = 0;
102 while(size < si.size && sizecount < 5)
103 {
104 sizecount ++;
105 size = (size*12)/10;
106 }
107 while(size > si.size && sizecount > -5)
108 {
109 sizecount --;
110 size = (size*10)/12;
111 }
112 html += "size=";
113 sprintf(buffer,"%+1d", sizecount);
114 html += buffer;
115
116 html +=">";
117
118 if(styleInfo != NULL)
119 html ="</font>"+html; // terminate any previous font command
120
121 if((si.weight == wxBOLD) && ( (!styleInfo) || (styleInfo->weight != wxBOLD)))
122 html += "<b>";
123 else
124 if(si.weight != wxBOLD && ( styleInfo && (styleInfo->weight == wxBOLD)))
125 html += "</b>";
126
127 if(si.style == wxSLANT)
128 si.style = wxITALIC; // the same for html
129
130 if((si.style == wxITALIC) && ( (!styleInfo) || (styleInfo->style != wxITALIC)))
131 html += "<i>";
132 else
133 if(si.style != wxITALIC && ( styleInfo && (styleInfo->style == wxITALIC)))
134 html += "</i>";
135
136 if(si.underline && ( (!styleInfo) || ! styleInfo->underline))
137 html += "<u>";
138 else if(si.underline == false && ( styleInfo && styleInfo->underline))
139 html += "</u>";
140
141
142 *styleInfo = si; // update last style info
143
144 return html;
145 }
146
147
148
149 #define WXLO_IS_TEXT(type) \
150 ( type == WXLO_TYPE_TEXT \
151 || (type == WXLO_TYPE_CMD \
152 && (mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML))
153
154
155
156 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
157 int mode)
158 {
159 wxASSERT(status);
160 wxLayoutExportObject * export;
161
162 if(status->m_iterator == NULLIT) // end of line
163 {
164 if(!status->m_line || status->m_line->GetNextLine() == NULL) // reached end of list
165 return NULL;
166 else
167 {
168 status->m_line = status->m_line->GetNextLine();
169 status->m_iterator = status->m_line->GetFirstObject();
170 export = new wxLayoutExportObject();;
171 if( (mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_OBJECTS) // simple case
172 {
173 export->type = WXLO_EXPORT_OBJECT;
174 export->content.object = *status->m_iterator;
175 status->m_iterator++;
176 return export;
177 }
178 //else: text object:
179 export->type = ((mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML)
180 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
181 if((mode & WXLO_EXPORT_WITH_CRLF) == WXLO_EXPORT_WITH_CRLF)
182 export->content.text = new wxString("\r\n");
183 else
184 export->content.text = new wxString("\n");
185 return export;
186 }
187 }
188
189 export = new wxLayoutExportObject();
190 wxLayoutObjectType type = (** status->m_iterator).GetType();
191 if( (mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
192 {
193 export->type = WXLO_EXPORT_OBJECT;
194 export->content.object = *status->m_iterator;
195 status->m_iterator++;
196 return export;
197 }
198
199 // else: must be text
200 wxString *str = new wxString();
201 // text must be concatenated
202 do
203 {
204 switch(type)
205 {
206 case WXLO_TYPE_TEXT:
207 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
208 break;
209 case WXLO_TYPE_CMD:
210 wxASSERT_MSG( (mode&WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML,
211 "reached cmd object in text mode" );
212
213 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
214 *)*status->m_iterator, & status->m_si);
215 break;
216 default: // ignore icons
217 ;
218 }
219 status->m_iterator++;
220 if(status->m_iterator == NULLIT) // end of line!
221 {
222 if((mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML)
223 *str += "<br>";
224 if((mode & WXLO_EXPORT_WITH_CRLF) == WXLO_EXPORT_WITH_CRLF)
225 *str += "\r\n";
226 else
227 *str += '\n';
228 status->m_line = status->m_line->GetNextLine();
229 if(status->m_line)
230 status->m_iterator = status->m_line->GetFirstObject();
231 else
232 status->m_iterator = NULLIT;
233 }
234 if(status->m_iterator != NULLIT)
235 type = (** status->m_iterator).GetType();
236 else
237 break;
238 }
239 while(WXLO_IS_TEXT(type));
240
241 export->type = ((mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML)
242 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
243 export->content.text = str;
244 return export;
245 }
246