]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
6ebe3a5ce39cb24faa4333a2d829ee7c880d73cd
[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 export->type = ((mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML)
172 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
173 if((mode & WXLO_EXPORT_WITH_CRLF) == WXLO_EXPORT_WITH_CRLF)
174 export->content.text = new wxString("\r\n");
175 else
176 export->content.text = new wxString("\n");
177 return export;
178 }
179 }
180
181 export = new wxLayoutExportObject();
182 wxLayoutObjectType type = (** status->m_iterator).GetType();
183 if( (mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
184 {
185 export->type = WXLO_EXPORT_OBJECT;
186 export->content.object = *status->m_iterator;
187 status->m_iterator++;
188 return export;
189 }
190
191 // else: must be text
192 wxString *str = new wxString();
193 // text must be concatenated
194 do
195 {
196 switch(type)
197 {
198 case WXLO_TYPE_TEXT:
199 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
200 break;
201 case WXLO_TYPE_CMD:
202 wxASSERT_MSG( (mode&WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML,
203 "reached cmd object in text mode" );
204
205 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
206 *)*status->m_iterator, & status->m_si);
207 break;
208 default: // ignore icons
209 ;
210 }
211 status->m_iterator++;
212 if(status->m_iterator == NULLIT) // end of line!
213 {
214 if((mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML)
215 *str += "<br>";
216 if((mode & WXLO_EXPORT_WITH_CRLF) == WXLO_EXPORT_WITH_CRLF)
217 *str += "\r\n";
218 else
219 *str += '\n';
220 status->m_line = status->m_line->GetNextLine();
221 if(status->m_line)
222 status->m_iterator = status->m_line->GetFirstObject();
223 else
224 status->m_iterator = NULLIT;
225 }
226 if(status->m_iterator != NULLIT)
227 type = (** status->m_iterator).GetType();
228 else
229 break;
230 }
231 while(WXLO_IS_TEXT(type));
232
233 export->type = ((mode & WXLO_EXPORT_AS_MASK) == WXLO_EXPORT_AS_HTML)
234 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
235 export->content.text = str;
236 return export;
237 }
238