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