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