]> git.saurik.com Git - wxWidgets.git/blob - user/wxLayout/wxlparser.cpp
Scroll to cursor works correctly now.
[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 {
153 list->GetDefaults()->GetStyle(&m_si);
154 m_line = list->GetFirstLine();
155 m_iterator = m_line->GetFirstObject();
156 }
157
158
159
160 #define WXLO_IS_TEXT(type) \
161 ( type == WXLO_TYPE_TEXT \
162 || (type == WXLO_TYPE_CMD \
163 && mode == WXLO_EXPORT_AS_HTML))
164
165
166 wxLayoutExportObject *wxLayoutExport(wxLayoutExportStatus *status,
167 int mode, int flags)
168 {
169 wxASSERT(status);
170 wxLayoutExportObject * export;
171
172 if(status->m_iterator == NULLIT) // end of line
173 {
174 if(!status->m_line || status->m_line->GetNextLine() == NULL)
175 // reached end of list
176 return NULL;
177 }
178 export = new wxLayoutExportObject();
179 wxLayoutObjectType type;
180 if(status->m_iterator != NULLIT)
181 {
182 type = (** status->m_iterator).GetType();
183 if( mode == 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
192 { // iterator == NULLIT
193 if(mode == WXLO_EXPORT_AS_OBJECTS)
194 {
195 export->type = WXLO_EXPORT_EMPTYLINE;
196 export->content.object = NULL; //empty line
197 status->m_line = status->m_line->GetNextLine();
198 if(status->m_line)
199 status->m_iterator = status->m_line->GetFirstObject();
200 return export;
201 }
202 else
203 type = WXLO_TYPE_TEXT;
204 }
205
206 wxString *str = new wxString();
207 // text must be concatenated
208 for(;;)
209 {
210 while(status->m_iterator == NULLIT)
211 {
212 if(flags & WXLO_EXPORT_AS_HTML)
213 *str += "<br>";
214 if(flags & WXLO_EXPORT_WITH_CRLF)
215 *str += "\r\n";
216 else
217 *str += '\n';
218
219 status->m_line = status->m_line->GetNextLine();
220 if(status->m_line)
221 status->m_iterator = status->m_line->GetFirstObject();
222 else
223 break; // end of list
224 }
225 if(! status->m_line) // reached end of list, fall through
226 break;
227 type = (** status->m_iterator).GetType();
228 if(type == WXLO_TYPE_ICON)
229 break;
230 switch(type)
231 {
232 case WXLO_TYPE_TEXT:
233 *str += ((wxLayoutObjectText *)*status->m_iterator)->GetText();
234 break;
235 case WXLO_TYPE_CMD:
236 wxASSERT_MSG( mode == WXLO_EXPORT_AS_HTML,
237 "reached cmd object in text mode" );
238
239 *str += wxLayoutExportCmdAsHTML(*(wxLayoutObjectCmd const
240 *)*status->m_iterator, & status->m_si);
241 break;
242 default: // ignore icons
243 ;
244 }
245 status->m_iterator++;
246 }
247
248 export->type = (mode == WXLO_EXPORT_AS_HTML)
249 ? WXLO_EXPORT_HTML : WXLO_EXPORT_TEXT;
250 export->content.text = str;
251 return export;
252 }
253