]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
804805cd6002fb2d540f69f65ae7df8ea67ddf77
[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 wxLayoutExportStatus::wxLayoutExportStatus(wxLayoutList *list,
152 wxPoint fromPos,
153 wxPoint toPos)
154 {
155 list->GetDefaults()->GetStyle(&m_si);
156 m_line = list->GetFirstLine();
157 m_iterator = m_line->GetFirstObject();
158 m_fromPos = fromPos;
159 m_toPos = toPos;
160
161 if(m_fromPos != wxLayoutExportNoPosition)
162 {
163 while(m_line && (*m_line)->GetLineNumber() != m_fromPos.y)
164 m_line->GetNextLine();
165 wxASSERT(m_line);
166 m_iterator = (**i).FindObject(fromPos.x);
167 }
168 }
169
170
171
172 #define WXLO_IS_TEXT(type) \
173 ( type == WXLO_TYPE_TEXT \
174 || (type == WXLO_TYPE_CMD \
175 && mode == WXLO_EXPORT_AS_HTML))
176
177
178
179 extern const wxPoint wxLayoutExportNoPosition = wxPoint(-1,-1);
180
181 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
182 int mode, int flags)
183 {
184 wxASSERT(status);
185 wxLayoutExportObject * export;
186
187 if(status->m_iterator == NULLIT) // end of line
188 {
189 if(!status->m_line || status->m_line->GetNextLine() == NULL)
190 // reached end of list
191 return NULL;
192 }
193 export = new wxLayoutExportObject();
194 wxLayoutObjectType type;
195 if(status->m_iterator != NULLIT)
196 {
197 type = (** status->m_iterator).GetType();
198 if( mode == WXLO_EXPORT_AS_OBJECTS || ! WXLO_IS_TEXT(type)) // simple case
199 {
200 export->type = WXLO_EXPORT_OBJECT;
201 export->content.object = *status->m_iterator;
202 status->m_iterator++;
203 return export;
204 }
205 }
206 else
207 { // iterator == NULLIT
208 if(mode == WXLO_EXPORT_AS_OBJECTS)
209 {
210 export->type = WXLO_EXPORT_EMPTYLINE;
211 export->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 export;
216 }
217 else
218 type = WXLO_TYPE_TEXT;
219 }
220
221 wxString *str = new wxString();
222 // text must be concatenated
223 for(;;)
224 {
225 while(status->m_iterator == NULLIT)
226 {
227 if(flags & WXLO_EXPORT_AS_HTML)
228 *str += "<br>";
229 if(flags & WXLO_EXPORT_WITH_CRLF)
230 *str += "\r\n";
231 else
232 *str += '\n';
233
234 status->m_line = status->m_line->GetNextLine();
235 if(status->m_line)
236 status->m_iterator = status->m_line->GetFirstObject();
237 else
238 break; // end of list
239 }
240 if(! status->m_line) // reached end of list, fall through
241 break;
242 type = (** status->m_iterator).GetType();
243 if(type == WXLO_TYPE_ICON)
244 break;
245 switch(type)
246 {
247 case WXLO_TYPE_TEXT:
248 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
249 break;
250 case WXLO_TYPE_CMD:
251 wxASSERT_MSG( mode == WXLO_EXPORT_AS_HTML,
252 "reached cmd object in text mode" );
253
254 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
255 *)*status->m_iterator, & status->m_si);
256 break;
257 default: // ignore icons
258 ;
259 }
260 status->m_iterator++;
261 }
262
263 export->type = (mode == WXLO_EXPORT_AS_HTML)
264 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
265 export->content.text = str;
266 return export;
267 }
268