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